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 get_die_language(const Dwarf_Die *die, translation_unit::language &lang) ;
384 
385 static bool
386 die_is_in_c(const Dwarf_Die *die);
387 
388 static bool
389 die_is_in_cplus_plus(const Dwarf_Die *die);
390 
391 static bool
392 die_is_in_c_or_cplusplus(const Dwarf_Die *die);
393 
394 static bool
395 die_is_anonymous(const Dwarf_Die* die);
396 
397 static bool
398 die_is_anonymous_data_member(const Dwarf_Die* die);
399 
400 static bool
401 die_is_type(const Dwarf_Die* die);
402 
403 static bool
404 die_is_decl(const Dwarf_Die* die);
405 
406 static bool
407 die_is_declaration_only(Dwarf_Die* die);
408 
409 static bool
410 die_is_variable_decl(const Dwarf_Die *die);
411 
412 static bool
413 die_is_function_decl(const Dwarf_Die *die);
414 
415 static bool
416 die_has_size_attribute(const Dwarf_Die *die);
417 
418 static bool
419 die_has_no_child(const Dwarf_Die *die);
420 
421 static bool
422 die_is_namespace(const Dwarf_Die* die);
423 
424 static bool
425 die_is_unspecified(Dwarf_Die* die);
426 
427 static bool
428 die_is_void_type(Dwarf_Die* die);
429 
430 static bool
431 die_is_pointer_type(const Dwarf_Die* die);
432 
433 static bool
434 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
435 
436 static bool
437 die_is_reference_type(const Dwarf_Die* die);
438 
439 static bool
440 die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
441 
442 static bool
443 die_is_pointer_or_reference_type(const Dwarf_Die* die);
444 
445 static bool
446 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
447 
448 static bool
449 die_is_class_type(const Dwarf_Die* die);
450 
451 static bool
452 die_is_qualified_type(const Dwarf_Die* die);
453 
454 static bool
455 die_is_function_type(const Dwarf_Die *die);
456 
457 static bool
458 die_has_object_pointer(const Dwarf_Die* die,
459  Dwarf_Die& object_pointer);
460 
461 static bool
462 die_has_children(const Dwarf_Die* die);
463 
464 static bool
465 die_this_pointer_from_object_pointer(Dwarf_Die* die,
466  Dwarf_Die& this_pointer);
467 
468 static bool
469 die_this_pointer_is_const(Dwarf_Die* die);
470 
471 static bool
472 die_object_pointer_is_for_const_method(Dwarf_Die* die);
473 
474 static bool
475 is_type_die_to_be_canonicalized(const Dwarf_Die *die);
476 
477 static bool
478 die_is_at_class_scope(const reader& rdr,
479  const Dwarf_Die* die,
480  size_t where_offset,
481  Dwarf_Die& class_scope_die);
482 static bool
483 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
484  size_t expr_len,
485  int64_t& value,
486  bool& is_tls_address);
487 
489 dwarf_language_to_tu_language(size_t l);
490 
491 static bool
492 die_unsigned_constant_attribute(const Dwarf_Die* die,
493  unsigned attr_name,
494  uint64_t& cst);
495 
496 static bool
497 die_signed_constant_attribute(const Dwarf_Die*die,
498  unsigned attr_name,
499  int64_t& cst);
500 
501 static bool
502 die_constant_attribute(const Dwarf_Die *die,
503  unsigned attr_name,
504  bool is_signed,
505  array_type_def::subrange_type::bound_value &value);
506 
507 static bool
508 die_member_offset(const reader& rdr,
509  const Dwarf_Die* die,
510  int64_t& offset);
511 
512 static bool
513 form_is_DW_FORM_strx(unsigned form);
514 
515 static bool
516 form_is_DW_FORM_line_strp(unsigned form);
517 
518 static bool
519 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
520 
521 static string
522 die_name(const Dwarf_Die* die);
523 
524 static void
525 die_name_and_linkage_name(const Dwarf_Die* die,
526  string& name,
527  string& linkage_name);
528 static location
529 die_location(const reader& rdr, const Dwarf_Die* die);
530 
531 static bool
532 die_location_address(Dwarf_Die* die,
533  Dwarf_Addr& address,
534  bool& is_tls_address);
535 
536 static bool
537 die_die_attribute(const Dwarf_Die* die,
538  unsigned attr_name,
539  Dwarf_Die& result,
540  bool recursively = true);
541 
542 static bool
543 die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
544 
545 static bool
546 subrange_die_indirect_bound_value(const Dwarf_Die *die,
547  unsigned attr_name,
548  array_type_def::subrange_type::bound_value& v,
549  bool& is_signed);
550 
551 static bool
552 subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
553  unsigned attr_name,
554  Dwarf_Die& referenced_subrange);
555 static string
556 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
557 
558 static string
559 build_internal_anonymous_die_name(const string &base_name,
560  size_t anonymous_type_index);
561 
562 static string
563 get_internal_anonymous_die_name(Dwarf_Die *die,
564  size_t anonymous_type_index);
565 
566 static string
567 die_qualified_type_name(const reader& rdr,
568  const Dwarf_Die* die,
569  size_t where);
570 
571 static string
572 die_qualified_decl_name(const reader& rdr,
573  const Dwarf_Die* die,
574  size_t where);
575 
576 static string
577 die_qualified_name(const reader& rdr,
578  const Dwarf_Die* die,
579  size_t where);
580 
581 static bool
582 die_qualified_type_name_empty(const reader& rdr,
583  const Dwarf_Die* die, size_t where,
584  string &qualified_name);
585 
586 static void
587 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
588  const Dwarf_Die* die,
589  size_t where_offset,
590  bool pretty_print,
591  string &return_type_name,
592  string &class_name,
593  vector<string>& parm_names,
594  bool& is_const,
595  bool& is_static);
596 
597 static string
598 die_function_signature(const reader& rdr,
599  const Dwarf_Die *die,
600  size_t where_offset);
601 
602 static bool
603 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
604 
605 static bool
606 die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
607 
608 static bool
609 die_function_type_is_method_type(const reader& rdr,
610  const Dwarf_Die *die,
611  size_t where_offset,
612  Dwarf_Die& object_pointer_die,
613  Dwarf_Die& class_die,
614  bool& is_static);
615 
616 static string
617 die_pretty_print_type(reader& rdr,
618  const Dwarf_Die* die,
619  size_t where_offset);
620 
621 static string
622 die_pretty_print_decl(reader& rdr,
623  const Dwarf_Die* die,
624  size_t where_offset);
625 
626 static string
627 die_pretty_print(reader& rdr,
628  const Dwarf_Die* die,
629  size_t where_offset);
630 
631 static void
632 maybe_canonicalize_type(const type_base_sptr& t,
633  reader& rdr);
634 
635 static uint64_t
636 get_default_array_lower_bound(translation_unit::language l);
637 
638 static bool
639 find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
640  Dwarf_Off,
641  imported_unit_points_type::const_iterator&);
642 
644 build_subrange_type(reader& rdr,
645  const Dwarf_Die* die,
646  size_t where_offset,
647  bool associate_type_to_die = true);
648 
649 static void
650 build_subranges_from_array_type_die(reader& rdr,
651  const Dwarf_Die* die,
653  size_t where_offset,
654  bool associate_type_to_die = true);
655 
656 static comparison_result
657 compare_dies(const reader& rdr,
658  const Dwarf_Die *l, const Dwarf_Die *r,
659  bool update_canonical_dies_on_the_fly);
660 
661 static bool
662 compare_dies_during_canonicalization(reader& rdr,
663  const Dwarf_Die *l, const Dwarf_Die *r,
664  bool update_canonical_dies_on_the_fly);
665 
666 static bool
667 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
668 
669 /// Get the language used to generate a given DIE.
670 ///
671 /// @param die the DIE to consider.
672 ///
673 /// @param lang the resulting language.
674 ///
675 /// @return true iff the language of the DIE was found.
676 static bool
677 get_die_language(const Dwarf_Die *die, translation_unit::language &lang)
678 {
679  Dwarf_Die cu_die;
680  ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
681 
682  uint64_t l = 0;
683  if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
684  return false;
685 
686  lang = dwarf_language_to_tu_language(l);
687  return true;
688 }
689 
690 /// Test if a given DIE originates from a program written in the C
691 /// language.
692 ///
693 /// @param die the DIE to consider.
694 ///
695 /// @return true iff @p die originates from a program in the C
696 /// language.
697 static bool
698 die_is_in_c(const Dwarf_Die *die)
699 {
700  translation_unit::language l = translation_unit::LANG_UNKNOWN;
701  if (!get_die_language(die, l))
702  return false;
703  return is_c_language(l);
704 }
705 
706 /// Test if a given DIE originates from a program written in the C++
707 /// language.
708 ///
709 /// @param die the DIE to consider.
710 ///
711 /// @return true iff @p die originates from a program in the C++
712 /// language.
713 static bool
714 die_is_in_cplus_plus(const Dwarf_Die *die)
715 {
716  translation_unit::language l = translation_unit::LANG_UNKNOWN;
717  if (!get_die_language(die, l))
718  return false;
719  return is_cplus_plus_language(l);
720 }
721 
722 /// Test if a given DIE originates from a program written either in
723 /// C or C++.
724 ///
725 /// @param die the DIE to consider.
726 ///
727 /// @return true iff @p die originates from a program written either in
728 /// C or C++.
729 static bool
730 die_is_in_c_or_cplusplus(const Dwarf_Die *die)
731 {
732  translation_unit::language l = translation_unit::LANG_UNKNOWN;
733  if (!get_die_language(die, l))
734  return false;
735  return (is_cplus_plus_language(l) || is_c_language(l));
736 }
737 
738 /// Compare a symbol name against another name, possibly demangling
739 /// the symbol_name before performing the comparison.
740 ///
741 /// @param symbol_name the symbol_name to take in account.
742 ///
743 /// @param name the second name to take in account.
744 ///
745 /// @param demangle if true, demangle @p symbol_name and compare the
746 /// result of the demangling with @p name.
747 ///
748 /// @return true iff symbol_name equals name.
749 static bool
750 compare_symbol_name(const string& symbol_name,
751  const string& name,
752  bool demangle)
753 {
754  if (demangle)
755  {
756  string m = demangle_cplus_mangled_name(symbol_name);
757  return m == name;
758  }
759  return symbol_name == name;
760 }
761 
762 /// Lookup a symbol using the SysV ELF hash table.
763 ///
764 /// Note that this function hasn't been tested. So it hasn't been
765 /// debugged yet. IOW, it is not known to work. Or rather, it's
766 /// almost like it's surely doesn't work ;-)
767 ///
768 /// Use it at your own risks. :-)
769 ///
770 ///@parm env the environment we are operating from.
771 ///
772 /// @param elf_handle the elf_handle to use.
773 ///
774 /// @param sym_name the symbol name to look for.
775 ///
776 /// @param ht_index the index (in the section headers table) of the
777 /// hash table section to use.
778 ///
779 /// @param sym_tab_index the index (in the section headers table) of
780 /// the symbol table to use.
781 ///
782 /// @param demangle if true, demangle @p sym_name before comparing it
783 /// to names from the symbol table.
784 ///
785 /// @param syms_found a vector of symbols found with the name @p
786 /// sym_name. table.
787 static bool
788 lookup_symbol_from_sysv_hash_tab(const environment& env,
789  Elf* elf_handle,
790  const string& sym_name,
791  size_t ht_index,
792  size_t sym_tab_index,
793  bool demangle,
794  vector<elf_symbol_sptr>& syms_found)
795 {
796  Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
797  ABG_ASSERT(sym_tab_section);
798 
799  Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
800  ABG_ASSERT(sym_tab_data);
801 
802  GElf_Shdr sheader_mem;
803  GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
804  &sheader_mem);
805  Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
806  ABG_ASSERT(hash_section);
807 
808  // Poke at the different parts of the hash table and get them ready
809  // to be used.
810  unsigned long hash = elf_hash(sym_name.c_str());
811  Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
812  Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
813  size_t nb_buckets = ht_data[0];
814  size_t nb_chains = ht_data[1];
815 
816  if (nb_buckets == 0)
817  // An empty hash table. Not sure if that is possible, but it
818  // would mean an empty table of exported symbols.
819  return false;
820 
821  //size_t nb_chains = ht_data[1];
822  Elf32_Word* ht_buckets = &ht_data[2];
823  Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
824 
825  // Now do the real work.
826  size_t bucket = hash % nb_buckets;
827  size_t symbol_index = ht_buckets[bucket];
828 
829  GElf_Sym symbol;
830  const char* sym_name_str;
831  size_t sym_size;
832  elf_symbol::type sym_type;
833  elf_symbol::binding sym_binding;
834  elf_symbol::visibility sym_visibility;
835  bool found = false;
836 
837  do
838  {
839  ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
840  sym_name_str = elf_strptr(elf_handle,
841  sym_tab_section_header->sh_link,
842  symbol.st_name);
843  if (sym_name_str
844  && compare_symbol_name(sym_name_str, sym_name, demangle))
845  {
846  sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
847  sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
848  sym_visibility =
849  stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
850  sym_size = symbol.st_size;
851  elf_symbol::version ver;
852  if (get_version_for_symbol(elf_handle, symbol_index,
853  /*get_def_version=*/true, ver))
854  ABG_ASSERT(!ver.str().empty());
855  elf_symbol_sptr symbol_found =
856  elf_symbol::create(env,
857  symbol_index,
858  sym_size,
859  sym_name_str,
860  sym_type,
861  sym_binding,
862  symbol.st_shndx != SHN_UNDEF,
863  symbol.st_shndx == SHN_COMMON,
864  ver, sym_visibility);
865  syms_found.push_back(symbol_found);
866  found = true;
867  }
868  symbol_index = ht_chains[symbol_index];
869  } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
870 
871  return found;
872 }
873 
874 /// Get the size of the elf class, in bytes.
875 ///
876 /// @param elf_handle the elf handle to use.
877 ///
878 /// @return the size computed.
879 static char
880 get_elf_class_size_in_bytes(Elf* elf_handle)
881 {
882  char result = 0;
883  GElf_Ehdr hdr;
884 
885  ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
886  int c = hdr.e_ident[EI_CLASS];
887 
888  switch (c)
889  {
890  case ELFCLASS32:
891  result = 4;
892  break;
893  case ELFCLASS64:
894  result = 8;
895  break;
896  default:
898  }
899 
900  return result;
901 }
902 
903 /// Get a given word of a bloom filter, referred to by the index of
904 /// the word.
905 ///
906 /// The bloom word size depends on the current elf class (32 bits for
907 /// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
908 /// abstracts that nicely.
909 ///
910 /// @param elf_handle the elf handle to use.
911 ///
912 /// @param bloom_filter the bloom filter to consider.
913 ///
914 /// @param index the index of the bloom filter to return.
915 ///
916 /// @return a 64 bits work containing the bloom word found at index @p
917 /// index. Note that if we are looking at an ELFCLASS32 binary, the 4
918 /// most significant bytes of the result are going to be zero.
919 static Elf64_Xword
920 bloom_word_at(Elf* elf_handle,
921  Elf32_Word* bloom_filter,
922  size_t index)
923 {
924  Elf64_Xword result = 0;
925  GElf_Ehdr h;
926  ABG_ASSERT(gelf_getehdr(elf_handle, &h));
927  int c;
928  c = h.e_ident[EI_CLASS];
929 
930  switch(c)
931  {
932  case ELFCLASS32:
933  result = bloom_filter[index];
934  break ;
935  case ELFCLASS64:
936  {
937  Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
938  result = f[index];
939  }
940  break;
941  default:
942  abort();
943  }
944 
945  return result;
946 }
947 
948 /// The abstraction of the gnu elf hash table.
949 ///
950 /// The members of this struct are explained at
951 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
952 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
953 struct gnu_ht
954 {
955  size_t nb_buckets;
956  Elf32_Word* buckets;
957  Elf32_Word* chain;
958  size_t first_sym_index;
959  size_t bf_nwords;
960  size_t bf_size;
961  Elf32_Word* bloom_filter;
962  size_t shift;
963  size_t sym_count;
964  Elf_Scn* sym_tab_section;
965  GElf_Shdr sym_tab_section_header;
966 
967  gnu_ht()
968  : nb_buckets(0),
969  buckets(0),
970  chain(0),
971  first_sym_index(0),
972  bf_nwords(0),
973  bf_size(0),
974  bloom_filter(0),
975  shift(0),
976  sym_count(0),
977  sym_tab_section(0)
978  {}
979 }; // end struct gnu_ht
980 
981 /// Setup the members of the gnu hash table.
982 ///
983 /// @param elf_handle a handle on the elf file to use.
984 ///
985 /// @param ht_index the index (into the elf section headers table) of
986 /// the hash table section to use.
987 ///
988 /// @param sym_tab_index the index (into the elf section headers
989 /// table) of the symbol table the gnu hash table is about.
990 ///
991 /// @param ht the resulting hash table.
992 ///
993 /// @return true iff the hash table @ ht could be setup.
994 static bool
995 setup_gnu_ht(Elf* elf_handle,
996  size_t ht_index,
997  size_t sym_tab_index,
998  gnu_ht& ht)
999 {
1000  ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1001  ABG_ASSERT(ht.sym_tab_section);
1002  ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
1003  ht.sym_count =
1004  ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
1005  Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1006  ABG_ASSERT(hash_section);
1007 
1008  // Poke at the different parts of the hash table and get them ready
1009  // to be used.
1010  Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1011  Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1012 
1013  ht.nb_buckets = ht_data[0];
1014  if (ht.nb_buckets == 0)
1015  // An empty hash table. Not sure if that is possible, but it
1016  // would mean an empty table of exported symbols.
1017  return false;
1018  ht.first_sym_index = ht_data[1];
1019  // The number of words used by the bloom filter. A size of a word
1020  // is ELFCLASS.
1021  ht.bf_nwords = ht_data[2];
1022  // The shift used by the bloom filter code.
1023  ht.shift = ht_data[3];
1024  // The data of the bloom filter proper.
1025  ht.bloom_filter = &ht_data[4];
1026  // The size of the bloom filter in 4 bytes word. This is going to
1027  // be used to index the 'bloom_filter' above, which is of type
1028  // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1029  // words.
1030  ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1031  // The buckets of the hash table.
1032  ht.buckets = ht.bloom_filter + ht.bf_size;
1033  // The chain of the hash table.
1034  ht.chain = ht.buckets + ht.nb_buckets;
1035 
1036  return true;
1037 }
1038 
1039 /// Look into the symbol tables of the underlying elf file and find
1040 /// the symbol we are being asked.
1041 ///
1042 /// This function uses the GNU hash table for the symbol lookup.
1043 ///
1044 /// The reference of for the implementation of this function can be
1045 /// found at:
1046 /// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1047 /// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1048 ///
1049 /// @param elf_handle the elf handle to use.
1050 ///
1051 /// @param sym_name the name of the symbol to look for.
1052 ///
1053 /// @param ht_index the index of the hash table header to use.
1054 ///
1055 /// @param sym_tab_index the index of the symbol table header to use
1056 /// with this hash table.
1057 ///
1058 /// @param demangle if true, demangle @p sym_name.
1059 ///
1060 /// @param syms_found the vector of symbols found with the name @p
1061 /// sym_name.
1062 ///
1063 /// @return true if a symbol was actually found.
1064 static bool
1065 lookup_symbol_from_gnu_hash_tab(const environment& env,
1066  Elf* elf_handle,
1067  const string& sym_name,
1068  size_t ht_index,
1069  size_t sym_tab_index,
1070  bool demangle,
1071  vector<elf_symbol_sptr>& syms_found)
1072 {
1073  gnu_ht ht;
1074  if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
1075  return false;
1076 
1077  // Now do the real work.
1078 
1079  // Compute bloom hashes (GNU hash and second bloom specific hashes).
1080  size_t h1 = elf_gnu_hash(sym_name.c_str());
1081  size_t h2 = h1 >> ht.shift;
1082  // The size of one of the words used in the bloom
1083  // filter, in bits.
1084  int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1085  int n = (h1 / c) % ht.bf_nwords;
1086  // The bitmask of the bloom filter has a size of either 32-bits on
1087  // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1088  // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1089  // type used here. When dealing with 32bits binaries, the upper
1090  // bits of the bitmask will be zero anyway.
1091  Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1092 
1093  // Test if the symbol is *NOT* present in this ELF file.
1094  if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1095  return false;
1096 
1097  size_t i = ht.buckets[h1 % ht.nb_buckets];
1098  if (i == STN_UNDEF)
1099  return false;
1100 
1101  Elf32_Word stop_word, *stop_wordp;
1102  elf_symbol::version ver;
1103  GElf_Sym symbol;
1104  const char* sym_name_str;
1105  bool found = false;
1106 
1107  elf_symbol::type sym_type;
1108  elf_symbol::binding sym_binding;
1109  elf_symbol::visibility sym_visibility;
1110 
1111  // Let's walk the hash table and record the versions of all the
1112  // symbols which name equal sym_name.
1113  for (i = ht.buckets[h1 % ht.nb_buckets],
1114  stop_wordp = &ht.chain[i - ht.first_sym_index];
1115  i != STN_UNDEF
1116  && (stop_wordp
1117  < ht.chain + (ht.sym_count - ht.first_sym_index));
1118  ++i, ++stop_wordp)
1119  {
1120  stop_word = *stop_wordp;
1121  if ((stop_word & ~ 1)!= (h1 & ~1))
1122  // A given bucket can reference several hashes. Here we
1123  // stumbled across a hash value different from the one we are
1124  // looking for. Let's keep walking.
1125  continue;
1126 
1127  ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1128  i, &symbol));
1129  sym_name_str = elf_strptr(elf_handle,
1130  ht.sym_tab_section_header.sh_link,
1131  symbol.st_name);
1132  if (sym_name_str
1133  && compare_symbol_name(sym_name_str, sym_name, demangle))
1134  {
1135  // So we found a symbol (in the symbol table) that equals
1136  // sym_name. Now lets try to get its version and record it.
1137  sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1138  sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1139  sym_visibility =
1140  stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1141 
1142  if (get_version_for_symbol(elf_handle, i,
1143  /*get_def_version=*/true,
1144  ver))
1145  ABG_ASSERT(!ver.str().empty());
1146 
1147  elf_symbol_sptr symbol_found =
1148  elf_symbol::create(env, i,
1149  symbol.st_size,
1150  sym_name_str,
1151  sym_type, sym_binding,
1152  symbol.st_shndx != SHN_UNDEF,
1153  symbol.st_shndx == SHN_COMMON,
1154  ver, sym_visibility);
1155  syms_found.push_back(symbol_found);
1156  found = true;
1157  }
1158 
1159  if (stop_word & 1)
1160  // The last bit of the stop_word is 1. That means we need to
1161  // stop here. We reached the end of the chain of values
1162  // referenced by the hask bucket.
1163  break;
1164  }
1165  return found;
1166 }
1167 
1168 /// Look into the symbol tables of the underlying elf file and find
1169 /// the symbol we are being asked.
1170 ///
1171 /// This function uses the elf hash table (be it the GNU hash table or
1172 /// the sysv hash table) for the symbol lookup.
1173 ///
1174 /// @param env the environment we are operating from.
1175 ///
1176 /// @param elf_handle the elf handle to use.
1177 ///
1178 /// @param ht_kind the kind of hash table to use. This is returned by
1179 /// the function function find_hash_table_section_index.
1180 ///
1181 /// @param ht_index the index (in the section headers table) of the
1182 /// hash table section to use.
1183 ///
1184 /// @param sym_tab_index the index (in section headers table) of the
1185 /// symbol table index to use with this hash table.
1186 ///
1187 /// @param symbol_name the name of the symbol to look for.
1188 ///
1189 /// @param demangle if true, demangle @p sym_name.
1190 ///
1191 /// @param syms_found the symbols that were actually found with the
1192 /// name @p symbol_name.
1193 ///
1194 /// @return true iff the function found the symbol from the elf hash
1195 /// table.
1196 static bool
1197 lookup_symbol_from_elf_hash_tab(const environment& env,
1198  Elf* elf_handle,
1199  hash_table_kind ht_kind,
1200  size_t ht_index,
1201  size_t symtab_index,
1202  const string& symbol_name,
1203  bool demangle,
1204  vector<elf_symbol_sptr>& syms_found)
1205 {
1206  if (elf_handle == 0 || symbol_name.empty())
1207  return false;
1208 
1209  if (ht_kind == NO_HASH_TABLE_KIND)
1210  return false;
1211 
1212  if (ht_kind == SYSV_HASH_TABLE_KIND)
1213  return lookup_symbol_from_sysv_hash_tab(env,
1214  elf_handle, symbol_name,
1215  ht_index,
1216  symtab_index,
1217  demangle,
1218  syms_found);
1219  else if (ht_kind == GNU_HASH_TABLE_KIND)
1220  return lookup_symbol_from_gnu_hash_tab(env,
1221  elf_handle, symbol_name,
1222  ht_index,
1223  symtab_index,
1224  demangle,
1225  syms_found);
1226  return false;
1227 }
1228 
1229 /// Lookup a symbol from the symbol table directly.
1230 ///
1231 ///
1232 /// @param env the environment we are operating from.
1233 ///
1234 /// @param elf_handle the elf handle to use.
1235 ///
1236 /// @param sym_name the name of the symbol to look up.
1237 ///
1238 /// @param sym_tab_index the index (in the section headers table) of
1239 /// the symbol table section.
1240 ///
1241 /// @param demangle if true, demangle the names found in the symbol
1242 /// table before comparing them with @p sym_name.
1243 ///
1244 /// @param sym_name_found the actual name of the symbol found.
1245 ///
1246 /// @param sym_type the type of the symbol found.
1247 ///
1248 /// @param sym_binding the binding of the symbol found.
1249 ///
1250 /// @param sym_versions the versions of the symbol found.
1251 ///
1252 /// @return true iff the symbol was found.
1253 static bool
1254 lookup_symbol_from_symtab(const environment& env,
1255  Elf* elf_handle,
1256  const string& sym_name,
1257  size_t sym_tab_index,
1258  bool demangle,
1259  vector<elf_symbol_sptr>& syms_found)
1260 {
1261  // TODO: read all of the symbol table, store it in memory in a data
1262  // structure that associates each symbol with its versions and in
1263  // which lookups of a given symbol is fast.
1264  Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1265  ABG_ASSERT(sym_tab_section);
1266 
1267  GElf_Shdr header_mem;
1268  GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1269  &header_mem);
1270 
1271  size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1272  Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1273  GElf_Sym* sym;
1274  char* name_str = 0;
1275  elf_symbol::version ver;
1276  bool found = false;
1277 
1278  for (size_t i = 0; i < symcount; ++i)
1279  {
1280  GElf_Sym sym_mem;
1281  sym = gelf_getsym(symtab, i, &sym_mem);
1282  name_str = elf_strptr(elf_handle,
1283  sym_tab_header->sh_link,
1284  sym->st_name);
1285 
1286  if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1287  {
1288  elf_symbol::type sym_type =
1289  stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1290  elf_symbol::binding sym_binding =
1291  stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1292  elf_symbol::visibility sym_visibility =
1293  stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1294  bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1295  bool sym_is_common = sym->st_shndx == SHN_COMMON;
1296 
1297  if (get_version_for_symbol(elf_handle, i,
1298  /*get_def_version=*/sym_is_defined,
1299  ver))
1300  ABG_ASSERT(!ver.str().empty());
1301  elf_symbol_sptr symbol_found =
1302  elf_symbol::create(env, i, sym->st_size,
1303  name_str, sym_type,
1304  sym_binding, sym_is_defined,
1305  sym_is_common, ver, sym_visibility);
1306  syms_found.push_back(symbol_found);
1307  found = true;
1308  }
1309  }
1310 
1311  if (found)
1312  return true;
1313 
1314  return false;
1315 }
1316 
1317 /// Look into the symbol tables of the underlying elf file and see
1318 /// if we find a given symbol.
1319 ///
1320 /// @param env the environment we are operating from.
1321 ///
1322 /// @param symbol_name the name of the symbol to look for.
1323 ///
1324 /// @param demangle if true, try to demangle the symbol name found in
1325 /// the symbol table before comparing it to @p symbol_name.
1326 ///
1327 /// @param syms_found the list of symbols found, with the name @p
1328 /// symbol_name.
1329 ///
1330 /// @param sym_type this is set to the type of the symbol found. This
1331 /// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1332 /// STT_FUNC, STT_IFUNC, etc ...
1333 ///
1334 /// Note that this parameter is set iff the function returns true.
1335 ///
1336 /// @param sym_binding this is set to the binding of the symbol found.
1337 /// This is a standard elf.h value of the symbol binding kind, that
1338 /// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1339 ///
1340 /// @param symbol_versions the versions of the symbol @p symbol_name,
1341 /// if it was found.
1342 ///
1343 /// @return true iff a symbol with the name @p symbol_name was found.
1344 static bool
1345 lookup_symbol_from_elf(const environment& env,
1346  Elf* elf_handle,
1347  const string& symbol_name,
1348  bool demangle,
1349  vector<elf_symbol_sptr>& syms_found)
1350 {
1351  size_t hash_table_index = 0, symbol_table_index = 0;
1352  hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1353 
1354  if (!demangle)
1355  ht_kind = find_hash_table_section_index(elf_handle,
1356  hash_table_index,
1357  symbol_table_index);
1358 
1359  if (ht_kind == NO_HASH_TABLE_KIND)
1360  {
1361  if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1362  return false;
1363 
1364  return lookup_symbol_from_symtab(env,
1365  elf_handle,
1366  symbol_name,
1367  symbol_table_index,
1368  demangle,
1369  syms_found);
1370  }
1371 
1372  return lookup_symbol_from_elf_hash_tab(env,
1373  elf_handle,
1374  ht_kind,
1375  hash_table_index,
1376  symbol_table_index,
1377  symbol_name,
1378  demangle,
1379  syms_found);
1380 }
1381 
1382 /// Look into the symbol tables of the underlying elf file and see if
1383 /// we find a given public (global or weak) symbol of function type.
1384 ///
1385 /// @param env the environment we are operating from.
1386 ///
1387 /// @param elf_handle the elf handle to use for the query.
1388 ///
1389 /// @param symbol_name the function symbol to look for.
1390 ///
1391 /// @param func_syms the vector of public functions symbols found, if
1392 /// any.
1393 ///
1394 /// @return true iff the symbol was found.
1395 static bool
1396 lookup_public_function_symbol_from_elf(environment& env,
1397  Elf* elf_handle,
1398  const string& symbol_name,
1399  vector<elf_symbol_sptr>& func_syms)
1400 {
1401  vector<elf_symbol_sptr> syms_found;
1402  bool found = false;
1403 
1404  if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1405  /*demangle=*/false, syms_found))
1406  {
1407  for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1408  i != syms_found.end();
1409  ++i)
1410  {
1411  elf_symbol::type type = (*i)->get_type();
1412  elf_symbol::binding binding = (*i)->get_binding();
1413 
1414  if ((type == elf_symbol::FUNC_TYPE
1415  || type == elf_symbol::GNU_IFUNC_TYPE
1416  || type == elf_symbol::COMMON_TYPE)
1417  && (binding == elf_symbol::GLOBAL_BINDING
1418  || binding == elf_symbol::WEAK_BINDING))
1419  {
1420  func_syms.push_back(*i);
1421  found = true;
1422  }
1423  }
1424  }
1425 
1426  return found;
1427 }
1428 
1429 // ---------------------------------------
1430 // <location expression evaluation types>
1431 // ---------------------------------------
1432 
1433 /// An abstraction of a value representing the result of the
1434 /// evaluation of a dwarf expression. This is abstraction represents
1435 /// a partial view on the possible values because we are only
1436 /// interested in extracting the latest and longuest constant
1437 /// sub-expression of a given dwarf expression.
1438 class expr_result
1439 {
1440  bool is_const_;
1441  int64_t const_value_;
1442 
1443 public:
1444  expr_result()
1445  : is_const_(true),
1446  const_value_(0)
1447  {}
1448 
1449  expr_result(bool is_const)
1450  : is_const_(is_const),
1451  const_value_(0)
1452  {}
1453 
1454  explicit expr_result(int64_t v)
1455  :is_const_(true),
1456  const_value_(v)
1457  {}
1458 
1459  /// @return true if the value is a constant. Otherwise, return
1460  /// false, meaning the value represents a quantity for which we need
1461  /// inferior (a running program) state to determine the value.
1462  bool
1463  is_const() const
1464  {return is_const_;}
1465 
1466 
1467  /// @param f a flag saying if the value is set to a constant or not.
1468  void
1469  is_const(bool f)
1470  {is_const_ = f;}
1471 
1472  /// Get the current constant value iff this represents a
1473  /// constant.
1474  ///
1475  /// @param value the out parameter. Is set to the constant value of
1476  /// the @ref expr_result. This is set iff the function return true.
1477  ///
1478  ///@return true if this has a constant value, false otherwise.
1479  bool
1480  const_value(int64_t& value)
1481  {
1482  if (is_const())
1483  {
1484  value = const_value_;
1485  return true;
1486  }
1487  return false;
1488  }
1489 
1490  /// Getter of the constant value of the current @ref expr_result.
1491  ///
1492  /// Note that the current @ref expr_result must be constant,
1493  /// otherwise the current process is aborted.
1494  ///
1495  /// @return the constant value of the current @ref expr_result.
1496  int64_t
1497  const_value() const
1498  {
1499  ABG_ASSERT(is_const());
1500  return const_value_;
1501  }
1502 
1503  operator int64_t() const
1504  {return const_value();}
1505 
1506  expr_result&
1507  operator=(const int64_t v)
1508  {
1509  const_value_ = v;
1510  return *this;
1511  }
1512 
1513  bool
1514  operator==(const expr_result& o) const
1515  {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1516 
1517  bool
1518  operator>=(const expr_result& o) const
1519  {return const_value_ >= o.const_value_;}
1520 
1521  bool
1522  operator<=(const expr_result& o) const
1523  {return const_value_ <= o.const_value_;}
1524 
1525  bool
1526  operator>(const expr_result& o) const
1527  {return const_value_ > o.const_value_;}
1528 
1529  bool
1530  operator<(const expr_result& o) const
1531  {return const_value_ < o.const_value_;}
1532 
1533  expr_result
1534  operator+(const expr_result& v) const
1535  {
1536  expr_result r(*this);
1537  r.const_value_ += v.const_value_;
1538  r.is_const_ = r.is_const_ && v.is_const_;
1539  return r;
1540  }
1541 
1542  expr_result&
1543  operator+=(int64_t v)
1544  {
1545  const_value_ += v;
1546  return *this;
1547  }
1548 
1549  expr_result
1550  operator-(const expr_result& v) const
1551  {
1552  expr_result r(*this);
1553  r.const_value_ -= v.const_value_;
1554  r.is_const_ = r.is_const_ && v.is_const_;
1555  return r;
1556  }
1557 
1558  expr_result
1559  operator%(const expr_result& v) const
1560  {
1561  expr_result r(*this);
1562  r.const_value_ %= v.const_value_;
1563  r.is_const_ = r.is_const_ && v.is_const();
1564  return r;
1565  }
1566 
1567  expr_result
1568  operator*(const expr_result& v) const
1569  {
1570  expr_result r(*this);
1571  r.const_value_ *= v.const_value_;
1572  r.is_const_ = r.is_const_ && v.is_const();
1573  return r;
1574  }
1575 
1576  expr_result
1577  operator|(const expr_result& v) const
1578  {
1579  expr_result r(*this);
1580  r.const_value_ |= v.const_value_;
1581  r.is_const_ = r.is_const_ && v.is_const_;
1582  return r;
1583  }
1584 
1585  expr_result
1586  operator^(const expr_result& v) const
1587  {
1588  expr_result r(*this);
1589  r.const_value_ ^= v.const_value_;
1590  r.is_const_ = r.is_const_ && v.is_const_;
1591  return r;
1592  }
1593 
1594  expr_result
1595  operator>>(const expr_result& v) const
1596  {
1597  expr_result r(*this);
1598  r.const_value_ = r.const_value_ >> v.const_value_;
1599  r.is_const_ = r.is_const_ && v.is_const_;
1600  return r;
1601  }
1602 
1603  expr_result
1604  operator<<(const expr_result& v) const
1605  {
1606  expr_result r(*this);
1607  r.const_value_ = r.const_value_ << v.const_value_;
1608  r.is_const_ = r.is_const_ && v.is_const_;
1609  return r;
1610  }
1611 
1612  expr_result
1613  operator~() const
1614  {
1615  expr_result r(*this);
1616  r.const_value_ = ~r.const_value_;
1617  return r;
1618  }
1619 
1620  expr_result
1621  neg() const
1622  {
1623  expr_result r(*this);
1624  r.const_value_ = -r.const_value_;
1625  return r;
1626  }
1627 
1628  expr_result
1629  abs() const
1630  {
1631  expr_result r = *this;
1632  r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1633  return r;
1634  }
1635 
1636  expr_result
1637  operator&(const expr_result& o)
1638  {
1639  expr_result r(*this);
1640  r.const_value_ &= o.const_value_;
1641  r.is_const_ = r.is_const_ && o.is_const_;
1642  return r;
1643  }
1644 
1645  expr_result
1646  operator/(const expr_result& o)
1647  {
1648  expr_result r(*this);
1649  r.is_const_ = r.is_const_ && o.is_const_;
1650  return r.const_value() / o.const_value();
1651  }
1652 };// class end expr_result;
1653 
1654 /// A class that implements a stack of @ref expr_result, to be used in
1655 /// the engine evaluating DWARF expressions.
1656 class expr_result_stack_type
1657 {
1658  vector<expr_result> elems_;
1659 
1660 public:
1661 
1662  expr_result_stack_type()
1663  {elems_.reserve(4);}
1664 
1665  expr_result&
1666  operator[](unsigned i)
1667  {
1668  unsigned s = elems_.size();
1669  ABG_ASSERT(s > i);
1670  return elems_[s - 1 -i];
1671  }
1672 
1673  const expr_result&
1674  operator[](unsigned i) const
1675  {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1676 
1677  unsigned
1678  size() const
1679  {return elems_.size();}
1680 
1681  vector<expr_result>::reverse_iterator
1682  begin()
1683  {return elems_.rbegin();}
1684 
1685  const vector<expr_result>::reverse_iterator
1686  begin() const
1687  {return const_cast<expr_result_stack_type*>(this)->begin();}
1688 
1689  vector<expr_result>::reverse_iterator
1690  end()
1691  {return elems_.rend();}
1692 
1693  const vector<expr_result>::reverse_iterator
1694  end() const
1695  {return const_cast<expr_result_stack_type*>(this)->end();}
1696 
1697  expr_result&
1698  front()
1699  {return elems_.back();}
1700 
1701  const expr_result&
1702  front() const
1703  {return const_cast<expr_result_stack_type*>(this)->front();}
1704 
1705  void
1706  push_front(expr_result e)
1707  {elems_.push_back(e);}
1708 
1709  expr_result
1710  pop_front()
1711  {
1712  expr_result r = front();
1713  elems_.pop_back();
1714  return r;
1715  }
1716 
1717  void
1718  erase(vector<expr_result>::reverse_iterator i)
1719  {elems_.erase(--i.base());}
1720 
1721  void
1722  clear()
1723  {elems_.clear();}
1724 }; // end class expr_result_stack_type
1725 
1726 /// Abstraction of the evaluation context of a dwarf expression.
1727 struct dwarf_expr_eval_context
1728 {
1729  expr_result accum;
1730  expr_result_stack_type stack;
1731  // Is set to true if the result of the expression that got evaluated
1732  // is a TLS address.
1733  bool set_tls_addr;
1734 
1735  dwarf_expr_eval_context()
1736  : accum(/*is_const=*/false),
1737  set_tls_addr(false)
1738  {
1739  stack.push_front(expr_result(true));
1740  }
1741 
1742  void
1743  reset()
1744  {
1745  stack.clear();
1746  stack.push_front(expr_result(true));
1747  accum = expr_result(false);
1748  set_tls_addr = false;
1749  }
1750 
1751  /// Set a flag to to tell that the result of the expression that got
1752  /// evaluated is a TLS address.
1753  ///
1754  /// @param f true iff the result of the expression that got
1755  /// evaluated is a TLS address, false otherwise.
1756  void
1757  set_tls_address(bool f)
1758  {set_tls_addr = f;}
1759 
1760  /// Getter for the flag that tells if the result of the expression
1761  /// that got evaluated is a TLS address.
1762  ///
1763  /// @return true iff the result of the expression that got evaluated
1764  /// is a TLS address.
1765  bool
1766  set_tls_address() const
1767  {return set_tls_addr;}
1768 
1769  expr_result
1770  pop()
1771  {
1772  expr_result r = stack.front();
1773  stack.pop_front();
1774  return r;
1775  }
1776 
1777  void
1778  push(const expr_result& v)
1779  {stack.push_front(v);}
1780 };//end class dwarf_expr_eval_context
1781 
1782 // ---------------------------------------
1783 // </location expression evaluation types>
1784 // ---------------------------------------
1785 
1786 class reader;
1787 
1788 typedef shared_ptr<reader> reader_sptr;
1789 
1790 /// The DWARF reader used to build the ABI corpus from debug info in
1791 /// DWARF format.
1792 ///
1793 /// This type is to be instanciated
1794 /// abigail::dwarf::reader::create().
1795 class reader : public elf_based_reader
1796 {
1797 public:
1798 
1799  /// A set of containers that contains one container per kind of @ref
1800  /// die_source. This allows to associate DIEs to things, depending
1801  /// on the source of the DIE.
1802  template <typename ContainerType>
1803  class die_source_dependant_container_set
1804  {
1805  ContainerType primary_debug_info_container_;
1806  ContainerType alt_debug_info_container_;
1807  ContainerType type_unit_container_;
1808 
1809  public:
1810 
1811  /// Getter for the container associated to DIEs coming from a
1812  /// given @ref die_source.
1813  ///
1814  /// @param source the die_source for which we want the container.
1815  ///
1816  /// @return the container that associates DIEs coming from @p
1817  /// source to something.
1818  ContainerType&
1819  get_container(die_source source)
1820  {
1821  ContainerType *result = 0;
1822  switch (source)
1823  {
1824  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1825  result = &primary_debug_info_container_;
1826  break;
1827  case ALT_DEBUG_INFO_DIE_SOURCE:
1828  result = &alt_debug_info_container_;
1829  break;
1830  case TYPE_UNIT_DIE_SOURCE:
1831  result = &type_unit_container_;
1832  break;
1833  case NO_DEBUG_INFO_DIE_SOURCE:
1834  case NUMBER_OF_DIE_SOURCES:
1836  }
1837  return *result;
1838  }
1839 
1840  /// Getter for the container associated to DIEs coming from a
1841  /// given @ref die_source.
1842  ///
1843  /// @param source the die_source for which we want the container.
1844  ///
1845  /// @return the container that associates DIEs coming from @p
1846  /// source to something.
1847  const ContainerType&
1848  get_container(die_source source) const
1849  {
1850  return const_cast<die_source_dependant_container_set*>(this)->
1851  get_container(source);
1852  }
1853 
1854  /// Getter for the container associated to DIEs coming from the
1855  /// same source as a given DIE.
1856  ///
1857  /// @param rdr the DWARF reader to consider.
1858  ///
1859  /// @param die the DIE which should have the same source as the
1860  /// source of the container we want.
1861  ///
1862  /// @return the container that associates DIEs coming from the
1863  /// same source as @p die.
1864  ContainerType&
1865  get_container(const reader& rdr, const Dwarf_Die *die)
1866  {
1867  const die_source source = rdr.get_die_source(die);
1868  return get_container(source);
1869  }
1870 
1871  /// Getter for the container associated to DIEs coming from the
1872  /// same source as a given DIE.
1873  ///
1874  /// @param rdr the DWARF reader to consider.
1875  ///
1876  /// @param die the DIE which should have the same source as the
1877  /// source of the container we want.
1878  ///
1879  /// @return the container that associates DIEs coming from the
1880  /// same source as @p die.
1881  const ContainerType&
1882  get_container(const reader& rdr, const Dwarf_Die *die) const
1883  {
1884  return const_cast<die_source_dependant_container_set*>(this)->
1885  get_container(rdr, die);
1886  }
1887 
1888  /// Clear the container set.
1889  void
1890  clear()
1891  {
1892  primary_debug_info_container_.clear();
1893  alt_debug_info_container_.clear();
1894  type_unit_container_.clear();
1895  }
1896  }; // end die_dependant_container_set
1897 
1898  unsigned short dwarf_version_;
1899  Dwarf_Die* cur_tu_die_;
1900  mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1901  // A set of maps (one per kind of die source) that associates a decl
1902  // string representation with the DIEs (offsets) representing that
1903  // decl.
1904  mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1905  decl_die_repr_die_offsets_maps_;
1906  // A set of maps (one per kind of die source) that associates a type
1907  // string representation with the DIEs (offsets) representing that
1908  // type.
1909  mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1910  type_die_repr_die_offsets_maps_;
1911  mutable die_source_dependant_container_set<die_istring_map_type>
1912  die_qualified_name_maps_;
1913  mutable die_source_dependant_container_set<die_istring_map_type>
1914  die_pretty_repr_maps_;
1915  mutable die_source_dependant_container_set<die_istring_map_type>
1916  die_pretty_type_repr_maps_;
1917  // A set of maps (one per kind of die source) that associates the
1918  // offset of a decl die to its corresponding decl artifact.
1919  mutable die_source_dependant_container_set<die_artefact_map_type>
1920  decl_die_artefact_maps_;
1921  // A set of maps (one per kind of die source) that associates the
1922  // offset of a type die to its corresponding type artifact.
1923  mutable die_source_dependant_container_set<die_artefact_map_type>
1924  type_die_artefact_maps_;
1925  /// A set of vectors (one per kind of die source) that associates
1926  /// the offset of a type DIE to the offset of its canonical DIE.
1927  mutable die_source_dependant_container_set<offset_offset_map_type>
1928  canonical_type_die_offsets_;
1929  /// A set of vectors (one per kind of die source) that associates
1930  /// the offset of a decl DIE to the offset of its canonical DIE.
1931  mutable die_source_dependant_container_set<offset_offset_map_type>
1932  canonical_decl_die_offsets_;
1933  /// A map that associates a function type representations to
1934  /// function types, inside a translation unit.
1935  mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1936  /// A map that associates a pair of DIE offsets to the result of the
1937  /// comparison of that pair.
1938  mutable std::unordered_map<std::pair<offset_type,offset_type>,
1940  dwarf_offset_pair_hash> die_comparison_results_;
1941  // The set of types pair that have been canonical-type-propagated.
1942  mutable offset_pair_set_type propagated_types_;
1943  die_class_or_union_map_type die_wip_classes_map_;
1944  die_class_or_union_map_type alternate_die_wip_classes_map_;
1945  die_class_or_union_map_type type_unit_die_wip_classes_map_;
1946  die_function_type_map_type die_wip_function_types_map_;
1947  die_function_type_map_type alternate_die_wip_function_types_map_;
1948  die_function_type_map_type type_unit_die_wip_function_types_map_;
1949  die_function_decl_map_type die_function_with_no_symbol_map_;
1950  vector<type_base_sptr> types_to_canonicalize_;
1951  string_classes_or_unions_map decl_only_classes_map_;
1952  string_enums_map decl_only_enums_map_;
1953  die_tu_map_type die_tu_map_;
1954  translation_unit_sptr cur_tu_;
1955  scope_decl_sptr nil_scope_;
1956  scope_stack_type scope_stack_;
1957  offset_offset_map_type primary_die_parent_map_;
1958  // A map that associates each tu die to a vector of unit import
1959  // points, in the main debug info
1960  tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1961  // A map that associates each tu die to a vector of unit import
1962  // points, in the alternate debug info
1963  tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1964  tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1965  // A DIE -> parent map for DIEs coming from the alternate debug info
1966  // file.
1967  offset_offset_map_type alternate_die_parent_map_;
1968  offset_offset_map_type type_section_die_parent_map_;
1969  list<var_decl_sptr> var_decls_to_add_;
1970 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1971  bool debug_die_canonicalization_is_on_;
1972  bool use_canonical_die_comparison_;
1973 #endif
1974  mutable size_t compare_count_;
1975  mutable size_t canonical_propagated_count_;
1976  mutable size_t cancelled_propagation_count_;
1977  mutable optional<bool> leverage_dwarf_factorization_;
1978 
1979 protected:
1980 
1981  reader() = delete;
1982 
1983  /// Constructor of reader.
1984  ///
1985  /// @param elf_path the path to the elf file the context is to be
1986  /// used for.
1987  ///
1988  /// @param debug_info_root_paths a vector of pointers to the path to
1989  /// the root directory under which the debug info is to be found for
1990  /// @p elf_path. Leave this empty if the debug info is not in a
1991  /// split file.
1992  ///
1993  /// @param environment the environment used by the current context.
1994  /// This environment contains resources needed by the DWARF reader and by
1995  /// the types and declarations that are to be created later. Note
1996  /// that ABI artifacts that are to be compared all need to be
1997  /// created within the same environment.
1998  ///
1999  /// Please also note that the life time of this environment object
2000  /// must be greater than the life time of the resulting @ref
2001  /// reader the context uses resources that are allocated in
2002  /// the environment.
2003  ///
2004  /// @param load_all_types if set to false only the types that are
2005  /// reachable from publicly exported declarations (of functions and
2006  /// variables) are read. If set to true then all types found in the
2007  /// debug information are loaded.
2008  ///
2009  /// @param linux_kernel_mode if set to true, then consider the special
2010  /// linux kernel symbol tables when determining if a symbol is
2011  /// exported or not.
2012  reader(const string& elf_path,
2013  const vector<char**>& debug_info_root_paths,
2014  environment& environment,
2015  bool load_all_types,
2016  bool linux_kernel_mode)
2017  : elf_based_reader(elf_path,
2018  debug_info_root_paths,
2019  environment)
2020  {
2021  initialize(load_all_types, linux_kernel_mode);
2022  }
2023 
2024 public:
2025 
2026  /// Initializer of reader.
2027  ///
2028  /// Resets the reader so that it can be re-used to read another binary.
2029  ///
2030  /// @param load_all_types if set to false only the types that are
2031  /// reachable from publicly exported declarations (of functions and
2032  /// variables) are read. If set to true then all types found in the
2033  /// debug information are loaded.
2034  ///
2035  /// @param linux_kernel_mode if set to true, then consider the
2036  /// special linux kernel symbol tables when determining if a symbol
2037  /// is exported or not.
2038  void
2039  initialize(bool load_all_types, bool linux_kernel_mode)
2040  {
2041  dwarf_version_ = 0;
2042  cur_tu_die_ = 0;
2043  decl_die_repr_die_offsets_maps_.clear();
2044  type_die_repr_die_offsets_maps_.clear();
2045  die_qualified_name_maps_.clear();
2046  die_pretty_repr_maps_.clear();
2047  die_pretty_type_repr_maps_.clear();
2048  decl_die_artefact_maps_.clear();
2049  type_die_artefact_maps_.clear();
2050  canonical_type_die_offsets_.clear();
2051  canonical_decl_die_offsets_.clear();
2052  die_wip_classes_map_.clear();
2053  alternate_die_wip_classes_map_.clear();
2054  type_unit_die_wip_classes_map_.clear();
2055  die_wip_function_types_map_.clear();
2056  alternate_die_wip_function_types_map_.clear();
2057  type_unit_die_wip_function_types_map_.clear();
2058  die_function_with_no_symbol_map_.clear();
2059  types_to_canonicalize_.clear();
2060  decl_only_classes_map_.clear();
2061  die_tu_map_.clear();
2062  corpus().reset();
2063  corpus_group().reset();
2064  cur_tu_.reset();
2065  primary_die_parent_map_.clear();
2066  tu_die_imported_unit_points_map_.clear();
2067  alt_tu_die_imported_unit_points_map_.clear();
2068  type_units_tu_die_imported_unit_points_map_.clear();
2069  alternate_die_parent_map_.clear();
2070  type_section_die_parent_map_.clear();
2071  var_decls_to_add_.clear();
2072  clear_per_translation_unit_data();
2073  options().load_in_linux_kernel_mode = linux_kernel_mode;
2074  options().load_all_types = load_all_types;
2075 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2076  debug_die_canonicalization_is_on_ =
2077  env().debug_die_canonicalization_is_on();
2078  use_canonical_die_comparison_ = true;
2079 #endif
2080  compare_count_ = 0;
2081  canonical_propagated_count_ = 0;
2082  cancelled_propagation_count_ = 0;
2083  load_in_linux_kernel_mode(linux_kernel_mode);
2084  }
2085 
2086  /// Initializer of reader.
2087  ///
2088  /// Resets the reader so that it can be re-used to read another binary.
2089  ///
2090  /// @param elf_path the path to the new ELF file.
2091  ///
2092  /// @param debug_info_root_paths the vector of debug-info path to
2093  /// look for split debug info.
2094  ///
2095  /// @param load_all_types if set to false only the types that are
2096  /// reachable from publicly exported declarations (of functions and
2097  /// variables) are read. If set to true then all types found in the
2098  /// debug information are loaded.
2099  ///
2100  /// @param linux_kernel_mode if set to true, then consider the
2101  /// special linux kernel symbol tables when determining if a symbol
2102  /// is exported or not.
2103  void
2104  initialize(const string& elf_path,
2105  const vector<char**>& debug_info_root_paths,
2106  bool load_all_types,
2107  bool linux_kernel_mode)
2108  {
2109  elf_based_reader::initialize(elf_path, debug_info_root_paths);
2110  initialize(load_all_types, linux_kernel_mode);
2111  }
2112 
2113  /// Create an instance of DWARF Reader.
2114  ///
2115  /// @param elf_path the path to the ELF file to read from.
2116  ///
2117  /// @param debug_info_root_paths a vector of paths where to look up
2118  /// split debug info files.
2119  ///
2120  /// @param environment the environment to be used by the reader.
2121  ///
2122  /// @param load_all_types if set to false only the types that are
2123  /// reachable from publicly exported declarations (of functions and
2124  /// variables) are read. If set to true then all types found in the
2125  /// debug information are loaded.
2126  ///
2127  /// @param linux_kernel_mode if set to true, then consider the
2128  /// special linux kernel symbol tables when determining if a symbol
2129  /// is exported or not.
2130  static dwarf::reader_sptr
2131  create(const std::string& elf_path,
2132  const vector<char**>& debug_info_root_paths,
2133  environment& environment,
2134  bool load_all_types,
2135  bool linux_kernel_mode)
2136  {
2137  reader_sptr result(new reader(elf_path, debug_info_root_paths,
2138  environment, load_all_types,
2139  linux_kernel_mode));
2140  return result;
2141  }
2142 
2143  /// Destructor of the @ref reader type.
2144  ~reader()
2145  {
2146  }
2147 
2148  /// Read and analyze the ELF and DWARF information associated with
2149  /// the underlying ELF file and build an ABI corpus out of it.
2150  ///
2151  /// @param status output parameter. This is set to the status of
2152  /// the analysis of the debug info.
2153  ///
2154  /// @return the resulting ABI corpus.
2155  corpus_sptr
2156  read_corpus(status& status)
2157  {
2158  status = STATUS_UNKNOWN;
2159 
2160  // Load the generic ELF parts of the corpus.
2161  elf::reader::read_corpus(status);
2162 
2163  if (!(status & STATUS_OK))
2164  {
2165  // Something went badly wrong. There is nothing we can do
2166  // with this ELF file. Bail out.
2167  return corpus_sptr();
2168  }
2169 
2170  // If we couldn't find debug info from the elf path, then say it.
2171  if (dwarf_debug_info() == nullptr)
2172  status |= STATUS_DEBUG_INFO_NOT_FOUND;
2173 
2174  {
2175  string alt_di_path;
2176  if (refers_to_alt_debug_info(alt_di_path)
2177  && !alternate_dwarf_debug_info())
2178  status |= STATUS_ALT_DEBUG_INFO_NOT_FOUND;
2179  }
2180 
2181  if (// If debug info was found but not the required alternate debug
2182  // info ...
2183  ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2184  && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2185  // ... then we cannot handle the binary.
2186  return corpus_sptr();
2187 
2188  // Read the variable and function descriptions from the debug info
2189  // we have, through the dwfl handle.
2190  corpus_sptr corp = read_debug_info_into_corpus();
2191 
2192  status |= STATUS_OK;
2193 
2194  return corp;
2195  }
2196 
2197  /// Read an analyze the DWARF information.
2198  ///
2199  /// Construct an ABI corpus from it.
2200  ///
2201  /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2202  ///
2203  /// @return the resulting ABI corpus.
2204  corpus_sptr
2205  read_debug_info_into_corpus()
2206  {
2207  clear_per_corpus_data();
2208 
2209  // First set some mundane properties of the corpus gathered from
2210  // ELF.
2211  corpus::origin origin = corpus()->get_origin();
2212  origin |= corpus::DWARF_ORIGIN;
2213  corpus()->set_origin(origin);
2214 
2215  if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2216  && !env().user_set_analyze_exported_interfaces_only())
2217  // So we are looking at the Linux Kernel and the user has not set
2218  // any particular option regarding the amount of types to analyse.
2219  // In that case, we need to only analyze types that are reachable
2220  // from exported interfaces otherwise we get such a massive amount
2221  // of type DIEs to look at that things are just too slow down the
2222  // road.
2223  env().analyze_exported_interfaces_only(true);
2224 
2225  corpus()->set_soname(dt_soname());
2226  corpus()->set_needed(dt_needed());
2227  corpus()->set_architecture_name(elf_architecture());
2228  // Set symbols information to the corpus.
2229  corpus()->set_symtab(symtab());
2230 
2231  // Get out now if no debug info is found or if the symbol table is
2232  // empty.
2233  if (!dwarf_debug_info()
2234  || !corpus()->get_symtab()
2235  || !corpus()->get_symtab()->has_symbols())
2236  return corpus();
2237 
2238  uint8_t address_size = 0;
2239  size_t header_size = 0;
2240 
2241 #ifdef WITH_DEBUG_SELF_COMPARISON
2242  if (env().self_comparison_debug_is_on())
2243  env().set_self_comparison_debug_input(corpus());
2244 #endif
2245 
2246  env().priv_->do_log(do_log());
2247 
2248  // Walk all the DIEs of the debug info to build a DIE -> parent map
2249  // useful for get_die_parent() to work.
2250  {
2251  tools_utils::timer t;
2252  if (do_log())
2253  {
2254  cerr << "building die -> parent maps ...";
2255  t.start();
2256  }
2257 
2258  build_die_parent_maps();
2259 
2260  if (do_log())
2261  {
2262  t.stop();
2263  cerr << " DONE@" << corpus()->get_path()
2264  << ":"
2265  << t
2266  << "\n";
2267  }
2268  }
2269 
2270  env().canonicalization_is_done(false);
2271 
2272  {
2273  tools_utils::timer t;
2274  if (do_log())
2275  {
2276  cerr << "building the libabigail internal representation ...\n";
2277  t.start();
2278  }
2279  // And now walk all the DIEs again to build the libabigail IR.
2280  Dwarf_Half dwarf_vers = 0;
2281  for (Dwarf_Off offset = 0, next_offset = 0;
2282  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2283  offset, &next_offset, &header_size,
2284  &dwarf_vers, NULL, &address_size, NULL,
2285  NULL, NULL) == 0);
2286  offset = next_offset)
2287  {
2288  Dwarf_Off die_offset = offset + header_size;
2289  Dwarf_Die unit;
2290  if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2291  die_offset, &unit)
2292  || dwarf_tag(&unit) != DW_TAG_compile_unit)
2293  continue;
2294 
2295  dwarf_version(dwarf_vers);
2296 
2297  address_size *= 8;
2298 
2299  // Build a translation_unit IR node from cu; note that cu must
2300  // be a DW_TAG_compile_unit die.
2301  translation_unit_sptr ir_node =
2302  build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2303  ABG_ASSERT(ir_node);
2304  }
2305  if (do_log())
2306  {
2307  t.stop();
2308  cerr << "building the libabigail internal representation "
2309  << "DONE for corpus << corpus()->get_path()"
2310  << " in :"
2311  << t
2312  << "\n";
2313 
2314  cerr << "Number of aggregate types compared: "
2315  << compare_count_ << "\n"
2316  << "Number of canonical types propagated: "
2317  << canonical_propagated_count_ << "\n"
2318  << "Number of cancelled propagated canonical types:"
2319  << cancelled_propagation_count_ << "\n";
2320  }
2321  }
2322 
2323  {
2324  tools_utils::timer t;
2325  if (do_log())
2326  {
2327  cerr << "resolving declaration only classes ...";
2328  t.start();
2329  }
2330  resolve_declaration_only_classes();
2331  if (do_log())
2332  {
2333  t.stop();
2334  cerr << " DONE@" << corpus()->get_path()
2335  << ":"
2336  << t
2337  <<"\n";
2338  }
2339  }
2340 
2341  {
2342  tools_utils::timer t;
2343  if (do_log())
2344  {
2345  cerr << "resolving declaration only enums ...";
2346  t.start();
2347  }
2348  resolve_declaration_only_enums();
2349  if (do_log())
2350  {
2351  t.stop();
2352  cerr << " DONE@" << corpus()->get_path()
2353  << ":"
2354  << t
2355  <<"\n";
2356  }
2357  }
2358 
2359  {
2360  tools_utils::timer t;
2361  if (do_log())
2362  {
2363  cerr << "fixing up functions with linkage name but "
2364  << "no advertised underlying symbols ....";
2365  t.start();
2366  }
2367  fixup_functions_with_no_symbols();
2368  if (do_log())
2369  {
2370  t.stop();
2371  cerr << " DONE@" << corpus()->get_path()
2372  <<":"
2373  << t
2374  <<"\n";
2375  }
2376  }
2377 
2378  /// Now, look at the types that needs to be canonicalized after the
2379  /// translation has been constructed (which is just now) and
2380  /// canonicalize them.
2381  ///
2382  /// These types need to be constructed at the end of the translation
2383  /// unit reading phase because some types are modified by some DIEs
2384  /// even after the principal DIE describing the type has been read;
2385  /// this happens for clones of virtual destructors (for instance) or
2386  /// even for some static data members. We need to do that for types
2387  /// are in the alternate debug info section and for types that in
2388  /// the main debug info section.
2389  {
2390  tools_utils::timer t;
2391  if (do_log())
2392  {
2393  cerr << "perform late type canonicalizing ...\n";
2394  t.start();
2395  }
2396 
2397  perform_late_type_canonicalizing();
2398  if (do_log())
2399  {
2400  t.stop();
2401  cerr << "late type canonicalizing DONE for "
2402  << corpus()->get_path()
2403  << " in :"
2404  << t
2405  << "\n";
2406  }
2407  }
2408 
2409  env().canonicalization_is_done(true);
2410 
2411  {
2412  tools_utils::timer t;
2413  if (do_log())
2414  {
2415  cerr << "sort functions and variables ...";
2416  t.start();
2417  }
2418  corpus()->sort_functions();
2419  corpus()->sort_variables();
2420  if (do_log())
2421  {
2422  t.stop();
2423  cerr << " DONE@" << corpus()->get_path()
2424  << ":"
2425  << t
2426  <<" \n";
2427  }
2428  }
2429 
2430  return corpus();
2431  }
2432 
2433  /// Clear the data that is relevant only for the current translation
2434  /// unit being read. The rest of the data is relevant for the
2435  /// entire ABI corpus.
2436  void
2437  clear_per_translation_unit_data()
2438  {
2439  while (!scope_stack().empty())
2440  scope_stack().pop();
2441  var_decls_to_re_add_to_tree().clear();
2442  per_tu_repr_to_fn_type_maps().clear();
2443  }
2444 
2445  /// Clear the data that is relevant for the current corpus being
2446  /// read.
2447  void
2448  clear_per_corpus_data()
2449  {
2450  die_qualified_name_maps_.clear();
2451  die_pretty_repr_maps_.clear();
2452  die_pretty_type_repr_maps_.clear();
2453  clear_types_to_canonicalize();
2454  }
2455 
2456  /// Getter for the current environment.
2457  ///
2458  /// @return the current environment.
2459  environment&
2460  env()
2461  {return options().env;}
2462 
2463  /// Getter for the current environment.
2464  ///
2465  /// @return the current environment.
2466  const environment&
2467  env() const
2468  {return const_cast<reader*>(this)->env();}
2469 
2470  /// Getter for the flag that tells us if we are dropping functions
2471  /// and variables that have undefined symbols.
2472  ///
2473  /// @return true iff we are dropping functions and variables that have
2474  /// undefined symbols.
2475  bool
2476  drop_undefined_syms() const
2477  {return options().drop_undefined_syms;}
2478 
2479  /// Setter for the flag that tells us if we are dropping functions
2480  /// and variables that have undefined symbols.
2481  ///
2482  /// @param f the new value of the flag.
2483  void
2484  drop_undefined_syms(bool f)
2485  {options().drop_undefined_syms = f;}
2486 
2487  /// Getter of the DWARF version.
2488  unsigned short
2489  dwarf_version() const
2490  {return dwarf_version_;}
2491 
2492  void
2493  dwarf_version(unsigned short v)
2494  {dwarf_version_ = v;}
2495 
2496  /// Return the ELF descriptor used for DWARF access.
2497  ///
2498  /// This can be the same as reader::elf_handle() above, if the
2499  /// DWARF info is in the same ELF file as the one of the binary we
2500  /// are analizing. It is different if e.g, the debug info is split
2501  /// from the ELF file we are analizing.
2502  ///
2503  /// @return a pointer to the ELF descriptor used to access debug
2504  /// info.
2505  Elf*
2506  dwarf_elf_handle() const
2507  {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2508 
2509  /// Test if the debug information is in a separate ELF file wrt the
2510  /// main ELF file of the program (application or shared library) we
2511  /// are analizing.
2512  ///
2513  /// @return true if the debug information is in a separate ELF file
2514  /// compared to the main ELF file of the program (application or
2515  /// shared library) that we are looking at.
2516  bool
2517  dwarf_is_splitted() const
2518  {return dwarf_elf_handle() != elf_handle();}
2519 
2520  /// Return the correct debug info, depending on the DIE source we
2521  /// are looking at.
2522  ///
2523  /// @param source the DIE source to consider.
2524  ///
2525  /// @return the right debug info, depending on @p source.
2526  const Dwarf*
2527  dwarf_per_die_source(die_source source) const
2528  {
2529  const Dwarf *result = 0;
2530  switch(source)
2531  {
2532  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2533  case TYPE_UNIT_DIE_SOURCE:
2534  result = dwarf_debug_info();
2535  break;
2536  case ALT_DEBUG_INFO_DIE_SOURCE:
2537  result = alternate_dwarf_debug_info();
2538  break;
2539  case NO_DEBUG_INFO_DIE_SOURCE:
2540  case NUMBER_OF_DIE_SOURCES:
2542  }
2543  return result;
2544  }
2545 
2546  /// Return the path to the ELF path we are reading.
2547  ///
2548  /// @return the elf path.
2549  const string&
2550  elf_path() const
2551  {return corpus_path();}
2552 
2553  const Dwarf_Die*
2554  cur_tu_die() const
2555  {return cur_tu_die_;}
2556 
2557  void
2558  cur_tu_die(Dwarf_Die* cur_tu_die)
2559  {cur_tu_die_ = cur_tu_die;}
2560 
2561  dwarf_expr_eval_context&
2562  dwarf_expr_eval_ctxt() const
2563  {return dwarf_expr_eval_context_;}
2564 
2565  /// Getter of the maps set that associates a representation of a
2566  /// decl DIE to a vector of offsets of DIEs having that representation.
2567  ///
2568  /// @return the maps set that associates a representation of a decl
2569  /// DIE to a vector of offsets of DIEs having that representation.
2570  const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2571  decl_die_repr_die_offsets_maps() const
2572  {return decl_die_repr_die_offsets_maps_;}
2573 
2574  /// Getter of the maps set that associates a representation of a
2575  /// decl DIE to a vector of offsets of DIEs having that representation.
2576  ///
2577  /// @return the maps set that associates a representation of a decl
2578  /// DIE to a vector of offsets of DIEs having that representation.
2579  die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2580  decl_die_repr_die_offsets_maps()
2581  {return decl_die_repr_die_offsets_maps_;}
2582 
2583  /// Getter of the maps set that associate a representation of a type
2584  /// DIE to a vector of offsets of DIEs having that representation.
2585  ///
2586  /// @return the maps set that associate a representation of a type
2587  /// DIE to a vector of offsets of DIEs having that representation.
2588  const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2589  type_die_repr_die_offsets_maps() const
2590  {return type_die_repr_die_offsets_maps_;}
2591 
2592  /// Getter of the maps set that associate a representation of a type
2593  /// DIE to a vector of offsets of DIEs having that representation.
2594  ///
2595  /// @return the maps set that associate a representation of a type
2596  /// DIE to a vector of offsets of DIEs having that representation.
2597  die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2598  type_die_repr_die_offsets_maps()
2599  {return type_die_repr_die_offsets_maps_;}
2600 
2601 
2602  /// Compute the offset of the canonical DIE of a given DIE.
2603  ///
2604  /// @param die the DIE to consider.
2605  ///
2606  /// @param canonical_die_offset out parameter. This is set to the
2607  /// resulting canonical DIE that was computed.
2608  ///
2609  /// @param die_as_type if yes, it means @p die has to be considered
2610  /// as a type.
2611  void
2612  compute_canonical_die_offset(const Dwarf_Die *die,
2613  Dwarf_Off &canonical_die_offset,
2614  bool die_as_type) const
2615  {
2616  offset_offset_map_type &canonical_dies =
2617  die_as_type
2618  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2619  get_container(*this, die)
2620  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2621  get_container(*this, die);
2622 
2623  Dwarf_Die canonical_die;
2624  compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2625 
2626  canonical_die_offset = dwarf_dieoffset(&canonical_die);
2627  }
2628 
2629  /// Compute (find) the canonical DIE of a given DIE.
2630  ///
2631  /// @param die the DIE to consider.
2632  ///
2633  /// @param canonical_dies the vector in which the canonical dies ar
2634  /// stored. The index of each element is the offset of the DIE we
2635  /// want the canonical DIE for. And the value of the element at
2636  /// that index is the canonical DIE offset we are looking for.
2637  ///
2638  /// @param canonical_die_offset out parameter. This is set to the
2639  /// resulting canonical DIE that was computed.
2640  ///
2641  /// @param die_as_type if yes, it means @p die has to be considered
2642  /// as a type.
2643  void
2644  compute_canonical_die(const Dwarf_Die *die,
2645  offset_offset_map_type& canonical_dies,
2646  Dwarf_Die &canonical_die,
2647  bool die_as_type) const
2648  {
2649  const die_source source = get_die_source(die);
2650 
2651  Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2652 
2653  compute_canonical_die(die_offset, source,
2654  canonical_dies,
2655  canonical_die, die_as_type);
2656  }
2657 
2658  /// Compute (find) the canonical DIE of a given DIE.
2659  ///
2660  /// @param die_offset the offset of the DIE to consider.
2661  ///
2662  /// @param source the source of the DIE to consider.
2663  ///
2664  /// @param canonical_dies the vector in which the canonical dies ar
2665  /// stored. The index of each element is the offset of the DIE we
2666  /// want the canonical DIE for. And the value of the element at
2667  /// that index is the canonical DIE offset we are looking for.
2668  ///
2669  /// @param canonical_die_offset out parameter. This is set to the
2670  /// resulting canonical DIE that was computed.
2671  ///
2672  /// @param die_as_type if yes, it means @p die has to be considered
2673  /// as a type.
2674  void
2675  compute_canonical_die(Dwarf_Off die_offset,
2676  die_source source,
2677  offset_offset_map_type& canonical_dies,
2678  Dwarf_Die &canonical_die,
2679  bool die_as_type) const
2680  {
2681  // The map that associates the string representation of 'die'
2682  // with a vector of offsets of potentially equivalent DIEs.
2684  die_as_type
2685  ? (const_cast<reader*>(this)->
2686  type_die_repr_die_offsets_maps().get_container(source))
2687  : (const_cast<reader*>(this)->
2688  decl_die_repr_die_offsets_maps().get_container(source));
2689 
2690  Dwarf_Die die;
2691  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2692  die_offset, &die));
2693 
2694  // The variable repr is the the string representation of 'die'.
2695  //
2696  // Even if die_as_type is true -- which means that 'die' is said
2697  // to be considered as a type -- we always consider a
2698  // DW_TAG_subprogram DIE as a decl here, as far as its string
2699  // representation is concerned.
2700  interned_string name =
2701  (die_as_type)
2702  ? get_die_pretty_type_representation(&die, /*where=*/0)
2703  : get_die_pretty_representation(&die, /*where=*/0);
2704 
2705  Dwarf_Off canonical_die_offset = 0;
2706  istring_dwarf_offsets_map_type::iterator i = map.find(name);
2707  if (i == map.end())
2708  {
2709  dwarf_offsets_type offsets;
2710  offsets.push_back(die_offset);
2711  map[name] = offsets;
2712  set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2713  get_die_from_offset(source, die_offset, &canonical_die);
2714  return;
2715  }
2716 
2717  Dwarf_Off cur_die_offset;
2718  Dwarf_Die potential_canonical_die;
2719  for (dwarf_offsets_type::const_iterator o = i->second.begin();
2720  o != i->second.end();
2721  ++o)
2722  {
2723  cur_die_offset = *o;
2724  get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2725  if (compare_dies(*this, &die, &potential_canonical_die,
2726  /*update_canonical_dies_on_the_fly=*/false))
2727  {
2728  canonical_die_offset = cur_die_offset;
2729  set_canonical_die_offset(canonical_dies, die_offset,
2730  canonical_die_offset);
2731  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2732  return;
2733  }
2734  }
2735 
2736  canonical_die_offset = die_offset;
2737  i->second.push_back(die_offset);
2738  set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2739  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2740  }
2741 
2742  /// Getter of the canonical DIE of a given DIE.
2743  ///
2744  /// @param die the DIE to consider.
2745  ///
2746  /// @param canonical_die output parameter. Is set to the resulting
2747  /// canonical die, if this function returns true.
2748  ///
2749  /// @param where the offset of the logical DIE we are supposed to be
2750  /// calling this function from. If set to zero this means this is
2751  /// to be ignored.
2752  ///
2753  /// @param die_as_type if set to yes, it means @p die is to be
2754  /// considered as a type DIE.
2755  ///
2756  /// @return true iff a canonical DIE was found for @p die.
2757  bool
2758  get_canonical_die(const Dwarf_Die *die,
2759  Dwarf_Die &canonical_die,
2760  size_t where,
2761  bool die_as_type)
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 die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2773  if (Dwarf_Off canonical_die_offset =
2774  get_canonical_die_offset(canonical_dies, die_offset))
2775  {
2776  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2777  return true;
2778  }
2779 
2780  // The map that associates the string representation of 'die'
2781  // with a vector of offsets of potentially equivalent DIEs.
2783  die_as_type
2784  ? (const_cast<reader*>(this)->
2785  type_die_repr_die_offsets_maps().get_container(*this, die))
2786  : (const_cast<reader*>(this)->
2787  decl_die_repr_die_offsets_maps().get_container(*this, die));
2788 
2789  // The variable repr is the the string representation of 'die'.
2790  //
2791  // Even if die_as_type is true -- which means that 'die' is said
2792  // to be considered as a type -- we always consider a
2793  // DW_TAG_subprogram DIE as a decl here, as far as its string
2794  // representation is concerned.
2795  interned_string name =
2796  (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2797  ? get_die_pretty_type_representation(die, where)
2798  : get_die_pretty_representation(die, where);
2799 
2800  istring_dwarf_offsets_map_type::iterator i = map.find(name);
2801  if (i == map.end())
2802  return false;
2803 
2804  Dwarf_Off cur_die_offset;
2805  for (dwarf_offsets_type::const_iterator o = i->second.begin();
2806  o != i->second.end();
2807  ++o)
2808  {
2809  cur_die_offset = *o;
2810  get_die_from_offset(source, cur_die_offset, &canonical_die);
2811  // compare die and canonical_die.
2812  if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2813  die, &canonical_die,
2814  /*update_canonical_dies_on_the_fly=*/true))
2815  {
2816  set_canonical_die_offset(canonical_dies,
2817  die_offset,
2818  cur_die_offset);
2819  return true;
2820  }
2821  }
2822 
2823  return false;
2824  }
2825 
2826  /// Retrieve the canonical DIE of a given DIE.
2827  ///
2828  /// The canonical DIE is a DIE that is structurally equivalent to
2829  /// this one.
2830  ///
2831  /// Note that this function caches the canonical DIE that was
2832  /// computed. Subsequent invocations of this function on the same
2833  /// DIE return the same cached DIE.
2834  ///
2835  /// @param die the DIE to get a canonical type for.
2836  ///
2837  /// @param canonical_die the resulting canonical DIE.
2838  ///
2839  /// @param where the offset of the logical DIE we are supposed to be
2840  /// calling this function from. If set to zero this means this is
2841  /// to be ignored.
2842  ///
2843  /// @param die_as_type if true, consider DIE is a type.
2844  ///
2845  /// @return true if an *existing* canonical DIE was found.
2846  /// Otherwise, @p die is considered as being a canonical DIE for
2847  /// itself. @p canonical_die is thus set to the canonical die in
2848  /// either cases.
2849  bool
2850  get_or_compute_canonical_die(const Dwarf_Die* die,
2851  Dwarf_Die& canonical_die,
2852  size_t where,
2853  bool die_as_type) const
2854  {
2855  const die_source source = get_die_source(die);
2856 
2857  offset_offset_map_type &canonical_dies =
2858  die_as_type
2859  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2860  get_container(source)
2861  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2862  get_container(source);
2863 
2864  Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2865 
2866  if (Dwarf_Off canonical_die_offset =
2867  get_canonical_die_offset(canonical_dies,
2868  initial_die_offset))
2869  {
2870  get_die_from_offset(source, canonical_die_offset, &canonical_die);
2871  return true;
2872  }
2873 
2874  if (!is_type_die_to_be_canonicalized(die))
2875  return false;
2876 
2877  // The map that associates the string representation of 'die'
2878  // with a vector of offsets of potentially equivalent DIEs.
2880  die_as_type
2881  ? (const_cast<reader*>(this)->
2882  type_die_repr_die_offsets_maps().get_container(*this, die))
2883  : (const_cast<reader*>(this)->
2884  decl_die_repr_die_offsets_maps().get_container(*this, die));
2885 
2886  // The variable repr is the the string representation of 'die'.
2887  //
2888  // Even if die_as_type is true -- which means that 'die' is said
2889  // to be considered as a type -- we always consider a
2890  // DW_TAG_subprogram DIE as a decl here, as far as its string
2891  // representation is concerned.
2892  interned_string name =
2893  (die_as_type)
2894  ? get_die_pretty_type_representation(die, where)
2895  : get_die_pretty_representation(die, where);
2896 
2897  istring_dwarf_offsets_map_type::iterator i = map.find(name);
2898  if (i == map.end())
2899  {
2900  dwarf_offsets_type offsets;
2901  offsets.push_back(initial_die_offset);
2902  map[name] = offsets;
2903  get_die_from_offset(source, initial_die_offset, &canonical_die);
2904  set_canonical_die_offset(canonical_dies,
2905  initial_die_offset,
2906  initial_die_offset);
2907  return false;
2908  }
2909 
2910  // walk i->second without any iterator (using a while loop rather
2911  // than a for loop) because compare_dies might add new content to
2912  // the end of the i->second vector during the walking.
2913  dwarf_offsets_type::size_type n = 0, s = i->second.size();
2914  while (n < s)
2915  {
2916  Dwarf_Off die_offset = i->second[n];
2917  get_die_from_offset(source, die_offset, &canonical_die);
2918  // compare die and canonical_die.
2919  if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2920  die, &canonical_die,
2921  /*update_canonical_dies_on_the_fly=*/true))
2922  {
2923  set_canonical_die_offset(canonical_dies,
2924  initial_die_offset,
2925  die_offset);
2926  return true;
2927  }
2928  ++n;
2929  }
2930 
2931  // We didn't find a canonical DIE for 'die'. So let's consider
2932  // that it is its own canonical DIE.
2933  get_die_from_offset(source, initial_die_offset, &canonical_die);
2934  i->second.push_back(initial_die_offset);
2935  set_canonical_die_offset(canonical_dies,
2936  initial_die_offset,
2937  initial_die_offset);
2938 
2939  return false;
2940  }
2941 
2942  /// Get the source of the DIE.
2943  ///
2944  /// The function returns an enumerator value saying if the DIE comes
2945  /// from the .debug_info section of the primary debug info file, the
2946  /// .debug_info section of the alternate debug info file, or the
2947  /// .debug_types section.
2948  ///
2949  /// @param die the DIE to get the source of.
2950  ///
2951  /// @return the source of the DIE if it could be determined,
2952  /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2953  die_source
2954  get_die_source(const Dwarf_Die *die) const
2955  {
2956  die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2957  ABG_ASSERT(die);
2958  ABG_ASSERT(get_die_source(*die, source));
2959  return source;
2960  }
2961 
2962  /// Get the source of the DIE.
2963  ///
2964  /// The function returns an enumerator value saying if the DIE comes
2965  /// from the .debug_info section of the primary debug info file, the
2966  /// .debug_info section of the alternate debug info file, or the
2967  /// .debug_types section.
2968  ///
2969  /// @param die the DIE to get the source of.
2970  ///
2971  /// @param source out parameter. The function sets this parameter
2972  /// to the source of the DIE @p iff it returns true.
2973  ///
2974  /// @return true iff the source of the DIE could be determined and
2975  /// returned.
2976  bool
2977  get_die_source(const Dwarf_Die &die, die_source &source) const
2978  {
2979  Dwarf_Die cu_die;
2980  Dwarf_Die cu_kind;
2981  uint8_t address_size = 0, offset_size = 0;
2982  if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2983  &cu_die, &address_size,
2984  &offset_size))
2985  return false;
2986 
2987  Dwarf_Half version = 0;
2988  Dwarf_Off abbrev_offset = 0;
2989  uint64_t type_signature = 0;
2990  Dwarf_Off type_offset = 0;
2991  if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2992  &version, &abbrev_offset,
2993  &address_size, &offset_size,
2994  &type_signature, &type_offset))
2995  return false;
2996 
2997  int tag = dwarf_tag(&cu_kind);
2998 
2999  if (tag == DW_TAG_compile_unit
3000  || tag == DW_TAG_partial_unit)
3001  {
3002  const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
3003  if (dwarf_debug_info() == die_dwarf)
3004  source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
3005  else if (alternate_dwarf_debug_info() == die_dwarf)
3006  source = ALT_DEBUG_INFO_DIE_SOURCE;
3007  else
3009  }
3010  else if (tag == DW_TAG_type_unit)
3011  source = TYPE_UNIT_DIE_SOURCE;
3012  else
3013  return false;
3014 
3015  return true;
3016  }
3017 
3018  /// Getter for the DIE designated by an offset.
3019  ///
3020  /// @param source the source of the DIE to get.
3021  ///
3022  /// @param offset the offset of the DIE to get.
3023  ///
3024  /// @param die the resulting DIE. The pointer has to point to an
3025  /// allocated memory region.
3026  void
3027  get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
3028  {
3029  if (source == TYPE_UNIT_DIE_SOURCE)
3030  ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3031  offset, die));
3032  else
3033  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3034  offset, die));
3035  }
3036 
3037 public:
3038 
3039  /// Add an entry to the relevant die->decl map.
3040  ///
3041  /// @param die the DIE to add the the map.
3042  ///
3043  /// @param decl the decl to consider.
3044  ///
3045  /// @param where_offset where in the DIE stream we logically are.
3046  ///
3047  /// @param do_associate_by_repr if true then this function
3048  /// associates the representation string of @p die with the
3049  /// declaration @p decl, in a corpus-wide manner. That is, in the
3050  /// entire current corpus, there is going to be just one declaration
3051  /// associated with a DIE of the string representation of @p die.
3052  ///
3053  /// @param do_associate_by_repr_per_tu if true, then this function
3054  /// associates the representation string of @p die with the
3055  /// declaration @p decl in a translation unit wide manner. That is,
3056  /// in the entire current translation unit, there is going to be
3057  /// just one declaration associated with a DIE of the string
3058  /// representation of @p die.
3059  void
3060  associate_die_to_decl(Dwarf_Die* die,
3061  decl_base_sptr decl,
3062  size_t where_offset,
3063  bool do_associate_by_repr = false)
3064  {
3065  const die_source source = get_die_source(die);
3066 
3068  decl_die_artefact_maps().get_container(source);
3069 
3070  size_t die_offset;
3071  if (do_associate_by_repr)
3072  {
3073  Dwarf_Die equiv_die;
3074  if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
3075  /*die_as_type=*/false))
3076  return;
3077  die_offset = dwarf_dieoffset(&equiv_die);
3078  }
3079  else
3080  die_offset = dwarf_dieoffset(die);
3081 
3082  m[die_offset] = decl;
3083  }
3084 
3085  /// Lookup the decl for a given DIE.
3086  ///
3087  /// The returned decl is either the decl of the DIE that as the
3088  /// exact offset @p die_offset
3089  /// die_offset, or
3090  /// give
3091  ///
3092  /// @param die_offset the offset of the DIE to consider.
3093  ///
3094  /// @param source where the DIE represented by @p die_offset comes
3095  /// from.
3096  ///
3097  /// Note that "alternate debug info sections" is a GNU extension as
3098  /// of DWARF4 and is described at
3099  /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3100  ///
3101  /// @return the resulting decl, or null if no decl is associated to
3102  /// the DIE represented by @p die_offset.
3103  decl_base_sptr
3104  lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3105  {
3106  decl_base_sptr result =
3107  is_decl(lookup_artifact_from_die_offset(die_offset, source,
3108  /*die_as_type=*/false));
3109 
3110  return result;
3111  }
3112 
3113  /// Get the qualified name of a given DIE.
3114  ///
3115  /// If the name of the DIE was already computed before just return
3116  /// that name from a cache. Otherwise, build the name, cache it and
3117  /// return it.
3118  ///
3119  /// @param die the DIE to consider.
3120  ///
3121  /// @param where_offset where in the DIE stream we logically are.
3122  ///
3123  /// @return the interned string representing the qualified name of
3124  /// @p die.
3125  interned_string
3126  get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3127  {
3128  ABG_ASSERT(die);
3129  die_istring_map_type& map =
3130  die_qualified_name_maps_.get_container(*this, die);
3131 
3132  size_t die_offset = dwarf_dieoffset(die);
3133  die_istring_map_type::const_iterator i = map.find(die_offset);
3134 
3135  if (i == map.end())
3136  {
3137  reader& rdr = *const_cast<reader*>(this);
3138  string qualified_name = die_qualified_name(rdr, die, where_offset);
3139  interned_string istr = env().intern(qualified_name);
3140  map[die_offset] = istr;
3141  return istr;
3142  }
3143 
3144  return i->second;
3145  }
3146 
3147  /// Get the qualified name of a given DIE.
3148  ///
3149  /// If the name of the DIE was already computed before just return
3150  /// that name from a cache. Otherwise, build the name, cache it and
3151  /// return it.
3152  ///
3153  /// @param die the DIE to consider.
3154  ///
3155  /// @param where_offset where in the DIE stream we logically are.
3156  ///
3157  /// @return the interned string representing the qualified name of
3158  /// @p die.
3159  interned_string
3160  get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3161  {
3162  return const_cast<reader*>(this)->
3163  get_die_qualified_name(die, where_offset);
3164  }
3165 
3166  /// Get the qualified name of a given DIE which is considered to be
3167  /// the DIE for a type.
3168  ///
3169  /// For instance, for a DW_TAG_subprogram DIE, this function
3170  /// computes the name of the function *type* that corresponds to the
3171  /// function.
3172  ///
3173  /// If the name of the DIE was already computed before just return
3174  /// that name from a cache. Otherwise, build the name, cache it and
3175  /// return it.
3176  ///
3177  /// @param die the DIE to consider.
3178  ///
3179  /// @param where_offset where in the DIE stream we logically are.
3180  ///
3181  /// @return the interned string representing the qualified name of
3182  /// @p die.
3183  interned_string
3184  get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3185  {
3186  ABG_ASSERT(die);
3187 
3188  // The name of the translation unit die is "".
3189  if (die == cur_tu_die())
3190  return env().intern("");
3191 
3192  die_istring_map_type& map =
3193  die_qualified_name_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 =
3198  map.find(die_offset);
3199 
3200  if (i == map.end())
3201  {
3202  reader& rdr = *const_cast<reader*>(this);
3203  string qualified_name;
3204  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3205  if ((tag == DW_TAG_structure_type
3206  || tag == DW_TAG_class_type
3207  || tag == DW_TAG_union_type)
3208  && die_is_anonymous(die))
3209  {
3210  location l = die_location(*this, die);
3211  qualified_name = l ? l.expand() : "noloc";
3212  qualified_name = "unnamed-at-" + qualified_name;
3213  }
3214  else
3215  qualified_name =
3216  die_qualified_type_name(rdr, die, where_offset);
3217 
3218  interned_string istr = env().intern(qualified_name);
3219  map[die_offset] = istr;
3220  return istr;
3221  }
3222 
3223  return i->second;
3224  }
3225 
3226  /// Get the pretty representation of a DIE that represents a type.
3227  ///
3228  /// For instance, for the DW_TAG_subprogram, this function computes
3229  /// the pretty representation of the type of the function, not the
3230  /// pretty representation of the function declaration.
3231  ///
3232  /// Once the pretty representation is computed, it's stored in a
3233  /// cache. Subsequent invocations of this function on the same DIE
3234  /// will yield the cached name.
3235  ///
3236  /// @param die the DIE to consider.
3237  ///
3238  /// @param where_offset where in the DIE stream we logically are.
3239  ///
3240  /// @return the interned_string that represents the pretty
3241  /// representation.
3242  interned_string
3243  get_die_pretty_type_representation(const Dwarf_Die *die,
3244  size_t where_offset) const
3245  {
3246  ABG_ASSERT(die);
3247  die_istring_map_type& map =
3248  die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3249  die);
3250 
3251  size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3252  die_istring_map_type::const_iterator i = map.find(die_offset);
3253 
3254  if (i == map.end())
3255  {
3256  reader& rdr = *const_cast<reader*>(this);
3257  string pretty_representation =
3258  die_pretty_print_type(rdr, die, where_offset);
3259  interned_string istr = env().intern(pretty_representation);
3260  map[die_offset] = istr;
3261  return istr;
3262  }
3263 
3264  return i->second;
3265  }
3266 
3267  /// Get the pretty representation of a DIE.
3268  ///
3269  /// Once the pretty representation is computed, it's stored in a
3270  /// cache. Subsequent invocations of this function on the same DIE
3271  /// will yield the cached name.
3272  ///
3273  /// @param die the DIE to consider.
3274  ///
3275  /// @param where_offset where in the DIE stream we logically are.
3276  ///
3277  /// @return the interned_string that represents the pretty
3278  /// representation.
3279  interned_string
3280  get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3281  {
3282  ABG_ASSERT(die);
3283 
3284  die_istring_map_type& map =
3285  die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3286  die);
3287 
3288  size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3289  die_istring_map_type::const_iterator i = map.find(die_offset);
3290 
3291  if (i == map.end())
3292  {
3293  reader& rdr = *const_cast<reader*>(this);
3294  string pretty_representation =
3295  die_pretty_print(rdr, die, where_offset);
3296  interned_string istr = env().intern(pretty_representation);
3297  map[die_offset] = istr;
3298  return istr;
3299  }
3300 
3301  return i->second;
3302  }
3303 
3304  /// Lookup the artifact that was built to represent a type that has
3305  /// the same pretty representation as the type denoted by a given
3306  /// DIE.
3307  ///
3308  /// Note that the DIE must have previously been associated with the
3309  /// artifact using the functions associate_die_to_decl or
3310  /// associate_die_to_type.
3311  ///
3312  /// Also, note that the scope of the lookup is the current ABI
3313  /// corpus.
3314  ///
3315  /// @param die the DIE to consider.
3316  ///
3317  /// @param where_offset where in the DIE stream we logically are.
3318  ///
3319  /// @return the type artifact found.
3321  lookup_type_artifact_from_die(Dwarf_Die *die) const
3322  {
3323  type_or_decl_base_sptr artifact =
3324  lookup_artifact_from_die(die, /*type_as_die=*/true);
3325  if (function_decl_sptr fn = is_function_decl(artifact))
3326  return fn->get_type();
3327  return artifact;
3328  }
3329 
3330  /// Lookup the artifact that was built to represent a type or a
3331  /// declaration that has the same pretty representation as the type
3332  /// denoted by a given DIE.
3333  ///
3334  /// Note that the DIE must have previously been associated with the
3335  /// artifact using the functions associate_die_to_decl or
3336  /// associate_die_to_type.
3337  ///
3338  /// Also, note that the scope of the lookup is the current ABI
3339  /// corpus.
3340  ///
3341  /// @param die the DIE to consider.
3342  ///
3343  /// @param where_offset where in the DIE stream we logically are.
3344  ///
3345  /// @param die_as_type if true, it means the DIE is to be considered
3346  /// as a type.
3347  ///
3348  /// @return the artifact found.
3350  lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3351  {
3352  Dwarf_Die equiv_die;
3353  if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3354  return type_or_decl_base_sptr();
3355 
3356  const die_artefact_map_type& m =
3357  die_as_type
3358  ? type_die_artefact_maps().get_container(*this, &equiv_die)
3359  : decl_die_artefact_maps().get_container(*this, &equiv_die);
3360 
3361  size_t die_offset = dwarf_dieoffset(&equiv_die);
3362  die_artefact_map_type::const_iterator i = m.find(die_offset);
3363 
3364  if (i == m.end())
3365  return type_or_decl_base_sptr();
3366  return i->second;
3367  }
3368 
3369  /// Lookup the artifact that was built to represent a type or a
3370  /// declaration that has the same pretty representation as the type
3371  /// denoted by the offset of a given DIE.
3372  ///
3373  /// Note that the DIE must have previously been associated with the
3374  /// artifact using either associate_die_to_decl or
3375  /// associate_die_to_type.
3376  ///
3377  /// Also, note that the scope of the lookup is the current ABI
3378  /// corpus.
3379  ///
3380  /// @param die the DIE to consider.
3381  ///
3382  /// @param where_offset where in the DIE stream we logically are.
3383  ///
3384  /// @param die_as_type if true, it means the DIE is to be considered
3385  /// as a type.
3386  ///
3387  /// @return the artifact found.
3389  lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3390  die_source source,
3391  bool die_as_type = false) const
3392  {
3393  const die_artefact_map_type& m =
3394  die_as_type
3395  ? type_die_artefact_maps().get_container(source)
3396  : decl_die_artefact_maps().get_container(source);
3397 
3398  die_artefact_map_type::const_iterator i = m.find(die_offset);
3399  if (i == m.end())
3400  return type_or_decl_base_sptr();
3401  return i->second;
3402  }
3403 
3404  /// Check if we can assume the One Definition Rule[1] to be relevant
3405  /// for the current translation unit.
3406  ///
3407  /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3408  ///
3409  /// At the moment this returns true if the current translation unit
3410  /// is in C++ language. In that case, it's relevant to assume that
3411  /// we use optimizations based on the ODR.
3412  bool
3413  odr_is_relevant() const
3414  {return odr_is_relevant(cur_transl_unit()->get_language());}
3415 
3416  /// Check if we can assume the One Definition Rule[1] to be relevant
3417  /// for a given language.
3418  ///
3419  /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3420  ///
3421  /// At the moment this returns true if the language considered
3422  /// is C++, Java or Ada.
3423  bool
3425  {
3426  return (is_cplus_plus_language(l)
3427  || is_java_language(l)
3428  || is_ada_language(l));
3429  }
3430 
3431  /// Check if we can assume the One Definition Rule to be relevant
3432  /// for a given DIE.
3433  ///
3434  /// @param die the DIE to consider.
3435  ///
3436  /// @return true if the ODR is relevant for @p die.
3437  bool
3438  odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3439  {
3440  Dwarf_Die die;
3441  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3442  die_offset, &die));
3443  return odr_is_relevant(&die);
3444  }
3445 
3446  /// Check if we can assume the One Definition Rule to be relevant
3447  /// for a given DIE.
3448  ///
3449  /// @param die the DIE to consider.
3450  ///
3451  /// @return true if the ODR is relevant for @p die.
3452  bool
3453  odr_is_relevant(const Dwarf_Die *die) const
3454  {
3456  if (!get_die_language(die, lang))
3457  return odr_is_relevant();
3458 
3459  return odr_is_relevant(lang);
3460  }
3461 
3462  /// Getter for the maps set that associates a decl DIE offset to an
3463  /// artifact.
3464  ///
3465  /// @return the maps set that associates a decl DIE offset to an
3466  /// artifact.
3467  die_source_dependant_container_set<die_artefact_map_type>&
3468  decl_die_artefact_maps()
3469  {return decl_die_artefact_maps_;}
3470 
3471  /// Getter for the maps set that associates a decl DIE offset to an
3472  /// artifact.
3473  ///
3474  /// @return the maps set that associates a decl DIE offset to an
3475  /// artifact.
3476  const die_source_dependant_container_set<die_artefact_map_type>&
3477  decl_die_artefact_maps() const
3478  {return decl_die_artefact_maps_;}
3479 
3480  /// Getter for the maps set that associates a type DIE offset to an
3481  /// artifact.
3482  ///
3483  /// @return the maps set that associates a type DIE offset to an
3484  /// artifact.
3485  die_source_dependant_container_set<die_artefact_map_type>&
3486  type_die_artefact_maps()
3487  {return type_die_artefact_maps_;}
3488 
3489  /// Getter for the maps set that associates a type DIE offset to an
3490  /// artifact.
3491  ///
3492  /// @return the maps set that associates a type DIE offset to an
3493  /// artifact.
3494  const die_source_dependant_container_set<die_artefact_map_type>&
3495  type_die_artefact_maps() const
3496  {return type_die_artefact_maps_;}
3497 
3498  /// Getter of the maps that associates function type representations
3499  /// to function types, inside a translation unit.
3500  ///
3501  /// @return the maps that associates function type representations
3502  /// to function types, inside a translation unit.
3504  per_tu_repr_to_fn_type_maps()
3505  {return per_tu_repr_to_fn_type_maps_;}
3506 
3507  /// Getter of the maps that associates function type representations
3508  /// to function types, inside a translation unit.
3509  ///
3510  /// @return the maps that associates function type representations
3511  /// to function types, inside a translation unit.
3513  per_tu_repr_to_fn_type_maps() const
3514  {return per_tu_repr_to_fn_type_maps_;}
3515 
3516  /// Associate the representation of a function type DIE to a given
3517  /// function type, inside the current translation unit.
3518  ///
3519  /// @param die the DIE to associate to the function type, using its
3520  /// representation.
3521  ///
3522  /// @param fn_type the function type to associate to @p die.
3523  void
3524  associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3525  const function_type_sptr &fn_type)
3526  {
3527  if (!die_is_function_type(die))
3528  return;
3529 
3530  interned_string repr =
3531  get_die_pretty_type_representation(die, /*where=*/0);
3532  ABG_ASSERT(!repr.empty());
3533 
3534  per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3535  }
3536 
3537  /// Lookup the function type associated to a given function type
3538  /// DIE, in the current translation unit.
3539  ///
3540  /// @param die the DIE of function type to consider.
3541  ///
3542  /// @return the @ref function_type_sptr associated to @p die, or nil
3543  /// of no function_type is associated to @p die.
3545  lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3546  {
3547  if (!die_is_function_type(die))
3548  return function_type_sptr();
3549 
3550  interned_string repr = die_name(die).empty() ?
3551  get_die_pretty_type_representation(die, /*where=*/0)
3552  : get_die_pretty_representation(die, /*where=*/0);
3553  ABG_ASSERT(!repr.empty());
3554 
3555  istring_fn_type_map_type::const_iterator i =
3556  per_tu_repr_to_fn_type_maps().find(repr);
3557 
3558  if (i == per_tu_repr_to_fn_type_maps().end())
3559  return function_type_sptr();
3560 
3561  return i->second;
3562  }
3563 
3564  /// Set the canonical DIE offset of a given DIE.
3565  ///
3566  /// @param canonical_dies the vector that holds canonical DIEs.
3567  ///
3568  /// @param die_offset the offset of the DIE to set the canonical DIE
3569  /// for.
3570  ///
3571  /// @param canonical_die_offset the canonical DIE offset to
3572  /// associate to @p die_offset.
3573  void
3574  set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3575  Dwarf_Off die_offset,
3576  Dwarf_Off canonical_die_offset) const
3577  {
3578  canonical_dies[die_offset] = canonical_die_offset;}
3579 
3580  /// Set the canonical DIE offset of a given DIE.
3581  ///
3582  ///
3583  /// @param die_offset the offset of the DIE to set the canonical DIE
3584  /// for.
3585  ///
3586  /// @param source the source of the DIE denoted by @p die_offset.
3587  ///
3588  /// @param canonical_die_offset the canonical DIE offset to
3589  /// associate to @p die_offset.
3590  ///
3591  /// @param die_as_type if true, it means that @p die_offset has to
3592  /// be considered as a type.
3593  void
3594  set_canonical_die_offset(Dwarf_Off die_offset,
3595  die_source source,
3596  Dwarf_Off canonical_die_offset,
3597  bool die_as_type) const
3598  {
3599  offset_offset_map_type &canonical_dies =
3600  die_as_type
3601  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3602  get_container(source)
3603  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3604  get_container(source);
3605 
3606  set_canonical_die_offset(canonical_dies,
3607  die_offset,
3608  canonical_die_offset);
3609  }
3610 
3611  /// Set the canonical DIE offset of a given DIE.
3612  ///
3613  ///
3614  /// @param die the DIE to set the canonical DIE for.
3615  ///
3616  /// @param canonical_die_offset the canonical DIE offset to
3617  /// associate to @p die_offset.
3618  ///
3619  /// @param die_as_type if true, it means that @p die has to be
3620  /// considered as a type.
3621  void
3622  set_canonical_die_offset(const Dwarf_Die *die,
3623  Dwarf_Off canonical_die_offset,
3624  bool die_as_type) const
3625  {
3626  const die_source source = get_die_source(die);
3627 
3628  Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3629 
3630  set_canonical_die_offset(die_offset, source,
3631  canonical_die_offset,
3632  die_as_type);
3633  }
3634 
3635  /// Get the canonical DIE offset of a given DIE.
3636  ///
3637  /// @param canonical_dies the vector that contains canonical DIES.
3638  ///
3639  /// @param die_offset the offset of the DIE to consider.
3640  ///
3641  /// @return the canonical of the DIE denoted by @p die_offset, or
3642  /// zero if no canonical DIE was found.
3643  Dwarf_Off
3644  get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3645  Dwarf_Off die_offset) const
3646  {
3647  offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3648  if (it == canonical_dies.end())
3649  return 0;
3650  return it->second;
3651  }
3652 
3653  /// Get the canonical DIE offset of a given DIE.
3654  ///
3655  /// @param die_offset the offset of the DIE to consider.
3656  ///
3657  /// @param source the source of the DIE denoted by @p die_offset.
3658  ///
3659  /// @param die_as_type if true, it means that @p is to be considered
3660  /// as a type DIE.
3661  ///
3662  /// @return the canonical of the DIE denoted by @p die_offset, or
3663  /// zero if no canonical DIE was found.
3664  Dwarf_Off
3665  get_canonical_die_offset(Dwarf_Off die_offset,
3666  die_source source,
3667  bool die_as_type) const
3668  {
3669  offset_offset_map_type &canonical_dies =
3670  die_as_type
3671  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3672  get_container(source)
3673  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3674  get_container(source);
3675 
3676  return get_canonical_die_offset(canonical_dies, die_offset);
3677  }
3678 
3679  /// Erase the canonical type of a given DIE.
3680  ///
3681  /// @param die_offset the offset of the DIE to consider.
3682  ///
3683  /// @param source the source of the canonical type.
3684  ///
3685  /// @param die_as_type if true, it means that @p is to be considered
3686  /// as a type DIE.
3687  ///
3688  /// @return the canonical of the DIE denoted by @p die_offset, or
3689  /// zero if no canonical DIE was found and erased..
3690  bool
3691  erase_canonical_die_offset(Dwarf_Off die_offset,
3692  die_source source,
3693  bool die_as_type) const
3694  {
3695  offset_offset_map_type &canonical_dies =
3696  die_as_type
3697  ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3698  get_container(source)
3699  : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3700  get_container(source);
3701 
3702  return canonical_dies.erase(die_offset);
3703  }
3704 
3705 
3706  /// Associate a DIE (representing a type) to the type that it
3707  /// represents.
3708  ///
3709  /// @param die the DIE to consider.
3710  ///
3711  /// @param type the type to associate the DIE to.
3712  ///
3713  /// @param where_offset where in the DIE stream we logically are.
3714  void
3715  associate_die_to_type(const Dwarf_Die *die,
3716  type_base_sptr type,
3717  size_t where)
3718  {
3719  if (!type)
3720  return;
3721 
3722  Dwarf_Die equiv_die;
3723  if (!get_or_compute_canonical_die(die, equiv_die, where,
3724  /*die_as_type=*/true))
3725  return;
3726 
3728  type_die_artefact_maps().get_container(*this, &equiv_die);
3729 
3730  size_t die_offset = dwarf_dieoffset(&equiv_die);
3731  m[die_offset] = type;
3732  }
3733 
3734  /// Lookup the type associated to a given DIE.
3735  ///
3736  /// Note that the DIE must have been associated to type by a
3737  /// previous invocation of the function
3738  /// reader::associate_die_to_type().
3739  ///
3740  /// @param die 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(const Dwarf_Die* die) const
3746  {
3747  type_or_decl_base_sptr artifact =
3748  lookup_artifact_from_die(die, /*die_as_type=*/true);
3749  if (function_decl_sptr fn = is_function_decl(artifact))
3750  return fn->get_type();
3751  return is_type(artifact);
3752  }
3753 
3754  /// Lookup the type associated to a DIE at a given offset, from a
3755  /// given source.
3756  ///
3757  /// Note that the DIE must have been associated to type by a
3758  /// previous invocation of the function
3759  /// reader::associate_die_to_type().
3760  ///
3761  /// @param die_offset the offset of the DIE to consider.
3762  ///
3763  /// @param source the source of the DIE to consider.
3764  ///
3765  /// @return the type associated to the DIE or NULL if no type is
3766  /// associated to the DIE.
3767  type_base_sptr
3768  lookup_type_from_die_offset(size_t die_offset, die_source source) const
3769  {
3770  type_base_sptr result;
3771  const die_artefact_map_type& m =
3772  type_die_artefact_maps().get_container(source);
3773  die_artefact_map_type::const_iterator i = m.find(die_offset);
3774  if (i != m.end())
3775  {
3776  if (function_decl_sptr fn = is_function_decl(i->second))
3777  return fn->get_type();
3778  result = is_type(i->second);
3779  }
3780 
3781  if (!result)
3782  {
3783  // Maybe we are looking for a class type being constructed?
3784  const die_class_or_union_map_type& m = die_wip_classes_map(source);
3785  die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3786 
3787  if (i != m.end())
3788  result = i->second;
3789  }
3790 
3791  if (!result)
3792  {
3793  // Maybe we are looking for a function type being constructed?
3794  const die_function_type_map_type& m =
3795  die_wip_function_types_map(source);
3796  die_function_type_map_type::const_iterator i = m.find(die_offset);
3797 
3798  if (i != m.end())
3799  result = i->second;
3800  }
3801 
3802  return result;
3803  }
3804 
3805  /// Getter of a map that associates a die that represents a
3806  /// class/struct with the declaration of the class, while the class
3807  /// is being constructed.
3808  ///
3809  /// @param source where the DIE is from.
3810  ///
3811  /// @return the map that associates a DIE to the class that is being
3812  /// built.
3814  die_wip_classes_map(die_source source) const
3815  {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3816 
3817  /// Getter of a map that associates a die that represents a
3818  /// class/struct with the declaration of the class, while the class
3819  /// is being constructed.
3820  ///
3821  /// @param source where the DIE comes from.
3822  ///
3823  /// @return the map that associates a DIE to the class that is being
3824  /// built.
3826  die_wip_classes_map(die_source source)
3827  {
3828  switch (source)
3829  {
3830  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3831  break;
3832  case ALT_DEBUG_INFO_DIE_SOURCE:
3833  return alternate_die_wip_classes_map_;
3834  case TYPE_UNIT_DIE_SOURCE:
3835  return type_unit_die_wip_classes_map_;
3836  case NO_DEBUG_INFO_DIE_SOURCE:
3837  case NUMBER_OF_DIE_SOURCES:
3839  }
3840  return die_wip_classes_map_;
3841  }
3842 
3843  /// Getter for a map that associates a die (that represents a
3844  /// function type) whith a function type, while the function type is
3845  /// being constructed (WIP == work in progress).
3846  ///
3847  /// @param source where the DIE comes from.n
3848  ///
3849  /// @return the map of wip function types.
3851  die_wip_function_types_map(die_source source) const
3852  {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3853 
3854  /// Getter for a map that associates a die (that represents a
3855  /// function type) whith a function type, while the function type is
3856  /// being constructed (WIP == work in progress).
3857  ///
3858  /// @param source where DIEs of the map come from.
3859  ///
3860  /// @return the map of wip function types.
3862  die_wip_function_types_map(die_source source)
3863  {
3864  switch (source)
3865  {
3866  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3867  break;
3868  case ALT_DEBUG_INFO_DIE_SOURCE:
3869  return alternate_die_wip_function_types_map_;
3870  case TYPE_UNIT_DIE_SOURCE:
3871  return type_unit_die_wip_function_types_map_;
3872  case NO_DEBUG_INFO_DIE_SOURCE:
3873  case NUMBER_OF_DIE_SOURCES:
3875  }
3876  return die_wip_function_types_map_;
3877  }
3878 
3879  /// Getter for a map that associates a die with a function decl
3880  /// which has a linkage name but no elf symbol yet.
3881  ///
3882  /// This is to fixup function decls with linkage names, but with no
3883  /// link to their underlying elf symbol. There are some DIEs like
3884  /// that in DWARF sometimes, especially when the compiler optimizes
3885  /// stuff aggressively.
3887  die_function_decl_with_no_symbol_map()
3888  {return die_function_with_no_symbol_map_;}
3889 
3890  /// Return true iff a given offset is for the DIE of a class that is
3891  /// being built, but that is not fully built yet. WIP == "work in
3892  /// progress".
3893  ///
3894  /// @param offset the DIE offset to consider.
3895  ///
3896  /// @param source where the DIE of the map come from.
3897  ///
3898  /// @return true iff @p offset is the offset of the DIE of a class
3899  /// that is being currently built.
3900  bool
3901  is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3902  {
3903  die_class_or_union_map_type::const_iterator i =
3904  die_wip_classes_map(source).find(offset);
3905  return (i != die_wip_classes_map(source).end());
3906  }
3907 
3908  /// Return true iff a given offset is for the DIE of a function type
3909  /// that is being built at the moment, but is not fully built yet.
3910  /// WIP == work in progress.
3911  ///
3912  /// @param offset DIE offset to consider.
3913  ///
3914  /// @param source where the DIE comes from.
3915  ///
3916  /// @return true iff @p offset is the offset of the DIE of a
3917  /// function type that is being currently built.
3918  bool
3919  is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3920  {
3921  die_function_type_map_type::const_iterator i =
3922  die_wip_function_types_map(source).find(offset);
3923  return (i != die_wip_function_types_map(source).end());
3924  }
3925 
3926  /// Sometimes, a data member die can erroneously have an empty name as
3927  /// a result of a bug of the DWARF emitter.
3928  ///
3929  /// This is what happens in
3930  /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3931  ///
3932  /// In that case, this function constructs an artificial name for that
3933  /// data member. The pattern of the name is as follows:
3934  ///
3935  /// "unnamed-@-<location>".
3936  ///
3937  ///location is either the value of the data member location of the
3938  ///data member if it has one or concatenation of its source location
3939  ///if it has none. If no location can be calculated then the function
3940  ///returns the empty string.
3941  string
3942  build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
3943  {
3944  string result;
3945  // Let's make sure we are looking at a data member with an empty
3946  // name ...
3947  if (!die
3948  || dwarf_tag(die) != DW_TAG_member
3949  || !die_name(die).empty())
3950  return result;
3951 
3952  // ... and yet, it's not an anonymous data member (aka unnamed
3953  // field) as described in
3954  // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3955  if (die_is_anonymous_data_member(die))
3956  return result;
3957 
3958  // If we come this far, it means we are looking at a buggy data
3959  // member with no name. Let's build a name for it so that it can be
3960  // addressed.
3961  int64_t offset_in_bits = 0;
3962  bool has_offset = die_member_offset(*this, die, offset_in_bits);
3963  location loc;
3964  if (!has_offset)
3965  {
3966  loc = die_location(*this, die);
3967  if (!loc)
3968  return result;
3969  }
3970 
3971  std::ostringstream o;
3972  o << "unnamed-dm-@-";
3973  if (has_offset)
3974  o << "offset-" << offset_in_bits << "bits";
3975  else
3976  o << "loc-" << loc.expand();
3977 
3978  return o.str();
3979  }
3980 
3981  /// Getter for the map of declaration-only classes that are to be
3982  /// resolved to their definition classes by the end of the corpus
3983  /// loading.
3984  ///
3985  /// @return a map of string -> vector of classes where the key is
3986  /// the fully qualified name of the class and the value is the
3987  /// vector of declaration-only class.
3989  declaration_only_classes() const
3990  {return decl_only_classes_map_;}
3991 
3992  /// Getter for the map of declaration-only classes that are to be
3993  /// resolved to their definition classes by the end of the corpus
3994  /// loading.
3995  ///
3996  /// @return a map of string -> vector of classes where the key is
3997  /// the fully qualified name of the class and the value is the
3998  /// vector of declaration-only class.
4000  declaration_only_classes()
4001  {return decl_only_classes_map_;}
4002 
4003  /// If a given class is a declaration-only class then stash it on
4004  /// the side so that at the end of the corpus reading we can resolve
4005  /// it to its definition.
4006  ///
4007  /// @param klass the class to consider.
4008  void
4009  maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
4010  {
4011  if (cou->get_is_declaration_only()
4012  && cou->get_definition_of_declaration() == 0
4013  // Make sure the class is not anonymous. Anonymous classes
4014  // are usually later named by a typedef. At that time, after
4015  // being named by a typedef, this method is going to be called
4016  // with the class being named by the typedef.
4017  && !cou->get_qualified_name().empty())
4018  {
4019  string qn = cou->get_qualified_name();
4020  string_classes_or_unions_map::iterator record =
4021  declaration_only_classes().find(qn);
4022  if (record == declaration_only_classes().end())
4023  declaration_only_classes()[qn].push_back(cou);
4024  else
4025  record->second.push_back(cou);
4026  }
4027  }
4028 
4029  /// Test if a given declaration-only class has been scheduled for
4030  /// resolution to a defined class.
4031  ///
4032  /// @param klass the class to consider for the test.
4033  ///
4034  /// @return true iff @p klass is a declaration-only class and if
4035  /// it's been scheduled for resolution to a defined class.
4036  bool
4037  is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4038  {
4039  if (cou->get_is_declaration_only())
4040  return ((declaration_only_classes().find(cou->get_qualified_name())
4041  != declaration_only_classes().end())
4042  || (declaration_only_classes().find(cou->get_name())
4043  != declaration_only_classes().end()));
4044 
4045  return false;
4046  }
4047 
4048  /// Compare two ABI artifacts in a context which canonicalization
4049  /// has not be done yet.
4050  ///
4051  /// @param l the left-hand-side operand of the comparison
4052  ///
4053  /// @param r the right-hand-side operand of the comparison.
4054  ///
4055  /// @return true if @p l equals @p r.
4056  bool
4057  compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4058  const type_or_decl_base_sptr &r)
4059  {
4060  if (!l || !r)
4061  return !!l == !!r;
4062 
4063  const environment& e = l->get_environment();
4064  ABG_ASSERT(!e.canonicalization_is_done());
4065 
4066  e.priv_->allow_type_comparison_results_caching(true);
4067  bool s0 = e.decl_only_class_equals_definition();
4068  e.decl_only_class_equals_definition(true);
4069  bool equal = l == r;
4070  e.decl_only_class_equals_definition(s0);
4071  e.priv_->clear_type_comparison_results_cache();
4072  e.priv_->allow_type_comparison_results_caching(false);
4073  return equal;
4074  }
4075 
4076  /// Walk the declaration-only classes that have been found during
4077  /// the building of the corpus and resolve them to their definitions.
4078  void
4079  resolve_declaration_only_classes()
4080  {
4081  vector<string> resolved_classes;
4082 
4083  for (string_classes_or_unions_map::iterator i =
4084  declaration_only_classes().begin();
4085  i != declaration_only_classes().end();
4086  ++i)
4087  {
4088  bool to_resolve = false;
4089  for (classes_or_unions_type::iterator j = i->second.begin();
4090  j != i->second.end();
4091  ++j)
4092  if ((*j)->get_is_declaration_only()
4093  && ((*j)->get_definition_of_declaration() == 0))
4094  to_resolve = true;
4095 
4096  if (!to_resolve)
4097  {
4098  resolved_classes.push_back(i->first);
4099  continue;
4100  }
4101 
4102  // Now, for each decl-only class that have the current name
4103  // 'i->first', let's try to poke at the fully defined class
4104  // that is defined in the same translation unit as the
4105  // declaration.
4106  //
4107  // If we find one class (defined in the TU of the declaration)
4108  // that defines the declaration, then the declaration can be
4109  // resolved to that class.
4110  //
4111  // If no defining class is found in the TU of the declaration,
4112  // then there are possibly three cases to consider:
4113  //
4114  // 1/ There is exactly one class that defines the
4115  // declaration and that class is defined in another TU. In
4116  // this case, the declaration is resolved to that
4117  // definition.
4118  //
4119  // 2/ There are more than one class that define that
4120  // declaration and none of them is defined in the TU of the
4121  // declaration. If those classes are all different, then
4122  // the declaration is left unresolved.
4123  //
4124  // 3/ No class defines the declaration. In this case, the
4125  // declaration is left unresoved.
4126 
4127  // So get the classes that might define the current
4128  // declarations which name is i->first.
4129  const type_base_wptrs_type *classes =
4130  lookup_class_types(i->first, *corpus());
4131  if (!classes)
4132  classes = lookup_union_types(i->first, *corpus());
4133 
4134  if (!classes)
4135  continue;
4136 
4137  // This is a map that associates the translation unit path to
4138  // the class (that potentially defines the declarations that
4139  // we consider) that are defined in that translation unit. It
4140  // should stay ordered by using the TU path as key to ensure
4141  // stability of the order of classe definitions in ABIXML
4142  // output.
4143  map<string, class_or_union_sptr> per_tu_class_map;
4144  for (type_base_wptrs_type::const_iterator c = classes->begin();
4145  c != classes->end();
4146  ++c)
4147  {
4148  class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4149  ABG_ASSERT(klass);
4150 
4152  if (klass->get_is_declaration_only())
4153  continue;
4154 
4155  string tu_path = klass->get_translation_unit()->get_absolute_path();
4156  if (tu_path.empty())
4157  continue;
4158 
4159  // Build a map that associates the translation unit path
4160  // to the class (that potentially defines the declarations
4161  // that we consider) that are defined in that translation unit.
4162  per_tu_class_map[tu_path] = klass;
4163  }
4164 
4165  if (!per_tu_class_map.empty())
4166  {
4167  // Walk the declarations to resolve and resolve them
4168  // either to the definitions that are in the same TU as
4169  // the declaration, or to the definition found elsewhere,
4170  // if there is only one such definition.
4171  for (classes_or_unions_type::iterator j = i->second.begin();
4172  j != i->second.end();
4173  ++j)
4174  {
4175  if ((*j)->get_is_declaration_only()
4176  && ((*j)->get_definition_of_declaration() == 0))
4177  {
4178  string tu_path =
4179  (*j)->get_translation_unit()->get_absolute_path();
4180  map<string, class_or_union_sptr>::const_iterator e =
4181  per_tu_class_map.find(tu_path);
4182  if (e != per_tu_class_map.end())
4183  (*j)->set_definition_of_declaration(e->second);
4184  else if (per_tu_class_map.size() == 1)
4185  (*j)->set_definition_of_declaration
4186  (per_tu_class_map.begin()->second);
4187  else
4188  {
4189  // We are in case where there are more than
4190  // one definition for the declaration. Let's
4191  // see if they are all equal. If they are,
4192  // then the declaration resolves to the
4193  // definition. Otherwise, we are in the case
4194  // 3/ described above.
4195  map<string,
4196  class_or_union_sptr>::const_iterator it;
4197  class_or_union_sptr first_class =
4198  per_tu_class_map.begin()->second;
4199  bool all_class_definitions_are_equal = true;
4200  for (it = per_tu_class_map.begin();
4201  it != per_tu_class_map.end();
4202  ++it)
4203  {
4204  if (it == per_tu_class_map.begin())
4205  continue;
4206  else
4207  {
4208  if (!compare_before_canonicalisation(it->second,
4209  first_class))
4210  {
4211  all_class_definitions_are_equal = false;
4212  break;
4213  }
4214  }
4215  }
4216  if (all_class_definitions_are_equal)
4217  (*j)->set_definition_of_declaration(first_class);
4218  }
4219  }
4220  }
4221  resolved_classes.push_back(i->first);
4222  }
4223  }
4224 
4225  size_t num_decl_only_classes = declaration_only_classes().size(),
4226  num_resolved = resolved_classes.size();
4227  if (show_stats())
4228  cerr << "resolved " << num_resolved
4229  << " class declarations out of "
4230  << num_decl_only_classes
4231  << "\n";
4232 
4233  for (vector<string>::const_iterator i = resolved_classes.begin();
4234  i != resolved_classes.end();
4235  ++i)
4236  declaration_only_classes().erase(*i);
4237 
4238  if (show_stats() && !declaration_only_classes().empty())
4239  {
4240  cerr << "Here are the "
4241  << num_decl_only_classes - num_resolved
4242  << " unresolved class declarations:\n";
4243  for (string_classes_or_unions_map::iterator i =
4244  declaration_only_classes().begin();
4245  i != declaration_only_classes().end();
4246  ++i)
4247  cerr << " " << i->first << "\n";
4248  }
4249  }
4250 
4251  /// Getter for the map of declaration-only enums that are to be
4252  /// resolved to their definition enums by the end of the corpus
4253  /// loading.
4254  ///
4255  /// @return a map of string -> vector of enums where the key is
4256  /// the fully qualified name of the enum and the value is the
4257  /// vector of declaration-only enum.
4258  const string_enums_map&
4259  declaration_only_enums() const
4260  {return decl_only_enums_map_;}
4261 
4262  /// Getter for the map of declaration-only enums that are to be
4263  /// resolved to their definition enums by the end of the corpus
4264  /// loading.
4265  ///
4266  /// @return a map of string -> vector of enums where the key is
4267  /// the fully qualified name of the enum and the value is the
4268  /// vector of declaration-only enum.
4270  declaration_only_enums()
4271  {return decl_only_enums_map_;}
4272 
4273  /// If a given enum is a declaration-only enum then stash it on
4274  /// the side so that at the end of the corpus reading we can resolve
4275  /// it to its definition.
4276  ///
4277  /// @param enom the enum to consider.
4278  void
4279  maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4280  {
4281  if (enom->get_is_declaration_only()
4282  && enom->get_definition_of_declaration() == 0
4283  // Make sure the enum is not anonymous. Anonymous enums are
4284  // usually later named by a typedef. At that time, after
4285  // being named by a typedef, this method is going to be called
4286  // with the enum being named by the typedef.
4287  && !enom->get_qualified_name().empty())
4288  {
4289  string qn = enom->get_qualified_name();
4290  string_enums_map::iterator record =
4291  declaration_only_enums().find(qn);
4292  if (record == declaration_only_enums().end())
4293  declaration_only_enums()[qn].push_back(enom);
4294  else
4295  record->second.push_back(enom);
4296  }
4297  }
4298 
4299  /// Test if a given declaration-only enum has been scheduled for
4300  /// resolution to a defined enum.
4301  ///
4302  /// @param enom the enum to consider for the test.
4303  ///
4304  /// @return true iff @p enom is a declaration-only enum and if
4305  /// it's been scheduled for resolution to a defined enum.
4306  bool
4307  is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4308  {
4309  if (enom->get_is_declaration_only())
4310  return (declaration_only_enums().find(enom->get_qualified_name())
4311  != declaration_only_enums().end());
4312 
4313  return false;
4314  }
4315 
4316  /// Walk the declaration-only enums that have been found during
4317  /// the building of the corpus and resolve them to their definitions.
4318  ///
4319  /// TODO: Do away with this function by factorizing it with
4320  /// resolve_declaration_only_classes. All declaration-only decls
4321  /// could be handled the same way as declaration-only-ness is a
4322  /// property of abigail::ir::decl_base now.
4323  void
4324  resolve_declaration_only_enums()
4325  {
4326  vector<string> resolved_enums;
4327 
4328  for (string_enums_map::iterator i =
4329  declaration_only_enums().begin();
4330  i != declaration_only_enums().end();
4331  ++i)
4332  {
4333  bool to_resolve = false;
4334  for (enums_type::iterator j = i->second.begin();
4335  j != i->second.end();
4336  ++j)
4337  if ((*j)->get_is_declaration_only()
4338  && ((*j)->get_definition_of_declaration() == 0))
4339  to_resolve = true;
4340 
4341  if (!to_resolve)
4342  {
4343  resolved_enums.push_back(i->first);
4344  continue;
4345  }
4346 
4347  // Now, for each decl-only enum that have the current name
4348  // 'i->first', let's try to poke at the fully defined enum
4349  // that is defined in the same translation unit as the
4350  // declaration.
4351  //
4352  // If we find one enum (defined in the TU of the declaration)
4353  // that defines the declaration, then the declaration can be
4354  // resolved to that enum.
4355  //
4356  // If no defining enum is found in the TU of the declaration,
4357  // then there are possibly three cases to consider:
4358  //
4359  // 1/ There is exactly one enum that defines the
4360  // declaration and that enum is defined in another TU. In
4361  // this case, the declaration is resolved to that
4362  // definition.
4363  //
4364  // 2/ There are more than one enum that define that
4365  // declaration and none of them is defined in the TU of the
4366  // declaration. In this case, the declaration is left
4367  // unresolved.
4368  //
4369  // 3/ No enum defines the declaration. In this case, the
4370  // declaration is left unresoved.
4371 
4372  // So get the enums that might define the current
4373  // declarations which name is i->first.
4374  const type_base_wptrs_type *enums =
4375  lookup_enum_types(i->first, *corpus());
4376  if (!enums)
4377  continue;
4378 
4379  // This is a map that associates the translation unit path to
4380  // the enum (that potentially defines the declarations that
4381  // we consider) that are defined in that translation unit. It
4382  // should stay ordered by using the TU path as key to ensure
4383  // stability of the order of enum definitions in ABIXML
4384  // output.
4385  map<string, enum_type_decl_sptr> per_tu_enum_map;
4386  for (type_base_wptrs_type::const_iterator c = enums->begin();
4387  c != enums->end();
4388  ++c)
4389  {
4390  enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4391  ABG_ASSERT(enom);
4392 
4394  if (enom->get_is_declaration_only())
4395  continue;
4396 
4397  string tu_path = enom->get_translation_unit()->get_absolute_path();
4398  if (tu_path.empty())
4399  continue;
4400 
4401  // Build a map that associates the translation unit path
4402  // to the enum (that potentially defines the declarations
4403  // that we consider) that are defined in that translation unit.
4404  per_tu_enum_map[tu_path] = enom;
4405  }
4406 
4407  if (!per_tu_enum_map.empty())
4408  {
4409  // Walk the declarations to resolve and resolve them
4410  // either to the definitions that are in the same TU as
4411  // the declaration, or to the definition found elsewhere,
4412  // if there is only one such definition.
4413  for (enums_type::iterator j = i->second.begin();
4414  j != i->second.end();
4415  ++j)
4416  {
4417  if ((*j)->get_is_declaration_only()
4418  && ((*j)->get_definition_of_declaration() == 0))
4419  {
4420  string tu_path =
4421  (*j)->get_translation_unit()->get_absolute_path();
4422  map<string, enum_type_decl_sptr>::const_iterator e =
4423  per_tu_enum_map.find(tu_path);
4424  if (e != per_tu_enum_map.end())
4425  (*j)->set_definition_of_declaration(e->second);
4426  else if (per_tu_enum_map.size() == 1)
4427  (*j)->set_definition_of_declaration
4428  (per_tu_enum_map.begin()->second);
4429  else
4430  {
4431  // We are in case where there are more than
4432  // one definition for the declaration. Let's
4433  // see if they are all equal. If they are,
4434  // then the declaration resolves to the
4435  // definition. Otherwise, we are in the case
4436  // 3/ described above.
4437  map<string,
4438  enum_type_decl_sptr>::const_iterator it;
4439  enum_type_decl_sptr first_enum =
4440  per_tu_enum_map.begin()->second;
4441  bool all_enum_definitions_are_equal = true;
4442  for (it = per_tu_enum_map.begin();
4443  it != per_tu_enum_map.end();
4444  ++it)
4445  {
4446  if (it == per_tu_enum_map.begin())
4447  continue;
4448  else
4449  {
4450  if (!compare_before_canonicalisation(it->second,
4451  first_enum))
4452  {
4453  all_enum_definitions_are_equal = false;
4454  break;
4455  }
4456  }
4457  }
4458  if (all_enum_definitions_are_equal)
4459  (*j)->set_definition_of_declaration(first_enum);
4460  }
4461  }
4462  }
4463  resolved_enums.push_back(i->first);
4464  }
4465  }
4466 
4467  size_t num_decl_only_enums = declaration_only_enums().size(),
4468  num_resolved = resolved_enums.size();
4469  if (show_stats())
4470  cerr << "resolved " << num_resolved
4471  << " enum declarations out of "
4472  << num_decl_only_enums
4473  << "\n";
4474 
4475  for (vector<string>::const_iterator i = resolved_enums.begin();
4476  i != resolved_enums.end();
4477  ++i)
4478  declaration_only_enums().erase(*i);
4479 
4480  if (show_stats() && !declaration_only_enums().empty())
4481  {
4482  cerr << "Here are the "
4483  << num_decl_only_enums - num_resolved
4484  << " unresolved enum declarations:\n";
4485  for (string_enums_map::iterator i = declaration_only_enums().begin();
4486  i != declaration_only_enums().end();
4487  ++i)
4488  cerr << " " << i->first << "\n";
4489  }
4490  }
4491 
4492  /// Test if a symbol belongs to a function of the current ABI
4493  /// corpus.
4494  ///
4495  /// This is a sub-routine of fixup_functions_with_no_symbols.
4496  ///
4497  /// @param fn the function symbol to consider.
4498  ///
4499  /// @returnt true if @p fn belongs to a function of the current ABI
4500  /// corpus.
4501  bool
4502  symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4503  {
4504  corpus_sptr corp = corpus();
4505  if (!corp)
4506  return false;
4507 
4508  interned_string id = corp->get_environment().intern(fn->get_id_string());
4509 
4510  const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4511  if (!fns)
4512  return false;
4513 
4514  for (auto f : *fns)
4515  if (f->get_symbol())
4516  return true;
4517 
4518  return false;
4519  }
4520 
4521  /// Some functions described by DWARF may have their linkage name
4522  /// set, but no link to their actual underlying elf symbol. When
4523  /// these are virtual member functions, comparing the enclosing type
4524  /// against another one which has its underlying symbol properly set
4525  /// might lead to spurious type changes.
4526  ///
4527  /// If the corpus contains a symbol with the same name as the
4528  /// linkage name of the function, then set up the link between the
4529  /// function and its underlying symbol.
4530  ///
4531  /// Note that for the moment, only virtual member functions are
4532  /// fixed up like this. This is because they really are the only
4533  /// fuctions of functions that can affect types (in spurious ways).
4534  void
4535  fixup_functions_with_no_symbols()
4536  {
4537  corpus_sptr corp = corpus();
4538  if (!corp)
4539  return;
4540 
4541  die_function_decl_map_type &fns_with_no_symbol =
4542  die_function_decl_with_no_symbol_map();
4543 
4544  if (do_log())
4545  cerr << fns_with_no_symbol.size()
4546  << " functions to fixup, potentially\n";
4547 
4548  for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4549  i != fns_with_no_symbol.end();
4550  ++i)
4551  if (elf_symbol_sptr sym =
4552  corp->lookup_function_symbol(i->second->get_linkage_name()))
4553  {
4554  // So i->second is a virtual member function that was
4555  // previously scheduled to be set a function symbol.
4556  //
4557  // But if it appears that it now has a symbol already set,
4558  // then do not set a symbol to it again.
4559  //
4560  // Or if it appears that another virtual member function
4561  // from the current ABI Corpus, with the same linkage
4562  // (mangled) name has already been set a symbol, then do not
4563  // set a symbol to this function either. Otherwise, there
4564  // will be two virtual member functions with the same symbol
4565  // in the class and that leads to spurious hard-to-debug
4566  // change reports later down the road.
4567  if (i->second->get_symbol()
4568  || symbol_already_belongs_to_a_function(sym))
4569  continue;
4570 
4571  ABG_ASSERT(is_member_function(i->second));
4573  i->second->set_symbol(sym);
4574 
4575  if (do_log())
4576  cerr << "fixed up '"
4577  << i->second->get_pretty_representation()
4578  << "' with symbol '"
4579  << sym->get_id_string()
4580  << "'\n";
4581  }
4582 
4583  fns_with_no_symbol.clear();
4584  }
4585 
4586  /// Return a reference to the vector containing the types created
4587  /// during the binary analysis but that are not tied to a given
4588  /// DWARF DIE.
4589  ///
4590  /// @return reference to the vector containing the types created
4591  /// during the binary analysis but that are not tied to a given
4592  /// DWARF DIE.
4593  const vector<type_base_sptr>&
4594  types_to_canonicalize() const
4595  {return types_to_canonicalize_;}
4596 
4597  /// Clear the containers holding types to canonicalize.
4598  void
4599  clear_types_to_canonicalize()
4600  {
4601  types_to_canonicalize_.clear();
4602  }
4603 
4604  /// Types that were created but not tied to a particular DIE, must
4605  /// be scheduled for late canonicalization using this method.
4606  ///
4607  /// @param t the type to schedule for late canonicalization.
4608  void
4609  schedule_type_for_late_canonicalization(const type_base_sptr &t)
4610  {
4611  types_to_canonicalize_.push_back(t);
4612  }
4613 
4614  /// Canonicalize types which DIE offsets are stored in vectors on
4615  /// the side. This is a sub-routine of
4616  /// reader::perform_late_type_canonicalizing().
4617  ///
4618  /// @param source where the DIE of the types to canonicalize are
4619  /// from.
4620  void
4621  canonicalize_types_scheduled()
4622  {
4623  tools_utils::timer cn_timer;
4624  if (do_log())
4625  {
4626  cerr << "DWARF Reader is going to canonicalize types";
4627  corpus_sptr c = corpus();
4628  if (c)
4629  cerr << " of corpus " << corpus()->get_path() << "\n";
4630  cn_timer.start();
4631  }
4632 
4633  if (!types_to_canonicalize().empty())
4634  canonicalize_types(types_to_canonicalize().begin(),
4635  types_to_canonicalize().end(),
4636  [](const vector<type_base_sptr>::const_iterator& i)
4637  {return *i;});
4638 
4639  if (do_log())
4640  {
4641  cn_timer.stop();
4642  cerr << "finished canonicalizing types";
4643  corpus_sptr c = corpus();
4644  if (c)
4645  cerr << " of corpus " << corpus()->get_path();
4646  cerr << ": (" << cn_timer << ")\n";
4647  }
4648  }
4649 
4650  /// Compute the number of canonicalized and missed types in the late
4651  /// canonicalization phase.
4652  ///
4653  /// @param source where the DIEs of the canonicalized types are
4654  /// from.
4655  ///
4656  /// @param canonicalized the number of types that got canonicalized
4657  /// is added to the value already present in this parameter.
4658  ///
4659  /// @param missed the number of types scheduled for late
4660  /// canonicalization and which couldn't be canonicalized (for a
4661  /// reason) is added to the value already present in this parameter.
4662  void
4663  add_late_canonicalized_types_stats(size_t& canonicalized,
4664  size_t& missed) const
4665  {
4666  for (auto t : types_to_canonicalize())
4667  {
4668  if (t->get_canonical_type())
4669  ++canonicalized;
4670  else
4671  ++missed;
4672  }
4673  }
4674 
4675  // Look at the types that need to be canonicalized after the
4676  // translation unit has been constructed and canonicalize them.
4677  void
4678  perform_late_type_canonicalizing()
4679  {
4680  canonicalize_types_scheduled();
4681 
4682  if (show_stats())
4683  {
4684  size_t num_canonicalized = 0, num_missed = 0, total = 0;
4685  add_late_canonicalized_types_stats(num_canonicalized,
4686  num_missed);
4687  total = num_canonicalized + num_missed;
4688  cerr << "binary: "
4689  << elf_path()
4690  << "\n";
4691  cerr << " # late canonicalized types: "
4692  << num_canonicalized;
4693  if (total)
4694  cerr << " (" << num_canonicalized * 100 / total << "%)";
4695  cerr << "\n"
4696  << " # missed canonicalization opportunities: "
4697  << num_missed;
4698  if (total)
4699  cerr << " (" << num_missed * 100 / total << "%)";
4700  cerr << "\n";
4701  }
4702 
4703  }
4704 
4705  const die_tu_map_type&
4706  die_tu_map() const
4707  {return die_tu_map_;}
4708 
4710  die_tu_map()
4711  {return die_tu_map_;}
4712 
4713  /// Getter for the map that associates a translation unit DIE to the
4714  /// vector of imported unit points that it contains.
4715  ///
4716  /// @param source where the DIEs are from.
4717  ///
4718  /// @return the map.
4720  tu_die_imported_unit_points_map(die_source source) const
4721  {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4722 
4723  /// Getter for the map that associates a translation unit DIE to the
4724  /// vector of imported unit points that it contains.
4725  ///
4726  /// @param source where the DIEs are from.
4727  ///
4728  /// @return the map.
4730  tu_die_imported_unit_points_map(die_source source)
4731  {
4732  switch (source)
4733  {
4734  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4735  break;
4736  case ALT_DEBUG_INFO_DIE_SOURCE:
4737  return alt_tu_die_imported_unit_points_map_;
4738  case TYPE_UNIT_DIE_SOURCE:
4739  return type_units_tu_die_imported_unit_points_map_;
4740  case NO_DEBUG_INFO_DIE_SOURCE:
4741  case NUMBER_OF_DIE_SOURCES:
4742  // We cannot reach this point.
4744  }
4745  return tu_die_imported_unit_points_map_;
4746  }
4747 
4748  /// Reset the current corpus being constructed.
4749  ///
4750  /// This actually deletes the current corpus being constructed.
4751  void
4752  reset_corpus()
4753  {corpus().reset();}
4754 
4755  /// Get the map that associates each DIE to its parent DIE. This is
4756  /// for DIEs coming from the main debug info sections.
4757  ///
4758  /// @param source where the DIEs in the map come from.
4759  ///
4760  /// @return the DIE -> parent map.
4761  const offset_offset_map_type&
4762  die_parent_map(die_source source) const
4763  {return const_cast<reader*>(this)->die_parent_map(source);}
4764 
4765  /// Get the map that associates each DIE to its parent DIE. This is
4766  /// for DIEs coming from the main debug info sections.
4767  ///
4768  /// @param source where the DIEs in the map come from.
4769  ///
4770  /// @return the DIE -> parent map.
4772  die_parent_map(die_source source)
4773  {
4774  switch (source)
4775  {
4776  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4777  break;
4778  case ALT_DEBUG_INFO_DIE_SOURCE:
4779  return alternate_die_parent_map_;
4780  case TYPE_UNIT_DIE_SOURCE:
4781  return type_section_die_parent_map();
4782  case NO_DEBUG_INFO_DIE_SOURCE:
4783  case NUMBER_OF_DIE_SOURCES:
4785  }
4786  return primary_die_parent_map_;
4787  }
4788 
4789  const offset_offset_map_type&
4790  type_section_die_parent_map() const
4791  {return type_section_die_parent_map_;}
4792 
4794  type_section_die_parent_map()
4795  {return type_section_die_parent_map_;}
4796 
4797  /// Getter of the current translation unit.
4798  ///
4799  /// @return the current translation unit being constructed.
4800  const translation_unit_sptr&
4801  cur_transl_unit() const
4802  {return cur_tu_;}
4803 
4804  /// Getter of the current translation unit.
4805  ///
4806  /// @return the current translation unit being constructed.
4808  cur_transl_unit()
4809  {return cur_tu_;}
4810 
4811  /// Setter of the current translation unit.
4812  ///
4813  /// @param tu the current translation unit being constructed.
4814  void
4815  cur_transl_unit(translation_unit_sptr tu)
4816  {
4817  if (tu)
4818  cur_tu_ = tu;
4819  }
4820 
4821  /// Return the global scope of the current translation unit.
4822  ///
4823  /// @return the global scope of the current translation unit.
4824  const scope_decl_sptr&
4825  global_scope() const
4826  {return cur_transl_unit()->get_global_scope();}
4827 
4828  /// Return a scope that is nil.
4829  ///
4830  /// @return a scope that is nil.
4831  const scope_decl_sptr&
4832  nil_scope() const
4833  {return nil_scope_;}
4834 
4835  const scope_stack_type&
4836  scope_stack() const
4837  {return scope_stack_;}
4838 
4840  scope_stack()
4841  {return scope_stack_;}
4842 
4843  scope_decl*
4844  current_scope()
4845  {
4846  if (scope_stack().empty())
4847  {
4848  if (cur_transl_unit())
4849  scope_stack().push(cur_transl_unit()->get_global_scope().get());
4850  }
4851  return scope_stack().top();
4852  }
4853 
4854  list<var_decl_sptr>&
4855  var_decls_to_re_add_to_tree()
4856  {return var_decls_to_add_;}
4857 
4858  /// Test if a DIE represents a decl (function or variable) that has
4859  /// a symbol that is exported, whatever that means. This is
4860  /// supposed to work for Linux Kernel binaries as well.
4861  ///
4862  /// This is useful to limit the amount of DIEs taken into account to
4863  /// the strict limit of what an ABI actually means. Limiting the
4864  /// volume of DIEs analyzed this way is an important optimization to
4865  /// keep big binaries "manageable" by libabigail.
4866  ///
4867  /// @param DIE the die to consider.
4868  bool
4869  is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
4870  {
4871  if (!die || !die_is_decl(die))
4872  return false;
4873 
4874  bool result = false, address_found = false, symbol_is_exported = false;;
4875  Dwarf_Addr decl_symbol_address = 0;
4876 
4877  if (die_is_variable_decl(die))
4878  {
4879  if ((address_found = get_variable_address(die, decl_symbol_address)))
4880  symbol_is_exported =
4881  !!variable_symbol_is_exported(decl_symbol_address);
4882  }
4883  else if (die_is_function_decl(die))
4884  {
4885  if ((address_found = get_function_address(die, decl_symbol_address)))
4886  symbol_is_exported =
4887  !!function_symbol_is_exported(decl_symbol_address);
4888  }
4889 
4890  if (address_found)
4891  result = symbol_is_exported;
4892 
4893  return result;
4894  }
4895 
4896  /// Test if a DIE is a variable or function DIE which name denotes
4897  /// an undefined ELF symbol.
4898  ///
4899  /// @return true iff @p die represents a function or variable that
4900  /// has an undefined symbol.
4901  bool
4902  is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
4903  {
4904  if (is_decl_die_with_exported_symbol(die))
4905  return false;
4906 
4907  string name, linkage_name;
4908  die_name_and_linkage_name(die, name, linkage_name);
4909  if (linkage_name.empty())
4910  linkage_name = name;
4911 
4912  bool result = false;
4913  if ((die_is_variable_decl(die)
4914  && symtab()->variable_symbol_is_undefined(linkage_name))
4915  ||
4916  (die_is_function_decl(die)
4917  && symtab()->function_symbol_is_undefined(linkage_name)))
4918  result = true;
4919 
4920  return result;
4921  }
4922 
4923  /// This is a sub-routine of maybe_adjust_fn_sym_address and
4924  /// maybe_adjust_var_sym_address.
4925  ///
4926  /// Given an address that we got by looking at some debug
4927  /// information (e.g, a symbol's address referred to by a DWARF
4928  /// TAG), If the ELF file we are interested in is a shared library
4929  /// or an executable, then adjust the address to be coherent with
4930  /// where the executable (or shared library) is loaded. That way,
4931  /// the address can be used to look for symbols in the executable or
4932  /// shared library.
4933  ///
4934  /// @return the adjusted address, or the same address as @p addr if
4935  /// it didn't need any adjustment.
4936  Dwarf_Addr
4937  maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4938  {
4939  if (addr == 0)
4940  return addr;
4941 
4942  GElf_Ehdr eh_mem;
4943  GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4944 
4945  if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4946  {
4947  Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4948  ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
4949  dwarf_elf_load_address));
4950  ABG_ASSERT(get_binary_load_address(elf_handle(),
4951  elf_load_address));
4952  if (dwarf_is_splitted()
4953  && (dwarf_elf_load_address != elf_load_address))
4954  // This means that in theory the DWARF and the executable are
4955  // not loaded at the same address. And addr is meaningful
4956  // only in the context of the DWARF.
4957  //
4958  // So let's transform addr into an offset relative to where
4959  // the DWARF is loaded, and let's add that relative offset
4960  // to the load address of the executable. That way, addr
4961  // becomes meaningful in the context of the executable and
4962  // can thus be used to compare against the address of
4963  // symbols of the executable, for instance.
4964  addr = addr - dwarf_elf_load_address + elf_load_address;
4965  }
4966 
4967  return addr;
4968  }
4969 
4970  /// For a relocatable (*.o) elf file, this function expects an
4971  /// absolute address, representing a function symbol. It then
4972  /// extracts the address of the .text section from the symbol
4973  /// absolute address to get the relative address of the function
4974  /// from the beginning of the .text section.
4975  ///
4976  /// For executable or shared library, this function expects an
4977  /// address of a function symbol that was retrieved by looking at a
4978  /// DWARF "file". The function thus adjusts the address to make it
4979  /// be meaningful in the context of the ELF file.
4980  ///
4981  /// In both cases, the address can then be compared against the
4982  /// st_value field of a function symbol from the ELF file.
4983  ///
4984  /// @param addr an adress for a function symbol that was retrieved
4985  /// from a DWARF file.
4986  ///
4987  /// @return the (possibly) adjusted address, or just @p addr if no
4988  /// adjustment took place.
4989  Dwarf_Addr
4990  maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4991  {
4992  if (addr == 0)
4993  return addr;
4994 
4995  Elf* elf = elf_handle();
4996  GElf_Ehdr eh_mem;
4997  GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4998 
4999  if (elf_header->e_type == ET_REL)
5000  // We are looking at a relocatable file. In this case, we don't
5001  // do anything because:
5002  //
5003  // 1/ the addresses from DWARF are absolute (relative to the
5004  // beginning of the relocatable file)
5005  //
5006  // 2/ The ELF symbol addresses that we store in our lookup
5007  // tables are translated from section-related to absolute as
5008  // well. So we don't have anything to do at this point for
5009  // ET_REL files.
5010  ;
5011  else
5012  addr = maybe_adjust_address_for_exec_or_dyn(addr);
5013 
5014  return addr;
5015  }
5016 
5017  /// For a relocatable (*.o) elf file, this function expects an
5018  /// absolute address, representing a global variable symbol. It
5019  /// then extracts the address of the {.data,.data1,.rodata,.bss}
5020  /// section from the symbol absolute address to get the relative
5021  /// address of the variable from the beginning of the data section.
5022  ///
5023  /// For executable or shared library, this function expects an
5024  /// address of a variable symbol that was retrieved by looking at a
5025  /// DWARF "file". The function thus adjusts the address to make it
5026  /// be meaningful in the context of the ELF file.
5027  ///
5028  /// In both cases, the address can then be compared against the
5029  /// st_value field of a function symbol from the ELF file.
5030  ///
5031  /// @param addr an address for a global variable symbol that was
5032  /// retrieved from a DWARF file.
5033  ///
5034  /// @return the (possibly) adjusted address, or just @p addr if no
5035  /// adjustment took place.
5036  Dwarf_Addr
5037  maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5038  {
5039  Elf* elf = elf_handle();
5040  GElf_Ehdr eh_mem;
5041  GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5042 
5043  if (elf_header->e_type == ET_REL)
5044  // We are looking at a relocatable file. In this case, we don't
5045  // do anything because:
5046  //
5047  // 1/ the addresses from DWARF are absolute (relative to the
5048  // beginning of the relocatable file)
5049  //
5050  // 2/ The ELF symbol addresses that we store in our lookup
5051  // tables are translated from section-related to absolute as
5052  // well. So we don't have anything to do at this point for
5053  // ET_REL files.
5054  ;
5055  else
5056  addr = maybe_adjust_address_for_exec_or_dyn(addr);
5057 
5058  return addr;
5059  }
5060 
5061  /// Get the first exported function address in the set of addresses
5062  /// referred to by the DW_AT_ranges attribute of a given DIE.
5063  ///
5064  /// @param die the DIE we are considering.
5065  ///
5066  /// @param address output parameter. This is set to the first
5067  /// address found in the sequence pointed to by the DW_AT_ranges
5068  /// attribute found on the DIE @p die, iff the function returns
5069  /// true. Otherwise, no value is set into this output parameter.
5070  ///
5071  /// @return true iff the DIE @p die does have a DW_AT_ranges
5072  /// attribute and an address of an exported function was found in
5073  /// its sequence value.
5074  bool
5075  get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5076  Dwarf_Addr& address) const
5077  {
5078  Dwarf_Addr base;
5079  Dwarf_Addr end_addr;
5080  ptrdiff_t offset = 0;
5081 
5082  do
5083  {
5084  Dwarf_Addr addr = 0, fn_addr = 0;
5085  if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5086  {
5087  fn_addr = maybe_adjust_fn_sym_address(addr);
5088  if (function_symbol_is_exported(fn_addr))
5089  {
5090  address = fn_addr;
5091  return true;
5092  }
5093  }
5094  } while (offset > 0);
5095  return false;
5096  }
5097 
5098  /// Get the address of the function.
5099  ///
5100  /// The address of the function is considered to be the value of the
5101  /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5102  /// only) to not point to an absolute address anymore, but rather to
5103  /// the address of the function inside the .text segment.
5104  ///
5105  /// @param function_die the die of the function to consider.
5106  ///
5107  /// @param address the resulting address iff the function returns
5108  /// true.
5109  ///
5110  /// @return true if the function address was found.
5111  bool
5112  get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5113  {
5114  if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5115  DW_AT_low_pc, address))
5116  // So no DW_AT_low_pc was found. Let's see if the function DIE
5117  // has got a DW_AT_ranges attribute instead. If it does, the
5118  // first address of the set of addresses represented by the
5119  // value of that DW_AT_ranges represents the function (symbol)
5120  // address we are looking for.
5121  if (!get_first_exported_fn_address_from_DW_AT_ranges
5122  (const_cast<Dwarf_Die*>(function_die),
5123  address))
5124  return false;
5125 
5126  address = maybe_adjust_fn_sym_address(address);
5127  return true;
5128  }
5129 
5130  /// Get the address of the global variable.
5131  ///
5132  /// The address of the global variable is considered to be the value
5133  /// of the DW_AT_location attribute, possibly adjusted (in
5134  /// relocatable files only) to not point to an absolute address
5135  /// anymore, but rather to the address of the global variable inside
5136  /// the data segment.
5137  ///
5138  /// @param variable_die the die of the function to consider.
5139  ///
5140  /// @param address the resulting address iff this function returns
5141  /// true.
5142  ///
5143  /// @return true if the variable address was found.
5144  bool
5145  get_variable_address(const Dwarf_Die* variable_die,
5146  Dwarf_Addr& address) const
5147  {
5148  bool is_tls_address = false;
5149  if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5150  address, is_tls_address))
5151  return false;
5152  if (!is_tls_address)
5153  address = maybe_adjust_var_sym_address(address);
5154  return true;
5155  }
5156 
5157  /// Getter of the exported decls builder object.
5158  ///
5159  /// @return the exported decls builder.
5160  corpus::exported_decls_builder*
5161  exported_decls_builder()
5162  {return corpus()->get_exported_decls_builder().get();}
5163 
5164  /// Getter of the "load_all_types" flag. This flag tells if all the
5165  /// types (including those not reachable by public declarations) are
5166  /// to be read and represented in the final ABI corpus.
5167  ///
5168  /// @return the load_all_types flag.
5169  bool
5170  load_all_types() const
5171  {return options().load_all_types;}
5172 
5173  /// Setter of the "load_all_types" flag. This flag tells if all the
5174  /// types (including those not reachable by public declarations) are
5175  /// to be read and represented in the final ABI corpus.
5176  ///
5177  /// @param f the new load_all_types flag.
5178  void
5179  load_all_types(bool f)
5180  {options().load_all_types = f;}
5181 
5182  bool
5183  load_in_linux_kernel_mode() const
5184  {return options().load_in_linux_kernel_mode;}
5185 
5186  void
5187  load_in_linux_kernel_mode(bool f)
5188  {options().load_in_linux_kernel_mode = f;}
5189 
5190  /// Getter of the 'load-undefined-interface' property.
5191  ///
5192  /// That property tells the reader if it should load the interfaces
5193  /// that are undefined in the binary. An undefined interface is a
5194  /// variable or function which has a symbol that is not defined in
5195  /// the binary.
5196  ///
5197  /// @return true iff the front-end has to load the undefined
5198  /// interfaces.
5199  bool
5200  load_undefined_interfaces() const
5201  {return options().load_undefined_interfaces;}
5202 
5203  /// Test if it's allowed to assume that the DWARF debug info has
5204  /// been factorized (for instance, with the DWZ tool) so that if two
5205  /// type DIEs originating from the .gnu_debugaltlink section have
5206  /// different offsets, they represent different types.
5207  ///
5208  /// @return true iff we can assume that the DWARF debug info has
5209  /// been factorized.
5210  bool
5211  leverage_dwarf_factorization() const
5212  {
5213  if (!leverage_dwarf_factorization_.has_value())
5214  {
5215  if (options().leverage_dwarf_factorization
5216  && elf_helpers::find_section_by_name(elf_handle(),
5217  ".gnu_debugaltlink"))
5218  leverage_dwarf_factorization_ = true;
5219  else
5220  leverage_dwarf_factorization_ = false;
5221  }
5222  ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5223 
5224  return *leverage_dwarf_factorization_;
5225  }
5226  /// Getter of the "show_stats" flag.
5227  ///
5228  /// This flag tells if we should emit statistics about various
5229  /// internal stuff.
5230  ///
5231  /// @return the value of the flag.
5232  bool
5233  show_stats() const
5234  {return options().show_stats;}
5235 
5236  /// Setter of the "show_stats" flag.
5237  ///
5238  /// This flag tells if we should emit statistics about various
5239  /// internal stuff.
5240  ///
5241  /// @param f the value of the flag.
5242  void
5243  show_stats(bool f)
5244  {options().show_stats = f;}
5245 
5246  /// Getter of the "do_log" flag.
5247  ///
5248  /// This flag tells if we should log about various internal
5249  /// details.
5250  ///
5251  /// return the "do_log" flag.
5252  bool
5253  do_log() const
5254  {return options().do_log;}
5255 
5256  /// Setter of the "do_log" flag.
5257  ///
5258  /// This flag tells if we should log about various internal details.
5259  ///
5260  /// @param f the new value of the flag.
5261  void
5262  do_log(bool f)
5263  {options().do_log = f;}
5264 
5265  /// Walk the DIEs under a given die and for each child, populate the
5266  /// die -> parent map to record the child -> parent relationship
5267  /// that
5268  /// exists between the child and the given die.
5269  ///
5270  /// The function also builds the vector of places where units are
5271  /// imported.
5272  ///
5273  /// This is done recursively as for each child DIE, this function
5274  /// walks its children as well.
5275  ///
5276  /// @param die the DIE whose children to walk recursively.
5277  ///
5278  /// @param source where the DIE @p die comes from.
5279  ///
5280  /// @param imported_units a vector containing all the offsets of the
5281  /// points where unit have been imported, under @p die.
5282  void
5283  build_die_parent_relations_under(Dwarf_Die* die,
5284  die_source source,
5285  imported_unit_points_type & imported_units)
5286  {
5287  if (!die)
5288  return;
5289 
5290  offset_offset_map_type& parent_of = die_parent_map(source);
5291 
5292  Dwarf_Die child;
5293  if (dwarf_child(die, &child) != 0)
5294  return;
5295 
5296  do
5297  {
5298  parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5299  if (dwarf_tag(&child) == DW_TAG_imported_unit)
5300  {
5301  Dwarf_Die imported_unit;
5302  if (die_die_attribute(&child, DW_AT_import, imported_unit)
5303  // If the imported_unit has a sub-tree, let's record
5304  // this point at which the sub-tree is imported into
5305  // the current debug info.
5306  //
5307  // Otherwise, if the imported_unit has no sub-tree,
5308  // there is no point in recording where a non-existent
5309  // sub-tree is being imported.
5310  //
5311  // Note that the imported_unit_points_type type below
5312  // expects the imported_unit to have a sub-tree.
5313  && die_has_children(&imported_unit))
5314  {
5315  die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5316  ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5317  imported_units.push_back
5318  (imported_unit_point(dwarf_dieoffset(&child),
5319  imported_unit,
5320  imported_unit_die_source));
5321  }
5322  }
5323  build_die_parent_relations_under(&child, source, imported_units);
5324  }
5325  while (dwarf_siblingof(&child, &child) == 0);
5326 
5327  }
5328 
5329  /// Determine if we do have to build a DIE -> parent map, depending
5330  /// on a given language.
5331  ///
5332  /// Some languages like C++, Ada etc, do have the concept of
5333  /// namespace and yet, the DIE data structure doesn't provide us
5334  /// with a way to get the parent namespace of a given DIE. So for
5335  /// those languages, we need to build a DIE -> parent map so that we
5336  /// can get the namespace DIE (or more generally the scope DIE) of a given
5337  /// DIE as we need it.
5338  ///
5339  /// But then some more basic languages like C or assembly don't have
5340  /// that need.
5341  ///
5342  /// This function, depending on the language, tells us if we need to
5343  /// build the DIE -> parent map or not.
5344  ///
5345  /// @param lang the language to consider.
5346  ///
5347  /// @return true iff we need to build the DIE -> parent map for this
5348  /// language.
5349  bool
5350  do_we_build_die_parent_maps(translation_unit::language lang)
5351  {
5352  if (is_c_language(lang))
5353  return false;
5354 
5355  switch (lang)
5356  {
5357  case translation_unit::LANG_UNKNOWN:
5358 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5359  case translation_unit::LANG_Mips_Assembler:
5360 #endif
5361  return false;
5362  default:
5363  break;
5364  }
5365  return true;
5366  }
5367 
5368  /// Walk all the DIEs accessible in the debug info (and in the
5369  /// alternate debug info as well) and build maps representing the
5370  /// relationship DIE -> parent. That is, make it so that we can get
5371  /// the parent for a given DIE.
5372  ///
5373  /// Note that the goal of this map is to be able to get the parent
5374  /// of a given DIE. This is to mainly to handle namespaces. For instance,
5375  /// when we get a DIE of a type, and we want to build an internal
5376  /// representation for it, we need to get its fully qualified name.
5377  /// For that, we need to know what is the parent DIE of that type
5378  /// DIE, so that we can know what the namespace of that type is.
5379  ///
5380  /// Note that as the C language doesn't have namespaces (all types
5381  /// are defined in the same global namespace), this function doesn't
5382  /// build the DIE -> parent map if the current translation unit
5383  /// comes from C. This saves time on big C ELF files with a lot of
5384  /// DIEs.
5385  void
5386  build_die_parent_maps()
5387  {
5388  bool we_do_have_to_build_die_parent_map = false;
5389  uint8_t address_size = 0;
5390  size_t header_size = 0;
5391  // Get the DIE of the current translation unit, look at it to get
5392  // its language. If that language is in C, then all types are in
5393  // the global namespace so we don't need to build the DIE ->
5394  // parent map. So we dont build it in that case.
5395  for (Dwarf_Off offset = 0, next_offset = 0;
5396  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5397  offset, &next_offset, &header_size,
5398  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5399  offset = next_offset)
5400  {
5401  Dwarf_Off die_offset = offset + header_size;
5402  Dwarf_Die cu;
5403  if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5404  die_offset, &cu))
5405  continue;
5406 
5407  uint64_t l = 0;
5408  die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5409  translation_unit::language lang = dwarf_language_to_tu_language(l);
5410  if (do_we_build_die_parent_maps(lang))
5411  we_do_have_to_build_die_parent_map = true;
5412  }
5413 
5414  if (!we_do_have_to_build_die_parent_map)
5415  return;
5416 
5417  // Build the DIE -> parent relation for DIEs coming from the
5418  // .debug_info section in the alternate debug info file.
5419  die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5420  for (Dwarf_Off offset = 0, next_offset = 0;
5421  (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5422  offset, &next_offset, &header_size,
5423  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5424  offset = next_offset)
5425  {
5426  Dwarf_Off die_offset = offset + header_size;
5427  Dwarf_Die cu;
5428  if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5429  die_offset, &cu))
5430  continue;
5431  cur_tu_die(&cu);
5432 
5433  imported_unit_points_type& imported_units =
5434  tu_die_imported_unit_points_map(source)[die_offset] =
5436  build_die_parent_relations_under(&cu, source, imported_units);
5437  }
5438 
5439  // Build the DIE -> parent relation for DIEs coming from the
5440  // .debug_info section of the main debug info file.
5441  source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5442  address_size = 0;
5443  header_size = 0;
5444  for (Dwarf_Off offset = 0, next_offset = 0;
5445  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5446  offset, &next_offset, &header_size,
5447  NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5448  offset = next_offset)
5449  {
5450  Dwarf_Off die_offset = offset + header_size;
5451  Dwarf_Die cu;
5452  if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5453  die_offset, &cu))
5454  continue;
5455  cur_tu_die(&cu);
5456  imported_unit_points_type& imported_units =
5457  tu_die_imported_unit_points_map(source)[die_offset] =
5459  build_die_parent_relations_under(&cu, source, imported_units);
5460  }
5461 
5462  // Build the DIE -> parent relation for DIEs coming from the
5463  // .debug_types section.
5464  source = TYPE_UNIT_DIE_SOURCE;
5465  address_size = 0;
5466  header_size = 0;
5467  uint64_t type_signature = 0;
5468  Dwarf_Off type_offset;
5469  for (Dwarf_Off offset = 0, next_offset = 0;
5470  (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5471  offset, &next_offset, &header_size,
5472  NULL, NULL, &address_size, NULL,
5473  &type_signature, &type_offset) == 0);
5474  offset = next_offset)
5475  {
5476  Dwarf_Off die_offset = offset + header_size;
5477  Dwarf_Die cu;
5478 
5479  if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5480  die_offset, &cu))
5481  continue;
5482  cur_tu_die(&cu);
5483  imported_unit_points_type& imported_units =
5484  tu_die_imported_unit_points_map(source)[die_offset] =
5486  build_die_parent_relations_under(&cu, source, imported_units);
5487  }
5488  }
5489 };// end class reader.
5490 
5491 /// The type of the aggregates being compared during a DIE comparison.
5492 ///
5493 /// This encapsulates the stack of aggregates being compared at any
5494 /// single point.
5495 ///
5496 /// This is useful to detect "comparison cycles" and thus avoid the
5497 /// resulting infinite loops.
5498 ///
5499 /// This is also useful for implementing a very important optimization
5500 /// that takes place during the canonicalization
5501 struct offset_pairs_stack_type
5502 {
5503  // The DWARF DWARF reader that is useful for so many things.
5504  const reader& rdr_;
5505  // The set of types that are being compared. This is to speed up
5506  // searches.
5507  offset_pair_set_type set_;
5508  // The stack of types that are being compared. The top of the
5509  // stack is the back of the vector.
5511  // A map that associates a redundant type pair to the vector of
5512  // types that depends on it.
5513  offset_pair_vect_map_type redundant_types_;
5514  // A map that associates a dependant type to the vector of redundant
5515  // types it depends on.
5516  offset_pair_vect_map_type dependant_types_;
5517 
5518  offset_pairs_stack_type(const reader& rdr)
5519  : rdr_ (rdr)
5520  {}
5521 
5522  /// Add a pair of types being compared to the stack of aggregates
5523  /// being compared.
5524  ///
5525  /// @param p the pair of offsets of the type DIEs to consider.
5526  void
5527  add(const offset_pair_type& p)
5528  {
5529  set_.insert(p);
5530  vect_.push_back(p);
5531  }
5532 
5533  /// Erase a pair of types being compared from the stack of
5534  /// aggregates being compared.
5535  ///
5536  /// @param p the pair of offsets of the type DIEs to consider.
5537  ///
5538  /// @return true iff @p was found and erased from the stack.
5539  bool
5540  erase(const offset_pair_type& p)
5541  {
5542  if (set_.erase(p))
5543  {
5544  offset_pair_vector_type::iterator i;
5545 
5546  for (i = vect_.begin();i < vect_.end(); ++i)
5547  if (*i == p)
5548  break;
5549 
5550  if (i != vect_.end())
5551  vect_.erase(i);
5552 
5553  return true;
5554  }
5555 
5556  return false;
5557  }
5558 
5559  /// Test if a pair of type DIEs is part of the stack of type DIEs
5560  /// being compared.
5561  ///
5562  /// @param p the pair of offsets of the type DIEs to consider.
5563  ///
5564  /// @return true iff @p was found in the stack of types being
5565  /// compared.
5566  bool
5567  contains(const offset_pair_type &p) const
5568  {
5569  if (set_.find(p) == set_.end())
5570  return false;
5571  return true;
5572  }
5573 
5574  /// Get the set of comparison pair that depends on a given
5575  /// comparison pair.
5576  ///
5577  /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5578  /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5579  /// words, the pair T appears in the comparison stack BEFORE the
5580  /// pair P.
5581  ///
5582  /// So, this function returns the vector of comparison pairs that
5583  /// appear in the comparison stack AFTER a given comparison pair.
5584  ///
5585  /// @param p the comparison pair to consider.
5586  ///
5587  /// @param pairs out parameter. This is filled with the comparison
5588  /// pairs that depend on @p, iff the function returns true.
5589  ///
5590  /// @return true iff comparison pairs depending on @p have been
5591  /// found and collected in @pairs.
5592  bool
5593  get_pairs_that_depend_on(const offset_pair_type& p,
5594  offset_pair_vector_type& pairs) const
5595  {
5596  bool result = false;
5597  if (!contains(p))
5598  return result;
5599 
5600  // First, get an iterator on the position of 'p'.
5601  offset_pair_vector_type::const_iterator i;
5602  for (i = vect_.begin(); i != vect_.end(); ++i)
5603  if (*i == p)
5604  break;
5605 
5606  if (i == vect_.end())
5607  return result;
5608 
5609  // Then, harvest all the comparison pairs that come after the
5610  // position of 'p'.
5611  for (++i; i != vect_.end(); ++i)
5612  {
5613  pairs.push_back(*i);
5614  result = true;
5615  }
5616 
5617  return result;
5618  }
5619 
5620  /// Record the fact that a set of comparison pairs depends on a
5621  /// given comparison pair.
5622  ///
5623  /// Set a map that associates each dependant comparison pair to the
5624  /// pair it depends on.
5625  ///
5626  /// @param p the comparison pair that the set depends on.
5627  ///
5628  /// @param dependant_types the set of types that depends on @p.
5629  void
5630  record_dependant_types(const offset_pair_type& p,
5631  const offset_pair_vector_type& dependant_types)
5632  {
5633  for (auto type_pair : dependant_types)
5634  dependant_types_[type_pair].push_back(p);
5635  }
5636 
5637  /// Record a comparison pair as being redundant.
5638  ///
5639  ///
5640  /// @param p the comparison pair to record as redundant.
5641  void
5642  record_redundant_type_die_pair(const offset_pair_type& p)
5643  {
5644  offset_pair_vector_type dependant_types;
5645  get_pairs_that_depend_on(p, dependant_types);
5646 
5647  // First, record the relationship "p -> [pairs that depend on p]".
5648  auto it = redundant_types_.find(p);
5649  if (it == redundant_types_.end())
5650  {
5651  auto entry = std::make_pair(p, dependant_types);
5652  redundant_types_.insert(entry);
5653  }
5654  else
5655  it->second.insert(it->second.end(),
5656  dependant_types.begin(),
5657  dependant_types.end());
5658 
5659  // For each dependant type pair, record the association:
5660  // dependant_pair --> [vect of redundant types]
5661  record_dependant_types(p, dependant_types);
5662  }
5663 
5664  /// Test if a given pair has been detected as redundant.
5665  ///
5666  /// @param p the pair of DIEs to consider.
5667  ///
5668  /// @return iff @p is redundant.
5669  bool
5670  is_redundant(const offset_pair_type& p)
5671  {
5672  auto i = redundant_types_.find(p);
5673  if (i != redundant_types_.end())
5674  return true;
5675  return false;
5676  }
5677 
5678  /// Test if a given pair is dependant on at least a redundant type.
5679  ///
5680  /// @param p the pair to consider.
5681  ///
5682  /// @return true iff @p depends on a redundant type.
5683  bool
5684  depends_on_redundant_types(const offset_pair_type& p)
5685  {
5686  auto i = dependant_types_.find(p);
5687  if (i == dependant_types_.end())
5688  return false;
5689  return true;
5690  }
5691 
5692  /// Remove a redundant pair from the system.
5693  ///
5694  /// This needs updating the system to also remove the dependant
5695  /// types that depend on the redundant pair (if they depend only on
5696  /// that redundant pair).
5697  ///
5698  /// @param p the pair to consider.
5699  ///
5700  /// @param erase_canonical_die_offset if true then erase the cached
5701  /// comparison results for the redundant pair and its dependant
5702  /// types.
5703  void
5704  erase_redundant_type_pair_entry(const offset_pair_type& p,
5705  bool erase_cached_results = false)
5706  {
5707  // First, update the dependant types that depend on the redundant
5708  // type pair
5709  auto redundant_type = redundant_types_.find(p);
5710  if (redundant_type != redundant_types_.end())
5711  {
5712  for (auto dependant_type : redundant_type->second)
5713  {
5714  // Each dependant_type depends on the redundant type 'p',
5715  // among others.
5716  auto dependant_types_it = dependant_types_.find(dependant_type);
5717  ABG_ASSERT(dependant_types_it != dependant_types_.end());
5718  // Erase the redundant type 'p' from the redundant types
5719  // that dependant_type depends on.
5720  {
5721  auto i = dependant_types_it->second.begin();
5722  for (; i!= dependant_types_it->second.end();++i)
5723  if (*i == p)
5724  break;
5725  if (i != dependant_types_it->second.end())
5726  dependant_types_it->second.erase(i);
5727  }
5728  // If the dependant type itself doesn't depend on ANY
5729  // redundant type anymore, then remove the depend type
5730  // from the map of the dependant types.
5731  if (dependant_types_it->second.empty())
5732  {
5733  if (erase_cached_results)
5734  rdr_.die_comparison_results_.erase(dependant_type);
5735  dependant_types_.erase(dependant_types_it);
5736  }
5737  }
5738  }
5739  if (erase_cached_results)
5740  rdr_.die_comparison_results_.erase(p);
5741  redundant_types_.erase(p);
5742  }
5743 
5744  /// If a comparison pair has been detected as redundant, stop
5745  /// tracking it as well as its dependant pairs. That will
5746  /// essentially make it impossible to reset/cancel the canonical
5747  /// propagated types for those depdant pairs, but will also save
5748  /// ressources.
5749  ///
5750  /// @param p the comparison pair to consider.
5751  void
5752  confirm_canonical_propagated_type(const offset_pair_type& p)
5753  {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5754 
5755  /// Walk the types that depend on a comparison pair and cancel their
5756  /// canonical-propagate-type, that means remove their canonical
5757  /// types and mark them as not being canonically-propagated. Also,
5758  /// erase their cached comparison results that was likely set to
5759  /// COMPARISON_RESULT_UNKNOWN.
5760  ///
5761  /// @param p the pair to consider.
5762  void
5763  cancel_canonical_propagated_type(const offset_pair_type& p)
5764  {
5765  offset_pair_set_type dependant_types;
5766  get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5767  for (auto dependant_type : dependant_types)
5768  {
5769  // If this dependant type was canonical-type-propagated then
5770  // erase that canonical type.
5771  if (rdr_.propagated_types_.find(dependant_type)
5772  != rdr_.propagated_types_.end())
5773  {
5774  rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5775  dependant_type.first.source_,
5776  /*die_as_type=*/true);
5777  rdr_.propagated_types_.erase(dependant_type);
5778  rdr_.cancelled_propagation_count_++;
5779  }
5780  // Update the cached result. We know the comparison result
5781  // must now be different.
5782  auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
5783  if (comp_result_it != rdr_.die_comparison_results_.end())
5784  comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5785  }
5786 
5787  // Update the cached result of the root type to cancel too.
5788  auto comp_result_it = rdr_.die_comparison_results_.find(p);
5789  if (comp_result_it != rdr_.die_comparison_results_.end())
5790  {
5791  // At this point, the result of p is either
5792  // COMPARISON_RESULT_UNKNOWN (if we cache comparison
5793  // results of that kind) or COMPARISON_RESULT_DIFFERENT.
5794  // Make sure it's the cached result is now
5795  // COMPARISON_RESULT_DIFFERENT.
5796  if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
5797  comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5798  ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
5799  }
5800 
5801  if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5802  {
5803  rdr_.erase_canonical_die_offset(p.first.offset_,
5804  p.first.source_,
5805  /*die_as_type=*/true);
5806  rdr_.propagated_types_.erase(p);
5807  rdr_.cancelled_propagation_count_++;
5808  }
5809  }
5810 
5811  /// Get the set of comparison pairs that depend on a given pair.
5812  ///
5813  /// @param p the pair to consider.
5814  ///
5815  /// @param result this is set to the pairs that depend on @p, iff
5816  /// the function returned true.
5817  ///
5818  /// @param transitive_closure if set to true, the transitive closure
5819  /// of the @result is set to it.
5820  ///
5821  /// @return true iff @result could be filled with the dependant
5822  /// types.
5823  bool
5824  get_dependant_types(const offset_pair_type& p,
5825  offset_pair_set_type& result,
5826  bool transitive_closure = false)
5827  {
5828  auto i = redundant_types_.find(p);
5829  if (i != redundant_types_.end())
5830  {
5831  for (auto dependant_type : i->second)
5832  if (result.find(dependant_type) == result.end())
5833  {
5834  result.insert(dependant_type);
5835  if (transitive_closure)
5836  get_dependant_types(p, result, /*transitive_closure=*/true);
5837  }
5838  return true;
5839  }
5840  return false;
5841  }
5842 }; // end struct offset_pairs_stack_type
5843 
5845 build_ir_node_from_die(reader& rdr,
5846  Dwarf_Die* die,
5847  scope_decl* scope,
5848  bool called_from_public_decl,
5849  size_t where_offset,
5850  bool is_declaration_only = true,
5851  bool is_required_decl_spec = false);
5852 
5854 build_ir_node_from_die(reader& rdr,
5855  Dwarf_Die* die,
5856  bool called_from_public_decl,
5857  size_t where_offset);
5858 
5859 static decl_base_sptr
5860 build_ir_node_for_void_type(reader& rdr);
5861 
5863 build_ir_node_for_void_pointer_type(reader& rdr);
5864 
5865 static class_decl_sptr
5866 add_or_update_class_type(reader& rdr,
5867  Dwarf_Die* die,
5868  scope_decl* scope,
5869  bool is_struct,
5870  class_decl_sptr klass,
5871  bool called_from_public_decl,
5872  size_t where_offset,
5873  bool is_declaration_only);
5874 
5875 static union_decl_sptr
5876 add_or_update_union_type(reader& rdr,
5877  Dwarf_Die* die,
5878  scope_decl* scope,
5879  union_decl_sptr union_type,
5880  bool called_from_public_decl,
5881  size_t where_offset,
5882  bool is_declaration_only);
5883 
5884 static decl_base_sptr
5885 build_ir_node_for_void_type(reader& rdr);
5886 
5887 static decl_base_sptr
5888 build_ir_node_for_variadic_parameter_type(reader &rdr);
5889 
5890 static function_decl_sptr
5891 build_function_decl(reader& rdr,
5892  Dwarf_Die* die,
5893  size_t where_offset,
5894  function_decl_sptr fn);
5895 
5896 static bool
5897 function_is_suppressed(const reader& rdr,
5898  const scope_decl* scope,
5899  Dwarf_Die *function_die,
5900  bool is_declaration_only);
5901 
5902 static function_decl_sptr
5903 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5904  scope_decl *scope,
5905  Dwarf_Die *die,
5906  size_t where_offset,
5907  bool is_declaration_only,
5908  function_decl_sptr f);
5909 
5910 static var_decl_sptr
5911 build_var_decl(reader& rdr,
5912  Dwarf_Die *die,
5913  size_t where_offset,
5914  var_decl_sptr result = var_decl_sptr());
5915 
5916 static var_decl_sptr
5917 build_or_get_var_decl_if_not_suppressed(reader& rdr,
5918  scope_decl *scope,
5919  Dwarf_Die *die,
5920  size_t where_offset,
5921  bool is_declaration_only,
5922  var_decl_sptr res = var_decl_sptr(),
5923  bool is_required_decl_spec = false);
5924 static bool
5925 variable_is_suppressed(const reader& rdr,
5926  const scope_decl* scope,
5927  Dwarf_Die *variable_die,
5928  bool is_declaration_only,
5929  bool is_required_decl_spec = false);
5930 
5931 static void
5932 finish_member_function_reading(Dwarf_Die* die,
5933  const function_decl_sptr& f,
5934  const class_or_union_sptr klass,
5935  reader& rdr);
5936 
5937 /// Test if a given DIE is anonymous
5938 ///
5939 /// @param die the DIE to consider.
5940 ///
5941 /// @return true iff @p die is anonymous.
5942 static bool
5943 die_is_anonymous(const Dwarf_Die* die)
5944 {
5945  Dwarf_Attribute attr;
5946  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5947  return true;
5948  return false;
5949 }
5950 
5951 /// Test if a DIE is an anonymous data member, aka, "unnamed field".
5952 ///
5953 /// Unnamed fields are specified at
5954 /// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
5955 ///
5956 /// @param die the DIE to consider.
5957 ///
5958 /// @return true iff @p die is an anonymous data member.
5959 static bool
5960 die_is_anonymous_data_member(const Dwarf_Die* die)
5961 {
5962  if (!die
5963  || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
5964  || !die_name(die).empty())
5965  return false;
5966 
5967  Dwarf_Die type_die;
5968  if (!die_die_attribute(die, DW_AT_type, type_die))
5969  return false;
5970 
5971  if (dwarf_tag(&type_die) != DW_TAG_structure_type
5972  && dwarf_tag(&type_die) != DW_TAG_union_type)
5973  return false;
5974 
5975  return true;
5976 }
5977 
5978 /// Get the value of an attribute that is supposed to be a string, or
5979 /// an empty string if the attribute could not be found.
5980 ///
5981 /// @param die the DIE to get the attribute value from.
5982 ///
5983 /// @param attr_name the attribute name. Must come from dwarf.h and
5984 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
5985 ///
5986 /// @return the string representing the value of the attribute, or an
5987 /// empty string if no string attribute could be found.
5988 static string
5989 die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5990 {
5991  if (!die)
5992  return "";
5993 
5994  Dwarf_Attribute attr;
5995  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5996  return "";
5997 
5998  const char* str = dwarf_formstring(&attr);
5999  return str ? str : "";
6000 }
6001 
6002 /// Get the value of an attribute that is supposed to be a string, or
6003 /// an empty string if the attribute could not be found.
6004 ///
6005 /// @param die the DIE to get the attribute value from.
6006 ///
6007 /// @param attr_name the attribute name. Must come from dwarf.h and
6008 /// be an enumerator representing an attribute like, e.g, DW_AT_name.
6009 ///
6010 /// @return the char* representing the value of the attribute, or an
6011 /// empty string if no string attribute could be found.
6012 static const char*
6013 die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
6014 {
6015  if (!die)
6016  return nullptr;
6017 
6018  Dwarf_Attribute attr;
6019  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6020  return nullptr;
6021 
6022  const char* str = dwarf_formstring(&attr);
6023  return str;
6024 }
6025 
6026 /// Get the value of an attribute that is supposed to be an unsigned
6027 /// constant.
6028 ///
6029 /// @param die the DIE to read the information from.
6030 ///
6031 /// @param attr_name the DW_AT_* name of the attribute. Must come
6032 /// from dwarf.h and be an enumerator representing an attribute like,
6033 /// e.g, DW_AT_decl_line.
6034 ///
6035 ///@param cst the output parameter that is set to the value of the
6036 /// attribute @p attr_name. This parameter is set iff the function
6037 /// return true.
6038 ///
6039 /// @return true if there was an attribute of the name @p attr_name
6040 /// and with a value that is a constant, false otherwise.
6041 static bool
6042 die_unsigned_constant_attribute(const Dwarf_Die* die,
6043  unsigned attr_name,
6044  uint64_t& cst)
6045 {
6046  if (!die)
6047  return false;
6048 
6049  Dwarf_Attribute attr;
6050  Dwarf_Word result = 0;
6051  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6052  || dwarf_formudata(&attr, &result))
6053  return false;
6054 
6055  cst = result;
6056  return true;
6057 }
6058 
6059 /// Read a signed constant value from a given attribute.
6060 ///
6061 /// The signed constant expected must be of constant form.
6062 ///
6063 /// @param die the DIE to get the attribute from.
6064 ///
6065 /// @param attr_name the attribute name.
6066 ///
6067 /// @param cst the resulting signed constant read.
6068 ///
6069 /// @return true iff a signed constant attribute of the name @p
6070 /// attr_name was found on the DIE @p die.
6071 static bool
6072 die_signed_constant_attribute(const Dwarf_Die *die,
6073  unsigned attr_name,
6074  int64_t& cst)
6075 {
6076  if (!die)
6077  return false;
6078 
6079  Dwarf_Attribute attr;
6080  Dwarf_Sword result = 0;
6081  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6082  || dwarf_formsdata(&attr, &result))
6083  return false;
6084 
6085  cst = result;
6086  return true;
6087 }
6088 
6089 /// Read the value of a constant attribute that is either signed or
6090 /// unsigned into a array_type_def::subrange_type::bound_value value.
6091 ///
6092 /// The bound_value instance will capture the actual signedness of the
6093 /// read attribute.
6094 ///
6095 /// @param die the DIE from which to read the value of the attribute.
6096 ///
6097 /// @param attr_name the attribute name to consider.
6098 ///
6099 /// @param is_signed true if the attribute value has to read as
6100 /// signed.
6101 ///
6102 /// @param value the resulting value read from attribute @p attr_name
6103 /// on DIE @p die.
6104 ///
6105 /// @return true iff DIE @p die has an attribute named @p attr_name
6106 /// with a constant value.
6107 static bool
6108 die_constant_attribute(const Dwarf_Die *die,
6109  unsigned attr_name,
6110  bool is_signed,
6111  array_type_def::subrange_type::bound_value &value)
6112 {
6113  if (!is_signed)
6114  {
6115  uint64_t l = 0;
6116  if (!die_unsigned_constant_attribute(die, attr_name, l))
6117  return false;
6118  value.set_unsigned(l);
6119  }
6120  else
6121  {
6122  int64_t l = 0;
6123  if (!die_signed_constant_attribute(die, attr_name, l))
6124  return false;
6125  value.set_signed(l);
6126  }
6127  return true;
6128 }
6129 
6130 /// Test if a given DWARF form is DW_FORM_strx{1,4}.
6131 ///
6132 /// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6133 /// enum in dwarf.h so we have to use an unsigned int for the form,
6134 /// grrr.
6135 ///
6136 /// @param form the form to consider.
6137 ///
6138 /// @return true iff @p form is DW_FORM_strx{1,4}.
6139 static bool
6140 form_is_DW_FORM_strx(unsigned form)
6141 {
6142  if (form)
6143  {
6144 #if defined HAVE_DW_FORM_strx1 \
6145  && defined HAVE_DW_FORM_strx2 \
6146  && defined HAVE_DW_FORM_strx3 \
6147  && defined HAVE_DW_FORM_strx4
6148  if (form == DW_FORM_strx1
6149  || form == DW_FORM_strx2
6150  || form == DW_FORM_strx3
6151  ||form == DW_FORM_strx4)
6152  return true;
6153 #endif
6154  }
6155  return false;
6156 }
6157 
6158 /// Test if a given DWARF form is DW_FORM_line_strp.
6159 ///
6160 /// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6161 /// enum in dwarf.h so we have to use an unsigned int for the form,
6162 /// grrr.
6163 ///
6164 /// @param form the form to consider.
6165 ///
6166 /// @return true iff @p form is DW_FORM_line_strp.
6167 static bool
6168 form_is_DW_FORM_line_strp(unsigned form)
6169 {
6170  if (form)
6171  {
6172 #if defined HAVE_DW_FORM_line_strp
6173  if (form == DW_FORM_line_strp)
6174  return true;
6175 #endif
6176  }
6177  return false;
6178 }
6179 
6180 /// Get the value of a DIE attribute; that value is meant to be a
6181 /// flag.
6182 ///
6183 /// @param die the DIE to get the attribute from.
6184 ///
6185 /// @param attr_name the DW_AT_* name of the attribute. Must come
6186 /// from dwarf.h and be an enumerator representing an attribute like,
6187 /// e.g, DW_AT_external.
6188 ///
6189 /// @param flag the output parameter to store the flag value into.
6190 /// This is set iff the function returns true.
6191 ///
6192 /// @param recursively if true, the function looks through the
6193 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6194 /// all the way down to the initial DIE that is cloned and look on
6195 /// that DIE to see if it has the @p attr_name attribute.
6196 ///
6197 /// @return true if the DIE has a flag attribute named @p attr_name,
6198 /// false otherwise.
6199 static bool
6200 die_flag_attribute(const Dwarf_Die* die,
6201  unsigned attr_name,
6202  bool& flag,
6203  bool recursively = true)
6204 {
6205  Dwarf_Attribute attr;
6206  if (recursively
6207  ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6208  : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6209  return false;
6210 
6211  bool f = false;
6212  if (dwarf_formflag(&attr, &f))
6213  return false;
6214 
6215  flag = f;
6216  return true;
6217 }
6218 
6219 /// Get the mangled name from a given DIE.
6220 ///
6221 /// @param die the DIE to read the mangled name from.
6222 ///
6223 /// @return the mangled name if it's present in the DIE, or just an
6224 /// empty string if it's not.
6225 static string
6226 die_linkage_name(const Dwarf_Die* die)
6227 {
6228  if (!die)
6229  return "";
6230 
6231  string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6232  if (linkage_name.empty())
6233  linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6234  return linkage_name;
6235 }
6236 
6237 /// Get the file path that is the value of the DW_AT_decl_file
6238 /// attribute on a given DIE, if the DIE is a decl DIE having that
6239 /// attribute.
6240 ///
6241 /// @param die the DIE to consider.
6242 ///
6243 /// @return a string containing the file path that is the logical
6244 /// value of the DW_AT_decl_file attribute. If the DIE @p die
6245 /// doesn't have a DW_AT_decl_file attribute, then the return value is
6246 /// just an empty string.
6247 static string
6248 die_decl_file_attribute(const Dwarf_Die* die)
6249 {
6250  if (!die)
6251  return "";
6252 
6253  const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6254 
6255  return str ? str : "";
6256 }
6257 
6258 /// Get the value of an attribute which value is supposed to be a
6259 /// reference to a DIE.
6260 ///
6261 /// @param die the DIE to read the value from.
6262 ///
6263 /// @param attr_name the DW_AT_* attribute name to read.
6264 ///
6265 /// @param result the DIE resulting from reading the attribute value.
6266 /// This is set iff the function returns true.
6267 ///
6268 /// @param recursively if true, the function looks through the
6269 /// possible DW_AT_specification and DW_AT_abstract_origin attribute
6270 /// all the way down to the initial DIE that is cloned and look on
6271 /// that DIE to see if it has the @p attr_name attribute.
6272 ///
6273 /// @return true if the DIE @p die contains an attribute named @p
6274 /// attr_name that is a DIE reference, false otherwise.
6275 static bool
6276 die_die_attribute(const Dwarf_Die* die,
6277  unsigned attr_name,
6278  Dwarf_Die& result,
6279  bool recursively)
6280 {
6281  Dwarf_Attribute attr;
6282  if (recursively
6283  ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6284  : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6285  return false;
6286 
6287  return dwarf_formref_die(&attr, &result);
6288 }
6289 
6290 /// Get the DIE that is the "origin" of the current one.
6291 ///
6292 /// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6293 /// attribute. Those DIEs represent a concrete instance of an
6294 /// abstract entity. The concrete instance can be a concrete instance
6295 /// of an inline function, or the concrete implementation of an
6296 /// abstract interface. On both cases, we call the abstract instance
6297 /// from which the concrete instance derives the "origin".
6298 ///
6299 /// This function returns the ultimate origin DIE of a given DIE by
6300 /// following the chain of its DW_AT_abstract_origin and
6301 /// DW_AT_specification attributes.
6302 ///
6303 /// @param die the DIE to consider.
6304 ///
6305 /// @param origin_die this is an output parameter that is set by this
6306 /// function to the resulting origin DIE iff the function returns
6307 /// true.
6308 ///
6309 /// @return true iff the function actually found an origin DIE and
6310 /// set it to the @p origin_die parameter.
6311 static bool
6312 die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6313 {
6314  if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6315  || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6316  {
6317  while (die_die_attribute(&origin_die,
6318  DW_AT_specification,
6319  origin_die, true)
6320  || die_die_attribute(&origin_die,
6321  DW_AT_abstract_origin,
6322  origin_die, true))
6323  {
6324  // Keep looking for the origin die ...
6325  ;
6326  }
6327  return true;
6328  }
6329  return false;
6330 }
6331 
6332 /// Test if a subrange DIE indirectly references another subrange DIE
6333 /// through a given attribute.
6334 ///
6335 /// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6336 /// attribute be a reference to either a data member or a variable
6337 /// which type is itself a DW_TAG_subrange_type. This latter subrange
6338 /// DIE is said to be "indirectly referenced" by the former subrange
6339 /// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6340 /// the value we want for the DW_AT_upper_bound of the former.
6341 ///
6342 /// This function tests if the former subrange DIE does indirectly
6343 /// reference another subrange DIE through a given attribute (not
6344 /// necessarily DW_AT_upper_bound).
6345 ///
6346 /// @param die the DIE to consider. Note that It must be a
6347 /// DW_TAG_subrange_type.
6348 ///
6349 /// @param attr_name the name of the attribute to look through for the
6350 /// indirectly referenced subrange DIE.
6351 ///
6352 /// @param referenced_subrange if the function returns true, then the
6353 /// argument of this parameter is set to the indirectly referenced
6354 /// DW_TAG_subrange_type DIE.
6355 ///
6356 /// @return true iff @p DIE indirectly references a subrange DIE
6357 /// through the attribute @p attr_name.
6358 static bool
6359 subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6360  unsigned attr_name,
6361  Dwarf_Die& referenced_subrange)
6362 {
6363  bool result = false;
6364 
6365  if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6366  return result;
6367 
6368  Dwarf_Die referenced_die;
6369  if (die_die_attribute(die, attr_name, referenced_die))
6370  {
6371  unsigned tag = dwarf_tag(&referenced_die);
6372  if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6373  {
6374  Dwarf_Die type_die;
6375  if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6376  {
6377  tag = dwarf_tag(&type_die);
6378  if (tag == DW_TAG_subrange_type)
6379  {
6380  memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6381  result = true;
6382  }
6383  }
6384  }
6385  }
6386  return result;
6387 }
6388 
6389 /// Return the bound value of subrange die by looking at an indirectly
6390 /// referenced subrange DIE.
6391 ///
6392 /// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6393 /// attribute be a reference to either a data member or a variable
6394 /// which type is itself a DW_TAG_subrange_type. This latter subrange
6395 /// DIE is said to be "indirectly referenced" by the former subrange
6396 /// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6397 /// the value we want for the DW_AT_{lower,upper}_bound of the former.
6398 ///
6399 /// This function gets the DW_AT_{lower,upper}_bound value of a
6400 /// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6401 /// the indirectly referenced subrange type, if it exists.
6402 ///
6403 /// @param die the subrange DIE to consider.
6404 ///
6405 /// @param attr_name the name of the attribute to consider, typically,
6406 /// DW_AT_{lower,upper}_bound.
6407 ///
6408 /// @param v the found value, iff this function returned true.
6409 ///
6410 /// @param is_signed, this is set to true if @p v is signed. This
6411 /// parameter is set at all only if the function returns true.
6412 ///
6413 /// @return true iff the DW_AT_{lower,upper}_bound was found on the
6414 /// indirectly referenced subrange type.
6415 static bool
6416 subrange_die_indirect_bound_value(const Dwarf_Die *die,
6417  unsigned attr_name,
6418  array_type_def::subrange_type::bound_value& v,
6419  bool& is_signed)
6420 {
6421  bool result = false;
6422 
6423  if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6424  return result;
6425 
6426  Dwarf_Die subrange_die;
6427  if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6428  subrange_die))
6429  {
6430  if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6431  result = true;
6432  }
6433  return result;
6434 }
6435 
6436 /// Read and return an addresss class attribute from a given DIE.
6437 ///
6438 /// @param die the DIE to consider.
6439 ///
6440 /// @param attr_name the name of the address class attribute to read
6441 /// the value from.
6442 ///
6443 /// @param the resulting address.
6444 ///
6445 /// @return true iff the attribute could be read, was of the expected
6446 /// address class and could thus be translated into the @p result.
6447 static bool
6448 die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6449 {
6450  Dwarf_Attribute attr;
6451  if (!dwarf_attr_integrate(die, attr_name, &attr))
6452  return false;
6453  return dwarf_formaddr(&attr, &result) == 0;
6454 }
6455 
6456 /// Returns the source location associated with a decl DIE.
6457 ///
6458 /// @param rdr the @ref reader to use.
6459 ///
6460 /// @param die the DIE the read the source location from.
6461 ///
6462 /// @return the location associated with @p die.
6463 static location
6464 die_location(const reader& rdr, const Dwarf_Die* die)
6465 {
6466  if (!die)
6467  return location();
6468 
6469  string file = die_decl_file_attribute(die);
6470  uint64_t line = 0;
6471  die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6472 
6473  if (!file.empty() && line != 0)
6474  {
6475  translation_unit_sptr tu = rdr.cur_transl_unit();
6476  location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6477  return l;
6478  }
6479  return location();
6480 }
6481 
6482 /// Return a copy of the name of a DIE.
6483 ///
6484 /// @param die the DIE to consider.
6485 ///
6486 /// @return a copy of the name of the DIE.
6487 static string
6488 die_name(const Dwarf_Die* die)
6489 {
6490  string name = die_string_attribute(die, DW_AT_name);
6491  return name;
6492 }
6493 
6494 /// Return the location, the name and the mangled name of a given DIE.
6495 ///
6496 /// @param rdr the DWARF reader to use.
6497 ///
6498 /// @param die the DIE to read location and names from.
6499 ///
6500 /// @param loc the location output parameter to set.
6501 ///
6502 /// @param name the name output parameter to set.
6503 ///
6504 /// @param linkage_name the linkage_name output parameter to set.
6505 static void
6506 die_loc_and_name(const reader& rdr,
6507  Dwarf_Die* die,
6508  location& loc,
6509  string& name,
6510  string& linkage_name)
6511 {
6512  loc = die_location(rdr, die);
6513  name = die_name(die);
6514  linkage_name = die_linkage_name(die);
6515 }
6516 
6517 /// Return the name and the mangled name of a given DIE.
6518 ///
6519 /// @param die the DIE to read location and names from.
6520 ///
6521 /// @param name the name output parameter to set.
6522 ///
6523 /// @param linkage_name the linkage_name output parameter to set.
6524 static void
6525 die_name_and_linkage_name(const Dwarf_Die* die,
6526  string& name,
6527  string& linkage_name)
6528 {
6529  name = die_name(die);
6530  linkage_name = die_linkage_name(die);
6531 }
6532 
6533 /// Get the size of a (type) DIE as the value for the parameter
6534 /// DW_AT_byte_size or DW_AT_bit_size.
6535 ///
6536 /// @param die the DIE to read the information from.
6537 ///
6538 /// @param size the resulting size in bits. This is set iff the
6539 /// function return true.
6540 ///
6541 /// @return true if the size attribute was found.
6542 static bool
6543 die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6544 {
6545  if (!die)
6546  return false;
6547 
6548  uint64_t byte_size = 0, bit_size = 0;
6549 
6550  if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6551  {
6552  if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6553  return false;
6554  }
6555  else
6556  bit_size = byte_size * 8;
6557 
6558  size = bit_size;
6559 
6560  return true;
6561 }
6562 
6563 /// Get the access specifier (from the DW_AT_accessibility attribute
6564 /// value) of a given DIE.
6565 ///
6566 /// @param die the DIE to consider.
6567 ///
6568 /// @param access the resulting access. This is set iff the function
6569 /// returns true.
6570 ///
6571 /// @return bool if the DIE contains the DW_AT_accessibility die.
6572 static bool
6573 die_access_specifier(Dwarf_Die * die, access_specifier& access)
6574 {
6575  if (!die)
6576  return false;
6577 
6578  uint64_t a = 0;
6579  if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6580  return false;
6581 
6582  access_specifier result = private_access;
6583 
6584  switch (a)
6585  {
6586  case private_access:
6587  result = private_access;
6588  break;
6589 
6590  case protected_access:
6591  result = protected_access;
6592  break;
6593 
6594  case public_access:
6595  result = public_access;
6596  break;
6597 
6598  default:
6599  break;
6600  }
6601 
6602  access = result;
6603  return true;
6604 }
6605 
6606 /// Test whether a given DIE represents a decl that is public. That
6607 /// is, one with the DW_AT_external attribute set.
6608 ///
6609 /// @param die the DIE to consider for testing.
6610 ///
6611 /// @return true if a DW_AT_external attribute is present and its
6612 /// value is set to the true; return false otherwise.
6613 static bool
6614 die_is_public_decl(const Dwarf_Die* die)
6615 {
6616  if (!die)
6617  return false;
6618  bool is_public = false;
6619 
6620  // If this is a DW_TAG_subprogram DIE, look for the
6621  // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6622  // then it's public. In all other cases, this should return false.
6623 
6624  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6625  if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6626  die_flag_attribute(die, DW_AT_external, is_public);
6627  else if (tag == DW_TAG_namespace)
6628  {
6629  string name = die_name(die);
6630  is_public = !name.empty();
6631  }
6632 
6633  return is_public;
6634 }
6635 
6636 /// Test if a DIE is effectively public.
6637 ///
6638 /// This is meant to return true when either the DIE is public or when
6639 /// it's a variable DIE that is at (global) namespace level.
6640 ///
6641 /// @return true iff either the DIE is public or is a variable DIE
6642 /// that is at (global) namespace level.
6643 static bool
6644 die_is_effectively_public_decl(const reader& rdr,
6645  const Dwarf_Die* die)
6646 {
6647  if (die_is_public_decl(die))
6648  return true;
6649 
6650  unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6651  if (tag == DW_TAG_variable || tag == DW_TAG_member)
6652  {
6653  // The DIE is a variable.
6654  Dwarf_Die parent_die;
6655  size_t where_offset = 0;
6656  if (!get_parent_die(rdr, die, parent_die, where_offset))
6657  return false;
6658 
6659  tag = dwarf_tag(&parent_die);
6660  if (tag == DW_TAG_compile_unit
6661  || tag == DW_TAG_partial_unit
6662  || tag == DW_TAG_type_unit)
6663  // The DIE is at global scope.
6664  return true;
6665 
6666  if (tag == DW_TAG_namespace)
6667  {
6668  string name = die_name(&parent_die);
6669  if (name.empty())
6670  // The DIE at unnamed namespace scope, so it's not public.
6671  return false;
6672  // The DIE is at namespace scope.
6673  return true;
6674  }
6675  }
6676  return false;
6677 }
6678 
6679 /// Test whether a given DIE represents a declaration-only DIE.
6680 ///
6681 /// That is, if the DIE has the DW_AT_declaration flag set.
6682 ///
6683 /// @param die the DIE to consider.
6684 //
6685 /// @return true if a DW_AT_declaration is present, false otherwise.
6686 static bool
6687 die_is_declaration_only(Dwarf_Die* die)
6688 {
6689  bool is_declaration = false;
6690  die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6691  if (is_declaration && !die_has_size_attribute(die))
6692  return true;
6693  return false;
6694 }
6695 
6696 /// Test if a DIE is for a function decl.
6697 ///
6698 /// @param die the DIE to consider.
6699 ///
6700 /// @return true iff @p die represents a function decl.
6701 static bool
6702 die_is_function_decl(const Dwarf_Die *die)
6703 {
6704  if (!die)
6705  return false;
6706 
6707  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6708  if (tag == DW_TAG_subprogram)
6709  return true;
6710  return false;
6711 }
6712 
6713 /// Test if a DIE is for a variable decl.
6714 ///
6715 /// @param die the DIE to consider.
6716 ///
6717 /// @return true iff @p die represents a variable decl.
6718 static bool
6719 die_is_variable_decl(const Dwarf_Die *die)
6720 {
6721  if (!die)
6722  return false;
6723 
6724  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6725  if (tag == DW_TAG_variable)
6726  return true;
6727  return false;
6728 }
6729 
6730 /// Test if a DIE has size attribute.
6731 ///
6732 /// @param die the DIE to consider.
6733 ///
6734 /// @return true if the DIE has a size attribute.
6735 static bool
6736 die_has_size_attribute(const Dwarf_Die *die)
6737 {
6738  uint64_t s;
6739  if (die_size_in_bits(die, s))
6740  return true;
6741  return false;
6742 }
6743 
6744 /// Test that a DIE has no child DIE.
6745 ///
6746 /// @param die the DIE to consider.
6747 ///
6748 /// @return true iff @p die has no child DIE.
6749 static bool
6750 die_has_no_child(const Dwarf_Die *die)
6751 {
6752  if (!die)
6753  return true;
6754 
6755  Dwarf_Die child;
6756  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6757  return false;
6758  return true;
6759 }
6760 
6761 /// Test whether a given DIE represents a declaration-only DIE.
6762 ///
6763 /// That is, if the DIE has the DW_AT_declaration flag set.
6764 ///
6765 /// @param die the DIE to consider.
6766 //
6767 /// @return true if a DW_AT_declaration is present, false otherwise.
6768 static bool
6769 die_is_declaration_only(const Dwarf_Die* die)
6770 {return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6771 
6772 /// Tests whether a given DIE is artificial.
6773 ///
6774 /// @param die the test to test for.
6775 ///
6776 /// @return true if the DIE is artificial, false otherwise.
6777 static bool
6778 die_is_artificial(Dwarf_Die* die)
6779 {
6780  bool is_artificial;
6781  return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6782 }
6783 
6784 ///@return true if a tag represents a type, false otherwise.
6785 ///
6786 ///@param tag the tag to consider.
6787 static bool
6788 is_type_tag(unsigned tag)
6789 {
6790  bool result = false;
6791 
6792  switch (tag)
6793  {
6794  case DW_TAG_array_type:
6795  case DW_TAG_class_type:
6796  case DW_TAG_enumeration_type:
6797  case DW_TAG_pointer_type:
6798  case DW_TAG_reference_type:
6799  case DW_TAG_string_type:
6800  case DW_TAG_structure_type:
6801  case DW_TAG_subroutine_type:
6802  case DW_TAG_typedef:
6803  case DW_TAG_union_type:
6804  case DW_TAG_ptr_to_member_type:
6805  case DW_TAG_set_type:
6806  case DW_TAG_subrange_type:
6807  case DW_TAG_base_type:
6808  case DW_TAG_const_type:
6809  case DW_TAG_file_type:
6810  case DW_TAG_packed_type:
6811  case DW_TAG_thrown_type:
6812  case DW_TAG_volatile_type:
6813  case DW_TAG_restrict_type:
6814  case DW_TAG_interface_type:
6815  case DW_TAG_unspecified_type:
6816  case DW_TAG_shared_type:
6817  case DW_TAG_rvalue_reference_type:
6818  case DW_TAG_coarray_type:
6819  case DW_TAG_atomic_type:
6820  case DW_TAG_immutable_type:
6821  result = true;
6822  break;
6823 
6824  default:
6825  result = false;
6826  break;
6827  }
6828 
6829  return result;
6830 }
6831 
6832 /// Test if a given DIE is a type whose canonical type is to be
6833 /// propagated during DIE canonicalization
6834 ///
6835 /// This is a sub-routine of compare_dies.
6836 ///
6837 /// @param tag the tag of the DIE to consider.
6838 ///
6839 /// @return true iff the DIE of tag @p tag is can see its canonical
6840 /// type be propagated during the type comparison that happens during
6841 /// DIE canonicalization.
6842 static bool
6843 is_canon_type_to_be_propagated_tag(unsigned tag)
6844 {
6845  bool result = false;
6846 
6847  switch (tag)
6848  {
6849  case DW_TAG_class_type:
6850  case DW_TAG_structure_type:
6851  case DW_TAG_union_type:
6852  case DW_TAG_subroutine_type:
6853  case DW_TAG_subprogram:
6854  result = true;
6855  break;
6856 
6857  default:
6858  result = false;
6859  break;
6860  }
6861 
6862  return result;
6863 }
6864 
6865 /// Test if a given kind of DIE ought to have its comparison result
6866 /// cached by compare_dies, so that subsequent invocations of
6867 /// compare_dies can be faster.
6868 ///
6869 /// @param tag the tag of the DIE to consider.
6870 ///
6871 /// @return true iff DIEs of the tag @p tag ought to have its
6872 /// comparison results cached.
6873 static bool
6874 type_comparison_result_to_be_cached(unsigned tag)
6875 {
6876  bool r = false;
6877  switch (tag)
6878  {
6879  case DW_TAG_class_type:
6880  case DW_TAG_structure_type:
6881  case DW_TAG_union_type:
6882  case DW_TAG_subroutine_type:
6883  case DW_TAG_subprogram:
6884  r = true;
6885  break;
6886 
6887  default:
6888  r = false;
6889  break;
6890  }
6891  return r;
6892 }
6893 
6894 /// Cache the result of comparing to type DIEs.
6895 ///
6896 /// @param rdr the context to consider.
6897 ///
6898 /// @param tag the tag of the DIEs to consider.
6899 ///
6900 /// @param p the offsets of the pair of DIEs being compared.
6901 ///
6902 /// @param result the comparison result to be cached.
6903 static bool
6904 maybe_cache_type_comparison_result(const reader& rdr,
6905  int tag,
6906  const offset_pair_type& p,
6907  comparison_result result)
6908 {
6909  if (!type_comparison_result_to_be_cached(tag)
6910  || (result != COMPARISON_RESULT_EQUAL
6911  && result != COMPARISON_RESULT_DIFFERENT))
6912  return false;
6913 
6914  rdr.die_comparison_results_[p] = result;
6915 
6916  return true;
6917 
6918 }
6919 
6920 /// Get the cached result of the comparison of a pair of DIEs.
6921 ///
6922 /// @param rdr the context to consider.
6923 ///
6924 /// @param tag the tag of the pair of DIEs to consider.
6925 ///
6926 /// @param p the offsets of the pair of DIEs to consider.
6927 ///
6928 /// @param result out parameter set to the cached result of the
6929 /// comparison of @p p if it has been found.
6930 ///
6931 /// @return true iff a cached result for the comparisonof @p has been
6932 /// found and set into @p result.
6933 static bool
6934 get_cached_type_comparison_result(const reader& rdr,
6935  const offset_pair_type& p,
6936  comparison_result& result)
6937 {
6938  auto i = rdr.die_comparison_results_.find(p);
6939  if (i != rdr.die_comparison_results_.end())
6940  {
6941  result = i->second;
6942  return true;
6943  }
6944  return false;
6945 }
6946 
6947 /// Get the cached result of the comparison of a pair of DIEs, if the
6948 /// kind of DIEs ought to have its comparison results cached.
6949 ///
6950 /// @param rdr the context to consider.
6951 ///
6952 /// @param tag the tag of the pair of DIEs to consider.
6953 ///
6954 /// @param p the offsets of the pair of DIEs to consider.
6955 ///
6956 /// @param result out parameter set to the cached result of the
6957 /// comparison of @p p if it has been found.
6958 ///
6959 /// @return true iff a cached result for the comparisonof @p has been
6960 /// found and set into @p result.
6961 static bool
6962 maybe_get_cached_type_comparison_result(const reader& rdr,
6963  int tag,
6964  const offset_pair_type& p,
6965  comparison_result& result)
6966 {
6967  if (type_comparison_result_to_be_cached(tag))
6968  {
6969  // Types of this kind might have their comparison result cached
6970  // when they are not canonicalized. So let's see if we have a
6971  // cached comparison result.
6972  if (get_cached_type_comparison_result(rdr, p, result))
6973  return true;
6974  }
6975  return false;
6976 }
6977 
6978 /// Test if a given DIE is to be canonicalized.
6979 ///
6980 /// @param die the DIE to consider.
6981 ///
6982 /// @return true iff @p die is to be canonicalized.
6983 static bool
6984 is_type_die_to_be_canonicalized(const Dwarf_Die *die)
6985 {
6986  bool result = false;
6987  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6988 
6989  if (!is_type_tag(tag))
6990  return false;
6991 
6992  switch (tag)
6993  {
6994  case DW_TAG_class_type:
6995  case DW_TAG_structure_type:
6996  case DW_TAG_union_type:
6997  result = !die_is_declaration_only(die);
6998  break;
6999 
7000  case DW_TAG_subroutine_type:
7001  case DW_TAG_subprogram:
7002  case DW_TAG_array_type:
7003  result = true;
7004 
7005  default:
7006  break;
7007  }
7008 
7009  return result;
7010 }
7011 
7012 /// Test if a DIE tag represents a declaration.
7013 ///
7014 /// @param tag the DWARF tag to consider.
7015 ///
7016 /// @return true iff @p tag is for a declaration.
7017 static bool
7018 is_decl_tag(unsigned tag)
7019 {
7020  switch (tag)
7021  {
7022  case DW_TAG_formal_parameter:
7023  case DW_TAG_imported_declaration:
7024  case DW_TAG_member:
7025  case DW_TAG_unspecified_parameters:
7026  case DW_TAG_subprogram:
7027  case DW_TAG_variable:
7028  case DW_TAG_namespace:
7029  case DW_TAG_GNU_template_template_param:
7030  case DW_TAG_GNU_template_parameter_pack:
7031  case DW_TAG_GNU_formal_parameter_pack:
7032  return true;
7033  }
7034  return false;
7035 }
7036 
7037 /// Test if a DIE represents a type DIE.
7038 ///
7039 /// @param die the DIE to consider.
7040 ///
7041 /// @return true if @p die represents a type, false otherwise.
7042 static bool
7043 die_is_type(const Dwarf_Die* die)
7044 {
7045  if (!die)
7046  return false;
7047  return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7048 }
7049 
7050 /// Test if a DIE represents a declaration.
7051 ///
7052 /// @param die the DIE to consider.
7053 ///
7054 /// @return true if @p die represents a decl, false otherwise.
7055 static bool
7056 die_is_decl(const Dwarf_Die* die)
7057 {
7058  if (!die)
7059  return false;
7060  return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7061 }
7062 
7063 /// Test if a DIE represents a namespace.
7064 ///
7065 /// @param die the DIE to consider.
7066 ///
7067 /// @return true if @p die represents a namespace, false otherwise.
7068 static bool
7069 die_is_namespace(const Dwarf_Die* die)
7070 {
7071  if (!die)
7072  return false;
7073  return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7074 }
7075 
7076 /// Test if a DIE has tag DW_TAG_unspecified_type.
7077 ///
7078 /// @param die the DIE to consider.
7079 ///
7080 /// @return true if @p die has tag DW_TAG_unspecified_type.
7081 static bool
7082 die_is_unspecified(Dwarf_Die* die)
7083 {
7084  if (!die)
7085  return false;
7086  return (dwarf_tag(die) == DW_TAG_unspecified_type);
7087 }
7088 
7089 /// Test if a DIE represents a void type.
7090 ///
7091 /// @param die the DIE to consider.
7092 ///
7093 /// @return true if @p die represents a void type, false otherwise.
7094 static bool
7095 die_is_void_type(Dwarf_Die* die)
7096 {
7097  if (!die || dwarf_tag(die) != DW_TAG_base_type)
7098  return false;
7099 
7100  string name = die_name(die);
7101  if (name == "void")
7102  return true;
7103 
7104  return false;
7105 }
7106 
7107 /// Test if a DIE represents a pointer type.
7108 ///
7109 /// @param die the die to consider.
7110 ///
7111 /// @return true iff @p die represents a pointer type.
7112 static bool
7113 die_is_pointer_type(const Dwarf_Die* die)
7114 {
7115  if (!die)
7116  return false;
7117 
7118  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7119  if (tag == DW_TAG_pointer_type)
7120  return true;
7121 
7122  return false;
7123 }
7124 
7125 /// Test if a DIE is for a pointer, reference or qualified type to
7126 /// anonymous class or struct.
7127 ///
7128 /// @param die the DIE to consider.
7129 ///
7130 /// @return true iff @p is for a pointer, reference or qualified type
7131 /// to anonymous class or struct.
7132 static bool
7133 pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7134 {
7135  if (!die_is_pointer_array_or_reference_type(die)
7136  && !die_is_qualified_type(die))
7137  return false;
7138 
7139  Dwarf_Die underlying_type_die;
7140  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7141  return false;
7142 
7143  if (!die_is_class_type(&underlying_type_die))
7144  return false;
7145 
7146  string name = die_name(&underlying_type_die);
7147 
7148  return name.empty();
7149 }
7150 
7151 /// Test if a DIE represents a reference type.
7152 ///
7153 /// @param die the die to consider.
7154 ///
7155 /// @return true iff @p die represents a reference type.
7156 static bool
7157 die_is_reference_type(const Dwarf_Die* die)
7158 {
7159  if (!die)
7160  return false;
7161 
7162  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7163  if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7164  return true;
7165 
7166  return false;
7167 }
7168 
7169 /// Test if a DIE represents an array type.
7170 ///
7171 /// @param die the die to consider.
7172 ///
7173 /// @return true iff @p die represents an array type.
7174 static bool
7175 die_is_array_type(const Dwarf_Die* die)
7176 {
7177  if (!die)
7178  return false;
7179 
7180  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7181  if (tag == DW_TAG_array_type)
7182  return true;
7183 
7184  return false;
7185 }
7186 
7187 /// Test if a DIE represents a pointer, reference or array type.
7188 ///
7189 /// @param die the die to consider.
7190 ///
7191 /// @return true iff @p die represents a pointer or reference type.
7192 static bool
7193 die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7194 {return (die_is_pointer_type(die)
7195  || die_is_reference_type(die)
7196  || die_is_array_type(die));}
7197 
7198 /// Test if a DIE represents a pointer or a reference type.
7199 ///
7200 /// @param die the die to consider.
7201 ///
7202 /// @return true iff @p die represents a pointer or reference type.
7203 static bool
7204 die_is_pointer_or_reference_type(const Dwarf_Die* die)
7205 {return (die_is_pointer_type(die) || die_is_reference_type(die));}
7206 
7207 /// Test if a DIE represents a pointer, a reference or a typedef type.
7208 ///
7209 /// @param die the die to consider.
7210 ///
7211 /// @return true iff @p die represents a pointer, a reference or a
7212 /// typedef type.
7213 static bool
7214 die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7215 {return (die_is_pointer_array_or_reference_type(die)
7216  || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7217 
7218 /// Test if a DIE represents a class type.
7219 ///
7220 /// @param die the die to consider.
7221 ///
7222 /// @return true iff @p die represents a class type.
7223 static bool
7224 die_is_class_type(const Dwarf_Die* die)
7225 {
7226  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7227 
7228  if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7229  return true;
7230 
7231  return false;
7232 }
7233 
7234 /// Test if a DIE is for a qualified type.
7235 ///
7236 /// @param die the DIE to consider.
7237 ///
7238 /// @return true iff @p die is for a qualified type.
7239 static bool
7240 die_is_qualified_type(const Dwarf_Die* die)
7241 {
7242  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7243  if (tag == DW_TAG_const_type
7244  || tag == DW_TAG_volatile_type
7245  || tag == DW_TAG_restrict_type)
7246  return true;
7247 
7248  return false;
7249 }
7250 
7251 /// Test if a DIE is for a function type.
7252 ///
7253 /// @param die the DIE to consider.
7254 ///
7255 /// @return true iff @p die is for a function type.
7256 static bool
7257 die_is_function_type(const Dwarf_Die *die)
7258 {
7259  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7260  if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7261  return true;
7262 
7263  return false;
7264 }
7265 
7266 /// Test if a DIE for a function pointer or member function has an
7267 /// DW_AT_object_pointer attribute.
7268 ///
7269 /// @param die the DIE to consider.
7270 ///
7271 /// @param object_pointer out parameter. It's set to the DIE for the
7272 /// object pointer iff the function returns true.
7273 ///
7274 /// @return true iff the DIE @p die has an object pointer. In that
7275 /// case, the parameter @p object_pointer is set to the DIE of that
7276 /// object pointer.
7277 static bool
7278 die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7279 {
7280  if (!die)
7281  return false;
7282 
7283  if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7284  return true;
7285 
7286  return false;
7287 }
7288 
7289 /// Test if a DIE has children DIEs.
7290 ///
7291 /// @param die the DIE to consider.
7292 ///
7293 /// @return true iff @p DIE has at least one child node.
7294 static bool
7295 die_has_children(const Dwarf_Die* die)
7296 {
7297  if (!die)
7298  return false;
7299 
7300  Dwarf_Die child;
7301  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7302  return true;
7303 
7304  return false;
7305 }
7306 
7307 /// When given the object pointer DIE of a function type or member
7308 /// function DIE, this function returns the "this" pointer that points
7309 /// to the associated class.
7310 ///
7311 /// @param die the DIE of the object pointer of the function or member
7312 /// function to consider.
7313 ///
7314 /// @param this_pointer_die out parameter. This is set to the DIE of
7315 /// the "this" pointer iff the function returns true.
7316 ///
7317 /// @return true iff the function found the "this" pointer from the
7318 /// object pointer DIE @p die. In that case, the parameter @p
7319 /// this_pointer_die is set to the DIE of that "this" pointer.
7320 static bool
7321 die_this_pointer_from_object_pointer(Dwarf_Die* die,
7322  Dwarf_Die& this_pointer_die)
7323 {
7324  ABG_ASSERT(die);
7325  ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7326 
7327  if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7328  return true;
7329 
7330  return false;
7331 }
7332 
7333 /// Test if a given "this" pointer that points to a particular class
7334 /// type is for a const class or not. If it's for a const class, then
7335 /// it means the function type or the member function associated to
7336 /// that "this" pointer is const.
7337 ///
7338 /// @param die the DIE of the "this" pointer to consider.
7339 ///
7340 /// @return true iff @p die points to a const class type.
7341 static bool
7342 die_this_pointer_is_const(Dwarf_Die* die)
7343 {
7344  ABG_ASSERT(die);
7345 
7346  if (dwarf_tag(die) == DW_TAG_pointer_type)
7347  {
7348  Dwarf_Die pointed_to_type_die;
7349  if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7350  if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7351  return true;
7352  }
7353 
7354  return false;
7355 }
7356 
7357 /// Test if an object pointer (referred-to via a DW_AT_object_pointer
7358 /// attribute) points to a const implicit class and so is for a const
7359 /// method or or a const member function type.
7360 ///
7361 /// @param die the DIE of the object pointer to consider.
7362 ///
7363 /// @return true iff the object pointer represented by @p die is for a
7364 /// a const method or const member function type.
7365 static bool
7366 die_object_pointer_is_for_const_method(Dwarf_Die* die)
7367 {
7368  ABG_ASSERT(die);
7369  ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7370 
7371  Dwarf_Die this_pointer_die;
7372  if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7373  if (die_this_pointer_is_const(&this_pointer_die))
7374  return true;
7375 
7376  return false;
7377 }
7378 
7379 /// Test if a DIE represents an entity that is at class scope.
7380 ///
7381 /// @param rdr the DWARF reader to use.
7382 ///
7383 /// @param die the DIE to consider.
7384 ///
7385 /// @param where_offset where we are logically at in the DIE stream.
7386 ///
7387 /// @param class_scope_die out parameter. Set to the DIE of the
7388 /// containing class iff @p die happens to be at class scope; that is,
7389 /// iff the function returns true.
7390 ///
7391 /// @return true iff @p die is at class scope. In that case, @p
7392 /// class_scope_die is set to the DIE of the class that contains @p
7393 /// die.
7394 static bool
7395 die_is_at_class_scope(const reader& rdr,
7396  const Dwarf_Die* die,
7397  size_t where_offset,
7398  Dwarf_Die& class_scope_die)
7399 {
7400  if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7401  return false;
7402 
7403  int tag = dwarf_tag(&class_scope_die);
7404 
7405  return (tag == DW_TAG_structure_type
7406  || tag == DW_TAG_class_type
7407  || tag == DW_TAG_union_type);
7408 }
7409 
7410 /// Return the leaf object under a pointer, reference or qualified
7411 /// type DIE.
7412 ///
7413 /// @param die the DIE of the type to consider.
7414 ///
7415 /// @param peeled_die out parameter. Set to the DIE of the leaf
7416 /// object iff the function actually peeled anything.
7417 ///
7418 /// @return true upon successful completion.
7419 static bool
7420 die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7421 {
7422  if (!die)
7423  return false;
7424 
7425  int tag = dwarf_tag(die);
7426 
7427  if (tag == DW_TAG_const_type
7428  || tag == DW_TAG_volatile_type
7429  || tag == DW_TAG_restrict_type
7430  || tag == DW_TAG_pointer_type
7431  || tag == DW_TAG_reference_type
7432  || tag == DW_TAG_rvalue_reference_type)
7433  {
7434  if (!die_die_attribute(die, DW_AT_type, peeled_die))
7435  return false;
7436  }
7437  else
7438  return false;
7439 
7440  memcpy(&peeled_die, die, sizeof(peeled_die));
7441 
7442  while (tag == DW_TAG_const_type
7443  || tag == DW_TAG_volatile_type
7444  || tag == DW_TAG_restrict_type
7445  || tag == DW_TAG_pointer_type
7446  || tag == DW_TAG_reference_type
7447  || tag == DW_TAG_rvalue_reference_type)
7448  {
7449  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7450  break;
7451  tag = dwarf_tag(&peeled_die);
7452  }
7453 
7454  return true;
7455 }
7456 
7457 /// Return the leaf object under a qualified type DIE.
7458 ///
7459 /// @param die the DIE of the type to consider.
7460 ///
7461 /// @param peeled_die out parameter. Set to the DIE of the leaf
7462 /// object iff the function actually peeled anything.
7463 ///
7464 /// @return true upon successful completion.
7465 static bool
7466 die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7467 {
7468  if (!die)
7469  return false;
7470 
7471  memcpy(&peeled_die, die, sizeof(peeled_die));
7472 
7473  int tag = dwarf_tag(&peeled_die);
7474 
7475  bool result = false;
7476  while (tag == DW_TAG_const_type
7477  || tag == DW_TAG_volatile_type
7478  || tag == DW_TAG_restrict_type)
7479  {
7480  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7481  break;
7482  tag = dwarf_tag(&peeled_die);
7483  result = true;
7484  }
7485 
7486  return result;
7487 }
7488 
7489 /// Return the leaf object under a typedef type DIE.
7490 ///
7491 /// @param die the DIE of the type to consider.
7492 ///
7493 /// @param peeled_die out parameter. Set to the DIE of the leaf
7494 /// object iff the function actually peeled anything.
7495 ///
7496 /// @return true upon successful completion.
7497 static bool
7498 die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7499 {
7500  if (!die)
7501  return false;
7502 
7503  int tag = dwarf_tag(die);
7504 
7505  memcpy(&peeled_die, die, sizeof(peeled_die));
7506 
7507  if (tag == DW_TAG_typedef)
7508  {
7509  if (!die_die_attribute(die, DW_AT_type, peeled_die))
7510  return false;
7511  }
7512  else
7513  return false;
7514 
7515  while (tag == DW_TAG_typedef)
7516  {
7517  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7518  break;
7519  tag = dwarf_tag(&peeled_die);
7520  }
7521 
7522  return true;
7523 
7524 }
7525 
7526 /// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7527 ///
7528 /// @param die the DIE to consider.
7529 ///
7530 /// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7531 /// iff the function returned true.
7532 ///
7533 /// @return true iff the function could peel @p die.
7534 static bool
7535 die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7536 {
7537  if (!die)
7538  return false;
7539 
7540  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7541 
7542  if (tag == DW_TAG_pointer_type
7543  || tag == DW_TAG_reference_type
7544  || tag == DW_TAG_rvalue_reference_type
7545  || tag == DW_TAG_typedef)
7546  {
7547  if (!die_die_attribute(die, DW_AT_type, peeled_die))
7548  return false;
7549  }
7550  else
7551  return false;
7552 
7553  while (tag == DW_TAG_pointer_type
7554  || tag == DW_TAG_reference_type
7555  || tag == DW_TAG_rvalue_reference_type
7556  || tag == DW_TAG_typedef)
7557  {
7558  if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7559  break;
7560  tag = dwarf_tag(&peeled_die);
7561  }
7562  return true;
7563 }
7564 
7565 /// Test if a DIE for a function type represents a method type.
7566 ///
7567 /// @param rdr the DWARF reader.
7568 ///
7569 /// @param die the DIE to consider.
7570 ///
7571 /// @param where_offset where we logically are in the stream of DIEs.
7572 ///
7573 /// @param object_pointer_die out parameter. This is set by the
7574 /// function to the DIE that refers to the formal function parameter
7575 /// which holds the implicit "this" pointer of the method. That die
7576 /// is called the object pointer DIE. This is set iff the function
7577 ///
7578 /// @param class_die out parameter. This is set by the function to
7579 /// the DIE that represents the class of the method type. This is set
7580 /// iff the function returns true.
7581 ///
7582 /// @param is_static out parameter. This is set to true by the
7583 /// function if @p die is a static method. This is set iff the
7584 /// function returns true.
7585 ///
7586 /// @return true iff @p die is a DIE for a method type.
7587 static bool
7588 die_function_type_is_method_type(const reader& rdr,
7589  const Dwarf_Die *die,
7590  size_t where_offset,
7591  Dwarf_Die& object_pointer_die,
7592  Dwarf_Die& class_die,
7593  bool& is_static)
7594 {
7595  if (!die)
7596  return false;
7597 
7598  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7599  ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7600 
7601  bool has_object_pointer = false;
7602  is_static = false;
7603  if (tag == DW_TAG_subprogram)
7604  {
7605  Dwarf_Die spec_or_origin_die;
7606  if (die_die_attribute(die, DW_AT_specification,
7607  spec_or_origin_die)
7608  || die_die_attribute(die, DW_AT_abstract_origin,
7609  spec_or_origin_die))
7610  {
7611  if (die_has_object_pointer(&spec_or_origin_die,
7612  object_pointer_die))
7613  has_object_pointer = true;
7614  else
7615  {
7616  if (die_is_at_class_scope(rdr, &spec_or_origin_die,
7617  where_offset, class_die))
7618  is_static = true;
7619  else
7620  return false;
7621  }
7622  }
7623  else
7624  {
7625  if (die_has_object_pointer(die, object_pointer_die))
7626  has_object_pointer = true;
7627  else
7628  {
7629  if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7630  is_static = true;
7631  else
7632  return false;
7633  }
7634  }
7635  }
7636  else
7637  {
7638  if (die_has_object_pointer(die, object_pointer_die))
7639  has_object_pointer = true;
7640  else
7641  return false;
7642  }
7643 
7644  if (!is_static)
7645  {
7646  ABG_ASSERT(has_object_pointer);
7647  // The object pointer die points to a DW_TAG_formal_parameter which
7648  // is the "this" parameter. The type of the "this" parameter is a
7649  // pointer. Let's get that pointer type.
7650  Dwarf_Die this_type_die;
7651  if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7652  return false;
7653 
7654  // So the class type is the type pointed to by the type of the "this"
7655  // parameter.
7656  if (!die_peel_qual_ptr(&this_type_die, class_die))
7657  return false;
7658 
7659  // And make we return a class type, rather than a typedef to a
7660  // class.
7661  die_peel_typedef(&class_die, class_die);
7662  }
7663 
7664  return true;
7665 }
7666 
7667 enum virtuality
7668 {
7669  VIRTUALITY_NOT_VIRTUAL,
7670  VIRTUALITY_VIRTUAL,
7671  VIRTUALITY_PURE_VIRTUAL
7672 };
7673 
7674 /// Get the virtual-ness of a given DIE, that is, the value of the
7675 /// DW_AT_virtuality attribute.
7676 ///
7677 /// @param die the DIE to read from.
7678 ///
7679 /// @param virt the resulting virtuality attribute. This is set iff
7680 /// the function returns true.
7681 ///
7682 /// @return true if the virtual-ness could be determined.
7683 static bool
7684 die_virtuality(const Dwarf_Die* die, virtuality& virt)
7685 {
7686  if (!die)
7687  return false;
7688 
7689  uint64_t v = 0;
7690  die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7691 
7692  if (v == DW_VIRTUALITY_virtual)
7693  virt = VIRTUALITY_VIRTUAL;
7694  else if (v == DW_VIRTUALITY_pure_virtual)
7695  virt = VIRTUALITY_PURE_VIRTUAL;
7696  else
7697  virt = VIRTUALITY_NOT_VIRTUAL;
7698 
7699  return true;
7700 }
7701 
7702 /// Test whether the DIE represent either a virtual base or function.
7703 ///
7704 /// @param die the DIE to consider.
7705 ///
7706 /// @return bool if the DIE represents a virtual base or function,
7707 /// false othersise.
7708 static bool
7709 die_is_virtual(const Dwarf_Die* die)
7710 {
7711  virtuality v;
7712  if (!die_virtuality(die, v))
7713  return false;
7714 
7715  return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7716 }
7717 
7718 /// Test if the DIE represents an entity that was declared inlined.
7719 ///
7720 /// @param die the DIE to test for.
7721 ///
7722 /// @return true if the DIE represents an entity that was declared
7723 /// inlined.
7724 static bool
7725 die_is_declared_inline(Dwarf_Die* die)
7726 {
7727  uint64_t inline_value = 0;
7728  if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7729  return false;
7730  return (inline_value == DW_INL_declared_inlined
7731  || inline_value == DW_INL_declared_not_inlined);
7732 }
7733 
7734 /// Compare two DWARF strings using the most accurate (and slowest)
7735 /// method possible.
7736 ///
7737 /// @param l the DIE that carries the first string to consider, as an
7738 /// attribute value.
7739 ///
7740 /// @param attr_name the name of the attribute which value is the
7741 /// string to compare.
7742 ///
7743 /// @return true iff the string carried by @p l equals the one carried
7744 /// by @p r.
7745 static bool
7746 slowly_compare_strings(const Dwarf_Die *l,
7747  const Dwarf_Die *r,
7748  unsigned attr_name)
7749 {
7750  const char *l_str = die_char_str_attribute(l, attr_name),
7751  *r_str = die_char_str_attribute(r, attr_name);
7752  if (!l_str && !r_str)
7753  return true;
7754  return l_str && r_str && !strcmp(l_str, r_str);
7755 }
7756 
7757 /// This function is a fast routine (optimization) to compare the
7758 /// values of two string attributes of two DIEs.
7759 ///
7760 /// @param l the first DIE to consider.
7761 ///
7762 /// @param r the second DIE to consider.
7763 ///
7764 /// @param attr_name the name of the attribute to compare, on the two
7765 /// DIEs above.
7766 ///
7767 /// @param result out parameter. This is set to the result of the
7768 /// comparison. If the value of attribute @p attr_name on DIE @p l
7769 /// equals the value of attribute @p attr_name on DIE @p r, then the
7770 /// the argument of this parameter is set to true. Otherwise, it's
7771 /// set to false. Note that the argument of this parameter is set iff
7772 /// the function returned true.
7773 ///
7774 /// @return true iff the comparison could be performed. There are
7775 /// cases in which the comparison cannot be performed. For instance,
7776 /// if one of the DIEs does not have the attribute @p attr_name. In
7777 /// any case, if this function returns true, then the parameter @p
7778 /// result is set to the result of the comparison.
7779 static bool
7780 compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7781  unsigned attr_name,
7782  bool &result)
7783 {
7784  Dwarf_Attribute l_attr, r_attr;
7785  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7786  || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7787  return false;
7788 
7789  ABG_ASSERT(l_attr.form == DW_FORM_strp
7790  || l_attr.form == DW_FORM_string
7791  || l_attr.form == DW_FORM_GNU_strp_alt
7792  || form_is_DW_FORM_strx(l_attr.form)
7793  || form_is_DW_FORM_line_strp(l_attr.form));
7794 
7795  ABG_ASSERT(r_attr.form == DW_FORM_strp
7796  || r_attr.form == DW_FORM_string
7797  || r_attr.form == DW_FORM_GNU_strp_alt
7798  || form_is_DW_FORM_strx(r_attr.form)
7799  || form_is_DW_FORM_line_strp(r_attr.form));
7800 
7801  if ((l_attr.form == DW_FORM_strp
7802  && r_attr.form == DW_FORM_strp)
7803  || (l_attr.form == DW_FORM_GNU_strp_alt
7804  && r_attr.form == DW_FORM_GNU_strp_alt)
7805  || (form_is_DW_FORM_strx(l_attr.form)
7806  && form_is_DW_FORM_strx(r_attr.form))
7807  || (form_is_DW_FORM_line_strp(l_attr.form)
7808  && form_is_DW_FORM_line_strp(r_attr.form)))
7809  {
7810  // So these string attributes are actually pointers into a
7811  // string table. The string table is most likely de-duplicated
7812  // so comparing the *values* of the pointers should be enough.
7813  //
7814  // This is the fast path.
7815  if (l_attr.valp == r_attr.valp)
7816  {
7817 #if WITH_DEBUG_TYPE_CANONICALIZATION
7818  ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
7819 #endif
7820  result = true;
7821  return true;
7822  }
7823  }
7824 
7825  // If we reached this point it means we couldn't use the fast path
7826  // because the string atttributes are strings that are "inline" in
7827  // the debug info section. Let's just compare them the slow and
7828  // obvious way.
7829  result = slowly_compare_strings(l, r, attr_name);
7830  return true;
7831 }
7832 
7833 /// Compare the file path of the compilation units (aka CUs)
7834 /// associated to two DIEs.
7835 ///
7836 /// If the DIEs are for pointers or typedefs, this function also
7837 /// compares the file paths of the CUs of the leaf DIEs (underlying
7838 /// DIEs of the pointer or the typedef).
7839 ///
7840 /// @param l the first type DIE to consider.
7841 ///
7842 /// @param r the second type DIE to consider.
7843 ///
7844 /// @return true iff the file paths of the DIEs of the two types are
7845 /// equal.
7846 static bool
7847 compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7848 {
7849  Dwarf_Die l_cu, r_cu;
7850  if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7851  ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7852  return false;
7853 
7854  bool compared =
7855  compare_dies_string_attribute_value(&l_cu, &r_cu,
7856  DW_AT_name,
7857  result);
7858  if (compared && result)
7859  {
7860  Dwarf_Die peeled_l, peeled_r;
7861  if (die_is_pointer_reference_or_typedef_type(l)
7862  && die_is_pointer_reference_or_typedef_type(r)
7863  && die_peel_pointer_and_typedef(l, peeled_l)
7864  && die_peel_pointer_and_typedef(r, peeled_r))
7865  {
7866  if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7867  ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7868  return false;
7869  compared =
7870  compare_dies_string_attribute_value(&l_cu, &r_cu,
7871  DW_AT_name,
7872  result);
7873  }
7874  }
7875 
7876  return compared;
7877 }
7878 
7879 // -----------------------------------
7880 // <location expression evaluation>
7881 // -----------------------------------
7882 
7883 /// Get the value of a given DIE attribute, knowing that it must be a
7884 /// location expression.
7885 ///
7886 /// @param die the DIE to read the attribute from.
7887 ///
7888 /// @param attr_name the name of the attribute to read the value for.
7889 ///
7890 /// @param expr the pointer to allocate and fill with the resulting
7891 /// array of operators + operands forming a dwarf expression. This is
7892 /// set iff the function returns true.
7893 ///
7894 /// @param expr_len the length of the resulting dwarf expression.
7895 /// This is set iff the function returns true.
7896 ///
7897 /// @return true if the attribute exists and has a non-empty dwarf expression
7898 /// as value. In that case the expr and expr_len arguments are set to the
7899 /// resulting dwarf expression.
7900 static bool
7901 die_location_expr(const Dwarf_Die* die,
7902  unsigned attr_name,
7903  Dwarf_Op** expr,
7904  size_t* expr_len)
7905 {
7906  if (!die)
7907  return false;
7908 
7909  Dwarf_Attribute attr;
7910  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7911  return false;
7912 
7913  size_t len = 0;
7914  bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7915 
7916  // Ignore location expressions where reading them succeeded but
7917  // their length is 0.
7918  result &= len > 0;
7919 
7920  if (result)
7921  *expr_len = len;
7922 
7923  return result;
7924 }
7925 
7926 /// If the current operation in the dwarf expression represents a push
7927 /// of a constant value onto the dwarf expr virtual machine (aka
7928 /// DEVM), perform the operation and update the DEVM.
7929 ///
7930 /// If the result of the operation is a constant, update the DEVM
7931 /// accumulator with its value. Otherwise, the DEVM accumulator is
7932 /// left with its previous value.
7933 ///
7934 /// @param ops the array of the dwarf expression operations to consider.
7935 ///
7936 /// @param ops_len the lengths of @p ops array above.
7937 ///
7938 /// @param index the index of the operation to interpret, in @p ops.
7939 ///
7940 /// @param next_index the index of the operation to interpret at the
7941 /// next step, after this function completed and returned. This is
7942 /// set an output parameter that is set iff the function returns true.
7943 ///
7944 /// @param ctxt the DEVM evaluation context.
7945 ///
7946 /// @return true if the current operation actually pushes a constant
7947 /// value onto the DEVM stack, false otherwise.
7948 static bool
7949 op_pushes_constant_value(Dwarf_Op* ops,
7950  size_t ops_len,
7951  size_t index,
7952  size_t& next_index,
7953  dwarf_expr_eval_context& ctxt)
7954 {
7955  ABG_ASSERT(index < ops_len);
7956 
7957  Dwarf_Op& op = ops[index];
7958  int64_t value = 0;
7959 
7960  switch (op.atom)
7961  {
7962  case DW_OP_addr:
7963  value = ops[index].number;
7964  break;
7965 
7966  case DW_OP_const1u:
7967  case DW_OP_const1s:
7968  case DW_OP_const2u:
7969  case DW_OP_const2s:
7970  case DW_OP_const4u:
7971  case DW_OP_const4s:
7972  case DW_OP_const8u:
7973  case DW_OP_const8s:
7974  case DW_OP_constu:
7975  case DW_OP_consts:
7976  value = ops[index].number;
7977  break;
7978 
7979  case DW_OP_lit0:
7980  value = 0;
7981  break;
7982  case DW_OP_lit1:
7983  value = 1;
7984  break;
7985  case DW_OP_lit2:
7986  value = 2;
7987  break;
7988  case DW_OP_lit3:
7989  value = 3;
7990  break;
7991  case DW_OP_lit4:
7992  value = 4;
7993  break;
7994  case DW_OP_lit5:
7995  value = 5;
7996  break;
7997  case DW_OP_lit6:
7998  value = 6;
7999  break;
8000  case DW_OP_lit7:
8001  value = 7;
8002  break;
8003  case DW_OP_lit8:
8004  value = 8;
8005  break;
8006  case DW_OP_lit9:
8007  value = 9;
8008  break;
8009  case DW_OP_lit10:
8010  value = 10;
8011  break;
8012  case DW_OP_lit11:
8013  value = 11;
8014  break;
8015  case DW_OP_lit12:
8016  value = 12;
8017  break;
8018  case DW_OP_lit13:
8019  value = 13;
8020  break;
8021  case DW_OP_lit14:
8022  value = 14;
8023  break;
8024  case DW_OP_lit15:
8025  value = 15;
8026  break;
8027  case DW_OP_lit16:
8028  value = 16;
8029  break;
8030  case DW_OP_lit17:
8031  value = 17;
8032  break;
8033  case DW_OP_lit18:
8034  value = 18;
8035  break;
8036  case DW_OP_lit19:
8037  value = 19;
8038  break;
8039  case DW_OP_lit20:
8040  value = 20;
8041  break;
8042  case DW_OP_lit21:
8043  value = 21;
8044  break;
8045  case DW_OP_lit22:
8046  value = 22;
8047  break;
8048  case DW_OP_lit23:
8049  value = 23;
8050  break;
8051  case DW_OP_lit24:
8052  value = 24;
8053  break;
8054  case DW_OP_lit25:
8055  value = 25;
8056  break;
8057  case DW_OP_lit26:
8058  value = 26;
8059  break;
8060  case DW_OP_lit27:
8061  value = 27;
8062  break;
8063  case DW_OP_lit28:
8064  value = 28;
8065  break;
8066  case DW_OP_lit29:
8067  value = 29;
8068  break;
8069  case DW_OP_lit30:
8070  value = 30;
8071  break;
8072  case DW_OP_lit31:
8073  value = 31;
8074  break;
8075 
8076  default:
8077  return false;
8078  }
8079 
8080  expr_result r(value);
8081  ctxt.push(r);
8082  ctxt.accum = r;
8083  next_index = index + 1;
8084 
8085  return true;
8086 }
8087 
8088 /// If the current operation in the dwarf expression represents a push
8089 /// of a non-constant value onto the dwarf expr virtual machine (aka
8090 /// DEVM), perform the operation and update the DEVM. A non-constant
8091 /// is namely a quantity for which we need inferior (a running program
8092 /// image) state to know the exact value.
8093 ///
8094 /// Upon successful completion, as the result of the operation is a
8095 /// non-constant the DEVM accumulator value is left to its state as of
8096 /// before the invocation of this function.
8097 ///
8098 /// @param ops the array of the dwarf expression operations to consider.
8099 ///
8100 /// @param ops_len the lengths of @p ops array above.
8101 ///
8102 /// @param index the index of the operation to interpret, in @p ops.
8103 ///
8104 /// @param next_index the index of the operation to interpret at the
8105 /// next step, after this function completed and returned. This is
8106 /// set an output parameter that is set iff the function returns true.
8107 ///
8108 /// @param ctxt the DEVM evaluation context.
8109 ///
8110 /// @return true if the current operation actually pushes a
8111 /// non-constant value onto the DEVM stack, false otherwise.
8112 static bool
8113 op_pushes_non_constant_value(Dwarf_Op* ops,
8114  size_t ops_len,
8115  size_t index,
8116  size_t& next_index,
8117  dwarf_expr_eval_context& ctxt)
8118 {
8119  ABG_ASSERT(index < ops_len);
8120  Dwarf_Op& op = ops[index];
8121 
8122  switch (op.atom)
8123  {
8124  case DW_OP_reg0:
8125  case DW_OP_reg1:
8126  case DW_OP_reg2:
8127  case DW_OP_reg3:
8128  case DW_OP_reg4:
8129  case DW_OP_reg5:
8130  case DW_OP_reg6:
8131  case DW_OP_reg7:
8132  case DW_OP_reg8:
8133  case DW_OP_reg9:
8134  case DW_OP_reg10:
8135  case DW_OP_reg11:
8136  case DW_OP_reg12:
8137  case DW_OP_reg13:
8138  case DW_OP_reg14:
8139  case DW_OP_reg15:
8140  case DW_OP_reg16:
8141  case DW_OP_reg17:
8142  case DW_OP_reg18:
8143  case DW_OP_reg19:
8144  case DW_OP_reg20:
8145  case DW_OP_reg21:
8146  case DW_OP_reg22:
8147  case DW_OP_reg23:
8148  case DW_OP_reg24:
8149  case DW_OP_reg25:
8150  case DW_OP_reg26:
8151  case DW_OP_reg27:
8152  case DW_OP_reg28:
8153  case DW_OP_reg29:
8154  case DW_OP_reg30:
8155  case DW_OP_reg31:
8156  next_index = index + 1;
8157  break;
8158 
8159  case DW_OP_breg0:
8160  case DW_OP_breg1:
8161  case DW_OP_breg2:
8162  case DW_OP_breg3:
8163  case DW_OP_breg4:
8164  case DW_OP_breg5:
8165  case DW_OP_breg6:
8166  case DW_OP_breg7:
8167  case DW_OP_breg8:
8168  case DW_OP_breg9:
8169  case DW_OP_breg10:
8170  case DW_OP_breg11:
8171  case DW_OP_breg12:
8172  case DW_OP_breg13:
8173  case DW_OP_breg14:
8174  case DW_OP_breg15:
8175  case DW_OP_breg16:
8176  case DW_OP_breg17:
8177  case DW_OP_breg18:
8178  case DW_OP_breg19:
8179  case DW_OP_breg20:
8180  case DW_OP_breg21:
8181  case DW_OP_breg22:
8182  case DW_OP_breg23:
8183  case DW_OP_breg24:
8184  case DW_OP_breg25:
8185  case DW_OP_breg26:
8186  case DW_OP_breg27:
8187  case DW_OP_breg28:
8188  case DW_OP_breg29:
8189  case DW_OP_breg30:
8190  case DW_OP_breg31:
8191  next_index = index + 1;
8192  break;
8193 
8194  case DW_OP_regx:
8195  next_index = index + 2;
8196  break;
8197 
8198  case DW_OP_fbreg:
8199  next_index = index + 1;
8200  break;
8201 
8202  case DW_OP_bregx:
8203  next_index = index + 1;
8204  break;
8205 
8206  case DW_OP_GNU_variable_value:
8207  next_index = index + 1;
8208  break;
8209 
8210  default:
8211  return false;
8212  }
8213 
8214  expr_result r(false);
8215  ctxt.push(r);
8216 
8217  return true;
8218 }
8219 
8220 /// If the current operation in the dwarf expression represents a
8221 /// manipulation of the stack of the DWARF Expression Virtual Machine
8222 /// (aka DEVM), this function performs the operation and updates the
8223 /// state of the DEVM. If the result of the operation represents a
8224 /// constant value, then the accumulator of the DEVM is set to that
8225 /// result's value, Otherwise, the DEVM accumulator is left with its
8226 /// previous value.
8227 ///
8228 /// @param expr the array of the dwarf expression operations to consider.
8229 ///
8230 /// @param expr_len the lengths of @p ops array above.
8231 ///
8232 /// @param index the index of the operation to interpret, in @p ops.
8233 ///
8234 /// @param next_index the index of the operation to interpret at the
8235 /// next step, after this function completed and returned. This is
8236 /// set an output parameter that is set iff the function returns true.
8237 ///
8238 /// @param ctxt the DEVM evaluation context.
8239 ///
8240 /// @return true if the current operation actually manipulates the
8241 /// DEVM stack, false otherwise.
8242 static bool
8243 op_manipulates_stack(Dwarf_Op* expr,
8244  size_t expr_len,
8245  size_t index,
8246  size_t& next_index,
8247  dwarf_expr_eval_context& ctxt)
8248 {
8249  Dwarf_Op& op = expr[index];
8250  expr_result v;
8251 
8252  switch (op.atom)
8253  {
8254  case DW_OP_dup:
8255  v = ctxt.stack.front();
8256  ctxt.push(v);
8257  break;
8258 
8259  case DW_OP_drop:
8260  v = ctxt.stack.front();
8261  ctxt.pop();
8262  break;
8263 
8264  case DW_OP_over:
8265  ABG_ASSERT(ctxt.stack.size() > 1);
8266  v = ctxt.stack[1];
8267  ctxt.push(v);
8268  break;
8269 
8270  case DW_OP_pick:
8271  ABG_ASSERT(index + 1 < expr_len);
8272  v = op.number;
8273  ctxt.push(v);
8274  break;
8275 
8276  case DW_OP_swap:
8277  ABG_ASSERT(ctxt.stack.size() > 1);
8278  v = ctxt.stack[1];
8279  ctxt.stack.erase(ctxt.stack.begin() + 1);
8280  ctxt.push(v);
8281  break;
8282 
8283  case DW_OP_rot:
8284  ABG_ASSERT(ctxt.stack.size() > 2);
8285  v = ctxt.stack[2];
8286  ctxt.stack.erase(ctxt.stack.begin() + 2);
8287  ctxt.push(v);
8288  break;
8289 
8290  case DW_OP_deref:
8291  case DW_OP_deref_size:
8292  ABG_ASSERT(ctxt.stack.size() > 0);
8293  ctxt.pop();
8294  v.is_const(false);
8295  ctxt.push(v);
8296  break;
8297 
8298  case DW_OP_xderef:
8299  case DW_OP_xderef_size:
8300  ABG_ASSERT(ctxt.stack.size() > 1);
8301  ctxt.pop();
8302  ctxt.pop();
8303  v.is_const(false);
8304  ctxt.push(v);
8305  break;
8306 
8307  case DW_OP_push_object_address:
8308  v.is_const(false);
8309  ctxt.push(v);
8310  break;
8311 
8312  case DW_OP_form_tls_address:
8313  case DW_OP_GNU_push_tls_address:
8314  ABG_ASSERT(ctxt.stack.size() > 0);
8315  v = ctxt.pop();
8316  if (op.atom == DW_OP_form_tls_address)
8317  v.is_const(false);
8318  ctxt.push(v);
8319  break;
8320 
8321  case DW_OP_call_frame_cfa:
8322  v.is_const(false);
8323  ctxt.push(v);
8324  break;
8325 
8326  default:
8327  return false;
8328  }
8329 
8330  if (v.is_const())
8331  ctxt.accum = v;
8332 
8333  if (op.atom == DW_OP_form_tls_address
8334  || op.atom == DW_OP_GNU_push_tls_address)
8335  ctxt.set_tls_address(true);
8336  else
8337  ctxt.set_tls_address(false);
8338 
8339  next_index = index + 1;
8340 
8341  return true;
8342 }
8343 
8344 /// If the current operation in the dwarf expression represents a push
8345 /// of an arithmetic or logic operation onto the dwarf expr virtual
8346 /// machine (aka DEVM), perform the operation and update the DEVM.
8347 ///
8348 /// If the result of the operation is a constant, update the DEVM
8349 /// accumulator with its value. Otherwise, the DEVM accumulator is
8350 /// left with its previous value.
8351 ///
8352 /// @param expr the array of the dwarf expression operations to consider.
8353 ///
8354 /// @param expr_len the lengths of @p expr array above.
8355 ///
8356 /// @param index the index of the operation to interpret, in @p expr.
8357 ///
8358 /// @param next_index the index of the operation to interpret at the
8359 /// next step, after this function completed and returned. This is
8360 /// set an output parameter that is set iff the function returns true.
8361 ///
8362 /// @param ctxt the DEVM evaluation context.
8363 ///
8364 /// @return true if the current operation actually represent an
8365 /// arithmetic or logic operation.
8366 static bool
8367 op_is_arith_logic(Dwarf_Op* expr,
8368  size_t expr_len,
8369  size_t index,
8370  size_t& next_index,
8371  dwarf_expr_eval_context& ctxt)
8372 {
8373  ABG_ASSERT(index < expr_len);
8374 
8375  Dwarf_Op& op = expr[index];
8376  expr_result val1, val2;
8377  bool result = false;
8378 
8379  switch (op.atom)
8380  {
8381  case DW_OP_abs:
8382  ABG_ASSERT(ctxt.stack.size() > 0);
8383  val1 = ctxt.pop();
8384  val1 = val1.abs();
8385  ctxt.push(val1);
8386  result = true;
8387  break;
8388 
8389  case DW_OP_and:
8390  ABG_ASSERT(ctxt.stack.size() > 1);
8391  val1 = ctxt.pop();
8392  val2 = ctxt.pop();
8393  ctxt.push(val1 & val2);
8394  break;
8395 
8396  case DW_OP_div:
8397  ABG_ASSERT(ctxt.stack.size() > 1);
8398  val1 = ctxt.pop();
8399  val2 = ctxt.pop();
8400  if (!val1.is_const())
8401  val1 = 1;
8402  ctxt.push(val2 / val1);
8403  result = true;
8404  break;
8405 
8406  case DW_OP_minus:
8407  ABG_ASSERT(ctxt.stack.size() > 1);
8408  val1 = ctxt.pop();
8409  val2 = ctxt.pop();
8410  ctxt.push(val2 - val1);
8411  result = true;
8412  break;
8413 
8414  case DW_OP_mod:
8415  ABG_ASSERT(ctxt.stack.size() > 1);
8416  val1 = ctxt.pop();
8417  val2 = ctxt.pop();
8418  ctxt.push(val2 % val1);
8419  result = true;
8420  break;
8421 
8422  case DW_OP_mul:
8423  ABG_ASSERT(ctxt.stack.size() > 1);
8424  val1 = ctxt.pop();
8425  val2 = ctxt.pop();
8426  ctxt.push(val2 * val1);
8427  result = true;
8428  break;
8429 
8430  case DW_OP_neg:
8431  ABG_ASSERT(ctxt.stack.size() > 0);
8432  val1 = ctxt.pop();
8433  ctxt.push(-val1);
8434  result = true;
8435  break;
8436 
8437  case DW_OP_not:
8438  ABG_ASSERT(ctxt.stack.size() > 0);
8439  val1 = ctxt.pop();
8440  ctxt.push(~val1);
8441  result = true;
8442  break;
8443 
8444  case DW_OP_or:
8445  ABG_ASSERT(ctxt.stack.size() > 1);
8446  val1 = ctxt.pop();
8447  val2 = ctxt.pop();
8448  ctxt.push(val1 | val2);
8449  result = true;
8450  break;
8451 
8452  case DW_OP_plus:
8453  ABG_ASSERT(ctxt.stack.size() > 1);
8454  val1 = ctxt.pop();
8455  val2 = ctxt.pop();
8456  ctxt.push(val2 + val1);
8457  result = true;
8458  break;
8459 
8460  case DW_OP_plus_uconst:
8461  ABG_ASSERT(ctxt.stack.size() > 0);
8462  val1 = ctxt.pop();
8463  val1 += op.number;
8464  ctxt.push(val1);
8465  result = true;
8466  break;
8467 
8468  case DW_OP_shl:
8469  ABG_ASSERT(ctxt.stack.size() > 1);
8470  val1 = ctxt.pop();
8471  val2 = ctxt.pop();
8472  ctxt.push(val2 << val1);
8473  result = true;
8474  break;
8475 
8476  case DW_OP_shr:
8477  case DW_OP_shra:
8478  ABG_ASSERT(ctxt.stack.size() > 1);
8479  val1 = ctxt.pop();
8480  val2 = ctxt.pop();
8481  ctxt.push(val2 >> val1);
8482  result = true;
8483  break;
8484 
8485  case DW_OP_xor:
8486  ABG_ASSERT(ctxt.stack.size() > 1);
8487  val1 = ctxt.pop();
8488  val2 = ctxt.pop();
8489  ctxt.push(val2 ^ val1);
8490  result = true;
8491  break;
8492 
8493  default:
8494  break;
8495  }
8496 
8497  if (result == true)
8498  {
8499  if (ctxt.stack.front().is_const())
8500  ctxt.accum = ctxt.stack.front();
8501 
8502  next_index = index + 1;
8503  }
8504  return result;;
8505 }
8506 
8507 /// If the current operation in the dwarf expression represents a push
8508 /// of a control flow operation onto the dwarf expr virtual machine
8509 /// (aka DEVM), perform the operation and update the DEVM.
8510 ///
8511 /// If the result of the operation is a constant, update the DEVM
8512 /// accumulator with its value. Otherwise, the DEVM accumulator is
8513 /// left with its previous value.
8514 ///
8515 /// @param expr the array of the dwarf expression operations to consider.
8516 ///
8517 /// @param expr_len the lengths of @p expr array above.
8518 ///
8519 /// @param index the index of the operation to interpret, in @p expr.
8520 ///
8521 /// @param next_index the index of the operation to interpret at the
8522 /// next step, after this function completed and returned. This is
8523 /// set an output parameter that is set iff the function returns true.
8524 ///
8525 /// @param ctxt the DEVM evaluation context.
8526 ///
8527 /// @return true if the current operation actually represents a
8528 /// control flow operation, false otherwise.
8529 static bool
8530 op_is_control_flow(Dwarf_Op* expr,
8531  size_t expr_len,
8532  size_t index,
8533  size_t& next_index,
8534  dwarf_expr_eval_context& ctxt)
8535 {
8536  ABG_ASSERT(index < expr_len);
8537 
8538  Dwarf_Op& op = expr[index];
8539  expr_result val1, val2;
8540 
8541  switch (op.atom)
8542  {
8543  case DW_OP_eq:
8544  case DW_OP_ge:
8545  case DW_OP_gt:
8546  case DW_OP_le:
8547  case DW_OP_lt:
8548  case DW_OP_ne:
8549  {
8550  bool value = true;
8551  val1 = ctxt.pop();
8552  val2 = ctxt.pop();
8553  if (op.atom == DW_OP_eq)
8554  value = val2 == val1;
8555  else if (op.atom == DW_OP_ge)
8556  value = val2 >= val1;
8557  else if (op.atom == DW_OP_gt)
8558  value = val2 > val1;
8559  else if (op.atom == DW_OP_le)
8560  value = val2 <= val1;
8561  else if (op.atom == DW_OP_lt)
8562  value = val2 < val1;
8563  else if (op.atom == DW_OP_ne)
8564  value = val2 != val1;
8565 
8566  val1 = value ? 1 : 0;
8567  ctxt.push(val1);
8568  }
8569  break;
8570 
8571  case DW_OP_skip:
8572  if (op.number > 0)
8573  index += op.number - 1;
8574  break;
8575 
8576  case DW_OP_bra:
8577  val1 = ctxt.pop();
8578  if (val1.const_value() != 0)
8579  index += val1.const_value() - 1;
8580  break;
8581 
8582  case DW_OP_call2:
8583  case DW_OP_call4:
8584  case DW_OP_call_ref:
8585  case DW_OP_nop:
8586  break;
8587 
8588  default:
8589  return false;
8590  }
8591 
8592  if (ctxt.stack.front().is_const())
8593  ctxt.accum = ctxt.stack.front();
8594 
8595  next_index = index + 1;
8596  return true;
8597 }
8598 
8599 /// This function quickly evaluates a DWARF expression that is a
8600 /// constant.
8601 ///
8602 /// This is a "fast path" function that quickly evaluates a DWARF
8603 /// expression that is only made of a DW_OP_plus_uconst operator.
8604 ///
8605 /// This is a sub-routine of die_member_offset.
8606 ///
8607 /// @param expr the DWARF expression to evaluate.
8608 ///
8609 /// @param expr_len the length of the expression @p expr.
8610 ///
8611 /// @param value out parameter. This is set to the result of the
8612 /// evaluation of @p expr, iff this function returns true.
8613 ///
8614 /// @return true iff the evaluation of @p expr went OK.
8615 static bool
8616 eval_quickly(Dwarf_Op* expr,
8617  uint64_t expr_len,
8618  int64_t& value)
8619 {
8620  if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8621  {
8622  value = expr[0].number;
8623  return true;
8624  }
8625  return false;
8626 }
8627 
8628 /// Evaluate the value of the last sub-expression that is a constant,
8629 /// inside a given DWARF expression.
8630 ///
8631 /// @param expr the DWARF expression to consider.
8632 ///
8633 /// @param expr_len the length of the expression to consider.
8634 ///
8635 /// @param value the resulting value of the last constant
8636 /// sub-expression of the DWARF expression. This is set iff the
8637 /// function returns true.
8638 ///
8639 /// @param is_tls_address out parameter. This is set to true iff
8640 /// the resulting value of the evaluation is a TLS (thread local
8641 /// storage) address.
8642 ///
8643 /// @param eval_ctxt the evaluation context to (re)use. Note that
8644 /// this function initializes this context before using it.
8645 ///
8646 /// @return true if the function could find a constant sub-expression
8647 /// to evaluate, false otherwise.
8648 static bool
8649 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8650  size_t expr_len,
8651  int64_t& value,
8652  bool& is_tls_address,
8653  dwarf_expr_eval_context &eval_ctxt)
8654 {
8655  // Reset the evaluation context before evaluating the constant sub
8656  // expression contained in the DWARF expression 'expr'.
8657  eval_ctxt.reset();
8658 
8659  size_t index = 0, next_index = 0;
8660  do
8661  {
8662  if (op_is_arith_logic(expr, expr_len, index,
8663  next_index, eval_ctxt)
8664  || op_pushes_constant_value(expr, expr_len, index,
8665  next_index, eval_ctxt)
8666  || op_manipulates_stack(expr, expr_len, index,
8667  next_index, eval_ctxt)
8668  || op_pushes_non_constant_value(expr, expr_len, index,
8669  next_index, eval_ctxt)
8670  || op_is_control_flow(expr, expr_len, index,
8671  next_index, eval_ctxt))
8672  ;
8673  else
8674  next_index = index + 1;
8675 
8676  ABG_ASSERT(next_index > index);
8677  index = next_index;
8678  } while (index < expr_len);
8679 
8680  is_tls_address = eval_ctxt.set_tls_address();
8681  if (eval_ctxt.accum.is_const())
8682  {
8683  value = eval_ctxt.accum;
8684  return true;
8685  }
8686  return false;
8687 }
8688 
8689 /// Evaluate the value of the last sub-expression that is a constant,
8690 /// inside a given DWARF expression.
8691 ///
8692 /// @param expr the DWARF expression to consider.
8693 ///
8694 /// @param expr_len the length of the expression to consider.
8695 ///
8696 /// @param value the resulting value of the last constant
8697 /// sub-expression of the DWARF expression. This is set iff the
8698 /// function returns true.
8699 ///
8700 /// @return true if the function could find a constant sub-expression
8701 /// to evaluate, false otherwise.
8702 static bool
8703 eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8704  size_t expr_len,
8705  int64_t& value,
8706  bool& is_tls_address)
8707 {
8708  dwarf_expr_eval_context eval_ctxt;
8709  return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8710  is_tls_address, eval_ctxt);
8711 }
8712 
8713 // -----------------------------------
8714 // </location expression evaluation>
8715 // -----------------------------------
8716 
8717 /// Convert a DW_AT_bit_offset attribute value into the same value as
8718 /// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8719 ///
8720 /// On big endian machines, the value of the DW_AT_bit_offset
8721 /// attribute + 8 * the value of the DW_AT_data_member_location
8722 /// attribute is the same as the value of the DW_AT_data_bit_offset
8723 /// attribute.
8724 ///
8725 /// On little endian machines however, the situation is different.
8726 /// The DW_AT_bit_offset value for a bit field is the number of bits
8727 /// to the left of the most significant bit of the bit field, within
8728 /// the integer value at DW_AT_data_member_location.
8729 ///
8730 /// The DW_AT_data_bit_offset offset value is the number of bits to
8731 /// the right of the least significant bit of the bit field, again
8732 /// relative to the containing integer value.
8733 ///
8734 /// In other words, DW_AT_data_bit_offset is what everybody would
8735 /// instinctively think of as being the "offset of the bit field". 8 *
8736 /// DW_AT_data_member_location + DW_AT_bit_offset however is very
8737 /// counter-intuitive on little endian machines.
8738 ///
8739 /// This function thus reads the value of a DW_AT_bit_offset property
8740 /// of a DIE and converts it into what the DW_AT_data_bit_offset would
8741 /// have been if it was present, ignoring the contribution of
8742 /// DW_AT_data_member_location.
8743 ///
8744 /// Note that DW_AT_bit_offset has been made obsolete starting from
8745 /// DWARF5 (for GCC; Clang still emits it).
8746 ///
8747 /// If you like coffee and it's not too late, now might be a good time
8748 /// to have a coffee break. Otherwise if it's late at night, you
8749 /// might want to consider an herbal tea break. Then come back to
8750 /// read this.
8751 ///
8752 ///
8753 /// In what follows, the bit fields are all contained within the first
8754 /// whole int of the struct, so DW_AT_data_member_location is 0.
8755 ///
8756 /// Okay, to have a better idea of what DW_AT_bit_offset and
8757 /// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8758 /// have bit fields data members defined as:
8759 ///
8760 /// struct S
8761 /// {
8762 /// int j:5;
8763 /// int k:6;
8764 /// int m:5;
8765 /// int n:8;
8766 /// };
8767 ///
8768 /// The below wonderful (at least!) ASCII art sketch describes the
8769 /// layout of the bitfields of 'struct S' on a little endian machine.
8770 /// You need to read the sketch from the bottom-up.
8771 ///
8772 /// So please scroll down to its bottom. Note how the 32 bits integer
8773 /// word containing the bit fields is laid out with its least
8774 /// significant bit starting on the right hand side, at index 0.
8775 ///
8776 /// Then slowly scroll up starting from there, and take the time to
8777 /// read each line and see how the bit fields are laid out and what
8778 /// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8779 /// the bit fields.
8780 ///
8781 /// DW_AT_bit_offset(n)
8782 /// < - - - - - - >
8783 /// | | n |
8784 /// ^ ^< - - - - >^
8785 /// DW_AT_data_bit_offset(n)
8786 /// < - - - - - - - - - - - - - - - >
8787 /// | |
8788 /// ^ ^
8789 /// DW_AT_bit_offset(m)
8790 /// <--------------------------------->
8791 /// | | m |
8792 /// ^ ^< - >^
8793 /// DW_AT_data_bit_offset(m)
8794 /// < - - - - - - - - - - >
8795 /// | |
8796 /// ^ ^
8797 /// DW_AT_bit_offset(k)
8798 /// <-------------------------------------------->
8799 /// | | k |
8800 /// ^ ^< - - >^
8801 /// DW_AT_data_bit_offset(k)
8802 /// < - - - - >
8803 /// | |
8804 /// ^ ^
8805 /// DW_AT_bit_offset(j)
8806 /// <-------------------------------------------------------->
8807 /// | |
8808 /// ^ ^
8809 /// n m k j
8810 /// < - - - - - - > < - - - > < - - - - > < - - - >
8811 ///
8812 /// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8813 /// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8814 /// 31 27 23 16 15 11 10 6 5 4 0
8815 ///
8816 /// So, the different bit fields all fit in one 32 bits word, assuming
8817 /// the bit fields are tightly packed.
8818 ///
8819 /// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8820 /// on this little endian machine and let's see how it relates to
8821 /// DW_AT_data_bit_offset of j.
8822 ///
8823 /// DW_AT_bit_offset(j) would be equal to the number of bits from the
8824 /// left of the 32 bits word (i.e from bit number 31) to the most
8825 /// significant bit of the j bit field (i.e, bit number 4). Thus:
8826 ///
8827 /// DW_AT_bit_offset(j) =
8828 /// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8829 ///
8830 /// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8831 /// 32 bits word (i.e, bit number 0) to the lest significant bit of
8832 /// the 'j' bit field (ie, bit number 0). Thus:
8833 ///
8834 /// DW_AT_data_bit_offset(j) = 0.
8835 ///
8836 /// More generally, we can notice that:
8837 ///
8838 /// sizeof_in_bits(int) =
8839 /// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8840 ///
8841 /// It follows that:
8842 ///
8843 /// DW_AT_data_bit_offset(j) =
8844 /// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8845 ///
8846 /// Thus:
8847 ///
8848 /// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8849 ///
8850 /// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8851 /// from the right hand side of the word. It is what we would
8852 /// intuitively think it is. DW_AT_bit_offset however is super
8853 /// counter-intuitive, pfff.
8854 ///
8855 /// Anyway, this general equation holds true for all bit fields.
8856 ///
8857 /// Similarly, it follows that:
8858 ///
8859 /// DW_AT_bit_offset(k) =
8860 /// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8861 ///
8862 /// Thus:
8863 /// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8864 ///
8865 ///
8866 /// Likewise:
8867 ///
8868 /// DW_AT_bit_offset(m) =
8869 /// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8870 ///
8871 ///
8872 /// Thus:
8873 /// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8874 ///
8875 /// And:
8876 ///
8877 ///
8878 /// Lastly:
8879 ///
8880 /// DW_AT_bit_offset(n) =
8881 /// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8882 ///
8883 /// Thus:
8884 /// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8885 ///
8886 /// Luckily, the body of the function is much smaller than this
8887 /// comment. Enjoy!
8888 ///
8889 /// @param die the DIE to consider.
8890 ///
8891 /// @param is_big_endian this is true iff the machine we are looking at
8892 /// is big endian.
8893 ///
8894 /// @param offset this is the output parameter into which the value of
8895 /// the DW_AT_bit_offset is put, converted as if it was the value of
8896 /// the DW_AT_data_bit_offset parameter, less the contribution of
8897 /// DW_AT_data_member_location. This parameter is set iff the
8898 /// function returns true.
8899 ///
8900 /// @return true if DW_AT_bit_offset was found on @p die.
8901 static bool
8902 read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8903  bool is_big_endian,
8904  uint64_t &offset)
8905 {
8906  uint64_t off = 0;
8907  if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8908  return false;
8909 
8910  if (is_big_endian)
8911  {
8912  offset = off;
8913  return true;
8914  }
8915 
8916  // Okay, we are looking at a little endian machine. We need to
8917  // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8918  // have been. To understand this, you really need to read the
8919  // preliminary comment of this function.
8920  uint64_t containing_anonymous_object_size = 0;
8921  ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8922  containing_anonymous_object_size));
8923  containing_anonymous_object_size *= 8;
8924 
8925  uint64_t bitfield_size = 0;
8926  ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8927  bitfield_size));
8928 
8929  // As noted in the the preliminary comment of this function if we
8930  // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8931  // its DW_AT_bit_offset value, the equation is:
8932  //
8933  // DW_AT_data_bit_offset(k) =
8934  // sizeof_in_bits(containing_anonymous_object_size)
8935  // - DW_AT_data_bit_offset(k)
8936  // - sizeof_in_bits(k)
8937  offset = containing_anonymous_object_size - off - bitfield_size;
8938 
8939  return true;
8940 }
8941 
8942 /// Get the value of the DW_AT_data_member_location of the given DIE
8943 /// attribute as an constant.
8944 ///
8945 /// @param die the DIE to read the attribute from.
8946 ///
8947 /// @param offset the attribute as a constant value. This is set iff
8948 /// the function returns true.
8949 ///
8950 /// @return true if the attribute exists and has a constant value. In
8951 /// that case the offset is set to the value.
8952 static bool
8953 die_constant_data_member_location(const Dwarf_Die *die,
8954  int64_t& offset)
8955 {
8956  if (!die)
8957  return false;
8958 
8959  Dwarf_Attribute attr;
8960  if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8961  DW_AT_data_member_location,
8962  &attr))
8963  return false;
8964 
8965  Dwarf_Word val;
8966  if (dwarf_formudata(&attr, &val) != 0)
8967  return false;
8968 
8969  offset = val;
8970  return true;
8971 }
8972 
8973 /// Get the offset of a struct/class member as represented by the
8974 /// value of the DW_AT_data_member_location attribute.
8975 ///
8976 /// There is a huge gotcha in here. The value of the
8977 /// DW_AT_data_member_location is not necessarily a constant that one
8978 /// would just read and be done with it. Rather, it can be a DWARF
8979 /// expression that one has to interpret. In general, the offset can
8980 /// be given by the DW_AT_data_bit_offset or by the
8981 /// DW_AT_data_member_location attribute and optionally the
8982 /// DW_AT_bit_offset attribute. The bit offset attributes are
8983 /// always simple constants, but the DW_AT_data_member_location
8984 /// attribute is a DWARF location expression.
8985 ///
8986 /// When it's the DW_AT_data_member_location that is present,
8987 /// there are three cases to possibly take into account:
8988 ///
8989 /// 1/ The offset in the vtable where the offset of a virtual base
8990 /// can be found, aka vptr offset. Given the address of a
8991 /// given object O, the vptr offset for B is given by the
8992 /// (DWARF) expression:
8993 ///
8994 /// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8995 ///
8996 /// where VIRTUAL_OFFSET is a constant value; In this case,
8997 /// this function returns the constant VIRTUAL_OFFSET, as this
8998 /// is enough to detect changes in a given virtual base
8999 /// relative to the other virtual bases.
9000 ///
9001 /// 2/ The offset of a regular data member. Given the address of
9002 /// a struct object named O, the memory location for a
9003 /// particular data member is given by the (DWARF) expression:
9004 ///
9005 /// address(O) + OFFSET
9006 ///
9007 /// where OFFSET is a constant. In this case, this function
9008 /// returns the OFFSET constant.
9009 ///
9010 /// 3/ The offset of a virtual member function in the virtual
9011 /// pointer. The DWARF expression is a constant that designates
9012 /// the offset of the function in the vtable. In this case this
9013 /// function returns that constant.
9014 ///
9015 /// @param rdr the DWARF reader to consider.
9016 ///
9017 /// @param die the DIE to read the information from.
9018 ///
9019 /// @param offset the resulting constant offset, in bits. This
9020 /// argument is set iff the function returns true.
9021 static bool
9022 die_member_offset(const reader& rdr,
9023  const Dwarf_Die* die,
9024  int64_t& offset)
9025 {
9026  Dwarf_Op* expr = NULL;
9027  size_t expr_len = 0;
9028  uint64_t bit_offset = 0;
9029 
9030  // First let's see if the DW_AT_data_bit_offset attribute is
9031  // present.
9032  if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
9033  {
9034  offset = bit_offset;
9035  return true;
9036  }
9037 
9038  // First try to read DW_AT_data_member_location as a plain constant.
9039  // We do this because the generic method using die_location_expr
9040  // might hit a bug in elfutils libdw dwarf_location_expression only
9041  // fixed in elfutils 0.184+. The bug only triggers if the attribute
9042  // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
9043  // handle all constants here because that is more consistent (and
9044  // slightly faster in the general case where the attribute isn't a
9045  // full DWARF expression).
9046  if (!die_constant_data_member_location(die, offset))
9047  {
9048  // Otherwise, let's see if the DW_AT_data_member_location
9049  // attribute and, optionally, the DW_AT_bit_offset attributes
9050  // are present.
9051  if (!die_location_expr(die, DW_AT_data_member_location,
9052  &expr, &expr_len))
9053  return false;
9054 
9055  // The DW_AT_data_member_location attribute is present. Let's
9056  // evaluate it and get its constant sub-expression and return
9057  // that one.
9058  if (!eval_quickly(expr, expr_len, offset))
9059  {
9060  bool is_tls_address = false;
9061  if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
9062  offset, is_tls_address,
9063  rdr.dwarf_expr_eval_ctxt()))
9064  return false;
9065  }
9066  }
9067  offset *= 8;
9068 
9069  // On little endian machines, we need to convert the
9070  // DW_AT_bit_offset attribute into a relative offset to 8 *
9071  // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
9072  // would be if it were used instead.
9073  //
9074  // In other words, before adding it to 8 *
9075  // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9076  // converted into a human-understandable form that represents the
9077  // offset of the bitfield data member it describes. For details
9078  // about the conversion, please read the extensive comments of
9079  // read_and_convert_DW_at_bit_offset.
9080  bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9081  if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9082  offset += bit_offset;
9083 
9084  return true;
9085 }
9086 
9087 /// Read the value of the DW_AT_location attribute from a DIE,
9088 /// evaluate the resulting DWARF expression and, if it's a constant
9089 /// expression, return it.
9090 ///
9091 /// @param die the DIE to consider.
9092 ///
9093 /// @param address the resulting constant address. This is set iff
9094 /// the function returns true.
9095 ///
9096 /// @return true iff the whole sequence of action described above
9097 /// could be completed normally.
9098 static bool
9099 die_location_address(Dwarf_Die* die,
9100  Dwarf_Addr& address,
9101  bool& is_tls_address)
9102 {
9103  Dwarf_Op* expr = NULL;
9104  size_t expr_len = 0;
9105 
9106  is_tls_address = false;
9107 
9108  if (!die)
9109  return false;
9110 
9111  Dwarf_Attribute attr;
9112  if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9113  return false;
9114 
9115  if (dwarf_getlocation(&attr, &expr, &expr_len))
9116  return false;
9117  // Ignore location expressions where reading them succeeded but
9118  // their length is 0.
9119  if (expr_len == 0)
9120  return false;
9121 
9122  Dwarf_Attribute result;
9123  if (!dwarf_getlocation_attr(&attr, expr, &result))
9124  // A location that has been interpreted as an address.
9125  return !dwarf_formaddr(&result, &address);
9126 
9127  // Just get the address out of the number field.
9128  address = expr->number;
9129  return true;
9130 }
9131 
9132 /// Return the index of a function in its virtual table. That is,
9133 /// return the value of the DW_AT_vtable_elem_location attribute.
9134 ///
9135 /// @param die the DIE of the function to consider.
9136 ///
9137 /// @param vindex the resulting index. This is set iff the function
9138 /// returns true.
9139 ///
9140 /// @return true if the DIE has a DW_AT_vtable_elem_location
9141 /// attribute.
9142 static bool
9143 die_virtual_function_index(Dwarf_Die* die,
9144  int64_t& vindex)
9145 {
9146  if (!die)
9147  return false;
9148 
9149  Dwarf_Op* expr = NULL;
9150  size_t expr_len = 0;
9151  if (!die_location_expr(die, DW_AT_vtable_elem_location,
9152  &expr, &expr_len))
9153  return false;
9154 
9155  int64_t i = 0;
9156  bool is_tls_addr = false;
9157  if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9158  return false;
9159 
9160  vindex = i;
9161  return true;
9162 }
9163 
9164 /// Test if a given DIE represents an anonymous type.
9165 ///
9166 /// Anonymous types we are interested in are classes, unions and
9167 /// enumerations.
9168 ///
9169 /// @param die the DIE to consider.
9170 ///
9171 /// @return true iff @p die represents an anonymous type.
9172 bool
9173 is_anonymous_type_die(Dwarf_Die *die)
9174 {
9175  int tag = dwarf_tag(die);
9176 
9177  if (tag == DW_TAG_class_type
9178  || tag == DW_TAG_structure_type
9179  || tag == DW_TAG_union_type
9180  || tag == DW_TAG_enumeration_type)
9181  return die_is_anonymous(die);
9182 
9183  return false;
9184 }
9185 
9186 /// Return the base of the internal name to represent an anonymous
9187 /// type.
9188 ///
9189 /// Typically, anonymous enums would be named
9190 /// __anonymous_enum__<number>, anonymous struct or classes would be
9191 /// named __anonymous_struct__<number> and anonymous unions would be
9192 /// named __anonymous_union__<number>. The first part of these
9193 /// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9194 /// the base name. This function returns that base name, depending on
9195 /// the kind of type DIE we are looking at.
9196 ///
9197 /// @param die the type DIE to look at. This function expects a type
9198 /// DIE with an empty DW_AT_name property value (anonymous).
9199 ///
9200 /// @return a string representing the base of the internal anonymous
9201 /// name.
9202 static string
9203 get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9204 {
9205  ABG_ASSERT(die_is_type(die));
9206  ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9207 
9208  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9209  string type_name;
9210  if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9212  else if (tag == DW_TAG_union_type)
9214  else if (tag == DW_TAG_enumeration_type)
9216 
9217  return type_name;
9218 }
9219 
9220 /// Build a full internal anonymous type name.
9221 ///
9222 /// @param base_name this is the base name as returned by the function
9223 /// @ref get_internal_anonymous_die_prefix_name.
9224 ///
9225 /// @param anonymous_type_index this is the index of the anonymous
9226 /// type in its scope. That is, if there are more than one anonymous
9227 /// types of a given kind in a scope, this index is what tells them
9228 /// appart, starting from 0.
9229 ///
9230 /// @return the built string, which is a concatenation of @p base_name
9231 /// and @p anonymous_type_index.
9232 static string
9233 build_internal_anonymous_die_name(const string &base_name,
9234  size_t anonymous_type_index)
9235 {
9236  string name = base_name;
9237  if (anonymous_type_index && !base_name.empty())
9238  {
9239  std::ostringstream o;
9240  o << base_name << anonymous_type_index;
9241  name = o.str();
9242  }
9243  return name;
9244 }
9245 
9246 
9247 /// Build a full internal anonymous type name.
9248 ///
9249 /// @param die the DIE representing the anonymous type to consider.
9250 ///
9251 /// @param anonymous_type_index the index of the anonymous type
9252 /// represented by @p DIE, in its scope. That is, if there are
9253 /// several different anonymous types of the same kind as @p die, this
9254 /// index is what tells them appart.
9255 ///
9256 /// @return the internal name of the anonymous type represented by @p
9257 /// DIE.
9258 static string
9259 get_internal_anonymous_die_name(Dwarf_Die *die,
9260  size_t anonymous_type_index)
9261 {
9262  string name = get_internal_anonymous_die_prefix_name(die);
9263  name = build_internal_anonymous_die_name(name, anonymous_type_index);
9264  return name;
9265 }
9266 
9267 // ------------------------------------
9268 // <DIE pretty printer>
9269 // ------------------------------------
9270 
9271 /// Compute the qualified name of a DIE that represents a type.
9272 ///
9273 /// For instance, if the DIE tag is DW_TAG_subprogram then this
9274 /// function computes the name of the function *type*.
9275 ///
9276 /// @param rdr the DWARF reader.
9277 ///
9278 /// @param die the DIE to consider.
9279 ///
9280 /// @param where_offset where in the are logically are in the DIE
9281 /// stream.
9282 ///
9283 /// @return a copy of the qualified name of the type.
9284 static string
9285 die_qualified_type_name(const reader& rdr,
9286  const Dwarf_Die* die,
9287  size_t where_offset)
9288 {
9289  if (!die)
9290  return "";
9291 
9292  int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9293  if (tag == DW_TAG_compile_unit
9294  || tag == DW_TAG_partial_unit
9295  || tag == DW_TAG_type_unit)
9296  return "";
9297 
9298  string name = die_name(die);
9299 
9300  Dwarf_Die scope_die;
9301  if (!get_scope_die(rdr, die, where_offset, scope_die))
9302  return "";
9303 
9304  string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
9305  bool colon_colon = die_is_type(die) || die_is_namespace(die);
9306  string separator = colon_colon ? "::" : ".";
9307 
9308  string repr;
9309 
9310  switch (tag)
9311  {
9312  case DW_TAG_unspecified_type:
9313  break;
9314 
9315  case DW_TAG_base_type:
9316  {
9317  abigail::ir::integral_type int_type;
9318  if (parse_integral_type(name, int_type))
9319  repr = int_type;
9320  else
9321  repr = name;
9322  }
9323  break;
9324 
9325  case DW_TAG_typedef:
9326  case DW_TAG_enumeration_type:
9327  case DW_TAG_structure_type:
9328  case DW_TAG_class_type:
9329  case DW_TAG_union_type:
9330  {
9331  if (name.empty())
9332  // TODO: handle cases where there are more than one
9333  // anonymous type of the same kind in the same scope. In
9334  // that case, their name must be built with the function
9335  // get_internal_anonymous_die_name or something of the same
9336  // kind.
9337  name = get_internal_anonymous_die_prefix_name(die);
9338 
9339  ABG_ASSERT(!name.empty());
9340  repr = parent_name.empty() ? name : parent_name + separator + name;
9341  }
9342  break;
9343 
9344  case DW_TAG_const_type:
9345  case DW_TAG_volatile_type:
9346  case DW_TAG_restrict_type:
9347  {
9348  Dwarf_Die underlying_type_die;
9349  bool has_underlying_type_die =
9350  die_die_attribute(die, DW_AT_type, underlying_type_die);
9351 
9352  if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9353  break;
9354 
9355  if (tag == DW_TAG_const_type)
9356  {
9357  if (has_underlying_type_die
9358  && die_is_reference_type(&underlying_type_die))
9359  // A reference is always const. So, to lower false
9360  // positive reports in diff computations, we consider a
9361  // const reference just as a reference. But we need to
9362  // keep the qualified-ness of the type. So we introduce
9363  // a 'no-op' qualifier here. Please remember that this
9364  // has to be kept in sync with what is done in
9365  // get_name_of_qualified_type. So if you change this
9366  // here, you have to change that code there too.
9367  repr = "";
9368  else if (!has_underlying_type_die
9369  || die_is_void_type(&underlying_type_die))
9370  {
9371  repr = "void";
9372  break;
9373  }
9374  else
9375  repr = "const";
9376  }
9377  else if (tag == DW_TAG_volatile_type)
9378  repr = "volatile";
9379  else if (tag == DW_TAG_restrict_type)
9380  repr = "restrict";
9381  else
9383 
9384  string underlying_type_repr;
9385  if (has_underlying_type_die)
9386  underlying_type_repr =
9387  die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9388  else
9389  underlying_type_repr = "void";
9390 
9391  if (underlying_type_repr.empty())
9392  repr.clear();
9393  else
9394  {
9395  if (has_underlying_type_die)
9396  {
9397  Dwarf_Die peeled;
9398  die_peel_qualified(&underlying_type_die, peeled);
9399  if (die_is_pointer_or_reference_type(&peeled))
9400  repr = underlying_type_repr + " " + repr;
9401  else
9402  repr += " " + underlying_type_repr;
9403  }
9404  else
9405  repr += " " + underlying_type_repr;
9406  }
9407  }
9408  break;
9409 
9410  case DW_TAG_pointer_type:
9411  case DW_TAG_reference_type:
9412  case DW_TAG_rvalue_reference_type:
9413  {
9414  Dwarf_Die pointed_to_type_die;
9415  if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9416  {
9417  if (tag == DW_TAG_pointer_type)
9418  repr = "void*";
9419  break;
9420  }
9421 
9422  if (die_is_unspecified(&pointed_to_type_die))
9423  break;
9424 
9425  string pointed_type_repr =
9426  die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9427 
9428  repr = pointed_type_repr;
9429  if (repr.empty())
9430  break;
9431 
9432  if (tag == DW_TAG_pointer_type)
9433  repr += "*";
9434  else if (tag == DW_TAG_reference_type)
9435  repr += "&";
9436  else if (tag == DW_TAG_rvalue_reference_type)
9437  repr += "&&";
9438  else
9440  }
9441  break;
9442 
9443  case DW_TAG_subrange_type:
9444  {
9445  // In Ada, this one can be generated on its own, that is, not
9446  // as a sub-type of an array. So we need to support it on its
9447  // own. Note that when it's emitted as the sub-type of an
9448  // array like in C and C++, this is handled differently, for
9449  // now. But we try to make this usable by other languages
9450  // that are not Ada, even if we modelled it after Ada.
9451 
9452  // So we build a subrange type for the sole purpose of using
9453  // the ::as_string() method of that type. So we don't add
9454  // that type to the current type tree being built.
9456  build_subrange_type(const_cast<reader&>(rdr),
9457  die, where_offset,
9458  /*associate_die_to_type=*/false);
9459  repr += s->as_string();
9460  break;
9461  }
9462 
9463  case DW_TAG_array_type:
9464  {
9465  Dwarf_Die element_type_die;
9466  if (!die_die_attribute(die, DW_AT_type, element_type_die))
9467  break;
9468  string element_type_name =
9469  die_qualified_type_name(rdr, &element_type_die, where_offset);
9470  if (element_type_name.empty())
9471  break;
9472 
9474  build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9475  die, subranges, where_offset,
9476  /*associate_type_to_die=*/false);
9477 
9478  repr = element_type_name;
9479  repr += array_type_def::subrange_type::vector_as_string(subranges);
9480  }
9481  break;
9482 
9483  case DW_TAG_subroutine_type:
9484  case DW_TAG_subprogram:
9485  {
9486  string return_type_name;
9487  string class_name;
9488  vector<string> parm_names;
9489  bool is_const = false;
9490  bool is_static = false;
9491 
9492  die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9493  /*pretty_print=*/true,
9494  return_type_name, class_name,
9495  parm_names, is_const,
9496  is_static);
9497  if (return_type_name.empty())
9498  return_type_name = "void";
9499 
9500  repr = return_type_name;
9501 
9502  if (!class_name.empty())
9503  {
9504  // This is a method, so print the class name.
9505  repr += " (" + class_name + "::*)";
9506  }
9507 
9508  // Now parameters.
9509  repr += " (";
9510  for (vector<string>::const_iterator i = parm_names.begin();
9511  i != parm_names.end();
9512  ++i)
9513  {
9514  if (i != parm_names.begin())
9515  repr += ", ";
9516  repr += *i;
9517  }
9518  repr += ")";
9519 
9520  }
9521  break;
9522 
9523  case DW_TAG_string_type:
9524  case DW_TAG_ptr_to_member_type:
9525  case DW_TAG_set_type:
9526  case DW_TAG_file_type:
9527  case DW_TAG_packed_type:
9528  case DW_TAG_thrown_type:
9529  case DW_TAG_interface_type:
9530  case DW_TAG_shared_type:
9531  break;
9532  }
9533 
9534  return repr;
9535 }
9536 
9537 /// Compute the qualified name of a decl represented by a given DIE.
9538 ///
9539 /// For instance, for a DIE of tag DW_TAG_subprogram this function
9540 /// computes the signature of the function *declaration*.
9541 ///
9542 /// @param rdr the DWARF reader.
9543 ///
9544 /// @param die the DIE to consider.
9545 ///
9546 /// @param where_offset where we are logically at in the DIE stream.
9547 ///
9548 /// @return a copy of the computed name.
9549 static string
9550 die_qualified_decl_name(const reader& rdr,
9551  const Dwarf_Die* die,
9552  size_t where_offset)
9553 {
9554  if (!die || !die_is_decl(die))
9555  return "";
9556 
9557  string name = die_name(die);
9558 
9559  Dwarf_Die scope_die;
9560  if (!get_scope_die(rdr, die, where_offset, scope_die))
9561  return "";
9562 
9563  string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9564  string separator = "::";
9565 
9566  string repr;
9567 
9568  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9569  switch (tag)
9570  {
9571  case DW_TAG_namespace:
9572  case DW_TAG_member:
9573  case DW_TAG_variable:
9574  repr = scope_name.empty() ? name : scope_name + separator + name;
9575  break;
9576  case DW_TAG_subprogram:
9577  repr = die_function_signature(rdr, die, where_offset);
9578  break;
9579 
9580  case DW_TAG_unspecified_parameters:
9581  repr = "...";
9582  break;
9583 
9584  case DW_TAG_formal_parameter:
9585  case DW_TAG_imported_declaration:
9586  case DW_TAG_GNU_template_template_param:
9587  case DW_TAG_GNU_template_parameter_pack:
9588  case DW_TAG_GNU_formal_parameter_pack:
9589  break;
9590  }
9591  return repr;
9592 }
9593 
9594 /// Compute the qualified name of the artifact represented by a given
9595 /// DIE.
9596 ///
9597 /// If the DIE represents a type, then the function computes the name
9598 /// of the type. Otherwise, if the DIE represents a decl then the
9599 /// function computes the name of the decl. Note that a DIE of tag
9600 /// DW_TAG_subprogram is going to be considered as a "type" -- just
9601 /// like if it was a DW_TAG_subroutine_type.
9602 ///
9603 /// @param rdr the DWARF reader.
9604 ///
9605 /// @param die the DIE to consider.
9606 ///
9607 /// @param where_offset where we are logically at in the DIE stream.
9608 ///
9609 /// @return a copy of the computed name.
9610 static string
9611 die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9612 {
9613  if (die_is_type(die))
9614  return die_qualified_type_name(rdr, die, where);
9615  else if (die_is_decl(die))
9616  return die_qualified_decl_name(rdr, die, where);
9617  return "";
9618 }
9619 
9620 /// Test if the qualified name of a given type should be empty.
9621 ///
9622 /// The reason why the name of a DIE with a given tag would be empty
9623 /// is that libabigail's internal representation doesn't yet support
9624 /// that tag; or if the DIE's qualified name is built from names of
9625 /// sub-types DIEs whose tags are not yet supported.
9626 ///
9627 /// @param rdr the DWARF reader.
9628 ///
9629 /// @param die the DIE to consider.
9630 ///
9631 /// @param where where we are logically at, in the DIE stream.
9632 ///
9633 /// @param qualified_name the qualified name of the DIE. This is set
9634 /// only iff the function returns false.
9635 ///
9636 /// @return true if the qualified name of the DIE is empty.
9637 static bool
9638 die_qualified_type_name_empty(const reader& rdr,
9639  const Dwarf_Die* die,
9640  size_t where, string &qualified_name)
9641 {
9642  if (!die)
9643  return true;
9644 
9645  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9646 
9647  string qname;
9648  if (tag == DW_TAG_typedef
9649  || tag == DW_TAG_pointer_type
9650  || tag == DW_TAG_reference_type
9651  || tag == DW_TAG_rvalue_reference_type
9652  || tag == DW_TAG_array_type
9653  || tag == DW_TAG_const_type
9654  || tag == DW_TAG_volatile_type
9655  || tag == DW_TAG_restrict_type)
9656  {
9657  Dwarf_Die underlying_type_die;
9658  if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9659  {
9660  string name =
9661  die_qualified_type_name(rdr, &underlying_type_die, where);
9662  if (name.empty())
9663  return true;
9664  }
9665  }
9666  else
9667  {
9668  string name = die_qualified_type_name(rdr, die, where);
9669  if (name.empty())
9670  return true;
9671  }
9672 
9673  qname = die_qualified_type_name(rdr, die, where);
9674  if (qname.empty())
9675  return true;
9676 
9677  qualified_name = qname;
9678  return false;
9679 }
9680 
9681 /// Given the DIE that represents a function type, compute the names
9682 /// of the following properties the function's type:
9683 ///
9684 /// - return type
9685 /// - enclosing class (if the function is a member function)
9686 /// - function parameter types
9687 ///
9688 /// When the function we are looking at is a member function, it also
9689 /// tells if it's const.
9690 ///
9691 /// @param rdr the DWARF reader.
9692 ///
9693 /// @param die the DIE of the function or function type we are looking
9694 /// at.
9695 ///
9696 /// @param where_offset where we are logically at in the DIE stream.
9697 ///
9698 /// @param pretty_print if set to yes, the type names are going to be
9699 /// pretty-printed names; otherwise, they are just qualified type
9700 /// names.
9701 ///
9702 /// @param return_type_name out parameter. This contains the name of
9703 /// the return type of the function.
9704 ///
9705 /// @param class_name out parameter. If the function is a member
9706 /// function, this contains the name of the enclosing class.
9707 ///
9708 /// @param parm_names out parameter. This vector is set to the names
9709 /// of the types of the parameters of the function.
9710 ///
9711 /// @param is_const out parameter. If the function is a member
9712 /// function, this is set to true iff the member function is const.
9713 ///
9714 /// @param is_static out parameter. If the function is a static
9715 /// member function, then this is set to true.
9716 static void
9717 die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9718  const Dwarf_Die* die,
9719  size_t where_offset,
9720  bool pretty_print,
9721  string &return_type_name,
9722  string &class_name,
9723  vector<string>& parm_names,
9724  bool& is_const,
9725  bool& is_static)
9726 {
9727  Dwarf_Die child;
9728  Dwarf_Die ret_type_die;
9729  if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9730  return_type_name = "void";
9731  else
9732  return_type_name =
9733  pretty_print
9734  ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9735  : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9736 
9737  if (return_type_name.empty())
9738  return_type_name = "void";
9739 
9740  Dwarf_Die object_pointer_die, class_die;
9741  bool is_method_type =
9742  die_function_type_is_method_type(rdr, die, where_offset,
9743  object_pointer_die,
9744  class_die, is_static);
9745 
9746  is_const = false;
9747  if (is_method_type)
9748  {
9749  class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9750 
9751  Dwarf_Die this_pointer_die;
9752  Dwarf_Die pointed_to_die;
9753  if (!is_static
9754  && die_die_attribute(&object_pointer_die, DW_AT_type,
9755  this_pointer_die))
9756  if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9757  if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9758  is_const = true;
9759 
9760  string fn_name = die_name(die);
9761  string non_qualified_class_name = die_name(&class_die);
9762  bool is_ctor = fn_name == non_qualified_class_name;
9763  bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9764 
9765  if (is_ctor || is_dtor)
9766  return_type_name.clear();
9767  }
9768 
9769  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9770  do
9771  {
9772  int child_tag = dwarf_tag(&child);
9773  if (child_tag == DW_TAG_formal_parameter)
9774  {
9775  Dwarf_Die parm_type_die;
9776  if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9777  continue;
9778  string qualified_name =
9779  pretty_print
9780  ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
9781  : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
9782 
9783  if (qualified_name.empty())
9784  continue;
9785  parm_names.push_back(qualified_name);
9786  }
9787  else if (child_tag == DW_TAG_unspecified_parameters)
9788  {
9789  // This is a variadic function parameter.
9790  parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
9791  // After a DW_TAG_unspecified_parameters tag, we shouldn't
9792  // keep reading for parameters. The
9793  // unspecified_parameters TAG should be the last parameter
9794  // that we record. For instance, if there are multiple
9795  // DW_TAG_unspecified_parameters DIEs then we should care
9796  // only for the first one.
9797  break;
9798  }
9799  }
9800  while (dwarf_siblingof(&child, &child) == 0);
9801 
9802  if (class_name.empty())
9803  {
9804  Dwarf_Die parent_die;
9805  if (get_parent_die(rdr, die, parent_die, where_offset))
9806  {
9807  if (die_is_class_type(&parent_die))
9808  class_name =
9809  rdr.get_die_qualified_type_name(&parent_die, where_offset);
9810  }
9811  }
9812 }
9813 
9814 /// This computes the signature of the a function declaration
9815 /// represented by a DIE.
9816 ///
9817 /// @param rdr the DWARF reader.
9818 ///
9819 /// @param fn_die the DIE of the function to consider.
9820 ///
9821 /// @param where_offset where we are logically at in the stream of
9822 /// DIEs.
9823 ///
9824 /// @return a copy of the computed function signature string.
9825 static string
9826 die_function_signature(const reader& rdr,
9827  const Dwarf_Die *fn_die,
9828  size_t where_offset)
9829 {
9830 
9832  bool has_lang = false;
9833  if ((has_lang = get_die_language(fn_die, lang)))
9834  {
9835  // In a binary originating from the C language, it's OK to use
9836  // the linkage name of the function as a key for the map which
9837  // is meant to reduce the number of DIE comparisons involved
9838  // during DIE canonicalization computation.
9839  if (is_c_language(lang))
9840  {
9841  string fn_name = die_linkage_name(fn_die);
9842  if (fn_name.empty())
9843  fn_name = die_name(fn_die);
9844  return fn_name;
9845  }
9846  }
9847 
9848  // TODO: When we can structurally compare DIEs originating from C++
9849  // as well, we can use the linkage name of functions in C++ too, to
9850  // reduce the number of comparisons involved during DIE
9851  // canonicalization.
9852 
9853  string return_type_name;
9854  Dwarf_Die ret_type_die;
9855  if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9856  return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
9857  where_offset);
9858 
9859  if (return_type_name.empty())
9860  return_type_name = "void";
9861 
9862  Dwarf_Die scope_die;
9863  string scope_name;
9864  if (get_scope_die(rdr, fn_die, where_offset, scope_die))
9865  scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
9866  string fn_name = die_name(fn_die);
9867  if (!scope_name.empty())
9868  fn_name = scope_name + "::" + fn_name;
9869 
9870  string class_name;
9871  vector<string> parm_names;
9872  bool is_const = false;
9873  bool is_static = false;
9874 
9875  die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
9876  /*pretty_print=*/false,
9877  return_type_name, class_name,
9878  parm_names, is_const, is_static);
9879 
9880  bool is_virtual = die_is_virtual(fn_die);
9881 
9882  string repr = class_name.empty() ? "function" : "method";
9883  if (is_virtual)
9884  repr += " virtual";
9885 
9886  if (!return_type_name.empty())
9887  repr += " " + return_type_name;
9888 
9889  repr += " " + fn_name;
9890 
9891  // Now parameters.
9892  repr += "(";
9893  bool some_parm_emitted = false;
9894  for (vector<string>::const_iterator i = parm_names.begin();
9895  i != parm_names.end();
9896  ++i)
9897  {
9898  if (i != parm_names.begin())
9899  {
9900  if (some_parm_emitted)
9901  repr += ", ";
9902  }
9903  else
9904  if (!is_static && !class_name.empty())
9905  // We are printing a non-static method name, skip the implicit "this"
9906  // parameter type.
9907  continue;
9908  repr += *i;
9909  some_parm_emitted = true;
9910  }
9911  repr += ")";
9912 
9913  if (is_const)
9914  {
9915  ABG_ASSERT(!class_name.empty());
9916  repr += " const";
9917  }
9918 
9919  return repr;
9920 }
9921 
9922 /// Return a pretty string representation of a type, for internal purposes.
9923 ///
9924 /// By internal purpose, we mean things like key-ing types for lookup
9925 /// purposes and so on.
9926 ///
9927 /// Note that this function is also used to pretty print functions.
9928 /// For functions, it prints the *type* of the function.
9929 ///
9930 /// @param rdr the context to use.
9931 ///
9932 /// @param the DIE of the type to pretty print.
9933 ///
9934 /// @param where_offset where we logically are placed when calling
9935 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
9936 /// entries.
9937 ///
9938 /// @return the resulting pretty representation.
9939 static string
9940 die_pretty_print_type(reader& rdr,
9941  const Dwarf_Die* die,
9942  size_t where_offset)
9943 {
9944  if (!die
9945  || (!die_is_type(die)
9946  && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9947  return "";
9948 
9949  string repr;
9950 
9951  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9952  switch (tag)
9953  {
9954  case DW_TAG_string_type:
9955  // For now, we won't try to go get the actual representation of
9956  // the string because this would make things more complicated;
9957  // for that we'd need to interpret some location expressions to
9958  // get the length of the string. And for dynamically allocated
9959  // strings, the result of the location expression evaluation
9960  // might not even be a constant. So at the moment I consider
9961  // this to be a lot of hassle for no great return. Until proven
9962  // otherwise, of course.
9963  repr = "string type";
9964 
9965  case DW_TAG_unspecified_type:
9966  case DW_TAG_ptr_to_member_type:
9967  break;
9968 
9969  case DW_TAG_namespace:
9970  repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
9971  break;
9972 
9973  case DW_TAG_base_type:
9974  repr = rdr.get_die_qualified_type_name(die, where_offset);
9975  break;
9976 
9977  case DW_TAG_typedef:
9978  {
9979  string qualified_name;
9980  if (!die_qualified_type_name_empty(rdr, die,
9981  where_offset,
9982  qualified_name))
9983  repr = "typedef " + qualified_name;
9984  }
9985  break;
9986 
9987  case DW_TAG_const_type:
9988  case DW_TAG_volatile_type:
9989  case DW_TAG_restrict_type:
9990  case DW_TAG_pointer_type:
9991  case DW_TAG_reference_type:
9992  case DW_TAG_rvalue_reference_type:
9993  repr = rdr.get_die_qualified_type_name(die, where_offset);
9994  break;
9995 
9996  case DW_TAG_enumeration_type:
9997  {
9998  string qualified_name =
9999  rdr.get_die_qualified_type_name(die, where_offset);
10000  repr = "enum " + qualified_name;
10001  }
10002  break;
10003 
10004  case DW_TAG_structure_type:
10005  case DW_TAG_class_type:
10006  {
10007  string qualified_name =
10008  rdr.get_die_qualified_type_name(die, where_offset);
10009  repr = "class " + qualified_name;
10010  }
10011  break;
10012 
10013  case DW_TAG_union_type:
10014  {
10015  string qualified_name =
10016  rdr.get_die_qualified_type_name(die, where_offset);
10017  repr = "union " + qualified_name;
10018  }
10019  break;
10020 
10021  case DW_TAG_array_type:
10022  {
10023  Dwarf_Die element_type_die;
10024  if (!die_die_attribute(die, DW_AT_type, element_type_die))
10025  break;
10026  string element_type_name =
10027  rdr.get_die_qualified_type_name(&element_type_die, where_offset);
10028  if (element_type_name.empty())
10029  break;
10030 
10032  build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
10033  /*associate_type_to_die=*/false);
10034 
10035  repr = element_type_name;
10036  repr += array_type_def::subrange_type::vector_as_string(subranges);
10037  }
10038  break;
10039 
10040  case DW_TAG_subrange_type:
10041  {
10042  // So this can be generated by Ada, on its own; that is, not
10043  // as a subtype of an array. In that case we need to handle
10044  // it properly.
10045 
10046  // For now, we consider that the pretty printed name of the
10047  // subrange type is its name. We might need something more
10048  // advance, should the needs of the users get more
10049  // complicated.
10050  repr += die_qualified_type_name(rdr, die, where_offset);
10051  }
10052  break;
10053 
10054  case DW_TAG_subroutine_type:
10055  case DW_TAG_subprogram:
10056  {
10057  string return_type_name;
10058  string class_name;
10059  vector<string> parm_names;
10060  bool is_const = false;
10061  bool is_static = false;
10062 
10063  die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10064  /*pretty_print=*/true,
10065  return_type_name, class_name,
10066  parm_names, is_const,
10067  is_static);
10068  if (class_name.empty())
10069  repr = "function type";
10070  else
10071  repr = "method type";
10072  repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
10073  }
10074  break;
10075 
10076  case DW_TAG_set_type:
10077  case DW_TAG_file_type:
10078  case DW_TAG_packed_type:
10079  case DW_TAG_thrown_type:
10080  case DW_TAG_interface_type:
10081  case DW_TAG_shared_type:
10083  }
10084 
10085  return repr;
10086 }
10087 
10088 /// Return a pretty string representation of a declaration, for
10089 /// internal purposes.
10090 ///
10091 /// By internal purpose, we mean things like key-ing declarations for
10092 /// lookup purposes and so on.
10093 ///
10094 /// Note that this function is also used to pretty print functions.
10095 /// For functions, it prints the signature of the function.
10096 ///
10097 /// @param rdr the context to use.
10098 ///
10099 /// @param the DIE of the declaration to pretty print.
10100 ///
10101 /// @param where_offset where we logically are placed when calling
10102 /// this. It's useful to handle inclusion of DW_TAG_compile_unit
10103 /// entries.
10104 ///
10105 /// @return the resulting pretty representation.
10106 static string
10107 die_pretty_print_decl(reader& rdr,
10108  const Dwarf_Die* die,
10109  size_t where_offset)
10110 {
10111  if (!die || !die_is_decl(die))
10112  return "";
10113 
10114  string repr;
10115 
10116  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10117  switch (tag)
10118  {
10119  case DW_TAG_namespace:
10120  repr = "namespace " + die_qualified_name(rdr, die, where_offset);
10121  break;
10122 
10123  case DW_TAG_member:
10124  case DW_TAG_variable:
10125  {
10126  string type_repr = "void";
10127  Dwarf_Die type_die;
10128  if (die_die_attribute(die, DW_AT_type, type_die))
10129  type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
10130  repr = die_qualified_name(rdr, die, where_offset);
10131  if (!repr.empty())
10132  repr = type_repr + " " + repr;
10133  }
10134  break;
10135 
10136  case DW_TAG_subprogram:
10137  repr = die_function_signature(rdr, die, where_offset);
10138  break;
10139 
10140  default:
10141  break;
10142  }
10143  return repr;
10144 }
10145 
10146 /// Compute the pretty printed representation of an artifact
10147 /// represented by a DIE.
10148 ///
10149 /// If the DIE is a type, compute the its pretty representation as a
10150 /// type; otherwise, if it's a declaration, compute its pretty
10151 /// representation as a declaration. Note for For instance, that a
10152 /// DW_TAG_subprogram DIE is going to be represented as a function
10153 /// *type*.
10154 ///
10155 /// @param rdr the DWARF reader.
10156 ///
10157 /// @param die the DIE to consider.
10158 ///
10159 /// @param where_offset we in the DIE stream we are logically at.
10160 ///
10161 /// @return a copy of the pretty printed artifact.
10162 static string
10163 die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
10164 {
10165  if (die_is_type(die))
10166  return die_pretty_print_type(rdr, die, where_offset);
10167  else if (die_is_decl(die))
10168  return die_pretty_print_decl(rdr, die, where_offset);
10169  return "";
10170 }
10171 
10172 // -----------------------------------
10173 // </die pretty printer>
10174 // -----------------------------------
10175 
10176 
10177 // ----------------------------------
10178 // <die comparison engine>
10179 // ---------------------------------
10180 
10181 /// Compares two decls DIEs
10182 ///
10183 /// This works only for DIEs emitted by the C language.
10184 ///
10185 /// This implementation doesn't yet support namespaces.
10186 ///
10187 /// This is a subroutine of compare_dies.
10188 ///
10189 /// @return true iff @p l equals @p r.
10190 static bool
10191 compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10192 {
10193  ABG_ASSERT(l && r);
10194 
10195  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10196  int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10197  if (l_tag != r_tag)
10198  return false;
10199 
10200  bool result = false;
10201 
10202  if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
10203  {
10204  // Fast path for functions and global variables.
10205  if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
10206  result)
10207  || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
10208  result))
10209  {
10210  if (!result)
10211  return false;
10212  }
10213 
10214  if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10215  result))
10216  {
10217  if (!result)
10218  return false;
10219  }
10220  return true;
10221  }
10222 
10223  // Fast path for types.
10224  if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10225  result))
10226  return result;
10227  return true;
10228 }
10229 
10230 /// Test if at least one of two ODR-relevant DIEs is decl-only.
10231 ///
10232 /// @param rdr the DWARF reader to consider.
10233 ///
10234 /// @param l the first type DIE to consider.
10235 ///
10236 /// @param r the second type DIE to consider.
10237 ///
10238 /// @return true iff either @p l or @p r is decl-only and both are
10239 /// ODR-relevant.
10240 static bool
10241 at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
10242  const Dwarf_Die *l,
10243  const Dwarf_Die *r)
10244 {
10245  if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
10246  return false;
10247 
10248  if ((die_is_declaration_only(l) && die_has_no_child(l))
10249  || (die_is_declaration_only(r) && die_has_no_child(r)))
10250  return true;
10251  return false;
10252 }
10253 
10254 /// Compares two type DIEs
10255 ///
10256 /// This is a subroutine of compare_dies.
10257 ///
10258 /// Note that this function doesn't look at the name of the DIEs.
10259 /// Naming is taken into account by the function compare_as_decl_dies.
10260 ///
10261 /// If the two DIEs are from a translation unit that is subject to the
10262 /// ONE Definition Rule, then the function considers that if one DIE
10263 /// is a declaration, then it's equivalent to the second. In that
10264 /// case, the sizes of the two DIEs are not compared. This is so that
10265 /// a declaration of a type compares equal to the definition of the
10266 /// type.
10267 ///
10268 /// @param rdr the DWARF reader to consider.
10269 ///
10270 /// @param l the left operand of the comparison operator.
10271 ///
10272 /// @param r the right operand of the comparison operator.
10273 ///
10274 /// @return true iff @p l equals @p r.
10275 static bool
10276 compare_as_type_dies(const reader& rdr,
10277  const Dwarf_Die *l,
10278  const Dwarf_Die *r)
10279 {
10280  ABG_ASSERT(l && r);
10281  ABG_ASSERT(die_is_type(l));
10282  ABG_ASSERT(die_is_type(r));
10283 
10284  if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10285  && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10286  && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10287  != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10288  // For now, we cannot compare DW_TAG_string_type because of its
10289  // string_length attribute that is a location descriptor that is
10290  // not necessarily a constant. So it's super hard to evaluate it
10291  // in a libabigail context. So for now, we just say that all
10292  // DW_TAG_string_type DIEs are different, by default.
10293  return false;
10294 
10295  if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10296  // A declaration of a type compares equal to the definition of the
10297  // type.
10298  return true;
10299 
10300  uint64_t l_size = 0, r_size = 0;
10301  die_size_in_bits(l, l_size);
10302  die_size_in_bits(r, r_size);
10303 
10304  return l_size == r_size;
10305 }
10306 
10307 /// Compare two DIEs as decls (looking as their names etc) and as
10308 /// types (looking at their size etc).
10309 ///
10310 /// @param rdr the DWARF reader to consider.
10311 ///
10312 /// @param l the first DIE to consider.
10313 ///
10314 /// @param r the second DIE to consider.
10315 ///
10316 /// @return TRUE iff @p l equals @p r as far as naming and size is
10317 /// concerned.
10318 static bool
10319 compare_as_decl_and_type_dies(const reader &rdr,
10320  const Dwarf_Die *l,
10321  const Dwarf_Die *r)
10322 {
10323  if (!compare_as_decl_dies(l, r)
10324  || !compare_as_type_dies(rdr, l, r))
10325  return false;
10326 
10327  return true;
10328 }
10329 
10330 /// Test if two DIEs representing function declarations have the same
10331 /// linkage name, and thus are considered equal if they are C or C++,
10332 /// because the two DIEs represent functions in the same binary.
10333 ///
10334 /// If the DIEs don't have a linkage name, the function compares their
10335 /// name. But in that case, the caller of the function must know that
10336 /// in C++ for instance, that doesn't imply that the two functions are
10337 /// equal.
10338 ///
10339 /// @param l the first function DIE to consider.
10340 ///
10341 /// @param r the second function DIE to consider.
10342 ///
10343 /// @return true iff the function represented by @p l have the same
10344 /// linkage name as the function represented by @p r.
10345 static bool
10346 fn_die_equal_by_linkage_name(const Dwarf_Die *l,
10347  const Dwarf_Die *r)
10348 {
10349  if (!!l != !!r)
10350  return false;
10351 
10352  if (!l)
10353  return false;
10354 
10355  int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10356  ABG_ASSERT(tag == DW_TAG_subprogram);
10357  tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10358  ABG_ASSERT(tag == DW_TAG_subprogram);
10359 
10360  string lname = die_name(l), rname = die_name(r);
10361  string llinkage_name = die_linkage_name(l),
10362  rlinkage_name = die_linkage_name(r);
10363 
10364  if (die_is_in_c_or_cplusplus(l)
10365  && die_is_in_c_or_cplusplus(r))
10366  {
10367  if (!llinkage_name.empty() && !rlinkage_name.empty())
10368  return llinkage_name == rlinkage_name;
10369  else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10370  return false;
10371  else
10372  return lname == rname;
10373  }
10374 
10375  return (!llinkage_name.empty()
10376  && !rlinkage_name.empty()
10377  && llinkage_name == rlinkage_name);
10378 }
10379 
10380 /// Compare two DIEs in the context of DIE canonicalization.
10381 ///
10382 /// If DIE canonicalization is on, the function compares the DIEs
10383 /// canonically and structurally. The two types of comparison should
10384 /// be equal, of course.
10385 ///
10386 /// @param rdr the DWARF reader.
10387 ///
10388 /// @param l_offset the offset of the first canonical DIE to compare.
10389 ///
10390 /// @param r_offset the offset of the second canonical DIE to compare.
10391 ///
10392 /// @param l_die_source the source of the DIE denoted by the offset @p
10393 /// l_offset.
10394 ///
10395 /// @param r_die_source the source of the DIE denoted by the offset @p
10396 /// r_offset.
10397 ///
10398 /// @param l_has_canonical_die_offset output parameter. Is set to
10399 /// true if @p l_offset has a canonical DIE.
10400 ///
10401 /// @param r_has_canonical_die_offset output parameter. Is set to
10402 /// true if @p r_offset has a canonical DIE.
10403 ///
10404 /// @param l_canonical_die_offset output parameter. If @p
10405 /// l_has_canonical_die_offset is set to true, then this parameter is
10406 /// set to the offset of the canonical DIE of the DIE designated by @p
10407 /// l_offset.
10408 static bool
10409 try_canonical_die_comparison(const reader& rdr,
10410  Dwarf_Off l_offset, Dwarf_Off r_offset,
10411  die_source l_die_source, die_source r_die_source,
10412  bool& l_has_canonical_die_offset,
10413  bool& r_has_canonical_die_offset,
10414  Dwarf_Off& l_canonical_die_offset,
10415  Dwarf_Off& r_canonical_die_offset,
10416  bool& result)
10417 {
10418 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10419  if (rdr.debug_die_canonicalization_is_on_
10420  && !rdr.use_canonical_die_comparison_)
10421  return false;
10422 #endif
10423 
10424 
10425  l_has_canonical_die_offset =
10426  (l_canonical_die_offset =
10427  rdr.get_canonical_die_offset(l_offset, l_die_source,
10428  /*die_as_type=*/true));
10429 
10430  r_has_canonical_die_offset =
10431  (r_canonical_die_offset =
10432  rdr.get_canonical_die_offset(r_offset, r_die_source,
10433  /*die_as_type=*/true));
10434 
10435  if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10436  {
10437  result = (l_canonical_die_offset == r_canonical_die_offset);
10438  return true;
10439  }
10440 
10441  return false;
10442 }
10443 
10444 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10445 /// This function is called whenever a DIE comparison fails.
10446 ///
10447 /// This function is intended for debugging purposes. The idea is for
10448 /// hackers to set a breakpoint on this function so that they can
10449 /// discover why exactly the comparison failed. They then can execute
10450 /// the program from compare_dies_during_canonicalization, for
10451 /// instance.
10452 ///
10453 /// @param @l the left-hand side of the DIE comparison.
10454 ///
10455 /// @param @r the right-hand side of the DIE comparison.
10456 static void
10457 notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10458 {
10459 }
10460 
10461 #define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10462  notify_die_comparison_failed(l, r)
10463 #else
10464 #define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10465 #endif
10466 
10467 /// A macro used to return from DIE comparison routines.
10468 ///
10469 /// If the return value is false, the macro invokes the
10470 /// notify_die_comparison_failed signalling function before returning.
10471 /// That way, hackers willing to learn more about why the comparison
10472 /// routine returned "false" can just set a breakpoint on
10473 /// notify_die_comparison_failed and execute the program from
10474 /// compare_dies_during_canonicalization, for instance.
10475 ///
10476 /// @param value the value to return from the DIE comparison routines.
10477 #define ABG_RETURN(value) \
10478  do \
10479  { \
10480  if ((value) == COMPARISON_RESULT_DIFFERENT) \
10481  { \
10482  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10483  } \
10484  return return_comparison_result(l, r, dies_being_compared, \
10485  value, aggregates_being_compared, \
10486  update_canonical_dies_on_the_fly); \
10487  } \
10488  while(false)
10489 
10490 /// A macro used to return the "false" boolean from DIE comparison
10491 /// routines.
10492 ///
10493 /// As the return value is false, the macro invokes the
10494 /// notify_die_comparison_failed signalling function before returning.
10495 ///
10496 /// @param value the value to return from the DIE comparison routines.
10497 #define ABG_RETURN_FALSE \
10498  do \
10499  { \
10500  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10501  return return_comparison_result(l, r, dies_being_compared, \
10502  COMPARISON_RESULT_DIFFERENT, \
10503  aggregates_being_compared, \
10504  update_canonical_dies_on_the_fly); \
10505  } while(false)
10506 
10507 /// A macro to set the 'result' variable to 'false'.
10508 ///
10509 /// The macro invokes the notify_die_comparison_failed function so
10510 /// that the hacker can set a debugging breakpoint on
10511 /// notify_die_comparison_failed to know where a DIE comparison failed
10512 /// during compare_dies_during_canonicalization for instance.
10513 ///
10514 /// @param result the 'result' variable to set.
10515 ///
10516 /// @param l the first DIE of the comparison operation.
10517 ///
10518 /// @param r the second DIE of the comparison operation.
10519 #define SET_RESULT_TO_FALSE(result, l , r) \
10520  do \
10521  { \
10522  result = COMPARISON_RESULT_DIFFERENT; \
10523  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10524  } while(false)
10525 
10526 /// A macro to set the 'result' variable to a given value.
10527 ///
10528 /// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10529 /// invokes the notify_die_comparison_failed function so that the
10530 /// hacker can set a debugging breakpoint on
10531 /// notify_die_comparison_failed to know where a DIE comparison failed
10532 /// during compare_dies_during_canonicalization for instance.
10533 ///
10534 /// @param result the 'result' variable to set.
10535 ///
10536 /// @param l the first DIE of the comparison operation.
10537 ///
10538 /// @param r the second DIE of the comparison operation.
10539 #define SET_RESULT_TO(result, value, l , r) \
10540  do \
10541  { \
10542  result = (value); \
10543  if (result == COMPARISON_RESULT_DIFFERENT) \
10544  { \
10545  NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10546  } \
10547  } while(false)
10548 
10549 #define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10550  do \
10551  { \
10552  if (aggregates_being_compared.contains(dies_being_compared)) \
10553  { \
10554  result = COMPARISON_RESULT_CYCLE_DETECTED; \
10555  aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10556  ABG_RETURN(result); \
10557  } \
10558  } \
10559  while(false)
10560 
10561 /// Get the next member sibling of a given class or union member DIE.
10562 ///
10563 /// @param die the DIE to consider.
10564 ///
10565 /// @param member out parameter. This is set to the next member
10566 /// sibling, iff the function returns TRUE.
10567 ///
10568 /// @return TRUE iff the function set @p member to the next member
10569 /// sibling DIE.
10570 static bool
10571 get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10572 {
10573  if (!die)
10574  return false;
10575 
10576  bool found_member = false;
10577  for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10578  member) == 0);
10579  found_member;
10580  found_member = (dwarf_siblingof(member, member) == 0))
10581  {
10582  int tag = dwarf_tag(member);
10583  if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10584  break;
10585  }
10586 
10587  return found_member;
10588 }
10589 
10590 /// Get the first child DIE of a class/struct/union DIE that is a
10591 /// member DIE.
10592 ///
10593 /// @param die the DIE to consider.
10594 ///
10595 /// @param child out parameter. This is set to the first child DIE of
10596 /// @p iff this function returns TRUE.
10597 ///
10598 /// @return TRUE iff @p child is set to the first child DIE of @p die
10599 /// that is a member DIE.
10600 static bool
10601 get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10602 {
10603  if (!die)
10604  return false;
10605 
10606  int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10607  ABG_ASSERT(tag == DW_TAG_structure_type
10608  || tag == DW_TAG_union_type
10609  || tag == DW_TAG_class_type);
10610 
10611  bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10612  child) == 0);
10613 
10614  if (!found_child)
10615  return false;
10616 
10617  tag = dwarf_tag(child);
10618 
10619  if (!(tag == DW_TAG_member
10620  || tag == DW_TAG_inheritance
10621  || tag == DW_TAG_subprogram))
10622  found_child = get_next_member_sibling_die(child, child);
10623 
10624  return found_child;
10625 }
10626 
10627 /// This is a sub-routine of return_comparison_result.
10628 ///
10629 /// Propagate the canonical type of a the right-hand-side DIE to the
10630 /// lef-hand-side DIE. This is a optimization that is done when the
10631 /// two DIEs compare equal.
10632 ///
10633 /// If the right-hand-side DIE is not canonicalized, the function
10634 /// performs its canonicalization.
10635 ///
10636 /// This optimization is performed only if
10637 /// is_canon_type_to_be_propagated_tag returns true.
10638 ///
10639 /// @param rdr the current context to consider.
10640 ///
10641 /// @param l the left-hand-side DIE of the comparison. It's going to
10642 /// receive the canonical type of the other DIE.
10643 ///
10644 /// @param r the right-hand-side DIE of the comparison. Its canonical
10645 /// type is propagated to @p l.
10646 static void
10647 maybe_propagate_canonical_type(const reader& rdr,
10648  const Dwarf_Die* l,
10649  const Dwarf_Die* r)
10650 {
10651  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10652  r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10653 
10654  if (l_tag != r_tag)
10655  return;
10656 
10657  if (is_canon_type_to_be_propagated_tag(l_tag))
10658  propagate_canonical_type(rdr, l, r);
10659 }
10660 
10661 /// Propagate the canonical type of a the right-hand-side DIE to the
10662 /// left-hand-side DIE. This is a optimization that is done when the
10663 /// two DIEs compare equal.
10664 ///
10665 /// If the right-hand-side DIE is not canonicalized, the function
10666 /// performs its canonicalization.
10667 ///
10668 /// @param rdr the current context to consider.
10669 ///
10670 /// @param l the left-hand-side DIE of the comparison. It's going to
10671 /// receive the canonical type of the other DIE.
10672 ///
10673 /// @param r the right-hand-side DIE of the comparison. Its canonical
10674 /// type is propagated to @p l.
10675 static void
10676 propagate_canonical_type(const reader& rdr,
10677  const Dwarf_Die* l,
10678  const Dwarf_Die* r)
10679 {
10680  ABG_ASSERT(l && r);
10681 
10682  // If 'l' has no canonical DIE and if 'r' has one, then propagage
10683  // the canonical DIE of 'r' to 'l'.
10684  //
10685  // In case 'r' has no canonical DIE, then compute it, and then
10686  // propagate that canonical DIE to 'r'.
10687  const die_source l_source = rdr.get_die_source(l);
10688  const die_source r_source = rdr.get_die_source(r);
10689 
10690  Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10691  Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10692  bool l_has_canonical_die_offset = false;
10693  bool r_has_canonical_die_offset = false;
10694  Dwarf_Off l_canonical_die_offset = 0;
10695  Dwarf_Off r_canonical_die_offset = 0;
10696 
10697  l_has_canonical_die_offset =
10698  (l_canonical_die_offset =
10699  rdr.get_canonical_die_offset(l_offset, l_source,
10700  /*die_as_type=*/true));
10701 
10702  r_has_canonical_die_offset =
10703  (r_canonical_die_offset =
10704  rdr.get_canonical_die_offset(r_offset, r_source,
10705  /*die_as_type=*/true));
10706 
10707 
10708  if (!l_has_canonical_die_offset
10709  && r_has_canonical_die_offset
10710  // A DIE can be equivalent only to another DIE of the same
10711  // source.
10712  && l_source == r_source)
10713  {
10714  ABG_ASSERT(r_canonical_die_offset);
10715  rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10716  /*die_as_type=*/true);
10717  offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10718  rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10719  rdr.canonical_propagated_count_++;
10720  }
10721 }
10722 
10723 /// This function does the book keeping of comparison pairs necessary
10724 /// to handle
10725 ///
10726 /// * the detection of cycles during the comparison of aggregate
10727 /// types, in conjuction with the macro
10728 /// RETURN_IF_COMPARISON_CYCLE_DETECTED
10729 ///
10730 /// * the handling of the canonical type propagation optimisation
10731 /// to speed-up type canonicalization.
10732 ///
10733 ///
10734 /// Note that this function is essentially a sub-routine of
10735 /// compare_dies.
10736 ///
10737 /// @param l the left-hand-side DIE being compared.
10738 ///
10739 /// @param r the right-hand-side DIE being compared.
10740 ///
10741 /// @param cur_dies the pair of die offsets of l and r. This is
10742 /// redundant as it can been computed from @p l and @p r. However,
10743 /// getting it as an argument is an optimization to avoid computing it
10744 /// over and over again, given how often this function is invoked from
10745 /// compare_dies.
10746 ///
10747 /// @param return the result of comparing @p l against @p r.
10748 ///
10749 /// @param comparison_stack the stack of pair of type DIEs being
10750 /// compared.
10751 ///
10752 /// @param do_propagate_canonical_type if true then the function
10753 /// performs canonical DIEs propagation, meaning that if @p l equals
10754 /// @p r and if @p r has a canonical type, then the canonical type of
10755 /// @p l is set to the canonical type of @p r.
10756 static comparison_result
10757 return_comparison_result(const Dwarf_Die* l,
10758  const Dwarf_Die* r,
10759  const offset_pair_type& cur_dies,
10760  comparison_result result,
10761  offset_pairs_stack_type& comparison_stack,
10762  bool do_propagate_canonical_type = true)
10763 {
10764  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10765 
10766  if (result == COMPARISON_RESULT_EQUAL)
10767  {
10768  // The result comparing the two types is "true", basically. So
10769  // let's propagate the canonical type of r onto l, so that we
10770  // don't need to compute the canonical type of r.
10771  if (do_propagate_canonical_type)
10772  {
10773  // Propagate canonical type.
10774  maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10775 
10776  // TODO: do we need to confirm any tentative canonical
10777  // propagation?
10778  }
10779  }
10780  else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10781  {
10782  // So upon detection of the comparison cycle, compare_dies
10783  // returned early with the comparison result
10784  // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
10785  // carry on with the comparison of all the OTHER sub-types of
10786  // the redundant type. If they all compare equal, then it means
10787  // the redundant type pair compared equal. Otherwise, it
10788  // compared different.
10789  //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
10790  // Let's fall through to let the end of this function set the
10791  // result to COMPARISON_RESULT_UNKNOWN;
10792  }
10793  else if (result == COMPARISON_RESULT_UNKNOWN)
10794  {
10795  // Here is an introductory comment describing what we are going
10796  // to do in this case where the result of the comparison of the
10797  // current pair of type is not "false", basically.
10798  //
10799  // This means that we don't yet know what the result of
10800  // comparing these two types is, because one of the sub-types of
10801  // the types being compared is "redundant", meaning it appears
10802  // more than once in the comparison stack, so if we were to
10803  // naively try to carry on with the comparison member-wise, we'd
10804  // end up with an endless loop, a.k.a "comparison cycle".
10805  //
10806  // If the current type pair is redundant then:
10807  //
10808  // * This is a redundant type that has just been fully
10809  // compared. In that case, all the types that depend on
10810  // this redundant type and that have been tentatively
10811  // canonical-type-propagated must see their canonical types
10812  // "confirmed". This means that this type is going to be
10813  // considered as not being redundant anymore, meaning all
10814  // the types that depend on it must be updated as not being
10815  // dependant on it anymore, and the type itsef must be
10816  // removed from the map of redundant types.
10817  //
10818  // After the type's canonical-type-propagation is confirmed,
10819  // the result of its comparison must also be changed into
10820  // COMPARISON_RESULT_EQUAL.
10821  //
10822  // After that, If the current type depends on a redundant type,
10823  // then propagate its canonical type AND track it as having its
10824  // type being canonical-type-propagated.
10825  //
10826  // If the current type is not redundant however, then it must be
10827  // dependant on a redundant type. If it's not dependant on a
10828  // redundant type, then it must be of those types which
10829  // comparisons are not tracked for cycle, probably because they
10830  // are not aggregates. Otherwise, ABORT to understand why. I
10831  // believe this should not happen. In any case, after that
10832  // safety check is passed, we just need to return at this point.
10833 
10834  if (comparison_stack.is_redundant(cur_dies)
10835  && comparison_stack.vect_.back() == cur_dies)
10836  {
10837  // We are in the case described above of a redundant type
10838  // that has been fully compared.
10839  maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10840  comparison_stack.confirm_canonical_propagated_type(cur_dies);
10841 
10842  result = COMPARISON_RESULT_EQUAL;
10843  }
10844  else if (is_canon_type_to_be_propagated_tag(l_tag)
10845  && comparison_stack.vect_.back() == cur_dies)
10846  {
10847  // The current type is not redundant. So, as described in
10848  // the introductory comment above, it must be dependant on a
10849  // redundant type.
10850  ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
10851  maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10852  // Then pass through.
10853  }
10854  }
10855  else if (result == COMPARISON_RESULT_DIFFERENT)
10856  {
10857  // Here is an introductory comment describing what we are going
10858  // to do in this case where the result of the comparison of the
10859  // current pair of type is "false", basically.
10860  //
10861  // If the type pair {l,r} is redundant then cancel the
10862  // canonical-type-propagation of all the dependant pairs that
10863  // depends on this redundant {l, r}. This means walk the types
10864  // that depends on {l, r} and cancel their
10865  // canonical-propagate-type, that means remove their canonical
10866  // types and mark them as not being canonically-propagated.
10867  // Also, erase their cached comparison results that was likely
10868  // set to COMPARISON_RESULT_UNKNOWN.
10869  //
10870  // Also, update the cached result for this pair, that was likely
10871  // to be COMPARISON_RESULT_UNKNOWN.
10872  if (comparison_stack.is_redundant(cur_dies)
10873  && comparison_stack.vect_.back() == cur_dies)
10874  comparison_stack.cancel_canonical_propagated_type(cur_dies);
10875  }
10876  else
10877  {
10878  // We should never reach here.
10880  }
10881 
10882  if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10883  result = COMPARISON_RESULT_UNKNOWN;
10884  else if (is_canon_type_to_be_propagated_tag(l_tag)
10885  && !comparison_stack.vect_.empty()
10886  && comparison_stack.vect_.back() == cur_dies)
10887  //Finally pop the pair types being compared from comparison_stack
10888  //iff {l,r} is on the top of the stack. If it's not, then it means
10889  //we are looking at a type that was detected as a being redundant
10890  //and thus hasn't been pushed to the stack yet gain.
10891  comparison_stack.erase(cur_dies);
10892 
10893  maybe_cache_type_comparison_result(comparison_stack.rdr_,
10894  l_tag, cur_dies, result);
10895 
10896  return result;
10897 }
10898 
10899 /// Compare two DIEs emitted by a C compiler.
10900 ///
10901 /// @param rdr the DWARF reader used to load the DWARF information.
10902 ///
10903 /// @param l the left-hand-side argument of this comparison operator.
10904 ///
10905 /// @param r the righ-hand-side argument of this comparison operator.
10906 ///
10907 /// @param aggregates_being_compared this holds the names of the set
10908 /// of aggregates being compared. It's used by the comparison
10909 /// function to avoid recursing infinitely when faced with types
10910 /// referencing themselves through pointers or references. By
10911 /// default, just pass an empty instance of @ref istring_set_type to
10912 /// it.
10913 ///
10914 /// @param update_canonical_dies_on_the_fly if true, when two
10915 /// sub-types compare equal (during the comparison of @p l and @p r)
10916 /// update their canonical type. That way, two types of the same name
10917 /// are structurally compared to each other only once. So the
10918 /// non-linear structural comparison of two types of the same name
10919 /// only happen once.
10920 ///
10921 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
10922 static comparison_result
10923 compare_dies(const reader& rdr,
10924  const Dwarf_Die *l, const Dwarf_Die *r,
10925  offset_pairs_stack_type& aggregates_being_compared,
10926  bool update_canonical_dies_on_the_fly)
10927 {
10928  ABG_ASSERT(l);
10929  ABG_ASSERT(r);
10930 
10931  const die_source l_die_source = rdr.get_die_source(l);
10932  const die_source r_die_source = rdr.get_die_source(r);
10933 
10934  offset_type l_offset =
10935  {
10936  l_die_source,
10937  dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10938  };
10939 
10940  offset_type r_offset =
10941  {
10942  r_die_source,
10943  dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
10944  };
10945 
10946  offset_pair_type dies_being_compared(l_offset, r_offset);
10947 
10948  int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10949  r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10950 
10951  if (l_tag != r_tag)
10953 
10954  if (l_offset == r_offset)
10955  return COMPARISON_RESULT_EQUAL;
10956 
10957  if (rdr.leverage_dwarf_factorization()
10958  && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
10959  && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
10960  if (l_offset != r_offset)
10961  return COMPARISON_RESULT_DIFFERENT;
10962 
10963  comparison_result result = COMPARISON_RESULT_EQUAL;
10964  if (maybe_get_cached_type_comparison_result(rdr, l_tag,
10965  dies_being_compared,
10966  result))
10967  return result;
10968 
10969  Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10970  bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
10971 
10972  // If 'l' and 'r' already have canonical DIEs, then just compare the
10973  // offsets of their canonical DIEs.
10974  if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
10975  {
10976  bool canonical_compare_result = false;
10977  if (try_canonical_die_comparison(rdr, l_offset, r_offset,
10978  l_die_source, r_die_source,
10979  l_has_canonical_die_offset,
10980  r_has_canonical_die_offset,
10981  l_canonical_die_offset,
10982  r_canonical_die_offset,
10983  canonical_compare_result))
10984  {
10985  comparison_result result;
10986  SET_RESULT_TO(result,
10987  (canonical_compare_result
10988  ? COMPARISON_RESULT_EQUAL
10989  : COMPARISON_RESULT_DIFFERENT),
10990  l, r);
10991  return result;
10992  }
10993  }
10994 
10995 
10996 
10997  switch (l_tag)
10998  {
10999  case DW_TAG_base_type:
11000  case DW_TAG_string_type:
11001  case DW_TAG_unspecified_type:
11002  if (!compare_as_decl_and_type_dies(rdr, l, r))
11003  SET_RESULT_TO_FALSE(result, l, r);
11004  break;
11005 
11006  case DW_TAG_typedef:
11007  case DW_TAG_pointer_type:
11008  case DW_TAG_reference_type:
11009  case DW_TAG_rvalue_reference_type:
11010  case DW_TAG_const_type:
11011  case DW_TAG_volatile_type:
11012  case DW_TAG_restrict_type:
11013  {
11014  if (!compare_as_type_dies(rdr, l, r))
11015  {
11016  SET_RESULT_TO_FALSE(result, l, r);
11017  break;
11018  }
11019 
11020  bool from_the_same_tu = false;
11021  if (!pointer_or_qual_die_of_anonymous_class_type(l)
11022  && compare_dies_cu_decl_file(l, r, from_the_same_tu)
11023  && from_the_same_tu)
11024  {
11025  // These two typedefs, pointer, reference, or qualified
11026  // types have the same name and are defined in the same TU.
11027  // They thus ought to be the same.
11028  //
11029  // Note that pointers, reference or qualified types to
11030  // anonymous types are not taking into account here because
11031  // those always need to be structurally compared.
11032  SET_RESULT_TO_FALSE(result, l, r);
11033  break;
11034  }
11035  }
11036 
11037  {
11038  // No fancy optimization in this case. We need to
11039  // structurally compare the two DIEs.
11040  Dwarf_Die lu_type_die, ru_type_die;
11041  bool lu_is_void, ru_is_void;
11042 
11043  lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
11044  ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
11045 
11046  if (lu_is_void && ru_is_void)
11047  result = COMPARISON_RESULT_EQUAL;
11048  else if (lu_is_void != ru_is_void)
11049  SET_RESULT_TO_FALSE(result, l, r);
11050  else
11051  result = compare_dies(rdr, &lu_type_die, &ru_type_die,
11052  aggregates_being_compared,
11053  update_canonical_dies_on_the_fly);
11054  }
11055  break;
11056 
11057  case DW_TAG_enumeration_type:
11058  if (!compare_as_decl_and_type_dies(rdr, l, r))
11059  SET_RESULT_TO_FALSE(result, l, r);
11060  else
11061  {
11062  // Walk the enumerators.
11063  Dwarf_Die l_enumtor, r_enumtor;
11064  bool found_l_enumtor = true, found_r_enumtor = true;
11065 
11066  if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11067  for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
11068  &l_enumtor) == 0,
11069  found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
11070  &r_enumtor) == 0;
11071  found_l_enumtor && found_r_enumtor;
11072  found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
11073  found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
11074  {
11075  int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
11076  if ( l_tag != r_tag)
11077  {
11078  SET_RESULT_TO_FALSE(result, l, r);
11079  break;
11080  }
11081 
11082  if (l_tag != DW_TAG_enumerator)
11083  continue;
11084 
11085  uint64_t l_val = 0, r_val = 0;
11086  die_unsigned_constant_attribute(&l_enumtor,
11087  DW_AT_const_value,
11088  l_val);
11089  die_unsigned_constant_attribute(&r_enumtor,
11090  DW_AT_const_value,
11091  r_val);
11092  if (l_val != r_val)
11093  {
11094  SET_RESULT_TO_FALSE(result, l, r);
11095  break;
11096  }
11097  }
11098  if (found_l_enumtor != found_r_enumtor )
11099  SET_RESULT_TO_FALSE(result, l, r);
11100  }
11101  break;
11102 
11103  case DW_TAG_structure_type:
11104  case DW_TAG_union_type:
11105  case DW_TAG_class_type:
11106  {
11107  RETURN_IF_COMPARISON_CYCLE_DETECTED;
11108 
11109  rdr.compare_count_++;
11110 
11111  if (!compare_as_decl_and_type_dies(rdr, l, r))
11112  SET_RESULT_TO_FALSE(result, l, r);
11113  else if (rdr.options().assume_odr_for_cplusplus
11114  && rdr.odr_is_relevant(l)
11115  && rdr.odr_is_relevant(r)
11116  && !die_is_anonymous(l)
11117  && !die_is_anonymous(r))
11118  result = COMPARISON_RESULT_EQUAL;
11119  else
11120  {
11121  aggregates_being_compared.add(dies_being_compared);
11122 
11123  Dwarf_Die l_member, r_member;
11124  bool found_l_member = true, found_r_member = true;
11125 
11126  if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11127  for (found_l_member = get_member_child_die(l, &l_member),
11128  found_r_member = get_member_child_die(r, &r_member);
11129  found_l_member && found_r_member;
11130  found_l_member = get_next_member_sibling_die(&l_member,
11131  &l_member),
11132  found_r_member = get_next_member_sibling_die(&r_member,
11133  &r_member))
11134  {
11135  int l_tag = dwarf_tag(&l_member),
11136  r_tag = dwarf_tag(&r_member);
11137 
11138  if (l_tag != r_tag)
11139  {
11140  SET_RESULT_TO_FALSE(result, l, r);
11141  break;
11142  }
11143 
11144  ABG_ASSERT(l_tag == DW_TAG_member
11145  || l_tag == DW_TAG_variable
11146  || l_tag == DW_TAG_inheritance
11147  || l_tag == DW_TAG_subprogram);
11148 
11149  comparison_result local_result =
11150  compare_dies(rdr, &l_member, &r_member,
11151  aggregates_being_compared,
11152  update_canonical_dies_on_the_fly);
11153 
11154  if (local_result == COMPARISON_RESULT_UNKNOWN)
11155  // Note that if the result of comparing any
11156  // sub-type is COMPARISON_RESULT_EQUAL, just
11157  // because we have at least one sub-type's
11158  // comparison being COMPARISON_RESULT_UNKNOWN
11159  // means that the comparison of this type will
11160  // return COMPARISON_RESULT_UNKNOWN to show
11161  // callers that this type (and all the types that
11162  // depend on it) depends on a redundant type
11163  result = local_result;
11164 
11165  if (local_result == COMPARISON_RESULT_DIFFERENT)
11166  {
11167  SET_RESULT_TO_FALSE(result, l, r);
11168  break;
11169  }
11170  }
11171  if (found_l_member != found_r_member)
11172  {
11173  SET_RESULT_TO_FALSE(result, l, r);
11174  break;
11175  }
11176  }
11177  }
11178  break;
11179 
11180  case DW_TAG_array_type:
11181  {
11182  RETURN_IF_COMPARISON_CYCLE_DETECTED;
11183 
11184  aggregates_being_compared.add(dies_being_compared);
11185 
11186  rdr.compare_count_++;
11187 
11188  Dwarf_Die l_child, r_child;
11189  bool found_l_child, found_r_child;
11190  for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11191  &l_child) == 0,
11192  found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11193  &r_child) == 0;
11194  found_l_child && found_r_child;
11195  found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
11196  found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
11197  {
11198  int l_child_tag = dwarf_tag(&l_child),
11199  r_child_tag = dwarf_tag(&r_child);
11200  if (l_child_tag == DW_TAG_subrange_type
11201  || r_child_tag == DW_TAG_subrange_type)
11202  {
11203  result = compare_dies(rdr, &l_child, &r_child,
11204  aggregates_being_compared,
11205  update_canonical_dies_on_the_fly);
11206  if (!result)
11207  {
11208  SET_RESULT_TO_FALSE(result, l, r);
11209  break;
11210  }
11211  }
11212  }
11213  if (found_l_child != found_r_child)
11214  SET_RESULT_TO_FALSE(result, l, r);
11215  // Compare the types of the elements of the array.
11216  Dwarf_Die ltype_die, rtype_die;
11217  bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
11218  bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
11219  ABG_ASSERT(found_ltype && found_rtype);
11220 
11221  result = compare_dies(rdr, &ltype_die, &rtype_die,
11222  aggregates_being_compared,
11223  update_canonical_dies_on_the_fly);
11224  if (!result)
11226  }
11227  break;
11228 
11229  case DW_TAG_subrange_type:
11230  {
11231  uint64_t l_lower_bound = 0, r_lower_bound = 0,
11232  l_upper_bound = 0, r_upper_bound = 0;
11233  bool l_lower_bound_set = false, r_lower_bound_set = false,
11234  l_upper_bound_set = false, r_upper_bound_set = false;
11235 
11236  l_lower_bound_set =
11237  die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
11238  r_lower_bound_set =
11239  die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
11240 
11241  if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
11242  l_upper_bound))
11243  {
11244  uint64_t l_count = 0;
11245  if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
11246  {
11247  l_upper_bound = l_lower_bound + l_count;
11248  l_upper_bound_set = true;
11249  if (l_upper_bound)
11250  --l_upper_bound;
11251  }
11252  }
11253  else
11254  l_upper_bound_set = true;
11255 
11256  if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
11257  r_upper_bound))
11258  {
11259  uint64_t r_count = 0;
11260  if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
11261  {
11262  r_upper_bound = r_lower_bound + r_count;
11263  r_upper_bound_set = true;
11264  if (r_upper_bound)
11265  --r_upper_bound;
11266  }
11267  }
11268  else
11269  r_upper_bound_set = true;
11270 
11271  if ((l_lower_bound_set != r_lower_bound_set)
11272  || (l_upper_bound_set != r_upper_bound_set)
11273  || (l_lower_bound != r_lower_bound)
11274  || (l_upper_bound != r_upper_bound))
11275  SET_RESULT_TO_FALSE(result, l, r);
11276  }
11277  break;
11278 
11279  case DW_TAG_subroutine_type:
11280  case DW_TAG_subprogram:
11281  {
11282  RETURN_IF_COMPARISON_CYCLE_DETECTED;
11283 
11284  aggregates_being_compared.add(dies_being_compared);
11285 
11286  rdr.compare_count_++;
11287 
11288  if (l_tag == DW_TAG_subprogram
11289  && !fn_die_equal_by_linkage_name(l, r))
11290  {
11291  SET_RESULT_TO_FALSE(result, l, r);
11292  break;
11293  }
11294  else if (l_tag == DW_TAG_subprogram
11295  && die_is_in_c(l) && die_is_in_c(r))
11296  {
11297  result = COMPARISON_RESULT_EQUAL;
11298  break;
11299  }
11300  else if (!die_is_in_c(l) && !die_is_in_c(r))
11301  {
11302  // In C, we cannot have two different functions with the
11303  // same linkage name in a given binary. But here we are
11304  // looking at DIEs that don't originate from C. So we
11305  // need to compare return types and parameter types.
11306  Dwarf_Die l_return_type, r_return_type;
11307  bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
11308  l_return_type);
11309  bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
11310  r_return_type);
11311  if (l_return_type_is_void != r_return_type_is_void
11312  || (!l_return_type_is_void
11313  && !compare_dies(rdr,
11314  &l_return_type, &r_return_type,
11315  aggregates_being_compared,
11316  update_canonical_dies_on_the_fly)))
11317  SET_RESULT_TO_FALSE(result, l, r);
11318  else
11319  {
11320  Dwarf_Die l_child, r_child;
11321  bool found_l_child, found_r_child;
11322  for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11323  &l_child) == 0,
11324  found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11325  &r_child) == 0;
11326  found_l_child && found_r_child;
11327  found_l_child = dwarf_siblingof(&l_child,
11328  &l_child) == 0,
11329  found_r_child = dwarf_siblingof(&r_child,
11330  &r_child)==0)
11331  {
11332  int l_child_tag = dwarf_tag(&l_child);
11333  int r_child_tag = dwarf_tag(&r_child);
11334  comparison_result local_result =
11335  COMPARISON_RESULT_EQUAL;
11336  if (l_child_tag != r_child_tag)
11337  local_result = COMPARISON_RESULT_DIFFERENT;
11338  if (l_child_tag == DW_TAG_formal_parameter)
11339  local_result =
11340  compare_dies(rdr, &l_child, &r_child,
11341  aggregates_being_compared,
11342  update_canonical_dies_on_the_fly);
11343  if (local_result == COMPARISON_RESULT_DIFFERENT)
11344  {
11345  result = local_result;
11346  SET_RESULT_TO_FALSE(result, l, r);
11347  break;
11348  }
11349  if (local_result == COMPARISON_RESULT_UNKNOWN)
11350  // Note that if the result of comparing any
11351  // sub-type is COMPARISON_RESULT_EQUAL, just
11352  // because we have at least one sub-type's
11353  // comparison being COMPARISON_RESULT_UNKNOWN
11354  // means that the comparison of this type will
11355  // return COMPARISON_RESULT_UNKNOWN to show
11356  // callers that this type (and all the types
11357  // that depend on it) depends on a redundant
11358  // type and so, can't be
11359  // canonical-type-propagated.
11360  result = local_result;
11361  }
11362  if (found_l_child != found_r_child)
11363  {
11364  SET_RESULT_TO_FALSE(result, l, r);
11365  break;
11366  }
11367  }
11368  }
11369  }
11370  break;
11371 
11372  case DW_TAG_formal_parameter:
11373  {
11374  Dwarf_Die l_type, r_type;
11375  bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
11376  bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
11377  if (l_type_is_void != r_type_is_void)
11378  SET_RESULT_TO_FALSE(result, l, r);
11379  else if (!l_type_is_void)
11380  {
11381  comparison_result local_result =
11382  compare_dies(rdr, &l_type, &r_type,
11383  aggregates_being_compared,
11384  update_canonical_dies_on_the_fly);
11385  SET_RESULT_TO(result, local_result, l, r);
11386  }
11387  }
11388  break;
11389 
11390  case DW_TAG_variable:
11391  case DW_TAG_member:
11392  if (compare_as_decl_dies(l, r))
11393  {
11394  // Compare the offsets of the data members
11395  if (l_tag == DW_TAG_member)
11396  {
11397  int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11398  die_member_offset(rdr, l, l_offset_in_bits);
11399  die_member_offset(rdr, r, r_offset_in_bits);
11400  if (l_offset_in_bits != r_offset_in_bits)
11401  SET_RESULT_TO_FALSE(result, l, r);
11402  }
11403  if (result)
11404  {
11405  // Compare the types of the data members or variables.
11406  Dwarf_Die l_type, r_type;
11407  ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11408  ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11409  comparison_result local_result =
11410  compare_dies(rdr, &l_type, &r_type,
11411  aggregates_being_compared,
11412  update_canonical_dies_on_the_fly);
11413  SET_RESULT_TO(result, local_result, l, r);
11414  }
11415  }
11416  else
11417  SET_RESULT_TO_FALSE(result, l, r);
11418  break;
11419 
11420  case DW_TAG_inheritance:
11421  {
11422  Dwarf_Die l_type, r_type;
11423  ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11424  ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11425  result = compare_dies(rdr, &l_type, &r_type,
11426  aggregates_being_compared,
11427  update_canonical_dies_on_the_fly);
11428  if (!result)
11429  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11430 
11431  uint64_t l_a = 0, r_a = 0;
11432  die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11433  die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11434  if (l_a != r_a)
11435  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11436 
11437  die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11438  die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11439  if (l_a != r_a)
11440  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11441 
11442  int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11443  die_member_offset(rdr, l, l_offset_in_bits);
11444  die_member_offset(rdr, r, r_offset_in_bits);
11445  if (l_offset_in_bits != r_offset_in_bits)
11446  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11447  }
11448  break;
11449 
11450  case DW_TAG_ptr_to_member_type:
11451  {
11452  bool comp_result = false;
11453  if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11454  if (!comp_result)
11455  ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11456 
11457  Dwarf_Die l_type, r_type;
11458  ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11459  ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11460  result = compare_dies(rdr, &l_type, &r_type,
11461  aggregates_being_compared,
11462  update_canonical_dies_on_the_fly);
11463  if (!result)
11464  ABG_RETURN(result);
11465 
11466  ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11467  ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11468  result = compare_dies(rdr, &l_type, &r_type,
11469  aggregates_being_compared,
11470  update_canonical_dies_on_the_fly);
11471  if (!result)
11472  ABG_RETURN(result);
11473  }
11474  break;
11475 
11476  case DW_TAG_enumerator:
11477  case DW_TAG_packed_type:
11478  case DW_TAG_set_type:
11479  case DW_TAG_file_type:
11480  case DW_TAG_thrown_type:
11481  case DW_TAG_interface_type:
11482  case DW_TAG_shared_type:
11483  case DW_TAG_compile_unit:
11484  case DW_TAG_namespace:
11485  case DW_TAG_module:
11486  case DW_TAG_constant:
11487  case DW_TAG_partial_unit:
11488  case DW_TAG_imported_unit:
11489  case DW_TAG_dwarf_procedure:
11490  case DW_TAG_imported_declaration:
11491  case DW_TAG_entry_point:
11492  case DW_TAG_label:
11493  case DW_TAG_lexical_block:
11494  case DW_TAG_unspecified_parameters:
11495  case DW_TAG_variant:
11496  case DW_TAG_common_block:
11497  case DW_TAG_common_inclusion:
11498  case DW_TAG_inlined_subroutine:
11499  case DW_TAG_with_stmt:
11500  case DW_TAG_access_declaration:
11501  case DW_TAG_catch_block:
11502  case DW_TAG_friend:
11503  case DW_TAG_namelist:
11504  case DW_TAG_namelist_item:
11505  case DW_TAG_template_type_parameter:
11506  case DW_TAG_template_value_parameter:
11507  case DW_TAG_try_block:
11508  case DW_TAG_variant_part:
11509  case DW_TAG_imported_module:
11510  case DW_TAG_condition:
11511  case DW_TAG_type_unit:
11512  case DW_TAG_template_alias:
11513  case DW_TAG_lo_user:
11514  case DW_TAG_MIPS_loop:
11515  case DW_TAG_format_label:
11516  case DW_TAG_function_template:
11517  case DW_TAG_class_template:
11518  case DW_TAG_GNU_BINCL:
11519  case DW_TAG_GNU_EINCL:
11520  case DW_TAG_GNU_template_template_param:
11521  case DW_TAG_GNU_template_parameter_pack:
11522  case DW_TAG_GNU_formal_parameter_pack:
11523  case DW_TAG_GNU_call_site:
11524  case DW_TAG_GNU_call_site_parameter:
11525  case DW_TAG_hi_user:
11526 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11527  if (rdr.debug_die_canonicalization_is_on_)
11529 #endif
11531  break;
11532  }
11533 
11534  ABG_RETURN(result);
11535 }
11536 
11537 /// Compare two DIEs emitted by a C compiler.
11538 ///
11539 /// @param rdr the DWARF reader used to load the DWARF information.
11540 ///
11541 /// @param l the left-hand-side argument of this comparison operator.
11542 ///
11543 /// @param r the righ-hand-side argument of this comparison operator.
11544 ///
11545 /// @param update_canonical_dies_on_the_fly if yes, then this function
11546 /// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11547 /// comparing l and r. This helps in making so that sub-type DIEs of
11548 /// 'l' and 'r' are compared structurally only once. This is how we
11549 /// turn this exponential comparison problem into a problem that is a
11550 /// closer to a linear one.
11551 ///
11552 /// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11553 static comparison_result
11554 compare_dies(const reader& rdr,
11555  const Dwarf_Die *l,
11556  const Dwarf_Die *r,
11557  bool update_canonical_dies_on_the_fly)
11558 {
11559  offset_pairs_stack_type aggregates_being_compared(rdr);
11560  return compare_dies(rdr, l, r, aggregates_being_compared,
11561  update_canonical_dies_on_the_fly);
11562 }
11563 
11564 /// Compare two DIEs for the purpose of canonicalization.
11565 ///
11566 /// This is a sub-routine of reader::get_canonical_die.
11567 ///
11568 /// When DIE canonicalization debugging is on, this function performs
11569 /// both structural and canonical comparison. It expects that both
11570 /// comparison yield the same result.
11571 ///
11572 /// @param rdr the DWARF reader.
11573 ///
11574 /// @param l the left-hand-side comparison operand DIE.
11575 ///
11576 /// @param r the right-hand-side comparison operand DIE.
11577 ///
11578 /// @param update_canonical_dies_on_the_fly if true, then some
11579 /// aggregate DIEs will see their canonical types propagated.
11580 ///
11581 /// @return true iff @p l equals @p r.
11582 static bool
11583 compare_dies_during_canonicalization(reader& rdr,
11584  const Dwarf_Die *l,
11585  const Dwarf_Die *r,
11586  bool update_canonical_dies_on_the_fly)
11587 {
11588 #ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11589  if (rdr.debug_die_canonicalization_is_on_)
11590  {
11591  bool canonical_equality = false, structural_equality = false;
11592  rdr.use_canonical_die_comparison_ = false;
11593  structural_equality = compare_dies(rdr, l, r,
11594  /*update_canonical_dies_on_the_fly=*/false);
11595  rdr.use_canonical_die_comparison_ = true;
11596  canonical_equality = compare_dies(rdr, l, r,
11597  update_canonical_dies_on_the_fly);
11598  if (canonical_equality != structural_equality)
11599  {
11600  std::cerr << "structural & canonical equality different for DIEs: "
11601  << std::hex
11602  << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11603  << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11604  << std::dec
11605  << ", repr: '"
11606  << rdr.get_die_pretty_type_representation(l, 0)
11607  << "'"
11608  << std::endl;
11610  }
11611  return structural_equality;
11612  }
11613 #endif
11614  return compare_dies(rdr, l, r,
11615  update_canonical_dies_on_the_fly);
11616 }
11617 
11618 // ----------------------------------
11619 // </die comparison engine>
11620 // ---------------------------------
11621 
11622 /// Get the point where a DW_AT_import DIE is used to import a given
11623 /// (unit) DIE, between two DIEs.
11624 ///
11625 /// @param rdr the dwarf reader to consider.
11626 ///
11627 /// @param partial_unit_offset the imported unit for which we want to
11628 /// know the insertion point. This is usually a partial unit (with
11629 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11630 /// so.
11631 ///
11632 /// @param first_die_offset the offset of the DIE from which this
11633 /// function starts looking for the import point of
11634 /// @partial_unit_offset. Note that this offset is excluded from the
11635 /// set of potential solutions.
11636 ///
11637 /// @param first_die_cu_offset the offset of the (compilation) unit
11638 /// that @p first_die_cu_offset belongs to.
11639 ///
11640 /// @param source where the DIE of first_die_cu_offset unit comes
11641 /// from.
11642 ///
11643 /// @param last_die_offset the offset of the last DIE of the up to
11644 /// which this function looks for the import point of @p
11645 /// partial_unit_offset. Note that this offset is excluded from the
11646 /// set of potential solutions.
11647 ///
11648 /// @param imported_point_offset. The resulting
11649 /// imported_point_offset. Note that if the imported DIE @p
11650 /// partial_unit_offset is not found between @p first_die_offset and
11651 /// @p last_die_offset, this parameter is left untouched by this
11652 /// function.
11653 ///
11654 /// @return true iff an imported unit is found between @p
11655 /// first_die_offset and @p last_die_offset.
11656 static bool
11657 find_import_unit_point_between_dies(const reader& rdr,
11658  size_t partial_unit_offset,
11659  Dwarf_Off first_die_offset,
11660  Dwarf_Off first_die_cu_offset,
11661  die_source source,
11662  size_t last_die_offset,
11663  size_t& imported_point_offset)
11664 {
11665  const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11666  rdr.tu_die_imported_unit_points_map(source);
11667 
11668  tu_die_imported_unit_points_map_type::const_iterator iter =
11669  tu_die_imported_unit_points_map.find(first_die_cu_offset);
11670 
11671  ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11672 
11673  const imported_unit_points_type& imported_unit_points = iter->second;
11674  if (imported_unit_points.empty())
11675  return false;
11676 
11677  imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11678  imported_unit_points_type::const_iterator e = imported_unit_points.end();
11679 
11680  find_lower_bound_in_imported_unit_points(imported_unit_points,
11681  first_die_offset,
11682  b);
11683 
11684  if (last_die_offset != static_cast<size_t>(-1))
11685  find_lower_bound_in_imported_unit_points(imported_unit_points,
11686  last_die_offset,
11687  e);
11688 
11689  if (e != imported_unit_points.end())
11690  {
11691  for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11692  if (i->imported_unit_die_off == partial_unit_offset)
11693  {
11694  imported_point_offset = i->offset_of_import ;
11695  return true;
11696  }
11697 
11698  for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11699  {
11700  if (find_import_unit_point_between_dies(rdr,
11701  partial_unit_offset,
11702  i->imported_unit_child_off,
11703  i->imported_unit_cu_off,
11704  i->imported_unit_die_source,
11705  /*(Dwarf_Off)*/-1,
11706  imported_point_offset))
11707  return true;
11708  }
11709  }
11710  else
11711  {
11712  for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11713  if (i->imported_unit_die_off == partial_unit_offset)
11714  {
11715  imported_point_offset = i->offset_of_import ;
11716  return true;
11717  }
11718 
11719  for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11720  {
11721  if (find_import_unit_point_between_dies(rdr,
11722  partial_unit_offset,
11723  i->imported_unit_child_off,
11724  i->imported_unit_cu_off,
11725  i->imported_unit_die_source,
11726  /*(Dwarf_Off)*/-1,
11727  imported_point_offset))
11728  return true;
11729  }
11730  }
11731 
11732  return false;
11733 }
11734 
11735 /// In the current translation unit, get the last point where a
11736 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
11737 /// given DIE is found. That given DIE is called the limit DIE.
11738 ///
11739 /// Said otherwise, this function returns the last import point of a
11740 /// unit, before a limit.
11741 ///
11742 /// @param rdr the dwarf reader to consider.
11743 ///
11744 /// @param partial_unit_offset the imported unit for which we want to
11745 /// know the insertion point of. This is usually a partial unit (with
11746 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
11747 /// so.
11748 ///
11749 /// @param where_offset the offset of the limit DIE.
11750 ///
11751 /// @param imported_point_offset. The resulting imported_point_offset.
11752 /// Note that if the imported DIE @p partial_unit_offset is not found
11753 /// before @p die_offset, this is set to the last @p
11754 /// partial_unit_offset found under @p parent_die.
11755 ///
11756 /// @return true iff an imported unit is found before @p die_offset.
11757 /// Note that if an imported unit is found after @p die_offset then @p
11758 /// imported_point_offset is set and the function return false.
11759 static bool
11760 find_import_unit_point_before_die(const reader& rdr,
11761  size_t partial_unit_offset,
11762  size_t where_offset,
11763  size_t& imported_point_offset)
11764 {
11765  size_t import_point_offset = 0;
11766  Dwarf_Die first_die_of_tu;
11767 
11768  if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11769  &first_die_of_tu) != 0)
11770  return false;
11771 
11772  Dwarf_Die cu_die_memory;
11773  Dwarf_Die *cu_die;
11774 
11775  cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
11776  &cu_die_memory, 0, 0);
11777 
11778  if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
11779  dwarf_dieoffset(&first_die_of_tu),
11780  dwarf_dieoffset(cu_die),
11781  /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
11782  where_offset,
11783  import_point_offset))
11784  {
11785  imported_point_offset = import_point_offset;
11786  return true;
11787  }
11788 
11789  if (import_point_offset)
11790  {
11791  imported_point_offset = import_point_offset;
11792  return true;
11793  }
11794 
11795  return false;
11796 }
11797 
11798 /// Return the parent DIE for a given DIE.
11799 ///
11800 /// Note that the function build_die_parent_map() must have been
11801 /// called before this one can work. This function either succeeds or
11802 /// aborts the current process.
11803 ///
11804 /// @param rdr the DWARF reader to consider.
11805 ///
11806 /// @param die the DIE for which we want the parent.
11807 ///
11808 /// @param parent_die the output parameter set to the parent die of
11809 /// @p die. Its memory must be allocated and handled by the caller.
11810 ///
11811 /// @param where_offset the offset of the DIE where we are "logically"
11812 /// positionned at, in the DIE tree. This is useful when @p die is
11813 /// e.g, DW_TAG_partial_unit that can be included in several places in
11814 /// the DIE tree.
11815 ///
11816 /// @return true if the function could get a parent DIE, false
11817 /// otherwise.
11818 static bool
11819 get_parent_die(const reader& rdr,
11820  const Dwarf_Die* die,
11821  Dwarf_Die& parent_die,
11822  size_t where_offset)
11823 {
11824  ABG_ASSERT(rdr.dwarf_debug_info());
11825 
11826  const die_source source = rdr.get_die_source(die);
11827 
11828  const offset_offset_map_type& m = rdr.die_parent_map(source);
11829  offset_offset_map_type::const_iterator i =
11830  m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
11831 
11832  if (i == m.end())
11833  return false;
11834 
11835  switch (source)
11836  {
11837  case PRIMARY_DEBUG_INFO_DIE_SOURCE:
11838  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11839  i->second, &parent_die));
11840  break;
11841  case ALT_DEBUG_INFO_DIE_SOURCE:
11842  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
11843  i->second, &parent_die));
11844  break;
11845  case TYPE_UNIT_DIE_SOURCE:
11846  ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11847  i->second, &parent_die));
11848  break;
11849  case NO_DEBUG_INFO_DIE_SOURCE:
11850  case NUMBER_OF_DIE_SOURCES:
11852  }
11853 
11854  if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
11855  {
11856  if (where_offset == 0)
11857  {
11858  parent_die = *rdr.cur_tu_die();
11859  return true;
11860  }
11861  size_t import_point_offset = 0;
11862  bool found =
11863  find_import_unit_point_before_die(rdr,
11864  dwarf_dieoffset(&parent_die),
11865  where_offset,
11866  import_point_offset);
11867  if (!found)
11868  // It looks like parent_die (which comes from the alternate
11869  // debug info file) hasn't been imported into this TU. So,
11870  // Let's assume its logical parent is the DIE of the current
11871  // TU.
11872  parent_die = *rdr.cur_tu_die();
11873  else
11874  {
11875  ABG_ASSERT(import_point_offset);
11876  Dwarf_Die import_point_die;
11877  ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11878  import_point_offset,
11879  &import_point_die));
11880  return get_parent_die(rdr, &import_point_die,
11881  parent_die, where_offset);
11882  }
11883  }
11884 
11885  return true;
11886 }
11887 
11888 /// Get the DIE representing the scope of a given DIE.
11889 ///
11890 /// Please note that when the DIE we are looking at has a
11891 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
11892 /// DIE is the parent DIE of the DIE referred to by that attribute.
11893 /// In other words, this function returns the scope of the origin DIE
11894 /// of the current DIE.
11895 ///
11896 /// So, the scope DIE can be different from the parent DIE of a given
11897 /// DIE.
11898 ///
11899 /// Also note that if the current translation unit is from C, then
11900 /// this returns the global scope.
11901 ///
11902 /// @param rdr the DWARF reader to use.
11903 ///
11904 /// @param dye the DIE to consider.
11905 ///
11906 /// @param where_offset where we are logically at in the DIE stream.
11907 ///
11908 /// @param scope_die out parameter. This is set to the resulting
11909 /// scope DIE iff the function returns true.
11910 ///
11911 /// @return true iff the scope was found and returned in the @p
11912 /// scope_die parameter.
11913 static bool
11914 get_scope_die(const reader& rdr,
11915  const Dwarf_Die* dye,
11916  size_t where_offset,
11917  Dwarf_Die& scope_die)
11918 {
11919  Dwarf_Die origin_die_mem;
11920  Dwarf_Die *die = &origin_die_mem;
11921  if (!die_origin_die(dye, origin_die_mem))
11922  memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
11923 
11924  translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11925  get_die_language(die, die_lang);
11926  if (is_c_language(die_lang)
11927  || rdr.die_parent_map(rdr.get_die_source(die)).empty())
11928  {
11929  ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
11930  return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
11931  }
11932 
11933  if (!get_parent_die(rdr, die, scope_die, where_offset))
11934  return false;
11935 
11936  if (dwarf_tag(&scope_die) == DW_TAG_subprogram
11937  || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
11938  || dwarf_tag(&scope_die) == DW_TAG_array_type)
11939  return get_scope_die(rdr, &scope_die, where_offset, scope_die);
11940 
11941  return true;
11942 }
11943 
11944 /// Return the abigail IR node representing the scope of a given DIE.
11945 ///
11946 /// Note that it is the logical scope that is returned. That is, if
11947 /// the DIE has a DW_AT_specification or DW_AT_abstract_origin
11948 /// attribute, it's the scope of the referred-to DIE (via these
11949 /// attributes) that is returned. In other words, its the scope of
11950 /// the origin DIE that is returned.
11951 ///
11952 /// Also note that if the current translation unit is from C, then
11953 /// this returns the global scope.
11954 ///
11955 /// @param rdr the dwarf reader to use.
11956 ///
11957 /// @param dye the DIE to get the scope for.
11958 ///
11959 /// @param called_from_public_decl is true if this function has been
11960 /// initially called within the context of a public decl.
11961 ///
11962 /// @param where_offset the offset of the DIE where we are "logically"
11963 /// positionned at, in the DIE tree. This is useful when @p die is
11964 /// e.g, DW_TAG_partial_unit that can be included in several places in
11965 /// the DIE tree.
11966 ///
11967 /// @return the resulting scope, or nil if could not be computed.
11968 static scope_decl_sptr
11969 get_scope_for_die(reader& rdr,
11970  Dwarf_Die* dye,
11971  bool called_for_public_decl,
11972  size_t where_offset)
11973 {
11974  Dwarf_Die origin_die_mem;
11975  Dwarf_Die *die = &origin_die_mem;
11976 
11977  if (!die_origin_die(dye, origin_die_mem))
11978  // There was no origin DIE found, so let's make the "die" pointer
11979  // above point to the content of the input "dye".
11980  memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
11981 
11982  const die_source source_of_die = rdr.get_die_source(die);
11983 
11984  translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11985  get_die_language(die, die_lang);
11986  if (is_c_language(die_lang)
11987  || rdr.die_parent_map(source_of_die).empty())
11988  {
11989  // In units for the C languages all decls belong to the global
11990  // namespace. This is generally the case if Libabigail
11991  // determined that no DIE -> parent map was needed.
11992  ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11993  return rdr.global_scope();
11994  }
11995 
11996  Dwarf_Die parent_die;
11997 
11998  if (!get_parent_die(rdr, die, parent_die, where_offset))
11999  return rdr.nil_scope();
12000 
12001  if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
12002  || dwarf_tag(&parent_die) == DW_TAG_partial_unit
12003  || dwarf_tag(&parent_die) == DW_TAG_type_unit)
12004  {
12005  if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
12006  || dwarf_tag(&parent_die) == DW_TAG_type_unit)
12007  {
12008  ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
12009  || source_of_die == TYPE_UNIT_DIE_SOURCE);
12010  return rdr.cur_transl_unit()->get_global_scope();
12011  }
12012 
12013  // For top level DIEs like DW_TAG_compile_unit, we just want to
12014  // return the global scope for the corresponding translation
12015  // unit. This must have been set by
12016  // build_translation_unit_and_add_to_ir if we already started to
12017  // build the translation unit of parent_die. Otherwise, just
12018  // return the global scope of the current translation unit.
12019  die_tu_map_type::const_iterator i =
12020  rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
12021  if (i != rdr.die_tu_map().end())
12022  return i->second->get_global_scope();
12023  return rdr.cur_transl_unit()->get_global_scope();
12024  }
12025 
12026  scope_decl_sptr s;
12028  if (dwarf_tag(&parent_die) == DW_TAG_subprogram
12029  || dwarf_tag(&parent_die) == DW_TAG_array_type
12030  || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
12031  // this is an entity defined in a scope that is a function.
12032  // Normally, I would say that this should be dropped. But I have
12033  // seen a case where a typedef DIE needed by a function parameter
12034  // was defined right before the parameter, under the scope of the
12035  // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
12036  // the function parm too. So for that case, let's say that the
12037  // scope is the scope of the function itself. Note that this is
12038  // an error of the DWARF emitter. We should never see this DIE in
12039  // this context.
12040  {
12041  scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
12042  called_for_public_decl,
12043  where_offset);
12044  if (is_anonymous_type_die(die))
12045  // For anonymous type that have nothing to do in a function or
12046  // array type context, let's put it in the containing
12047  // namespace. That is, do not let it be in a containing class
12048  // or union where it has nothing to do.
12049  while (is_class_or_union_type(s))
12050  {
12051  if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
12052  return rdr.nil_scope();
12053  s = get_scope_for_die(rdr, &parent_die,
12054  called_for_public_decl,
12055  where_offset);
12056  }
12057  return s;
12058  }
12059  else
12060  d = build_ir_node_from_die(rdr, &parent_die,
12061  called_for_public_decl,
12062  where_offset);
12063  s = dynamic_pointer_cast<scope_decl>(d);
12064  if (!s)
12065  // this is an entity defined in someting that is not a scope.
12066  // Let's drop it.
12067  return rdr.nil_scope();
12068 
12069  class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
12070  if (cl && cl->get_is_declaration_only())
12071  {
12072  scope_decl_sptr scop =
12073  dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
12074  if (scop)
12075  s = scop;
12076  else
12077  s = cl;
12078  }
12079  return s;
12080 }
12081 
12082 /// Convert a DWARF constant representing the value of the
12083 /// DW_AT_language property into the translation_unit::language
12084 /// enumerator.
12085 ///
12086 /// @param l the DWARF constant to convert.
12087 ///
12088 /// @return the resulting translation_unit::language enumerator.
12090 dwarf_language_to_tu_language(size_t l)
12091 {
12092  switch (l)
12093  {
12094  case DW_LANG_C89:
12095  return translation_unit::LANG_C89;
12096  case DW_LANG_C:
12097  return translation_unit::LANG_C;
12098  case DW_LANG_Ada83:
12099  return translation_unit::LANG_Ada83;
12100  case DW_LANG_C_plus_plus:
12101  return translation_unit::LANG_C_plus_plus;
12102  case DW_LANG_Cobol74:
12103  return translation_unit::LANG_Cobol74;
12104  case DW_LANG_Cobol85:
12105  return translation_unit::LANG_Cobol85;
12106  case DW_LANG_Fortran77:
12107  return translation_unit::LANG_Fortran77;
12108  case DW_LANG_Fortran90:
12109  return translation_unit::LANG_Fortran90;
12110  case DW_LANG_Pascal83:
12111  return translation_unit::LANG_Pascal83;
12112  case DW_LANG_Modula2:
12113  return translation_unit::LANG_Modula2;
12114  case DW_LANG_Java:
12115  return translation_unit::LANG_Java;
12116  case DW_LANG_C99:
12117  return translation_unit::LANG_C99;
12118  case DW_LANG_Ada95:
12119  return translation_unit::LANG_Ada95;
12120  case DW_LANG_Fortran95:
12121  return translation_unit::LANG_Fortran95;
12122  case DW_LANG_PLI:
12123  return translation_unit::LANG_PLI;
12124  case DW_LANG_ObjC:
12125  return translation_unit::LANG_ObjC;
12126  case DW_LANG_ObjC_plus_plus:
12127  return translation_unit::LANG_ObjC_plus_plus;
12128 
12129 #ifdef HAVE_DW_LANG_Rust_enumerator
12130  case DW_LANG_Rust:
12131  return translation_unit::LANG_Rust;
12132 #endif
12133 
12134 #ifdef HAVE_DW_LANG_UPC_enumerator
12135  case DW_LANG_UPC:
12136  return translation_unit::LANG_UPC;
12137 #endif
12138 
12139 #ifdef HAVE_DW_LANG_D_enumerator
12140  case DW_LANG_D:
12141  return translation_unit::LANG_D;
12142 #endif
12143 
12144 #ifdef HAVE_DW_LANG_Python_enumerator
12145  case DW_LANG_Python:
12146  return translation_unit::LANG_Python;
12147 #endif
12148 
12149 #ifdef HAVE_DW_LANG_Go_enumerator
12150  case DW_LANG_Go:
12151  return translation_unit::LANG_Go;
12152 #endif
12153 
12154 #ifdef HAVE_DW_LANG_C11_enumerator
12155  case DW_LANG_C11:
12156  return translation_unit::LANG_C11;
12157 #endif
12158 
12159 #ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
12160  case DW_LANG_C_plus_plus_03:
12161  return translation_unit::LANG_C_plus_plus_03;
12162 #endif
12163 
12164 #ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
12165  case DW_LANG_C_plus_plus_11:
12166  return translation_unit::LANG_C_plus_plus_11;
12167 #endif
12168 
12169 #ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
12170  case DW_LANG_C_plus_plus_14:
12171  return translation_unit::LANG_C_plus_plus_14;
12172 #endif
12173 
12174 #ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
12175  case DW_LANG_Mips_Assembler:
12176  return translation_unit::LANG_Mips_Assembler;
12177 #endif
12178 
12179  default:
12180  return translation_unit::LANG_UNKNOWN;
12181  }
12182 }
12183 
12184 /// Get the default array lower bound value as defined by the DWARF
12185 /// specification, version 4, depending on the language of the
12186 /// translation unit.
12187 ///
12188 /// @param l the language of the translation unit.
12189 ///
12190 /// @return the default array lower bound value.
12191 static uint64_t
12192 get_default_array_lower_bound(translation_unit::language l)
12193 {
12194  int value = 0;
12195  switch (l)
12196  {
12197  case translation_unit::LANG_UNKNOWN:
12198  value = 0;
12199  break;
12200  case translation_unit::LANG_Cobol74:
12201  case translation_unit::LANG_Cobol85:
12202  value = 1;
12203  break;
12204  case translation_unit::LANG_C89:
12205  case translation_unit::LANG_C99:
12206  case translation_unit::LANG_C11:
12207  case translation_unit::LANG_C:
12208  case translation_unit::LANG_C_plus_plus_03:
12209  case translation_unit::LANG_C_plus_plus_11:
12210  case translation_unit::LANG_C_plus_plus_14:
12211  case translation_unit::LANG_C_plus_plus:
12212  case translation_unit::LANG_ObjC:
12213  case translation_unit::LANG_ObjC_plus_plus:
12214  case translation_unit::LANG_Rust:
12215  value = 0;
12216  break;
12217  case translation_unit::LANG_Fortran77:
12218  case translation_unit::LANG_Fortran90:
12219  case translation_unit::LANG_Fortran95:
12220  case translation_unit::LANG_Ada83:
12221  case translation_unit::LANG_Ada95:
12222  case translation_unit::LANG_Pascal83:
12223  case translation_unit::LANG_Modula2:
12224  value = 1;
12225  break;
12226  case translation_unit::LANG_Java:
12227  value = 0;
12228  break;
12229  case translation_unit::LANG_PLI:
12230  value = 1;
12231  break;
12232  case translation_unit::LANG_UPC:
12233  case translation_unit::LANG_D:
12234  case translation_unit::LANG_Python:
12235  case translation_unit::LANG_Go:
12236  case translation_unit::LANG_Mips_Assembler:
12237  value = 0;
12238  break;
12239  }
12240 
12241  return value;
12242 }
12243 
12244 /// For a given offset, find the lower bound of a sorted vector of
12245 /// imported unit point offset.
12246 ///
12247 /// The lower bound is the smallest point (the point with the smallest
12248 /// offset) which is the greater than a given offset.
12249 ///
12250 /// @param imported_unit_points_type the sorted vector of imported
12251 /// unit points.
12252 ///
12253 /// @param val the offset to consider when looking for the lower
12254 /// bound.
12255 ///
12256 /// @param r an iterator to the lower bound found. This parameter is
12257 /// set iff the function returns true.
12258 ///
12259 /// @return true iff the lower bound has been found.
12260 static bool
12261 find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
12262  Dwarf_Off val,
12263  imported_unit_points_type::const_iterator& r)
12264 {
12265  imported_unit_point v(val);
12266  imported_unit_points_type::const_iterator result =
12267  std::lower_bound(p.begin(), p.end(), v);
12268 
12269  bool is_ok = result != p.end();
12270 
12271  if (is_ok)
12272  r = result;
12273 
12274  return is_ok;
12275 }
12276 
12277 /// Given a DW_TAG_compile_unit, build and return the corresponding
12278 /// abigail::translation_unit ir node. Note that this function
12279 /// recursively reads the children dies of the current DIE and
12280 /// populates the resulting translation unit.
12281 ///
12282 /// @param rdr the DWARF reader to use.
12283 ///
12284 /// @param die the DW_TAG_compile_unit DIE to consider.
12285 ///
12286 /// @param address_size the size of the addresses expressed in this
12287 /// translation unit in general.
12288 ///
12289 /// @return a pointer to the resulting translation_unit.
12290 static translation_unit_sptr
12291 build_translation_unit_and_add_to_ir(reader& rdr,
12292  Dwarf_Die* die,
12293  char address_size)
12294 {
12295  translation_unit_sptr result;
12296 
12297  if (!die)
12298  return result;
12299  ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
12300 
12301  // Clear the part of the context that is dependent on the translation
12302  // unit we are reading.
12303  rdr.clear_per_translation_unit_data();
12304 
12305  rdr.cur_tu_die(die);
12306 
12307  string path = die_string_attribute(die, DW_AT_name);
12308  if (path == "<artificial>")
12309  {
12310  // This is a file artificially generated by the compiler, so its
12311  // name is '<artificial>'. As we want all different translation
12312  // units to have unique path names, let's suffix this path name
12313  // with its die offset.
12314  std::ostringstream o;
12315  o << path << "-" << std::hex << dwarf_dieoffset(die);
12316  path = o.str();
12317  }
12318  string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
12319 
12320  // See if the same translation unit exits already in the current
12321  // corpus. Sometimes, the same translation unit can be present
12322  // several times in the same debug info. The content of the
12323  // different instances of the translation unit are different. So to
12324  // represent that, we are going to re-use the same translation
12325  // unit. That is, it's going to be the union of all the translation
12326  // units of the same path.
12327  {
12328  const string& abs_path =
12329  compilation_dir.empty() ? path : compilation_dir + "/" + path;
12330  result = rdr.corpus()->find_translation_unit(abs_path);
12331  }
12332 
12333  if (!result)
12334  {
12335  result.reset(new translation_unit(rdr.env(),
12336  path,
12337  address_size));
12338  result->set_compilation_dir_path(compilation_dir);
12339  rdr.corpus()->add(result);
12340  uint64_t l = 0;
12341  die_unsigned_constant_attribute(die, DW_AT_language, l);
12342  result->set_language(dwarf_language_to_tu_language(l));
12343  }
12344 
12345  rdr.cur_transl_unit(result);
12346  rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
12347 
12348  Dwarf_Die child;
12349  if (dwarf_child(die, &child) != 0)
12350  return result;
12351 
12352  result->set_is_constructed(false);
12353  int tag = dwarf_tag(&child);
12354  do
12355  if (rdr.load_undefined_interfaces()
12356  && (rdr.is_decl_die_with_undefined_symbol(&child)
12357  || tag == DW_TAG_class_type // Top-level classes might
12358  // have undefined interfaces
12359  // that need to be
12360  // represented, so let's
12361  // analyze them as well.
12362  || ((tag == DW_TAG_union_type || tag == DW_TAG_structure_type)
12363  && die_is_in_cplus_plus(&child))))
12364  {
12365  // Analyze undefined functions & variables for the purpose of
12366  // analyzing compatibility matters.
12367  build_ir_node_from_die(rdr, &child,
12368  // Pretend the DIE is publicly defined
12369  // so that types that are reachable
12370  // from it get analyzed as well.
12371  /*die_is_public=*/true,
12372  dwarf_dieoffset(&child));
12373  }
12374  else if (!rdr.env().analyze_exported_interfaces_only()
12375  || rdr.is_decl_die_with_exported_symbol(&child))
12376  {
12377  // Analyze all the DIEs we encounter unless we are asked to only
12378  // analyze exported interfaces and the types reachables from them.
12379  build_ir_node_from_die(rdr, &child,
12380  die_is_public_decl(&child),
12381  dwarf_dieoffset(&child));
12382  }
12383  while (dwarf_siblingof(&child, &child) == 0);
12384 
12385  if (!rdr.var_decls_to_re_add_to_tree().empty())
12386  for (list<var_decl_sptr>::const_iterator v =
12387  rdr.var_decls_to_re_add_to_tree().begin();
12388  v != rdr.var_decls_to_re_add_to_tree().end();
12389  ++v)
12390  {
12391  if (is_member_decl(*v))
12392  continue;
12393 
12394  ABG_ASSERT((*v)->get_scope());
12395  string demangled_name =
12396  demangle_cplus_mangled_name((*v)->get_linkage_name());
12397  if (!demangled_name.empty())
12398  {
12399  std::list<string> fqn_comps;
12400  fqn_to_components(demangled_name, fqn_comps);
12401  string mem_name = fqn_comps.back();
12402  fqn_comps.pop_back();
12403  class_decl_sptr class_type;
12404  string ty_name;
12405  if (!fqn_comps.empty())
12406  {
12407  ty_name = components_to_type_name(fqn_comps);
12408  class_type =
12409  lookup_class_type(ty_name, *rdr.cur_transl_unit());
12410  }
12411  if (class_type)
12412  {
12413  // So we are seeing a member variable for which there
12414  // is a global variable definition DIE not having a
12415  // reference attribute pointing back to the member
12416  // variable declaration DIE. Thus remove the global
12417  // variable definition from its current non-class
12418  // scope ...
12419  decl_base_sptr d;
12420  if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12421  // This is the data member with the same name in cl.
12422  // We just need to flag it as static.
12423  ;
12424  else
12425  {
12426  // In this case there is no data member with the
12427  // same name in cl already. Let's add it there then
12428  // ...
12430  d = add_decl_to_scope(*v, class_type);
12431  }
12432 
12433  ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12434  // Let's flag the data member as static.
12435  set_member_is_static(d, true);
12436  }
12437  }
12438  }
12439  rdr.var_decls_to_re_add_to_tree().clear();
12440 
12441  result->set_is_constructed(true);
12442 
12443  return result;
12444 }
12445 
12446 /// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12447 /// DW_TAG_module (for fortran) DIE.
12448 ///
12449 /// Note that this function connects the DW_TAG_namespace to the IR
12450 /// being currently created, reads the children of the DIE and
12451 /// connects them to the IR as well.
12452 ///
12453 /// @param rdr the DWARF reader to use.
12454 ///
12455 /// @param die the DIE to read from. Must be either DW_TAG_namespace
12456 /// or DW_TAG_module.
12457 ///
12458 /// @param where_offset the offset of the DIE where we are "logically"
12459 /// positionned at, in the DIE tree. This is useful when @p die is
12460 /// e.g, DW_TAG_partial_unit that can be included in several places in
12461 /// the DIE tree.
12462 ///
12463 /// @return the resulting @ref abigail::namespace_decl or NULL if it
12464 /// couldn't be created.
12465 static namespace_decl_sptr
12466 build_namespace_decl_and_add_to_ir(reader& rdr,
12467  Dwarf_Die* die,
12468  size_t where_offset)
12469 {
12470  namespace_decl_sptr result;
12471 
12472  if (!die)
12473  return result;
12474 
12475  unsigned tag = dwarf_tag(die);
12476  if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12477  return result;
12478 
12479  scope_decl_sptr scope = get_scope_for_die(rdr, die,
12480  /*called_for_public_decl=*/false,
12481  where_offset);
12482 
12483  string name, linkage_name;
12484  location loc;
12485  die_loc_and_name(rdr, die, loc, name, linkage_name);
12486 
12487  result.reset(new namespace_decl(rdr.env(), name, loc));
12488  add_decl_to_scope(result, scope.get());
12489  rdr.associate_die_to_decl(die, result, where_offset);
12490 
12491  Dwarf_Die child;
12492  if (dwarf_child(die, &child) != 0)
12493  return result;
12494 
12495  rdr.scope_stack().push(result.get());
12496  do
12497  build_ir_node_from_die(rdr, &child,
12498  // If this namespace DIE is private
12499  // (anonymous) then all its content is
12500  // considered private. Otherwise, its
12501  // public decls are considered public.
12502  /*called_from_public_decl=*/
12503  die_is_public_decl(die) && die_is_public_decl(&child),
12504  where_offset);
12505  while (dwarf_siblingof(&child, &child) == 0);
12506  rdr.scope_stack().pop();
12507 
12508  return result;
12509 }
12510 
12511 /// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12512 ///
12513 /// @param rdr the DWARF reader to use.
12514 ///
12515 /// @param die the DW_TAG_base_type to consider.
12516 ///
12517 /// @param where_offset where we are logically at in the DIE stream.
12518 ///
12519 /// @return the resulting decl_base_sptr.
12520 static type_decl_sptr
12521 build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12522 {
12523  type_decl_sptr result;
12524 
12525  if (!die)
12526  return result;
12527  ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12528 
12529  uint64_t byte_size = 0, bit_size = 0;
12530  if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12531  if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12532  return result;
12533 
12534  if (bit_size == 0 && byte_size != 0)
12535  // Update the bit size.
12536  bit_size = byte_size * 8;
12537 
12538  string type_name, linkage_name;
12539  location loc;
12540  die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12541 
12542  if (byte_size == 0)
12543  {
12544  // The size of the type is zero, that must mean that we are
12545  // looking at the definition of the void type.
12546  if (type_name == "void")
12547  result = is_type_decl(build_ir_node_for_void_type(rdr));
12548  else
12549  // A type of size zero that is not void? Hmmh, I am not sure
12550  // what that means. Return nil for now.
12551  return result;
12552  }
12553 
12554  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12555  {
12556  string normalized_type_name = type_name;
12557  integral_type int_type;
12558  if (parse_integral_type(type_name, int_type))
12559  normalized_type_name = int_type.to_string();
12560  result = lookup_basic_type(normalized_type_name, *corp);
12561  }
12562 
12563  if (!result)
12564  if (corpus_sptr corp = rdr.corpus())
12565  result = lookup_basic_type(type_name, *corp);
12566  if (!result)
12567  result.reset(new type_decl(rdr.env(), type_name, bit_size,
12568  /*alignment=*/0, loc, linkage_name));
12569  rdr.associate_die_to_type(die, result, where_offset);
12570  return result;
12571 }
12572 
12573 /// Construct the type that is to be used as the underlying type of an
12574 /// enum.
12575 ///
12576 /// @param rdr the DWARF reader to use.
12577 ///
12578 /// @param enum_name the name of the enum that this type is going to
12579 /// be the underlying type of.
12580 ///
12581 /// @param enum_size the size of the enum.
12582 ///
12583 /// @param is_anonymous whether the underlying type is anonymous or
12584 /// not. By default, this should be set to true as before c++11 (and
12585 /// in C), it's almost the case.
12586 static type_decl_sptr
12587 build_enum_underlying_type(reader& rdr,
12588  string enum_name,
12589  uint64_t enum_size,
12590  bool is_anonymous = true)
12591 {
12592  string underlying_type_name =
12593  build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12594  enum_size);
12595 
12596  type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12597  enum_size, enum_size, location()));
12598  result->set_is_anonymous(is_anonymous);
12599  result->set_is_artificial(true);
12600  translation_unit_sptr tu = rdr.cur_transl_unit();
12601  decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12602  result = dynamic_pointer_cast<type_decl>(d);
12603  ABG_ASSERT(result);
12604  canonicalize(result);
12605  return result;
12606 }
12607 
12608 /// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12609 ///
12610 /// @param rdr the DWARF reader to use.
12611 ///
12612 /// @param die the DIE to read from.
12613 ///
12614 /// @param scope the scope of the final enum. Note that this function
12615 /// does *NOT* add the built type to this scope. The scope is just so
12616 /// that the function knows how to name anonymous enums.
12617 ///
12618 /// @param is_declaration_only is true if the DIE denoted by @p die is
12619 /// a declaration-only DIE.
12620 ///
12621 /// @return the built enum_type_decl or NULL if it could not be built.
12622 static enum_type_decl_sptr
12623 build_enum_type(reader& rdr,
12624  Dwarf_Die* die,
12625  scope_decl* scope,
12626  size_t where_offset,
12627  bool is_declaration_only)
12628 {
12629  enum_type_decl_sptr result;
12630  if (!die)
12631  return result;
12632 
12633  unsigned tag = dwarf_tag(die);
12634  if (tag != DW_TAG_enumeration_type)
12635  return result;
12636 
12637  string name, linkage_name;
12638  location loc;
12639  die_loc_and_name(rdr, die, loc, name, linkage_name);
12640 
12641  bool is_anonymous = false;
12642  // If the enum is anonymous, let's give it a name.
12643  if (name.empty())
12644  {
12645  name = get_internal_anonymous_die_prefix_name(die);
12646  ABG_ASSERT(!name.empty());
12647  // But we remember that the type is anonymous.
12648  is_anonymous = true;
12649 
12650  if (size_t s = scope->get_num_anonymous_member_enums())
12651  name = build_internal_anonymous_die_name(name, s);
12652  }
12653 
12654  bool use_odr = rdr.odr_is_relevant(die);
12655  // If the type has location, then associate it to its
12656  // representation. This way, all occurences of types with the same
12657  // representation (name) and location can be later detected as being
12658  // for the same type.
12659 
12660  if (!is_anonymous)
12661  {
12662  if (use_odr)
12663  {
12664  if (enum_type_decl_sptr pre_existing_enum =
12665  is_enum_type(rdr.lookup_artifact_from_die(die)))
12666  result = pre_existing_enum;
12667  }
12668  else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12669  {
12670  if (loc)
12671  result = lookup_enum_type_per_location(loc.expand(), *corp);
12672  }
12673  else if (loc)
12674  {
12675  if (enum_type_decl_sptr pre_existing_enum =
12676  is_enum_type(rdr.lookup_artifact_from_die(die)))
12677  if (pre_existing_enum->get_location() == loc)
12678  result = pre_existing_enum;
12679  }
12680 
12681  if (result)
12682  {
12683  rdr.associate_die_to_type(die, result, where_offset);
12684  return result;
12685  }
12686  }
12687  // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12688  // we can look them up?
12689 
12690  uint64_t size = 0;
12691  if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12692  size *= 8;
12693  bool is_artificial = die_is_artificial(die);
12694 
12695  // for now we consider that underlying types of enums are all anonymous
12696  bool enum_underlying_type_is_anonymous= true;
12697 
12699  Dwarf_Die child;
12700  if (dwarf_child(die, &child) == 0)
12701  {
12702  do
12703  {
12704  if (dwarf_tag(&child) != DW_TAG_enumerator)
12705  continue;
12706 
12707  string n, m;
12708  location l;
12709  die_loc_and_name(rdr, &child, l, n, m);
12710  uint64_t val = 0;
12711  die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12712  enms.push_back(enum_type_decl::enumerator(n, val));
12713  }
12714  while (dwarf_siblingof(&child, &child) == 0);
12715  }
12716 
12717  // DWARF up to version 4 (at least) doesn't seem to carry the
12718  // underlying type, so let's create an artificial one here, which
12719  // sole purpose is to be passed to the constructor of the
12720  // enum_type_decl type.
12721  type_decl_sptr t =
12722  build_enum_underlying_type(rdr, name, size,
12723  enum_underlying_type_is_anonymous);
12724  t->set_is_declaration_only(is_declaration_only);
12725 
12726  result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12727  result->set_is_anonymous(is_anonymous);
12728  result->set_is_declaration_only(is_declaration_only);
12729  result->set_is_artificial(is_artificial);
12730  rdr.associate_die_to_type(die, result, where_offset);
12731 
12732  rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12733 
12734  return result;
12735 }
12736 
12737 /// Once a function_decl has been built and added to a class as a
12738 /// member function, this function updates the information of the
12739 /// function_decl concerning the properties of its relationship with
12740 /// the member class. That is, it updates properties like
12741 /// virtualness, access, constness, cdtorness, etc ...
12742 ///
12743 /// @param die the DIE of the function_decl that has been just built.
12744 ///
12745 /// @param f the function_decl that has just been built from @p die.
12746 ///
12747 /// @param klass the @ref class_or_union that @p f belongs to.
12748 ///
12749 /// @param rdr the context used to read the ELF/DWARF information.
12750 static void
12751 finish_member_function_reading(Dwarf_Die* die,
12752  const function_decl_sptr& f,
12753  const class_or_union_sptr klass,
12754  reader& rdr)
12755 {
12756  ABG_ASSERT(klass);
12757 
12758  method_decl_sptr m = is_method_decl(f);
12759  ABG_ASSERT(m);
12760 
12761  method_type_sptr method_t = is_method_type(m->get_type());
12762  ABG_ASSERT(method_t);
12763 
12764  size_t is_inline = die_is_declared_inline(die);
12765  bool is_ctor = (f->get_name() == klass->get_name());
12766  bool is_dtor = (!f->get_name().empty()
12767  && static_cast<string>(f->get_name())[0] == '~');
12768  bool is_virtual = die_is_virtual(die);
12769  int64_t vindex = -1;
12770  if (is_virtual)
12771  die_virtual_function_index(die, vindex);
12772  access_specifier access = public_access;
12773  if (class_decl_sptr c = is_class_type(klass))
12774  if (!c->is_struct())
12775  access = private_access;
12776  die_access_specifier(die, access);
12777 
12778  bool is_static = false;
12779  {
12780  // Let's see if the first parameter is a pointer to an instance of
12781  // the same class type as the current class and has a
12782  // DW_AT_artificial attribute flag set. We are not looking at
12783  // DW_AT_object_pointer (for DWARF 3) because it wasn't being
12784  // emitted in GCC 4_4, which was already DWARF 3.
12785  function_decl::parameter_sptr first_parm;
12786  if (!f->get_parameters().empty())
12787  first_parm = f->get_parameters()[0];
12788 
12789  bool is_artificial = first_parm && first_parm->get_is_artificial();
12790  type_base_sptr this_ptr_type, other_klass;
12791 
12792  if (is_artificial)
12793  this_ptr_type = first_parm->get_type();
12794 
12795  // Sometimes, the type of the "this" pointer is "const class_type* const".
12796  //
12797  // Meaning that the "this pointer" itself is const qualified. So
12798  // let's get the underlying underlying non-qualified pointer.
12799  if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
12800  this_ptr_type = q->get_underlying_type();
12801 
12802  // Now, get the pointed-to type.
12803  if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
12804  other_klass = p->get_pointed_to_type();
12805 
12806  // Sometimes, other_klass can be qualified; e.g, volatile. In
12807  // that case, let's get the unqualified version of other_klass.
12808  if (qualified_type_def_sptr q = is_qualified_type(other_klass))
12809  other_klass = q->get_underlying_type();
12810 
12811  if (other_klass
12812  && get_type_name(other_klass) == klass->get_qualified_name())
12813  ;
12814  else
12815  is_static = true;
12816 
12817  if (is_static)
12818  {
12819  // If we are looking at a DWARF version that is high enough
12820  // for the DW_AT_object_pointer attribute to be present, let's
12821  // see if it's present. If it is, then the current member
12822  // function is not static.
12823  Dwarf_Die object_pointer_die;
12824  if (die_has_object_pointer(die, object_pointer_die))
12825  is_static = false;
12826  }
12827  }
12828  m->is_declared_inline(is_inline);
12829  set_member_access_specifier(m, access);
12830  if (vindex != -1)
12832  if (is_virtual)
12833  set_member_function_is_virtual(m, is_virtual);
12834  set_member_is_static(m, is_static);
12835  set_member_function_is_ctor(m, is_ctor);
12836  set_member_function_is_dtor(m, is_dtor);
12837  set_member_function_is_const(m, method_t->get_is_const());
12838 
12840 
12841  if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
12842  {
12843  // This is a virtual member function which has a linkage name
12844  // but has no underlying symbol set.
12845  //
12846  // The underlying elf symbol to set to this function can show up
12847  // later in the DWARF input or it can be that, because of some
12848  // compiler optimization, the relation between this function and
12849  // its underlying elf symbol is simply not emitted in the DWARF.
12850  //
12851  // Let's thus schedule this function for a later fixup pass
12852  // (performed by
12853  // reader::fixup_functions_with_no_symbols()) that will
12854  // set its underlying symbol.
12855  //
12856  // Note that if the underying symbol is encountered later in the
12857  // DWARF input, then the part of build_function_decl() that
12858  // updates the function to set its underlying symbol will
12859  // de-schedule this function wrt fixup pass.
12860  Dwarf_Off die_offset = dwarf_dieoffset(die);
12861  die_function_decl_map_type &fns_with_no_symbol =
12862  rdr.die_function_decl_with_no_symbol_map();
12863  die_function_decl_map_type::const_iterator i =
12864  fns_with_no_symbol.find(die_offset);
12865  if (i == fns_with_no_symbol.end())
12866  fns_with_no_symbol[die_offset] = f;
12867  }
12868 
12869 }
12870 
12871 /// If a function DIE has attributes which have not yet been read and
12872 /// added to the internal representation that represents that function
12873 /// then read those extra attributes and update the internal
12874 /// representation.
12875 ///
12876 /// @param rdr the DWARF reader to use.
12877 ///
12878 /// @param die the function DIE to consider.
12879 ///
12880 /// @param where_offset where we logical are, currently, in the stream
12881 /// of DIEs. If you don't know what this is, you can just set it to zero.
12882 ///
12883 /// @param existing_fn the representation of the function to update.
12884 ///
12885 /// @return the updated function representation.
12886 static function_decl_sptr
12887 maybe_finish_function_decl_reading(reader& rdr,
12888  Dwarf_Die* die,
12889  size_t where_offset,
12890  const function_decl_sptr& existing_fn)
12891 {
12892  function_decl_sptr result = build_function_decl(rdr, die,
12893  where_offset,
12894  existing_fn);
12895 
12896  return result;
12897 }
12898 
12899 /// Lookup a class or a typedef with a given qualified name in the
12900 /// corpus that a given scope belongs to.
12901 ///
12902 /// @param scope the scope to consider.
12903 ///
12904 /// @param type_name the qualified name of the type to look for.
12905 ///
12906 /// @return the typedef or class type found.
12907 static type_base_sptr
12908 lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
12909 {
12910  string qname = build_qualified_name(scope, type_name);
12911  corpus* corp = scope->get_corpus();
12912  type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
12913  return result;
12914 }
12915 
12916 /// Lookup a class of typedef type from the current corpus being
12917 /// constructed.
12918 ///
12919 /// The type being looked for has the same name as a given DIE.
12920 ///
12921 /// @param rdr the DWARF reader to use.
12922 ///
12923 /// @param die the DIE which has the same name as the type we are
12924 /// looking for.
12925 ///
12926 /// @param called_for_public_decl whether this function is being
12927 /// called from a a publicly defined declaration.
12928 ///
12929 /// @param where_offset where we are logically at in the DIE stream.
12930 ///
12931 /// @return the type found.
12932 static type_base_sptr
12933 lookup_class_or_typedef_from_corpus(reader& rdr,
12934  Dwarf_Die* die,
12935  bool called_for_public_decl,
12936  size_t where_offset)
12937 {
12938  if (!die)
12939  return class_decl_sptr();
12940 
12941  string class_name = die_string_attribute(die, DW_AT_name);
12942  if (class_name.empty())
12943  return class_decl_sptr();
12944 
12945  scope_decl_sptr scope = get_scope_for_die(rdr, die,
12946  called_for_public_decl,
12947  where_offset);
12948  if (scope)
12949  return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
12950 
12951  return type_base_sptr();
12952 }
12953 
12954 /// Lookup a class, typedef or enum type with a given qualified name
12955 /// in the corpus that a given scope belongs to.
12956 ///
12957 /// @param scope the scope to consider.
12958 ///
12959 /// @param type_name the qualified name of the type to look for.
12960 ///
12961 /// @return the typedef, enum or class type found.
12962 static type_base_sptr
12963 lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
12964  const string& type_name)
12965 {
12966  string qname = build_qualified_name(scope, type_name);
12967  corpus* corp = scope->get_corpus();
12968  type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
12969  return result;
12970 }
12971 
12972 /// Lookup a class, typedef or enum type in a given scope, in the
12973 /// corpus that scope belongs to.
12974 ///
12975 /// @param die the DIE of the class, typedef or enum to lookup.
12976 ///
12977 /// @param anonymous_member_type_idx if @p DIE represents an anonymous
12978 /// type, this is the index of that anonymous type in its scope, in
12979 /// case there are several anonymous types of the same kind in that
12980 /// scope.
12981 ///
12982 /// @param scope the scope in which to look the type for.
12983 ///
12984 /// @return the typedef, enum or class type found.
12985 static type_base_sptr
12986 lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
12987  size_t anonymous_member_type_idx,
12988  scope_decl* scope)
12989 {
12990  if (!die)
12991  return class_decl_sptr();
12992 
12993  string type_name = die_string_attribute(die, DW_AT_name);
12994  if (is_anonymous_type_die(die))
12995  type_name =
12996  get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12997 
12998  if (type_name.empty())
12999  return class_decl_sptr();
13000 
13001  return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
13002 }
13003 
13004 
13005 /// Test if a DIE represents a function that is a member of a given
13006 /// class type.
13007 ///
13008 /// @param rdr the DWARF reader.
13009 ///
13010 /// @param function_die the DIE of the function to consider.
13011 ///
13012 /// @param class_type the class type to consider.
13013 ///
13014 /// @param where_offset where we are logically at in the DIE stream.
13015 ///
13016 /// @return the method declaration corresponding to the member
13017 /// function of @p class_type, iff @p function_die is for a member
13018 /// function of @p class_type.
13019 static method_decl_sptr
13020 is_function_for_die_a_member_of_class(reader& rdr,
13021  Dwarf_Die* function_die,
13022  const class_or_union_sptr& class_type)
13023 {
13024  type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
13025 
13026  if (!artifact)
13027  return method_decl_sptr();
13028 
13029  method_decl_sptr method = is_method_decl(artifact);
13030  method_type_sptr method_type;
13031 
13032  if (method)
13033  method_type = method->get_type();
13034  else
13035  method_type = is_method_type(artifact);
13036  ABG_ASSERT(method_type);
13037 
13038  class_or_union_sptr method_class = method_type->get_class_type();
13039  ABG_ASSERT(method_class);
13040 
13041  string method_class_name = method_class->get_qualified_name(),
13042  class_type_name = class_type->get_qualified_name();
13043 
13044  if (method_class_name == class_type_name)
13045  {
13046  //ABG_ASSERT(class_type.get() == method_class.get());
13047  return method;
13048  }
13049 
13050  return method_decl_sptr();
13051 }
13052 
13053 /// If a given function DIE represents an existing member function of
13054 /// a given class, then update that member function with new
13055 /// properties present in the DIE. Otherwise, if the DIE represents a
13056 /// new member function that is not already present in the class then
13057 /// add that new member function to the class.
13058 ///
13059 /// @param rdr the DWARF reader.
13060 ///
13061 /// @param function_die the DIE of the potential member function to
13062 /// consider.
13063 ///
13064 /// @param class_type the class type to consider.
13065 ///
13066 /// @param called_from_public_decl is true iff this function was
13067 /// called from a publicly defined and exported declaration.
13068 ///
13069 /// @param where_offset where we are logically at in the DIE stream.
13070 ///
13071 /// @return the method decl representing the member function.
13072 static method_decl_sptr
13073 add_or_update_member_function(reader& rdr,
13074  Dwarf_Die* function_die,
13075  const class_or_union_sptr& class_type,
13076  bool called_from_public_decl,
13077  size_t where_offset)
13078 {
13079  method_decl_sptr method =
13080  is_function_for_die_a_member_of_class(rdr, function_die, class_type);
13081 
13082  if (!method)
13083  method = is_method_decl(build_ir_node_from_die(rdr, function_die,
13084  class_type.get(),
13085  called_from_public_decl,
13086  where_offset));
13087  if (!method)
13088  return method_decl_sptr();
13089 
13090  finish_member_function_reading(function_die,
13091  is_function_decl(method),
13092  class_type, rdr);
13093  return method;
13094 }
13095 
13096 /// Build a an IR node for class type from a DW_TAG_structure_type or
13097 /// DW_TAG_class_type DIE and add that node to the ABI corpus being
13098 /// currently built.
13099 ///
13100 /// If the represents class type that already exists, then update the
13101 /// existing class type with the new properties found in the DIE.
13102 ///
13103 /// It meanst that this function can also update an existing
13104 /// class_decl node with data members, member functions and other
13105 /// properties coming from the DIE.
13106 ///
13107 /// @param rdr the DWARF reader to consider.
13108 ///
13109 /// @param die the DIE to read information from. Must be either a
13110 /// DW_TAG_structure_type or a DW_TAG_class_type.
13111 ///
13112 /// @param scope a pointer to the scope_decl* under which this class
13113 /// is to be added to.
13114 ///
13115 /// @param is_struct whether the class was declared as a struct.
13116 ///
13117 /// @param klass if non-null, this is a klass to append the members
13118 /// to. Otherwise, this function just builds the class from scratch.
13119 ///
13120 /// @param called_from_public_decl set to true if this class is being
13121 /// called from a "Public declaration like vars or public symbols".
13122 ///
13123 /// @param where_offset the offset of the DIE where we are "logically"
13124 /// positionned at, in the DIE tree. This is useful when @p die is
13125 /// e.g, DW_TAG_partial_unit that can be included in several places in
13126 /// the DIE tree.
13127 ///
13128 /// @param is_declaration_only is true if the DIE denoted by @p die is
13129 /// a declaration-only DIE.
13130 ///
13131 /// @return the resulting class_type.
13132 static class_decl_sptr
13133 add_or_update_class_type(reader& rdr,
13134  Dwarf_Die* die,
13135  scope_decl* scope,
13136  bool is_struct,
13137  class_decl_sptr klass,
13138  bool called_from_public_decl,
13139  size_t where_offset,
13140  bool is_declaration_only)
13141 {
13142  class_decl_sptr result;
13143  if (!die)
13144  return result;
13145 
13146  const die_source source = rdr.get_die_source(die);
13147 
13148  unsigned tag = dwarf_tag(die);
13149 
13150  if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
13151  return result;
13152 
13153  {
13154  die_class_or_union_map_type::const_iterator i =
13155  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13156  if (i != rdr.die_wip_classes_map(source).end())
13157  {
13158  class_decl_sptr class_type = is_class_type(i->second);
13159  ABG_ASSERT(class_type);
13160  return class_type;
13161  }
13162  }
13163 
13164  string name, linkage_name;
13165  location loc;
13166  die_loc_and_name(rdr, die, loc, name, linkage_name);
13167 
13168  bool is_anonymous = false;
13169  if (name.empty())
13170  {
13171  // So we are looking at an anonymous struct. Let's
13172  // give it a name.
13173  name = get_internal_anonymous_die_prefix_name(die);
13174  ABG_ASSERT(!name.empty());
13175  // But we remember that the type is anonymous.
13176  is_anonymous = true;
13177 
13178  if (size_t s = scope->get_num_anonymous_member_classes())
13179  name = build_internal_anonymous_die_name(name, s);
13180  }
13181 
13182  if (!is_anonymous)
13183  {
13184  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13185  {
13186  if (loc)
13187  // TODO: if there is only one class defined in the corpus
13188  // for this location, then re-use it. But if there are
13189  // more than one, then do not re-use it, for now.
13190  result = lookup_class_type_per_location(loc.expand(), *corp);
13191  else
13192  // TODO: if there is just one class for that name defined,
13193  // then re-use it. Otherwise, don't.
13194  result = lookup_class_type(name, *corp);
13195  if (result
13196  // If we are seeing a declaration of a definition we
13197  // already had, or if we are seing a type with the same
13198  // declaration-only-ness that we had before, then keep
13199  // the one we already had.
13200  && (result->get_is_declaration_only() == is_declaration_only
13201  || (!result->get_is_declaration_only()
13202  && is_declaration_only)))
13203  {
13204  rdr.associate_die_to_type(die, result, where_offset);
13205  return result;
13206  }
13207  else
13208  // We might be seeing the definition of a declaration we
13209  // already had. In that case, keep the definition and
13210  // drop the declaration.
13211  result.reset();
13212  }
13213  }
13214 
13215  // If we've already seen the same class as 'die', then let's re-use
13216  // that one, unless it's an anonymous class. We can't really safely
13217  // re-use anonymous classes as they have no name, by construction.
13218  // What we can do, rather, is to reuse the typedef that name them,
13219  // when they do have a naming typedef.
13220  if (!is_anonymous)
13221  if (class_decl_sptr pre_existing_class =
13222  is_class_type(rdr.lookup_type_artifact_from_die(die)))
13223  klass = pre_existing_class;
13224 
13225  uint64_t size = 0;
13226  die_size_in_bits(die, size);
13227  bool is_artificial = die_is_artificial(die);
13228 
13229  Dwarf_Die child;
13230  bool has_child = (dwarf_child(die, &child) == 0);
13231 
13232  decl_base_sptr res;
13233  if (klass)
13234  {
13235  res = result = klass;
13236  if (has_child && klass->get_is_declaration_only()
13237  && klass->get_definition_of_declaration())
13238  res = result = is_class_type(klass->get_definition_of_declaration());
13239  if (loc)
13240  result->set_location(loc);
13241  }
13242  else
13243  {
13244  result.reset(new class_decl(rdr.env(), name, size,
13245  /*alignment=*/0, is_struct, loc,
13246  decl_base::VISIBILITY_DEFAULT,
13247  is_anonymous));
13248 
13249  result->set_is_declaration_only(is_declaration_only);
13250 
13251  res = add_decl_to_scope(result, scope);
13252  result = dynamic_pointer_cast<class_decl>(res);
13253  ABG_ASSERT(result);
13254  }
13255 
13256  if (!klass || klass->get_is_declaration_only())
13257  if (size != result->get_size_in_bits())
13258  result->set_size_in_bits(size);
13259 
13260  if (klass)
13261  // We are amending a class that was built before. So let's check
13262  // if we need to amend its "declaration-only-ness" status.
13263  if (!!result->get_size_in_bits() == result->get_is_declaration_only())
13264  // The size of the class doesn't match its
13265  // 'declaration-only-ness". We might have a non-zero sized
13266  // class which is declaration-only, or a zero sized class that
13267  // is not declaration-only. Let's set the declaration-only-ness
13268  // according to what we are instructed to.
13269  //
13270  // Note however that there are binaries out there emitted by
13271  // compilers (Clang, in C++) emit declarations-only classes that
13272  // have non-zero size. So we must honor these too. That is why
13273  // we are not forcing the declaration-only-ness to false when a
13274  // class has non-zero size. An example of such binary is
13275  // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
13276  result->set_is_declaration_only(is_declaration_only);
13277 
13278  // If a non-decl-only class has children node and is advertized as
13279  // having a non-zero size let's trust that.
13280  if (!result->get_is_declaration_only() && has_child)
13281  if (result->get_size_in_bits() == 0 && size != 0)
13282  result->set_size_in_bits(size);
13283 
13284  result->set_is_artificial(is_artificial);
13285 
13286  rdr.associate_die_to_type(die, result, where_offset);
13287 
13288  rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13289 
13290  if (!has_child)
13291  // TODO: set the access specifier for the declaration-only class
13292  // here.
13293  return result;
13294 
13295  rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13296 
13297  bool is_incomplete_type = false;
13298  if (is_declaration_only && size == 0 && has_child)
13299  // this is an incomplete DWARF type as defined by [5.7.1]
13300  //
13301  // An incomplete structure, union or class type is represented by
13302  // a structure, union or class entry that does not have a byte
13303  // size attribute and that has a DW_AT_declaration attribute.
13304  //
13305  // Let's consider that it's thus a decl-only class, likely
13306  // referred to by a pointer. If we later encounter a definition
13307  // for this decl-only class type, then this decl-only class will
13308  // be resolved to it by the code in
13309  // reader::resolve_declaration_only_classes.
13310  is_incomplete_type = true;
13311 
13312  scope_decl_sptr scop =
13313  dynamic_pointer_cast<scope_decl>(res);
13314  ABG_ASSERT(scop);
13315  rdr.scope_stack().push(scop.get());
13316 
13317  if (has_child && !is_incomplete_type)
13318  {
13319  int anonymous_member_class_index = -1;
13320  int anonymous_member_union_index = -1;
13321  int anonymous_member_enum_index = -1;
13322 
13323  do
13324  {
13325  tag = dwarf_tag(&child);
13326 
13327  // Handle base classes.
13328  if (tag == DW_TAG_inheritance)
13329  {
13330  result->set_is_declaration_only(false);
13331 
13332  Dwarf_Die type_die;
13333  if (!die_die_attribute(&child, DW_AT_type, type_die))
13334  continue;
13335 
13336  type_base_sptr base_type;
13337  if (!(base_type =
13338  lookup_class_or_typedef_from_corpus(rdr, &type_die,
13339  called_from_public_decl,
13340  where_offset)))
13341  {
13342  base_type =
13343  is_type(build_ir_node_from_die(rdr, &type_die,
13344  called_from_public_decl,
13345  where_offset));
13346  }
13347  // Sometimes base_type can be a typedef. Let's make
13348  // sure that typedef is compatible with a class type.
13350  if (!b)
13351  continue;
13352 
13353  access_specifier access =
13354  is_struct
13355  ? public_access
13356  : private_access;
13357 
13358  die_access_specifier(&child, access);
13359 
13360  bool is_virt= die_is_virtual(&child);
13361  int64_t offset = 0;
13362  bool is_offset_present =
13363  die_member_offset(rdr, &child, offset);
13364 
13365  class_decl::base_spec_sptr base(new class_decl::base_spec
13366  (b, access,
13367  is_offset_present ? offset : -1,
13368  is_virt));
13369  if (b->get_is_declaration_only()
13370  // Only non-anonymous decl-only classes are
13371  // scheduled for resolution to their definition.
13372  // Anonymous classes that are decl-only are likely
13373  // only artificially created by
13374  // get_opaque_version_of_type, from anonymous fully
13375  // defined classes. Those are never defined.
13376  && !b->get_qualified_name().empty())
13377  ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
13378  if (result->find_base_class(b->get_qualified_name()))
13379  continue;
13380  result->add_base_specifier(base);
13381  }
13382  // Handle data members.
13383  else if (tag == DW_TAG_member
13384  || tag == DW_TAG_variable)
13385  {
13386  Dwarf_Die type_die;
13387  if (!die_die_attribute(&child, DW_AT_type, type_die))
13388  continue;
13389 
13390  string n, m;
13391  location loc;
13392  die_loc_and_name(rdr, &child, loc, n, m);
13393  /// For now, we skip the hidden vtable pointer.
13394  /// Currently, we're looking for a member starting with
13395  /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
13396  /// use as a name for the hidden vtable pointer.
13397  if (n.substr(0, 5) == "_vptr"
13398  && n.size() > 5
13399  && !std::isalnum(n.at(5))
13400  && n.at(5) != '_')
13401  continue;
13402 
13403  // If the variable is already a member of this class,
13404  // move on. If it's an anonymous data member, we need
13405  // to handle it differently. We'll do that later below.
13406  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13407  continue;
13408 
13409  int64_t offset_in_bits = 0;
13410  bool is_laid_out = die_member_offset(rdr, &child,
13411  offset_in_bits);
13412  // For now, is_static == !is_laid_out. When we have
13413  // templates, we'll try to be more specific. For now,
13414  // this approximation should do OK.
13415  bool is_static = !is_laid_out;
13416 
13417  if (is_static && variable_is_suppressed(rdr,
13418  result.get(),
13419  &child,
13420  is_declaration_only))
13421  continue;
13422 
13423  decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
13424  called_from_public_decl,
13425  where_offset));
13426  type_base_sptr t = is_type(ty);
13427  if (!t)
13428  continue;
13429 
13430  if (n.empty() && !die_is_anonymous_data_member(&child))
13431  {
13432  // We must be in a case where the data member has an
13433  // empty name because the DWARF emitter has a bug.
13434  // Let's generate an artificial name for that data
13435  // member.
13436  n = rdr.build_name_for_buggy_anonymous_data_member(&child);
13437  ABG_ASSERT(!n.empty());
13438  }
13439 
13440  // The call to build_ir_node_from_die above could have
13441  // triggered the adding of a data member named 'n' into
13442  // result. So let's check again if the variable is
13443  // already a member of this class. Here again, if it's
13444  // an anonymous data member, we need to handle it
13445  // differently. We'll do that later below.
13446  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13447  continue;
13448 
13449  if (!is_static)
13450  // We have a non-static data member. So this class
13451  // cannot be a declaration-only class anymore, even if
13452  // some DWARF emitters might consider it otherwise.
13453  result->set_is_declaration_only(false);
13454  access_specifier access =
13455  is_struct
13456  ? public_access
13457  : private_access;
13458 
13459  die_access_specifier(&child, access);
13460 
13461  var_decl_sptr dm(new var_decl(n, t, loc, m));
13462  if (n.empty()
13463  && anonymous_data_member_exists_in_class(*dm, *result))
13464  // dm is an anonymous data member that was already
13465  // present in the current class so let's not add it.
13466  continue;
13467  result->add_data_member(dm, access, is_laid_out,
13468  is_static, offset_in_bits);
13469  ABG_ASSERT(has_scope(dm));
13470  rdr.associate_die_to_decl(&child, dm, where_offset,
13471  /*associate_by_repr=*/false);
13472  }
13473  // Handle member functions;
13474  else if (tag == DW_TAG_subprogram)
13475  {
13476  decl_base_sptr r =
13477  add_or_update_member_function(rdr, &child, result,
13478  called_from_public_decl,
13479  where_offset);
13481  rdr.associate_die_to_decl(&child, f, where_offset,
13482  /*associate_by_repr=*/true);
13483  }
13484  // Handle member types
13485  else if (die_is_type(&child))
13486  {
13487  // Track the anonymous type index in the current
13488  // scope. Look for what this means by reading the
13489  // comment of the function
13490  // build_internal_anonymous_die_name.
13491  int anonymous_member_type_index = 0;
13492  if (is_anonymous_type_die(&child))
13493  {
13494  // Update the anonymous type index.
13495  if (die_is_class_type(&child))
13496  anonymous_member_type_index =
13497  ++anonymous_member_class_index;
13498  else if (dwarf_tag(&child) == DW_TAG_union_type)
13499  anonymous_member_type_index =
13500  ++anonymous_member_union_index;
13501  else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13502  anonymous_member_type_index =
13503  ++anonymous_member_enum_index;
13504  }
13505  // if the type is not already a member of this class,
13506  // then add it to the class.
13507  if ((is_anonymous_type_die(&child)
13508  && !lookup_class_typedef_or_enum_type_from_corpus
13509  (&child, anonymous_member_type_index, result.get()))
13510  || !result->find_member_type(die_name(&child)))
13511  build_ir_node_from_die(rdr, &child, result.get(),
13512  called_from_public_decl,
13513  where_offset);
13514  }
13515  } while (dwarf_siblingof(&child, &child) == 0);
13516  }
13517 
13518  rdr.scope_stack().pop();
13519 
13520  {
13521  die_class_or_union_map_type::const_iterator i =
13522  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13523  if (i != rdr.die_wip_classes_map(source).end())
13524  {
13525  if (is_member_type(i->second))
13527  get_member_access_specifier(i->second));
13528  rdr.die_wip_classes_map(source).erase(i);
13529  }
13530  }
13531 
13532  rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13533  return result;
13534 }
13535 
13536 /// Build an @ref union_decl from a DW_TAG_union_type DIE.
13537 ///
13538 /// @param rdr the DWARF reader to use.
13539 ///
13540 /// @param die the DIE to read from.
13541 ///
13542 /// @param scope the scope the resulting @ref union_decl belongs to.
13543 ///
13544 /// @param union_type if this parameter is non-nil, then this function
13545 /// updates the @ref union_decl that it points to, rather than
13546 /// creating a new @ref union_decl.
13547 ///
13548 /// @param called_from_public_decl is true if this function has been
13549 /// initially called within the context of a public decl.
13550 ///
13551 /// @param where_offset the offset of the DIE where we are "logically"
13552 /// positionned at, in the DIE tree. This is useful when @p die is
13553 /// e.g, DW_TAG_partial_unit that can be included in several places in
13554 /// the DIE tree.
13555 ///
13556 /// @param is_declaration_only is true if the DIE denoted by @p die is
13557 /// a declaration-only DIE.
13558 ///
13559 /// @return the resulting @ref union_decl type.
13560 static union_decl_sptr
13561 add_or_update_union_type(reader& rdr,
13562  Dwarf_Die* die,
13563  scope_decl* scope,
13564  union_decl_sptr union_type,
13565  bool called_from_public_decl,
13566  size_t where_offset,
13567  bool is_declaration_only)
13568 {
13569  union_decl_sptr result;
13570  if (!die)
13571  return result;
13572 
13573  unsigned tag = dwarf_tag(die);
13574 
13575  if (tag != DW_TAG_union_type)
13576  return result;
13577 
13578  const die_source source = rdr.get_die_source(die);
13579  {
13580  die_class_or_union_map_type::const_iterator i =
13581  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13582  if (i != rdr.die_wip_classes_map(source).end())
13583  {
13584  union_decl_sptr u = is_union_type(i->second);
13585  ABG_ASSERT(u);
13586  return u;
13587  }
13588  }
13589 
13590  string name, linkage_name;
13591  location loc;
13592  die_loc_and_name(rdr, die, loc, name, linkage_name);
13593 
13594  bool is_anonymous = false;
13595  if (name.empty())
13596  {
13597  // So we are looking at an anonymous union. Let's give it a
13598  // name.
13599  name = get_internal_anonymous_die_prefix_name(die);
13600  ABG_ASSERT(!name.empty());
13601  // But we remember that the type is anonymous.
13602  is_anonymous = true;
13603 
13604  if (size_t s = scope->get_num_anonymous_member_unions())
13605  name = build_internal_anonymous_die_name(name, s);
13606  }
13607 
13608  // If the type has location, then associate it to its
13609  // representation. This way, all occurences of types with the same
13610  // representation (name) and location can be later detected as being
13611  // for the same type.
13612 
13613  if (!is_anonymous)
13614  {
13615  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13616  {
13617  if (loc)
13618  result = lookup_union_type_per_location(loc.expand(), *corp);
13619  else
13620  result = lookup_union_type(name, *corp);
13621 
13622  if (result)
13623  {
13624  rdr.associate_die_to_type(die, result, where_offset);
13625  return result;
13626  }
13627  }
13628  }
13629 
13630  // if we've already seen a union with the same union as 'die' then
13631  // let's re-use that one. We can't really safely re-use anonymous
13632  // unions as they have no name, by construction. What we can do,
13633  // rather, is to reuse the typedef that name them, when they do have
13634  // a naming typedef.
13635  if (!is_anonymous)
13636  if (union_decl_sptr pre_existing_union =
13637  is_union_type(rdr.lookup_artifact_from_die(die)))
13638  union_type = pre_existing_union;
13639 
13640  uint64_t size = 0;
13641  die_size_in_bits(die, size);
13642  bool is_artificial = die_is_artificial(die);
13643 
13644  if (union_type)
13645  {
13646  result = union_type;
13647  result->set_location(loc);
13648  }
13649  else
13650  {
13651  result.reset(new union_decl(rdr.env(), name, size, loc,
13652  decl_base::VISIBILITY_DEFAULT,
13653  is_anonymous));
13654  if (is_declaration_only)
13655  result->set_is_declaration_only(true);
13656  result = is_union_type(add_decl_to_scope(result, scope));
13657  ABG_ASSERT(result);
13658  }
13659 
13660  if (size)
13661  {
13662  result->set_size_in_bits(size);
13663  result->set_is_declaration_only(false);
13664  }
13665 
13666  result->set_is_artificial(is_artificial);
13667 
13668  rdr.associate_die_to_type(die, result, where_offset);
13669 
13670  rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13671 
13672  Dwarf_Die child;
13673  bool has_child = (dwarf_child(die, &child) == 0);
13674  if (!has_child)
13675  return result;
13676 
13677  rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13678 
13679  scope_decl_sptr scop =
13680  dynamic_pointer_cast<scope_decl>(result);
13681  ABG_ASSERT(scop);
13682  rdr.scope_stack().push(scop.get());
13683 
13684  if (has_child)
13685  {
13686  do
13687  {
13688  tag = dwarf_tag(&child);
13689  // Handle data members.
13690  if (tag == DW_TAG_member || tag == DW_TAG_variable)
13691  {
13692  Dwarf_Die type_die;
13693  if (!die_die_attribute(&child, DW_AT_type, type_die))
13694  continue;
13695 
13696  string n, m;
13697  location loc;
13698  die_loc_and_name(rdr, &child, loc, n, m);
13699 
13700  // Because we can be updating an existing union, let's
13701  // make sure we don't already have a member of the same
13702  // name. Anonymous member are handled a bit later below
13703  // so let's not consider them here.
13704  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13705  continue;
13706 
13707  ssize_t offset_in_bits = 0;
13708  decl_base_sptr ty =
13709  is_decl(build_ir_node_from_die(rdr, &type_die,
13710  called_from_public_decl,
13711  where_offset));
13712  type_base_sptr t = is_type(ty);
13713  if (!t)
13714  continue;
13715 
13716  // We have a non-static data member. So this union
13717  // cannot be a declaration-only union anymore, even if
13718  // some DWARF emitters might consider it otherwise.
13719  result->set_is_declaration_only(false);
13720  access_specifier access = public_access;
13721 
13722  die_access_specifier(&child, access);
13723 
13724  var_decl_sptr dm(new var_decl(n, t, loc, m));
13725  // If dm is an anonymous data member, let's make sure
13726  // the current union doesn't already have it as a data
13727  // member.
13728  if (n.empty() && result->find_data_member(dm))
13729  continue;
13730 
13731  if (!n.empty() && lookup_var_decl_in_scope(n, result))
13732  continue;
13733 
13734  result->add_data_member(dm, access, /*is_laid_out=*/true,
13735  /*is_static=*/false,
13736  offset_in_bits);
13737  ABG_ASSERT(has_scope(dm));
13738  rdr.associate_die_to_decl(&child, dm, where_offset,
13739  /*associate_by_repr=*/false);
13740  }
13741  // Handle member functions;
13742  else if (tag == DW_TAG_subprogram)
13743  {
13744  decl_base_sptr r =
13745  is_decl(build_ir_node_from_die(rdr, &child,
13746  result.get(),
13747  called_from_public_decl,
13748  where_offset));
13749  if (!r)
13750  continue;
13751 
13752  function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13753  ABG_ASSERT(f);
13754 
13755  finish_member_function_reading(&child, f, result, rdr);
13756 
13757  rdr.associate_die_to_decl(&child, f, where_offset,
13758  /*associate_by_repr=*/false);
13759  }
13760  // Handle member types
13761  else if (die_is_type(&child))
13762  decl_base_sptr td =
13763  is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13764  called_from_public_decl,
13765  where_offset));
13766  } while (dwarf_siblingof(&child, &child) == 0);
13767  }
13768 
13769  rdr.scope_stack().pop();
13770 
13771  {
13772  die_class_or_union_map_type::const_iterator i =
13773  rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13774  if (i != rdr.die_wip_classes_map(source).end())
13775  {
13776  if (is_member_type(i->second))
13778  get_member_access_specifier(i->second));
13779  rdr.die_wip_classes_map(source).erase(i);
13780  }
13781  }
13782 
13783  return result;
13784 }
13785 
13786 /// build a qualified type from a DW_TAG_const_type,
13787 /// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13788 ///
13789 /// @param rdr the DWARF reader to consider.
13790 ///
13791 /// @param die the input DIE to read from.
13792 ///
13793 /// @param called_from_public_decl true if this function was called
13794 /// from a context where either a public function or a public variable
13795 /// is being built.
13796 ///
13797 /// @param where_offset the offset of the DIE where we are "logically"
13798 /// positionned at, in the DIE tree. This is useful when @p die is
13799 /// e.g, DW_TAG_partial_unit that can be included in several places in
13800 /// the DIE tree.
13801 ///
13802 /// @return the resulting qualified_type_def.
13803 static type_base_sptr
13804 build_qualified_type(reader& rdr,
13805  Dwarf_Die* die,
13806  bool called_from_public_decl,
13807  size_t where_offset)
13808 {
13809  type_base_sptr result;
13810  if (!die)
13811  return result;
13812 
13813  unsigned tag = dwarf_tag(die);
13814 
13815  if (tag != DW_TAG_const_type
13816  && tag != DW_TAG_volatile_type
13817  && tag != DW_TAG_restrict_type)
13818  return result;
13819 
13820  Dwarf_Die underlying_type_die;
13821  decl_base_sptr utype_decl;
13822  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13823  // So, if no DW_AT_type is present, then this means (if we are
13824  // looking at a debug info emitted by GCC) that we are looking
13825  // at a qualified void type.
13826  utype_decl = build_ir_node_for_void_type(rdr);
13827 
13828  if (!utype_decl)
13829  utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
13830  called_from_public_decl,
13831  where_offset));
13832  if (!utype_decl)
13833  return result;
13834 
13835  // The call to build_ir_node_from_die() could have triggered the
13836  // creation of the type for this DIE. In that case, just return it.
13837  if (type_base_sptr t = rdr.lookup_type_from_die(die))
13838  {
13839  result = t;
13840  rdr.associate_die_to_type(die, result, where_offset);
13841  return result;
13842  }
13843 
13844  type_base_sptr utype = is_type(utype_decl);
13845  ABG_ASSERT(utype);
13846 
13847  qualified_type_def::CV qual = qualified_type_def::CV_NONE;
13848  if (tag == DW_TAG_const_type)
13849  qual |= qualified_type_def::CV_CONST;
13850  else if (tag == DW_TAG_volatile_type)
13851  qual |= qualified_type_def::CV_VOLATILE;
13852  else if (tag == DW_TAG_restrict_type)
13853  qual |= qualified_type_def::CV_RESTRICT;
13854  else
13856 
13857  if (!result)
13858  result.reset(new qualified_type_def(utype, qual, location()));
13859 
13860  rdr.associate_die_to_type(die, result, where_offset);
13861 
13862  return result;
13863 }
13864 
13865 /// Walk a tree of typedef of qualified arrays and schedule all type
13866 /// nodes for canonicalization.
13867 ///
13868 /// This is to be used after an array tree has been cloned. In that
13869 /// case, the newly cloned type nodes have to be scheduled for
13870 /// canonicalization.
13871 ///
13872 /// This is a subroutine of maybe_strip_qualification.
13873 ///
13874 /// @param t the type node to be scheduled for canonicalization.
13875 ///
13876 /// @param rdr the DWARF reader to use.
13877 static void
13878 schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
13879  reader &rdr)
13880 {
13881  if (typedef_decl_sptr type = is_typedef(t))
13882  {
13883  schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13884  rdr);
13885  rdr.schedule_type_for_late_canonicalization(t);
13886  }
13887  else if (qualified_type_def_sptr type = is_qualified_type(t))
13888  {
13889  schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13890  rdr);
13891  rdr.schedule_type_for_late_canonicalization(t);
13892  }
13893  else if (array_type_def_sptr type = is_array_type(t))
13894  {
13895  for (vector<array_type_def::subrange_sptr>::const_iterator i =
13896  type->get_subranges().begin();
13897  i != type->get_subranges().end();
13898  ++i)
13899  {
13900  if (!(*i)->get_scope())
13901  add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
13902  rdr.schedule_type_for_late_canonicalization(*i);
13903 
13904  }
13905  schedule_array_tree_for_late_canonicalization(type->get_element_type(),
13906  rdr);
13907  rdr.schedule_type_for_late_canonicalization(type);
13908  }
13909 }
13910 
13911 /// Strip qualification from a qualified type, when it makes sense.
13912 ///
13913 /// DWARF constructs "const reference". This is redundant because a
13914 /// reference is always const. The issue is these redundant types then
13915 /// leak into the IR and make for bad diagnostics.
13916 ///
13917 /// This function thus strips the const qualifier from the type in
13918 /// that case. It might contain code to strip other cases like this
13919 /// in the future.
13920 ///
13921 /// @param t the type to strip const qualification from.
13922 ///
13923 /// @param rdr the @ref reader to use.
13924 ///
13925 /// @return the stripped type or just return @p t.
13926 static decl_base_sptr
13927 maybe_strip_qualification(const qualified_type_def_sptr t,
13928  reader &rdr)
13929 {
13930  if (!t)
13931  return t;
13932 
13933  decl_base_sptr result = t;
13934  type_base_sptr u = t->get_underlying_type();
13935 
13938  if (result.get() != t.get())
13939  return result;
13940 
13941  if (is_array_type(u) || is_typedef_of_array(u))
13942  {
13943  array_type_def_sptr array;
13944  scope_decl * scope = 0;
13945  if ((array = is_array_type(u)))
13946  {
13947  scope = array->get_scope();
13948  ABG_ASSERT(scope);
13949  array = is_array_type(clone_array_tree(array));
13950  schedule_array_tree_for_late_canonicalization(array, rdr);
13951  add_decl_to_scope(array, scope);
13952  t->set_underlying_type(array);
13953  u = t->get_underlying_type();
13954  }
13955  else if (is_typedef_of_array(u))
13956  {
13957  scope = is_decl(u)->get_scope();
13958  ABG_ASSERT(scope);
13959  typedef_decl_sptr typdef =
13961  schedule_array_tree_for_late_canonicalization(typdef, rdr);
13962  ABG_ASSERT(typdef);
13963  add_decl_to_scope(typdef, scope);
13964  t->set_underlying_type(typdef);
13965  u = t->get_underlying_type();
13966  array = is_typedef_of_array(u);
13967  }
13968  else
13970 
13971  ABG_ASSERT(array);
13972  // We should not be editing types that are already canonicalized.
13973  ABG_ASSERT(!array->get_canonical_type());
13974  type_base_sptr element_type = array->get_element_type();
13975 
13976  if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
13977  {
13978  // We should not be editing types that are already canonicalized.
13979  ABG_ASSERT(!qualified->get_canonical_type());
13980  qualified_type_def::CV quals = qualified->get_cv_quals();
13981  quals |= t->get_cv_quals();
13982  qualified->set_cv_quals(quals);
13984  result = is_decl(u);
13985  }
13986  else
13987  {
13988  qualified_type_def_sptr qual_type
13989  (new qualified_type_def(element_type,
13990  t->get_cv_quals(),
13991  t->get_location()));
13993  add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
13994  array->set_element_type(qual_type);
13995  rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
13996  result = is_decl(u);
13997  }
13998  }
13999 
14000  return result;
14001 }
14002 
14003 /// Build a pointer type from a DW_TAG_pointer_type DIE.
14004 ///
14005 /// @param rdr the DWARF reader to consider.
14006 ///
14007 /// @param die the DIE to read information from.
14008 ///
14009 /// @param called_from_public_decl true if this function was called
14010 /// from a context where either a public function or a public variable
14011 /// is being built.
14012 ///
14013 /// @param where_offset the offset of the DIE where we are "logically"
14014 /// positionned at, in the DIE tree. This is useful when @p die is
14015 /// e.g, DW_TAG_partial_unit that can be included in several places in
14016 /// the DIE tree.
14017 ///
14018 /// @return the resulting pointer to pointer_type_def.
14019 static pointer_type_def_sptr
14020 build_pointer_type_def(reader& rdr,
14021  Dwarf_Die* die,
14022  bool called_from_public_decl,
14023  size_t where_offset)
14024 {
14025  pointer_type_def_sptr result;
14026 
14027  if (!die)
14028  return result;
14029 
14030  unsigned tag = dwarf_tag(die);
14031  if (tag != DW_TAG_pointer_type)
14032  return result;
14033 
14034  type_or_decl_base_sptr utype_decl;
14035  Dwarf_Die underlying_type_die;
14036  bool has_underlying_type_die = false;
14037  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14038  // If the DW_AT_type attribute is missing, that means we are
14039  // looking at a pointer to "void".
14040  utype_decl = build_ir_node_for_void_type(rdr);
14041  else
14042  has_underlying_type_die = true;
14043 
14044  if (!utype_decl && has_underlying_type_die)
14045  utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
14046  called_from_public_decl,
14047  where_offset);
14048  if (!utype_decl)
14049  return result;
14050 
14051  // The call to build_ir_node_from_die() could have triggered the
14052  // creation of the type for this DIE. In that case, just return it.
14053  if (type_base_sptr t = rdr.lookup_type_from_die(die))
14054  {
14055  result = is_pointer_type(t);
14056  ABG_ASSERT(result);
14057  return result;
14058  }
14059 
14060  type_base_sptr utype = is_type(utype_decl);
14061  ABG_ASSERT(utype);
14062 
14063  // if the DIE for the pointer type doesn't have a byte_size
14064  // attribute then we assume the size of the pointer is the address
14065  // size of the current translation unit.
14066  uint64_t size = rdr.cur_transl_unit()->get_address_size();
14067  if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14068  // The size as expressed by DW_AT_byte_size is in byte, so let's
14069  // convert it to bits.
14070  size *= 8;
14071 
14072  // And the size of the pointer must be the same as the address size
14073  // of the current translation unit.
14074  ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
14075 
14076  result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
14077  ABG_ASSERT(result->get_pointed_to_type());
14078 
14079  if (is_void_pointer_type(result))
14080  result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
14081 
14082  rdr.associate_die_to_type(die, result, where_offset);
14083  return result;
14084 }
14085 
14086 /// Build a reference type from either a DW_TAG_reference_type or
14087 /// DW_TAG_rvalue_reference_type DIE.
14088 ///
14089 /// @param rdr the DWARF reader to consider.
14090 ///
14091 /// @param die the DIE to read from.
14092 ///
14093 /// @param called_from_public_decl true if this function was called
14094 /// from a context where either a public function or a public variable
14095 /// is being built.
14096 ///
14097 /// @param where_offset the offset of the DIE where we are "logically"
14098 /// positionned at, in the DIE tree. This is useful when @p die is
14099 /// e.g, DW_TAG_partial_unit that can be included in several places in
14100 /// the DIE tree.
14101 ///
14102 /// @return a pointer to the resulting reference_type_def.
14104 build_reference_type(reader& rdr,
14105  Dwarf_Die* die,
14106  bool called_from_public_decl,
14107  size_t where_offset)
14108 {
14109  reference_type_def_sptr result;
14110 
14111  if (!die)
14112  return result;
14113 
14114  unsigned tag = dwarf_tag(die);
14115  if (tag != DW_TAG_reference_type
14116  && tag != DW_TAG_rvalue_reference_type)
14117  return result;
14118 
14119  Dwarf_Die underlying_type_die;
14120  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14121  return result;
14122 
14123  type_or_decl_base_sptr utype_decl =
14124  build_ir_node_from_die(rdr, &underlying_type_die,
14125  called_from_public_decl,
14126  where_offset);
14127  if (!utype_decl)
14128  return result;
14129 
14130  // The call to build_ir_node_from_die() could have triggered the
14131  // creation of the type for this DIE. In that case, just return it.
14132  if (type_base_sptr t = rdr.lookup_type_from_die(die))
14133  {
14134  result = is_reference_type(t);
14135  ABG_ASSERT(result);
14136  return result;
14137  }
14138 
14139  type_base_sptr utype = is_type(utype_decl);
14140  ABG_ASSERT(utype);
14141 
14142  // if the DIE for the reference type doesn't have a byte_size
14143  // attribute then we assume the size of the reference is the address
14144  // size of the current translation unit.
14145  uint64_t size = rdr.cur_transl_unit()->get_address_size();
14146  if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14147  size *= 8;
14148 
14149  // And the size of the pointer must be the same as the address size
14150  // of the current translation unit.
14151  ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
14152 
14153  bool is_lvalue = tag == DW_TAG_reference_type;
14154 
14155  result.reset(new reference_type_def(utype, is_lvalue, size,
14156  /*alignment=*/0,
14157  location()));
14158  if (corpus_sptr corp = rdr.corpus())
14159  if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
14160  result = t;
14161  rdr.associate_die_to_type(die, result, where_offset);
14162  return result;
14163 }
14164 
14165 /// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
14166 /// DW_TAG_ptr_to_member_type.
14167 ///
14168 /// @param the DWARF reader touse.
14169 ///
14170 /// @param the DIE to consider. It must carry the tag
14171 /// DW_TAG_ptr_to_member_type.
14172 ///
14173 /// @param called_from_public_decl true if this function was called
14174 /// from a context where either a public function or a public variable
14175 /// is being built.
14176 ///
14177 /// @param where_offset the offset of the DIE where we are "logically"
14178 /// positionned at, in the DIE tree. This is useful when @p die is
14179 /// e.g, DW_TAG_partial_unit that can be included in several places in
14180 /// the DIE tree.
14181 ///
14182 /// @return a pointer to the resulting @ref ptr_to_mbr_type.
14183 static ptr_to_mbr_type_sptr
14184 build_ptr_to_mbr_type(reader& rdr,
14185  Dwarf_Die* die,
14186  bool called_from_public_decl,
14187  size_t where_offset)
14188 {
14189  ptr_to_mbr_type_sptr result;
14190 
14191  if (!die)
14192  return result;
14193 
14194  unsigned tag = dwarf_tag(die);
14195  if (tag != DW_TAG_ptr_to_member_type)
14196  return result;
14197 
14198  Dwarf_Die data_member_type_die, containing_type_die;
14199 
14200  if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
14201  || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
14202  return result;
14203 
14204  type_or_decl_base_sptr data_member_type =
14205  build_ir_node_from_die(rdr, &data_member_type_die,
14206  called_from_public_decl, where_offset);
14207  if (!data_member_type)
14208  return result;
14209 
14210  type_or_decl_base_sptr containing_type =
14211  build_ir_node_from_die(rdr, &containing_type_die,
14212  called_from_public_decl, where_offset);
14213  if (!containing_type)
14214  return result;
14215 
14217  (is_type(containing_type)))
14218  return result;
14219 
14220  if (type_base_sptr t = rdr.lookup_type_from_die(die))
14221  {
14222  result = is_ptr_to_mbr_type(t);
14223  ABG_ASSERT(result);
14224  return result;
14225  }
14226 
14227  uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
14228 
14229  result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
14230  is_type(data_member_type),
14231  is_type(containing_type),
14232  size_in_bits,
14233  /*alignment=*/0,
14234  location()));
14235 
14236  rdr.associate_die_to_type(die, result, where_offset);
14237  return result;
14238 }
14239 
14240 /// Build a subroutine type from a DW_TAG_subroutine_type DIE.
14241 ///
14242 /// @param rdr the DWARF reader to consider.
14243 ///
14244 /// @param die the DIE to read from.
14245 ///
14246 /// @param is_method points to a class or union declaration iff we're
14247 /// building the type for a method. This is the enclosing class or
14248 /// union of the method.
14249 ///
14250 /// @param where_offset the offset of the DIE where we are "logically"
14251 /// positioned at, in the DIE tree. This is useful when @p die is
14252 /// e.g, DW_TAG_partial_unit that can be included in several places in
14253 /// the DIE tree.
14254 ///
14255 /// @return a pointer to the resulting function_type_sptr.
14256 static function_type_sptr
14257 build_function_type(reader& rdr,
14258  Dwarf_Die* die,
14259  class_or_union_sptr is_method,
14260  size_t where_offset)
14261 {
14262  function_type_sptr result;
14263 
14264  if (!die)
14265  return result;
14266 
14267  ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
14268  || dwarf_tag(die) == DW_TAG_subprogram);
14269 
14270  const die_source source = rdr.get_die_source(die);
14271 
14272  {
14273  size_t off = dwarf_dieoffset(die);
14274  auto i = rdr.die_wip_function_types_map(source).find(off);
14275  if (i != rdr.die_wip_function_types_map(source).end())
14276  {
14277  function_type_sptr fn_type = is_function_type(i->second);
14278  ABG_ASSERT(fn_type);
14279  return fn_type;
14280  }
14281  }
14282 
14283  decl_base_sptr type_decl;
14284 
14285  translation_unit_sptr tu = rdr.cur_transl_unit();
14286  ABG_ASSERT(tu);
14287 
14288  /// If, inside the current translation unit, we've already seen a
14289  /// function type with the same text representation, then reuse that
14290  /// one instead.
14291  if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
14292  {
14293  result = is_function_type(t);
14294  ABG_ASSERT(result);
14295  rdr.associate_die_to_type(die, result, where_offset);
14296  return result;
14297  }
14298 
14299  bool odr_is_relevant = rdr.odr_is_relevant(die);
14300  if (odr_is_relevant)
14301  {
14302  // So we can rely on the One Definition Rule to say that if
14303  // several different function types have the same name (or
14304  // rather, representation) across the entire binary, then they
14305  // ought to designate the same function type. So let's ensure
14306  // that if we've already seen a function type with the same
14307  // representation as the function type 'die', then it's the same
14308  // type as the one denoted by 'die'.
14309  if (function_type_sptr fn_type =
14310  is_function_type(rdr.lookup_type_artifact_from_die(die)))
14311  {
14312  rdr.associate_die_to_type(die, fn_type, where_offset);
14313  return fn_type;
14314  }
14315  }
14316 
14317  // Let's look at the DIE to detect if it's the DIE for a method
14318  // (type). If it is, we can deduce the name of its enclosing class
14319  // and if it's a static or const.
14320  bool is_const = false;
14321  bool is_static = false;
14322  Dwarf_Die object_pointer_die;
14323  Dwarf_Die class_type_die;
14324  bool has_this_parm_die =
14325  die_function_type_is_method_type(rdr, die, where_offset,
14326  object_pointer_die,
14327  class_type_die,
14328  is_static);
14329  if (has_this_parm_die)
14330  {
14331  // The function (type) has a "this" parameter DIE. It means it's
14332  // a member function DIE.
14333  if (!is_static)
14334  if (die_object_pointer_is_for_const_method(&object_pointer_die))
14335  is_const = true;
14336 
14337  if (!is_method)
14338  {
14339  // We were initially called as if the function represented
14340  // by DIE was *NOT* a member function. But now we know it's
14341  // a member function. Let's take that into account.
14342  class_or_union_sptr klass_type =
14343  is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
14344  /*called_from_pub_decl=*/true,
14345  where_offset));
14346  ABG_ASSERT(klass_type);
14347  is_method = klass_type;
14348  }
14349  }
14350 
14351  // Let's create the type early and record it as being for the DIE
14352  // 'die'. This way, when building the sub-type triggers the
14353  // creation of a type matching the same 'die', then we'll reuse this
14354  // one.
14355 
14356  result.reset(is_method
14357  ? new method_type(is_method, is_const,
14358  tu->get_address_size(),
14359  /*alignment=*/0)
14360  : new function_type(rdr.env(), tu->get_address_size(),
14361  /*alignment=*/0));
14362  rdr.associate_die_to_type(die, result, where_offset);
14363  rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
14364 
14365  type_base_sptr return_type;
14366  Dwarf_Die ret_type_die;
14367  if (die_die_attribute(die, DW_AT_type, ret_type_die))
14368  return_type =
14369  is_type(build_ir_node_from_die(rdr, &ret_type_die,
14370  /*called_from_public_decl=*/true,
14371  where_offset));
14372  if (!return_type)
14373  return_type = is_type(build_ir_node_for_void_type(rdr));
14374  result->set_return_type(return_type);
14375 
14376  Dwarf_Die child;
14377  function_decl::parameters function_parms;
14378 
14379  if (dwarf_child(die, &child) == 0)
14380  do
14381  {
14382  int child_tag = dwarf_tag(&child);
14383  if (child_tag == DW_TAG_formal_parameter)
14384  {
14385  // This is a "normal" function parameter.
14386  string name, linkage_name;
14387  location loc;
14388  die_loc_and_name(rdr, &child, loc, name, linkage_name);
14390  // Sometimes, bogus compiler emit names that are
14391  // non-ascii garbage. Let's just ditch that for now.
14392  name.clear();
14393  bool is_artificial = die_is_artificial(&child);
14394  type_base_sptr parm_type;
14395  Dwarf_Die parm_type_die;
14396  if (die_die_attribute(&child, DW_AT_type, parm_type_die))
14397  parm_type =
14398  is_type(build_ir_node_from_die(rdr, &parm_type_die,
14399  /*called_from_public_decl=*/true,
14400  where_offset));
14401  if (!parm_type)
14402  continue;
14403  if (is_method
14404  && is_const_qualified_type(parm_type)
14405  && function_parms.empty())
14406  // We are looking at the first (implicit) parameter of a
14407  // method. This is basically the "this pointer". For
14408  // concrete instances of abstract methods, GCC sometimes
14409  // represents that pointer as a const pointer, whereas
14410  // in the abstract interface representing that method
14411  // the this-pointer is represented as a non-qualified
14412  // pointer. Let's trim the const qualifier away. That
14413  // will minize the chance to have spurious
14414  // const-qualifier changes on implicit parameters when
14415  // comparing methods that otherwise have no meaningful
14416  // ABI changes.
14417  parm_type =
14419 
14421  (new function_decl::parameter(parm_type, name, loc,
14422  /*variadic_marker=*/false,
14423  is_artificial));
14424  function_parms.push_back(p);
14425  }
14426  else if (child_tag == DW_TAG_unspecified_parameters)
14427  {
14428  // This is a variadic function parameter.
14429  bool is_artificial = die_is_artificial(&child);
14430 
14431  type_base_sptr parm_type =
14432  is_type(build_ir_node_for_variadic_parameter_type(rdr));
14434  (new function_decl::parameter(parm_type,
14435  /*name=*/"",
14436  location(),
14437  /*variadic_marker=*/true,
14438  is_artificial));
14439  function_parms.push_back(p);
14440  // After a DW_TAG_unspecified_parameters tag, we shouldn't
14441  // keep reading for parameters. The
14442  // unspecified_parameters TAG should be the last parameter
14443  // that we record. For instance, if there are multiple
14444  // DW_TAG_unspecified_parameters DIEs then we should care
14445  // only for the first one.
14446  break;
14447  }
14448  }
14449  while (dwarf_siblingof(&child, &child) == 0);
14450 
14451  result->set_parameters(function_parms);
14452 
14453  tu->bind_function_type_life_time(result);
14454 
14455  result->set_is_artificial(true);
14456 
14457  rdr.associate_die_repr_to_fn_type_per_tu(die, result);
14458 
14459  {
14460  die_function_type_map_type::const_iterator i =
14461  rdr.die_wip_function_types_map(source).
14462  find(dwarf_dieoffset(die));
14463  if (i != rdr.die_wip_function_types_map(source).end())
14464  rdr.die_wip_function_types_map(source).erase(i);
14465  }
14466 
14467  maybe_canonicalize_type(result, rdr);
14468  return result;
14469 }
14470 
14471 /// Build a subrange type from a DW_TAG_subrange_type.
14472 ///
14473 /// @param rdr the DWARF reader to consider.
14474 ///
14475 /// @param die the DIE to read from.
14476 ///
14477 /// @param where_offset the offset of the DIE where we are "logically"
14478 /// positionned at in the DIE tree. This is useful when @p die is
14479 /// e,g, DW_TAG_partial_unit that can be included in several places in
14480 /// the DIE tree.
14481 ///
14482 /// @param associate_die_to_type if this is true then the resulting
14483 /// type is associated to the @p die, so that next time when the
14484 /// system looks up the type associated to it, the current resulting
14485 /// type is returned. If false, then no association is done and the
14486 /// resulting type can be destroyed right after. This can be useful
14487 /// when the sole purpose of building the @ref
14488 /// array_type_def::subrange_type is to use some of its method like,
14489 /// e.g, its name pretty printing methods.
14490 ///
14491 /// @return the newly built instance of @ref
14492 /// array_type_def::subrange_type, or nil if no type could be built.
14494 build_subrange_type(reader& rdr,
14495  const Dwarf_Die* die,
14496  size_t where_offset,
14497  bool associate_type_to_die)
14498 {
14500 
14501  if (!die)
14502  return result;
14503 
14504  unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
14505  if (tag != DW_TAG_subrange_type)
14506  return result;
14507 
14508  string name = die_name(die);
14509 
14510  // load the underlying type.
14511  Dwarf_Die underlying_type_die;
14512  type_base_sptr underlying_type;
14513  /* Unless there is an underlying type which says differently. */
14514  bool is_signed = false;
14515  if (die_die_attribute(die, DW_AT_type, underlying_type_die))
14516  underlying_type =
14517  is_type(build_ir_node_from_die(rdr,
14518  &underlying_type_die,
14519  /*called_from_public_decl=*/true,
14520  where_offset));
14521 
14522  if (underlying_type)
14523  {
14524  uint64_t ate;
14525  if (die_unsigned_constant_attribute (&underlying_type_die,
14526  DW_AT_encoding,
14527  ate))
14528  is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
14529  }
14530 
14531  translation_unit::language language = rdr.cur_transl_unit()->get_language();
14532  array_type_def::subrange_type::bound_value lower_bound =
14533  get_default_array_lower_bound(language);
14534  array_type_def::subrange_type::bound_value upper_bound;
14535  uint64_t count = 0;
14536  bool is_non_finite = false;
14537  bool non_zero_count_present = false;
14538 
14539  // The DWARF 4 specifications says, in [5.11 Subrange
14540  // Type Entries]:
14541  //
14542  // The subrange entry may have the attributes
14543  // DW_AT_lower_bound and DW_AT_upper_bound to
14544  // specify, respectively, the lower and upper bound
14545  // values of the subrange.
14546  //
14547  // So let's look for DW_AT_lower_bound first.
14548  die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14549 
14550  bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
14551  is_signed, upper_bound);
14552  if (!found_upper_bound)
14553  found_upper_bound = subrange_die_indirect_bound_value(die,
14554  DW_AT_upper_bound,
14555  upper_bound,
14556  is_signed);
14557  // Then, DW_AT_upper_bound.
14558  if (!found_upper_bound)
14559  {
14560  // The DWARF 4 spec says, in [5.11 Subrange Type
14561  // Entries]:
14562  //
14563  // The DW_AT_upper_bound attribute may be replaced
14564  // by a DW_AT_count attribute, whose value
14565  // describes the number of elements in the
14566  // subrange rather than the value of the last
14567  // element."
14568  //
14569  // So, as DW_AT_upper_bound is not present in this
14570  // case, let's see if there is a DW_AT_count.
14571  if (die_unsigned_constant_attribute(die, DW_AT_count, count))
14572  {
14573  if (count)
14574  // DW_AT_count can be present and be set to zero. This is
14575  // for instance the case to model this gcc extension to
14576  // represent flexible arrays:
14577  // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
14578  // For instance: int flex_array[0];
14579  non_zero_count_present = true;
14580 
14581  // When the count is present and non-zero, we can deduce the
14582  // upper_bound from the lower_bound and the number of
14583  // elements of the array:
14584  int64_t u = lower_bound.get_signed_value() + count;
14585  if (u)
14586  upper_bound = u - 1;
14587  }
14588 
14589  if (!non_zero_count_present)
14590  // No upper_bound nor count was present on the DIE, this means
14591  // the array is considered to have an infinite (or rather not
14592  // known) size.
14593  is_non_finite = true;
14594  }
14595 
14596  if (UINT64_MAX == upper_bound.get_unsigned_value())
14597  // If the upper_bound size is the max of the integer value
14598  // then it most certainly means unknown size.
14599  is_non_finite = true;
14600 
14601  result.reset
14602  (new array_type_def::subrange_type(rdr.env(),
14603  name,
14604  lower_bound,
14605  upper_bound,
14606  location()));
14607  result->is_non_finite(is_non_finite);
14608 
14609  if (underlying_type)
14610  result->set_underlying_type(underlying_type);
14611 
14612  // Let's ensure the resulting subrange looks metabolically healhty.
14613  ABG_ASSERT(result->is_non_finite()
14614  || (result->get_length() ==
14615  (uint64_t) (result->get_upper_bound()
14616  - result->get_lower_bound() + 1)));
14617 
14618  if (associate_type_to_die)
14619  rdr.associate_die_to_type(die, result, where_offset);
14620 
14621  return result;
14622 }
14623 
14624 /// Build the sub-ranges of an array type.
14625 ///
14626 /// This is a sub-routine of build_array_type().
14627 ///
14628 /// @param rdr the context to read from.
14629 ///
14630 /// @param die the DIE of tag DW_TAG_array_type which contains
14631 /// children DIEs that represent the sub-ranges.
14632 ///
14633 /// @param subranges out parameter. This is set to the sub-ranges
14634 /// that are built from @p die.
14635 ///
14636 /// @param where_offset the offset of the DIE where we are "logically"
14637 /// positioned at, in the DIE tree. This is useful when @p die is
14638 /// e.g, DW_TAG_partial_unit that can be included in several places in
14639 /// the DIE tree.
14640 static void
14641 build_subranges_from_array_type_die(reader& rdr,
14642  const Dwarf_Die* die,
14643  array_type_def::subranges_type& subranges,
14644  size_t where_offset,
14645  bool associate_type_to_die)
14646 {
14647  Dwarf_Die child;
14648 
14649  if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14650  {
14651  do
14652  {
14653  int child_tag = dwarf_tag(&child);
14654  if (child_tag == DW_TAG_subrange_type)
14655  {
14657  if (associate_type_to_die)
14658  {
14659  // We are being called to create the type, add it to
14660  // the current type graph and associate it to the
14661  // DIE it's been created from.
14663  build_ir_node_from_die(rdr, &child,
14664  /*called_from_public_decl=*/true,
14665  where_offset);
14666  s = is_subrange_type(t);
14667  }
14668  else
14669  // We are being called to create the type but *NOT*
14670  // add it to the current tyupe tree, *NOR* associate
14671  // it to the DIE it's been created from.
14672  s = build_subrange_type(rdr, &child,
14673  where_offset,
14674  /*associate_type_to_die=*/false);
14675  if (s)
14676  subranges.push_back(s);
14677  }
14678  }
14679  while (dwarf_siblingof(&child, &child) == 0);
14680  }
14681 }
14682 
14683 /// Build an array type from a DW_TAG_array_type DIE.
14684 ///
14685 /// @param rdr the DWARF reader to consider.
14686 ///
14687 /// @param die the DIE to read from.
14688 ///
14689 /// @param called_from_public_decl true if this function was called
14690 /// from a context where either a public function or a public variable
14691 /// is being built.
14692 ///
14693 /// @param where_offset the offset of the DIE where we are "logically"
14694 /// positioned at, in the DIE tree. This is useful when @p die is
14695 /// e.g, DW_TAG_partial_unit that can be included in several places in
14696 /// the DIE tree.
14697 ///
14698 /// @return a pointer to the resulting array_type_def.
14699 static array_type_def_sptr
14700 build_array_type(reader& rdr,
14701  Dwarf_Die* die,
14702  bool called_from_public_decl,
14703  size_t where_offset)
14704 {
14705  array_type_def_sptr result;
14706 
14707  if (!die)
14708  return result;
14709 
14710  unsigned tag = dwarf_tag(die);
14711  if (tag != DW_TAG_array_type)
14712  return result;
14713 
14714  decl_base_sptr type_decl;
14715  Dwarf_Die type_die;
14716 
14717  if (die_die_attribute(die, DW_AT_type, type_die))
14718  type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14719  called_from_public_decl,
14720  where_offset));
14721  if (!type_decl)
14722  return result;
14723 
14724  // The call to build_ir_node_from_die() could have triggered the
14725  // creation of the type for this DIE. In that case, just return it.
14726  if (type_base_sptr t = rdr.lookup_type_from_die(die))
14727  {
14728  result = is_array_type(t);
14729  ABG_ASSERT(result);
14730  return result;
14731  }
14732 
14733  type_base_sptr type = is_type(type_decl);
14734  ABG_ASSERT(type);
14735 
14737 
14738  build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14739 
14740  result.reset(new array_type_def(type, subranges, location()));
14741 
14742  return result;
14743 }
14744 
14745 /// Create a typedef_decl from a DW_TAG_typedef DIE.
14746 ///
14747 /// @param rdr the DWARF reader to consider.
14748 ///
14749 /// @param die the DIE to read from.
14750 ///
14751 /// @param called_from_public_decl true if this function was called
14752 /// from a context where either a public function or a public variable
14753 /// is being built.
14754 ///
14755 /// @param where_offset the offset of the DIE where we are "logically"
14756 /// positionned at, in the DIE tree. This is useful when @p die is
14757 /// e.g, DW_TAG_partial_unit that can be included in several places in
14758 /// the DIE tree.
14759 ///
14760 /// @return the newly created typedef_decl.
14761 static typedef_decl_sptr
14762 build_typedef_type(reader& rdr,
14763  Dwarf_Die* die,
14764  bool called_from_public_decl,
14765  size_t where_offset)
14766 {
14767  typedef_decl_sptr result;
14768 
14769  if (!die)
14770  return result;
14771 
14772  unsigned tag = dwarf_tag(die);
14773  if (tag != DW_TAG_typedef)
14774  return result;
14775 
14776  string name, linkage_name;
14777  location loc;
14778  die_loc_and_name(rdr, die, loc, name, linkage_name);
14779 
14780  if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14781  if (loc)
14782  result = lookup_typedef_type_per_location(loc.expand(), *corp);
14783 
14784  if (!result)
14785  {
14786  type_base_sptr utype;
14787  Dwarf_Die underlying_type_die;
14788  if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14789  // A typedef DIE with no underlying type means a typedef to
14790  // void type.
14791  utype = rdr.env().get_void_type();
14792 
14793  if (!utype)
14794  utype =
14795  is_type(build_ir_node_from_die(rdr,
14796  &underlying_type_die,
14797  called_from_public_decl,
14798  where_offset));
14799  if (!utype)
14800  return result;
14801 
14802  ABG_ASSERT(utype);
14803  result.reset(new typedef_decl(name, utype, loc, linkage_name));
14804 
14805  if ((is_class_or_union_type(utype) || is_enum_type(utype))
14806  && is_anonymous_type(utype))
14807  {
14808  // This is a naming typedef for an enum or a class. Let's
14809  // mark the underlying decl as such.
14810  decl_base_sptr decl = is_decl(utype);
14811  ABG_ASSERT(decl);
14812  decl->set_naming_typedef(result);
14813  if (is_class_or_union_type(utype))
14814  rdr.maybe_schedule_declaration_only_class_for_resolution
14815  (is_class_or_union_type(utype));
14816  else if (is_enum_type(utype))
14817  rdr.maybe_schedule_declaration_only_enum_for_resolution
14818  (is_enum_type(utype));
14819  }
14820  }
14821 
14822  rdr.associate_die_to_type(die, result, where_offset);
14823 
14824  return result;
14825 }
14826 
14827 /// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
14828 /// denoted by the DIE is not suppressed by a suppression
14829 /// specification associated to the current DWARF reader.
14830 ///
14831 /// Note that if a member variable declaration with the same name as
14832 /// the name of the DIE we are looking at exists, this function returns
14833 /// that existing variable declaration.
14834 ///
14835 /// @param rdr the DWARF reader to use.
14836 ///
14837 /// @param die the DIE representing the variable we are looking at.
14838 ///
14839 /// @param where_offset the offset of the DIE where we are "logically"
14840 /// positionned at, in the DIE tree. This is useful when @p die is
14841 /// e.g, DW_TAG_partial_unit that can be included in several places in
14842 /// the DIE tree.
14843 ///
14844 /// @param is_declaration_only if true, it means the variable DIE has
14845 /// the is_declaration_only only attribute.
14846 ///
14847 /// @param result if this is set to an existing var_decl, this means
14848 /// that the function will append the new properties it sees on @p die
14849 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14850 /// new var_decl is going to be allocated and returned.
14851 ///
14852 /// @param is_required_decl_spec this is true iff the variable to
14853 /// build is referred to as being the specification of another
14854 /// variable.
14855 ///
14856 /// @return a pointer to the newly created var_decl. If the var_decl
14857 /// could not be built, this function returns NULL.
14858 static var_decl_sptr
14859 build_or_get_var_decl_if_not_suppressed(reader& rdr,
14860  scope_decl *scope,
14861  Dwarf_Die *die,
14862  size_t where_offset,
14863  bool is_declaration_only,
14864  var_decl_sptr result,
14865  bool is_required_decl_spec)
14866 {
14867  var_decl_sptr var;
14868  if (variable_is_suppressed(rdr, scope, die,
14869  is_declaration_only,
14870  is_required_decl_spec))
14871  return var;
14872 
14873  if (class_decl* class_type = is_class_type(scope))
14874  {
14875  string var_name = die_name(die);
14876  if (!var_name.empty())
14877  if ((var = class_type->find_data_member(var_name)))
14878  return var;
14879  }
14880  var = build_var_decl(rdr, die, where_offset, result);
14881  return var;
14882 }
14883 
14884 /// Build a @ref var_decl out of a DW_TAG_variable DIE.
14885 ///
14886 /// @param rdr the DWARF reader to use.
14887 ///
14888 /// @param die the DIE representing the variable we are looking at.
14889 ///
14890 /// @param where_offset the offset of the DIE where we are "logically"
14891 /// positionned at, in the DIE tree. This is useful when @p die is
14892 /// e.g, DW_TAG_partial_unit that can be included in several places in
14893 /// the DIE tree.
14894 ///
14895 /// @param result if this is set to an existing var_decl, this means
14896 /// that the function will append the new properties it sees on @p die
14897 /// to that exising var_decl. Otherwise, if this parameter is NULL, a
14898 /// new var_decl is going to be allocated and returned.
14899 ///
14900 /// @return a pointer to the newly created var_decl. If the var_decl
14901 /// could not be built, this function returns NULL.
14902 static var_decl_sptr
14903 build_var_decl(reader& rdr,
14904  Dwarf_Die *die,
14905  size_t where_offset,
14906  var_decl_sptr result)
14907 {
14908  if (!die)
14909  return result;
14910 
14911  int tag = dwarf_tag(die);
14912  ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
14913 
14914  if (!die_is_public_decl(die))
14915  return result;
14916 
14917  type_base_sptr type;
14918  Dwarf_Die type_die;
14919  if (die_die_attribute(die, DW_AT_type, type_die))
14920  {
14921  decl_base_sptr ty =
14922  is_decl(build_ir_node_from_die(rdr, &type_die,
14923  /*called_from_public_decl=*/true,
14924  where_offset));
14925  if (!ty)
14926  return result;
14927  type = is_type(ty);
14928  ABG_ASSERT(type);
14929  }
14930 
14931  if (!type && !result)
14932  return result;
14933 
14934  string name, linkage_name;
14935  location loc;
14936  die_loc_and_name(rdr, die, loc, name, linkage_name);
14937 
14938  if (!result)
14939  result.reset(new var_decl(name, type, loc, linkage_name));
14940  else
14941  {
14942  // We were called to append properties that might have been
14943  // missing from the first version of the variable. And usually
14944  // that missing property is the mangled name or the type.
14945  if (!linkage_name.empty())
14946  result->set_linkage_name(linkage_name);
14947 
14948  if (type)
14949  result->set_type(type);
14950  }
14951 
14952  // Check if a variable symbol with this name is exported by the elf
14953  // binary. If it is, then set the symbol of the variable, if it's
14954  // not set already.
14955  if (!result->get_symbol())
14956  {
14957  elf_symbol_sptr var_sym;
14958  Dwarf_Addr var_addr;
14959 
14960  if (rdr.get_variable_address(die, var_addr))
14961  {
14962  rdr.symtab()->
14963  update_main_symbol(var_addr,
14964  result->get_linkage_name().empty()
14965  ? result->get_name()
14966  : result->get_linkage_name());
14967  var_sym = rdr.variable_symbol_is_exported(var_addr);
14968  }
14969 
14970  if (var_sym)
14971  {
14972  result->set_symbol(var_sym);
14973  // If the linkage name is not set or is wrong, set it to
14974  // the name of the underlying symbol.
14975  string linkage_name = result->get_linkage_name();
14976  if (linkage_name.empty()
14977  || !var_sym->get_alias_from_name(linkage_name))
14978  result->set_linkage_name(var_sym->get_name());
14979  result->set_is_in_public_symbol_table(true);
14980  }
14981 
14982  if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
14983  {
14984  // We are looking at a global variable which symbol is
14985  // undefined. Let's set its symbol.
14986  string n = result->get_linkage_name();
14987  if (n.empty())
14988  n = result->get_name();
14989  var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
14990  if (var_sym)
14991  {
14992  result->set_symbol(var_sym);
14993  result->set_is_in_public_symbol_table(false);
14994  }
14995  }
14996  }
14997 
14998  return result;
14999 }
15000 
15001 /// Test if a given function denoted by its DIE and its scope is
15002 /// suppressed by any of the suppression specifications associated to
15003 /// a given context of ELF/DWARF reading.
15004 ///
15005 /// Note that a non-member function which symbol is not exported is
15006 /// also suppressed.
15007 ///
15008 /// @param rdr the ELF/DWARF reading content of interest.
15009 ///
15010 /// @param scope of the scope of the function.
15011 ///
15012 /// @param function_die the DIE representing the function.
15013 ///
15014 /// @param is_declaration_only is true if the DIE denoted by @p die is
15015 /// a declaration-only DIE.
15016 ///
15017 /// @return true iff @p function_die is suppressed by at least one
15018 /// suppression specification attached to the @p rdr.
15019 static bool
15020 function_is_suppressed(const reader& rdr,
15021  const scope_decl* scope,
15022  Dwarf_Die *function_die,
15023  bool is_declaration_only)
15024 {
15025  if (function_die == 0
15026  || dwarf_tag(function_die) != DW_TAG_subprogram)
15027  return false;
15028 
15029  string fname = die_string_attribute(function_die, DW_AT_name);
15030  string flinkage_name = die_linkage_name(function_die);
15031  if (flinkage_name.empty() && die_is_in_c(function_die))
15032  flinkage_name = fname;
15033  string qualified_name = build_qualified_name(scope, fname);
15034 
15035  // A non-member non-static function which symbol is not exported is
15036  // suppressed.
15037  //
15038  // Note that if the non-member non-static function has an undefined
15039  // symbol, by default, it's not suppressed. Unless we are asked to
15040  // drop undefined symbols too.
15041  if (!is_class_type(scope)
15042  && (!is_declaration_only || rdr.drop_undefined_syms()))
15043  {
15044  Dwarf_Addr fn_addr;
15045  if (!rdr.get_function_address(function_die, fn_addr))
15046  return true;
15047 
15048  elf_symbol_sptr symbol =
15049  rdr.function_symbol_is_exported(fn_addr);
15050  if (!symbol)
15051  return true;
15052  if (!symbol->is_suppressed())
15053  return false;
15054 
15055  // Since there is only one symbol in DWARF associated with an elf_symbol,
15056  // we can assume this is the main symbol then. Otherwise the main hinting
15057  // did not work as expected.
15058  ABG_ASSERT(symbol->is_main_symbol());
15059  if (symbol->has_aliases())
15060  for (elf_symbol_sptr a = symbol->get_next_alias();
15061  !a->is_main_symbol(); a = a->get_next_alias())
15062  if (!a->is_suppressed())
15063  return false;
15064  }
15065 
15066  return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
15067  /*require_drop_property=*/true);
15068 }
15069 
15070 /// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
15071 /// function denoted by the DIE is not suppressed by a suppression
15072 /// specification associated to the current DWARF reader.
15073 ///
15074 /// Note that if a member function declaration with the same signature
15075 /// (pretty representation) as one of the DIE we are looking at
15076 /// exists, this function returns that existing function declaration.
15077 /// Similarly, if there is already a constructed member function with
15078 /// the same linkage name as the one on the DIE, this function returns
15079 /// that member function.
15080 ///
15081 /// Also note that the function_decl IR returned by this function must
15082 /// be passed to finish_member_function_reading because several
15083 /// properties from the DIE are actually read by that function, and
15084 /// the corresponding properties on the function_decl IR are updated
15085 /// accordingly. This is done to support "updating" a function_decl
15086 /// IR with properties scathered across several DIEs.
15087 ///
15088 /// @param rdr the DWARF reader to use.
15089 ///
15090 /// @param scope the scope of the function we are looking at.
15091 ///
15092 /// @param fn_die the DIE representing the function we are looking at.
15093 ///
15094 /// @param where_offset the offset of the DIE where we are "logically"
15095 /// positionned at, in the DIE tree. This is useful when @p die is
15096 /// e.g, DW_TAG_partial_unit that can be included in several places in
15097 /// the DIE tree.
15098 ///
15099 /// @param is_declaration_only is true if the DIE denoted by @p fn_die
15100 /// is a declaration-only DIE.
15101 ///
15102 /// @param result if this is set to an existing function_decl, this
15103 /// means that the function will append the new properties it sees on
15104 /// @p fn_die to that exising function_decl. Otherwise, if this
15105 /// parameter is NULL, a new function_decl is going to be allocated
15106 /// and returned.
15107 ///
15108 /// @return a pointer to the newly created var_decl. If the var_decl
15109 /// could not be built, this function returns NULL.
15110 static function_decl_sptr
15111 build_or_get_fn_decl_if_not_suppressed(reader& rdr,
15112  scope_decl *scope,
15113  Dwarf_Die *fn_die,
15114  size_t where_offset,
15115  bool is_declaration_only,
15116  function_decl_sptr result)
15117 {
15118  function_decl_sptr fn;
15119  if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
15120  return fn;
15121 
15122  string name = die_name(fn_die);
15123  string linkage_name = die_linkage_name(fn_die);
15124  bool is_dtor = !name.empty() && name[0]== '~';
15125  bool is_virtual = false;
15126  if (is_dtor)
15127  {
15128  Dwarf_Attribute attr;
15129  if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
15130  DW_AT_vtable_elem_location,
15131  &attr))
15132  is_virtual = true;
15133  }
15134 
15135 
15136  // If we've already built an IR for a function with the same
15137  // signature (from another DIE), reuse it, unless that function is a
15138  // virtual C++ destructor. Several virtual C++ destructors with the
15139  // same signature can be implemented by several different ELF
15140  // symbols. So re-using C++ destructors like that can lead to us
15141  // missing some destructors.
15142  if (!result && (!(is_dtor && is_virtual)))
15143  if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
15144  {
15145  fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
15146  rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
15147  rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
15148  return fn;
15149  }
15150 
15151  // If a member function with the same linkage name as the one
15152  // carried by the DIE already exists, then return it.
15153  if (class_decl* klass = is_class_type(scope))
15154  {
15155  string linkage_name = die_linkage_name(fn_die);
15156  fn = klass->find_member_function_sptr(linkage_name);
15157  if (fn)
15158  // We found a member function that has the same signature.
15159  // Let's mark it for update.
15160  result = fn;
15161  }
15162 
15163  if (!fn || !fn->get_symbol())
15164  // We haven't yet been able to construct a function IR, or, we
15165  // have one 'partial' function IR that doesn't have any associated
15166  // symbol yet. Note that in the later case, a function IR without
15167  // any associated symbol will be dropped on the floor by
15168  // potential_member_fn_should_be_dropped. So let's build or a new
15169  // function IR or complete the existing partial IR.
15170  fn = build_function_decl(rdr, fn_die, where_offset, result);
15171 
15172  return fn;
15173 }
15174 
15175 /// Test if a given variable denoted by its DIE and its scope is
15176 /// suppressed by any of the suppression specifications associated to
15177 /// a given context of ELF/DWARF reading.
15178 ///
15179 /// @param rdr the ELF/DWARF reading content of interest.
15180 ///
15181 /// @param scope of the scope of the variable.
15182 ///
15183 /// @param variable_die the DIE representing the variable.
15184 ///
15185 /// @param is_declaration_only true if the variable is supposed to be
15186 /// decl-only.
15187 ///
15188 /// @param is_required_decl_spec if true, means that the @p
15189 /// variable_die being considered is for a variable decl that is a
15190 /// specification for a concrete variable being built.
15191 ///
15192 /// @return true iff @p variable_die is suppressed by at least one
15193 /// suppression specification attached to the @p rdr.
15194 static bool
15195 variable_is_suppressed(const reader& rdr,
15196  const scope_decl* scope,
15197  Dwarf_Die *variable_die,
15198  bool is_declaration_only,
15199  bool is_required_decl_spec)
15200 {
15201  if (variable_die == 0
15202  || (dwarf_tag(variable_die) != DW_TAG_variable
15203  && dwarf_tag(variable_die) != DW_TAG_member))
15204  return false;
15205 
15206  string name = die_string_attribute(variable_die, DW_AT_name);
15207  string linkage_name = die_linkage_name(variable_die);
15208  if (linkage_name.empty() && die_is_in_c(variable_die))
15209  linkage_name = name;
15210  string qualified_name = build_qualified_name(scope, name);
15211 
15212  // If a non member variable that is a declaration (has no defined
15213  // and exported symbol) and is not the specification of another
15214  // concrete variable, then it's suppressed. This is a size
15215  // optimization; it removes useless declaration-only variables from
15216  // the IR.
15217  if (!is_class_type(scope)
15218  && !is_required_decl_spec
15219  // If we are asked to load undefined interfaces, then we don't
15220  // suppress declaration-only variables as they might have
15221  // undefined elf-symbols.
15222  && (!is_declaration_only || !rdr.load_undefined_interfaces()))
15223  {
15224  Dwarf_Addr var_addr = 0;
15225  if (!rdr.get_variable_address(variable_die, var_addr))
15226  return true;
15227 
15228  elf_symbol_sptr symbol =
15229  rdr.variable_symbol_is_exported(var_addr);
15230  if (!symbol)
15231  return true;
15232  if (!symbol->is_suppressed())
15233  return false;
15234 
15235  // Since there is only one symbol in DWARF associated with an elf_symbol,
15236  // we can assume this is the main symbol then. Otherwise the main hinting
15237  // did not work as expected.
15238  ABG_ASSERT(symbol->is_main_symbol());
15239  if (symbol->has_aliases())
15240  for (elf_symbol_sptr a = symbol->get_next_alias();
15241  !a->is_main_symbol(); a = a->get_next_alias())
15242  if (!a->is_suppressed())
15243  return false;
15244  }
15245 
15246  return suppr::is_variable_suppressed(rdr,
15247  qualified_name,
15248  linkage_name,
15249  /*require_drop_property=*/true);
15250 }
15251 
15252 /// Test if a type (designated by a given DIE) in a given scope is
15253 /// suppressed by the suppression specifications that are associated
15254 /// to a given DWARF reader.
15255 ///
15256 /// @param rdr the DWARF reader to consider.
15257 ///
15258 /// @param scope of the scope of the type DIE to consider.
15259 ///
15260 /// @param type_die the DIE that designates the type to consider.
15261 ///
15262 /// @param type_is_opaque out parameter. If this function returns
15263 /// true (the type @p type_die is suppressed) and if the type was
15264 /// suppressed because it's opaque then this parameter is set to
15265 /// true.
15266 ///
15267 /// @return true iff the type designated by the DIE @p type_die, in
15268 /// the scope @p scope is suppressed by at the suppression
15269 /// specifications associated to the current DWARF reader.
15270 static bool
15271 type_is_suppressed(const reader& rdr,
15272  const scope_decl* scope,
15273  Dwarf_Die *type_die,
15274  bool &type_is_opaque)
15275 {
15276  if (type_die == 0
15277  || (dwarf_tag(type_die) != DW_TAG_enumeration_type
15278  && dwarf_tag(type_die) != DW_TAG_class_type
15279  && dwarf_tag(type_die) != DW_TAG_structure_type
15280  && dwarf_tag(type_die) != DW_TAG_union_type))
15281  return false;
15282 
15283  string type_name, linkage_name;
15284  location type_location;
15285  die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15286  string qualified_name = build_qualified_name(scope, type_name);
15287 
15288  return suppr::is_type_suppressed(rdr,
15289  qualified_name,
15290  type_location,
15291  type_is_opaque,
15292  /*require_drop_property=*/true);
15293 }
15294 
15295 /// Test if a type (designated by a given DIE) in a given scope is
15296 /// suppressed by the suppression specifications that are associated
15297 /// to a given DWARF reader.
15298 ///
15299 /// @param rdr the DWARF reader to consider.
15300 ///
15301 /// @param scope of the scope of the type DIE to consider.
15302 ///
15303 /// @param type_die the DIE that designates the type to consider.
15304 ///
15305 /// @return true iff the type designated by the DIE @p type_die, in
15306 /// the scope @p scope is suppressed by at the suppression
15307 /// specifications associated to the current DWARF reader.
15308 static bool
15309 type_is_suppressed(const reader& rdr,
15310  const scope_decl* scope,
15311  Dwarf_Die *type_die)
15312 {
15313  bool type_is_opaque = false;
15314  return type_is_suppressed(rdr, scope, type_die, type_is_opaque);
15315 }
15316 
15317 /// Get the opaque version of a type that was suppressed because it's
15318 /// a private type.
15319 ///
15320 /// The opaque version version of the type is just a declared-only
15321 /// version of the type (class, union or enum type) denoted by @p
15322 /// type_die.
15323 ///
15324 /// @param rdr the DWARF reader in use.
15325 ///
15326 /// @param scope the scope of the type die we are looking at.
15327 ///
15328 /// @param type_die the type DIE we are looking at.
15329 ///
15330 /// @param where_offset the offset of the DIE where we are "logically"
15331 /// positionned at, in the DIE tree. This is useful when @p die is
15332 /// e.g, DW_TAG_partial_unit that can be included in several places in
15333 /// the DIE tree.
15334 ///
15335 /// @return the opaque version of the type denoted by @p type_die or
15336 /// nil if no opaque version was found.
15338 get_opaque_version_of_type(reader &rdr,
15339  scope_decl *scope,
15340  Dwarf_Die *type_die,
15341  size_t where_offset)
15342 {
15343  type_or_decl_base_sptr result;
15344 
15345  if (type_die == 0)
15346  return result;
15347 
15348  unsigned tag = dwarf_tag(type_die);
15349  if (tag != DW_TAG_class_type
15350  && tag != DW_TAG_structure_type
15351  && tag != DW_TAG_union_type
15352  && tag != DW_TAG_enumeration_type)
15353  return result;
15354 
15355  string type_name, linkage_name;
15356  location type_location;
15357  die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15358 
15359  string qualified_name = build_qualified_name(scope, type_name);
15360 
15361  //
15362  // TODO: also handle declaration-only unions. To do that, we mostly
15363  // need to adapt add_or_update_union_type to make it schedule
15364  // declaration-only unions for resolution too.
15365  //
15366  if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
15367  {
15368  string_classes_or_unions_map::const_iterator i =
15369  rdr.declaration_only_classes().find(qualified_name);
15370  if (i != rdr.declaration_only_classes().end())
15371  result = i->second.back();
15372 
15373  if (!result)
15374  {
15375  // So we didn't find any pre-existing forward-declared-only
15376  // class for the class definition that we could return as an
15377  // opaque type. So let's build one.
15378  //
15379  // TODO: we need to be able to do this for unions too!
15380  class_decl_sptr klass(new class_decl(rdr.env(), type_name,
15381  /*alignment=*/0, /*size=*/0,
15382  tag == DW_TAG_structure_type,
15383  type_location,
15384  decl_base::VISIBILITY_DEFAULT));
15385  klass->set_is_declaration_only(true);
15386  klass->set_is_artificial(die_is_artificial(type_die));
15387  add_decl_to_scope(klass, scope);
15388  rdr.associate_die_to_type(type_die, klass, where_offset);
15389  rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
15390  result = klass;
15391  }
15392  }
15393 
15394  if (tag == DW_TAG_enumeration_type)
15395  {
15396  string_enums_map::const_iterator i =
15397  rdr.declaration_only_enums().find(qualified_name);
15398  if (i != rdr.declaration_only_enums().end())
15399  result = i->second.back();
15400 
15401  if (!result)
15402  {
15403  uint64_t size = 0;
15404  if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
15405  size *= 8;
15406  type_decl_sptr underlying_type =
15407  build_enum_underlying_type(rdr, type_name, size,
15408  /*anonymous=*/true);
15409  enum_type_decl::enumerators enumeratorz;
15410  enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
15411  type_location,
15412  underlying_type,
15413  enumeratorz,
15414  linkage_name));
15415  enum_type->set_is_artificial(die_is_artificial(type_die));
15416  add_decl_to_scope(enum_type, scope);
15417  result = enum_type;
15418  }
15419  }
15420 
15421  return result;
15422 }
15423 
15424 /// Create a function symbol with a given name.
15425 ///
15426 /// @param sym_name the name of the symbol to create.
15427 ///
15428 /// @param env the environment to create the symbol in.
15429 ///
15430 /// @return the newly created symbol.
15432 create_default_fn_sym(const string& sym_name, const environment& env)
15433 {
15434  elf_symbol::version ver;
15435  elf_symbol_sptr result =
15436  elf_symbol::create(env,
15437  /*symbol index=*/ 0,
15438  /*symbol size=*/ 0,
15439  sym_name,
15440  /*symbol type=*/ elf_symbol::FUNC_TYPE,
15441  /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
15442  /*symbol is defined=*/ true,
15443  /*symbol is common=*/ false,
15444  /*symbol version=*/ ver,
15445  /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
15446  return result;
15447 }
15448 
15449 /// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
15450 ///
15451 /// @param rdr the DWARF reader to use
15452 ///
15453 /// @param die the DW_TAG_subprogram DIE to read from.
15454 ///
15455 /// @param where_offset the offset of the DIE where we are "logically"
15456 /// positionned at, in the DIE tree. This is useful when @p die is
15457 /// e.g, DW_TAG_partial_unit that can be included in several places in
15458 /// the DIE tree.
15459 ///
15460 /// @param called_for_public_decl this is set to true if the function
15461 /// was called for a public (function) decl.
15462 static function_decl_sptr
15463 build_function_decl(reader& rdr,
15464  Dwarf_Die* die,
15465  size_t where_offset,
15466  function_decl_sptr fn)
15467 {
15468  function_decl_sptr result = fn;
15469  if (!die)
15470  return result;
15471  int tag = dwarf_tag(die);
15472  ABG_ASSERT(tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine);
15473 
15474  if (!die_is_public_decl(die))
15475  return result;
15476 
15477  translation_unit_sptr tu = rdr.cur_transl_unit();
15478  ABG_ASSERT(tu);
15479 
15480  string fname, flinkage_name;
15481  location floc;
15482  die_loc_and_name(rdr, die, floc, fname, flinkage_name);
15483 
15484  size_t is_inline = die_is_declared_inline(die);
15485  class_or_union_sptr is_method =
15486  is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
15487 
15488  if (result)
15489  {
15490  // Add the properties that might have been missing from the
15491  // first declaration of the function. For now, it usually is
15492  // the mangled name that goes missing in the first declarations.
15493  //
15494  // Also note that if 'fn' has just been cloned, the current
15495  // linkage name (of the current DIE) might be different from the
15496  // linkage name of 'fn'. In that case, update the linkage name
15497  // of 'fn' too.
15498  if (!flinkage_name.empty()
15499  && result->get_linkage_name() != flinkage_name)
15500  result->set_linkage_name(flinkage_name);
15501  if (floc)
15502  if (!result->get_location())
15503  result->set_location(floc);
15504  result->is_declared_inline(is_inline);
15505  }
15506  else
15507  {
15508  function_type_sptr fn_type(build_function_type(rdr, die, is_method,
15509  where_offset));
15510  if (!fn_type)
15511  return result;
15512 
15513  maybe_canonicalize_type(fn_type, rdr);
15514 
15515  result.reset(is_method
15516  ? new method_decl(fname, fn_type,
15517  is_inline, floc,
15518  flinkage_name)
15519  : new function_decl(fname, fn_type,
15520  is_inline, floc,
15521  flinkage_name));
15522  }
15523 
15524  // Set the symbol of the function. If the linkage name is not set
15525  // or is wrong, set it to the name of the underlying symbol.
15526  if (!result->get_symbol())
15527  {
15528  elf_symbol_sptr fn_sym;
15529  Dwarf_Addr fn_addr;
15530  if (rdr.get_function_address(die, fn_addr))
15531  {
15532  rdr.symtab()->
15533  update_main_symbol(fn_addr,
15534  result->get_linkage_name().empty()
15535  ? result->get_name()
15536  : result->get_linkage_name());
15537  fn_sym = rdr.function_symbol_is_exported(fn_addr);
15538  }
15539 
15540  if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
15541  {
15542  result->set_symbol(fn_sym);
15543  string linkage_name = result->get_linkage_name();
15544  if (linkage_name.empty())
15545  result->set_linkage_name(fn_sym->get_name());
15546  result->set_is_in_public_symbol_table(true);
15547  }
15548 
15549  if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
15550  {
15551  // We are looking at a function which symbol is undefined.
15552  // let's set its symbol.
15553  string n = result->get_linkage_name();
15554  if (n.empty())
15555  n = result->get_name();
15556  fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
15557  if (fn_sym)
15558  {
15559  result->set_symbol(fn_sym);
15560  result->set_is_in_public_symbol_table(false);
15561  }
15562  }
15563  }
15564 
15565  rdr.associate_die_to_type(die, result->get_type(), where_offset);
15566 
15567  size_t die_offset = dwarf_dieoffset(die);
15568 
15569  if (fn
15570  && is_member_function(fn)
15572  && !result->get_linkage_name().empty())
15573  // This function is a virtual member function which has its
15574  // linkage name *and* and has its underlying symbol correctly set.
15575  // It thus doesn't need any fixup related to elf symbol. So
15576  // remove it from the set of virtual member functions with linkage
15577  // names and no elf symbol that need to be fixed up.
15578  rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
15579  return result;
15580 }
15581 
15582 /// Canonicalize a type if it's suitable for early canonicalizing, or,
15583 /// if it's not, schedule it for late canonicalization, after the
15584 /// debug info of the current translation unit has been fully read.
15585 ///
15586 /// A (composite) type is deemed suitable for early canonicalizing iff
15587 /// all of its sub-types are canonicalized themselve. Non composite
15588 /// types are always deemed suitable for early canonicalization.
15589 ///
15590 /// Note that this function knows how to deal with anonymous classes,
15591 /// structs and enums, unlike the overload below:
15592 ///
15593 /// @param t the type DIE to consider for canonicalization.
15594 ///
15595 /// @param rdr the @ref reader to use.
15596 static void
15597 maybe_canonicalize_type(const type_base_sptr& t,
15598  reader& rdr)
15599 {
15600  if (!t)
15601  return;
15602 
15603  type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15604  if (is_class_type(peeled_type)
15605  || is_union_type(peeled_type)
15606  || is_function_type(peeled_type)
15607  || is_array_type(peeled_type)
15608  || is_qualified_type(peeled_type)
15609  || is_enum_type(peeled_type)
15610  ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15611  // We delay canonicalization of classes/unions or typedef,
15612  // pointers, references and array to classes/unions. This is
15613  // because the (underlying) class might not be finished yet and we
15614  // might not be able to able detect it here (thinking about
15615  // classes that are work-in-progress, or classes that might be
15616  // later amended by some DWARF construct). So we err on the safe
15617  // side. We also delay canonicalization for array and qualified
15618  // types because they can be edited (in particular by
15619  // maybe_strip_qualification) after they are initially built.
15620  rdr.schedule_type_for_late_canonicalization(t);
15622  rdr.schedule_type_for_late_canonicalization(t);
15623  else
15624  canonicalize(t);
15625 }
15626 
15627 /// If a given decl is a member type declaration, set its access
15628 /// specifier from the DIE that represents it.
15629 ///
15630 /// @param member_type_declaration the member type declaration to
15631 /// consider.
15632 static void
15633 maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15634  Dwarf_Die* die)
15635 {
15636  if (is_type(member_type_declaration)
15637  && is_member_decl(member_type_declaration))
15638  {
15639  class_or_union* scope =
15640  is_class_or_union_type(member_type_declaration->get_scope());
15641  ABG_ASSERT(scope);
15642 
15643  access_specifier access = public_access;
15644  if (class_decl* cl = is_class_type(scope))
15645  if (!cl->is_struct())
15646  access = private_access;
15647 
15648  die_access_specifier(die, access);
15649  set_member_access_specifier(member_type_declaration, access);
15650  }
15651 }
15652 
15653 /// This function tests if a given function which might be intented to
15654 /// be added to a class scope (to become a member function) should be
15655 /// dropped on the floor instead and not be added to the class.
15656 ///
15657 /// This is a subroutine of build_ir_node_from_die.
15658 ///
15659 /// @param fn the function to consider.
15660 ///
15661 /// @param fn_die the DWARF die of @p fn.
15662 ///
15663 /// @param scope the scope in which @p fn is to be added.
15664 ///
15665 /// @return true iff @p fn should be dropped on the floor.
15666 static bool
15667 potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15668  const Dwarf_Die *fn_die)
15669 {
15670  if (!fn || fn->get_scope())
15671  return false;
15672 
15673  if (// A function that is not virtual ...
15674  !die_is_virtual(fn_die)
15675  // .. and yet has no defined ELF symbol associated ...
15676  && !fn->get_symbol())
15677  // Should not be added to its class scope.
15678  //
15679  // Why would it? It's not part of the ABI anyway, as it doesn't
15680  // have any ELF symbol associated and is not a virtual member
15681  // function. It just constitutes bloat in the IR and might even
15682  // induce spurious change reports down the road.
15683  return true;
15684 
15685  return false;
15686 }
15687 
15688 /// Build an IR node from a given DIE and add the node to the current
15689 /// IR being build and held in the DWARF reader. Doing that is called
15690 /// "emitting an IR node for the DIE".
15691 ///
15692 /// @param rdr the DWARF reader.
15693 ///
15694 /// @param die the DIE to consider.
15695 ///
15696 /// @param scope the scope under which the resulting IR node has to be
15697 /// added.
15698 ///
15699 /// @param called_from_public_decl set to yes if this function is
15700 /// called from the functions used to build a public decl (functions
15701 /// and variables). In that case, this function accepts building IR
15702 /// nodes representing types. Otherwise, this function only creates
15703 /// IR nodes representing public decls (functions and variables).
15704 /// This is done to avoid emitting IR nodes for types that are not
15705 /// referenced by public functions or variables.
15706 ///
15707 /// @param where_offset the offset of the DIE where we are "logically"
15708 /// positionned at, in the DIE tree. This is useful when @p die is
15709 /// e.g, DW_TAG_partial_unit that can be included in several places in
15710 /// the DIE tree.
15711 ///
15712 /// @param is_required_decl_spec if true, it means the ir node to
15713 /// build is for a decl that is a specification for another decl that
15714 /// is concrete. If you don't know what this is, set it to false.
15715 ///
15716 /// @param is_declaration_only is true if the DIE denoted by @p die is
15717 /// a declaration-only DIE.
15718 ///
15719 /// @return the resulting IR node.
15721 build_ir_node_from_die(reader& rdr,
15722  Dwarf_Die* die,
15723  scope_decl* scope,
15724  bool called_from_public_decl,
15725  size_t where_offset,
15726  bool is_declaration_only,
15727  bool is_required_decl_spec)
15728 {
15729  type_or_decl_base_sptr result;
15730 
15731  if (!die || !scope)
15732  return result;
15733 
15734  int tag = dwarf_tag(die);
15735 
15736  if (!called_from_public_decl)
15737  {
15738  if (rdr.load_all_types() && die_is_type(die))
15739  /* We were instructed to load debug info for all types,
15740  included those that are not reachable from a public
15741  declaration. So load the debug info for this type. */;
15742  else if (tag != DW_TAG_subprogram
15743  && tag != DW_TAG_variable
15744  && tag != DW_TAG_member
15745  && tag != DW_TAG_namespace)
15746  return result;
15747  }
15748 
15749  const die_source source_of_die = rdr.get_die_source(die);
15750 
15751  if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15752  source_of_die)))
15753  {
15754  if (rdr.load_all_types())
15755  if (called_from_public_decl)
15756  if (type_base_sptr t = is_type(result))
15757  if (corpus *abi_corpus = scope->get_corpus())
15758  abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15759 
15760  return result;
15761  }
15762 
15763  // This is *the* bit of code that ensures we have the right notion
15764  // of "declared" at any point in a DIE chain formed from
15765  // DW_AT_abstract_origin and DW_AT_specification links. There should
15766  // be no other callers of die_is_declaration_only.
15767  is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15768 
15769  switch (tag)
15770  {
15771  // Type DIEs we support.
15772  case DW_TAG_base_type:
15773  if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15774  {
15775  result =
15776  add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15777  canonicalize(t);
15778  }
15779  break;
15780 
15781  case DW_TAG_typedef:
15782  {
15783  typedef_decl_sptr t = build_typedef_type(rdr, die,
15784  called_from_public_decl,
15785  where_offset);
15786 
15787  result = add_decl_to_scope(t, scope);
15788  if (result)
15789  {
15790  maybe_set_member_type_access_specifier(is_decl(result), die);
15791  maybe_canonicalize_type(t, rdr);
15792  }
15793  }
15794  break;
15795 
15796  case DW_TAG_pointer_type:
15797  {
15799  build_pointer_type_def(rdr, die,
15800  called_from_public_decl,
15801  where_offset);
15802  if (p)
15803  {
15804  result =
15805  add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15806  ABG_ASSERT(result->get_translation_unit());
15807  maybe_canonicalize_type(p, rdr);
15808  }
15809  }
15810  break;
15811 
15812  case DW_TAG_reference_type:
15813  case DW_TAG_rvalue_reference_type:
15814  {
15816  build_reference_type(rdr, die,
15817  called_from_public_decl,
15818  where_offset);
15819  if (r)
15820  {
15821  result =
15822  add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
15823 
15824  rdr.associate_die_to_type(die, r, where_offset);
15825  maybe_canonicalize_type(r, rdr);
15826  }
15827  }
15828  break;
15829 
15830  case DW_TAG_ptr_to_member_type:
15831  {
15833  build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
15834  where_offset);
15835  if (p)
15836  {
15837  result =
15838  add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15839  maybe_canonicalize_type(p, rdr);
15840  }
15841  }
15842  break;
15843 
15844  case DW_TAG_const_type:
15845  case DW_TAG_volatile_type:
15846  case DW_TAG_restrict_type:
15847  {
15848  type_base_sptr q =
15849  build_qualified_type(rdr, die,
15850  called_from_public_decl,
15851  where_offset);
15852  if (q)
15853  {
15854  // Strip some potentially redundant type qualifiers from
15855  // the qualified type we just built.
15856  decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
15857  rdr);
15858  if (!d)
15859  d = get_type_declaration(q);
15860  ABG_ASSERT(d);
15861  type_base_sptr ty = is_type(d);
15862  // Associate the die to type ty again because 'ty'might be
15863  // different from 'q', because 'ty' is 'q' possibly
15864  // stripped from some redundant type qualifier.
15865  rdr.associate_die_to_type(die, ty, where_offset);
15866  result =
15867  add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
15868  maybe_canonicalize_type(is_type(result), rdr);
15869  }
15870  }
15871  break;
15872 
15873  case DW_TAG_enumeration_type:
15874  {
15875  bool type_is_opaque = false;
15876  bool type_suppressed =
15877  type_is_suppressed(rdr, scope, die, type_is_opaque);
15878  if (type_suppressed && type_is_opaque)
15879  {
15880  // The type is suppressed because it's private. If other
15881  // non-suppressed and declaration-only instances of this
15882  // type exist in the current corpus, then it means those
15883  // non-suppressed instances are opaque versions of the
15884  // suppressed private type. Lets return one of these opaque
15885  // types then.
15886  result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15887  maybe_canonicalize_type(is_type(result), rdr);
15888  }
15889  else if (!type_suppressed)
15890  {
15891  enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
15892  where_offset,
15893  is_declaration_only);
15894  result = add_decl_to_scope(e, scope);
15895  if (result)
15896  {
15897  maybe_set_member_type_access_specifier(is_decl(result), die);
15898  maybe_canonicalize_type(is_type(result), rdr);
15899  }
15900  }
15901  }
15902  break;
15903 
15904  case DW_TAG_class_type:
15905  case DW_TAG_structure_type:
15906  {
15907  bool type_is_opaque = false;
15908  bool type_suppressed=
15909  type_is_suppressed(rdr, scope, die, type_is_opaque);
15910 
15911  if (type_suppressed && type_is_opaque)
15912  {
15913  // The type is suppressed because it's private. If other
15914  // non-suppressed and declaration-only instances of this
15915  // type exist in the current corpus, then it means those
15916  // non-suppressed instances are opaque versions of the
15917  // suppressed private type. Lets return one of these opaque
15918  // types then.
15919  result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15920  maybe_canonicalize_type(is_type(result), rdr);
15921  }
15922  else if (!type_suppressed)
15923  {
15924  Dwarf_Die spec_die;
15925  scope_decl_sptr scop;
15926  class_decl_sptr klass;
15927  if (die_die_attribute(die, DW_AT_specification, spec_die))
15928  {
15929  scope_decl_sptr skope =
15930  get_scope_for_die(rdr, &spec_die,
15931  called_from_public_decl,
15932  where_offset);
15933  ABG_ASSERT(skope);
15934  decl_base_sptr cl =
15935  is_decl(build_ir_node_from_die(rdr, &spec_die,
15936  skope.get(),
15937  called_from_public_decl,
15938  where_offset,
15939  is_declaration_only,
15940  /*is_required_decl_spec=*/false));
15941  ABG_ASSERT(cl);
15942  klass = dynamic_pointer_cast<class_decl>(cl);
15943  ABG_ASSERT(klass);
15944 
15945  klass =
15946  add_or_update_class_type(rdr, die,
15947  skope.get(),
15948  tag == DW_TAG_structure_type,
15949  klass,
15950  called_from_public_decl,
15951  where_offset,
15952  is_declaration_only);
15953  }
15954  else
15955  klass =
15956  add_or_update_class_type(rdr, die, scope,
15957  tag == DW_TAG_structure_type,
15958  class_decl_sptr(),
15959  called_from_public_decl,
15960  where_offset,
15961  is_declaration_only);
15962  result = klass;
15963  if (klass)
15964  {
15965  maybe_set_member_type_access_specifier(klass, die);
15966  maybe_canonicalize_type(klass, rdr);
15967  }
15968  }
15969  }
15970  break;
15971  case DW_TAG_union_type:
15972  if (!type_is_suppressed(rdr, scope, die))
15973  {
15974  union_decl_sptr union_type =
15975  add_or_update_union_type(rdr, die, scope,
15976  union_decl_sptr(),
15977  called_from_public_decl,
15978  where_offset,
15979  is_declaration_only);
15980  if (union_type)
15981  {
15982  maybe_set_member_type_access_specifier(union_type, die);
15983  maybe_canonicalize_type(union_type, rdr);
15984  }
15985  result = union_type;
15986  }
15987  break;
15988  case DW_TAG_string_type:
15989  break;
15990  case DW_TAG_subroutine_type:
15991  {
15992  function_type_sptr f = build_function_type(rdr, die,
15993  class_decl_sptr(),
15994  where_offset);
15995  if (f)
15996  {
15997  result = f;
15998  result->set_is_artificial(false);
15999  maybe_canonicalize_type(f, rdr);
16000  }
16001  }
16002  break;
16003  case DW_TAG_array_type:
16004  {
16005  array_type_def_sptr a = build_array_type(rdr,
16006  die,
16007  called_from_public_decl,
16008  where_offset);
16009  if (a)
16010  {
16011  result =
16012  add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
16013  rdr.associate_die_to_type(die, a, where_offset);
16014  maybe_canonicalize_type(a, rdr);
16015  }
16016  break;
16017  }
16018  case DW_TAG_subrange_type:
16019  {
16020  // If we got here, this means the subrange type is a "free
16021  // form" defined in the global namespace of the current
16022  // translation unit, like what is found in Ada.
16024  build_subrange_type(rdr, die, where_offset);
16025  if (s)
16026  {
16027  result =
16028  add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
16029  rdr.associate_die_to_type(die, s, where_offset);
16030  maybe_canonicalize_type(s, rdr);
16031  }
16032  }
16033  break;
16034  case DW_TAG_packed_type:
16035  break;
16036  case DW_TAG_set_type:
16037  break;
16038  case DW_TAG_file_type:
16039  break;
16040  case DW_TAG_thrown_type:
16041  break;
16042  case DW_TAG_interface_type:
16043  break;
16044  case DW_TAG_unspecified_type:
16045  break;
16046  case DW_TAG_shared_type:
16047  break;
16048 
16049  case DW_TAG_compile_unit:
16050  // We shouldn't reach this point b/c this should be handled by
16051  // build_translation_unit.
16053 
16054  case DW_TAG_namespace:
16055  case DW_TAG_module:
16056  result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
16057  break;
16058 
16059  case DW_TAG_variable:
16060  case DW_TAG_member:
16061  {
16062  Dwarf_Die spec_die;
16063  bool var_is_cloned = false;
16064 
16065  if (tag == DW_TAG_member)
16066  ABG_ASSERT(!die_is_in_c(die));
16067 
16068  if (die_die_attribute(die, DW_AT_specification, spec_die, false)
16069  || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
16070  spec_die, false)))
16071  {
16072  scope_decl_sptr spec_scope =
16073  get_scope_for_die(rdr, &spec_die,
16074  /*called_from_public_decl=*/
16075  die_is_effectively_public_decl(rdr, die),
16076  where_offset);
16077  if (spec_scope)
16078  {
16079  decl_base_sptr d =
16080  is_decl(build_ir_node_from_die(rdr, &spec_die,
16081  spec_scope.get(),
16082  called_from_public_decl,
16083  where_offset,
16084  is_declaration_only,
16085  /*is_required_decl_spec=*/true));
16086  if (d)
16087  {
16088  var_decl_sptr m =
16089  dynamic_pointer_cast<var_decl>(d);
16090  if (var_is_cloned)
16091  m = m->clone();
16092  m = build_var_decl(rdr, die, where_offset, m);
16093  if (is_data_member(m))
16094  {
16095  set_member_is_static(m, true);
16096  rdr.associate_die_to_decl(die, m, where_offset,
16097  /*associate_by_repr=*/false);
16098  }
16099  else
16100  {
16101  ABG_ASSERT(has_scope(m));
16102  rdr.var_decls_to_re_add_to_tree().push_back(m);
16103  }
16104  ABG_ASSERT(m->get_scope());
16105  rdr.add_var_to_exported_or_undefined_decls(m.get());
16106  result = m;
16107  }
16108  }
16109  }
16110  else if (var_decl_sptr v =
16111  build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
16112  where_offset,
16113  is_declaration_only,
16114  /*result=*/var_decl_sptr(),
16115  is_required_decl_spec))
16116  {
16117  result = add_decl_to_scope(v, scope);
16118  ABG_ASSERT(is_decl(result)->get_scope());
16119  v = dynamic_pointer_cast<var_decl>(result);
16120  ABG_ASSERT(v);
16121  ABG_ASSERT(v->get_scope());
16122  rdr.var_decls_to_re_add_to_tree().push_back(v);
16123  rdr.add_var_to_exported_or_undefined_decls(v.get());
16124  }
16125  }
16126  break;
16127 
16128  case DW_TAG_subprogram:
16129  case DW_TAG_inlined_subroutine:
16130  {
16131  if (die_is_artificial(die))
16132  break;
16133 
16134  Dwarf_Die abstract_origin_die;
16135  bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
16136  abstract_origin_die,
16137  /*recursive=*/true);
16138 
16139 
16140  scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
16141  where_offset);
16142  scope_decl* interface_scope = scope ? scope : s.get();
16143 
16144  class_decl* class_scope = is_class_type(interface_scope);
16145  string linkage_name = die_linkage_name(die);
16146  string spec_linkage_name;
16147  function_decl_sptr existing_fn;
16148 
16149  if (class_scope)
16150  {
16151  // The scope of the function DIE we are looking at is a
16152  // class. So we are looking at a member function.
16153  if (!linkage_name.empty())
16154  {
16155  if ((existing_fn =
16156  class_scope->find_member_function_sptr(linkage_name)))
16157  {
16158  // A function with the same linkage name has
16159  // already been created. Let's see if we are a
16160  // clone of it or not.
16161  spec_linkage_name = existing_fn->get_linkage_name();
16162  if (has_abstract_origin
16163  && !spec_linkage_name.empty()
16164  && linkage_name != spec_linkage_name)
16165  {
16166  // The current DIE has 'existing_fn' as
16167  // abstract orign, and has a linkage name that
16168  // is different from from the linkage name of
16169  // 'existing_fn'. That means, the current DIE
16170  // represents a clone of 'existing_fn'.
16171  existing_fn = existing_fn->clone();
16172  }
16173  }
16174  }
16175  }
16176 
16177  rdr.scope_stack().push(interface_scope);
16178 
16179  // Either we create a branch new IR for the current function
16180  // DIE we are looking at, or we complete an existing IR node
16181  // with the new completementary information carried by this
16182  // DIE for that IR node.
16183  result =
16184  build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
16185  die, where_offset,
16186  is_declaration_only,
16187  existing_fn);
16188 
16189  if (result && !existing_fn)
16190  {
16191  // We built a brand new IR for the function DIE. Now
16192  // there should be enough information on that IR to know
16193  // if we should drop it on the floor or keep it ...
16194  if (potential_member_fn_should_be_dropped(is_function_decl(result), die)
16195  && !is_required_decl_spec)
16196  {
16197  // So apparently we should drop that function IR on
16198  // the floor. Let's do so.
16199  result.reset();
16200  break;
16201  }
16202  // OK so we came to the conclusion that we need to keep
16203  // the function. So let's add it to its scope.
16204  result = add_decl_to_scope(is_decl(result), interface_scope);
16205  }
16206 
16207  function_decl_sptr fn = is_function_decl(result);
16208  if (fn && is_member_function(fn))
16209  {
16210  class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
16211  sptr_utils::noop_deleter());
16212  ABG_ASSERT(klass);
16213  finish_member_function_reading(die, fn, klass, rdr);
16214  }
16215 
16216  if (fn)
16217  {
16218  if (!is_member_function(fn)
16220  // Virtual member functions are added to the set of
16221  // functions exported by the current ABI corpus *after*
16222  // the canonicalization of their parent type. So let's
16223  // not do it here.
16224  rdr.add_fn_to_exported_or_undefined_decls(fn.get());
16225  rdr.associate_die_to_decl(die, fn, where_offset,
16226  /*associate_by_repr=*/false);
16227  maybe_canonicalize_type(fn->get_type(), rdr);
16228  }
16229 
16230  rdr.scope_stack().pop();
16231  }
16232  break;
16233 
16234  case DW_TAG_formal_parameter:
16235  // We should not read this case as it should have been dealt
16236  // with by build_function_decl above.
16238 
16239  case DW_TAG_constant:
16240  break;
16241  case DW_TAG_enumerator:
16242  break;
16243 
16244  case DW_TAG_partial_unit:
16245  case DW_TAG_imported_unit:
16246  // For now, the DIEs under these are read lazily when they are
16247  // referenced by a public decl DIE that is under a
16248  // DW_TAG_compile_unit, so we shouldn't get here.
16250 
16251  // Other declaration we don't really intend to support yet.
16252  case DW_TAG_dwarf_procedure:
16253  case DW_TAG_imported_declaration:
16254  case DW_TAG_entry_point:
16255  case DW_TAG_label:
16256  case DW_TAG_lexical_block:
16257  case DW_TAG_unspecified_parameters:
16258  case DW_TAG_variant:
16259  case DW_TAG_common_block:
16260  case DW_TAG_common_inclusion:
16261  case DW_TAG_inheritance:
16262  case DW_TAG_with_stmt:
16263  case DW_TAG_access_declaration:
16264  case DW_TAG_catch_block:
16265  case DW_TAG_friend:
16266  case DW_TAG_namelist:
16267  case DW_TAG_namelist_item:
16268  case DW_TAG_template_type_parameter:
16269  case DW_TAG_template_value_parameter:
16270  case DW_TAG_try_block:
16271  case DW_TAG_variant_part:
16272  case DW_TAG_imported_module:
16273  case DW_TAG_condition:
16274  case DW_TAG_type_unit:
16275  case DW_TAG_template_alias:
16276  case DW_TAG_lo_user:
16277  case DW_TAG_MIPS_loop:
16278  case DW_TAG_format_label:
16279  case DW_TAG_function_template:
16280  case DW_TAG_class_template:
16281  case DW_TAG_GNU_BINCL:
16282  case DW_TAG_GNU_EINCL:
16283  case DW_TAG_GNU_template_template_param:
16284  case DW_TAG_GNU_template_parameter_pack:
16285  case DW_TAG_GNU_formal_parameter_pack:
16286  case DW_TAG_GNU_call_site:
16287  case DW_TAG_GNU_call_site_parameter:
16288  case DW_TAG_hi_user:
16289  default:
16290  break;
16291  }
16292 
16293  if (result && tag != DW_TAG_subroutine_type)
16294  rdr.associate_die_to_decl(die, is_decl(result), where_offset,
16295  /*associate_by_repr=*/false);
16296 
16297  if (result)
16298  if (rdr.load_all_types())
16299  if (called_from_public_decl)
16300  if (type_base_sptr t = is_type(result))
16301  if (corpus *abi_corpus = scope->get_corpus())
16302  abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
16303 
16304  return result;
16305 }
16306 
16307 /// Build the IR node for a void type.
16308 ///
16309 /// @param rdr the DWARF reader to use.
16310 ///
16311 /// @return the void type node.
16312 static decl_base_sptr
16313 build_ir_node_for_void_type(reader& rdr)
16314 {
16315  const environment& env = rdr.env();
16316 
16317  type_base_sptr t = env.get_void_type();
16318  add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16319  decl_base_sptr type_declaration = get_type_declaration(t);
16320  canonicalize(t);
16321  return type_declaration;
16322 }
16323 
16324 /// Build the IR node for a "pointer to void type".
16325 ///
16326 /// That IR node is shared across the ABI corpus.
16327 ///
16328 /// Note that this function just gets that IR node from the
16329 /// environment and, if it's not added to any scope yet, adds it to
16330 /// the global scope associated to the current translation unit.
16331 ///
16332 /// @param rdr the DWARF reader to consider.
16333 ///
16334 /// @return the IR node.
16336 build_ir_node_for_void_pointer_type(reader& rdr)
16337 {
16338  const environment& env = rdr.env();
16339 
16340  type_base_sptr t = env.get_void_pointer_type();
16341  add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16342  decl_base_sptr type_declaration = get_type_declaration(t);
16343  canonicalize(t);
16344  return type_declaration;
16345 }
16346 
16347 /// Build the IR node for a variadic parameter type.
16348 ///
16349 /// @param rdr the DWARF reader to use.
16350 ///
16351 /// @return the variadic parameter type.
16352 static decl_base_sptr
16353 build_ir_node_for_variadic_parameter_type(reader &rdr)
16354 {
16355 
16356  const environment& env = rdr.env();
16357 
16358  type_base_sptr t = env.get_variadic_parameter_type();
16359  add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16360  decl_base_sptr type_declaration = get_type_declaration(t);
16361  canonicalize(t);
16362  return type_declaration;
16363 }
16364 
16365 /// Build an IR node from a given DIE and add the node to the current
16366 /// IR being build and held in the DWARF reader. Doing that is called
16367 /// "emitting an IR node for the DIE".
16368 ///
16369 /// @param rdr the DWARF reader.
16370 ///
16371 /// @param die the DIE to consider.
16372 ///
16373 /// @param called_from_public_decl set to yes if this function is
16374 /// called from the functions used to build a public decl (functions
16375 /// and variables). In that case, this function accepts building IR
16376 /// nodes representing types. Otherwise, this function only creates
16377 /// IR nodes representing public decls (functions and variables).
16378 /// This is done to avoid emitting IR nodes for types that are not
16379 /// referenced by public functions or variables.
16380 ///
16381 /// @param where_offset the offset of the DIE where we are "logically"
16382 /// positionned at, in the DIE tree. This is useful when @p die is
16383 /// e.g, DW_TAG_partial_unit that can be included in several places in
16384 /// the DIE tree.
16385 ///
16386 /// @return the resulting IR node.
16388 build_ir_node_from_die(reader& rdr,
16389  Dwarf_Die* die,
16390  bool called_from_public_decl,
16391  size_t where_offset)
16392 {
16393  if (!die)
16394  return decl_base_sptr();
16395 
16396  // Normaly, a decl that is meant to be external has a DW_AT_external
16397  // set. But then some compilers fail to always emit that flag. For
16398  // instance, for static data members, some compilers won't emit the
16399  // DW_AT_external. In that case, we assume that if the variable is
16400  // at global or named namespace scope, then we can assume it's
16401  // external. If the variable doesn't have any ELF symbol associated
16402  // to it, it'll be dropped on the floor anyway. Those variable
16403  // decls are considered as being "effectively public".
16404  bool consider_as_called_from_public_decl =
16405  called_from_public_decl || die_is_effectively_public_decl(rdr, die);
16406  scope_decl_sptr scope = get_scope_for_die(rdr, die,
16407  consider_as_called_from_public_decl,
16408  where_offset);
16409  return build_ir_node_from_die(rdr, die, scope.get(),
16410  called_from_public_decl,
16411  where_offset, true);
16412 }
16413 
16414 /// Create a dwarf::reader.
16415 ///
16416 /// @param elf_path the path to the elf file the reader is to be used
16417 /// for.
16418 ///
16419 /// @param debug_info_root_paths a vector to the paths to the
16420 /// directories under which the debug info is to be found for @p
16421 /// elf_path. Pass an empty vector if the debug info is not in a
16422 /// split file.
16423 ///
16424 /// @param environment the environment used by the current context.
16425 /// This environment contains resources needed by the DWARF reader and by
16426 /// the types and declarations that are to be created later. Note
16427 /// that ABI artifacts that are to be compared all need to be created
16428 /// within the same environment.
16429 ///
16430 /// Please also note that the life time of this environment object
16431 /// must be greater than the life time of the resulting @ref
16432 /// reader the context uses resources that are allocated in the
16433 /// environment.
16434 ///
16435 /// @param load_all_types if set to false only the types that are
16436 /// reachable from publicly exported declarations (of functions and
16437 /// variables) are read. If set to true then all types found in the
16438 /// debug information are loaded.
16439 ///
16440 /// @param linux_kernel_mode if set to true, then consider the special
16441 /// linux kernel symbol tables when determining if a symbol is
16442 /// exported or not.
16443 ///
16444 /// @return a smart pointer to the resulting dwarf::reader.
16445 elf_based_reader_sptr
16446 create_reader(const std::string& elf_path,
16447  const vector<char**>& debug_info_root_paths,
16449  bool load_all_types,
16450  bool linux_kernel_mode)
16451 {
16452 
16453  reader_sptr r = reader::create(elf_path,
16454  debug_info_root_paths,
16455  environment,
16456  load_all_types,
16457  linux_kernel_mode);
16458  return static_pointer_cast<elf_based_reader>(r);
16459 }
16460 
16461 /// Re-initialize a reader so that it can re-used to read
16462 /// another binary.
16463 ///
16464 /// @param rdr the context to re-initialize.
16465 ///
16466 /// @param elf_path the path to the elf file the context is to be used
16467 /// for.
16468 ///
16469 /// @param debug_info_root_path a pointer to the path to the root
16470 /// directory under which the debug info is to be found for @p
16471 /// elf_path. Leave this to NULL if the debug info is not in a split
16472 /// file.
16473 ///
16474 /// @param environment the environment used by the current context.
16475 /// This environment contains resources needed by the DWARF reader and by
16476 /// the types and declarations that are to be created later. Note
16477 /// that ABI artifacts that are to be compared all need to be created
16478 /// within the same environment.
16479 ///
16480 /// Please also note that the life time of this environment object
16481 /// must be greater than the life time of the resulting @ref
16482 /// reader the context uses resources that are allocated in the
16483 /// environment.
16484 ///
16485 /// @param load_all_types if set to false only the types that are
16486 /// reachable from publicly exported declarations (of functions and
16487 /// variables) are read. If set to true then all types found in the
16488 /// debug information are loaded.
16489 ///
16490 /// @param linux_kernel_mode if set to true, then consider the special
16491 /// linux kernel symbol tables when determining if a symbol is
16492 /// exported or not.
16493 ///
16494 /// @return a smart pointer to the resulting dwarf::reader.
16495 void
16497  const std::string& elf_path,
16498  const vector<char**>&debug_info_root_path,
16499  bool read_all_types,
16500  bool linux_kernel_mode)
16501 {
16502  reader& r = dynamic_cast<reader&>(rdr);
16503  r.initialize(elf_path, debug_info_root_path,
16504  read_all_types, linux_kernel_mode);
16505 }
16506 
16507 /// Read all @ref abigail::translation_unit possible from the debug info
16508 /// accessible from an elf file, stuff them into a libabigail ABI
16509 /// Corpus and return it.
16510 ///
16511 /// @param elf_path the path to the elf file.
16512 ///
16513 /// @param debug_info_root_paths a vector of pointers to root paths
16514 /// under which to look for the debug info of the elf files that are
16515 /// later handled by the Dwfl. This for cases where the debug info is
16516 /// split into a different file from the binary we want to inspect.
16517 /// On Red Hat compatible systems, this root path is usually
16518 /// /usr/lib/debug by default. If this argument is set to NULL, then
16519 /// "./debug" and /usr/lib/debug will be searched for sub-directories
16520 /// containing the debug info file.
16521 ///
16522 /// @param environment the environment used by the current context.
16523 /// This environment contains resources needed by the DWARF reader and by
16524 /// the types and declarations that are to be created later. Note
16525 /// that ABI artifacts that are to be compared all need to be created
16526 /// within the same environment. Also, the lifetime of the
16527 /// environment must be greater than the lifetime of the resulting
16528 /// corpus because the corpus uses resources that are allocated in the
16529 /// environment.
16530 ///
16531 /// @param load_all_types if set to false only the types that are
16532 /// reachable from publicly exported declarations (of functions and
16533 /// variables) are read. If set to true then all types found in the
16534 /// debug information are loaded.
16535 ///
16536 /// @param resulting_corp a pointer to the resulting abigail::corpus.
16537 ///
16538 /// @return the resulting status.
16539 corpus_sptr
16540 read_corpus_from_elf(const std::string& elf_path,
16541  const vector<char**>& debug_info_root_paths,
16543  bool load_all_types,
16544  fe_iface::status& status)
16545 {
16546  elf_based_reader_sptr rdr =
16547  dwarf::reader::create(elf_path, debug_info_root_paths,
16548  environment, load_all_types,
16549  /*linux_kernel_mode=*/false);
16550 
16551  return rdr->read_corpus(status);
16552 }
16553 
16554 /// Look into the symbol tables of a given elf file and see if we find
16555 /// a given symbol.
16556 ///
16557 /// @param env the environment we are operating from.
16558 ///
16559 /// @param elf_path the path to the elf file to consider.
16560 ///
16561 /// @param symbol_name the name of the symbol to look for.
16562 ///
16563 /// @param demangle if true, try to demangle the symbol name found in
16564 /// the symbol table.
16565 ///
16566 /// @param syms the vector of symbols found with the name @p symbol_name.
16567 ///
16568 /// @return true iff the symbol was found among the publicly exported
16569 /// symbols of the ELF file.
16570 bool
16571 lookup_symbol_from_elf(const environment& env,
16572  const string& elf_path,
16573  const string& symbol_name,
16574  bool demangle,
16575  vector<elf_symbol_sptr>& syms)
16576 
16577 {
16578  if (elf_version(EV_CURRENT) == EV_NONE)
16579  return false;
16580 
16581  int fd = open(elf_path.c_str(), O_RDONLY);
16582  if (fd < 0)
16583  return false;
16584 
16585  struct stat s;
16586  if (fstat(fd, &s))
16587  return false;
16588 
16589  Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16590  if (elf == 0)
16591  return false;
16592 
16593  bool value = lookup_symbol_from_elf(env, elf, symbol_name,
16594  demangle, syms);
16595  elf_end(elf);
16596  close(fd);
16597 
16598  return value;
16599 }
16600 
16601 /// Look into the symbol tables of an elf file to see if a public
16602 /// function of a given name is found.
16603 ///
16604 /// @param env the environment we are operating from.
16605 ///
16606 /// @param elf_path the path to the elf file to consider.
16607 ///
16608 /// @param symbol_name the name of the function to look for.
16609 ///
16610 /// @param syms the vector of public function symbols found with the
16611 /// name @p symname.
16612 ///
16613 /// @return true iff a function with symbol name @p symbol_name is
16614 /// found.
16615 bool
16617  const string& path,
16618  const string& symname,
16619  vector<elf_symbol_sptr>& syms)
16620 {
16621  if (elf_version(EV_CURRENT) == EV_NONE)
16622  return false;
16623 
16624  int fd = open(path.c_str(), O_RDONLY);
16625  if (fd < 0)
16626  return false;
16627 
16628  struct stat s;
16629  if (fstat(fd, &s))
16630  return false;
16631 
16632  Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16633  if (elf == 0)
16634  return false;
16635 
16636  bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16637  elf_end(elf);
16638  close(fd);
16639 
16640  return value;
16641 }
16642 
16643 }// end namespace dwarf
16644 
16645 }// 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:1714
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.
virtual void initialize(const std::string &elf_path, const vector< char ** > &debug_info_root_paths)
(re)Initialize) the resources used by the current reader.
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:2533
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition: abg-ir.h:2540
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition: abg-ir.h:4245
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition: abg-corpus.h:51
scope_decl * get_scope() const
Return the type containing the current decl, if any.
Definition: abg-ir.cc:5032
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1194
binding
The binding of a symbol.
Definition: abg-ir.h:940
type
The type of a symbol.
Definition: abg-ir.h:927
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:1994
visibility
The visibility of the symbol.
Definition: abg-ir.h:949
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2763
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:3131
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3138
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:16471
The source location of a token.
Definition: abg-ir.h:299
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition: abg-ir.h:2245
language
The language of the translation unit.
Definition: abg-ir.h:699
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:12558
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:233
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:12227
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition: abg-fwd.h:219
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition: abg-fwd.h:267
access_specifier
Access specifier for class members.
Definition: abg-ir.h:879
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:13808
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:13970
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6920
vector< type_base_wptr > type_base_wptrs_type
A convenience typedef for a vector of type_base_wptr.
Definition: abg-fwd.h:143
const type_base * is_void_pointer_type(const type_base *t)
Test if a type is a pointer to void type.
Definition: abg-ir.cc:11569
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10665
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5640
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:11997
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:886
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:10915
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8649
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:10279
const ptr_to_mbr_type * is_ptr_to_mbr_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a ptr_to_mbr_type.
Definition: abg-ir.cc:11493
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:10947
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:242
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition: abg-ir.cc:10716
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:13995
type_base_sptr peel_const_qualified_type(const qualified_type_def_sptr &q)
If a qualified type is const, then return its underlying type.
Definition: abg-ir.cc:7525
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:6355
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:13671
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6723
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:11178
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:191
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:1326
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:7601
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:6779
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:7107
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:6666
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:10767
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:11640
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5785
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10825
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:13838
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition: abg-fwd.h:209
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition: abg-fwd.h:165
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:12267
bool is_typedef_of_maybe_qualified_class_or_union_type(const type_base *t)
Test if a type is a typedef of a class or union type, or a typedef of a qualified class or union type...
Definition: abg-ir.cc:11396
reference_type_def * is_reference_type(type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:11433
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1743
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:16389
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:10897
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8717
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:254
shared_ptr< ptr_to_mbr_type > ptr_to_mbr_type_sptr
Convenience typedef for a shared pointer to a ptr_to_mbr_type.
Definition: abg-fwd.h:238
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition: abg-fwd.h:262
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:12757
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:27782
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1757
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:12304
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:15880
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:224
bool is_const_qualified_type(const qualified_type_def_sptr &t)
Test if a given qualified type is const.
Definition: abg-ir.cc:7493
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6609
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1729
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10605
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:25121
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5705
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 and if the d...
Definition: abg-ir.cc:8625
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:28453
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5756
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:173
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6882
const pointer_type_def * is_pointer_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:11261
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:11700
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:11227
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:13626
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:11976
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5854
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:13575
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:10298
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:26245
array_type_def * is_array_type(const type_or_decl_base *type, bool look_through_qualifiers)
Test if a type is an array_type_def.
Definition: abg-ir.cc:11905
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:160
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:7230
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:1766
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:15210
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:12117
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:9009
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:7860
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:13933
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10553
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:11670
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:11620
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:12337
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:8906
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6852
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:11730
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5658
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:12091
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_type_suppressed(const fe_iface &fe, const string &type_name, const location &type_location, bool &type_is_opaque, bool require_drop_property)
Test if a type is matched by at least one suppression specification associated with a given front-end...
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.
void initialize()
This function needs to be called before any libabigail function.
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:187
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:153
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:170
A functor to hash instances of interned_string.