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-2024 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"
39#include "abg-corpus-priv.h"
40#include "abg-symtab-reader.h"
41
42// <headers defining libabigail's API go under here>
43ABG_BEGIN_EXPORT_DECLARATIONS
44
45#include "abg-dwarf-reader.h"
47#include "abg-sptr-utils.h"
48#include "abg-tools-utils.h"
49#include "abg-elf-helpers.h"
50
51ABG_END_EXPORT_DECLARATIONS
52// </headers defining libabigail's API>
53
54#ifndef UINT64_MAX
55#define UINT64_MAX 0xffffffffffffffff
56#endif
57
58using std::string;
59
60namespace abigail
61{
62
63using std::cerr;
64
65/// The namespace for the DWARF reader.
66namespace dwarf
67{
68
69using std::dynamic_pointer_cast;
70using std::static_pointer_cast;
71using std::unordered_map;
72using std::unordered_set;
73using std::stack;
74using std::deque;
75using std::list;
76using std::map;
78
79using 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.
96typedef 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.
100typedef 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.
105typedef 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.
110typedef 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.
114typedef 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.
118typedef 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.
123typedef 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.
127typedef 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.
131typedef 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.
138struct 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
145typedef 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.
151struct 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.
179typedef std::pair<offset_type, offset_type> offset_pair_type;
180
181/// A hasher for an instance of offset_type.
182struct 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.
191struct 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.
205typedef unordered_set<offset_type, offset_hash> offset_set_type;
206
207///A convenience typedef for an unordered set of pairs of offset_type.
208typedef 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.
213typedef 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.
217typedef 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.
223typedef 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.
228typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
229
230class reader;
231
233build_translation_unit_and_add_to_ir(reader& rdr,
234 Dwarf_Die* die,
235 char address_size);
236
237static void
238maybe_propagate_canonical_type(const reader& rdr,
239 const Dwarf_Die* l,
240 const Dwarf_Die* r);
241
242static void
243propagate_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.
249typedef 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.
253typedef 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.
260typedef 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.
264typedef 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.
268typedef 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.
272typedef 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.
276typedef 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.
285struct 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.
352typedef vector<imported_unit_point> imported_unit_points_type;
353
354/// Convenience typedef for a vector of @ref imported_unit_point.
355typedef 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.
366static bool
367operator<(const imported_unit_point& l, const imported_unit_point& r)
368{return l.offset_of_import < r.offset_of_import;}
369
370static bool
371get_parent_die(const reader& rdr,
372 const Dwarf_Die* die,
373 Dwarf_Die& parent_die,
374 size_t where_offset);
375
376static bool
377get_scope_die(const reader& rdr,
378 const Dwarf_Die* die,
379 size_t where_offset,
380 Dwarf_Die& scope_die);
381
382static bool
383get_die_language(const Dwarf_Die *die, translation_unit::language &lang) ;
384
385static bool
386die_is_in_c(const Dwarf_Die *die);
387
388static bool
389die_is_in_cplus_plus(const Dwarf_Die *die);
390
391static bool
392die_is_in_c_or_cplusplus(const Dwarf_Die *die);
393
394static bool
395die_is_anonymous(const Dwarf_Die* die);
396
397static bool
398die_is_anonymous_data_member(const Dwarf_Die* die);
399
400static bool
401die_is_type(const Dwarf_Die* die);
402
403static bool
404die_is_decl(const Dwarf_Die* die);
405
406static bool
407die_is_declaration_only(Dwarf_Die* die);
408
409static bool
410die_is_variable_decl(const Dwarf_Die *die);
411
412static bool
413die_is_function_decl(const Dwarf_Die *die);
414
415static bool
416die_has_size_attribute(const Dwarf_Die *die);
417
418static bool
419die_has_no_child(const Dwarf_Die *die);
420
421static bool
422die_is_namespace(const Dwarf_Die* die);
423
424static bool
425die_is_unspecified(Dwarf_Die* die);
426
427static bool
428die_is_void_type(Dwarf_Die* die);
429
430static bool
431die_is_pointer_type(const Dwarf_Die* die);
432
433static bool
434pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
435
436static bool
437die_is_reference_type(const Dwarf_Die* die);
438
439static bool
440die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
441
442static bool
443die_is_pointer_or_reference_type(const Dwarf_Die* die);
444
445static bool
446die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
447
448static bool
449die_is_class_type(const Dwarf_Die* die);
450
451static bool
452die_is_qualified_type(const Dwarf_Die* die);
453
454static bool
455die_is_function_type(const Dwarf_Die *die);
456
457static bool
458die_has_object_pointer(const Dwarf_Die* die,
459 Dwarf_Die& object_pointer);
460
461static bool
462die_has_children(const Dwarf_Die* die);
463
464static bool
465fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die);
466
467static bool
468member_fn_die_has_this_pointer(const reader& rdr,
469 const Dwarf_Die* die,
470 size_t where_offset,
471 Dwarf_Die& class_die,
472 Dwarf_Die& object_pointer_die);
473
474static bool
475die_this_pointer_from_object_pointer(Dwarf_Die* die,
476 Dwarf_Die& this_pointer);
477
478static bool
479die_this_pointer_is_const(Dwarf_Die* die);
480
481static bool
482die_object_pointer_is_for_const_method(Dwarf_Die* die);
483
484static bool
485is_type_die_to_be_canonicalized(const Dwarf_Die *die);
486
487static bool
488die_is_at_class_scope(const reader& rdr,
489 const Dwarf_Die* die,
490 size_t where_offset,
491 Dwarf_Die& class_scope_die);
492static bool
493eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
494 size_t expr_len,
495 int64_t& value,
496 bool& is_tls_address);
497
499dwarf_language_to_tu_language(size_t l);
500
501static bool
502die_unsigned_constant_attribute(const Dwarf_Die* die,
503 unsigned attr_name,
504 uint64_t& cst);
505
506static bool
507die_signed_constant_attribute(const Dwarf_Die*die,
508 unsigned attr_name,
509 int64_t& cst);
510
511static bool
512die_constant_attribute(const Dwarf_Die *die,
513 unsigned attr_name,
514 bool is_signed,
515 array_type_def::subrange_type::bound_value &value);
516
517static bool
518die_member_offset(const reader& rdr,
519 const Dwarf_Die* die,
520 int64_t& offset);
521
522static bool
523form_is_DW_FORM_strx(unsigned form);
524
525static bool
526form_is_DW_FORM_line_strp(unsigned form);
527
528static bool
529die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
530
531static string
532die_name(const Dwarf_Die* die);
533
534static void
535die_name_and_linkage_name(const Dwarf_Die* die,
536 string& name,
537 string& linkage_name);
538static location
539die_location(const reader& rdr, const Dwarf_Die* die);
540
541static bool
542die_location_address(Dwarf_Die* die,
543 Dwarf_Addr& address,
544 bool& is_tls_address);
545
546static bool
547die_die_attribute(const Dwarf_Die* die,
548 unsigned attr_name,
549 Dwarf_Die& result,
550 bool recursively = true);
551
552static bool
553die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
554
555static bool
556subrange_die_indirect_bound_value(const Dwarf_Die *die,
557 unsigned attr_name,
558 array_type_def::subrange_type::bound_value& v,
559 bool& is_signed);
560
561static bool
562subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
563 unsigned attr_name,
564 Dwarf_Die& referenced_subrange);
565static string
566get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
567
568static string
569build_internal_anonymous_die_name(const string &base_name,
570 size_t anonymous_type_index);
571
572static string
573get_internal_anonymous_die_name(Dwarf_Die *die,
574 size_t anonymous_type_index);
575
576static string
577die_qualified_type_name(const reader& rdr,
578 const Dwarf_Die* die,
579 size_t where);
580
581static string
582die_qualified_decl_name(const reader& rdr,
583 const Dwarf_Die* die,
584 size_t where);
585
586static string
587die_qualified_name(const reader& rdr,
588 const Dwarf_Die* die,
589 size_t where);
590
591static bool
592die_qualified_type_name_empty(const reader& rdr,
593 const Dwarf_Die* die, size_t where,
594 string &qualified_name);
595
596static void
597die_return_and_parm_names_from_fn_type_die(const reader& rdr,
598 const Dwarf_Die* die,
599 size_t where_offset,
600 bool pretty_print,
601 string &return_type_name,
602 string &class_name,
603 vector<string>& parm_names,
604 bool& is_const,
605 bool& is_static);
606
607static string
608die_function_signature(const reader& rdr,
609 const Dwarf_Die *die,
610 size_t where_offset);
611
612static bool
613die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
614
615static bool
616die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
617
618static bool
619die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die);
620
621static bool
622die_function_type_is_method_type(const reader& rdr,
623 const Dwarf_Die *die,
624 size_t where_offset,
625 Dwarf_Die& object_pointer_die,
626 Dwarf_Die& class_die,
627 bool& is_static);
628
629static string
630die_pretty_print_type(reader& rdr,
631 const Dwarf_Die* die,
632 size_t where_offset);
633
634static string
635die_pretty_print_decl(reader& rdr,
636 const Dwarf_Die* die,
637 size_t where_offset);
638
639static string
640die_pretty_print(reader& rdr,
641 const Dwarf_Die* die,
642 size_t where_offset);
643
644static void
645maybe_canonicalize_type(const type_base_sptr& t,
646 reader& rdr);
647
648static uint64_t
649get_default_array_lower_bound(translation_unit::language l);
650
651static bool
652find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
653 Dwarf_Off,
654 imported_unit_points_type::const_iterator&);
655
657build_subrange_type(reader& rdr,
658 const Dwarf_Die* die,
659 size_t where_offset,
660 bool associate_type_to_die = true);
661
662static void
663build_subranges_from_array_type_die(reader& rdr,
664 const Dwarf_Die* die,
666 size_t where_offset,
667 bool associate_type_to_die = true);
668
670compare_dies(const reader& rdr,
671 const Dwarf_Die *l, const Dwarf_Die *r,
672 bool update_canonical_dies_on_the_fly);
673
674static bool
675compare_dies_during_canonicalization(reader& rdr,
676 const Dwarf_Die *l, const Dwarf_Die *r,
677 bool update_canonical_dies_on_the_fly);
678
679static bool
680get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
681
682/// Get the language used to generate a given DIE.
683///
684/// @param die the DIE to consider.
685///
686/// @param lang the resulting language.
687///
688/// @return true iff the language of the DIE was found.
689static bool
690get_die_language(const Dwarf_Die *die, translation_unit::language &lang)
691{
692 Dwarf_Die cu_die;
693 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
694
695 uint64_t l = 0;
696 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
697 return false;
698
699 lang = dwarf_language_to_tu_language(l);
700 return true;
701}
702
703/// Test if a given DIE originates from a program written in the C
704/// language.
705///
706/// @param die the DIE to consider.
707///
708/// @return true iff @p die originates from a program in the C
709/// language.
710static bool
711die_is_in_c(const Dwarf_Die *die)
712{
713 translation_unit::language l = translation_unit::LANG_UNKNOWN;
714 if (!get_die_language(die, l))
715 return false;
716 return is_c_language(l);
717}
718
719/// Test if a given DIE originates from a program written in the C++
720/// language.
721///
722/// @param die the DIE to consider.
723///
724/// @return true iff @p die originates from a program in the C++
725/// language.
726static bool
727die_is_in_cplus_plus(const Dwarf_Die *die)
728{
729 translation_unit::language l = translation_unit::LANG_UNKNOWN;
730 if (!get_die_language(die, l))
731 return false;
732 return is_cplus_plus_language(l);
733}
734
735/// Test if a given DIE originates from a program written either in
736/// C or C++.
737///
738/// @param die the DIE to consider.
739///
740/// @return true iff @p die originates from a program written either in
741/// C or C++.
742static bool
743die_is_in_c_or_cplusplus(const Dwarf_Die *die)
744{
745 translation_unit::language l = translation_unit::LANG_UNKNOWN;
746 if (!get_die_language(die, l))
747 return false;
748 return (is_cplus_plus_language(l) || is_c_language(l));
749}
750
751/// Compare a symbol name against another name, possibly demangling
752/// the symbol_name before performing the comparison.
753///
754/// @param symbol_name the symbol_name to take in account.
755///
756/// @param name the second name to take in account.
757///
758/// @param demangle if true, demangle @p symbol_name and compare the
759/// result of the demangling with @p name.
760///
761/// @return true iff symbol_name equals name.
762static bool
763compare_symbol_name(const string& symbol_name,
764 const string& name,
765 bool demangle)
766{
767 if (demangle)
768 {
769 string m = demangle_cplus_mangled_name(symbol_name);
770 return m == name;
771 }
772 return symbol_name == name;
773}
774
775/// Lookup a symbol using the SysV ELF hash table.
776///
777/// Note that this function hasn't been tested. So it hasn't been
778/// debugged yet. IOW, it is not known to work. Or rather, it's
779/// almost like it's surely doesn't work ;-)
780///
781/// Use it at your own risks. :-)
782///
783///@parm env the environment we are operating from.
784///
785/// @param elf_handle the elf_handle to use.
786///
787/// @param sym_name the symbol name to look for.
788///
789/// @param ht_index the index (in the section headers table) of the
790/// hash table section to use.
791///
792/// @param sym_tab_index the index (in the section headers table) of
793/// the symbol table to use.
794///
795/// @param demangle if true, demangle @p sym_name before comparing it
796/// to names from the symbol table.
797///
798/// @param syms_found a vector of symbols found with the name @p
799/// sym_name. table.
800static bool
801lookup_symbol_from_sysv_hash_tab(const environment& env,
802 Elf* elf_handle,
803 const string& sym_name,
804 size_t ht_index,
805 size_t sym_tab_index,
806 bool demangle,
807 vector<elf_symbol_sptr>& syms_found)
808{
809 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
810 ABG_ASSERT(sym_tab_section);
811
812 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
813 ABG_ASSERT(sym_tab_data);
814
815 GElf_Shdr sheader_mem;
816 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
817 &sheader_mem);
818 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
819 ABG_ASSERT(hash_section);
820
821 // Poke at the different parts of the hash table and get them ready
822 // to be used.
823 unsigned long hash = elf_hash(sym_name.c_str());
824 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
825 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
826 size_t nb_buckets = ht_data[0];
827 size_t nb_chains = ht_data[1];
828
829 if (nb_buckets == 0)
830 // An empty hash table. Not sure if that is possible, but it
831 // would mean an empty table of exported symbols.
832 return false;
833
834 //size_t nb_chains = ht_data[1];
835 Elf32_Word* ht_buckets = &ht_data[2];
836 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
837
838 // Now do the real work.
839 size_t bucket = hash % nb_buckets;
840 size_t symbol_index = ht_buckets[bucket];
841
842 GElf_Sym symbol;
843 const char* sym_name_str;
844 size_t sym_size;
845 elf_symbol::type sym_type;
846 elf_symbol::binding sym_binding;
847 elf_symbol::visibility sym_visibility;
848 bool found = false;
849
850 do
851 {
852 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
853 sym_name_str = elf_strptr(elf_handle,
854 sym_tab_section_header->sh_link,
855 symbol.st_name);
856 if (sym_name_str
857 && compare_symbol_name(sym_name_str, sym_name, demangle))
858 {
859 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
860 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
861 sym_visibility =
862 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
863 sym_size = symbol.st_size;
864 elf_symbol::version ver;
865 if (get_version_for_symbol(elf_handle, symbol_index,
866 /*get_def_version=*/true, ver))
867 ABG_ASSERT(!ver.str().empty());
868 elf_symbol_sptr symbol_found =
870 symbol_index,
871 sym_size,
872 sym_name_str,
873 sym_type,
874 sym_binding,
875 symbol.st_shndx != SHN_UNDEF,
876 symbol.st_shndx == SHN_COMMON,
877 ver, sym_visibility);
878 syms_found.push_back(symbol_found);
879 found = true;
880 }
881 symbol_index = ht_chains[symbol_index];
882 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
883
884 return found;
885}
886
887/// Get the size of the elf class, in bytes.
888///
889/// @param elf_handle the elf handle to use.
890///
891/// @return the size computed.
892static char
893get_elf_class_size_in_bytes(Elf* elf_handle)
894{
895 char result = 0;
896 GElf_Ehdr hdr;
897
898 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
899 int c = hdr.e_ident[EI_CLASS];
900
901 switch (c)
902 {
903 case ELFCLASS32:
904 result = 4;
905 break;
906 case ELFCLASS64:
907 result = 8;
908 break;
909 default:
911 }
912
913 return result;
914}
915
916/// Get a given word of a bloom filter, referred to by the index of
917/// the word.
918///
919/// The bloom word size depends on the current elf class (32 bits for
920/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
921/// abstracts that nicely.
922///
923/// @param elf_handle the elf handle to use.
924///
925/// @param bloom_filter the bloom filter to consider.
926///
927/// @param index the index of the bloom filter to return.
928///
929/// @return a 64 bits work containing the bloom word found at index @p
930/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
931/// most significant bytes of the result are going to be zero.
932static Elf64_Xword
933bloom_word_at(Elf* elf_handle,
934 Elf32_Word* bloom_filter,
935 size_t index)
936{
937 Elf64_Xword result = 0;
938 GElf_Ehdr h;
939 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
940 int c;
941 c = h.e_ident[EI_CLASS];
942
943 switch(c)
944 {
945 case ELFCLASS32:
946 result = bloom_filter[index];
947 break ;
948 case ELFCLASS64:
949 {
950 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
951 result = f[index];
952 }
953 break;
954 default:
955 abort();
956 }
957
958 return result;
959}
960
961/// The abstraction of the gnu elf hash table.
962///
963/// The members of this struct are explained at
964/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
965/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
966struct gnu_ht
967{
968 size_t nb_buckets;
969 Elf32_Word* buckets;
970 Elf32_Word* chain;
971 size_t first_sym_index;
972 size_t bf_nwords;
973 size_t bf_size;
974 Elf32_Word* bloom_filter;
975 size_t shift;
976 size_t sym_count;
977 Elf_Scn* sym_tab_section;
978 GElf_Shdr sym_tab_section_header;
979
980 gnu_ht()
981 : nb_buckets(0),
982 buckets(0),
983 chain(0),
984 first_sym_index(0),
985 bf_nwords(0),
986 bf_size(0),
987 bloom_filter(0),
988 shift(0),
989 sym_count(0),
990 sym_tab_section(0)
991 {}
992}; // end struct gnu_ht
993
994/// Setup the members of the gnu hash table.
995///
996/// @param elf_handle a handle on the elf file to use.
997///
998/// @param ht_index the index (into the elf section headers table) of
999/// the hash table section to use.
1000///
1001/// @param sym_tab_index the index (into the elf section headers
1002/// table) of the symbol table the gnu hash table is about.
1003///
1004/// @param ht the resulting hash table.
1005///
1006/// @return true iff the hash table @ ht could be setup.
1007static bool
1008setup_gnu_ht(Elf* elf_handle,
1009 size_t ht_index,
1010 size_t sym_tab_index,
1011 gnu_ht& ht)
1012{
1013 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1014 ABG_ASSERT(ht.sym_tab_section);
1015 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
1016 ht.sym_count =
1017 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
1018 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1019 ABG_ASSERT(hash_section);
1020
1021 // Poke at the different parts of the hash table and get them ready
1022 // to be used.
1023 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1024 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1025
1026 ht.nb_buckets = ht_data[0];
1027 if (ht.nb_buckets == 0)
1028 // An empty hash table. Not sure if that is possible, but it
1029 // would mean an empty table of exported symbols.
1030 return false;
1031 ht.first_sym_index = ht_data[1];
1032 // The number of words used by the bloom filter. A size of a word
1033 // is ELFCLASS.
1034 ht.bf_nwords = ht_data[2];
1035 // The shift used by the bloom filter code.
1036 ht.shift = ht_data[3];
1037 // The data of the bloom filter proper.
1038 ht.bloom_filter = &ht_data[4];
1039 // The size of the bloom filter in 4 bytes word. This is going to
1040 // be used to index the 'bloom_filter' above, which is of type
1041 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1042 // words.
1043 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1044 // The buckets of the hash table.
1045 ht.buckets = ht.bloom_filter + ht.bf_size;
1046 // The chain of the hash table.
1047 ht.chain = ht.buckets + ht.nb_buckets;
1048
1049 return true;
1050}
1051
1052/// Look into the symbol tables of the underlying elf file and find
1053/// the symbol we are being asked.
1054///
1055/// This function uses the GNU hash table for the symbol lookup.
1056///
1057/// The reference of for the implementation of this function can be
1058/// found at:
1059/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1060/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1061///
1062/// @param elf_handle the elf handle to use.
1063///
1064/// @param sym_name the name of the symbol to look for.
1065///
1066/// @param ht_index the index of the hash table header to use.
1067///
1068/// @param sym_tab_index the index of the symbol table header to use
1069/// with this hash table.
1070///
1071/// @param demangle if true, demangle @p sym_name.
1072///
1073/// @param syms_found the vector of symbols found with the name @p
1074/// sym_name.
1075///
1076/// @return true if a symbol was actually found.
1077static bool
1078lookup_symbol_from_gnu_hash_tab(const environment& env,
1079 Elf* elf_handle,
1080 const string& sym_name,
1081 size_t ht_index,
1082 size_t sym_tab_index,
1083 bool demangle,
1084 vector<elf_symbol_sptr>& syms_found)
1085{
1086 gnu_ht ht;
1087 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
1088 return false;
1089
1090 // Now do the real work.
1091
1092 // Compute bloom hashes (GNU hash and second bloom specific hashes).
1093 size_t h1 = elf_gnu_hash(sym_name.c_str());
1094 size_t h2 = h1 >> ht.shift;
1095 // The size of one of the words used in the bloom
1096 // filter, in bits.
1097 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1098 int n = (h1 / c) % ht.bf_nwords;
1099 // The bitmask of the bloom filter has a size of either 32-bits on
1100 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1101 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1102 // type used here. When dealing with 32bits binaries, the upper
1103 // bits of the bitmask will be zero anyway.
1104 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1105
1106 // Test if the symbol is *NOT* present in this ELF file.
1107 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1108 return false;
1109
1110 size_t i = ht.buckets[h1 % ht.nb_buckets];
1111 if (i == STN_UNDEF)
1112 return false;
1113
1114 Elf32_Word stop_word, *stop_wordp;
1115 elf_symbol::version ver;
1116 GElf_Sym symbol;
1117 const char* sym_name_str;
1118 bool found = false;
1119
1120 elf_symbol::type sym_type;
1121 elf_symbol::binding sym_binding;
1122 elf_symbol::visibility sym_visibility;
1123
1124 // Let's walk the hash table and record the versions of all the
1125 // symbols which name equal sym_name.
1126 for (i = ht.buckets[h1 % ht.nb_buckets],
1127 stop_wordp = &ht.chain[i - ht.first_sym_index];
1128 i != STN_UNDEF
1129 && (stop_wordp
1130 < ht.chain + (ht.sym_count - ht.first_sym_index));
1131 ++i, ++stop_wordp)
1132 {
1133 stop_word = *stop_wordp;
1134 if ((stop_word & ~ 1)!= (h1 & ~1))
1135 // A given bucket can reference several hashes. Here we
1136 // stumbled across a hash value different from the one we are
1137 // looking for. Let's keep walking.
1138 continue;
1139
1140 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1141 i, &symbol));
1142 sym_name_str = elf_strptr(elf_handle,
1143 ht.sym_tab_section_header.sh_link,
1144 symbol.st_name);
1145 if (sym_name_str
1146 && compare_symbol_name(sym_name_str, sym_name, demangle))
1147 {
1148 // So we found a symbol (in the symbol table) that equals
1149 // sym_name. Now lets try to get its version and record it.
1150 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1151 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1152 sym_visibility =
1153 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1154
1155 if (get_version_for_symbol(elf_handle, i,
1156 /*get_def_version=*/true,
1157 ver))
1158 ABG_ASSERT(!ver.str().empty());
1159
1160 elf_symbol_sptr symbol_found =
1161 elf_symbol::create(env, i,
1162 symbol.st_size,
1163 sym_name_str,
1164 sym_type, sym_binding,
1165 symbol.st_shndx != SHN_UNDEF,
1166 symbol.st_shndx == SHN_COMMON,
1167 ver, sym_visibility);
1168 syms_found.push_back(symbol_found);
1169 found = true;
1170 }
1171
1172 if (stop_word & 1)
1173 // The last bit of the stop_word is 1. That means we need to
1174 // stop here. We reached the end of the chain of values
1175 // referenced by the hask bucket.
1176 break;
1177 }
1178 return found;
1179}
1180
1181/// Look into the symbol tables of the underlying elf file and find
1182/// the symbol we are being asked.
1183///
1184/// This function uses the elf hash table (be it the GNU hash table or
1185/// the sysv hash table) for the symbol lookup.
1186///
1187/// @param env the environment we are operating from.
1188///
1189/// @param elf_handle the elf handle to use.
1190///
1191/// @param ht_kind the kind of hash table to use. This is returned by
1192/// the function function find_hash_table_section_index.
1193///
1194/// @param ht_index the index (in the section headers table) of the
1195/// hash table section to use.
1196///
1197/// @param sym_tab_index the index (in section headers table) of the
1198/// symbol table index to use with this hash table.
1199///
1200/// @param symbol_name the name of the symbol to look for.
1201///
1202/// @param demangle if true, demangle @p sym_name.
1203///
1204/// @param syms_found the symbols that were actually found with the
1205/// name @p symbol_name.
1206///
1207/// @return true iff the function found the symbol from the elf hash
1208/// table.
1209static bool
1210lookup_symbol_from_elf_hash_tab(const environment& env,
1211 Elf* elf_handle,
1212 hash_table_kind ht_kind,
1213 size_t ht_index,
1214 size_t symtab_index,
1215 const string& symbol_name,
1216 bool demangle,
1217 vector<elf_symbol_sptr>& syms_found)
1218{
1219 if (elf_handle == 0 || symbol_name.empty())
1220 return false;
1221
1222 if (ht_kind == NO_HASH_TABLE_KIND)
1223 return false;
1224
1225 if (ht_kind == SYSV_HASH_TABLE_KIND)
1226 return lookup_symbol_from_sysv_hash_tab(env,
1227 elf_handle, symbol_name,
1228 ht_index,
1229 symtab_index,
1230 demangle,
1231 syms_found);
1232 else if (ht_kind == GNU_HASH_TABLE_KIND)
1233 return lookup_symbol_from_gnu_hash_tab(env,
1234 elf_handle, symbol_name,
1235 ht_index,
1236 symtab_index,
1237 demangle,
1238 syms_found);
1239 return false;
1240}
1241
1242/// Lookup a symbol from the symbol table directly.
1243///
1244///
1245/// @param env the environment we are operating from.
1246///
1247/// @param elf_handle the elf handle to use.
1248///
1249/// @param sym_name the name of the symbol to look up.
1250///
1251/// @param sym_tab_index the index (in the section headers table) of
1252/// the symbol table section.
1253///
1254/// @param demangle if true, demangle the names found in the symbol
1255/// table before comparing them with @p sym_name.
1256///
1257/// @param sym_name_found the actual name of the symbol found.
1258///
1259/// @param sym_type the type of the symbol found.
1260///
1261/// @param sym_binding the binding of the symbol found.
1262///
1263/// @param sym_versions the versions of the symbol found.
1264///
1265/// @return true iff the symbol was found.
1266static bool
1267lookup_symbol_from_symtab(const environment& env,
1268 Elf* elf_handle,
1269 const string& sym_name,
1270 size_t sym_tab_index,
1271 bool demangle,
1272 vector<elf_symbol_sptr>& syms_found)
1273{
1274 // TODO: read all of the symbol table, store it in memory in a data
1275 // structure that associates each symbol with its versions and in
1276 // which lookups of a given symbol is fast.
1277 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1278 ABG_ASSERT(sym_tab_section);
1279
1280 GElf_Shdr header_mem;
1281 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1282 &header_mem);
1283
1284 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1285 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1286 GElf_Sym* sym;
1287 char* name_str = 0;
1288 elf_symbol::version ver;
1289 bool found = false;
1290
1291 for (size_t i = 0; i < symcount; ++i)
1292 {
1293 GElf_Sym sym_mem;
1294 sym = gelf_getsym(symtab, i, &sym_mem);
1295 name_str = elf_strptr(elf_handle,
1296 sym_tab_header->sh_link,
1297 sym->st_name);
1298
1299 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1300 {
1301 elf_symbol::type sym_type =
1302 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1303 elf_symbol::binding sym_binding =
1304 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1305 elf_symbol::visibility sym_visibility =
1306 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1307 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1308 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1309
1310 if (get_version_for_symbol(elf_handle, i,
1311 /*get_def_version=*/sym_is_defined,
1312 ver))
1313 ABG_ASSERT(!ver.str().empty());
1314 elf_symbol_sptr symbol_found =
1315 elf_symbol::create(env, i, sym->st_size,
1316 name_str, sym_type,
1317 sym_binding, sym_is_defined,
1318 sym_is_common, ver, sym_visibility);
1319 syms_found.push_back(symbol_found);
1320 found = true;
1321 }
1322 }
1323
1324 if (found)
1325 return true;
1326
1327 return false;
1328}
1329
1330/// Look into the symbol tables of the underlying elf file and see
1331/// if we find a given symbol.
1332///
1333/// @param env the environment we are operating from.
1334///
1335/// @param symbol_name the name of the symbol to look for.
1336///
1337/// @param demangle if true, try to demangle the symbol name found in
1338/// the symbol table before comparing it to @p symbol_name.
1339///
1340/// @param syms_found the list of symbols found, with the name @p
1341/// symbol_name.
1342///
1343/// @param sym_type this is set to the type of the symbol found. This
1344/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1345/// STT_FUNC, STT_IFUNC, etc ...
1346///
1347/// Note that this parameter is set iff the function returns true.
1348///
1349/// @param sym_binding this is set to the binding of the symbol found.
1350/// This is a standard elf.h value of the symbol binding kind, that
1351/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1352///
1353/// @param symbol_versions the versions of the symbol @p symbol_name,
1354/// if it was found.
1355///
1356/// @return true iff a symbol with the name @p symbol_name was found.
1357static bool
1358lookup_symbol_from_elf(const environment& env,
1359 Elf* elf_handle,
1360 const string& symbol_name,
1361 bool demangle,
1362 vector<elf_symbol_sptr>& syms_found)
1363{
1364 size_t hash_table_index = 0, symbol_table_index = 0;
1365 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1366
1367 if (!demangle)
1368 ht_kind = find_hash_table_section_index(elf_handle,
1369 hash_table_index,
1370 symbol_table_index);
1371
1372 if (ht_kind == NO_HASH_TABLE_KIND)
1373 {
1374 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1375 return false;
1376
1377 return lookup_symbol_from_symtab(env,
1378 elf_handle,
1379 symbol_name,
1380 symbol_table_index,
1381 demangle,
1382 syms_found);
1383 }
1384
1385 return lookup_symbol_from_elf_hash_tab(env,
1386 elf_handle,
1387 ht_kind,
1388 hash_table_index,
1389 symbol_table_index,
1390 symbol_name,
1391 demangle,
1392 syms_found);
1393}
1394
1395/// Look into the symbol tables of the underlying elf file and see if
1396/// we find a given public (global or weak) symbol of function type.
1397///
1398/// @param env the environment we are operating from.
1399///
1400/// @param elf_handle the elf handle to use for the query.
1401///
1402/// @param symbol_name the function symbol to look for.
1403///
1404/// @param func_syms the vector of public functions symbols found, if
1405/// any.
1406///
1407/// @return true iff the symbol was found.
1408static bool
1409lookup_public_function_symbol_from_elf(environment& env,
1410 Elf* elf_handle,
1411 const string& symbol_name,
1412 vector<elf_symbol_sptr>& func_syms)
1413{
1414 vector<elf_symbol_sptr> syms_found;
1415 bool found = false;
1416
1417 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1418 /*demangle=*/false, syms_found))
1419 {
1420 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1421 i != syms_found.end();
1422 ++i)
1423 {
1424 elf_symbol::type type = (*i)->get_type();
1425 elf_symbol::binding binding = (*i)->get_binding();
1426
1427 if ((type == elf_symbol::FUNC_TYPE
1428 || type == elf_symbol::GNU_IFUNC_TYPE
1429 || type == elf_symbol::COMMON_TYPE)
1430 && (binding == elf_symbol::GLOBAL_BINDING
1431 || binding == elf_symbol::WEAK_BINDING))
1432 {
1433 func_syms.push_back(*i);
1434 found = true;
1435 }
1436 }
1437 }
1438
1439 return found;
1440}
1441
1442// ---------------------------------------
1443// <location expression evaluation types>
1444// ---------------------------------------
1445
1446/// An abstraction of a value representing the result of the
1447/// evaluation of a dwarf expression. This is abstraction represents
1448/// a partial view on the possible values because we are only
1449/// interested in extracting the latest and longuest constant
1450/// sub-expression of a given dwarf expression.
1451class expr_result
1452{
1453 bool is_const_;
1454 int64_t const_value_;
1455
1456public:
1457 expr_result()
1458 : is_const_(true),
1459 const_value_(0)
1460 {}
1461
1462 expr_result(bool is_const)
1463 : is_const_(is_const),
1464 const_value_(0)
1465 {}
1466
1467 explicit expr_result(int64_t v)
1468 :is_const_(true),
1469 const_value_(v)
1470 {}
1471
1472 /// @return true if the value is a constant. Otherwise, return
1473 /// false, meaning the value represents a quantity for which we need
1474 /// inferior (a running program) state to determine the value.
1475 bool
1476 is_const() const
1477 {return is_const_;}
1478
1479
1480 /// @param f a flag saying if the value is set to a constant or not.
1481 void
1482 is_const(bool f)
1483 {is_const_ = f;}
1484
1485 /// Get the current constant value iff this represents a
1486 /// constant.
1487 ///
1488 /// @param value the out parameter. Is set to the constant value of
1489 /// the @ref expr_result. This is set iff the function return true.
1490 ///
1491 ///@return true if this has a constant value, false otherwise.
1492 bool
1493 const_value(int64_t& value)
1494 {
1495 if (is_const())
1496 {
1497 value = const_value_;
1498 return true;
1499 }
1500 return false;
1501 }
1502
1503 /// Getter of the constant value of the current @ref expr_result.
1504 ///
1505 /// Note that the current @ref expr_result must be constant,
1506 /// otherwise the current process is aborted.
1507 ///
1508 /// @return the constant value of the current @ref expr_result.
1509 int64_t
1510 const_value() const
1511 {
1512 ABG_ASSERT(is_const());
1513 return const_value_;
1514 }
1515
1516 operator int64_t() const
1517 {return const_value();}
1518
1519 expr_result&
1520 operator=(const int64_t v)
1521 {
1522 const_value_ = v;
1523 return *this;
1524 }
1525
1526 bool
1527 operator==(const expr_result& o) const
1528 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1529
1530 bool
1531 operator>=(const expr_result& o) const
1532 {return const_value_ >= o.const_value_;}
1533
1534 bool
1535 operator<=(const expr_result& o) const
1536 {return const_value_ <= o.const_value_;}
1537
1538 bool
1539 operator>(const expr_result& o) const
1540 {return const_value_ > o.const_value_;}
1541
1542 bool
1543 operator<(const expr_result& o) const
1544 {return const_value_ < o.const_value_;}
1545
1546 expr_result
1547 operator+(const expr_result& v) const
1548 {
1549 expr_result r(*this);
1550 r.const_value_ += v.const_value_;
1551 r.is_const_ = r.is_const_ && v.is_const_;
1552 return r;
1553 }
1554
1555 expr_result&
1556 operator+=(int64_t v)
1557 {
1558 const_value_ += v;
1559 return *this;
1560 }
1561
1562 expr_result
1563 operator-(const expr_result& v) const
1564 {
1565 expr_result r(*this);
1566 r.const_value_ -= v.const_value_;
1567 r.is_const_ = r.is_const_ && v.is_const_;
1568 return r;
1569 }
1570
1571 expr_result
1572 operator%(const expr_result& v) const
1573 {
1574 expr_result r(*this);
1575 r.const_value_ %= v.const_value_;
1576 r.is_const_ = r.is_const_ && v.is_const();
1577 return r;
1578 }
1579
1580 expr_result
1581 operator*(const expr_result& v) const
1582 {
1583 expr_result r(*this);
1584 r.const_value_ *= v.const_value_;
1585 r.is_const_ = r.is_const_ && v.is_const();
1586 return r;
1587 }
1588
1589 expr_result
1590 operator|(const expr_result& v) const
1591 {
1592 expr_result r(*this);
1593 r.const_value_ |= v.const_value_;
1594 r.is_const_ = r.is_const_ && v.is_const_;
1595 return r;
1596 }
1597
1598 expr_result
1599 operator^(const expr_result& v) const
1600 {
1601 expr_result r(*this);
1602 r.const_value_ ^= v.const_value_;
1603 r.is_const_ = r.is_const_ && v.is_const_;
1604 return r;
1605 }
1606
1607 expr_result
1608 operator>>(const expr_result& v) const
1609 {
1610 expr_result r(*this);
1611 r.const_value_ = r.const_value_ >> v.const_value_;
1612 r.is_const_ = r.is_const_ && v.is_const_;
1613 return r;
1614 }
1615
1616 expr_result
1617 operator<<(const expr_result& v) const
1618 {
1619 expr_result r(*this);
1620 r.const_value_ = r.const_value_ << v.const_value_;
1621 r.is_const_ = r.is_const_ && v.is_const_;
1622 return r;
1623 }
1624
1625 expr_result
1626 operator~() const
1627 {
1628 expr_result r(*this);
1629 r.const_value_ = ~r.const_value_;
1630 return r;
1631 }
1632
1633 expr_result
1634 neg() const
1635 {
1636 expr_result r(*this);
1637 r.const_value_ = -r.const_value_;
1638 return r;
1639 }
1640
1641 expr_result
1642 abs() const
1643 {
1644 expr_result r = *this;
1645 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1646 return r;
1647 }
1648
1649 expr_result
1650 operator&(const expr_result& o)
1651 {
1652 expr_result r(*this);
1653 r.const_value_ &= o.const_value_;
1654 r.is_const_ = r.is_const_ && o.is_const_;
1655 return r;
1656 }
1657
1658 expr_result
1659 operator/(const expr_result& o)
1660 {
1661 expr_result r(*this);
1662 r.is_const_ = r.is_const_ && o.is_const_;
1663 return r.const_value() / o.const_value();
1664 }
1665};// class end expr_result;
1666
1667/// A class that implements a stack of @ref expr_result, to be used in
1668/// the engine evaluating DWARF expressions.
1669class expr_result_stack_type
1670{
1671 vector<expr_result> elems_;
1672
1673public:
1674
1675 expr_result_stack_type()
1676 {elems_.reserve(4);}
1677
1678 expr_result&
1679 operator[](unsigned i)
1680 {
1681 unsigned s = elems_.size();
1682 ABG_ASSERT(s > i);
1683 return elems_[s - 1 -i];
1684 }
1685
1686 const expr_result&
1687 operator[](unsigned i) const
1688 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1689
1690 unsigned
1691 size() const
1692 {return elems_.size();}
1693
1694 vector<expr_result>::reverse_iterator
1695 begin()
1696 {return elems_.rbegin();}
1697
1698 const vector<expr_result>::reverse_iterator
1699 begin() const
1700 {return const_cast<expr_result_stack_type*>(this)->begin();}
1701
1702 vector<expr_result>::reverse_iterator
1703 end()
1704 {return elems_.rend();}
1705
1706 const vector<expr_result>::reverse_iterator
1707 end() const
1708 {return const_cast<expr_result_stack_type*>(this)->end();}
1709
1710 expr_result&
1711 front()
1712 {return elems_.back();}
1713
1714 const expr_result&
1715 front() const
1716 {return const_cast<expr_result_stack_type*>(this)->front();}
1717
1718 void
1719 push_front(expr_result e)
1720 {elems_.push_back(e);}
1721
1722 expr_result
1723 pop_front()
1724 {
1725 expr_result r = front();
1726 elems_.pop_back();
1727 return r;
1728 }
1729
1730 void
1731 erase(vector<expr_result>::reverse_iterator i)
1732 {elems_.erase(--i.base());}
1733
1734 void
1735 clear()
1736 {elems_.clear();}
1737}; // end class expr_result_stack_type
1738
1739/// Abstraction of the evaluation context of a dwarf expression.
1740struct dwarf_expr_eval_context
1741{
1742 expr_result accum;
1743 expr_result_stack_type stack;
1744 // Is set to true if the result of the expression that got evaluated
1745 // is a TLS address.
1746 bool set_tls_addr;
1747
1748 dwarf_expr_eval_context()
1749 : accum(/*is_const=*/false),
1750 set_tls_addr(false)
1751 {
1752 stack.push_front(expr_result(true));
1753 }
1754
1755 void
1756 reset()
1757 {
1758 stack.clear();
1759 stack.push_front(expr_result(true));
1760 accum = expr_result(false);
1761 set_tls_addr = false;
1762 }
1763
1764 /// Set a flag to to tell that the result of the expression that got
1765 /// evaluated is a TLS address.
1766 ///
1767 /// @param f true iff the result of the expression that got
1768 /// evaluated is a TLS address, false otherwise.
1769 void
1770 set_tls_address(bool f)
1771 {set_tls_addr = f;}
1772
1773 /// Getter for the flag that tells if the result of the expression
1774 /// that got evaluated is a TLS address.
1775 ///
1776 /// @return true iff the result of the expression that got evaluated
1777 /// is a TLS address.
1778 bool
1779 set_tls_address() const
1780 {return set_tls_addr;}
1781
1782 expr_result
1783 pop()
1784 {
1785 expr_result r = stack.front();
1786 stack.pop_front();
1787 return r;
1788 }
1789
1790 void
1791 push(const expr_result& v)
1792 {stack.push_front(v);}
1793};//end class dwarf_expr_eval_context
1794
1795// ---------------------------------------
1796// </location expression evaluation types>
1797// ---------------------------------------
1798
1799class reader;
1800
1801typedef shared_ptr<reader> reader_sptr;
1802
1803/// The DWARF reader used to build the ABI corpus from debug info in
1804/// DWARF format.
1805///
1806/// This type is to be instanciated
1807/// abigail::dwarf::reader::create().
1808class reader : public elf_based_reader
1809{
1810public:
1811
1812 /// A set of containers that contains one container per kind of @ref
1813 /// die_source. This allows to associate DIEs to things, depending
1814 /// on the source of the DIE.
1815 template <typename ContainerType>
1816 class die_source_dependant_container_set
1817 {
1818 ContainerType primary_debug_info_container_;
1819 ContainerType alt_debug_info_container_;
1820 ContainerType type_unit_container_;
1821
1822 public:
1823
1824 /// Getter for the container associated to DIEs coming from a
1825 /// given @ref die_source.
1826 ///
1827 /// @param source the die_source for which we want the container.
1828 ///
1829 /// @return the container that associates DIEs coming from @p
1830 /// source to something.
1831 ContainerType&
1832 get_container(die_source source)
1833 {
1834 ContainerType *result = 0;
1835 switch (source)
1836 {
1837 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1838 result = &primary_debug_info_container_;
1839 break;
1840 case ALT_DEBUG_INFO_DIE_SOURCE:
1841 result = &alt_debug_info_container_;
1842 break;
1843 case TYPE_UNIT_DIE_SOURCE:
1844 result = &type_unit_container_;
1845 break;
1846 case NO_DEBUG_INFO_DIE_SOURCE:
1847 case NUMBER_OF_DIE_SOURCES:
1849 }
1850 return *result;
1851 }
1852
1853 /// Getter for the container associated to DIEs coming from a
1854 /// given @ref die_source.
1855 ///
1856 /// @param source the die_source for which we want the container.
1857 ///
1858 /// @return the container that associates DIEs coming from @p
1859 /// source to something.
1860 const ContainerType&
1861 get_container(die_source source) const
1862 {
1863 return const_cast<die_source_dependant_container_set*>(this)->
1864 get_container(source);
1865 }
1866
1867 /// Getter for the container associated to DIEs coming from the
1868 /// same source as a given DIE.
1869 ///
1870 /// @param rdr the DWARF reader to consider.
1871 ///
1872 /// @param die the DIE which should have the same source as the
1873 /// source of the container we want.
1874 ///
1875 /// @return the container that associates DIEs coming from the
1876 /// same source as @p die.
1877 ContainerType&
1878 get_container(const reader& rdr, const Dwarf_Die *die)
1879 {
1880 const die_source source = rdr.get_die_source(die);
1881 return get_container(source);
1882 }
1883
1884 /// Getter for the container associated to DIEs coming from the
1885 /// same source as a given DIE.
1886 ///
1887 /// @param rdr the DWARF reader to consider.
1888 ///
1889 /// @param die the DIE which should have the same source as the
1890 /// source of the container we want.
1891 ///
1892 /// @return the container that associates DIEs coming from the
1893 /// same source as @p die.
1894 const ContainerType&
1895 get_container(const reader& rdr, const Dwarf_Die *die) const
1896 {
1897 return const_cast<die_source_dependant_container_set*>(this)->
1898 get_container(rdr, die);
1899 }
1900
1901 /// Clear the container set.
1902 void
1903 clear()
1904 {
1905 primary_debug_info_container_.clear();
1906 alt_debug_info_container_.clear();
1907 type_unit_container_.clear();
1908 }
1909 }; // end die_dependant_container_set
1910
1911 unsigned short dwarf_version_;
1912 Dwarf_Die* cur_tu_die_;
1913 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1914 // A set of maps (one per kind of die source) that associates a decl
1915 // string representation with the DIEs (offsets) representing that
1916 // decl.
1917 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1918 decl_die_repr_die_offsets_maps_;
1919 // A set of maps (one per kind of die source) that associates a type
1920 // string representation with the DIEs (offsets) representing that
1921 // type.
1922 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1923 type_die_repr_die_offsets_maps_;
1924 mutable die_source_dependant_container_set<die_istring_map_type>
1925 die_qualified_name_maps_;
1926 mutable die_source_dependant_container_set<die_istring_map_type>
1927 die_pretty_repr_maps_;
1928 mutable die_source_dependant_container_set<die_istring_map_type>
1929 die_pretty_type_repr_maps_;
1930 // A set of maps (one per kind of die source) that associates the
1931 // offset of a decl die to its corresponding decl artifact.
1932 mutable die_source_dependant_container_set<die_artefact_map_type>
1933 decl_die_artefact_maps_;
1934 // A set of maps (one per kind of die source) that associates the
1935 // offset of a type die to its corresponding type artifact.
1936 mutable die_source_dependant_container_set<die_artefact_map_type>
1937 type_die_artefact_maps_;
1938 /// A set of vectors (one per kind of die source) that associates
1939 /// the offset of a type DIE to the offset of its canonical DIE.
1940 mutable die_source_dependant_container_set<offset_offset_map_type>
1941 canonical_type_die_offsets_;
1942 /// A set of vectors (one per kind of die source) that associates
1943 /// the offset of a decl DIE to the offset of its canonical DIE.
1944 mutable die_source_dependant_container_set<offset_offset_map_type>
1945 canonical_decl_die_offsets_;
1946 /// A map that associates a function type representations to
1947 /// function types, inside a translation unit.
1948 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1949 /// A map that associates a pair of DIE offsets to the result of the
1950 /// comparison of that pair.
1951 mutable std::unordered_map<std::pair<offset_type,offset_type>,
1953 dwarf_offset_pair_hash> die_comparison_results_;
1954 // The set of types pair that have been canonical-type-propagated.
1955 mutable offset_pair_set_type propagated_types_;
1956 die_class_or_union_map_type die_wip_classes_map_;
1957 die_class_or_union_map_type alternate_die_wip_classes_map_;
1958 die_class_or_union_map_type type_unit_die_wip_classes_map_;
1959 die_function_type_map_type die_wip_function_types_map_;
1960 die_function_type_map_type alternate_die_wip_function_types_map_;
1961 die_function_type_map_type type_unit_die_wip_function_types_map_;
1962 die_function_decl_map_type die_function_with_no_symbol_map_;
1963 vector<type_base_sptr> types_to_canonicalize_;
1964 string_classes_or_unions_map decl_only_classes_map_;
1965 string_enums_map decl_only_enums_map_;
1966 die_tu_map_type die_tu_map_;
1967 translation_unit_sptr cur_tu_;
1968 scope_decl_sptr nil_scope_;
1969 scope_stack_type scope_stack_;
1970 offset_offset_map_type primary_die_parent_map_;
1971 // A map that associates each tu die to a vector of unit import
1972 // points, in the main debug info
1973 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1974 // A map that associates each tu die to a vector of unit import
1975 // points, in the alternate debug info
1976 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1977 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1978 // A DIE -> parent map for DIEs coming from the alternate debug info
1979 // file.
1980 offset_offset_map_type alternate_die_parent_map_;
1981 offset_offset_map_type type_section_die_parent_map_;
1982 list<var_decl_sptr> var_decls_to_add_;
1983#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1984 bool debug_die_canonicalization_is_on_;
1985 bool use_canonical_die_comparison_;
1986#endif
1987 mutable size_t compare_count_;
1988 mutable size_t canonical_propagated_count_;
1989 mutable size_t cancelled_propagation_count_;
1990 mutable optional<bool> leverage_dwarf_factorization_;
1991
1992protected:
1993
1994 reader() = delete;
1995
1996 /// Constructor of reader.
1997 ///
1998 /// @param elf_path the path to the elf file the context is to be
1999 /// used for.
2000 ///
2001 /// @param debug_info_root_paths a vector of pointers to the path to
2002 /// the root directory under which the debug info is to be found for
2003 /// @p elf_path. Leave this empty if the debug info is not in a
2004 /// split file.
2005 ///
2006 /// @param environment the environment used by the current context.
2007 /// This environment contains resources needed by the DWARF reader and by
2008 /// the types and declarations that are to be created later. Note
2009 /// that ABI artifacts that are to be compared all need to be
2010 /// created within the same environment.
2011 ///
2012 /// Please also note that the life time of this environment object
2013 /// must be greater than the life time of the resulting @ref
2014 /// reader the context uses resources that are allocated in
2015 /// the environment.
2016 ///
2017 /// @param load_all_types if set to false only the types that are
2018 /// reachable from publicly exported declarations (of functions and
2019 /// variables) are read. If set to true then all types found in the
2020 /// debug information are loaded.
2021 ///
2022 /// @param linux_kernel_mode if set to true, then consider the special
2023 /// linux kernel symbol tables when determining if a symbol is
2024 /// exported or not.
2025 reader(const string& elf_path,
2026 const vector<char**>& debug_info_root_paths,
2027 environment& environment,
2028 bool load_all_types,
2029 bool linux_kernel_mode)
2030 : elf_based_reader(elf_path,
2031 debug_info_root_paths,
2032 environment)
2033 {
2034 initialize(load_all_types, linux_kernel_mode);
2035 }
2036
2037public:
2038
2039 /// Initializer of reader.
2040 ///
2041 /// Resets the reader so that it can be re-used to read another binary.
2042 ///
2043 /// @param load_all_types if set to false only the types that are
2044 /// reachable from publicly exported declarations (of functions and
2045 /// variables) are read. If set to true then all types found in the
2046 /// debug information are loaded.
2047 ///
2048 /// @param linux_kernel_mode if set to true, then consider the
2049 /// special linux kernel symbol tables when determining if a symbol
2050 /// is exported or not.
2051 void
2052 initialize(bool load_all_types, bool linux_kernel_mode)
2053 {
2054 dwarf_version_ = 0;
2055 cur_tu_die_ = 0;
2056 decl_die_repr_die_offsets_maps_.clear();
2057 type_die_repr_die_offsets_maps_.clear();
2058 die_qualified_name_maps_.clear();
2059 die_pretty_repr_maps_.clear();
2060 die_pretty_type_repr_maps_.clear();
2061 decl_die_artefact_maps_.clear();
2062 type_die_artefact_maps_.clear();
2063 canonical_type_die_offsets_.clear();
2064 canonical_decl_die_offsets_.clear();
2065 die_wip_classes_map_.clear();
2066 alternate_die_wip_classes_map_.clear();
2067 type_unit_die_wip_classes_map_.clear();
2068 die_wip_function_types_map_.clear();
2069 alternate_die_wip_function_types_map_.clear();
2070 type_unit_die_wip_function_types_map_.clear();
2071 die_function_with_no_symbol_map_.clear();
2072 types_to_canonicalize_.clear();
2073 decl_only_classes_map_.clear();
2074 die_tu_map_.clear();
2075 corpus().reset();
2076 corpus_group().reset();
2077 cur_tu_.reset();
2078 primary_die_parent_map_.clear();
2079 tu_die_imported_unit_points_map_.clear();
2080 alt_tu_die_imported_unit_points_map_.clear();
2081 type_units_tu_die_imported_unit_points_map_.clear();
2082 alternate_die_parent_map_.clear();
2083 type_section_die_parent_map_.clear();
2084 var_decls_to_add_.clear();
2085 clear_per_translation_unit_data();
2086 options().load_in_linux_kernel_mode = linux_kernel_mode;
2087 options().load_all_types = load_all_types;
2088#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2089 debug_die_canonicalization_is_on_ =
2090 env().debug_die_canonicalization_is_on();
2091 use_canonical_die_comparison_ = true;
2092#endif
2093 compare_count_ = 0;
2094 canonical_propagated_count_ = 0;
2095 cancelled_propagation_count_ = 0;
2096 load_in_linux_kernel_mode(linux_kernel_mode);
2097 }
2098
2099 /// Initializer of reader.
2100 ///
2101 /// Resets the reader so that it can be re-used to read another binary.
2102 ///
2103 /// @param elf_path the path to the new ELF file.
2104 ///
2105 /// @param debug_info_root_paths the vector of debug-info path to
2106 /// look for split debug info.
2107 ///
2108 /// @param load_all_types if set to false only the types that are
2109 /// reachable from publicly exported declarations (of functions and
2110 /// variables) are read. If set to true then all types found in the
2111 /// debug information are loaded.
2112 ///
2113 /// @param linux_kernel_mode if set to true, then consider the
2114 /// special linux kernel symbol tables when determining if a symbol
2115 /// is exported or not.
2116 void
2117 initialize(const string& elf_path,
2118 const vector<char**>& debug_info_root_paths,
2119 bool load_all_types,
2120 bool linux_kernel_mode)
2121 {
2122 elf_based_reader::initialize(elf_path, debug_info_root_paths);
2123 initialize(load_all_types, linux_kernel_mode);
2124 }
2125
2126 /// Create an instance of DWARF Reader.
2127 ///
2128 /// @param elf_path the path to the ELF file to read from.
2129 ///
2130 /// @param debug_info_root_paths a vector of paths where to look up
2131 /// split debug info files.
2132 ///
2133 /// @param environment the environment to be used by the reader.
2134 ///
2135 /// @param load_all_types if set to false only the types that are
2136 /// reachable from publicly exported declarations (of functions and
2137 /// variables) are read. If set to true then all types found in the
2138 /// debug information are loaded.
2139 ///
2140 /// @param linux_kernel_mode if set to true, then consider the
2141 /// special linux kernel symbol tables when determining if a symbol
2142 /// is exported or not.
2143 static dwarf::reader_sptr
2144 create(const std::string& elf_path,
2145 const vector<char**>& debug_info_root_paths,
2146 environment& environment,
2147 bool load_all_types,
2148 bool linux_kernel_mode)
2149 {
2150 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2151 environment, load_all_types,
2152 linux_kernel_mode));
2153 return result;
2154 }
2155
2156 /// Destructor of the @ref reader type.
2157 ~reader()
2158 {
2159 }
2160
2161 /// Read and analyze the ELF and DWARF information associated with
2162 /// the underlying ELF file and build an ABI corpus out of it.
2163 ///
2164 /// @param status output parameter. This is set to the status of
2165 /// the analysis of the debug info.
2166 ///
2167 /// @return the resulting ABI corpus.
2168 corpus_sptr
2169 read_corpus(status& status)
2170 {
2171 status = STATUS_UNKNOWN;
2172
2173 // Load the generic ELF parts of the corpus.
2175
2176 if (!(status & STATUS_OK))
2177 {
2178 // Something went badly wrong. There is nothing we can do
2179 // with this ELF file. Bail out.
2180 return corpus_sptr();
2181 }
2182
2183 // If we couldn't find debug info from the elf path, then say it.
2184 if (dwarf_debug_info() == nullptr)
2186
2187 {
2188 string alt_di_path;
2189 if (refers_to_alt_debug_info(alt_di_path)
2192 }
2193
2194 if (// If debug info was found but not the required alternate debug
2195 // info ...
2196 ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2197 && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2198 // ... then we cannot handle the binary.
2199 return corpus_sptr();
2200
2201 // Read the variable and function descriptions from the debug info
2202 // we have, through the dwfl handle.
2203 corpus_sptr corp = read_debug_info_into_corpus();
2204
2205 status |= STATUS_OK;
2206
2207 return corp;
2208 }
2209
2210 /// Read an analyze the DWARF information.
2211 ///
2212 /// Construct an ABI corpus from it.
2213 ///
2214 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2215 ///
2216 /// @return the resulting ABI corpus.
2217 corpus_sptr
2218 read_debug_info_into_corpus()
2219 {
2220 clear_per_corpus_data();
2221
2222 // First set some mundane properties of the corpus gathered from
2223 // ELF.
2224 corpus::origin origin = corpus()->get_origin();
2225 origin |= corpus::DWARF_ORIGIN;
2226 corpus()->set_origin(origin);
2227
2228 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2229 && !env().user_set_analyze_exported_interfaces_only())
2230 // So we are looking at the Linux Kernel and the user has not set
2231 // any particular option regarding the amount of types to analyse.
2232 // In that case, we need to only analyze types that are reachable
2233 // from exported interfaces otherwise we get such a massive amount
2234 // of type DIEs to look at that things are just too slow down the
2235 // road.
2236 env().analyze_exported_interfaces_only(true);
2237
2238 corpus()->set_soname(dt_soname());
2239 corpus()->set_needed(dt_needed());
2240 corpus()->set_architecture_name(elf_architecture());
2241 // Set symbols information to the corpus.
2242 corpus()->set_symtab(symtab());
2243
2244 // Get out now if no debug info is found or if the symbol table is
2245 // empty.
2246 if (!dwarf_debug_info()
2247 || !corpus()->get_symtab()
2248 || !corpus()->get_symtab()->has_symbols())
2249 return corpus();
2250
2251 uint8_t address_size = 0;
2252 size_t header_size = 0;
2253
2254#ifdef WITH_DEBUG_SELF_COMPARISON
2255 if (env().self_comparison_debug_is_on())
2256 env().set_self_comparison_debug_input(corpus());
2257#endif
2258
2259 env().priv_->do_log(do_log());
2260
2261 // Walk all the DIEs of the debug info to build a DIE -> parent map
2262 // useful for get_die_parent() to work.
2263 {
2264 tools_utils::timer t;
2265 if (do_log())
2266 {
2267 cerr << "building die -> parent maps ...";
2268 t.start();
2269 }
2270
2271 build_die_parent_maps();
2272
2273 if (do_log())
2274 {
2275 t.stop();
2276 cerr << " DONE@" << corpus()->get_path()
2277 << ":"
2278 << t
2279 << "\n";
2280 }
2281 }
2282
2283 env().canonicalization_is_done(false);
2284
2285 {
2286 tools_utils::timer t;
2287 if (do_log())
2288 {
2289 cerr << "building the libabigail internal representation ...\n";
2290 t.start();
2291 }
2292 // And now walk all the DIEs again to build the libabigail IR.
2293 Dwarf_Half dwarf_vers = 0;
2294 for (Dwarf_Off offset = 0, next_offset = 0;
2295 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2296 offset, &next_offset, &header_size,
2297 &dwarf_vers, NULL, &address_size, NULL,
2298 NULL, NULL) == 0);
2299 offset = next_offset)
2300 {
2301 Dwarf_Off die_offset = offset + header_size;
2302 Dwarf_Die unit;
2303 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2304 die_offset, &unit)
2305 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2306 continue;
2307
2308 dwarf_version(dwarf_vers);
2309
2310 address_size *= 8;
2311
2312 // Build a translation_unit IR node from cu; note that cu must
2313 // be a DW_TAG_compile_unit die.
2314 translation_unit_sptr ir_node =
2315 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2316 ABG_ASSERT(ir_node);
2317 }
2318 if (do_log())
2319 {
2320 t.stop();
2321 cerr << "building the libabigail internal representation "
2322 << "DONE for corpus << corpus()->get_path()"
2323 << " in :"
2324 << t
2325 << "\n";
2326
2327 cerr << "Number of aggregate types compared: "
2328 << compare_count_ << "\n"
2329 << "Number of canonical types propagated: "
2330 << canonical_propagated_count_ << "\n"
2331 << "Number of cancelled propagated canonical types:"
2332 << cancelled_propagation_count_ << "\n";
2333 }
2334 }
2335
2336 {
2337 tools_utils::timer t;
2338 if (do_log())
2339 {
2340 cerr << "resolving declaration only classes ...";
2341 t.start();
2342 }
2343 resolve_declaration_only_classes();
2344 if (do_log())
2345 {
2346 t.stop();
2347 cerr << " DONE@" << corpus()->get_path()
2348 << ":"
2349 << t
2350 <<"\n";
2351 }
2352 }
2353
2354 {
2355 tools_utils::timer t;
2356 if (do_log())
2357 {
2358 cerr << "resolving declaration only enums ...";
2359 t.start();
2360 }
2361 resolve_declaration_only_enums();
2362 if (do_log())
2363 {
2364 t.stop();
2365 cerr << " DONE@" << corpus()->get_path()
2366 << ":"
2367 << t
2368 <<"\n";
2369 }
2370 }
2371
2372 {
2373 tools_utils::timer t;
2374 if (do_log())
2375 {
2376 cerr << "fixing up functions with linkage name but "
2377 << "no advertised underlying symbols ....";
2378 t.start();
2379 }
2380 fixup_functions_with_no_symbols();
2381 if (do_log())
2382 {
2383 t.stop();
2384 cerr << " DONE@" << corpus()->get_path()
2385 <<":"
2386 << t
2387 <<"\n";
2388 }
2389 }
2390
2391 merge_member_functions_in_classes_of_same_names();
2392
2393 /// Now, look at the types that needs to be canonicalized after the
2394 /// translation has been constructed (which is just now) and
2395 /// canonicalize them.
2396 ///
2397 /// These types need to be constructed at the end of the translation
2398 /// unit reading phase because some types are modified by some DIEs
2399 /// even after the principal DIE describing the type has been read;
2400 /// this happens for clones of virtual destructors (for instance) or
2401 /// even for some static data members. We need to do that for types
2402 /// are in the alternate debug info section and for types that in
2403 /// the main debug info section.
2404 {
2405 tools_utils::timer t;
2406 if (do_log())
2407 {
2408 cerr << "perform late type canonicalizing ...\n";
2409 t.start();
2410 }
2411
2412 perform_late_type_canonicalizing();
2413 if (do_log())
2414 {
2415 t.stop();
2416 cerr << "late type canonicalizing DONE for "
2417 << corpus()->get_path()
2418 << " in :"
2419 << t
2420 << "\n";
2421 }
2422 }
2423
2424 env().canonicalization_is_done(true);
2425
2426 {
2427 tools_utils::timer t;
2428 if (do_log())
2429 {
2430 cerr << "sort functions and variables ...";
2431 t.start();
2432 }
2433 corpus()->sort_functions();
2434 corpus()->sort_variables();
2435 if (do_log())
2436 {
2437 t.stop();
2438 cerr << " DONE@" << corpus()->get_path()
2439 << ":"
2440 << t
2441 <<" \n";
2442 }
2443 }
2444
2445 return corpus();
2446 }
2447
2448 /// Clear the data that is relevant only for the current translation
2449 /// unit being read. The rest of the data is relevant for the
2450 /// entire ABI corpus.
2451 void
2452 clear_per_translation_unit_data()
2453 {
2454 while (!scope_stack().empty())
2455 scope_stack().pop();
2456 var_decls_to_re_add_to_tree().clear();
2457 per_tu_repr_to_fn_type_maps().clear();
2458 }
2459
2460 /// Clear the data that is relevant for the current corpus being
2461 /// read.
2462 void
2463 clear_per_corpus_data()
2464 {
2465 die_qualified_name_maps_.clear();
2466 die_pretty_repr_maps_.clear();
2467 die_pretty_type_repr_maps_.clear();
2468 clear_types_to_canonicalize();
2469 }
2470
2471 /// Getter for the current environment.
2472 ///
2473 /// @return the current environment.
2474 environment&
2475 env()
2476 {return options().env;}
2477
2478 /// Getter for the current environment.
2479 ///
2480 /// @return the current environment.
2481 const environment&
2482 env() const
2483 {return const_cast<reader*>(this)->env();}
2484
2485 /// Getter for the flag that tells us if we are dropping functions
2486 /// and variables that have undefined symbols.
2487 ///
2488 /// @return true iff we are dropping functions and variables that have
2489 /// undefined symbols.
2490 bool
2491 drop_undefined_syms() const
2492 {return options().drop_undefined_syms;}
2493
2494 /// Setter for the flag that tells us if we are dropping functions
2495 /// and variables that have undefined symbols.
2496 ///
2497 /// @param f the new value of the flag.
2498 void
2499 drop_undefined_syms(bool f)
2500 {options().drop_undefined_syms = f;}
2501
2502 /// Getter of the DWARF version.
2503 unsigned short
2504 dwarf_version() const
2505 {return dwarf_version_;}
2506
2507 void
2508 dwarf_version(unsigned short v)
2509 {dwarf_version_ = v;}
2510
2511 /// Return the ELF descriptor used for DWARF access.
2512 ///
2513 /// This can be the same as reader::elf_handle() above, if the
2514 /// DWARF info is in the same ELF file as the one of the binary we
2515 /// are analizing. It is different if e.g, the debug info is split
2516 /// from the ELF file we are analizing.
2517 ///
2518 /// @return a pointer to the ELF descriptor used to access debug
2519 /// info.
2520 Elf*
2521 dwarf_elf_handle() const
2522 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2523
2524 /// Test if the debug information is in a separate ELF file wrt the
2525 /// main ELF file of the program (application or shared library) we
2526 /// are analizing.
2527 ///
2528 /// @return true if the debug information is in a separate ELF file
2529 /// compared to the main ELF file of the program (application or
2530 /// shared library) that we are looking at.
2531 bool
2532 dwarf_is_splitted() const
2533 {return dwarf_elf_handle() != elf_handle();}
2534
2535 /// Return the correct debug info, depending on the DIE source we
2536 /// are looking at.
2537 ///
2538 /// @param source the DIE source to consider.
2539 ///
2540 /// @return the right debug info, depending on @p source.
2541 const Dwarf*
2542 dwarf_per_die_source(die_source source) const
2543 {
2544 const Dwarf *result = 0;
2545 switch(source)
2546 {
2547 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2548 case TYPE_UNIT_DIE_SOURCE:
2549 result = dwarf_debug_info();
2550 break;
2551 case ALT_DEBUG_INFO_DIE_SOURCE:
2552 result = alternate_dwarf_debug_info();
2553 break;
2554 case NO_DEBUG_INFO_DIE_SOURCE:
2555 case NUMBER_OF_DIE_SOURCES:
2557 }
2558 return result;
2559 }
2560
2561 /// Return the path to the ELF path we are reading.
2562 ///
2563 /// @return the elf path.
2564 const string&
2565 elf_path() const
2566 {return corpus_path();}
2567
2568 const Dwarf_Die*
2569 cur_tu_die() const
2570 {return cur_tu_die_;}
2571
2572 void
2573 cur_tu_die(Dwarf_Die* cur_tu_die)
2574 {cur_tu_die_ = cur_tu_die;}
2575
2576 dwarf_expr_eval_context&
2577 dwarf_expr_eval_ctxt() const
2578 {return dwarf_expr_eval_context_;}
2579
2580 /// Getter of the maps set that associates a representation of a
2581 /// decl DIE to a vector of offsets of DIEs having that representation.
2582 ///
2583 /// @return the maps set that associates a representation of a decl
2584 /// DIE to a vector of offsets of DIEs having that representation.
2585 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2586 decl_die_repr_die_offsets_maps() const
2587 {return decl_die_repr_die_offsets_maps_;}
2588
2589 /// Getter of the maps set that associates a representation of a
2590 /// decl DIE to a vector of offsets of DIEs having that representation.
2591 ///
2592 /// @return the maps set that associates a representation of a decl
2593 /// DIE to a vector of offsets of DIEs having that representation.
2594 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2595 decl_die_repr_die_offsets_maps()
2596 {return decl_die_repr_die_offsets_maps_;}
2597
2598 /// Getter of the maps set that associate a representation of a type
2599 /// DIE to a vector of offsets of DIEs having that representation.
2600 ///
2601 /// @return the maps set that associate a representation of a type
2602 /// DIE to a vector of offsets of DIEs having that representation.
2603 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2604 type_die_repr_die_offsets_maps() const
2605 {return type_die_repr_die_offsets_maps_;}
2606
2607 /// Getter of the maps set that associate a representation of a type
2608 /// DIE to a vector of offsets of DIEs having that representation.
2609 ///
2610 /// @return the maps set that associate a representation of a type
2611 /// DIE to a vector of offsets of DIEs having that representation.
2612 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2613 type_die_repr_die_offsets_maps()
2614 {return type_die_repr_die_offsets_maps_;}
2615
2616
2617 /// Compute the offset of the canonical DIE of a given DIE.
2618 ///
2619 /// @param die the DIE to consider.
2620 ///
2621 /// @param canonical_die_offset out parameter. This is set to the
2622 /// resulting canonical DIE that was computed.
2623 ///
2624 /// @param die_as_type if yes, it means @p die has to be considered
2625 /// as a type.
2626 void
2627 compute_canonical_die_offset(const Dwarf_Die *die,
2628 Dwarf_Off &canonical_die_offset,
2629 bool die_as_type) const
2630 {
2631 offset_offset_map_type &canonical_dies =
2632 die_as_type
2633 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2634 get_container(*this, die)
2635 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2636 get_container(*this, die);
2637
2638 Dwarf_Die canonical_die;
2639 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2640
2641 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2642 }
2643
2644 /// Compute (find) the canonical DIE of a given DIE.
2645 ///
2646 /// @param die the DIE to consider.
2647 ///
2648 /// @param canonical_dies the vector in which the canonical dies ar
2649 /// stored. The index of each element is the offset of the DIE we
2650 /// want the canonical DIE for. And the value of the element at
2651 /// that index is the canonical DIE offset we are looking for.
2652 ///
2653 /// @param canonical_die_offset out parameter. This is set to the
2654 /// resulting canonical DIE that was computed.
2655 ///
2656 /// @param die_as_type if yes, it means @p die has to be considered
2657 /// as a type.
2658 void
2659 compute_canonical_die(const Dwarf_Die *die,
2660 offset_offset_map_type& canonical_dies,
2661 Dwarf_Die &canonical_die,
2662 bool die_as_type) const
2663 {
2664 const die_source source = get_die_source(die);
2665
2666 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2667
2668 compute_canonical_die(die_offset, source,
2669 canonical_dies,
2670 canonical_die, die_as_type);
2671 }
2672
2673 /// Compute (find) the canonical DIE of a given DIE.
2674 ///
2675 /// @param die_offset the offset of the DIE to consider.
2676 ///
2677 /// @param source the source of the DIE to consider.
2678 ///
2679 /// @param canonical_dies the vector in which the canonical dies ar
2680 /// stored. The index of each element is the offset of the DIE we
2681 /// want the canonical DIE for. And the value of the element at
2682 /// that index is the canonical DIE offset we are looking for.
2683 ///
2684 /// @param canonical_die_offset out parameter. This is set to the
2685 /// resulting canonical DIE that was computed.
2686 ///
2687 /// @param die_as_type if yes, it means @p die has to be considered
2688 /// as a type.
2689 void
2690 compute_canonical_die(Dwarf_Off die_offset,
2691 die_source source,
2692 offset_offset_map_type& canonical_dies,
2693 Dwarf_Die &canonical_die,
2694 bool die_as_type) const
2695 {
2696 // The map that associates the string representation of 'die'
2697 // with a vector of offsets of potentially equivalent DIEs.
2699 die_as_type
2700 ? (const_cast<reader*>(this)->
2701 type_die_repr_die_offsets_maps().get_container(source))
2702 : (const_cast<reader*>(this)->
2703 decl_die_repr_die_offsets_maps().get_container(source));
2704
2705 Dwarf_Die die;
2706 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2707 die_offset, &die));
2708
2709 // The variable repr is the the string representation of 'die'.
2710 //
2711 // Even if die_as_type is true -- which means that 'die' is said
2712 // to be considered as a type -- we always consider a
2713 // DW_TAG_subprogram DIE as a decl here, as far as its string
2714 // representation is concerned.
2715 interned_string name =
2716 (die_as_type)
2717 ? get_die_pretty_type_representation(&die, /*where=*/0)
2718 : get_die_pretty_representation(&die, /*where=*/0);
2719
2720 Dwarf_Off canonical_die_offset = 0;
2721 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2722 if (i == map.end())
2723 {
2724 dwarf_offsets_type offsets;
2725 offsets.push_back(die_offset);
2726 map[name] = offsets;
2727 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2728 get_die_from_offset(source, die_offset, &canonical_die);
2729 return;
2730 }
2731
2732 Dwarf_Off cur_die_offset;
2733 Dwarf_Die potential_canonical_die;
2734 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2735 o != i->second.end();
2736 ++o)
2737 {
2738 cur_die_offset = *o;
2739 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2740 if (compare_dies(*this, &die, &potential_canonical_die,
2741 /*update_canonical_dies_on_the_fly=*/false))
2742 {
2743 canonical_die_offset = cur_die_offset;
2744 set_canonical_die_offset(canonical_dies, die_offset,
2745 canonical_die_offset);
2746 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2747 return;
2748 }
2749 }
2750
2751 canonical_die_offset = die_offset;
2752 i->second.push_back(die_offset);
2753 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2754 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2755 }
2756
2757 /// Getter of the canonical DIE of a given DIE.
2758 ///
2759 /// @param die the DIE to consider.
2760 ///
2761 /// @param canonical_die output parameter. Is set to the resulting
2762 /// canonical die, if this function returns true.
2763 ///
2764 /// @param where the offset of the logical DIE we are supposed to be
2765 /// calling this function from. If set to zero this means this is
2766 /// to be ignored.
2767 ///
2768 /// @param die_as_type if set to yes, it means @p die is to be
2769 /// considered as a type DIE.
2770 ///
2771 /// @return true iff a canonical DIE was found for @p die.
2772 bool
2773 get_canonical_die(const Dwarf_Die *die,
2774 Dwarf_Die &canonical_die,
2775 size_t where,
2776 bool die_as_type)
2777 {
2778 const die_source source = get_die_source(die);
2779
2780 offset_offset_map_type &canonical_dies =
2781 die_as_type
2782 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2783 get_container(source)
2784 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2785 get_container(source);
2786
2787 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2788 if (Dwarf_Off canonical_die_offset =
2789 get_canonical_die_offset(canonical_dies, die_offset))
2790 {
2791 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2792 return true;
2793 }
2794
2795 // The map that associates the string representation of 'die'
2796 // with a vector of offsets of potentially equivalent DIEs.
2798 die_as_type
2799 ? (const_cast<reader*>(this)->
2800 type_die_repr_die_offsets_maps().get_container(*this, die))
2801 : (const_cast<reader*>(this)->
2802 decl_die_repr_die_offsets_maps().get_container(*this, die));
2803
2804 // The variable repr is the the string representation of 'die'.
2805 //
2806 // Even if die_as_type is true -- which means that 'die' is said
2807 // to be considered as a type -- we always consider a
2808 // DW_TAG_subprogram DIE as a decl here, as far as its string
2809 // representation is concerned.
2810 interned_string name =
2811 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2812 ? get_die_pretty_type_representation(die, where)
2813 : get_die_pretty_representation(die, where);
2814
2815 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2816 if (i == map.end())
2817 return false;
2818
2819 Dwarf_Off cur_die_offset;
2820 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2821 o != i->second.end();
2822 ++o)
2823 {
2824 cur_die_offset = *o;
2825 get_die_from_offset(source, cur_die_offset, &canonical_die);
2826 // compare die and canonical_die.
2827 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2828 die, &canonical_die,
2829 /*update_canonical_dies_on_the_fly=*/true))
2830 {
2831 set_canonical_die_offset(canonical_dies,
2832 die_offset,
2833 cur_die_offset);
2834 return true;
2835 }
2836 }
2837
2838 return false;
2839 }
2840
2841 /// Retrieve the canonical DIE of a given DIE.
2842 ///
2843 /// The canonical DIE is a DIE that is structurally equivalent to
2844 /// this one.
2845 ///
2846 /// Note that this function caches the canonical DIE that was
2847 /// computed. Subsequent invocations of this function on the same
2848 /// DIE return the same cached DIE.
2849 ///
2850 /// @param die the DIE to get a canonical type for.
2851 ///
2852 /// @param canonical_die the resulting canonical DIE.
2853 ///
2854 /// @param where the offset of the logical DIE we are supposed to be
2855 /// calling this function from. If set to zero this means this is
2856 /// to be ignored.
2857 ///
2858 /// @param die_as_type if true, consider DIE is a type.
2859 ///
2860 /// @return true if an *existing* canonical DIE was found.
2861 /// Otherwise, @p die is considered as being a canonical DIE for
2862 /// itself. @p canonical_die is thus set to the canonical die in
2863 /// either cases.
2864 bool
2865 get_or_compute_canonical_die(const Dwarf_Die* die,
2866 Dwarf_Die& canonical_die,
2867 size_t where,
2868 bool die_as_type) const
2869 {
2870 const die_source source = get_die_source(die);
2871
2872 offset_offset_map_type &canonical_dies =
2873 die_as_type
2874 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2875 get_container(source)
2876 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2877 get_container(source);
2878
2879 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2880
2881 if (Dwarf_Off canonical_die_offset =
2882 get_canonical_die_offset(canonical_dies,
2883 initial_die_offset))
2884 {
2885 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2886 return true;
2887 }
2888
2889 if (!is_type_die_to_be_canonicalized(die))
2890 return false;
2891
2892 // The map that associates the string representation of 'die'
2893 // with a vector of offsets of potentially equivalent DIEs.
2895 die_as_type
2896 ? (const_cast<reader*>(this)->
2897 type_die_repr_die_offsets_maps().get_container(*this, die))
2898 : (const_cast<reader*>(this)->
2899 decl_die_repr_die_offsets_maps().get_container(*this, die));
2900
2901 // The variable repr is the the string representation of 'die'.
2902 //
2903 // Even if die_as_type is true -- which means that 'die' is said
2904 // to be considered as a type -- we always consider a
2905 // DW_TAG_subprogram DIE as a decl here, as far as its string
2906 // representation is concerned.
2907 interned_string name =
2908 (die_as_type)
2909 ? get_die_pretty_type_representation(die, where)
2910 : get_die_pretty_representation(die, where);
2911
2912 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2913 if (i == map.end())
2914 {
2915 dwarf_offsets_type offsets;
2916 offsets.push_back(initial_die_offset);
2917 map[name] = offsets;
2918 get_die_from_offset(source, initial_die_offset, &canonical_die);
2919 set_canonical_die_offset(canonical_dies,
2920 initial_die_offset,
2921 initial_die_offset);
2922 return false;
2923 }
2924
2925 // walk i->second without any iterator (using a while loop rather
2926 // than a for loop) because compare_dies might add new content to
2927 // the end of the i->second vector during the walking.
2928 dwarf_offsets_type::size_type n = 0, s = i->second.size();
2929 while (n < s)
2930 {
2931 Dwarf_Off die_offset = i->second[n];
2932 get_die_from_offset(source, die_offset, &canonical_die);
2933 // compare die and canonical_die.
2934 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2935 die, &canonical_die,
2936 /*update_canonical_dies_on_the_fly=*/true))
2937 {
2938 set_canonical_die_offset(canonical_dies,
2939 initial_die_offset,
2940 die_offset);
2941 return true;
2942 }
2943 ++n;
2944 }
2945
2946 // We didn't find a canonical DIE for 'die'. So let's consider
2947 // that it is its own canonical DIE.
2948 get_die_from_offset(source, initial_die_offset, &canonical_die);
2949 i->second.push_back(initial_die_offset);
2950 set_canonical_die_offset(canonical_dies,
2951 initial_die_offset,
2952 initial_die_offset);
2953
2954 return false;
2955 }
2956
2957 /// Get the source of the DIE.
2958 ///
2959 /// The function returns an enumerator value saying if the DIE comes
2960 /// from the .debug_info section of the primary debug info file, the
2961 /// .debug_info section of the alternate debug info file, or the
2962 /// .debug_types section.
2963 ///
2964 /// @param die the DIE to get the source of.
2965 ///
2966 /// @return the source of the DIE if it could be determined,
2967 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2969 get_die_source(const Dwarf_Die *die) const
2970 {
2971 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2972 ABG_ASSERT(die);
2973 ABG_ASSERT(get_die_source(*die, source));
2974 return source;
2975 }
2976
2977 /// Get the source of the DIE.
2978 ///
2979 /// The function returns an enumerator value saying if the DIE comes
2980 /// from the .debug_info section of the primary debug info file, the
2981 /// .debug_info section of the alternate debug info file, or the
2982 /// .debug_types section.
2983 ///
2984 /// @param die the DIE to get the source of.
2985 ///
2986 /// @param source out parameter. The function sets this parameter
2987 /// to the source of the DIE @p iff it returns true.
2988 ///
2989 /// @return true iff the source of the DIE could be determined and
2990 /// returned.
2991 bool
2992 get_die_source(const Dwarf_Die &die, die_source &source) const
2993 {
2994 Dwarf_Die cu_die;
2995 Dwarf_Die cu_kind;
2996 uint8_t address_size = 0, offset_size = 0;
2997 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2998 &cu_die, &address_size,
2999 &offset_size))
3000 return false;
3001
3002 Dwarf_Half version = 0;
3003 Dwarf_Off abbrev_offset = 0;
3004 uint64_t type_signature = 0;
3005 Dwarf_Off type_offset = 0;
3006 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
3007 &version, &abbrev_offset,
3008 &address_size, &offset_size,
3009 &type_signature, &type_offset))
3010 return false;
3011
3012 int tag = dwarf_tag(&cu_kind);
3013
3014 if (tag == DW_TAG_compile_unit
3015 || tag == DW_TAG_partial_unit)
3016 {
3017 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
3018 if (dwarf_debug_info() == die_dwarf)
3019 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
3020 else if (alternate_dwarf_debug_info() == die_dwarf)
3021 source = ALT_DEBUG_INFO_DIE_SOURCE;
3022 else
3024 }
3025 else if (tag == DW_TAG_type_unit)
3026 source = TYPE_UNIT_DIE_SOURCE;
3027 else
3028 return false;
3029
3030 return true;
3031 }
3032
3033 /// Getter for the DIE designated by an offset.
3034 ///
3035 /// @param source the source of the DIE to get.
3036 ///
3037 /// @param offset the offset of the DIE to get.
3038 ///
3039 /// @param die the resulting DIE. The pointer has to point to an
3040 /// allocated memory region.
3041 void
3042 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
3043 {
3044 if (source == TYPE_UNIT_DIE_SOURCE)
3045 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3046 offset, die));
3047 else
3048 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3049 offset, die));
3050 }
3051
3052public:
3053
3054 /// Add an entry to the relevant die->decl map.
3055 ///
3056 /// @param die the DIE to add the the map.
3057 ///
3058 /// @param decl the decl to consider.
3059 ///
3060 /// @param where_offset where in the DIE stream we logically are.
3061 ///
3062 /// @param do_associate_by_repr if true then this function
3063 /// associates the representation string of @p die with the
3064 /// declaration @p decl, in a corpus-wide manner. That is, in the
3065 /// entire current corpus, there is going to be just one declaration
3066 /// associated with a DIE of the string representation of @p die.
3067 ///
3068 /// @param do_associate_by_repr_per_tu if true, then this function
3069 /// associates the representation string of @p die with the
3070 /// declaration @p decl in a translation unit wide manner. That is,
3071 /// in the entire current translation unit, there is going to be
3072 /// just one declaration associated with a DIE of the string
3073 /// representation of @p die.
3074 void
3075 associate_die_to_decl(Dwarf_Die* die,
3076 decl_base_sptr decl,
3077 size_t where_offset,
3078 bool do_associate_by_repr = false)
3079 {
3080 const die_source source = get_die_source(die);
3081
3083 decl_die_artefact_maps().get_container(source);
3084
3085 size_t die_offset;
3086 if (do_associate_by_repr)
3087 {
3088 Dwarf_Die equiv_die;
3089 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
3090 /*die_as_type=*/false))
3091 return;
3092 die_offset = dwarf_dieoffset(&equiv_die);
3093 }
3094 else
3095 die_offset = dwarf_dieoffset(die);
3096
3097 m[die_offset] = decl;
3098 }
3099
3100 /// Lookup the decl for a given DIE.
3101 ///
3102 /// The returned decl is either the decl of the DIE that as the
3103 /// exact offset @p die_offset
3104 /// die_offset, or
3105 /// give
3106 ///
3107 /// @param die_offset the offset of the DIE to consider.
3108 ///
3109 /// @param source where the DIE represented by @p die_offset comes
3110 /// from.
3111 ///
3112 /// Note that "alternate debug info sections" is a GNU extension as
3113 /// of DWARF4 and is described at
3114 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3115 ///
3116 /// @return the resulting decl, or null if no decl is associated to
3117 /// the DIE represented by @p die_offset.
3118 decl_base_sptr
3119 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3120 {
3121 decl_base_sptr result =
3122 is_decl(lookup_artifact_from_die_offset(die_offset, source,
3123 /*die_as_type=*/false));
3124
3125 return result;
3126 }
3127
3128 /// Get the qualified name of a given DIE.
3129 ///
3130 /// If the name of the DIE was already computed before just return
3131 /// that name from a cache. Otherwise, build the name, cache it and
3132 /// return it.
3133 ///
3134 /// @param die the DIE to consider.
3135 ///
3136 /// @param where_offset where in the DIE stream we logically are.
3137 ///
3138 /// @return the interned string representing the qualified name of
3139 /// @p die.
3140 interned_string
3141 get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3142 {
3143 ABG_ASSERT(die);
3145 die_qualified_name_maps_.get_container(*this, die);
3146
3147 size_t die_offset = dwarf_dieoffset(die);
3148 die_istring_map_type::const_iterator i = map.find(die_offset);
3149
3150 if (i == map.end())
3151 {
3152 reader& rdr = *const_cast<reader*>(this);
3153 string qualified_name = die_qualified_name(rdr, die, where_offset);
3154 interned_string istr = env().intern(qualified_name);
3155 map[die_offset] = istr;
3156 return istr;
3157 }
3158
3159 return i->second;
3160 }
3161
3162 /// Get the qualified name of a given DIE.
3163 ///
3164 /// If the name of the DIE was already computed before just return
3165 /// that name from a cache. Otherwise, build the name, cache it and
3166 /// return it.
3167 ///
3168 /// @param die the DIE to consider.
3169 ///
3170 /// @param where_offset where in the DIE stream we logically are.
3171 ///
3172 /// @return the interned string representing the qualified name of
3173 /// @p die.
3174 interned_string
3175 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3176 {
3177 return const_cast<reader*>(this)->
3178 get_die_qualified_name(die, where_offset);
3179 }
3180
3181 /// Get the qualified name of a given DIE which is considered to be
3182 /// the DIE for a type.
3183 ///
3184 /// For instance, for a DW_TAG_subprogram DIE, this function
3185 /// computes the name of the function *type* that corresponds to the
3186 /// function.
3187 ///
3188 /// If the name of the DIE was already computed before just return
3189 /// that name from a cache. Otherwise, build the name, cache it and
3190 /// return it.
3191 ///
3192 /// @param die the DIE to consider.
3193 ///
3194 /// @param where_offset where in the DIE stream we logically are.
3195 ///
3196 /// @return the interned string representing the qualified name of
3197 /// @p die.
3198 interned_string
3199 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3200 {
3201 ABG_ASSERT(die);
3202
3203 // The name of the translation unit die is "".
3204 if (die == cur_tu_die())
3205 return env().intern("");
3206
3208 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3209 die);
3210
3211 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3212 die_istring_map_type::const_iterator i =
3213 map.find(die_offset);
3214
3215 if (i == map.end())
3216 {
3217 reader& rdr = *const_cast<reader*>(this);
3218 string qualified_name;
3219 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3220 if ((tag == DW_TAG_structure_type
3221 || tag == DW_TAG_class_type
3222 || tag == DW_TAG_union_type)
3223 && die_is_anonymous(die))
3224 {
3225 location l = die_location(*this, die);
3226 qualified_name = l ? l.expand() : "noloc";
3227 qualified_name = "unnamed-at-" + qualified_name;
3228 }
3229 else
3230 qualified_name =
3231 die_qualified_type_name(rdr, die, where_offset);
3232
3233 interned_string istr = env().intern(qualified_name);
3234 map[die_offset] = istr;
3235 return istr;
3236 }
3237
3238 return i->second;
3239 }
3240
3241 /// Get the pretty representation of a DIE that represents a type.
3242 ///
3243 /// For instance, for the DW_TAG_subprogram, this function computes
3244 /// the pretty representation of the type of the function, not the
3245 /// pretty representation of the function declaration.
3246 ///
3247 /// Once the pretty representation is computed, it's stored in a
3248 /// cache. Subsequent invocations of this function on the same DIE
3249 /// will yield the cached name.
3250 ///
3251 /// @param die the DIE to consider.
3252 ///
3253 /// @param where_offset where in the DIE stream we logically are.
3254 ///
3255 /// @return the interned_string that represents the pretty
3256 /// representation.
3257 interned_string
3258 get_die_pretty_type_representation(const Dwarf_Die *die,
3259 size_t where_offset) const
3260 {
3261 ABG_ASSERT(die);
3263 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3264 die);
3265
3266 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3267 die_istring_map_type::const_iterator i = map.find(die_offset);
3268
3269 if (i == map.end())
3270 {
3271 reader& rdr = *const_cast<reader*>(this);
3272 string pretty_representation =
3273 die_pretty_print_type(rdr, die, where_offset);
3274 interned_string istr = env().intern(pretty_representation);
3275 map[die_offset] = istr;
3276 return istr;
3277 }
3278
3279 return i->second;
3280 }
3281
3282 /// Get the pretty representation of a DIE.
3283 ///
3284 /// Once the pretty representation is computed, it's stored in a
3285 /// cache. Subsequent invocations of this function on the same DIE
3286 /// will yield the cached name.
3287 ///
3288 /// @param die the DIE to consider.
3289 ///
3290 /// @param where_offset where in the DIE stream we logically are.
3291 ///
3292 /// @return the interned_string that represents the pretty
3293 /// representation.
3294 interned_string
3295 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3296 {
3297 ABG_ASSERT(die);
3298
3300 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3301 die);
3302
3303 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3304 die_istring_map_type::const_iterator i = map.find(die_offset);
3305
3306 if (i == map.end())
3307 {
3308 reader& rdr = *const_cast<reader*>(this);
3309 string pretty_representation =
3310 die_pretty_print(rdr, die, where_offset);
3311 interned_string istr = env().intern(pretty_representation);
3312 map[die_offset] = istr;
3313 return istr;
3314 }
3315
3316 return i->second;
3317 }
3318
3319 /// Lookup the artifact that was built to represent a type that has
3320 /// the same pretty representation as the type denoted by a given
3321 /// DIE.
3322 ///
3323 /// Note that the DIE must have previously been associated with the
3324 /// artifact using the functions associate_die_to_decl or
3325 /// associate_die_to_type.
3326 ///
3327 /// Also, note that the scope of the lookup is the current ABI
3328 /// corpus.
3329 ///
3330 /// @param die the DIE to consider.
3331 ///
3332 /// @param where_offset where in the DIE stream we logically are.
3333 ///
3334 /// @return the type artifact found.
3336 lookup_type_artifact_from_die(Dwarf_Die *die) const
3337 {
3338 type_or_decl_base_sptr artifact =
3339 lookup_artifact_from_die(die, /*type_as_die=*/true);
3340 if (function_decl_sptr fn = is_function_decl(artifact))
3341 return fn->get_type();
3342 return artifact;
3343 }
3344
3345 /// Lookup the artifact that was built to represent a type or a
3346 /// declaration that has the same pretty representation as the type
3347 /// denoted by a given DIE.
3348 ///
3349 /// Note that the DIE must have previously been associated with the
3350 /// artifact using the functions associate_die_to_decl or
3351 /// associate_die_to_type.
3352 ///
3353 /// Also, note that the scope of the lookup is the current ABI
3354 /// corpus.
3355 ///
3356 /// @param die the DIE to consider.
3357 ///
3358 /// @param where_offset where in the DIE stream we logically are.
3359 ///
3360 /// @param die_as_type if true, it means the DIE is to be considered
3361 /// as a type.
3362 ///
3363 /// @return the artifact found.
3365 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3366 {
3367 Dwarf_Die equiv_die;
3368 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3369 return type_or_decl_base_sptr();
3370
3371 const die_artefact_map_type& m =
3372 die_as_type
3373 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3374 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3375
3376 size_t die_offset = dwarf_dieoffset(&equiv_die);
3377 die_artefact_map_type::const_iterator i = m.find(die_offset);
3378
3379 if (i == m.end())
3380 return type_or_decl_base_sptr();
3381 return i->second;
3382 }
3383
3384 /// Lookup the artifact that was built to represent a type or a
3385 /// declaration that has the same pretty representation as the type
3386 /// denoted by the offset of a given DIE.
3387 ///
3388 /// Note that the DIE must have previously been associated with the
3389 /// artifact using either associate_die_to_decl or
3390 /// associate_die_to_type.
3391 ///
3392 /// Also, note that the scope of the lookup is the current ABI
3393 /// corpus.
3394 ///
3395 /// @param die the DIE to consider.
3396 ///
3397 /// @param where_offset where in the DIE stream we logically are.
3398 ///
3399 /// @param die_as_type if true, it means the DIE is to be considered
3400 /// as a type.
3401 ///
3402 /// @return the artifact found.
3404 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3405 die_source source,
3406 bool die_as_type = false) const
3407 {
3408 const die_artefact_map_type& m =
3409 die_as_type
3410 ? type_die_artefact_maps().get_container(source)
3411 : decl_die_artefact_maps().get_container(source);
3412
3413 die_artefact_map_type::const_iterator i = m.find(die_offset);
3414 if (i == m.end())
3415 return type_or_decl_base_sptr();
3416 return i->second;
3417 }
3418
3419 /// Check if we can assume the One Definition Rule[1] to be relevant
3420 /// for the current translation unit.
3421 ///
3422 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3423 ///
3424 /// At the moment this returns true if the current translation unit
3425 /// is in C++ language. In that case, it's relevant to assume that
3426 /// we use optimizations based on the ODR.
3427 bool
3428 odr_is_relevant() const
3429 {return odr_is_relevant(cur_transl_unit()->get_language());}
3430
3431 /// Check if we can assume the One Definition Rule[1] to be relevant
3432 /// for a given language.
3433 ///
3434 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3435 ///
3436 /// At the moment this returns true if the language considered
3437 /// is C++, Java or Ada.
3438 bool
3440 {
3441 return (is_cplus_plus_language(l)
3442 || is_java_language(l)
3443 || is_ada_language(l));
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(Dwarf_Off die_offset, die_source source) const
3454 {
3455 Dwarf_Die die;
3456 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3457 die_offset, &die));
3458 return odr_is_relevant(&die);
3459 }
3460
3461 /// Check if we can assume the One Definition Rule to be relevant
3462 /// for a given DIE.
3463 ///
3464 /// @param die the DIE to consider.
3465 ///
3466 /// @return true if the ODR is relevant for @p die.
3467 bool
3468 odr_is_relevant(const Dwarf_Die *die) const
3469 {
3471 if (!get_die_language(die, lang))
3472 return odr_is_relevant();
3473
3474 return odr_is_relevant(lang);
3475 }
3476
3477 /// Getter for the maps set that associates a decl DIE offset to an
3478 /// artifact.
3479 ///
3480 /// @return the maps set that associates a decl DIE offset to an
3481 /// artifact.
3482 die_source_dependant_container_set<die_artefact_map_type>&
3483 decl_die_artefact_maps()
3484 {return decl_die_artefact_maps_;}
3485
3486 /// Getter for the maps set that associates a decl DIE offset to an
3487 /// artifact.
3488 ///
3489 /// @return the maps set that associates a decl DIE offset to an
3490 /// artifact.
3491 const die_source_dependant_container_set<die_artefact_map_type>&
3492 decl_die_artefact_maps() const
3493 {return decl_die_artefact_maps_;}
3494
3495 /// Getter for the maps set that associates a type DIE offset to an
3496 /// artifact.
3497 ///
3498 /// @return the maps set that associates a type DIE offset to an
3499 /// artifact.
3500 die_source_dependant_container_set<die_artefact_map_type>&
3501 type_die_artefact_maps()
3502 {return type_die_artefact_maps_;}
3503
3504 /// Getter for the maps set that associates a type DIE offset to an
3505 /// artifact.
3506 ///
3507 /// @return the maps set that associates a type DIE offset to an
3508 /// artifact.
3509 const die_source_dependant_container_set<die_artefact_map_type>&
3510 type_die_artefact_maps() const
3511 {return type_die_artefact_maps_;}
3512
3513 /// Getter of the maps that associates function type representations
3514 /// to function types, inside a translation unit.
3515 ///
3516 /// @return the maps that associates function type representations
3517 /// to function types, inside a translation unit.
3519 per_tu_repr_to_fn_type_maps()
3520 {return per_tu_repr_to_fn_type_maps_;}
3521
3522 /// Getter of the maps that associates function type representations
3523 /// to function types, inside a translation unit.
3524 ///
3525 /// @return the maps that associates function type representations
3526 /// to function types, inside a translation unit.
3528 per_tu_repr_to_fn_type_maps() const
3529 {return per_tu_repr_to_fn_type_maps_;}
3530
3531 /// Associate the representation of a function type DIE to a given
3532 /// function type, inside the current translation unit.
3533 ///
3534 /// @param die the DIE to associate to the function type, using its
3535 /// representation.
3536 ///
3537 /// @param fn_type the function type to associate to @p die.
3538 void
3539 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3540 const function_type_sptr &fn_type)
3541 {
3542 if (!die_is_function_type(die))
3543 return;
3544
3545 interned_string repr =
3546 get_die_pretty_type_representation(die, /*where=*/0);
3547 ABG_ASSERT(!repr.empty());
3548
3549 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3550 }
3551
3552 /// Lookup the function type associated to a given function type
3553 /// DIE, in the current translation unit.
3554 ///
3555 /// @param die the DIE of function type to consider.
3556 ///
3557 /// @return the @ref function_type_sptr associated to @p die, or nil
3558 /// of no function_type is associated to @p die.
3560 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3561 {
3562 if (!die_is_function_type(die))
3563 return function_type_sptr();
3564
3565 interned_string repr = die_name(die).empty() ?
3566 get_die_pretty_type_representation(die, /*where=*/0)
3567 : get_die_pretty_representation(die, /*where=*/0);
3568 ABG_ASSERT(!repr.empty());
3569
3570 istring_fn_type_map_type::const_iterator i =
3571 per_tu_repr_to_fn_type_maps().find(repr);
3572
3573 if (i == per_tu_repr_to_fn_type_maps().end())
3574 return function_type_sptr();
3575
3576 return i->second;
3577 }
3578
3579 /// Set the canonical DIE offset of a given DIE.
3580 ///
3581 /// @param canonical_dies the vector that holds canonical DIEs.
3582 ///
3583 /// @param die_offset the offset of the DIE to set the canonical DIE
3584 /// for.
3585 ///
3586 /// @param canonical_die_offset the canonical DIE offset to
3587 /// associate to @p die_offset.
3588 void
3589 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3590 Dwarf_Off die_offset,
3591 Dwarf_Off canonical_die_offset) const
3592 {
3593 canonical_dies[die_offset] = canonical_die_offset;}
3594
3595 /// Set the canonical DIE offset of a given DIE.
3596 ///
3597 ///
3598 /// @param die_offset the offset of the DIE to set the canonical DIE
3599 /// for.
3600 ///
3601 /// @param source the source of the DIE denoted by @p die_offset.
3602 ///
3603 /// @param canonical_die_offset the canonical DIE offset to
3604 /// associate to @p die_offset.
3605 ///
3606 /// @param die_as_type if true, it means that @p die_offset has to
3607 /// be considered as a type.
3608 void
3609 set_canonical_die_offset(Dwarf_Off die_offset,
3610 die_source source,
3611 Dwarf_Off canonical_die_offset,
3612 bool die_as_type) const
3613 {
3614 offset_offset_map_type &canonical_dies =
3615 die_as_type
3616 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3617 get_container(source)
3618 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3619 get_container(source);
3620
3621 set_canonical_die_offset(canonical_dies,
3622 die_offset,
3623 canonical_die_offset);
3624 }
3625
3626 /// Set the canonical DIE offset of a given DIE.
3627 ///
3628 ///
3629 /// @param die the DIE to set the canonical DIE for.
3630 ///
3631 /// @param canonical_die_offset the canonical DIE offset to
3632 /// associate to @p die_offset.
3633 ///
3634 /// @param die_as_type if true, it means that @p die has to be
3635 /// considered as a type.
3636 void
3637 set_canonical_die_offset(const Dwarf_Die *die,
3638 Dwarf_Off canonical_die_offset,
3639 bool die_as_type) const
3640 {
3641 const die_source source = get_die_source(die);
3642
3643 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3644
3645 set_canonical_die_offset(die_offset, source,
3646 canonical_die_offset,
3647 die_as_type);
3648 }
3649
3650 /// Get the canonical DIE offset of a given DIE.
3651 ///
3652 /// @param canonical_dies the vector that contains canonical DIES.
3653 ///
3654 /// @param die_offset the offset of the DIE to consider.
3655 ///
3656 /// @return the canonical of the DIE denoted by @p die_offset, or
3657 /// zero if no canonical DIE was found.
3658 Dwarf_Off
3659 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3660 Dwarf_Off die_offset) const
3661 {
3662 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3663 if (it == canonical_dies.end())
3664 return 0;
3665 return it->second;
3666 }
3667
3668 /// Get the canonical DIE offset of a given DIE.
3669 ///
3670 /// @param die_offset the offset of the DIE to consider.
3671 ///
3672 /// @param source the source of the DIE denoted by @p die_offset.
3673 ///
3674 /// @param die_as_type if true, it means that @p is to be considered
3675 /// as a type DIE.
3676 ///
3677 /// @return the canonical of the DIE denoted by @p die_offset, or
3678 /// zero if no canonical DIE was found.
3679 Dwarf_Off
3680 get_canonical_die_offset(Dwarf_Off die_offset,
3681 die_source source,
3682 bool die_as_type) const
3683 {
3684 offset_offset_map_type &canonical_dies =
3685 die_as_type
3686 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3687 get_container(source)
3688 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3689 get_container(source);
3690
3691 return get_canonical_die_offset(canonical_dies, die_offset);
3692 }
3693
3694 /// Erase the canonical type of a given DIE.
3695 ///
3696 /// @param die_offset the offset of the DIE to consider.
3697 ///
3698 /// @param source the source of the canonical type.
3699 ///
3700 /// @param die_as_type if true, it means that @p is to be considered
3701 /// as a type DIE.
3702 ///
3703 /// @return the canonical of the DIE denoted by @p die_offset, or
3704 /// zero if no canonical DIE was found and erased..
3705 bool
3706 erase_canonical_die_offset(Dwarf_Off die_offset,
3707 die_source source,
3708 bool die_as_type) const
3709 {
3710 offset_offset_map_type &canonical_dies =
3711 die_as_type
3712 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3713 get_container(source)
3714 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3715 get_container(source);
3716
3717 return canonical_dies.erase(die_offset);
3718 }
3719
3720
3721 /// Associate a DIE (representing a type) to the type that it
3722 /// represents.
3723 ///
3724 /// @param die the DIE to consider.
3725 ///
3726 /// @param type the type to associate the DIE to.
3727 ///
3728 /// @param where_offset where in the DIE stream we logically are.
3729 void
3730 associate_die_to_type(const Dwarf_Die *die,
3731 type_base_sptr type,
3732 size_t where)
3733 {
3734 if (!type)
3735 return;
3736
3737 Dwarf_Die equiv_die;
3738 if (!get_or_compute_canonical_die(die, equiv_die, where,
3739 /*die_as_type=*/true))
3740 return;
3741
3743 type_die_artefact_maps().get_container(*this, &equiv_die);
3744
3745 size_t die_offset = dwarf_dieoffset(&equiv_die);
3746 m[die_offset] = type;
3747 }
3748
3749 /// Lookup the type associated to a given DIE.
3750 ///
3751 /// Note that the DIE must have been associated to type by a
3752 /// previous invocation of the function
3753 /// reader::associate_die_to_type().
3754 ///
3755 /// @param die the DIE to consider.
3756 ///
3757 /// @return the type associated to the DIE or NULL if no type is
3758 /// associated to the DIE.
3759 type_base_sptr
3760 lookup_type_from_die(const Dwarf_Die* die) const
3761 {
3762 type_or_decl_base_sptr artifact =
3763 lookup_artifact_from_die(die, /*die_as_type=*/true);
3764 if (function_decl_sptr fn = is_function_decl(artifact))
3765 return fn->get_type();
3766 return is_type(artifact);
3767 }
3768
3769 /// Lookup the type associated to a DIE at a given offset, from a
3770 /// given source.
3771 ///
3772 /// Note that the DIE must have been associated to type by a
3773 /// previous invocation of the function
3774 /// reader::associate_die_to_type().
3775 ///
3776 /// @param die_offset the offset of the DIE to consider.
3777 ///
3778 /// @param source the source of the DIE to consider.
3779 ///
3780 /// @return the type associated to the DIE or NULL if no type is
3781 /// associated to the DIE.
3782 type_base_sptr
3783 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3784 {
3785 type_base_sptr result;
3786 const die_artefact_map_type& m =
3787 type_die_artefact_maps().get_container(source);
3788 die_artefact_map_type::const_iterator i = m.find(die_offset);
3789 if (i != m.end())
3790 {
3791 if (function_decl_sptr fn = is_function_decl(i->second))
3792 return fn->get_type();
3793 result = is_type(i->second);
3794 }
3795
3796 if (!result)
3797 {
3798 // Maybe we are looking for a class type being constructed?
3799 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3800 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3801
3802 if (i != m.end())
3803 result = i->second;
3804 }
3805
3806 if (!result)
3807 {
3808 // Maybe we are looking for a function type being constructed?
3810 die_wip_function_types_map(source);
3811 die_function_type_map_type::const_iterator i = m.find(die_offset);
3812
3813 if (i != m.end())
3814 result = i->second;
3815 }
3816
3817 return result;
3818 }
3819
3820 /// Getter of a map that associates a die that represents a
3821 /// class/struct with the declaration of the class, while the class
3822 /// is being constructed.
3823 ///
3824 /// @param source where the DIE is from.
3825 ///
3826 /// @return the map that associates a DIE to the class that is being
3827 /// built.
3829 die_wip_classes_map(die_source source) const
3830 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3831
3832 /// Getter of a map that associates a die that represents a
3833 /// class/struct with the declaration of the class, while the class
3834 /// is being constructed.
3835 ///
3836 /// @param source where the DIE comes from.
3837 ///
3838 /// @return the map that associates a DIE to the class that is being
3839 /// built.
3841 die_wip_classes_map(die_source source)
3842 {
3843 switch (source)
3844 {
3845 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3846 break;
3847 case ALT_DEBUG_INFO_DIE_SOURCE:
3848 return alternate_die_wip_classes_map_;
3849 case TYPE_UNIT_DIE_SOURCE:
3850 return type_unit_die_wip_classes_map_;
3851 case NO_DEBUG_INFO_DIE_SOURCE:
3852 case NUMBER_OF_DIE_SOURCES:
3854 }
3855 return die_wip_classes_map_;
3856 }
3857
3858 /// Getter for a map that associates a die (that represents a
3859 /// function type) whith a function type, while the function type is
3860 /// being constructed (WIP == work in progress).
3861 ///
3862 /// @param source where the DIE comes from.n
3863 ///
3864 /// @return the map of wip function types.
3866 die_wip_function_types_map(die_source source) const
3867 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3868
3869 /// Getter for a map that associates a die (that represents a
3870 /// function type) whith a function type, while the function type is
3871 /// being constructed (WIP == work in progress).
3872 ///
3873 /// @param source where DIEs of the map come from.
3874 ///
3875 /// @return the map of wip function types.
3877 die_wip_function_types_map(die_source source)
3878 {
3879 switch (source)
3880 {
3881 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3882 break;
3883 case ALT_DEBUG_INFO_DIE_SOURCE:
3884 return alternate_die_wip_function_types_map_;
3885 case TYPE_UNIT_DIE_SOURCE:
3886 return type_unit_die_wip_function_types_map_;
3887 case NO_DEBUG_INFO_DIE_SOURCE:
3888 case NUMBER_OF_DIE_SOURCES:
3890 }
3891 return die_wip_function_types_map_;
3892 }
3893
3894 /// Getter for a map that associates a die with a function decl
3895 /// which has a linkage name but no elf symbol yet.
3896 ///
3897 /// This is to fixup function decls with linkage names, but with no
3898 /// link to their underlying elf symbol. There are some DIEs like
3899 /// that in DWARF sometimes, especially when the compiler optimizes
3900 /// stuff aggressively.
3902 die_function_decl_with_no_symbol_map()
3903 {return die_function_with_no_symbol_map_;}
3904
3905 /// Return true iff a given offset is for the DIE of a class that is
3906 /// being built, but that is not fully built yet. WIP == "work in
3907 /// progress".
3908 ///
3909 /// @param offset the DIE offset to consider.
3910 ///
3911 /// @param source where the DIE of the map come from.
3912 ///
3913 /// @return true iff @p offset is the offset of the DIE of a class
3914 /// that is being currently built.
3915 bool
3916 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3917 {
3918 die_class_or_union_map_type::const_iterator i =
3919 die_wip_classes_map(source).find(offset);
3920 return (i != die_wip_classes_map(source).end());
3921 }
3922
3923 /// Return true iff a given offset is for the DIE of a function type
3924 /// that is being built at the moment, but is not fully built yet.
3925 /// WIP == work in progress.
3926 ///
3927 /// @param offset DIE offset to consider.
3928 ///
3929 /// @param source where the DIE comes from.
3930 ///
3931 /// @return true iff @p offset is the offset of the DIE of a
3932 /// function type that is being currently built.
3933 bool
3934 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3935 {
3936 die_function_type_map_type::const_iterator i =
3937 die_wip_function_types_map(source).find(offset);
3938 return (i != die_wip_function_types_map(source).end());
3939 }
3940
3941 /// Sometimes, a data member die can erroneously have an empty name as
3942 /// a result of a bug of the DWARF emitter.
3943 ///
3944 /// This is what happens in
3945 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3946 ///
3947 /// In that case, this function constructs an artificial name for that
3948 /// data member. The pattern of the name is as follows:
3949 ///
3950 /// "unnamed-@-<location>".
3951 ///
3952 ///location is either the value of the data member location of the
3953 ///data member if it has one or concatenation of its source location
3954 ///if it has none. If no location can be calculated then the function
3955 ///returns the empty string.
3956 string
3957 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
3958 {
3959 string result;
3960 // Let's make sure we are looking at a data member with an empty
3961 // name ...
3962 if (!die
3963 || dwarf_tag(die) != DW_TAG_member
3964 || !die_name(die).empty())
3965 return result;
3966
3967 // ... and yet, it's not an anonymous data member (aka unnamed
3968 // field) as described in
3969 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3970 if (die_is_anonymous_data_member(die))
3971 return result;
3972
3973 // If we come this far, it means we are looking at a buggy data
3974 // member with no name. Let's build a name for it so that it can be
3975 // addressed.
3976 int64_t offset_in_bits = 0;
3977 bool has_offset = die_member_offset(*this, die, offset_in_bits);
3978 location loc;
3979 if (!has_offset)
3980 {
3981 loc = die_location(*this, die);
3982 if (!loc)
3983 return result;
3984 }
3985
3986 std::ostringstream o;
3987 o << "unnamed-dm-@-";
3988 if (has_offset)
3989 o << "offset-" << offset_in_bits << "bits";
3990 else
3991 o << "loc-" << loc.expand();
3992
3993 return o.str();
3994 }
3995
3996 /// Getter for the map of declaration-only classes that are to be
3997 /// resolved to their definition classes by the end of the corpus
3998 /// loading.
3999 ///
4000 /// @return a map of string -> vector of classes where the key is
4001 /// the fully qualified name of the class and the value is the
4002 /// vector of declaration-only class.
4004 declaration_only_classes() const
4005 {return decl_only_classes_map_;}
4006
4007 /// Getter for the map of declaration-only classes that are to be
4008 /// resolved to their definition classes by the end of the corpus
4009 /// loading.
4010 ///
4011 /// @return a map of string -> vector of classes where the key is
4012 /// the fully qualified name of the class and the value is the
4013 /// vector of declaration-only class.
4015 declaration_only_classes()
4016 {return decl_only_classes_map_;}
4017
4018 /// If a given class is a declaration-only class then stash it on
4019 /// the side so that at the end of the corpus reading we can resolve
4020 /// it to its definition.
4021 ///
4022 /// @param klass the class to consider.
4023 void
4024 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
4025 {
4026 if (cou->get_is_declaration_only()
4027 && cou->get_definition_of_declaration() == 0
4028 // Make sure the class is not anonymous. Anonymous classes
4029 // are usually later named by a typedef. At that time, after
4030 // being named by a typedef, this method is going to be called
4031 // with the class being named by the typedef.
4032 && !cou->get_qualified_name().empty())
4033 {
4034 string qn = cou->get_qualified_name();
4035 string_classes_or_unions_map::iterator record =
4036 declaration_only_classes().find(qn);
4037 if (record == declaration_only_classes().end())
4038 declaration_only_classes()[qn].push_back(cou);
4039 else
4040 record->second.push_back(cou);
4041 }
4042 }
4043
4044 /// Test if a given declaration-only class has been scheduled for
4045 /// resolution to a defined class.
4046 ///
4047 /// @param klass the class to consider for the test.
4048 ///
4049 /// @return true iff @p klass is a declaration-only class and if
4050 /// it's been scheduled for resolution to a defined class.
4051 bool
4052 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4053 {
4054 if (cou->get_is_declaration_only())
4055 return ((declaration_only_classes().find(cou->get_qualified_name())
4056 != declaration_only_classes().end())
4057 || (declaration_only_classes().find(cou->get_name())
4058 != declaration_only_classes().end()));
4059
4060 return false;
4061 }
4062
4063 /// Compare two ABI artifacts in a context which canonicalization
4064 /// has not be done yet.
4065 ///
4066 /// @param l the left-hand-side operand of the comparison
4067 ///
4068 /// @param r the right-hand-side operand of the comparison.
4069 ///
4070 /// @return true if @p l equals @p r.
4071 bool
4072 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4073 const type_or_decl_base_sptr &r)
4074 {
4075 if (!l || !r)
4076 return !!l == !!r;
4077
4078 const environment& e = l->get_environment();
4079 ABG_ASSERT(!e.canonicalization_is_done());
4080
4081 e.priv_->allow_type_comparison_results_caching(true);
4082 bool s0 = e.decl_only_class_equals_definition();
4083 e.decl_only_class_equals_definition(true);
4084 bool equal = l == r;
4085 e.decl_only_class_equals_definition(s0);
4086 e.priv_->clear_type_comparison_results_cache();
4087 e.priv_->allow_type_comparison_results_caching(false);
4088 return equal;
4089 }
4090
4091 /// Walk the declaration-only classes that have been found during
4092 /// the building of the corpus and resolve them to their definitions.
4093 void
4094 resolve_declaration_only_classes()
4095 {
4096 vector<string> resolved_classes;
4097
4098 for (string_classes_or_unions_map::iterator i =
4099 declaration_only_classes().begin();
4100 i != declaration_only_classes().end();
4101 ++i)
4102 {
4103 bool to_resolve = false;
4104 for (classes_or_unions_type::iterator j = i->second.begin();
4105 j != i->second.end();
4106 ++j)
4107 if ((*j)->get_is_declaration_only()
4108 && ((*j)->get_definition_of_declaration() == 0))
4109 to_resolve = true;
4110
4111 if (!to_resolve)
4112 {
4113 resolved_classes.push_back(i->first);
4114 continue;
4115 }
4116
4117 // Now, for each decl-only class that have the current name
4118 // 'i->first', let's try to poke at the fully defined class
4119 // that is defined in the same translation unit as the
4120 // declaration.
4121 //
4122 // If we find one class (defined in the TU of the declaration)
4123 // that defines the declaration, then the declaration can be
4124 // resolved to that class.
4125 //
4126 // If no defining class is found in the TU of the declaration,
4127 // then there are possibly three cases to consider:
4128 //
4129 // 1/ There is exactly one class that defines the
4130 // declaration and that class is defined in another TU. In
4131 // this case, the declaration is resolved to that
4132 // definition.
4133 //
4134 // 2/ There are more than one class that define that
4135 // declaration and none of them is defined in the TU of the
4136 // declaration. If those classes are all different, then
4137 // the declaration is left unresolved.
4138 //
4139 // 3/ No class defines the declaration. In this case, the
4140 // declaration is left unresoved.
4141
4142 // So get the classes that might define the current
4143 // declarations which name is i->first.
4144 const type_base_wptrs_type *classes =
4145 lookup_class_types(i->first, *corpus());
4146 if (!classes)
4147 classes = lookup_union_types(i->first, *corpus());
4148
4149 if (!classes)
4150 continue;
4151
4152 // This is a map that associates the translation unit path to
4153 // the class (that potentially defines the declarations that
4154 // we consider) that are defined in that translation unit. It
4155 // should stay ordered by using the TU path as key to ensure
4156 // stability of the order of classe definitions in ABIXML
4157 // output.
4158 map<string, class_or_union_sptr> per_tu_class_map;
4159 for (type_base_wptrs_type::const_iterator c = classes->begin();
4160 c != classes->end();
4161 ++c)
4162 {
4163 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4164 ABG_ASSERT(klass);
4165
4167 if (klass->get_is_declaration_only())
4168 continue;
4169
4170 string tu_path = klass->get_translation_unit()->get_absolute_path();
4171 if (tu_path.empty())
4172 continue;
4173
4174 // Build a map that associates the translation unit path
4175 // to the class (that potentially defines the declarations
4176 // that we consider) that are defined in that translation unit.
4177 per_tu_class_map[tu_path] = klass;
4178 }
4179
4180 if (!per_tu_class_map.empty())
4181 {
4182 // Walk the declarations to resolve and resolve them
4183 // either to the definitions that are in the same TU as
4184 // the declaration, or to the definition found elsewhere,
4185 // if there is only one such definition.
4186 for (classes_or_unions_type::iterator j = i->second.begin();
4187 j != i->second.end();
4188 ++j)
4189 {
4190 if ((*j)->get_is_declaration_only()
4191 && ((*j)->get_definition_of_declaration() == 0))
4192 {
4193 string tu_path =
4194 (*j)->get_translation_unit()->get_absolute_path();
4195 map<string, class_or_union_sptr>::const_iterator e =
4196 per_tu_class_map.find(tu_path);
4197 if (e != per_tu_class_map.end())
4198 (*j)->set_definition_of_declaration(e->second);
4199 else if (per_tu_class_map.size() == 1)
4200 (*j)->set_definition_of_declaration
4201 (per_tu_class_map.begin()->second);
4202 else
4203 {
4204 // We are in case where there are more than
4205 // one definition for the declaration. Let's
4206 // see if they are all equal. If they are,
4207 // then the declaration resolves to the
4208 // definition. Otherwise, we are in the case
4209 // 3/ described above.
4210 map<string,
4211 class_or_union_sptr>::const_iterator it;
4212 class_or_union_sptr first_class =
4213 per_tu_class_map.begin()->second;
4214 bool all_class_definitions_are_equal = true;
4215 for (it = per_tu_class_map.begin();
4216 it != per_tu_class_map.end();
4217 ++it)
4218 {
4219 if (it == per_tu_class_map.begin())
4220 continue;
4221 else
4222 {
4223 if (!compare_before_canonicalisation(it->second,
4224 first_class))
4225 {
4226 all_class_definitions_are_equal = false;
4227 break;
4228 }
4229 }
4230 }
4231 if (all_class_definitions_are_equal)
4232 (*j)->set_definition_of_declaration(first_class);
4233 }
4234 }
4235 }
4236 resolved_classes.push_back(i->first);
4237 }
4238 }
4239
4240 size_t num_decl_only_classes = declaration_only_classes().size(),
4241 num_resolved = resolved_classes.size();
4242 if (show_stats())
4243 cerr << "resolved " << num_resolved
4244 << " class declarations out of "
4245 << num_decl_only_classes
4246 << "\n";
4247
4248 for (vector<string>::const_iterator i = resolved_classes.begin();
4249 i != resolved_classes.end();
4250 ++i)
4251 declaration_only_classes().erase(*i);
4252
4253 if (show_stats() && !declaration_only_classes().empty())
4254 {
4255 cerr << "Here are the "
4256 << num_decl_only_classes - num_resolved
4257 << " unresolved class declarations:\n";
4258 for (string_classes_or_unions_map::iterator i =
4259 declaration_only_classes().begin();
4260 i != declaration_only_classes().end();
4261 ++i)
4262 cerr << " " << i->first << "\n";
4263 }
4264 }
4265
4266 /// Getter for the map of declaration-only enums that are to be
4267 /// resolved to their definition enums by the end of the corpus
4268 /// loading.
4269 ///
4270 /// @return a map of string -> vector of enums where the key is
4271 /// the fully qualified name of the enum and the value is the
4272 /// vector of declaration-only enum.
4273 const string_enums_map&
4274 declaration_only_enums() const
4275 {return decl_only_enums_map_;}
4276
4277 /// Getter for the map of declaration-only enums that are to be
4278 /// resolved to their definition enums by the end of the corpus
4279 /// loading.
4280 ///
4281 /// @return a map of string -> vector of enums where the key is
4282 /// the fully qualified name of the enum and the value is the
4283 /// vector of declaration-only enum.
4285 declaration_only_enums()
4286 {return decl_only_enums_map_;}
4287
4288 /// If a given enum is a declaration-only enum then stash it on
4289 /// the side so that at the end of the corpus reading we can resolve
4290 /// it to its definition.
4291 ///
4292 /// @param enom the enum to consider.
4293 void
4294 maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4295 {
4296 if (enom->get_is_declaration_only()
4297 && enom->get_definition_of_declaration() == 0
4298 // Make sure the enum is not anonymous. Anonymous enums are
4299 // usually later named by a typedef. At that time, after
4300 // being named by a typedef, this method is going to be called
4301 // with the enum being named by the typedef.
4302 && !enom->get_qualified_name().empty())
4303 {
4304 string qn = enom->get_qualified_name();
4305 string_enums_map::iterator record =
4306 declaration_only_enums().find(qn);
4307 if (record == declaration_only_enums().end())
4308 declaration_only_enums()[qn].push_back(enom);
4309 else
4310 record->second.push_back(enom);
4311 }
4312 }
4313
4314 /// Test if a given declaration-only enum has been scheduled for
4315 /// resolution to a defined enum.
4316 ///
4317 /// @param enom the enum to consider for the test.
4318 ///
4319 /// @return true iff @p enom is a declaration-only enum and if
4320 /// it's been scheduled for resolution to a defined enum.
4321 bool
4322 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4323 {
4324 if (enom->get_is_declaration_only())
4325 return (declaration_only_enums().find(enom->get_qualified_name())
4326 != declaration_only_enums().end());
4327
4328 return false;
4329 }
4330
4331 /// Walk the declaration-only enums that have been found during
4332 /// the building of the corpus and resolve them to their definitions.
4333 ///
4334 /// TODO: Do away with this function by factorizing it with
4335 /// resolve_declaration_only_classes. All declaration-only decls
4336 /// could be handled the same way as declaration-only-ness is a
4337 /// property of abigail::ir::decl_base now.
4338 void
4339 resolve_declaration_only_enums()
4340 {
4341 vector<string> resolved_enums;
4342
4343 for (string_enums_map::iterator i =
4344 declaration_only_enums().begin();
4345 i != declaration_only_enums().end();
4346 ++i)
4347 {
4348 bool to_resolve = false;
4349 for (enums_type::iterator j = i->second.begin();
4350 j != i->second.end();
4351 ++j)
4352 if ((*j)->get_is_declaration_only()
4353 && ((*j)->get_definition_of_declaration() == 0))
4354 to_resolve = true;
4355
4356 if (!to_resolve)
4357 {
4358 resolved_enums.push_back(i->first);
4359 continue;
4360 }
4361
4362 // Now, for each decl-only enum that have the current name
4363 // 'i->first', let's try to poke at the fully defined enum
4364 // that is defined in the same translation unit as the
4365 // declaration.
4366 //
4367 // If we find one enum (defined in the TU of the declaration)
4368 // that defines the declaration, then the declaration can be
4369 // resolved to that enum.
4370 //
4371 // If no defining enum is found in the TU of the declaration,
4372 // then there are possibly three cases to consider:
4373 //
4374 // 1/ There is exactly one enum that defines the
4375 // declaration and that enum is defined in another TU. In
4376 // this case, the declaration is resolved to that
4377 // definition.
4378 //
4379 // 2/ There are more than one enum that define that
4380 // declaration and none of them is defined in the TU of the
4381 // declaration. In this case, the declaration is left
4382 // unresolved.
4383 //
4384 // 3/ No enum defines the declaration. In this case, the
4385 // declaration is left unresoved.
4386
4387 // So get the enums that might define the current
4388 // declarations which name is i->first.
4389 const type_base_wptrs_type *enums =
4390 lookup_enum_types(i->first, *corpus());
4391 if (!enums)
4392 continue;
4393
4394 // This is a map that associates the translation unit path to
4395 // the enum (that potentially defines the declarations that
4396 // we consider) that are defined in that translation unit. It
4397 // should stay ordered by using the TU path as key to ensure
4398 // stability of the order of enum definitions in ABIXML
4399 // output.
4400 map<string, enum_type_decl_sptr> per_tu_enum_map;
4401 for (type_base_wptrs_type::const_iterator c = enums->begin();
4402 c != enums->end();
4403 ++c)
4404 {
4405 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4406 ABG_ASSERT(enom);
4407
4409 if (enom->get_is_declaration_only())
4410 continue;
4411
4412 string tu_path = enom->get_translation_unit()->get_absolute_path();
4413 if (tu_path.empty())
4414 continue;
4415
4416 // Build a map that associates the translation unit path
4417 // to the enum (that potentially defines the declarations
4418 // that we consider) that are defined in that translation unit.
4419 per_tu_enum_map[tu_path] = enom;
4420 }
4421
4422 if (!per_tu_enum_map.empty())
4423 {
4424 // Walk the declarations to resolve and resolve them
4425 // either to the definitions that are in the same TU as
4426 // the declaration, or to the definition found elsewhere,
4427 // if there is only one such definition.
4428 for (enums_type::iterator j = i->second.begin();
4429 j != i->second.end();
4430 ++j)
4431 {
4432 if ((*j)->get_is_declaration_only()
4433 && ((*j)->get_definition_of_declaration() == 0))
4434 {
4435 string tu_path =
4436 (*j)->get_translation_unit()->get_absolute_path();
4437 map<string, enum_type_decl_sptr>::const_iterator e =
4438 per_tu_enum_map.find(tu_path);
4439 if (e != per_tu_enum_map.end())
4440 (*j)->set_definition_of_declaration(e->second);
4441 else if (per_tu_enum_map.size() == 1)
4442 (*j)->set_definition_of_declaration
4443 (per_tu_enum_map.begin()->second);
4444 else
4445 {
4446 // We are in case where there are more than
4447 // one definition for the declaration. Let's
4448 // see if they are all equal. If they are,
4449 // then the declaration resolves to the
4450 // definition. Otherwise, we are in the case
4451 // 3/ described above.
4452 map<string,
4453 enum_type_decl_sptr>::const_iterator it;
4454 enum_type_decl_sptr first_enum =
4455 per_tu_enum_map.begin()->second;
4456 bool all_enum_definitions_are_equal = true;
4457 for (it = per_tu_enum_map.begin();
4458 it != per_tu_enum_map.end();
4459 ++it)
4460 {
4461 if (it == per_tu_enum_map.begin())
4462 continue;
4463 else
4464 {
4465 if (!compare_before_canonicalisation(it->second,
4466 first_enum))
4467 {
4468 all_enum_definitions_are_equal = false;
4469 break;
4470 }
4471 }
4472 }
4473 if (all_enum_definitions_are_equal)
4474 (*j)->set_definition_of_declaration(first_enum);
4475 }
4476 }
4477 }
4478 resolved_enums.push_back(i->first);
4479 }
4480 }
4481
4482 size_t num_decl_only_enums = declaration_only_enums().size(),
4483 num_resolved = resolved_enums.size();
4484 if (show_stats())
4485 cerr << "resolved " << num_resolved
4486 << " enum declarations out of "
4487 << num_decl_only_enums
4488 << "\n";
4489
4490 for (vector<string>::const_iterator i = resolved_enums.begin();
4491 i != resolved_enums.end();
4492 ++i)
4493 declaration_only_enums().erase(*i);
4494
4495 if (show_stats() && !declaration_only_enums().empty())
4496 {
4497 cerr << "Here are the "
4498 << num_decl_only_enums - num_resolved
4499 << " unresolved enum declarations:\n";
4500 for (string_enums_map::iterator i = declaration_only_enums().begin();
4501 i != declaration_only_enums().end();
4502 ++i)
4503 cerr << " " << i->first << "\n";
4504 }
4505 }
4506
4507 /// Test if a symbol belongs to a function of the current ABI
4508 /// corpus.
4509 ///
4510 /// This is a sub-routine of fixup_functions_with_no_symbols.
4511 ///
4512 /// @param fn the function symbol to consider.
4513 ///
4514 /// @returnt true if @p fn belongs to a function of the current ABI
4515 /// corpus.
4516 bool
4517 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4518 {
4519 corpus_sptr corp = corpus();
4520 if (!corp)
4521 return false;
4522
4523 interned_string id = corp->get_environment().intern(fn->get_id_string());
4524
4525 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4526 if (!fns)
4527 return false;
4528
4529 for (auto f : *fns)
4530 if (f->get_symbol())
4531 return true;
4532
4533 return false;
4534 }
4535
4536 /// Some functions described by DWARF may have their linkage name
4537 /// set, but no link to their actual underlying elf symbol. When
4538 /// these are virtual member functions, comparing the enclosing type
4539 /// against another one which has its underlying symbol properly set
4540 /// might lead to spurious type changes.
4541 ///
4542 /// If the corpus contains a symbol with the same name as the
4543 /// linkage name of the function, then set up the link between the
4544 /// function and its underlying symbol.
4545 ///
4546 /// Note that for the moment, only virtual member functions are
4547 /// fixed up like this. This is because they really are the only
4548 /// fuctions of functions that can affect types (in spurious ways).
4549 void
4550 fixup_functions_with_no_symbols()
4551 {
4552 corpus_sptr corp = corpus();
4553 if (!corp)
4554 return;
4555
4556 die_function_decl_map_type &fns_with_no_symbol =
4557 die_function_decl_with_no_symbol_map();
4558
4559 if (do_log())
4560 cerr << fns_with_no_symbol.size()
4561 << " functions to fixup, potentially\n";
4562
4563 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4564 i != fns_with_no_symbol.end();
4565 ++i)
4566 if (elf_symbol_sptr sym =
4567 corp->lookup_function_symbol(i->second->get_linkage_name()))
4568 {
4569 // So i->second is a virtual member function that was
4570 // previously scheduled to be set a function symbol.
4571 //
4572 // But if it appears that it now has a symbol already set,
4573 // then do not set a symbol to it again.
4574 //
4575 // Or if it appears that another virtual member function
4576 // from the current ABI Corpus, with the same linkage
4577 // (mangled) name has already been set a symbol, then do not
4578 // set a symbol to this function either. Otherwise, there
4579 // will be two virtual member functions with the same symbol
4580 // in the class and that leads to spurious hard-to-debug
4581 // change reports later down the road.
4582 if (i->second->get_symbol()
4583 || symbol_already_belongs_to_a_function(sym))
4584 continue;
4585
4586 ABG_ASSERT(is_member_function(i->second));
4588 i->second->set_symbol(sym);
4589
4590 if (do_log())
4591 cerr << "fixed up '"
4592 << i->second->get_pretty_representation()
4593 << "' with symbol '"
4594 << sym->get_id_string()
4595 << "'\n";
4596 }
4597
4598 fns_with_no_symbol.clear();
4599 }
4600
4601 /// Copy missing member functions from a source @ref class_decl to a
4602 /// destination one.
4603 ///
4604 /// If a function is present on the source @ref class_decl and not
4605 /// on the destination one, then it's copied from the source class
4606 /// to the destination one.
4607 void
4608 copy_missing_member_functions(const class_decl_sptr& dest_class,
4609 const class_decl_sptr& src_class)
4610 {
4611 for (auto method : src_class->get_member_functions())
4612 if (!method->get_linkage_name().empty())
4613 if (!dest_class->find_member_function(method->get_linkage_name()))
4614 {
4615 method_decl_sptr copied_method =
4616 copy_member_function(dest_class, method);
4617 ABG_ASSERT(copied_method);
4618 schedule_type_for_late_canonicalization(copied_method->get_type());
4619 }
4620 }
4621
4622 /// Test if there is an interator in a given range that points to
4623 /// an anonymous class.
4624 ///
4625 /// @param begin the start of the iterator range to consider.
4626 ///
4627 /// @param end the end of the iterator range to consider. This
4628 /// points to after the range.
4629 template <typename iterator_type>
4630 bool
4631 contains_anonymous_class(const iterator_type& begin,
4632 const iterator_type& end)
4633 {
4634 for (auto i = begin; i < end; ++i)
4635 {
4636 type_base_sptr t(*i);
4638 if (c && c->get_is_anonymous())
4639 return true;
4640 }
4641 return false;
4642 }
4643
4644 /// Ensure that all classes of the same name have the same virtual
4645 /// member functions. So copy the virtual member functions from a
4646 /// class C that have them to another class C that doesn't.
4647 ///
4648 /// @param begin an iterator to the first member of the set of
4649 /// classes which to merge virtual member functions for.
4650 ///
4651 /// @param end an iterator to the last member (one past the end
4652 /// actually) of the set of classes which to merge virtual member
4653 /// functions for.
4654 template <typename iterator_type>
4655 void
4656 merge_member_functions_of_classes(const iterator_type& begin,
4657 const iterator_type& end)
4658 {
4659 if (contains_anonymous_class(begin, end))
4660 return;
4661
4662 for (auto i = begin; i < end; ++i)
4663 {
4664 type_base_sptr t(*i);
4665 class_decl_sptr reference_class = is_class_type(t);
4666 if (!reference_class)
4667 continue;
4668
4669 string n1 = reference_class->get_pretty_representation(true, true);
4670 string n2;
4671 for (auto j = begin; j < end; ++j)
4672 {
4673 if (j == i)
4674 continue;
4675
4676 type_base_sptr type(*j);
4677 class_decl_sptr klass = is_class_type(type);
4678 if (!klass)
4679 continue;
4680
4681 n2 = klass->get_pretty_representation(true, true);
4682 ABG_ASSERT(n1 == n2);
4683
4684 copy_missing_member_functions(reference_class, klass);
4685 copy_missing_member_functions(klass, reference_class);
4686 }
4687 }
4688 }
4689
4690 /// Ensure that all classes of the same name have the same virtual
4691 /// member functions. So copy the virtual member functions from a
4692 /// class C that have them to another class C that doesn't.
4693 void
4694 merge_member_functions_in_classes_of_same_names()
4695 {
4696 corpus_sptr abi = corpus();
4697 if (!abi)
4698 return;
4699
4701 abi->get_types().class_types();
4702
4703 for (auto entry : class_types)
4704 {
4705 auto& classes = entry.second;
4706 if (classes.size() > 1)
4707 {
4708 bool a_class_has_member_fns = false;
4709 for (auto& c : classes)
4710 {
4711 type_base_sptr t(c);
4712 if (class_decl_sptr klass = is_class_type(t))
4713 if (!klass->get_member_functions().empty())
4714 {
4715 a_class_has_member_fns = true;
4716 break;
4717 }
4718 }
4719 if (a_class_has_member_fns)
4720 merge_member_functions_of_classes(classes.begin(),
4721 classes.end());
4722 }
4723 }
4724 }
4725
4726 /// @return vectors of types created during the analysis of the
4727 /// DWARF and in the need of being canonicalized.
4728 const vector<type_base_sptr>&
4729 types_to_canonicalize() const
4730 {return types_to_canonicalize_;}
4731
4732 /// @return vectors of types created during the analysis of the
4733 /// DWARF and in the need of being canonicalized.
4734 vector<type_base_sptr>&
4735 types_to_canonicalize()
4736 {return types_to_canonicalize_;}
4737
4738 /// Clear the containers holding types to canonicalize.
4739 void
4740 clear_types_to_canonicalize()
4741 {
4742 types_to_canonicalize_.clear();
4743 }
4744
4745 /// Types that were created but not tied to a particular DIE, must
4746 /// be scheduled for late canonicalization using this method.
4747 ///
4748 /// @param t the type to schedule for late canonicalization.
4749 void
4750 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4751 {
4752 types_to_canonicalize_.push_back(t);
4753 }
4754
4755 /// Canonicalize types which DIE offsets are stored in vectors on
4756 /// the side. This is a sub-routine of
4757 /// reader::perform_late_type_canonicalizing().
4758 ///
4759 /// @param source where the DIE of the types to canonicalize are
4760 /// from.
4761 void
4762 canonicalize_types_scheduled()
4763 {
4764 tools_utils::timer cn_timer;
4765 if (do_log())
4766 {
4767 cerr << "DWARF Reader is going to canonicalize types";
4768 corpus_sptr c = corpus();
4769 if (c)
4770 cerr << " of corpus " << corpus()->get_path() << "\n";
4771 cn_timer.start();
4772 }
4773
4774 if (!types_to_canonicalize().empty())
4775 canonicalize_types(types_to_canonicalize().begin(),
4776 types_to_canonicalize().end(),
4777 [](const vector<type_base_sptr>::const_iterator& i)
4778 {return *i;});
4779
4780 if (do_log())
4781 {
4782 cn_timer.stop();
4783 cerr << "finished canonicalizing types";
4784 corpus_sptr c = corpus();
4785 if (c)
4786 cerr << " of corpus " << corpus()->get_path();
4787 cerr << ": (" << cn_timer << ")\n";
4788 }
4789 }
4790
4791 /// Compute the number of canonicalized and missed types in the late
4792 /// canonicalization phase.
4793 ///
4794 /// @param source where the DIEs of the canonicalized types are
4795 /// from.
4796 ///
4797 /// @param canonicalized the number of types that got canonicalized
4798 /// is added to the value already present in this parameter.
4799 ///
4800 /// @param missed the number of types scheduled for late
4801 /// canonicalization and which couldn't be canonicalized (for a
4802 /// reason) is added to the value already present in this parameter.
4803 void
4804 add_late_canonicalized_types_stats(size_t& canonicalized,
4805 size_t& missed) const
4806 {
4807 for (auto t : types_to_canonicalize())
4808 {
4809 if (t->get_canonical_type())
4810 ++canonicalized;
4811 else
4812 ++missed;
4813 }
4814 }
4815
4816 // Look at the types that need to be canonicalized after the
4817 // translation unit has been constructed and canonicalize them.
4818 void
4819 perform_late_type_canonicalizing()
4820 {
4821 canonicalize_types_scheduled();
4822
4823 if (show_stats())
4824 {
4825 size_t num_canonicalized = 0, num_missed = 0, total = 0;
4826 add_late_canonicalized_types_stats(num_canonicalized,
4827 num_missed);
4828 total = num_canonicalized + num_missed;
4829 cerr << "binary: "
4830 << elf_path()
4831 << "\n";
4832 cerr << " # late canonicalized types: "
4833 << num_canonicalized;
4834 if (total)
4835 cerr << " (" << num_canonicalized * 100 / total << "%)";
4836 cerr << "\n"
4837 << " # missed canonicalization opportunities: "
4838 << num_missed;
4839 if (total)
4840 cerr << " (" << num_missed * 100 / total << "%)";
4841 cerr << "\n";
4842 }
4843
4844 }
4845
4846 const die_tu_map_type&
4847 die_tu_map() const
4848 {return die_tu_map_;}
4849
4851 die_tu_map()
4852 {return die_tu_map_;}
4853
4854 /// Getter for the map that associates a translation unit DIE to the
4855 /// vector of imported unit points that it contains.
4856 ///
4857 /// @param source where the DIEs are from.
4858 ///
4859 /// @return the map.
4861 tu_die_imported_unit_points_map(die_source source) const
4862 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4863
4864 /// Getter for the map that associates a translation unit DIE to the
4865 /// vector of imported unit points that it contains.
4866 ///
4867 /// @param source where the DIEs are from.
4868 ///
4869 /// @return the map.
4871 tu_die_imported_unit_points_map(die_source source)
4872 {
4873 switch (source)
4874 {
4875 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4876 break;
4877 case ALT_DEBUG_INFO_DIE_SOURCE:
4878 return alt_tu_die_imported_unit_points_map_;
4879 case TYPE_UNIT_DIE_SOURCE:
4880 return type_units_tu_die_imported_unit_points_map_;
4881 case NO_DEBUG_INFO_DIE_SOURCE:
4882 case NUMBER_OF_DIE_SOURCES:
4883 // We cannot reach this point.
4885 }
4886 return tu_die_imported_unit_points_map_;
4887 }
4888
4889 /// Reset the current corpus being constructed.
4890 ///
4891 /// This actually deletes the current corpus being constructed.
4892 void
4893 reset_corpus()
4894 {corpus().reset();}
4895
4896 /// Get the map that associates each DIE to its parent DIE. This is
4897 /// for DIEs coming from the main debug info sections.
4898 ///
4899 /// @param source where the DIEs in the map come from.
4900 ///
4901 /// @return the DIE -> parent map.
4903 die_parent_map(die_source source) const
4904 {return const_cast<reader*>(this)->die_parent_map(source);}
4905
4906 /// Get the map that associates each DIE to its parent DIE. This is
4907 /// for DIEs coming from the main debug info sections.
4908 ///
4909 /// @param source where the DIEs in the map come from.
4910 ///
4911 /// @return the DIE -> parent map.
4913 die_parent_map(die_source source)
4914 {
4915 switch (source)
4916 {
4917 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4918 break;
4919 case ALT_DEBUG_INFO_DIE_SOURCE:
4920 return alternate_die_parent_map_;
4921 case TYPE_UNIT_DIE_SOURCE:
4922 return type_section_die_parent_map();
4923 case NO_DEBUG_INFO_DIE_SOURCE:
4924 case NUMBER_OF_DIE_SOURCES:
4926 }
4927 return primary_die_parent_map_;
4928 }
4929
4931 type_section_die_parent_map() const
4932 {return type_section_die_parent_map_;}
4933
4935 type_section_die_parent_map()
4936 {return type_section_die_parent_map_;}
4937
4938 /// Getter of the current translation unit.
4939 ///
4940 /// @return the current translation unit being constructed.
4942 cur_transl_unit() const
4943 {return cur_tu_;}
4944
4945 /// Getter of the current translation unit.
4946 ///
4947 /// @return the current translation unit being constructed.
4949 cur_transl_unit()
4950 {return cur_tu_;}
4951
4952 /// Setter of the current translation unit.
4953 ///
4954 /// @param tu the current translation unit being constructed.
4955 void
4956 cur_transl_unit(translation_unit_sptr tu)
4957 {
4958 if (tu)
4959 cur_tu_ = tu;
4960 }
4961
4962 /// Return the global scope of the current translation unit.
4963 ///
4964 /// @return the global scope of the current translation unit.
4965 const scope_decl_sptr&
4966 global_scope() const
4967 {return cur_transl_unit()->get_global_scope();}
4968
4969 /// Return a scope that is nil.
4970 ///
4971 /// @return a scope that is nil.
4972 const scope_decl_sptr&
4973 nil_scope() const
4974 {return nil_scope_;}
4975
4976 const scope_stack_type&
4977 scope_stack() const
4978 {return scope_stack_;}
4979
4981 scope_stack()
4982 {return scope_stack_;}
4983
4984 scope_decl*
4985 current_scope()
4986 {
4987 if (scope_stack().empty())
4988 {
4989 if (cur_transl_unit())
4990 scope_stack().push(cur_transl_unit()->get_global_scope().get());
4991 }
4992 return scope_stack().top();
4993 }
4994
4995 list<var_decl_sptr>&
4996 var_decls_to_re_add_to_tree()
4997 {return var_decls_to_add_;}
4998
4999 /// Test if a DIE represents a decl (function or variable) that has
5000 /// a symbol that is exported, whatever that means. This is
5001 /// supposed to work for Linux Kernel binaries as well.
5002 ///
5003 /// This is useful to limit the amount of DIEs taken into account to
5004 /// the strict limit of what an ABI actually means. Limiting the
5005 /// volume of DIEs analyzed this way is an important optimization to
5006 /// keep big binaries "manageable" by libabigail.
5007 ///
5008 /// @param DIE the die to consider.
5009 bool
5010 is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
5011 {
5012 if (!die || !die_is_decl(die))
5013 return false;
5014
5015 bool result = false, address_found = false, symbol_is_exported = false;;
5016 Dwarf_Addr decl_symbol_address = 0;
5017
5018 if (die_is_variable_decl(die))
5019 {
5020 if ((address_found = get_variable_address(die, decl_symbol_address)))
5021 symbol_is_exported =
5022 !!variable_symbol_is_exported(decl_symbol_address);
5023 }
5024 else if (die_is_function_decl(die))
5025 {
5026 if ((address_found = get_function_address(die, decl_symbol_address)))
5027 symbol_is_exported =
5028 !!function_symbol_is_exported(decl_symbol_address);
5029 }
5030
5031 if (address_found)
5032 result = symbol_is_exported;
5033
5034 return result;
5035 }
5036
5037 /// Test if a DIE is a variable or function DIE which name denotes
5038 /// an undefined ELF symbol.
5039 ///
5040 /// @return true iff @p die represents a function or variable that
5041 /// has an undefined symbol.
5042 bool
5043 is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
5044 {
5045 if (is_decl_die_with_exported_symbol(die))
5046 return false;
5047
5048 string name, linkage_name;
5049 die_name_and_linkage_name(die, name, linkage_name);
5050 if (linkage_name.empty())
5051 linkage_name = name;
5052
5053 bool result = false;
5054 if ((die_is_variable_decl(die)
5055 && symtab()->variable_symbol_is_undefined(linkage_name))
5056 ||
5057 (die_is_function_decl(die)
5058 && symtab()->function_symbol_is_undefined(linkage_name)))
5059 result = true;
5060
5061 return result;
5062 }
5063
5064 /// This is a sub-routine of maybe_adjust_fn_sym_address and
5065 /// maybe_adjust_var_sym_address.
5066 ///
5067 /// Given an address that we got by looking at some debug
5068 /// information (e.g, a symbol's address referred to by a DWARF
5069 /// TAG), If the ELF file we are interested in is a shared library
5070 /// or an executable, then adjust the address to be coherent with
5071 /// where the executable (or shared library) is loaded. That way,
5072 /// the address can be used to look for symbols in the executable or
5073 /// shared library.
5074 ///
5075 /// @return the adjusted address, or the same address as @p addr if
5076 /// it didn't need any adjustment.
5077 Dwarf_Addr
5078 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
5079 {
5080 if (addr == 0)
5081 return addr;
5082
5083 GElf_Ehdr eh_mem;
5084 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5085
5086 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
5087 {
5088 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
5089 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
5090 dwarf_elf_load_address));
5092 elf_load_address));
5093 if (dwarf_is_splitted()
5094 && (dwarf_elf_load_address != elf_load_address))
5095 // This means that in theory the DWARF and the executable are
5096 // not loaded at the same address. And addr is meaningful
5097 // only in the context of the DWARF.
5098 //
5099 // So let's transform addr into an offset relative to where
5100 // the DWARF is loaded, and let's add that relative offset
5101 // to the load address of the executable. That way, addr
5102 // becomes meaningful in the context of the executable and
5103 // can thus be used to compare against the address of
5104 // symbols of the executable, for instance.
5105 addr = addr - dwarf_elf_load_address + elf_load_address;
5106 }
5107
5108 return addr;
5109 }
5110
5111 /// For a relocatable (*.o) elf file, this function expects an
5112 /// absolute address, representing a function symbol. It then
5113 /// extracts the address of the .text section from the symbol
5114 /// absolute address to get the relative address of the function
5115 /// from the beginning of the .text section.
5116 ///
5117 /// For executable or shared library, this function expects an
5118 /// address of a function symbol that was retrieved by looking at a
5119 /// DWARF "file". The function thus adjusts the address to make it
5120 /// be meaningful in the context of the ELF file.
5121 ///
5122 /// In both cases, the address can then be compared against the
5123 /// st_value field of a function symbol from the ELF file.
5124 ///
5125 /// @param addr an adress for a function symbol that was retrieved
5126 /// from a DWARF file.
5127 ///
5128 /// @return the (possibly) adjusted address, or just @p addr if no
5129 /// adjustment took place.
5130 Dwarf_Addr
5131 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
5132 {
5133 if (addr == 0)
5134 return addr;
5135
5136 Elf* elf = elf_handle();
5137 GElf_Ehdr eh_mem;
5138 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5139
5140 if (elf_header->e_type == ET_REL)
5141 // We are looking at a relocatable file. In this case, we don't
5142 // do anything because:
5143 //
5144 // 1/ the addresses from DWARF are absolute (relative to the
5145 // beginning of the relocatable file)
5146 //
5147 // 2/ The ELF symbol addresses that we store in our lookup
5148 // tables are translated from section-related to absolute as
5149 // well. So we don't have anything to do at this point for
5150 // ET_REL files.
5151 ;
5152 else
5153 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5154
5155 return addr;
5156 }
5157
5158 /// For a relocatable (*.o) elf file, this function expects an
5159 /// absolute address, representing a global variable symbol. It
5160 /// then extracts the address of the {.data,.data1,.rodata,.bss}
5161 /// section from the symbol absolute address to get the relative
5162 /// address of the variable from the beginning of the data section.
5163 ///
5164 /// For executable or shared library, this function expects an
5165 /// address of a variable symbol that was retrieved by looking at a
5166 /// DWARF "file". The function thus adjusts the address to make it
5167 /// be meaningful in the context of the ELF file.
5168 ///
5169 /// In both cases, the address can then be compared against the
5170 /// st_value field of a function symbol from the ELF file.
5171 ///
5172 /// @param addr an address for a global variable symbol that was
5173 /// retrieved from a DWARF file.
5174 ///
5175 /// @return the (possibly) adjusted address, or just @p addr if no
5176 /// adjustment took place.
5177 Dwarf_Addr
5178 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5179 {
5180 Elf* elf = elf_handle();
5181 GElf_Ehdr eh_mem;
5182 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5183
5184 if (elf_header->e_type == ET_REL)
5185 // We are looking at a relocatable file. In this case, we don't
5186 // do anything because:
5187 //
5188 // 1/ the addresses from DWARF are absolute (relative to the
5189 // beginning of the relocatable file)
5190 //
5191 // 2/ The ELF symbol addresses that we store in our lookup
5192 // tables are translated from section-related to absolute as
5193 // well. So we don't have anything to do at this point for
5194 // ET_REL files.
5195 ;
5196 else
5197 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5198
5199 return addr;
5200 }
5201
5202 /// Get the first exported function address in the set of addresses
5203 /// referred to by the DW_AT_ranges attribute of a given DIE.
5204 ///
5205 /// @param die the DIE we are considering.
5206 ///
5207 /// @param address output parameter. This is set to the first
5208 /// address found in the sequence pointed to by the DW_AT_ranges
5209 /// attribute found on the DIE @p die, iff the function returns
5210 /// true. Otherwise, no value is set into this output parameter.
5211 ///
5212 /// @return true iff the DIE @p die does have a DW_AT_ranges
5213 /// attribute and an address of an exported function was found in
5214 /// its sequence value.
5215 bool
5216 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5217 Dwarf_Addr& address) const
5218 {
5219 Dwarf_Addr base;
5220 Dwarf_Addr end_addr;
5221 ptrdiff_t offset = 0;
5222
5223 do
5224 {
5225 Dwarf_Addr addr = 0, fn_addr = 0;
5226 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5227 {
5228 fn_addr = maybe_adjust_fn_sym_address(addr);
5229 if (function_symbol_is_exported(fn_addr))
5230 {
5231 address = fn_addr;
5232 return true;
5233 }
5234 }
5235 } while (offset > 0);
5236 return false;
5237 }
5238
5239 /// Get the address of the function.
5240 ///
5241 /// The address of the function is considered to be the value of the
5242 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5243 /// only) to not point to an absolute address anymore, but rather to
5244 /// the address of the function inside the .text segment.
5245 ///
5246 /// @param function_die the die of the function to consider.
5247 ///
5248 /// @param address the resulting address iff the function returns
5249 /// true.
5250 ///
5251 /// @return true if the function address was found.
5252 bool
5253 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5254 {
5255 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5256 DW_AT_low_pc, address))
5257 // So no DW_AT_low_pc was found. Let's see if the function DIE
5258 // has got a DW_AT_ranges attribute instead. If it does, the
5259 // first address of the set of addresses represented by the
5260 // value of that DW_AT_ranges represents the function (symbol)
5261 // address we are looking for.
5262 if (!get_first_exported_fn_address_from_DW_AT_ranges
5263 (const_cast<Dwarf_Die*>(function_die),
5264 address))
5265 return false;
5266
5267 address = maybe_adjust_fn_sym_address(address);
5268 return true;
5269 }
5270
5271 /// Get the address of the global variable.
5272 ///
5273 /// The address of the global variable is considered to be the value
5274 /// of the DW_AT_location attribute, possibly adjusted (in
5275 /// relocatable files only) to not point to an absolute address
5276 /// anymore, but rather to the address of the global variable inside
5277 /// the data segment.
5278 ///
5279 /// @param variable_die the die of the function to consider.
5280 ///
5281 /// @param address the resulting address iff this function returns
5282 /// true.
5283 ///
5284 /// @return true if the variable address was found.
5285 bool
5286 get_variable_address(const Dwarf_Die* variable_die,
5287 Dwarf_Addr& address) const
5288 {
5289 bool is_tls_address = false;
5290 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5291 address, is_tls_address))
5292 return false;
5293 if (!is_tls_address)
5294 address = maybe_adjust_var_sym_address(address);
5295 return true;
5296 }
5297
5298 /// Getter of the exported decls builder object.
5299 ///
5300 /// @return the exported decls builder.
5301 corpus::exported_decls_builder*
5302 exported_decls_builder()
5303 {return corpus()->get_exported_decls_builder().get();}
5304
5305 /// Getter of the "load_all_types" flag. This flag tells if all the
5306 /// types (including those not reachable by public declarations) are
5307 /// to be read and represented in the final ABI corpus.
5308 ///
5309 /// @return the load_all_types flag.
5310 bool
5311 load_all_types() const
5312 {return options().load_all_types;}
5313
5314 /// Setter of the "load_all_types" flag. This flag tells if all the
5315 /// types (including those not reachable by public declarations) are
5316 /// to be read and represented in the final ABI corpus.
5317 ///
5318 /// @param f the new load_all_types flag.
5319 void
5320 load_all_types(bool f)
5321 {options().load_all_types = f;}
5322
5323 bool
5324 load_in_linux_kernel_mode() const
5325 {return options().load_in_linux_kernel_mode;}
5326
5327 void
5328 load_in_linux_kernel_mode(bool f)
5329 {options().load_in_linux_kernel_mode = f;}
5330
5331 /// Getter of the 'load-undefined-interface' property.
5332 ///
5333 /// That property tells the reader if it should load the interfaces
5334 /// that are undefined in the binary. An undefined interface is a
5335 /// variable or function which has a symbol that is not defined in
5336 /// the binary.
5337 ///
5338 /// @return true iff the front-end has to load the undefined
5339 /// interfaces.
5340 bool
5341 load_undefined_interfaces() const
5343
5344 /// Test if it's allowed to assume that the DWARF debug info has
5345 /// been factorized (for instance, with the DWZ tool) so that if two
5346 /// type DIEs originating from the .gnu_debugaltlink section have
5347 /// different offsets, they represent different types.
5348 ///
5349 /// @return true iff we can assume that the DWARF debug info has
5350 /// been factorized.
5351 bool
5352 leverage_dwarf_factorization() const
5353 {
5354 if (!leverage_dwarf_factorization_.has_value())
5355 {
5356 if (options().leverage_dwarf_factorization
5357 && elf_helpers::find_section_by_name(elf_handle(),
5358 ".gnu_debugaltlink"))
5359 leverage_dwarf_factorization_ = true;
5360 else
5361 leverage_dwarf_factorization_ = false;
5362 }
5363 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5364
5365 return *leverage_dwarf_factorization_;
5366 }
5367 /// Getter of the "show_stats" flag.
5368 ///
5369 /// This flag tells if we should emit statistics about various
5370 /// internal stuff.
5371 ///
5372 /// @return the value of the flag.
5373 bool
5374 show_stats() const
5375 {return options().show_stats;}
5376
5377 /// Setter of the "show_stats" flag.
5378 ///
5379 /// This flag tells if we should emit statistics about various
5380 /// internal stuff.
5381 ///
5382 /// @param f the value of the flag.
5383 void
5384 show_stats(bool f)
5385 {options().show_stats = f;}
5386
5387 /// Getter of the "do_log" flag.
5388 ///
5389 /// This flag tells if we should log about various internal
5390 /// details.
5391 ///
5392 /// return the "do_log" flag.
5393 bool
5394 do_log() const
5395 {return options().do_log;}
5396
5397 /// Setter of the "do_log" flag.
5398 ///
5399 /// This flag tells if we should log about various internal details.
5400 ///
5401 /// @param f the new value of the flag.
5402 void
5403 do_log(bool f)
5404 {options().do_log = f;}
5405
5406 /// Walk the DIEs under a given die and for each child, populate the
5407 /// die -> parent map to record the child -> parent relationship
5408 /// that
5409 /// exists between the child and the given die.
5410 ///
5411 /// The function also builds the vector of places where units are
5412 /// imported.
5413 ///
5414 /// This is done recursively as for each child DIE, this function
5415 /// walks its children as well.
5416 ///
5417 /// @param die the DIE whose children to walk recursively.
5418 ///
5419 /// @param source where the DIE @p die comes from.
5420 ///
5421 /// @param imported_units a vector containing all the offsets of the
5422 /// points where unit have been imported, under @p die.
5423 void
5424 build_die_parent_relations_under(Dwarf_Die* die,
5425 die_source source,
5426 imported_unit_points_type & imported_units)
5427 {
5428 if (!die)
5429 return;
5430
5431 offset_offset_map_type& parent_of = die_parent_map(source);
5432
5433 Dwarf_Die child;
5434 if (dwarf_child(die, &child) != 0)
5435 return;
5436
5437 do
5438 {
5439 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5440 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5441 {
5442 Dwarf_Die imported_unit;
5443 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5444 // If the imported_unit has a sub-tree, let's record
5445 // this point at which the sub-tree is imported into
5446 // the current debug info.
5447 //
5448 // Otherwise, if the imported_unit has no sub-tree,
5449 // there is no point in recording where a non-existent
5450 // sub-tree is being imported.
5451 //
5452 // Note that the imported_unit_points_type type below
5453 // expects the imported_unit to have a sub-tree.
5454 && die_has_children(&imported_unit))
5455 {
5456 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5457 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5458 imported_units.push_back
5459 (imported_unit_point(dwarf_dieoffset(&child),
5460 imported_unit,
5461 imported_unit_die_source));
5462 }
5463 }
5464 build_die_parent_relations_under(&child, source, imported_units);
5465 }
5466 while (dwarf_siblingof(&child, &child) == 0);
5467
5468 }
5469
5470 /// Determine if we do have to build a DIE -> parent map, depending
5471 /// on a given language.
5472 ///
5473 /// Some languages like C++, Ada etc, do have the concept of
5474 /// namespace and yet, the DIE data structure doesn't provide us
5475 /// with a way to get the parent namespace of a given DIE. So for
5476 /// those languages, we need to build a DIE -> parent map so that we
5477 /// can get the namespace DIE (or more generally the scope DIE) of a given
5478 /// DIE as we need it.
5479 ///
5480 /// But then some more basic languages like C or assembly don't have
5481 /// that need.
5482 ///
5483 /// This function, depending on the language, tells us if we need to
5484 /// build the DIE -> parent map or not.
5485 ///
5486 /// @param lang the language to consider.
5487 ///
5488 /// @return true iff we need to build the DIE -> parent map for this
5489 /// language.
5490 bool
5491 do_we_build_die_parent_maps(translation_unit::language lang)
5492 {
5493 if (is_c_language(lang))
5494 return false;
5495
5496 switch (lang)
5497 {
5498 case translation_unit::LANG_UNKNOWN:
5499#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5500 case translation_unit::LANG_Mips_Assembler:
5501#endif
5502 return false;
5503 default:
5504 break;
5505 }
5506 return true;
5507 }
5508
5509 /// Walk all the DIEs accessible in the debug info (and in the
5510 /// alternate debug info as well) and build maps representing the
5511 /// relationship DIE -> parent. That is, make it so that we can get
5512 /// the parent for a given DIE.
5513 ///
5514 /// Note that the goal of this map is to be able to get the parent
5515 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5516 /// when we get a DIE of a type, and we want to build an internal
5517 /// representation for it, we need to get its fully qualified name.
5518 /// For that, we need to know what is the parent DIE of that type
5519 /// DIE, so that we can know what the namespace of that type is.
5520 ///
5521 /// Note that as the C language doesn't have namespaces (all types
5522 /// are defined in the same global namespace), this function doesn't
5523 /// build the DIE -> parent map if the current translation unit
5524 /// comes from C. This saves time on big C ELF files with a lot of
5525 /// DIEs.
5526 void
5527 build_die_parent_maps()
5528 {
5529 bool we_do_have_to_build_die_parent_map = false;
5530 uint8_t address_size = 0;
5531 size_t header_size = 0;
5532 // Get the DIE of the current translation unit, look at it to get
5533 // its language. If that language is in C, then all types are in
5534 // the global namespace so we don't need to build the DIE ->
5535 // parent map. So we dont build it in that case.
5536 for (Dwarf_Off offset = 0, next_offset = 0;
5537 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5538 offset, &next_offset, &header_size,
5539 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5540 offset = next_offset)
5541 {
5542 Dwarf_Off die_offset = offset + header_size;
5543 Dwarf_Die cu;
5544 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5545 die_offset, &cu))
5546 continue;
5547
5548 uint64_t l = 0;
5549 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5550 translation_unit::language lang = dwarf_language_to_tu_language(l);
5551 if (do_we_build_die_parent_maps(lang))
5552 we_do_have_to_build_die_parent_map = true;
5553 }
5554
5555 if (!we_do_have_to_build_die_parent_map)
5556 return;
5557
5558 // Build the DIE -> parent relation for DIEs coming from the
5559 // .debug_info section in the alternate debug info file.
5560 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5561 for (Dwarf_Off offset = 0, next_offset = 0;
5562 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5563 offset, &next_offset, &header_size,
5564 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5565 offset = next_offset)
5566 {
5567 Dwarf_Off die_offset = offset + header_size;
5568 Dwarf_Die cu;
5569 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5570 die_offset, &cu))
5571 continue;
5572 cur_tu_die(&cu);
5573
5574 imported_unit_points_type& imported_units =
5575 tu_die_imported_unit_points_map(source)[die_offset] =
5577 build_die_parent_relations_under(&cu, source, imported_units);
5578 }
5579
5580 // Build the DIE -> parent relation for DIEs coming from the
5581 // .debug_info section of the main debug info file.
5582 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5583 address_size = 0;
5584 header_size = 0;
5585 for (Dwarf_Off offset = 0, next_offset = 0;
5586 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5587 offset, &next_offset, &header_size,
5588 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5589 offset = next_offset)
5590 {
5591 Dwarf_Off die_offset = offset + header_size;
5592 Dwarf_Die cu;
5593 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5594 die_offset, &cu))
5595 continue;
5596 cur_tu_die(&cu);
5597 imported_unit_points_type& imported_units =
5598 tu_die_imported_unit_points_map(source)[die_offset] =
5600 build_die_parent_relations_under(&cu, source, imported_units);
5601 }
5602
5603 // Build the DIE -> parent relation for DIEs coming from the
5604 // .debug_types section.
5605 source = TYPE_UNIT_DIE_SOURCE;
5606 address_size = 0;
5607 header_size = 0;
5608 uint64_t type_signature = 0;
5609 Dwarf_Off type_offset;
5610 for (Dwarf_Off offset = 0, next_offset = 0;
5611 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5612 offset, &next_offset, &header_size,
5613 NULL, NULL, &address_size, NULL,
5614 &type_signature, &type_offset) == 0);
5615 offset = next_offset)
5616 {
5617 Dwarf_Off die_offset = offset + header_size;
5618 Dwarf_Die cu;
5619
5620 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5621 die_offset, &cu))
5622 continue;
5623 cur_tu_die(&cu);
5624 imported_unit_points_type& imported_units =
5625 tu_die_imported_unit_points_map(source)[die_offset] =
5627 build_die_parent_relations_under(&cu, source, imported_units);
5628 }
5629 }
5630};// end class reader.
5631
5632/// The type of the aggregates being compared during a DIE comparison.
5633///
5634/// This encapsulates the stack of aggregates being compared at any
5635/// single point.
5636///
5637/// This is useful to detect "comparison cycles" and thus avoid the
5638/// resulting infinite loops.
5639///
5640/// This is also useful for implementing a very important optimization
5641/// that takes place during the canonicalization
5642struct offset_pairs_stack_type
5643{
5644 // The DWARF DWARF reader that is useful for so many things.
5645 const reader& rdr_;
5646 // The set of types that are being compared. This is to speed up
5647 // searches.
5649 // The stack of types that are being compared. The top of the
5650 // stack is the back of the vector.
5652 // A map that associates a redundant type pair to the vector of
5653 // types that depends on it.
5654 offset_pair_vect_map_type redundant_types_;
5655 // A map that associates a dependant type to the vector of redundant
5656 // types it depends on.
5657 offset_pair_vect_map_type dependant_types_;
5658
5659 offset_pairs_stack_type(const reader& rdr)
5660 : rdr_ (rdr)
5661 {}
5662
5663 /// Add a pair of types being compared to the stack of aggregates
5664 /// being compared.
5665 ///
5666 /// @param p the pair of offsets of the type DIEs to consider.
5667 void
5668 add(const offset_pair_type& p)
5669 {
5670 set_.insert(p);
5671 vect_.push_back(p);
5672 }
5673
5674 /// Erase a pair of types being compared from the stack of
5675 /// aggregates being compared.
5676 ///
5677 /// @param p the pair of offsets of the type DIEs to consider.
5678 ///
5679 /// @return true iff @p was found and erased from the stack.
5680 bool
5681 erase(const offset_pair_type& p)
5682 {
5683 if (set_.erase(p))
5684 {
5685 offset_pair_vector_type::iterator i;
5686
5687 for (i = vect_.begin();i < vect_.end(); ++i)
5688 if (*i == p)
5689 break;
5690
5691 if (i != vect_.end())
5692 vect_.erase(i);
5693
5694 return true;
5695 }
5696
5697 return false;
5698 }
5699
5700 /// Test if a pair of type DIEs is part of the stack of type DIEs
5701 /// being compared.
5702 ///
5703 /// @param p the pair of offsets of the type DIEs to consider.
5704 ///
5705 /// @return true iff @p was found in the stack of types being
5706 /// compared.
5707 bool
5708 contains(const offset_pair_type &p) const
5709 {
5710 if (set_.find(p) == set_.end())
5711 return false;
5712 return true;
5713 }
5714
5715 /// Get the set of comparison pair that depends on a given
5716 /// comparison pair.
5717 ///
5718 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5719 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5720 /// words, the pair T appears in the comparison stack BEFORE the
5721 /// pair P.
5722 ///
5723 /// So, this function returns the vector of comparison pairs that
5724 /// appear in the comparison stack AFTER a given comparison pair.
5725 ///
5726 /// @param p the comparison pair to consider.
5727 ///
5728 /// @param pairs out parameter. This is filled with the comparison
5729 /// pairs that depend on @p, iff the function returns true.
5730 ///
5731 /// @return true iff comparison pairs depending on @p have been
5732 /// found and collected in @pairs.
5733 bool
5734 get_pairs_that_depend_on(const offset_pair_type& p,
5735 offset_pair_vector_type& pairs) const
5736 {
5737 bool result = false;
5738 if (!contains(p))
5739 return result;
5740
5741 // First, get an iterator on the position of 'p'.
5742 offset_pair_vector_type::const_iterator i;
5743 for (i = vect_.begin(); i != vect_.end(); ++i)
5744 if (*i == p)
5745 break;
5746
5747 if (i == vect_.end())
5748 return result;
5749
5750 // Then, harvest all the comparison pairs that come after the
5751 // position of 'p'.
5752 for (++i; i != vect_.end(); ++i)
5753 {
5754 pairs.push_back(*i);
5755 result = true;
5756 }
5757
5758 return result;
5759 }
5760
5761 /// Record the fact that a set of comparison pairs depends on a
5762 /// given comparison pair.
5763 ///
5764 /// Set a map that associates each dependant comparison pair to the
5765 /// pair it depends on.
5766 ///
5767 /// @param p the comparison pair that the set depends on.
5768 ///
5769 /// @param dependant_types the set of types that depends on @p.
5770 void
5771 record_dependant_types(const offset_pair_type& p,
5772 const offset_pair_vector_type& dependant_types)
5773 {
5774 for (auto type_pair : dependant_types)
5775 dependant_types_[type_pair].push_back(p);
5776 }
5777
5778 /// Record a comparison pair as being redundant.
5779 ///
5780 ///
5781 /// @param p the comparison pair to record as redundant.
5782 void
5783 record_redundant_type_die_pair(const offset_pair_type& p)
5784 {
5785 offset_pair_vector_type dependant_types;
5786 get_pairs_that_depend_on(p, dependant_types);
5787
5788 // First, record the relationship "p -> [pairs that depend on p]".
5789 auto it = redundant_types_.find(p);
5790 if (it == redundant_types_.end())
5791 {
5792 auto entry = std::make_pair(p, dependant_types);
5793 redundant_types_.insert(entry);
5794 }
5795 else
5796 it->second.insert(it->second.end(),
5797 dependant_types.begin(),
5798 dependant_types.end());
5799
5800 // For each dependant type pair, record the association:
5801 // dependant_pair --> [vect of redundant types]
5802 record_dependant_types(p, dependant_types);
5803 }
5804
5805 /// Test if a given pair has been detected as redundant.
5806 ///
5807 /// @param p the pair of DIEs to consider.
5808 ///
5809 /// @return iff @p is redundant.
5810 bool
5811 is_redundant(const offset_pair_type& p)
5812 {
5813 auto i = redundant_types_.find(p);
5814 if (i != redundant_types_.end())
5815 return true;
5816 return false;
5817 }
5818
5819 /// Test if a given pair is dependant on at least a redundant type.
5820 ///
5821 /// @param p the pair to consider.
5822 ///
5823 /// @return true iff @p depends on a redundant type.
5824 bool
5825 depends_on_redundant_types(const offset_pair_type& p)
5826 {
5827 auto i = dependant_types_.find(p);
5828 if (i == dependant_types_.end())
5829 return false;
5830 return true;
5831 }
5832
5833 /// Remove a redundant pair from the system.
5834 ///
5835 /// This needs updating the system to also remove the dependant
5836 /// types that depend on the redundant pair (if they depend only on
5837 /// that redundant pair).
5838 ///
5839 /// @param p the pair to consider.
5840 ///
5841 /// @param erase_canonical_die_offset if true then erase the cached
5842 /// comparison results for the redundant pair and its dependant
5843 /// types.
5844 void
5845 erase_redundant_type_pair_entry(const offset_pair_type& p,
5846 bool erase_cached_results = false)
5847 {
5848 // First, update the dependant types that depend on the redundant
5849 // type pair
5850 auto redundant_type = redundant_types_.find(p);
5851 if (redundant_type != redundant_types_.end())
5852 {
5853 for (auto dependant_type : redundant_type->second)
5854 {
5855 // Each dependant_type depends on the redundant type 'p',
5856 // among others.
5857 auto dependant_types_it = dependant_types_.find(dependant_type);
5858 ABG_ASSERT(dependant_types_it != dependant_types_.end());
5859 // Erase the redundant type 'p' from the redundant types
5860 // that dependant_type depends on.
5861 {
5862 auto i = dependant_types_it->second.begin();
5863 for (; i!= dependant_types_it->second.end();++i)
5864 if (*i == p)
5865 break;
5866 if (i != dependant_types_it->second.end())
5867 dependant_types_it->second.erase(i);
5868 }
5869 // If the dependant type itself doesn't depend on ANY
5870 // redundant type anymore, then remove the depend type
5871 // from the map of the dependant types.
5872 if (dependant_types_it->second.empty())
5873 {
5874 if (erase_cached_results)
5875 rdr_.die_comparison_results_.erase(dependant_type);
5876 dependant_types_.erase(dependant_types_it);
5877 }
5878 }
5879 }
5880 if (erase_cached_results)
5881 rdr_.die_comparison_results_.erase(p);
5882 redundant_types_.erase(p);
5883 }
5884
5885 /// If a comparison pair has been detected as redundant, stop
5886 /// tracking it as well as its dependant pairs. That will
5887 /// essentially make it impossible to reset/cancel the canonical
5888 /// propagated types for those depdant pairs, but will also save
5889 /// ressources.
5890 ///
5891 /// @param p the comparison pair to consider.
5892 void
5893 confirm_canonical_propagated_type(const offset_pair_type& p)
5894 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5895
5896 /// Walk the types that depend on a comparison pair and cancel their
5897 /// canonical-propagate-type, that means remove their canonical
5898 /// types and mark them as not being canonically-propagated. Also,
5899 /// erase their cached comparison results that was likely set to
5900 /// COMPARISON_RESULT_UNKNOWN.
5901 ///
5902 /// @param p the pair to consider.
5903 void
5904 cancel_canonical_propagated_type(const offset_pair_type& p)
5905 {
5906 offset_pair_set_type dependant_types;
5907 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5908 for (auto dependant_type : dependant_types)
5909 {
5910 // If this dependant type was canonical-type-propagated then
5911 // erase that canonical type.
5912 if (rdr_.propagated_types_.find(dependant_type)
5913 != rdr_.propagated_types_.end())
5914 {
5915 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5916 dependant_type.first.source_,
5917 /*die_as_type=*/true);
5918 rdr_.propagated_types_.erase(dependant_type);
5919 rdr_.cancelled_propagation_count_++;
5920 }
5921 // Update the cached result. We know the comparison result
5922 // must now be different.
5923 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
5924 if (comp_result_it != rdr_.die_comparison_results_.end())
5925 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5926 }
5927
5928 // Update the cached result of the root type to cancel too.
5929 auto comp_result_it = rdr_.die_comparison_results_.find(p);
5930 if (comp_result_it != rdr_.die_comparison_results_.end())
5931 {
5932 // At this point, the result of p is either
5933 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
5934 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
5935 // Make sure it's the cached result is now
5936 // COMPARISON_RESULT_DIFFERENT.
5937 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
5938 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5939 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
5940 }
5941
5942 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5943 {
5944 rdr_.erase_canonical_die_offset(p.first.offset_,
5945 p.first.source_,
5946 /*die_as_type=*/true);
5947 rdr_.propagated_types_.erase(p);
5948 rdr_.cancelled_propagation_count_++;
5949 }
5950 }
5951
5952 /// Get the set of comparison pairs that depend on a given pair.
5953 ///
5954 /// @param p the pair to consider.
5955 ///
5956 /// @param result this is set to the pairs that depend on @p, iff
5957 /// the function returned true.
5958 ///
5959 /// @param transitive_closure if set to true, the transitive closure
5960 /// of the @result is set to it.
5961 ///
5962 /// @return true iff @result could be filled with the dependant
5963 /// types.
5964 bool
5965 get_dependant_types(const offset_pair_type& p,
5966 offset_pair_set_type& result,
5967 bool transitive_closure = false)
5968 {
5969 auto i = redundant_types_.find(p);
5970 if (i != redundant_types_.end())
5971 {
5972 for (auto dependant_type : i->second)
5973 if (result.find(dependant_type) == result.end())
5974 {
5975 result.insert(dependant_type);
5976 if (transitive_closure)
5977 get_dependant_types(p, result, /*transitive_closure=*/true);
5978 }
5979 return true;
5980 }
5981 return false;
5982 }
5983}; // end struct offset_pairs_stack_type
5984
5986build_ir_node_from_die(reader& rdr,
5987 Dwarf_Die* die,
5988 scope_decl* scope,
5989 bool called_from_public_decl,
5990 size_t where_offset,
5991 bool is_declaration_only = true,
5992 bool is_required_decl_spec = false);
5993
5995build_ir_node_from_die(reader& rdr,
5996 Dwarf_Die* die,
5997 bool called_from_public_decl,
5998 size_t where_offset);
5999
6000static decl_base_sptr
6001build_ir_node_for_void_type(reader& rdr);
6002
6004build_ir_node_for_void_pointer_type(reader& rdr);
6005
6006static class_decl_sptr
6007add_or_update_class_type(reader& rdr,
6008 Dwarf_Die* die,
6009 scope_decl* scope,
6010 bool is_struct,
6011 class_decl_sptr klass,
6012 bool called_from_public_decl,
6013 size_t where_offset,
6014 bool is_declaration_only);
6015
6016static union_decl_sptr
6017add_or_update_union_type(reader& rdr,
6018 Dwarf_Die* die,
6019 scope_decl* scope,
6020 union_decl_sptr union_type,
6021 bool called_from_public_decl,
6022 size_t where_offset,
6023 bool is_declaration_only);
6024
6025static decl_base_sptr
6026build_ir_node_for_void_type(reader& rdr);
6027
6028static decl_base_sptr
6029build_ir_node_for_variadic_parameter_type(reader &rdr);
6030
6031static function_decl_sptr
6032build_function_decl(reader& rdr,
6033 Dwarf_Die* die,
6034 size_t where_offset,
6036
6037static bool
6038function_is_suppressed(const reader& rdr,
6039 const scope_decl* scope,
6040 Dwarf_Die *function_die,
6041 bool is_declaration_only);
6042
6043static function_decl_sptr
6044build_or_get_fn_decl_if_not_suppressed(reader& rdr,
6045 scope_decl *scope,
6046 Dwarf_Die *die,
6047 size_t where_offset,
6048 bool is_declaration_only,
6050
6051static var_decl_sptr
6052build_var_decl(reader& rdr,
6053 Dwarf_Die *die,
6054 size_t where_offset,
6055 var_decl_sptr result = var_decl_sptr());
6056
6057static var_decl_sptr
6058build_or_get_var_decl_if_not_suppressed(reader& rdr,
6059 scope_decl *scope,
6060 Dwarf_Die *die,
6061 size_t where_offset,
6062 bool is_declaration_only,
6064 bool is_required_decl_spec = false);
6065static bool
6066variable_is_suppressed(const reader& rdr,
6067 const scope_decl* scope,
6068 Dwarf_Die *variable_die,
6069 bool is_declaration_only,
6070 bool is_required_decl_spec = false);
6071
6072static void
6073finish_member_function_reading(Dwarf_Die* die,
6074 const function_decl_sptr& f,
6075 const class_or_union_sptr klass,
6076 reader& rdr);
6077
6078/// Test if a given DIE is anonymous
6079///
6080/// @param die the DIE to consider.
6081///
6082/// @return true iff @p die is anonymous.
6083static bool
6084die_is_anonymous(const Dwarf_Die* die)
6085{
6086 Dwarf_Attribute attr;
6087 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
6088 return true;
6089 return false;
6090}
6091
6092/// Test if a DIE is an anonymous data member, aka, "unnamed field".
6093///
6094/// Unnamed fields are specified at
6095/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
6096///
6097/// @param die the DIE to consider.
6098///
6099/// @return true iff @p die is an anonymous data member.
6100static bool
6101die_is_anonymous_data_member(const Dwarf_Die* die)
6102{
6103 if (!die
6104 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
6105 || !die_name(die).empty())
6106 return false;
6107
6108 Dwarf_Die type_die;
6109 if (!die_die_attribute(die, DW_AT_type, type_die))
6110 return false;
6111
6112 if (dwarf_tag(&type_die) != DW_TAG_structure_type
6113 && dwarf_tag(&type_die) != DW_TAG_union_type)
6114 return false;
6115
6116 return true;
6117}
6118
6119/// Get the value of an attribute that is supposed to be a string, or
6120/// an empty string if the attribute could not be found.
6121///
6122/// @param die the DIE to get the attribute value from.
6123///
6124/// @param attr_name the attribute name. Must come from dwarf.h and
6125/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6126///
6127/// @return the string representing the value of the attribute, or an
6128/// empty string if no string attribute could be found.
6129static string
6130die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
6131{
6132 if (!die)
6133 return "";
6134
6135 Dwarf_Attribute attr;
6136 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6137 return "";
6138
6139 const char* str = dwarf_formstring(&attr);
6140 return str ? str : "";
6141}
6142
6143/// Get the value of an attribute that is supposed to be a string, or
6144/// an empty string if the attribute could not be found.
6145///
6146/// @param die the DIE to get the attribute value from.
6147///
6148/// @param attr_name the attribute name. Must come from dwarf.h and
6149/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6150///
6151/// @return the char* representing the value of the attribute, or an
6152/// empty string if no string attribute could be found.
6153static const char*
6154die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
6155{
6156 if (!die)
6157 return nullptr;
6158
6159 Dwarf_Attribute attr;
6160 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6161 return nullptr;
6162
6163 const char* str = dwarf_formstring(&attr);
6164 return str;
6165}
6166
6167/// Get the value of an attribute that is supposed to be an unsigned
6168/// constant.
6169///
6170/// @param die the DIE to read the information from.
6171///
6172/// @param attr_name the DW_AT_* name of the attribute. Must come
6173/// from dwarf.h and be an enumerator representing an attribute like,
6174/// e.g, DW_AT_decl_line.
6175///
6176///@param cst the output parameter that is set to the value of the
6177/// attribute @p attr_name. This parameter is set iff the function
6178/// return true.
6179///
6180/// @return true if there was an attribute of the name @p attr_name
6181/// and with a value that is a constant, false otherwise.
6182static bool
6183die_unsigned_constant_attribute(const Dwarf_Die* die,
6184 unsigned attr_name,
6185 uint64_t& cst)
6186{
6187 if (!die)
6188 return false;
6189
6190 Dwarf_Attribute attr;
6191 Dwarf_Word result = 0;
6192 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6193 || dwarf_formudata(&attr, &result))
6194 return false;
6195
6196 cst = result;
6197 return true;
6198}
6199
6200/// Read a signed constant value from a given attribute.
6201///
6202/// The signed constant expected must be of constant form.
6203///
6204/// @param die the DIE to get the attribute from.
6205///
6206/// @param attr_name the attribute name.
6207///
6208/// @param cst the resulting signed constant read.
6209///
6210/// @return true iff a signed constant attribute of the name @p
6211/// attr_name was found on the DIE @p die.
6212static bool
6213die_signed_constant_attribute(const Dwarf_Die *die,
6214 unsigned attr_name,
6215 int64_t& cst)
6216{
6217 if (!die)
6218 return false;
6219
6220 Dwarf_Attribute attr;
6221 Dwarf_Sword result = 0;
6222 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6223 || dwarf_formsdata(&attr, &result))
6224 return false;
6225
6226 cst = result;
6227 return true;
6228}
6229
6230/// Read the value of a constant attribute that is either signed or
6231/// unsigned into a array_type_def::subrange_type::bound_value value.
6232///
6233/// The bound_value instance will capture the actual signedness of the
6234/// read attribute.
6235///
6236/// @param die the DIE from which to read the value of the attribute.
6237///
6238/// @param attr_name the attribute name to consider.
6239///
6240/// @param is_signed true if the attribute value has to read as
6241/// signed.
6242///
6243/// @param value the resulting value read from attribute @p attr_name
6244/// on DIE @p die.
6245///
6246/// @return true iff DIE @p die has an attribute named @p attr_name
6247/// with a constant value.
6248static bool
6249die_constant_attribute(const Dwarf_Die *die,
6250 unsigned attr_name,
6251 bool is_signed,
6252 array_type_def::subrange_type::bound_value &value)
6253{
6254 if (!is_signed)
6255 {
6256 uint64_t l = 0;
6257 if (!die_unsigned_constant_attribute(die, attr_name, l))
6258 return false;
6259 value.set_unsigned(l);
6260 }
6261 else
6262 {
6263 int64_t l = 0;
6264 if (!die_signed_constant_attribute(die, attr_name, l))
6265 return false;
6266 value.set_signed(l);
6267 }
6268 return true;
6269}
6270
6271/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6272///
6273/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6274/// enum in dwarf.h so we have to use an unsigned int for the form,
6275/// grrr.
6276///
6277/// @param form the form to consider.
6278///
6279/// @return true iff @p form is DW_FORM_strx{1,4}.
6280static bool
6281form_is_DW_FORM_strx(unsigned form)
6282{
6283 if (form)
6284 {
6285#if defined HAVE_DW_FORM_strx1 \
6286 && defined HAVE_DW_FORM_strx2 \
6287 && defined HAVE_DW_FORM_strx3 \
6288 && defined HAVE_DW_FORM_strx4
6289 if (form == DW_FORM_strx1
6290 || form == DW_FORM_strx2
6291 || form == DW_FORM_strx3
6292 ||form == DW_FORM_strx4)
6293 return true;
6294#endif
6295 }
6296 return false;
6297}
6298
6299/// Test if a given DWARF form is DW_FORM_line_strp.
6300///
6301/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6302/// enum in dwarf.h so we have to use an unsigned int for the form,
6303/// grrr.
6304///
6305/// @param form the form to consider.
6306///
6307/// @return true iff @p form is DW_FORM_line_strp.
6308static bool
6309form_is_DW_FORM_line_strp(unsigned form)
6310{
6311 if (form)
6312 {
6313#if defined HAVE_DW_FORM_line_strp
6314 if (form == DW_FORM_line_strp)
6315 return true;
6316#endif
6317 }
6318 return false;
6319}
6320
6321/// Get the value of a DIE attribute; that value is meant to be a
6322/// flag.
6323///
6324/// @param die the DIE to get the attribute from.
6325///
6326/// @param attr_name the DW_AT_* name of the attribute. Must come
6327/// from dwarf.h and be an enumerator representing an attribute like,
6328/// e.g, DW_AT_external.
6329///
6330/// @param flag the output parameter to store the flag value into.
6331/// This is set iff the function returns true.
6332///
6333/// @param recursively if true, the function looks through the
6334/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6335/// all the way down to the initial DIE that is cloned and look on
6336/// that DIE to see if it has the @p attr_name attribute.
6337///
6338/// @return true if the DIE has a flag attribute named @p attr_name,
6339/// false otherwise.
6340static bool
6341die_flag_attribute(const Dwarf_Die* die,
6342 unsigned attr_name,
6343 bool& flag,
6344 bool recursively = true)
6345{
6346 Dwarf_Attribute attr;
6347 if (recursively
6348 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6349 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6350 return false;
6351
6352 bool f = false;
6353 if (dwarf_formflag(&attr, &f))
6354 return false;
6355
6356 flag = f;
6357 return true;
6358}
6359
6360/// Get the mangled name from a given DIE.
6361///
6362/// @param die the DIE to read the mangled name from.
6363///
6364/// @return the mangled name if it's present in the DIE, or just an
6365/// empty string if it's not.
6366static string
6367die_linkage_name(const Dwarf_Die* die)
6368{
6369 if (!die)
6370 return "";
6371
6372 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6373 if (linkage_name.empty())
6374 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6375 return linkage_name;
6376}
6377
6378/// Get the file path that is the value of the DW_AT_decl_file
6379/// attribute on a given DIE, if the DIE is a decl DIE having that
6380/// attribute.
6381///
6382/// @param die the DIE to consider.
6383///
6384/// @return a string containing the file path that is the logical
6385/// value of the DW_AT_decl_file attribute. If the DIE @p die
6386/// doesn't have a DW_AT_decl_file attribute, then the return value is
6387/// just an empty string.
6388static string
6389die_decl_file_attribute(const Dwarf_Die* die)
6390{
6391 if (!die)
6392 return "";
6393
6394 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6395
6396 return str ? str : "";
6397}
6398
6399/// Get the value of an attribute which value is supposed to be a
6400/// reference to a DIE.
6401///
6402/// @param die the DIE to read the value from.
6403///
6404/// @param attr_name the DW_AT_* attribute name to read.
6405///
6406/// @param result the DIE resulting from reading the attribute value.
6407/// This is set iff the function returns true.
6408///
6409/// @param recursively if true, the function looks through the
6410/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6411/// all the way down to the initial DIE that is cloned and look on
6412/// that DIE to see if it has the @p attr_name attribute.
6413///
6414/// @return true if the DIE @p die contains an attribute named @p
6415/// attr_name that is a DIE reference, false otherwise.
6416static bool
6417die_die_attribute(const Dwarf_Die* die,
6418 unsigned attr_name,
6419 Dwarf_Die& result,
6420 bool recursively)
6421{
6422 Dwarf_Attribute attr;
6423 if (recursively
6424 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6425 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6426 return false;
6427
6428 return dwarf_formref_die(&attr, &result);
6429}
6430
6431/// Get the DIE that is the "origin" of the current one.
6432///
6433/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6434/// attribute. Those DIEs represent a concrete instance of an
6435/// abstract entity. The concrete instance can be a concrete instance
6436/// of an inline function, or the concrete implementation of an
6437/// abstract interface. On both cases, we call the abstract instance
6438/// from which the concrete instance derives the "origin".
6439///
6440/// This function returns the ultimate origin DIE of a given DIE by
6441/// following the chain of its DW_AT_abstract_origin and
6442/// DW_AT_specification attributes.
6443///
6444/// @param die the DIE to consider.
6445///
6446/// @param origin_die this is an output parameter that is set by this
6447/// function to the resulting origin DIE iff the function returns
6448/// true.
6449///
6450/// @return true iff the function actually found an origin DIE and
6451/// set it to the @p origin_die parameter.
6452static bool
6453die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6454{
6455 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6456 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6457 {
6458 while (die_die_attribute(&origin_die,
6459 DW_AT_specification,
6460 origin_die, true)
6461 || die_die_attribute(&origin_die,
6462 DW_AT_abstract_origin,
6463 origin_die, true))
6464 {
6465 // Keep looking for the origin die ...
6466 ;
6467 }
6468 return true;
6469 }
6470 return false;
6471}
6472
6473/// Test if a subrange DIE indirectly references another subrange DIE
6474/// through a given attribute.
6475///
6476/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6477/// attribute be a reference to either a data member or a variable
6478/// which type is itself a DW_TAG_subrange_type. This latter subrange
6479/// DIE is said to be "indirectly referenced" by the former subrange
6480/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6481/// the value we want for the DW_AT_upper_bound of the former.
6482///
6483/// This function tests if the former subrange DIE does indirectly
6484/// reference another subrange DIE through a given attribute (not
6485/// necessarily DW_AT_upper_bound).
6486///
6487/// @param die the DIE to consider. Note that It must be a
6488/// DW_TAG_subrange_type.
6489///
6490/// @param attr_name the name of the attribute to look through for the
6491/// indirectly referenced subrange DIE.
6492///
6493/// @param referenced_subrange if the function returns true, then the
6494/// argument of this parameter is set to the indirectly referenced
6495/// DW_TAG_subrange_type DIE.
6496///
6497/// @return true iff @p DIE indirectly references a subrange DIE
6498/// through the attribute @p attr_name.
6499static bool
6500subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6501 unsigned attr_name,
6502 Dwarf_Die& referenced_subrange)
6503{
6504 bool result = false;
6505
6506 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6507 return result;
6508
6509 Dwarf_Die referenced_die;
6510 if (die_die_attribute(die, attr_name, referenced_die))
6511 {
6512 unsigned tag = dwarf_tag(&referenced_die);
6513 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6514 {
6515 Dwarf_Die type_die;
6516 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6517 {
6518 tag = dwarf_tag(&type_die);
6519 if (tag == DW_TAG_subrange_type)
6520 {
6521 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6522 result = true;
6523 }
6524 }
6525 }
6526 }
6527 return result;
6528}
6529
6530/// Return the bound value of subrange die by looking at an indirectly
6531/// referenced subrange DIE.
6532///
6533/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6534/// attribute be a reference to either a data member or a variable
6535/// which type is itself a DW_TAG_subrange_type. This latter subrange
6536/// DIE is said to be "indirectly referenced" by the former subrange
6537/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6538/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6539///
6540/// This function gets the DW_AT_{lower,upper}_bound value of a
6541/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6542/// the indirectly referenced subrange type, if it exists.
6543///
6544/// @param die the subrange DIE to consider.
6545///
6546/// @param attr_name the name of the attribute to consider, typically,
6547/// DW_AT_{lower,upper}_bound.
6548///
6549/// @param v the found value, iff this function returned true.
6550///
6551/// @param is_signed, this is set to true if @p v is signed. This
6552/// parameter is set at all only if the function returns true.
6553///
6554/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6555/// indirectly referenced subrange type.
6556static bool
6557subrange_die_indirect_bound_value(const Dwarf_Die *die,
6558 unsigned attr_name,
6559 array_type_def::subrange_type::bound_value& v,
6560 bool& is_signed)
6561{
6562 bool result = false;
6563
6564 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6565 return result;
6566
6567 Dwarf_Die subrange_die;
6568 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6569 subrange_die))
6570 {
6571 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6572 result = true;
6573 }
6574 return result;
6575}
6576
6577/// Read and return an addresss class attribute from a given DIE.
6578///
6579/// @param die the DIE to consider.
6580///
6581/// @param attr_name the name of the address class attribute to read
6582/// the value from.
6583///
6584/// @param the resulting address.
6585///
6586/// @return true iff the attribute could be read, was of the expected
6587/// address class and could thus be translated into the @p result.
6588static bool
6589die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6590{
6591 Dwarf_Attribute attr;
6592 if (!dwarf_attr_integrate(die, attr_name, &attr))
6593 return false;
6594 return dwarf_formaddr(&attr, &result) == 0;
6595}
6596
6597/// Returns the source location associated with a decl DIE.
6598///
6599/// @param rdr the @ref reader to use.
6600///
6601/// @param die the DIE the read the source location from.
6602///
6603/// @return the location associated with @p die.
6604static location
6605die_location(const reader& rdr, const Dwarf_Die* die)
6606{
6607 if (!die)
6608 return location();
6609
6610 string file = die_decl_file_attribute(die);
6611 uint64_t line = 0;
6612 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6613
6614 if (!file.empty() && line != 0)
6615 {
6616 translation_unit_sptr tu = rdr.cur_transl_unit();
6617 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6618 return l;
6619 }
6620 return location();
6621}
6622
6623/// Return a copy of the name of a DIE.
6624///
6625/// @param die the DIE to consider.
6626///
6627/// @return a copy of the name of the DIE.
6628static string
6629die_name(const Dwarf_Die* die)
6630{
6631 string name = die_string_attribute(die, DW_AT_name);
6632 return name;
6633}
6634
6635/// Return the location, the name and the mangled name of a given DIE.
6636///
6637/// @param rdr the DWARF reader to use.
6638///
6639/// @param die the DIE to read location and names from.
6640///
6641/// @param loc the location output parameter to set.
6642///
6643/// @param name the name output parameter to set.
6644///
6645/// @param linkage_name the linkage_name output parameter to set.
6646static void
6647die_loc_and_name(const reader& rdr,
6648 Dwarf_Die* die,
6649 location& loc,
6650 string& name,
6651 string& linkage_name)
6652{
6653 loc = die_location(rdr, die);
6654 name = die_name(die);
6655 linkage_name = die_linkage_name(die);
6656}
6657
6658/// Return the name and the mangled name of a given DIE.
6659///
6660/// @param die the DIE to read location and names from.
6661///
6662/// @param name the name output parameter to set.
6663///
6664/// @param linkage_name the linkage_name output parameter to set.
6665static void
6666die_name_and_linkage_name(const Dwarf_Die* die,
6667 string& name,
6668 string& linkage_name)
6669{
6670 name = die_name(die);
6671 linkage_name = die_linkage_name(die);
6672}
6673
6674/// Get the size of a (type) DIE as the value for the parameter
6675/// DW_AT_byte_size or DW_AT_bit_size.
6676///
6677/// @param die the DIE to read the information from.
6678///
6679/// @param size the resulting size in bits. This is set iff the
6680/// function return true.
6681///
6682/// @return true if the size attribute was found.
6683static bool
6684die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6685{
6686 if (!die)
6687 return false;
6688
6689 uint64_t byte_size = 0, bit_size = 0;
6690
6691 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6692 {
6693 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6694 return false;
6695 }
6696 else
6697 bit_size = byte_size * 8;
6698
6699 size = bit_size;
6700
6701 return true;
6702}
6703
6704/// Get the access specifier (from the DW_AT_accessibility attribute
6705/// value) of a given DIE.
6706///
6707/// @param die the DIE to consider.
6708///
6709/// @param access the resulting access. This is set iff the function
6710/// returns true.
6711///
6712/// @return bool if the DIE contains the DW_AT_accessibility die.
6713static bool
6714die_access_specifier(Dwarf_Die * die, access_specifier& access)
6715{
6716 if (!die)
6717 return false;
6718
6719 uint64_t a = 0;
6720 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6721 return false;
6722
6723 access_specifier result = private_access;
6724
6725 switch (a)
6726 {
6727 case private_access:
6728 result = private_access;
6729 break;
6730
6731 case protected_access:
6732 result = protected_access;
6733 break;
6734
6735 case public_access:
6736 result = public_access;
6737 break;
6738
6739 default:
6740 break;
6741 }
6742
6743 access = result;
6744 return true;
6745}
6746
6747/// Test whether a given DIE represents a decl that is public. That
6748/// is, one with the DW_AT_external attribute set.
6749///
6750/// @param die the DIE to consider for testing.
6751///
6752/// @return true if a DW_AT_external attribute is present and its
6753/// value is set to the true; return false otherwise.
6754static bool
6755die_is_public_decl(const Dwarf_Die* die)
6756{
6757 if (!die)
6758 return false;
6759 bool is_public = false;
6760
6761 // If this is a DW_TAG_subprogram DIE, look for the
6762 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6763 // then it's public. In all other cases, this should return false.
6764
6765 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6766 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6767 die_flag_attribute(die, DW_AT_external, is_public);
6768 else if (tag == DW_TAG_namespace)
6769 {
6770 string name = die_name(die);
6771 is_public = !name.empty();
6772 }
6773
6774 return is_public;
6775}
6776
6777/// Test if a DIE is effectively public.
6778///
6779/// This is meant to return true when either the DIE is public or when
6780/// it's a variable DIE that is at (global) namespace level.
6781///
6782/// @return true iff either the DIE is public or is a variable DIE
6783/// that is at (global) namespace level.
6784static bool
6785die_is_effectively_public_decl(const reader& rdr,
6786 const Dwarf_Die* die)
6787{
6788 if (die_is_public_decl(die))
6789 return true;
6790
6791 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6792 if (tag == DW_TAG_variable || tag == DW_TAG_member)
6793 {
6794 // The DIE is a variable.
6795 Dwarf_Die parent_die;
6796 size_t where_offset = 0;
6797 if (!get_parent_die(rdr, die, parent_die, where_offset))
6798 return false;
6799
6800 tag = dwarf_tag(&parent_die);
6801 if (tag == DW_TAG_compile_unit
6802 || tag == DW_TAG_partial_unit
6803 || tag == DW_TAG_type_unit)
6804 // The DIE is at global scope.
6805 return true;
6806
6807 if (tag == DW_TAG_namespace)
6808 {
6809 string name = die_name(&parent_die);
6810 if (name.empty())
6811 // The DIE at unnamed namespace scope, so it's not public.
6812 return false;
6813 // The DIE is at namespace scope.
6814 return true;
6815 }
6816 }
6817 return false;
6818}
6819
6820/// Test whether a given DIE represents a declaration-only DIE.
6821///
6822/// That is, if the DIE has the DW_AT_declaration flag set.
6823///
6824/// @param die the DIE to consider.
6825//
6826/// @return true if a DW_AT_declaration is present, false otherwise.
6827static bool
6828die_is_declaration_only(Dwarf_Die* die)
6829{
6830 bool is_declaration = false;
6831 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6832 if (is_declaration && !die_has_size_attribute(die))
6833 return true;
6834 return false;
6835}
6836
6837/// Test if a DIE is for a function decl.
6838///
6839/// @param die the DIE to consider.
6840///
6841/// @return true iff @p die represents a function decl.
6842static bool
6843die_is_function_decl(const Dwarf_Die *die)
6844{
6845 if (!die)
6846 return false;
6847
6848 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6849 if (tag == DW_TAG_subprogram)
6850 return true;
6851 return false;
6852}
6853
6854/// Test if a DIE is for a variable decl.
6855///
6856/// @param die the DIE to consider.
6857///
6858/// @return true iff @p die represents a variable decl.
6859static bool
6860die_is_variable_decl(const Dwarf_Die *die)
6861{
6862 if (!die)
6863 return false;
6864
6865 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6866 if (tag == DW_TAG_variable)
6867 return true;
6868 return false;
6869}
6870
6871/// Test if a DIE has size attribute.
6872///
6873/// @param die the DIE to consider.
6874///
6875/// @return true if the DIE has a size attribute.
6876static bool
6877die_has_size_attribute(const Dwarf_Die *die)
6878{
6879 uint64_t s;
6880 if (die_size_in_bits(die, s))
6881 return true;
6882 return false;
6883}
6884
6885/// Test that a DIE has no child DIE.
6886///
6887/// @param die the DIE to consider.
6888///
6889/// @return true iff @p die has no child DIE.
6890static bool
6891die_has_no_child(const Dwarf_Die *die)
6892{
6893 if (!die)
6894 return true;
6895
6896 Dwarf_Die child;
6897 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6898 return false;
6899 return true;
6900}
6901
6902/// Test whether a given DIE represents a declaration-only DIE.
6903///
6904/// That is, if the DIE has the DW_AT_declaration flag set.
6905///
6906/// @param die the DIE to consider.
6907//
6908/// @return true if a DW_AT_declaration is present, false otherwise.
6909static bool
6910die_is_declaration_only(const Dwarf_Die* die)
6911{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6912
6913/// Tests whether a given DIE is artificial.
6914///
6915/// @param die the test to test for.
6916///
6917/// @return true if the DIE is artificial, false otherwise.
6918static bool
6919die_is_artificial(Dwarf_Die* die)
6920{
6921 bool is_artificial;
6922 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6923}
6924
6925///@return true if a tag represents a type, false otherwise.
6926///
6927///@param tag the tag to consider.
6928static bool
6929is_type_tag(unsigned tag)
6930{
6931 bool result = false;
6932
6933 switch (tag)
6934 {
6935 case DW_TAG_array_type:
6936 case DW_TAG_class_type:
6937 case DW_TAG_enumeration_type:
6938 case DW_TAG_pointer_type:
6939 case DW_TAG_reference_type:
6940 case DW_TAG_string_type:
6941 case DW_TAG_structure_type:
6942 case DW_TAG_subroutine_type:
6943 case DW_TAG_typedef:
6944 case DW_TAG_union_type:
6945 case DW_TAG_ptr_to_member_type:
6946 case DW_TAG_set_type:
6947 case DW_TAG_subrange_type:
6948 case DW_TAG_base_type:
6949 case DW_TAG_const_type:
6950 case DW_TAG_file_type:
6951 case DW_TAG_packed_type:
6952 case DW_TAG_thrown_type:
6953 case DW_TAG_volatile_type:
6954 case DW_TAG_restrict_type:
6955 case DW_TAG_interface_type:
6956 case DW_TAG_unspecified_type:
6957 case DW_TAG_shared_type:
6958 case DW_TAG_rvalue_reference_type:
6959 case DW_TAG_coarray_type:
6960 case DW_TAG_atomic_type:
6961 case DW_TAG_immutable_type:
6962 result = true;
6963 break;
6964
6965 default:
6966 result = false;
6967 break;
6968 }
6969
6970 return result;
6971}
6972
6973/// Test if a given DIE is a type whose canonical type is to be
6974/// propagated during DIE canonicalization
6975///
6976/// This is a sub-routine of compare_dies.
6977///
6978/// @param tag the tag of the DIE to consider.
6979///
6980/// @return true iff the DIE of tag @p tag is can see its canonical
6981/// type be propagated during the type comparison that happens during
6982/// DIE canonicalization.
6983static bool
6984is_canon_type_to_be_propagated_tag(unsigned tag)
6985{
6986 bool result = false;
6987
6988 switch (tag)
6989 {
6990 case DW_TAG_class_type:
6991 case DW_TAG_structure_type:
6992 case DW_TAG_union_type:
6993 case DW_TAG_subroutine_type:
6994 case DW_TAG_subprogram:
6995 result = true;
6996 break;
6997
6998 default:
6999 result = false;
7000 break;
7001 }
7002
7003 return result;
7004}
7005
7006/// Test if a given kind of DIE ought to have its comparison result
7007/// cached by compare_dies, so that subsequent invocations of
7008/// compare_dies can be faster.
7009///
7010/// @param tag the tag of the DIE to consider.
7011///
7012/// @return true iff DIEs of the tag @p tag ought to have its
7013/// comparison results cached.
7014static bool
7015type_comparison_result_to_be_cached(unsigned tag)
7016{
7017 bool r = false;
7018 switch (tag)
7019 {
7020 case DW_TAG_class_type:
7021 case DW_TAG_structure_type:
7022 case DW_TAG_union_type:
7023 case DW_TAG_subroutine_type:
7024 case DW_TAG_subprogram:
7025 r = true;
7026 break;
7027
7028 default:
7029 r = false;
7030 break;
7031 }
7032 return r;
7033}
7034
7035/// Cache the result of comparing to type DIEs.
7036///
7037/// @param rdr the context to consider.
7038///
7039/// @param tag the tag of the DIEs to consider.
7040///
7041/// @param p the offsets of the pair of DIEs being compared.
7042///
7043/// @param result the comparison result to be cached.
7044static bool
7045maybe_cache_type_comparison_result(const reader& rdr,
7046 int tag,
7047 const offset_pair_type& p,
7048 comparison_result result)
7049{
7050 if (!type_comparison_result_to_be_cached(tag)
7051 || (result != COMPARISON_RESULT_EQUAL
7052 && result != COMPARISON_RESULT_DIFFERENT))
7053 return false;
7054
7055 rdr.die_comparison_results_[p] = result;
7056
7057 return true;
7058
7059}
7060
7061/// Get the cached result of the comparison of a pair of DIEs.
7062///
7063/// @param rdr the context to consider.
7064///
7065/// @param tag the tag of the pair of DIEs to consider.
7066///
7067/// @param p the offsets of the pair of DIEs to consider.
7068///
7069/// @param result out parameter set to the cached result of the
7070/// comparison of @p p if it has been found.
7071///
7072/// @return true iff a cached result for the comparisonof @p has been
7073/// found and set into @p result.
7074static bool
7075get_cached_type_comparison_result(const reader& rdr,
7076 const offset_pair_type& p,
7077 comparison_result& result)
7078{
7079 auto i = rdr.die_comparison_results_.find(p);
7080 if (i != rdr.die_comparison_results_.end())
7081 {
7082 result = i->second;
7083 return true;
7084 }
7085 return false;
7086}
7087
7088/// Get the cached result of the comparison of a pair of DIEs, if the
7089/// kind of DIEs ought to have its comparison results cached.
7090///
7091/// @param rdr the context to consider.
7092///
7093/// @param tag the tag of the pair of DIEs to consider.
7094///
7095/// @param p the offsets of the pair of DIEs to consider.
7096///
7097/// @param result out parameter set to the cached result of the
7098/// comparison of @p p if it has been found.
7099///
7100/// @return true iff a cached result for the comparisonof @p has been
7101/// found and set into @p result.
7102static bool
7103maybe_get_cached_type_comparison_result(const reader& rdr,
7104 int tag,
7105 const offset_pair_type& p,
7106 comparison_result& result)
7107{
7108 if (type_comparison_result_to_be_cached(tag))
7109 {
7110 // Types of this kind might have their comparison result cached
7111 // when they are not canonicalized. So let's see if we have a
7112 // cached comparison result.
7113 if (get_cached_type_comparison_result(rdr, p, result))
7114 return true;
7115 }
7116 return false;
7117}
7118
7119/// Test if a given DIE is to be canonicalized.
7120///
7121/// @param die the DIE to consider.
7122///
7123/// @return true iff @p die is to be canonicalized.
7124static bool
7125is_type_die_to_be_canonicalized(const Dwarf_Die *die)
7126{
7127 bool result = false;
7128 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7129
7130 if (!is_type_tag(tag))
7131 return false;
7132
7133 switch (tag)
7134 {
7135 case DW_TAG_class_type:
7136 case DW_TAG_structure_type:
7137 case DW_TAG_union_type:
7138 result = !die_is_declaration_only(die);
7139 break;
7140
7141 case DW_TAG_subroutine_type:
7142 case DW_TAG_subprogram:
7143 case DW_TAG_array_type:
7144 result = true;
7145
7146 default:
7147 break;
7148 }
7149
7150 return result;
7151}
7152
7153/// Test if a DIE tag represents a declaration.
7154///
7155/// @param tag the DWARF tag to consider.
7156///
7157/// @return true iff @p tag is for a declaration.
7158static bool
7159is_decl_tag(unsigned tag)
7160{
7161 switch (tag)
7162 {
7163 case DW_TAG_formal_parameter:
7164 case DW_TAG_imported_declaration:
7165 case DW_TAG_member:
7166 case DW_TAG_unspecified_parameters:
7167 case DW_TAG_subprogram:
7168 case DW_TAG_variable:
7169 case DW_TAG_namespace:
7170 case DW_TAG_GNU_template_template_param:
7171 case DW_TAG_GNU_template_parameter_pack:
7172 case DW_TAG_GNU_formal_parameter_pack:
7173 return true;
7174 }
7175 return false;
7176}
7177
7178/// Test if a DIE represents a type DIE.
7179///
7180/// @param die the DIE to consider.
7181///
7182/// @return true if @p die represents a type, false otherwise.
7183static bool
7184die_is_type(const Dwarf_Die* die)
7185{
7186 if (!die)
7187 return false;
7188 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7189}
7190
7191/// Test if a DIE represents a declaration.
7192///
7193/// @param die the DIE to consider.
7194///
7195/// @return true if @p die represents a decl, false otherwise.
7196static bool
7197die_is_decl(const Dwarf_Die* die)
7198{
7199 if (!die)
7200 return false;
7201 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7202}
7203
7204/// Test if a DIE represents a namespace.
7205///
7206/// @param die the DIE to consider.
7207///
7208/// @return true if @p die represents a namespace, false otherwise.
7209static bool
7210die_is_namespace(const Dwarf_Die* die)
7211{
7212 if (!die)
7213 return false;
7214 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7215}
7216
7217/// Test if a DIE has tag DW_TAG_unspecified_type.
7218///
7219/// @param die the DIE to consider.
7220///
7221/// @return true if @p die has tag DW_TAG_unspecified_type.
7222static bool
7223die_is_unspecified(Dwarf_Die* die)
7224{
7225 if (!die)
7226 return false;
7227 return (dwarf_tag(die) == DW_TAG_unspecified_type);
7228}
7229
7230/// Test if a DIE represents a void type.
7231///
7232/// @param die the DIE to consider.
7233///
7234/// @return true if @p die represents a void type, false otherwise.
7235static bool
7236die_is_void_type(Dwarf_Die* die)
7237{
7238 if (!die || dwarf_tag(die) != DW_TAG_base_type)
7239 return false;
7240
7241 string name = die_name(die);
7242 if (name == "void")
7243 return true;
7244
7245 return false;
7246}
7247
7248/// Test if a DIE represents a pointer type.
7249///
7250/// @param die the die to consider.
7251///
7252/// @return true iff @p die represents a pointer type.
7253static bool
7254die_is_pointer_type(const Dwarf_Die* die)
7255{
7256 if (!die)
7257 return false;
7258
7259 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7260 if (tag == DW_TAG_pointer_type)
7261 return true;
7262
7263 return false;
7264}
7265
7266/// Test if a DIE is for a pointer, reference or qualified type to
7267/// anonymous class or struct.
7268///
7269/// @param die the DIE to consider.
7270///
7271/// @return true iff @p is for a pointer, reference or qualified type
7272/// to anonymous class or struct.
7273static bool
7274pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7275{
7276 if (!die_is_pointer_array_or_reference_type(die)
7277 && !die_is_qualified_type(die))
7278 return false;
7279
7280 Dwarf_Die underlying_type_die;
7281 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7282 return false;
7283
7284 if (!die_is_class_type(&underlying_type_die))
7285 return false;
7286
7287 string name = die_name(&underlying_type_die);
7288
7289 return name.empty();
7290}
7291
7292/// Test if a DIE represents a reference type.
7293///
7294/// @param die the die to consider.
7295///
7296/// @return true iff @p die represents a reference type.
7297static bool
7298die_is_reference_type(const Dwarf_Die* die)
7299{
7300 if (!die)
7301 return false;
7302
7303 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7304 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7305 return true;
7306
7307 return false;
7308}
7309
7310/// Test if a DIE represents an array type.
7311///
7312/// @param die the die to consider.
7313///
7314/// @return true iff @p die represents an array type.
7315static bool
7316die_is_array_type(const Dwarf_Die* die)
7317{
7318 if (!die)
7319 return false;
7320
7321 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7322 if (tag == DW_TAG_array_type)
7323 return true;
7324
7325 return false;
7326}
7327
7328/// Test if a DIE represents a pointer, reference or array type.
7329///
7330/// @param die the die to consider.
7331///
7332/// @return true iff @p die represents a pointer or reference type.
7333static bool
7334die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7335{return (die_is_pointer_type(die)
7336 || die_is_reference_type(die)
7337 || die_is_array_type(die));}
7338
7339/// Test if a DIE represents a pointer or a reference type.
7340///
7341/// @param die the die to consider.
7342///
7343/// @return true iff @p die represents a pointer or reference type.
7344static bool
7345die_is_pointer_or_reference_type(const Dwarf_Die* die)
7346{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7347
7348/// Test if a DIE represents a pointer, a reference or a typedef type.
7349///
7350/// @param die the die to consider.
7351///
7352/// @return true iff @p die represents a pointer, a reference or a
7353/// typedef type.
7354static bool
7355die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7356{return (die_is_pointer_array_or_reference_type(die)
7357 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7358
7359/// Test if a DIE represents a class type.
7360///
7361/// @param die the die to consider.
7362///
7363/// @return true iff @p die represents a class type.
7364static bool
7365die_is_class_type(const Dwarf_Die* die)
7366{
7367 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7368
7369 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7370 return true;
7371
7372 return false;
7373}
7374
7375/// Test if a DIE is for a qualified type.
7376///
7377/// @param die the DIE to consider.
7378///
7379/// @return true iff @p die is for a qualified type.
7380static bool
7381die_is_qualified_type(const Dwarf_Die* die)
7382{
7383 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7384 if (tag == DW_TAG_const_type
7385 || tag == DW_TAG_volatile_type
7386 || tag == DW_TAG_restrict_type)
7387 return true;
7388
7389 return false;
7390}
7391
7392/// Test if a DIE is for a function type.
7393///
7394/// @param die the DIE to consider.
7395///
7396/// @return true iff @p die is for a function type.
7397static bool
7398die_is_function_type(const Dwarf_Die *die)
7399{
7400 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7401 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7402 return true;
7403
7404 return false;
7405}
7406
7407/// Test if a DIE for a function pointer or member function has an
7408/// DW_AT_object_pointer attribute.
7409///
7410/// @param die the DIE to consider.
7411///
7412/// @param object_pointer out parameter. It's set to the DIE for the
7413/// object pointer iff the function returns true.
7414///
7415/// @return true iff the DIE @p die has an object pointer. In that
7416/// case, the parameter @p object_pointer is set to the DIE of that
7417/// object pointer.
7418static bool
7419die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7420{
7421 if (!die)
7422 return false;
7423
7424 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7425 return true;
7426
7427 return false;
7428}
7429
7430/// Test if a DIE has children DIEs.
7431///
7432/// @param die the DIE to consider.
7433///
7434/// @return true iff @p DIE has at least one child node.
7435static bool
7436die_has_children(const Dwarf_Die* die)
7437{
7438 if (!die)
7439 return false;
7440
7441 Dwarf_Die child;
7442 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7443 return true;
7444
7445 return false;
7446}
7447
7448/// Get the DIE representing the first parameter of the function
7449/// denoted by a given DIE.
7450///
7451/// @param die the function DIE to consider. Note that if this
7452/// parameter is neither a DW_TAG_subprogram nor a
7453/// DW_TAG_subroutine_type, then the current process is aborted.
7454///
7455/// @param first_parm_die output parameter. This is set to the DIE of
7456/// the first parameter of the function denoted by @p die. This
7457/// output parameter is set iff the function returns true.
7458///
7459/// @return true iff the first parameter of the function denoted by @p
7460/// die is returned in output parameter @p first_parm_die.
7461static bool
7462fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die)
7463{
7464 if (!die)
7465 return false;
7466
7467 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7468 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7469
7470 Dwarf_Die child;
7471 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7472 {
7473 int child_tag = dwarf_tag(&child);
7474 if (child_tag == DW_TAG_formal_parameter)
7475 {
7476 memcpy(&first_parm_die, &child, sizeof(Dwarf_Die));
7477 return true;
7478 }
7479 }
7480 return false;
7481}
7482
7483/// Test if a member function denoted by a given DIE has a parameter
7484/// which is a "this pointer".
7485///
7486/// Please note that if the member function denotes a static member
7487/// function or if the DIE does not denote a member function to begin
7488/// with, then the function will return false because no "this
7489/// pointer" will be found.
7490///
7491/// @param rdr the current DWARF reader in use.
7492///
7493/// @param die the DIE of the member function this function should
7494/// inspect.
7495///
7496/// @param where_offset where in the DIE stream we logically are.
7497///
7498/// @param class_die output parameter. This is set iff a "this
7499/// pointer" was found as the first parameters of the member function
7500/// denoted by @p die, and thus the function returns true If set, this
7501/// then points to the DIE of the class containing the member function
7502/// denoted by @p die.
7503///
7504/// @param object_pointer_die output parameter. This is set to the
7505/// DIE of the function parameter that carries the "this pointe".
7506/// This is set iff this function return true.
7507///
7508/// @return true iff the first parameter of the member function
7509/// denoted by @p die points to a "this pointer".
7510static bool
7511member_fn_die_has_this_pointer(const reader& rdr,
7512 const Dwarf_Die* die,
7513 size_t where_offset,
7514 Dwarf_Die& class_die,
7515 Dwarf_Die& object_pointer_die)
7516{
7517 if (!die)
7518 return false;
7519
7520 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7521 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
7522 return false;
7523
7524 if (tag == DW_TAG_subprogram
7525 && !die_is_at_class_scope(rdr, die, where_offset, class_die))
7526 return false;
7527
7528 Dwarf_Die first_parm_die;
7529 Dwarf_Die parm_type_die;
7530 if (die_has_object_pointer(die, object_pointer_die))
7531 {
7532 // This can be either a member function with a
7533 // DW_AT_object_pointer attribute or a DW_TAG_subroutine_type
7534 // with a DW_AT_object_pointer. In the later case, we are
7535 // looking at a member function type.
7536 memcpy(&first_parm_die, &object_pointer_die, sizeof(Dwarf_Die));
7537 if (!die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7538 return false;
7539 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7540 die_peel_typedef(&parm_type_die, parm_type_die);
7541 }
7542 else if (fn_die_first_parameter_die(die, first_parm_die))
7543 {
7544 memcpy(&object_pointer_die, &first_parm_die, sizeof(Dwarf_Die));
7545 bool is_artificial = false;
7546 if (die_flag_attribute(&first_parm_die, DW_AT_artificial, is_artificial))
7547 {
7548 if (die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7549 {
7550 tag = dwarf_tag(&parm_type_die);
7551 if (tag == DW_TAG_pointer_type)
7552 {
7553 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7554 die_peel_typedef(&parm_type_die, parm_type_die);
7555 }
7556 else
7557 return false;
7558 }
7559 else
7560 return false;
7561 }
7562 else
7563 return false;
7564 }
7565 else
7566 return false;
7567
7568 tag = dwarf_tag(&parm_type_die);
7569 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7570 {
7571 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7572 return true;
7573 }
7574 return false;
7575}
7576
7577/// When given the object pointer DIE of a function type or member
7578/// function DIE, this function returns the "this" pointer that points
7579/// to the associated class.
7580///
7581/// @param die the DIE of the object pointer of the function or member
7582/// function to consider.
7583///
7584/// @param this_pointer_die out parameter. This is set to the DIE of
7585/// the "this" pointer iff the function returns true.
7586///
7587/// @return true iff the function found the "this" pointer from the
7588/// object pointer DIE @p die. In that case, the parameter @p
7589/// this_pointer_die is set to the DIE of that "this" pointer.
7590static bool
7591die_this_pointer_from_object_pointer(Dwarf_Die* die,
7592 Dwarf_Die& this_pointer_die)
7593{
7594 ABG_ASSERT(die);
7595 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7596
7597 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7598 return true;
7599
7600 return false;
7601}
7602
7603/// Test if a given "this" pointer that points to a particular class
7604/// type is for a const class or not. If it's for a const class, then
7605/// it means the function type or the member function associated to
7606/// that "this" pointer is const.
7607///
7608/// @param dye the DIE of the "this" pointer to consider.
7609///
7610/// @return true iff @p die points to a const class type.
7611static bool
7612die_this_pointer_is_const(Dwarf_Die* dye)
7613{
7614 ABG_ASSERT(dye);
7615
7616 Dwarf_Die die;
7617 memcpy(&die, dye, sizeof(Dwarf_Die));
7618 if (dwarf_tag(&die) == DW_TAG_const_type)
7619 ABG_ASSERT(die_peel_qualified(&die, die));
7620
7621 if (dwarf_tag(&die) == DW_TAG_pointer_type)
7622 {
7623 Dwarf_Die pointed_to_type_die;
7624 if (die_die_attribute(&die, DW_AT_type, pointed_to_type_die))
7625 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7626 return true;
7627 }
7628
7629 return false;
7630}
7631
7632/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7633/// attribute) points to a const implicit class and so is for a const
7634/// method or or a const member function type.
7635///
7636/// @param die the DIE of the object pointer to consider.
7637///
7638/// @return true iff the object pointer represented by @p die is for a
7639/// a const method or const member function type.
7640static bool
7641die_object_pointer_is_for_const_method(Dwarf_Die* die)
7642{
7643 ABG_ASSERT(die);
7644 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7645
7646 Dwarf_Die this_pointer_die;
7647 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7648 if (die_this_pointer_is_const(&this_pointer_die))
7649 return true;
7650
7651 return false;
7652}
7653
7654/// Test if a DIE represents an entity that is at class scope.
7655///
7656/// @param rdr the DWARF reader to use.
7657///
7658/// @param die the DIE to consider.
7659///
7660/// @param where_offset where we are logically at in the DIE stream.
7661///
7662/// @param class_scope_die out parameter. Set to the DIE of the
7663/// containing class iff @p die happens to be at class scope; that is,
7664/// iff the function returns true.
7665///
7666/// @return true iff @p die is at class scope. In that case, @p
7667/// class_scope_die is set to the DIE of the class that contains @p
7668/// die.
7669static bool
7670die_is_at_class_scope(const reader& rdr,
7671 const Dwarf_Die* die,
7672 size_t where_offset,
7673 Dwarf_Die& class_scope_die)
7674{
7675 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7676 return false;
7677
7678 int tag = dwarf_tag(&class_scope_die);
7679
7680 return (tag == DW_TAG_structure_type
7681 || tag == DW_TAG_class_type
7682 || tag == DW_TAG_union_type);
7683}
7684
7685/// Return the leaf object under a pointer, reference or qualified
7686/// type DIE.
7687///
7688/// @param die the DIE of the type to consider.
7689///
7690/// @param peeled_die out parameter. Set to the DIE of the leaf
7691/// object iff the function actually peeled anything.
7692///
7693/// @return true upon successful completion.
7694static bool
7695die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7696{
7697 if (!die)
7698 return false;
7699
7700 int tag = dwarf_tag(die);
7701
7702 if (tag == DW_TAG_const_type
7703 || tag == DW_TAG_volatile_type
7704 || tag == DW_TAG_restrict_type
7705 || tag == DW_TAG_pointer_type
7706 || tag == DW_TAG_reference_type
7707 || tag == DW_TAG_rvalue_reference_type)
7708 {
7709 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7710 return false;
7711 }
7712 else
7713 return false;
7714
7715 memcpy(&peeled_die, die, sizeof(peeled_die));
7716
7717 while (tag == DW_TAG_const_type
7718 || tag == DW_TAG_volatile_type
7719 || tag == DW_TAG_restrict_type
7720 || tag == DW_TAG_pointer_type
7721 || tag == DW_TAG_reference_type
7722 || tag == DW_TAG_rvalue_reference_type)
7723 {
7724 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7725 break;
7726 tag = dwarf_tag(&peeled_die);
7727 }
7728
7729 return true;
7730}
7731
7732/// Return the leaf object under a qualified type DIE.
7733///
7734/// @param die the DIE of the type to consider.
7735///
7736/// @param peeled_die out parameter. Set to the DIE of the leaf
7737/// object iff the function actually peeled anything.
7738///
7739/// @return true upon successful completion.
7740static bool
7741die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7742{
7743 if (!die)
7744 return false;
7745
7746 memcpy(&peeled_die, die, sizeof(peeled_die));
7747
7748 int tag = dwarf_tag(&peeled_die);
7749
7750 bool result = false;
7751 while (tag == DW_TAG_const_type
7752 || tag == DW_TAG_volatile_type
7753 || tag == DW_TAG_restrict_type)
7754 {
7755 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7756 break;
7757 tag = dwarf_tag(&peeled_die);
7758 result = true;
7759 }
7760
7761 return result;
7762}
7763
7764/// Return the leaf object under a typedef type DIE.
7765///
7766/// @param die the DIE of the type to consider.
7767///
7768/// @param peeled_die out parameter. Set to the DIE of the leaf
7769/// object iff the function actually peeled anything.
7770///
7771/// @return true upon successful completion.
7772static bool
7773die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7774{
7775 if (!die)
7776 return false;
7777
7778 int tag = dwarf_tag(die);
7779
7780 memcpy(&peeled_die, die, sizeof(peeled_die));
7781
7782 if (tag == DW_TAG_typedef)
7783 {
7784 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7785 return false;
7786 }
7787 else
7788 return false;
7789
7790 while (tag == DW_TAG_typedef)
7791 {
7792 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7793 break;
7794 tag = dwarf_tag(&peeled_die);
7795 }
7796
7797 return true;
7798
7799}
7800
7801/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7802///
7803/// @param die the DIE to consider.
7804///
7805/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7806/// iff the function returned true.
7807///
7808/// @return true iff the function could peel @p die.
7809static bool
7810die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7811{
7812 if (!die)
7813 return false;
7814
7815 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7816
7817 if (tag == DW_TAG_pointer_type
7818 || tag == DW_TAG_reference_type
7819 || tag == DW_TAG_rvalue_reference_type
7820 || tag == DW_TAG_typedef)
7821 {
7822 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7823 return false;
7824 }
7825 else
7826 return false;
7827
7828 while (tag == DW_TAG_pointer_type
7829 || tag == DW_TAG_reference_type
7830 || tag == DW_TAG_rvalue_reference_type
7831 || tag == DW_TAG_typedef)
7832 {
7833 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7834 break;
7835 tag = dwarf_tag(&peeled_die);
7836 }
7837 return true;
7838}
7839
7840/// Test if a DIE for a function type represents a method type.
7841///
7842/// @param rdr the DWARF reader.
7843///
7844/// @param die the DIE to consider.
7845///
7846/// @param where_offset where we logically are in the stream of DIEs.
7847///
7848/// @param object_pointer_die out parameter. This is set by the
7849/// function to the DIE that refers to the formal function parameter
7850/// which holds the implicit "this" pointer of the method. That die
7851/// is called the object pointer DIE. This is set iff the member
7852/// function is a non-static member function and if the function
7853/// returns true. In other words, this is only set if the is_static
7854/// out parameter is set to false and the function returns true.
7855///
7856/// @param class_die out parameter. This is set by the function to
7857/// the DIE that represents the class of the method type. This is set
7858/// iff the function returns true.
7859///
7860/// @param is_static out parameter. This is set to true by the
7861/// function if @p die is a static method or a the type of a static
7862/// method. This is set iff the function returns true.
7863///
7864/// @return true iff @p die is a DIE for a method type.
7865static bool
7866die_function_type_is_method_type(const reader& rdr,
7867 const Dwarf_Die *die,
7868 size_t where_offset,
7869 Dwarf_Die& object_pointer_die,
7870 Dwarf_Die& class_die,
7871 bool& is_static)
7872{
7873 if (!die)
7874 return false;
7875
7876 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7877 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7878
7879 if (member_fn_die_has_this_pointer(rdr, die, where_offset, class_die, object_pointer_die))
7880 {
7881 is_static = false;
7882 return true;
7883 }
7884 else if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7885 {
7886 is_static = true;
7887 return true;
7888 }
7889
7890 return false;
7891}
7892
7893enum virtuality
7894{
7895 VIRTUALITY_NOT_VIRTUAL,
7896 VIRTUALITY_VIRTUAL,
7897 VIRTUALITY_PURE_VIRTUAL
7898};
7899
7900/// Get the virtual-ness of a given DIE, that is, the value of the
7901/// DW_AT_virtuality attribute.
7902///
7903/// @param die the DIE to read from.
7904///
7905/// @param virt the resulting virtuality attribute. This is set iff
7906/// the function returns true.
7907///
7908/// @return true if the virtual-ness could be determined.
7909static bool
7910die_virtuality(const Dwarf_Die* die, virtuality& virt)
7911{
7912 if (!die)
7913 return false;
7914
7915 uint64_t v = 0;
7916 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7917
7918 if (v == DW_VIRTUALITY_virtual)
7919 virt = VIRTUALITY_VIRTUAL;
7920 else if (v == DW_VIRTUALITY_pure_virtual)
7921 virt = VIRTUALITY_PURE_VIRTUAL;
7922 else
7923 virt = VIRTUALITY_NOT_VIRTUAL;
7924
7925 return true;
7926}
7927
7928/// Test whether the DIE represent either a virtual base or function.
7929///
7930/// @param die the DIE to consider.
7931///
7932/// @return bool if the DIE represents a virtual base or function,
7933/// false othersise.
7934static bool
7935die_is_virtual(const Dwarf_Die* die)
7936{
7937 virtuality v;
7938 if (!die_virtuality(die, v))
7939 return false;
7940
7941 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7942}
7943
7944/// Test if the DIE represents an entity that was declared inlined.
7945///
7946/// @param die the DIE to test for.
7947///
7948/// @return true if the DIE represents an entity that was declared
7949/// inlined.
7950static bool
7951die_is_declared_inline(Dwarf_Die* die)
7952{
7953 uint64_t inline_value = 0;
7954 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7955 return false;
7956 return (inline_value == DW_INL_declared_inlined
7957 || inline_value == DW_INL_declared_not_inlined);
7958}
7959
7960/// Compare two DWARF strings using the most accurate (and slowest)
7961/// method possible.
7962///
7963/// @param l the DIE that carries the first string to consider, as an
7964/// attribute value.
7965///
7966/// @param attr_name the name of the attribute which value is the
7967/// string to compare.
7968///
7969/// @return true iff the string carried by @p l equals the one carried
7970/// by @p r.
7971static bool
7972slowly_compare_strings(const Dwarf_Die *l,
7973 const Dwarf_Die *r,
7974 unsigned attr_name)
7975{
7976 const char *l_str = die_char_str_attribute(l, attr_name),
7977 *r_str = die_char_str_attribute(r, attr_name);
7978 if (!l_str && !r_str)
7979 return true;
7980 return l_str && r_str && !strcmp(l_str, r_str);
7981}
7982
7983/// This function is a fast routine (optimization) to compare the
7984/// values of two string attributes of two DIEs.
7985///
7986/// @param l the first DIE to consider.
7987///
7988/// @param r the second DIE to consider.
7989///
7990/// @param attr_name the name of the attribute to compare, on the two
7991/// DIEs above.
7992///
7993/// @param result out parameter. This is set to the result of the
7994/// comparison. If the value of attribute @p attr_name on DIE @p l
7995/// equals the value of attribute @p attr_name on DIE @p r, then the
7996/// the argument of this parameter is set to true. Otherwise, it's
7997/// set to false. Note that the argument of this parameter is set iff
7998/// the function returned true.
7999///
8000/// @return true iff the comparison could be performed. There are
8001/// cases in which the comparison cannot be performed. For instance,
8002/// if one of the DIEs does not have the attribute @p attr_name. In
8003/// any case, if this function returns true, then the parameter @p
8004/// result is set to the result of the comparison.
8005static bool
8006compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
8007 unsigned attr_name,
8008 bool &result)
8009{
8010 Dwarf_Attribute l_attr, r_attr;
8011 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
8012 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
8013 return false;
8014
8015 ABG_ASSERT(l_attr.form == DW_FORM_strp
8016 || l_attr.form == DW_FORM_string
8017 || l_attr.form == DW_FORM_GNU_strp_alt
8018 || form_is_DW_FORM_strx(l_attr.form)
8019 || form_is_DW_FORM_line_strp(l_attr.form));
8020
8021 ABG_ASSERT(r_attr.form == DW_FORM_strp
8022 || r_attr.form == DW_FORM_string
8023 || r_attr.form == DW_FORM_GNU_strp_alt
8024 || form_is_DW_FORM_strx(r_attr.form)
8025 || form_is_DW_FORM_line_strp(r_attr.form));
8026
8027 if ((l_attr.form == DW_FORM_strp
8028 && r_attr.form == DW_FORM_strp)
8029 || (l_attr.form == DW_FORM_GNU_strp_alt
8030 && r_attr.form == DW_FORM_GNU_strp_alt)
8031 || (form_is_DW_FORM_strx(l_attr.form)
8032 && form_is_DW_FORM_strx(r_attr.form))
8033 || (form_is_DW_FORM_line_strp(l_attr.form)
8034 && form_is_DW_FORM_line_strp(r_attr.form)))
8035 {
8036 // So these string attributes are actually pointers into a
8037 // string table. The string table is most likely de-duplicated
8038 // so comparing the *values* of the pointers should be enough.
8039 //
8040 // This is the fast path.
8041 if (l_attr.valp == r_attr.valp)
8042 {
8043#if WITH_DEBUG_TYPE_CANONICALIZATION
8044 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
8045#endif
8046 result = true;
8047 return true;
8048 }
8049 }
8050
8051 // If we reached this point it means we couldn't use the fast path
8052 // because the string atttributes are strings that are "inline" in
8053 // the debug info section. Let's just compare them the slow and
8054 // obvious way.
8055 result = slowly_compare_strings(l, r, attr_name);
8056 return true;
8057}
8058
8059/// Compare the file path of the compilation units (aka CUs)
8060/// associated to two DIEs.
8061///
8062/// If the DIEs are for pointers or typedefs, this function also
8063/// compares the file paths of the CUs of the leaf DIEs (underlying
8064/// DIEs of the pointer or the typedef).
8065///
8066/// @param l the first type DIE to consider.
8067///
8068/// @param r the second type DIE to consider.
8069///
8070/// @return true iff the file paths of the DIEs of the two types are
8071/// equal.
8072static bool
8073compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
8074{
8075 Dwarf_Die l_cu, r_cu;
8076 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
8077 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
8078 return false;
8079
8080 bool compared =
8081 compare_dies_string_attribute_value(&l_cu, &r_cu,
8082 DW_AT_name,
8083 result);
8084 if (compared && result)
8085 {
8086 Dwarf_Die peeled_l, peeled_r;
8087 if (die_is_pointer_reference_or_typedef_type(l)
8088 && die_is_pointer_reference_or_typedef_type(r)
8089 && die_peel_pointer_and_typedef(l, peeled_l)
8090 && die_peel_pointer_and_typedef(r, peeled_r))
8091 {
8092 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
8093 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
8094 return false;
8095 compared =
8096 compare_dies_string_attribute_value(&l_cu, &r_cu,
8097 DW_AT_name,
8098 result);
8099 }
8100 }
8101
8102 return compared;
8103}
8104
8105// -----------------------------------
8106// <location expression evaluation>
8107// -----------------------------------
8108
8109/// Get the value of a given DIE attribute, knowing that it must be a
8110/// location expression.
8111///
8112/// @param die the DIE to read the attribute from.
8113///
8114/// @param attr_name the name of the attribute to read the value for.
8115///
8116/// @param expr the pointer to allocate and fill with the resulting
8117/// array of operators + operands forming a dwarf expression. This is
8118/// set iff the function returns true.
8119///
8120/// @param expr_len the length of the resulting dwarf expression.
8121/// This is set iff the function returns true.
8122///
8123/// @return true if the attribute exists and has a non-empty dwarf expression
8124/// as value. In that case the expr and expr_len arguments are set to the
8125/// resulting dwarf expression.
8126static bool
8127die_location_expr(const Dwarf_Die* die,
8128 unsigned attr_name,
8129 Dwarf_Op** expr,
8130 size_t* expr_len)
8131{
8132 if (!die)
8133 return false;
8134
8135 Dwarf_Attribute attr;
8136 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
8137 return false;
8138
8139 size_t len = 0;
8140 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
8141
8142 // Ignore location expressions where reading them succeeded but
8143 // their length is 0.
8144 result &= len > 0;
8145
8146 if (result)
8147 *expr_len = len;
8148
8149 return result;
8150}
8151
8152/// If the current operation in the dwarf expression represents a push
8153/// of a constant value onto the dwarf expr virtual machine (aka
8154/// DEVM), perform the operation and update the DEVM.
8155///
8156/// If the result of the operation is a constant, update the DEVM
8157/// accumulator with its value. Otherwise, the DEVM accumulator is
8158/// left with its previous value.
8159///
8160/// @param ops the array of the dwarf expression operations to consider.
8161///
8162/// @param ops_len the lengths of @p ops array above.
8163///
8164/// @param index the index of the operation to interpret, in @p ops.
8165///
8166/// @param next_index the index of the operation to interpret at the
8167/// next step, after this function completed and returned. This is
8168/// set an output parameter that is set iff the function returns true.
8169///
8170/// @param ctxt the DEVM evaluation context.
8171///
8172/// @return true if the current operation actually pushes a constant
8173/// value onto the DEVM stack, false otherwise.
8174static bool
8175op_pushes_constant_value(Dwarf_Op* ops,
8176 size_t ops_len,
8177 size_t index,
8178 size_t& next_index,
8179 dwarf_expr_eval_context& ctxt)
8180{
8181 ABG_ASSERT(index < ops_len);
8182
8183 Dwarf_Op& op = ops[index];
8184 int64_t value = 0;
8185
8186 switch (op.atom)
8187 {
8188 case DW_OP_addr:
8189 value = ops[index].number;
8190 break;
8191
8192 case DW_OP_const1u:
8193 case DW_OP_const1s:
8194 case DW_OP_const2u:
8195 case DW_OP_const2s:
8196 case DW_OP_const4u:
8197 case DW_OP_const4s:
8198 case DW_OP_const8u:
8199 case DW_OP_const8s:
8200 case DW_OP_constu:
8201 case DW_OP_consts:
8202 value = ops[index].number;
8203 break;
8204
8205 case DW_OP_lit0:
8206 value = 0;
8207 break;
8208 case DW_OP_lit1:
8209 value = 1;
8210 break;
8211 case DW_OP_lit2:
8212 value = 2;
8213 break;
8214 case DW_OP_lit3:
8215 value = 3;
8216 break;
8217 case DW_OP_lit4:
8218 value = 4;
8219 break;
8220 case DW_OP_lit5:
8221 value = 5;
8222 break;
8223 case DW_OP_lit6:
8224 value = 6;
8225 break;
8226 case DW_OP_lit7:
8227 value = 7;
8228 break;
8229 case DW_OP_lit8:
8230 value = 8;
8231 break;
8232 case DW_OP_lit9:
8233 value = 9;
8234 break;
8235 case DW_OP_lit10:
8236 value = 10;
8237 break;
8238 case DW_OP_lit11:
8239 value = 11;
8240 break;
8241 case DW_OP_lit12:
8242 value = 12;
8243 break;
8244 case DW_OP_lit13:
8245 value = 13;
8246 break;
8247 case DW_OP_lit14:
8248 value = 14;
8249 break;
8250 case DW_OP_lit15:
8251 value = 15;
8252 break;
8253 case DW_OP_lit16:
8254 value = 16;
8255 break;
8256 case DW_OP_lit17:
8257 value = 17;
8258 break;
8259 case DW_OP_lit18:
8260 value = 18;
8261 break;
8262 case DW_OP_lit19:
8263 value = 19;
8264 break;
8265 case DW_OP_lit20:
8266 value = 20;
8267 break;
8268 case DW_OP_lit21:
8269 value = 21;
8270 break;
8271 case DW_OP_lit22:
8272 value = 22;
8273 break;
8274 case DW_OP_lit23:
8275 value = 23;
8276 break;
8277 case DW_OP_lit24:
8278 value = 24;
8279 break;
8280 case DW_OP_lit25:
8281 value = 25;
8282 break;
8283 case DW_OP_lit26:
8284 value = 26;
8285 break;
8286 case DW_OP_lit27:
8287 value = 27;
8288 break;
8289 case DW_OP_lit28:
8290 value = 28;
8291 break;
8292 case DW_OP_lit29:
8293 value = 29;
8294 break;
8295 case DW_OP_lit30:
8296 value = 30;
8297 break;
8298 case DW_OP_lit31:
8299 value = 31;
8300 break;
8301
8302 default:
8303 return false;
8304 }
8305
8306 expr_result r(value);
8307 ctxt.push(r);
8308 ctxt.accum = r;
8309 next_index = index + 1;
8310
8311 return true;
8312}
8313
8314/// If the current operation in the dwarf expression represents a push
8315/// of a non-constant value onto the dwarf expr virtual machine (aka
8316/// DEVM), perform the operation and update the DEVM. A non-constant
8317/// is namely a quantity for which we need inferior (a running program
8318/// image) state to know the exact value.
8319///
8320/// Upon successful completion, as the result of the operation is a
8321/// non-constant the DEVM accumulator value is left to its state as of
8322/// before the invocation of this function.
8323///
8324/// @param ops the array of the dwarf expression operations to consider.
8325///
8326/// @param ops_len the lengths of @p ops array above.
8327///
8328/// @param index the index of the operation to interpret, in @p ops.
8329///
8330/// @param next_index the index of the operation to interpret at the
8331/// next step, after this function completed and returned. This is
8332/// set an output parameter that is set iff the function returns true.
8333///
8334/// @param ctxt the DEVM evaluation context.
8335///
8336/// @return true if the current operation actually pushes a
8337/// non-constant value onto the DEVM stack, false otherwise.
8338static bool
8339op_pushes_non_constant_value(Dwarf_Op* ops,
8340 size_t ops_len,
8341 size_t index,
8342 size_t& next_index,
8343 dwarf_expr_eval_context& ctxt)
8344{
8345 ABG_ASSERT(index < ops_len);
8346 Dwarf_Op& op = ops[index];
8347
8348 switch (op.atom)
8349 {
8350 case DW_OP_reg0:
8351 case DW_OP_reg1:
8352 case DW_OP_reg2:
8353 case DW_OP_reg3:
8354 case DW_OP_reg4:
8355 case DW_OP_reg5:
8356 case DW_OP_reg6:
8357 case DW_OP_reg7:
8358 case DW_OP_reg8:
8359 case DW_OP_reg9:
8360 case DW_OP_reg10:
8361 case DW_OP_reg11:
8362 case DW_OP_reg12:
8363 case DW_OP_reg13:
8364 case DW_OP_reg14:
8365 case DW_OP_reg15:
8366 case DW_OP_reg16:
8367 case DW_OP_reg17:
8368 case DW_OP_reg18:
8369 case DW_OP_reg19:
8370 case DW_OP_reg20:
8371 case DW_OP_reg21:
8372 case DW_OP_reg22:
8373 case DW_OP_reg23:
8374 case DW_OP_reg24:
8375 case DW_OP_reg25:
8376 case DW_OP_reg26:
8377 case DW_OP_reg27:
8378 case DW_OP_reg28:
8379 case DW_OP_reg29:
8380 case DW_OP_reg30:
8381 case DW_OP_reg31:
8382 next_index = index + 1;
8383 break;
8384
8385 case DW_OP_breg0:
8386 case DW_OP_breg1:
8387 case DW_OP_breg2:
8388 case DW_OP_breg3:
8389 case DW_OP_breg4:
8390 case DW_OP_breg5:
8391 case DW_OP_breg6:
8392 case DW_OP_breg7:
8393 case DW_OP_breg8:
8394 case DW_OP_breg9:
8395 case DW_OP_breg10:
8396 case DW_OP_breg11:
8397 case DW_OP_breg12:
8398 case DW_OP_breg13:
8399 case DW_OP_breg14:
8400 case DW_OP_breg15:
8401 case DW_OP_breg16:
8402 case DW_OP_breg17:
8403 case DW_OP_breg18:
8404 case DW_OP_breg19:
8405 case DW_OP_breg20:
8406 case DW_OP_breg21:
8407 case DW_OP_breg22:
8408 case DW_OP_breg23:
8409 case DW_OP_breg24:
8410 case DW_OP_breg25:
8411 case DW_OP_breg26:
8412 case DW_OP_breg27:
8413 case DW_OP_breg28:
8414 case DW_OP_breg29:
8415 case DW_OP_breg30:
8416 case DW_OP_breg31:
8417 next_index = index + 1;
8418 break;
8419
8420 case DW_OP_regx:
8421 next_index = index + 2;
8422 break;
8423
8424 case DW_OP_fbreg:
8425 next_index = index + 1;
8426 break;
8427
8428 case DW_OP_bregx:
8429 next_index = index + 1;
8430 break;
8431
8432 case DW_OP_GNU_variable_value:
8433 next_index = index + 1;
8434 break;
8435
8436 default:
8437 return false;
8438 }
8439
8440 expr_result r(false);
8441 ctxt.push(r);
8442
8443 return true;
8444}
8445
8446/// If the current operation in the dwarf expression represents a
8447/// manipulation of the stack of the DWARF Expression Virtual Machine
8448/// (aka DEVM), this function performs the operation and updates the
8449/// state of the DEVM. If the result of the operation represents a
8450/// constant value, then the accumulator of the DEVM is set to that
8451/// result's value, Otherwise, the DEVM accumulator is left with its
8452/// previous value.
8453///
8454/// @param expr the array of the dwarf expression operations to consider.
8455///
8456/// @param expr_len the lengths of @p ops array above.
8457///
8458/// @param index the index of the operation to interpret, in @p ops.
8459///
8460/// @param next_index the index of the operation to interpret at the
8461/// next step, after this function completed and returned. This is
8462/// set an output parameter that is set iff the function returns true.
8463///
8464/// @param ctxt the DEVM evaluation context.
8465///
8466/// @return true if the current operation actually manipulates the
8467/// DEVM stack, false otherwise.
8468static bool
8469op_manipulates_stack(Dwarf_Op* expr,
8470 size_t expr_len,
8471 size_t index,
8472 size_t& next_index,
8473 dwarf_expr_eval_context& ctxt)
8474{
8475 Dwarf_Op& op = expr[index];
8476 expr_result v;
8477
8478 switch (op.atom)
8479 {
8480 case DW_OP_dup:
8481 v = ctxt.stack.front();
8482 ctxt.push(v);
8483 break;
8484
8485 case DW_OP_drop:
8486 v = ctxt.stack.front();
8487 ctxt.pop();
8488 break;
8489
8490 case DW_OP_over:
8491 ABG_ASSERT(ctxt.stack.size() > 1);
8492 v = ctxt.stack[1];
8493 ctxt.push(v);
8494 break;
8495
8496 case DW_OP_pick:
8497 ABG_ASSERT(index + 1 < expr_len);
8498 v = op.number;
8499 ctxt.push(v);
8500 break;
8501
8502 case DW_OP_swap:
8503 ABG_ASSERT(ctxt.stack.size() > 1);
8504 v = ctxt.stack[1];
8505 ctxt.stack.erase(ctxt.stack.begin() + 1);
8506 ctxt.push(v);
8507 break;
8508
8509 case DW_OP_rot:
8510 ABG_ASSERT(ctxt.stack.size() > 2);
8511 v = ctxt.stack[2];
8512 ctxt.stack.erase(ctxt.stack.begin() + 2);
8513 ctxt.push(v);
8514 break;
8515
8516 case DW_OP_deref:
8517 case DW_OP_deref_size:
8518 ABG_ASSERT(ctxt.stack.size() > 0);
8519 ctxt.pop();
8520 v.is_const(false);
8521 ctxt.push(v);
8522 break;
8523
8524 case DW_OP_xderef:
8525 case DW_OP_xderef_size:
8526 ABG_ASSERT(ctxt.stack.size() > 1);
8527 ctxt.pop();
8528 ctxt.pop();
8529 v.is_const(false);
8530 ctxt.push(v);
8531 break;
8532
8533 case DW_OP_push_object_address:
8534 v.is_const(false);
8535 ctxt.push(v);
8536 break;
8537
8538 case DW_OP_form_tls_address:
8539 case DW_OP_GNU_push_tls_address:
8540 ABG_ASSERT(ctxt.stack.size() > 0);
8541 v = ctxt.pop();
8542 if (op.atom == DW_OP_form_tls_address)
8543 v.is_const(false);
8544 ctxt.push(v);
8545 break;
8546
8547 case DW_OP_call_frame_cfa:
8548 v.is_const(false);
8549 ctxt.push(v);
8550 break;
8551
8552 default:
8553 return false;
8554 }
8555
8556 if (v.is_const())
8557 ctxt.accum = v;
8558
8559 if (op.atom == DW_OP_form_tls_address
8560 || op.atom == DW_OP_GNU_push_tls_address)
8561 ctxt.set_tls_address(true);
8562 else
8563 ctxt.set_tls_address(false);
8564
8565 next_index = index + 1;
8566
8567 return true;
8568}
8569
8570/// If the current operation in the dwarf expression represents a push
8571/// of an arithmetic or logic operation onto the dwarf expr virtual
8572/// machine (aka DEVM), perform the operation and update the DEVM.
8573///
8574/// If the result of the operation is a constant, update the DEVM
8575/// accumulator with its value. Otherwise, the DEVM accumulator is
8576/// left with its previous value.
8577///
8578/// @param expr the array of the dwarf expression operations to consider.
8579///
8580/// @param expr_len the lengths of @p expr array above.
8581///
8582/// @param index the index of the operation to interpret, in @p expr.
8583///
8584/// @param next_index the index of the operation to interpret at the
8585/// next step, after this function completed and returned. This is
8586/// set an output parameter that is set iff the function returns true.
8587///
8588/// @param ctxt the DEVM evaluation context.
8589///
8590/// @return true if the current operation actually represent an
8591/// arithmetic or logic operation.
8592static bool
8593op_is_arith_logic(Dwarf_Op* expr,
8594 size_t expr_len,
8595 size_t index,
8596 size_t& next_index,
8597 dwarf_expr_eval_context& ctxt)
8598{
8599 ABG_ASSERT(index < expr_len);
8600
8601 Dwarf_Op& op = expr[index];
8602 expr_result val1, val2;
8603 bool result = false;
8604
8605 switch (op.atom)
8606 {
8607 case DW_OP_abs:
8608 ABG_ASSERT(ctxt.stack.size() > 0);
8609 val1 = ctxt.pop();
8610 val1 = val1.abs();
8611 ctxt.push(val1);
8612 result = true;
8613 break;
8614
8615 case DW_OP_and:
8616 ABG_ASSERT(ctxt.stack.size() > 1);
8617 val1 = ctxt.pop();
8618 val2 = ctxt.pop();
8619 ctxt.push(val1 & val2);
8620 break;
8621
8622 case DW_OP_div:
8623 ABG_ASSERT(ctxt.stack.size() > 1);
8624 val1 = ctxt.pop();
8625 val2 = ctxt.pop();
8626 if (!val1.is_const())
8627 val1 = 1;
8628 ctxt.push(val2 / val1);
8629 result = true;
8630 break;
8631
8632 case DW_OP_minus:
8633 ABG_ASSERT(ctxt.stack.size() > 1);
8634 val1 = ctxt.pop();
8635 val2 = ctxt.pop();
8636 ctxt.push(val2 - val1);
8637 result = true;
8638 break;
8639
8640 case DW_OP_mod:
8641 ABG_ASSERT(ctxt.stack.size() > 1);
8642 val1 = ctxt.pop();
8643 val2 = ctxt.pop();
8644 ctxt.push(val2 % val1);
8645 result = true;
8646 break;
8647
8648 case DW_OP_mul:
8649 ABG_ASSERT(ctxt.stack.size() > 1);
8650 val1 = ctxt.pop();
8651 val2 = ctxt.pop();
8652 ctxt.push(val2 * val1);
8653 result = true;
8654 break;
8655
8656 case DW_OP_neg:
8657 ABG_ASSERT(ctxt.stack.size() > 0);
8658 val1 = ctxt.pop();
8659 ctxt.push(-val1);
8660 result = true;
8661 break;
8662
8663 case DW_OP_not:
8664 ABG_ASSERT(ctxt.stack.size() > 0);
8665 val1 = ctxt.pop();
8666 ctxt.push(~val1);
8667 result = true;
8668 break;
8669
8670 case DW_OP_or:
8671 ABG_ASSERT(ctxt.stack.size() > 1);
8672 val1 = ctxt.pop();
8673 val2 = ctxt.pop();
8674 ctxt.push(val1 | val2);
8675 result = true;
8676 break;
8677
8678 case DW_OP_plus:
8679 ABG_ASSERT(ctxt.stack.size() > 1);
8680 val1 = ctxt.pop();
8681 val2 = ctxt.pop();
8682 ctxt.push(val2 + val1);
8683 result = true;
8684 break;
8685
8686 case DW_OP_plus_uconst:
8687 ABG_ASSERT(ctxt.stack.size() > 0);
8688 val1 = ctxt.pop();
8689 val1 += op.number;
8690 ctxt.push(val1);
8691 result = true;
8692 break;
8693
8694 case DW_OP_shl:
8695 ABG_ASSERT(ctxt.stack.size() > 1);
8696 val1 = ctxt.pop();
8697 val2 = ctxt.pop();
8698 ctxt.push(val2 << val1);
8699 result = true;
8700 break;
8701
8702 case DW_OP_shr:
8703 case DW_OP_shra:
8704 ABG_ASSERT(ctxt.stack.size() > 1);
8705 val1 = ctxt.pop();
8706 val2 = ctxt.pop();
8707 ctxt.push(val2 >> val1);
8708 result = true;
8709 break;
8710
8711 case DW_OP_xor:
8712 ABG_ASSERT(ctxt.stack.size() > 1);
8713 val1 = ctxt.pop();
8714 val2 = ctxt.pop();
8715 ctxt.push(val2 ^ val1);
8716 result = true;
8717 break;
8718
8719 default:
8720 break;
8721 }
8722
8723 if (result == true)
8724 {
8725 if (ctxt.stack.front().is_const())
8726 ctxt.accum = ctxt.stack.front();
8727
8728 next_index = index + 1;
8729 }
8730 return result;;
8731}
8732
8733/// If the current operation in the dwarf expression represents a push
8734/// of a control flow operation onto the dwarf expr virtual machine
8735/// (aka DEVM), perform the operation and update the DEVM.
8736///
8737/// If the result of the operation is a constant, update the DEVM
8738/// accumulator with its value. Otherwise, the DEVM accumulator is
8739/// left with its previous value.
8740///
8741/// @param expr the array of the dwarf expression operations to consider.
8742///
8743/// @param expr_len the lengths of @p expr array above.
8744///
8745/// @param index the index of the operation to interpret, in @p expr.
8746///
8747/// @param next_index the index of the operation to interpret at the
8748/// next step, after this function completed and returned. This is
8749/// set an output parameter that is set iff the function returns true.
8750///
8751/// @param ctxt the DEVM evaluation context.
8752///
8753/// @return true if the current operation actually represents a
8754/// control flow operation, false otherwise.
8755static bool
8756op_is_control_flow(Dwarf_Op* expr,
8757 size_t expr_len,
8758 size_t index,
8759 size_t& next_index,
8760 dwarf_expr_eval_context& ctxt)
8761{
8762 ABG_ASSERT(index < expr_len);
8763
8764 Dwarf_Op& op = expr[index];
8765 expr_result val1, val2;
8766
8767 switch (op.atom)
8768 {
8769 case DW_OP_eq:
8770 case DW_OP_ge:
8771 case DW_OP_gt:
8772 case DW_OP_le:
8773 case DW_OP_lt:
8774 case DW_OP_ne:
8775 {
8776 bool value = true;
8777 val1 = ctxt.pop();
8778 val2 = ctxt.pop();
8779 if (op.atom == DW_OP_eq)
8780 value = val2 == val1;
8781 else if (op.atom == DW_OP_ge)
8782 value = val2 >= val1;
8783 else if (op.atom == DW_OP_gt)
8784 value = val2 > val1;
8785 else if (op.atom == DW_OP_le)
8786 value = val2 <= val1;
8787 else if (op.atom == DW_OP_lt)
8788 value = val2 < val1;
8789 else if (op.atom == DW_OP_ne)
8790 value = val2 != val1;
8791
8792 val1 = value ? 1 : 0;
8793 ctxt.push(val1);
8794 }
8795 break;
8796
8797 case DW_OP_skip:
8798 if (op.number > 0)
8799 index += op.number - 1;
8800 break;
8801
8802 case DW_OP_bra:
8803 val1 = ctxt.pop();
8804 if (val1.const_value() != 0)
8805 index += val1.const_value() - 1;
8806 break;
8807
8808 case DW_OP_call2:
8809 case DW_OP_call4:
8810 case DW_OP_call_ref:
8811 case DW_OP_nop:
8812 break;
8813
8814 default:
8815 return false;
8816 }
8817
8818 if (ctxt.stack.front().is_const())
8819 ctxt.accum = ctxt.stack.front();
8820
8821 next_index = index + 1;
8822 return true;
8823}
8824
8825/// This function quickly evaluates a DWARF expression that is a
8826/// constant.
8827///
8828/// This is a "fast path" function that quickly evaluates a DWARF
8829/// expression that is only made of a DW_OP_plus_uconst operator.
8830///
8831/// This is a sub-routine of die_member_offset.
8832///
8833/// @param expr the DWARF expression to evaluate.
8834///
8835/// @param expr_len the length of the expression @p expr.
8836///
8837/// @param value out parameter. This is set to the result of the
8838/// evaluation of @p expr, iff this function returns true.
8839///
8840/// @return true iff the evaluation of @p expr went OK.
8841static bool
8842eval_quickly(Dwarf_Op* expr,
8843 uint64_t expr_len,
8844 int64_t& value)
8845{
8846 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8847 {
8848 value = expr[0].number;
8849 return true;
8850 }
8851 return false;
8852}
8853
8854/// Evaluate the value of the last sub-expression that is a constant,
8855/// inside a given DWARF expression.
8856///
8857/// @param expr the DWARF expression to consider.
8858///
8859/// @param expr_len the length of the expression to consider.
8860///
8861/// @param value the resulting value of the last constant
8862/// sub-expression of the DWARF expression. This is set iff the
8863/// function returns true.
8864///
8865/// @param is_tls_address out parameter. This is set to true iff
8866/// the resulting value of the evaluation is a TLS (thread local
8867/// storage) address.
8868///
8869/// @param eval_ctxt the evaluation context to (re)use. Note that
8870/// this function initializes this context before using it.
8871///
8872/// @return true if the function could find a constant sub-expression
8873/// to evaluate, false otherwise.
8874static bool
8875eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8876 size_t expr_len,
8877 int64_t& value,
8878 bool& is_tls_address,
8879 dwarf_expr_eval_context &eval_ctxt)
8880{
8881 // Reset the evaluation context before evaluating the constant sub
8882 // expression contained in the DWARF expression 'expr'.
8883 eval_ctxt.reset();
8884
8885 size_t index = 0, next_index = 0;
8886 do
8887 {
8888 if (op_is_arith_logic(expr, expr_len, index,
8889 next_index, eval_ctxt)
8890 || op_pushes_constant_value(expr, expr_len, index,
8891 next_index, eval_ctxt)
8892 || op_manipulates_stack(expr, expr_len, index,
8893 next_index, eval_ctxt)
8894 || op_pushes_non_constant_value(expr, expr_len, index,
8895 next_index, eval_ctxt)
8896 || op_is_control_flow(expr, expr_len, index,
8897 next_index, eval_ctxt))
8898 ;
8899 else
8900 next_index = index + 1;
8901
8902 ABG_ASSERT(next_index > index);
8903 index = next_index;
8904 } while (index < expr_len);
8905
8906 is_tls_address = eval_ctxt.set_tls_address();
8907 if (eval_ctxt.accum.is_const())
8908 {
8909 value = eval_ctxt.accum;
8910 return true;
8911 }
8912 return false;
8913}
8914
8915/// Evaluate the value of the last sub-expression that is a constant,
8916/// inside a given DWARF expression.
8917///
8918/// @param expr the DWARF expression to consider.
8919///
8920/// @param expr_len the length of the expression to consider.
8921///
8922/// @param value the resulting value of the last constant
8923/// sub-expression of the DWARF expression. This is set iff the
8924/// function returns true.
8925///
8926/// @return true if the function could find a constant sub-expression
8927/// to evaluate, false otherwise.
8928static bool
8929eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8930 size_t expr_len,
8931 int64_t& value,
8932 bool& is_tls_address)
8933{
8934 dwarf_expr_eval_context eval_ctxt;
8935 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8936 is_tls_address, eval_ctxt);
8937}
8938
8939// -----------------------------------
8940// </location expression evaluation>
8941// -----------------------------------
8942
8943/// Convert a DW_AT_bit_offset attribute value into the same value as
8944/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8945///
8946/// On big endian machines, the value of the DW_AT_bit_offset
8947/// attribute + 8 * the value of the DW_AT_data_member_location
8948/// attribute is the same as the value of the DW_AT_data_bit_offset
8949/// attribute.
8950///
8951/// On little endian machines however, the situation is different.
8952/// The DW_AT_bit_offset value for a bit field is the number of bits
8953/// to the left of the most significant bit of the bit field, within
8954/// the integer value at DW_AT_data_member_location.
8955///
8956/// The DW_AT_data_bit_offset offset value is the number of bits to
8957/// the right of the least significant bit of the bit field, again
8958/// relative to the containing integer value.
8959///
8960/// In other words, DW_AT_data_bit_offset is what everybody would
8961/// instinctively think of as being the "offset of the bit field". 8 *
8962/// DW_AT_data_member_location + DW_AT_bit_offset however is very
8963/// counter-intuitive on little endian machines.
8964///
8965/// This function thus reads the value of a DW_AT_bit_offset property
8966/// of a DIE and converts it into what the DW_AT_data_bit_offset would
8967/// have been if it was present, ignoring the contribution of
8968/// DW_AT_data_member_location.
8969///
8970/// Note that DW_AT_bit_offset has been made obsolete starting from
8971/// DWARF5 (for GCC; Clang still emits it).
8972///
8973/// If you like coffee and it's not too late, now might be a good time
8974/// to have a coffee break. Otherwise if it's late at night, you
8975/// might want to consider an herbal tea break. Then come back to
8976/// read this.
8977///
8978///
8979/// In what follows, the bit fields are all contained within the first
8980/// whole int of the struct, so DW_AT_data_member_location is 0.
8981///
8982/// Okay, to have a better idea of what DW_AT_bit_offset and
8983/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8984/// have bit fields data members defined as:
8985///
8986/// struct S
8987/// {
8988/// int j:5;
8989/// int k:6;
8990/// int m:5;
8991/// int n:8;
8992/// };
8993///
8994/// The below wonderful (at least!) ASCII art sketch describes the
8995/// layout of the bitfields of 'struct S' on a little endian machine.
8996/// You need to read the sketch from the bottom-up.
8997///
8998/// So please scroll down to its bottom. Note how the 32 bits integer
8999/// word containing the bit fields is laid out with its least
9000/// significant bit starting on the right hand side, at index 0.
9001///
9002/// Then slowly scroll up starting from there, and take the time to
9003/// read each line and see how the bit fields are laid out and what
9004/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
9005/// the bit fields.
9006///
9007/// DW_AT_bit_offset(n)
9008/// < - - - - - - >
9009/// | | n |
9010/// ^ ^< - - - - >^
9011/// DW_AT_data_bit_offset(n)
9012/// < - - - - - - - - - - - - - - - >
9013/// | |
9014/// ^ ^
9015/// DW_AT_bit_offset(m)
9016/// <--------------------------------->
9017/// | | m |
9018/// ^ ^< - >^
9019/// DW_AT_data_bit_offset(m)
9020/// < - - - - - - - - - - >
9021/// | |
9022/// ^ ^
9023/// DW_AT_bit_offset(k)
9024/// <-------------------------------------------->
9025/// | | k |
9026/// ^ ^< - - >^
9027/// DW_AT_data_bit_offset(k)
9028/// < - - - - >
9029/// | |
9030/// ^ ^
9031/// DW_AT_bit_offset(j)
9032/// <-------------------------------------------------------->
9033/// | |
9034/// ^ ^
9035/// n m k j
9036/// < - - - - - - > < - - - > < - - - - > < - - - >
9037///
9038/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9039/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
9040/// 31 27 23 16 15 11 10 6 5 4 0
9041///
9042/// So, the different bit fields all fit in one 32 bits word, assuming
9043/// the bit fields are tightly packed.
9044///
9045/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
9046/// on this little endian machine and let's see how it relates to
9047/// DW_AT_data_bit_offset of j.
9048///
9049/// DW_AT_bit_offset(j) would be equal to the number of bits from the
9050/// left of the 32 bits word (i.e from bit number 31) to the most
9051/// significant bit of the j bit field (i.e, bit number 4). Thus:
9052///
9053/// DW_AT_bit_offset(j) =
9054/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
9055///
9056/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
9057/// 32 bits word (i.e, bit number 0) to the lest significant bit of
9058/// the 'j' bit field (ie, bit number 0). Thus:
9059///
9060/// DW_AT_data_bit_offset(j) = 0.
9061///
9062/// More generally, we can notice that:
9063///
9064/// sizeof_in_bits(int) =
9065/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
9066///
9067/// It follows that:
9068///
9069/// DW_AT_data_bit_offset(j) =
9070/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
9071///
9072/// Thus:
9073///
9074/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
9075///
9076/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
9077/// from the right hand side of the word. It is what we would
9078/// intuitively think it is. DW_AT_bit_offset however is super
9079/// counter-intuitive, pfff.
9080///
9081/// Anyway, this general equation holds true for all bit fields.
9082///
9083/// Similarly, it follows that:
9084///
9085/// DW_AT_bit_offset(k) =
9086/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
9087///
9088/// Thus:
9089/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
9090///
9091///
9092/// Likewise:
9093///
9094/// DW_AT_bit_offset(m) =
9095/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
9096///
9097///
9098/// Thus:
9099/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
9100///
9101/// And:
9102///
9103///
9104/// Lastly:
9105///
9106/// DW_AT_bit_offset(n) =
9107/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
9108///
9109/// Thus:
9110/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
9111///
9112/// Luckily, the body of the function is much smaller than this
9113/// comment. Enjoy!
9114///
9115/// @param die the DIE to consider.
9116///
9117/// @param is_big_endian this is true iff the machine we are looking at
9118/// is big endian.
9119///
9120/// @param offset this is the output parameter into which the value of
9121/// the DW_AT_bit_offset is put, converted as if it was the value of
9122/// the DW_AT_data_bit_offset parameter, less the contribution of
9123/// DW_AT_data_member_location. This parameter is set iff the
9124/// function returns true.
9125///
9126/// @return true if DW_AT_bit_offset was found on @p die.
9127static bool
9128read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
9129 bool is_big_endian,
9130 uint64_t &offset)
9131{
9132 uint64_t off = 0;
9133 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
9134 return false;
9135
9136 if (is_big_endian)
9137 {
9138 offset = off;
9139 return true;
9140 }
9141
9142 // Okay, we are looking at a little endian machine. We need to
9143 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
9144 // have been. To understand this, you really need to read the
9145 // preliminary comment of this function.
9146 uint64_t containing_anonymous_object_size = 0;
9147 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
9148 containing_anonymous_object_size));
9149 containing_anonymous_object_size *= 8;
9150
9151 uint64_t bitfield_size = 0;
9152 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
9153 bitfield_size));
9154
9155 // As noted in the the preliminary comment of this function if we
9156 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
9157 // its DW_AT_bit_offset value, the equation is:
9158 //
9159 // DW_AT_data_bit_offset(k) =
9160 // sizeof_in_bits(containing_anonymous_object_size)
9161 // - DW_AT_data_bit_offset(k)
9162 // - sizeof_in_bits(k)
9163 offset = containing_anonymous_object_size - off - bitfield_size;
9164
9165 return true;
9166}
9167
9168/// Get the value of the DW_AT_data_member_location of the given DIE
9169/// attribute as an constant.
9170///
9171/// @param die the DIE to read the attribute from.
9172///
9173/// @param offset the attribute as a constant value. This is set iff
9174/// the function returns true.
9175///
9176/// @return true if the attribute exists and has a constant value. In
9177/// that case the offset is set to the value.
9178static bool
9179die_constant_data_member_location(const Dwarf_Die *die,
9180 int64_t& offset)
9181{
9182 if (!die)
9183 return false;
9184
9185 Dwarf_Attribute attr;
9186 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
9187 DW_AT_data_member_location,
9188 &attr))
9189 return false;
9190
9191 Dwarf_Word val;
9192 if (dwarf_formudata(&attr, &val) != 0)
9193 return false;
9194
9195 offset = val;
9196 return true;
9197}
9198
9199/// Get the offset of a struct/class member as represented by the
9200/// value of the DW_AT_data_member_location attribute.
9201///
9202/// There is a huge gotcha in here. The value of the
9203/// DW_AT_data_member_location is not necessarily a constant that one
9204/// would just read and be done with it. Rather, it can be a DWARF
9205/// expression that one has to interpret. In general, the offset can
9206/// be given by the DW_AT_data_bit_offset or by the
9207/// DW_AT_data_member_location attribute and optionally the
9208/// DW_AT_bit_offset attribute. The bit offset attributes are
9209/// always simple constants, but the DW_AT_data_member_location
9210/// attribute is a DWARF location expression.
9211///
9212/// When it's the DW_AT_data_member_location that is present,
9213/// there are three cases to possibly take into account:
9214///
9215/// 1/ The offset in the vtable where the offset of a virtual base
9216/// can be found, aka vptr offset. Given the address of a
9217/// given object O, the vptr offset for B is given by the
9218/// (DWARF) expression:
9219///
9220/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
9221///
9222/// where VIRTUAL_OFFSET is a constant value; In this case,
9223/// this function returns the constant VIRTUAL_OFFSET, as this
9224/// is enough to detect changes in a given virtual base
9225/// relative to the other virtual bases.
9226///
9227/// 2/ The offset of a regular data member. Given the address of
9228/// a struct object named O, the memory location for a
9229/// particular data member is given by the (DWARF) expression:
9230///
9231/// address(O) + OFFSET
9232///
9233/// where OFFSET is a constant. In this case, this function
9234/// returns the OFFSET constant.
9235///
9236/// 3/ The offset of a virtual member function in the virtual
9237/// pointer. The DWARF expression is a constant that designates
9238/// the offset of the function in the vtable. In this case this
9239/// function returns that constant.
9240///
9241/// @param rdr the DWARF reader to consider.
9242///
9243/// @param die the DIE to read the information from.
9244///
9245/// @param offset the resulting constant offset, in bits. This
9246/// argument is set iff the function returns true.
9247static bool
9248die_member_offset(const reader& rdr,
9249 const Dwarf_Die* die,
9250 int64_t& offset)
9251{
9252 Dwarf_Op* expr = NULL;
9253 size_t expr_len = 0;
9254 uint64_t bit_offset = 0;
9255
9256 // First let's see if the DW_AT_data_bit_offset attribute is
9257 // present.
9258 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
9259 {
9260 offset = bit_offset;
9261 return true;
9262 }
9263
9264 // First try to read DW_AT_data_member_location as a plain constant.
9265 // We do this because the generic method using die_location_expr
9266 // might hit a bug in elfutils libdw dwarf_location_expression only
9267 // fixed in elfutils 0.184+. The bug only triggers if the attribute
9268 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
9269 // handle all constants here because that is more consistent (and
9270 // slightly faster in the general case where the attribute isn't a
9271 // full DWARF expression).
9272 if (!die_constant_data_member_location(die, offset))
9273 {
9274 // Otherwise, let's see if the DW_AT_data_member_location
9275 // attribute and, optionally, the DW_AT_bit_offset attributes
9276 // are present.
9277 if (!die_location_expr(die, DW_AT_data_member_location,
9278 &expr, &expr_len))
9279 return false;
9280
9281 // The DW_AT_data_member_location attribute is present. Let's
9282 // evaluate it and get its constant sub-expression and return
9283 // that one.
9284 if (!eval_quickly(expr, expr_len, offset))
9285 {
9286 bool is_tls_address = false;
9287 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
9288 offset, is_tls_address,
9289 rdr.dwarf_expr_eval_ctxt()))
9290 return false;
9291 }
9292 }
9293 offset *= 8;
9294
9295 // On little endian machines, we need to convert the
9296 // DW_AT_bit_offset attribute into a relative offset to 8 *
9297 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
9298 // would be if it were used instead.
9299 //
9300 // In other words, before adding it to 8 *
9301 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9302 // converted into a human-understandable form that represents the
9303 // offset of the bitfield data member it describes. For details
9304 // about the conversion, please read the extensive comments of
9305 // read_and_convert_DW_at_bit_offset.
9306 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9307 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9308 offset += bit_offset;
9309
9310 return true;
9311}
9312
9313/// Read the value of the DW_AT_location attribute from a DIE,
9314/// evaluate the resulting DWARF expression and, if it's a constant
9315/// expression, return it.
9316///
9317/// @param die the DIE to consider.
9318///
9319/// @param address the resulting constant address. This is set iff
9320/// the function returns true.
9321///
9322/// @return true iff the whole sequence of action described above
9323/// could be completed normally.
9324static bool
9325die_location_address(Dwarf_Die* die,
9326 Dwarf_Addr& address,
9327 bool& is_tls_address)
9328{
9329 Dwarf_Op* expr = NULL;
9330 size_t expr_len = 0;
9331
9332 is_tls_address = false;
9333
9334 if (!die)
9335 return false;
9336
9337 Dwarf_Attribute attr;
9338 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9339 return false;
9340
9341 if (dwarf_getlocation(&attr, &expr, &expr_len))
9342 return false;
9343 // Ignore location expressions where reading them succeeded but
9344 // their length is 0.
9345 if (expr_len == 0)
9346 return false;
9347
9348 Dwarf_Attribute result;
9349 if (!dwarf_getlocation_attr(&attr, expr, &result))
9350 // A location that has been interpreted as an address.
9351 return !dwarf_formaddr(&result, &address);
9352
9353 // Just get the address out of the number field.
9354 address = expr->number;
9355 return true;
9356}
9357
9358/// Return the index of a function in its virtual table. That is,
9359/// return the value of the DW_AT_vtable_elem_location attribute.
9360///
9361/// @param die the DIE of the function to consider.
9362///
9363/// @param vindex the resulting index. This is set iff the function
9364/// returns true.
9365///
9366/// @return true if the DIE has a DW_AT_vtable_elem_location
9367/// attribute.
9368static bool
9369die_virtual_function_index(Dwarf_Die* die,
9370 int64_t& vindex)
9371{
9372 if (!die)
9373 return false;
9374
9375 Dwarf_Op* expr = NULL;
9376 size_t expr_len = 0;
9377 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9378 &expr, &expr_len))
9379 return false;
9380
9381 int64_t i = 0;
9382 bool is_tls_addr = false;
9383 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9384 return false;
9385
9386 vindex = i;
9387 return true;
9388}
9389
9390/// Test if a given DIE represents an anonymous type.
9391///
9392/// Anonymous types we are interested in are classes, unions and
9393/// enumerations.
9394///
9395/// @param die the DIE to consider.
9396///
9397/// @return true iff @p die represents an anonymous type.
9398bool
9400{
9401 int tag = dwarf_tag(die);
9402
9403 if (tag == DW_TAG_class_type
9404 || tag == DW_TAG_structure_type
9405 || tag == DW_TAG_union_type
9406 || tag == DW_TAG_enumeration_type)
9407 return die_is_anonymous(die);
9408
9409 return false;
9410}
9411
9412/// Return the base of the internal name to represent an anonymous
9413/// type.
9414///
9415/// Typically, anonymous enums would be named
9416/// __anonymous_enum__<number>, anonymous struct or classes would be
9417/// named __anonymous_struct__<number> and anonymous unions would be
9418/// named __anonymous_union__<number>. The first part of these
9419/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9420/// the base name. This function returns that base name, depending on
9421/// the kind of type DIE we are looking at.
9422///
9423/// @param die the type DIE to look at. This function expects a type
9424/// DIE with an empty DW_AT_name property value (anonymous).
9425///
9426/// @return a string representing the base of the internal anonymous
9427/// name.
9428static string
9429get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9430{
9431 ABG_ASSERT(die_is_type(die));
9432 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9433
9434 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9435 string type_name;
9436 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9438 else if (tag == DW_TAG_union_type)
9440 else if (tag == DW_TAG_enumeration_type)
9442
9443 return type_name;
9444}
9445
9446/// Build a full internal anonymous type name.
9447///
9448/// @param base_name this is the base name as returned by the function
9449/// @ref get_internal_anonymous_die_prefix_name.
9450///
9451/// @param anonymous_type_index this is the index of the anonymous
9452/// type in its scope. That is, if there are more than one anonymous
9453/// types of a given kind in a scope, this index is what tells them
9454/// appart, starting from 0.
9455///
9456/// @return the built string, which is a concatenation of @p base_name
9457/// and @p anonymous_type_index.
9458static string
9459build_internal_anonymous_die_name(const string &base_name,
9460 size_t anonymous_type_index)
9461{
9462 string name = base_name;
9463 if (anonymous_type_index && !base_name.empty())
9464 {
9465 std::ostringstream o;
9466 o << base_name << anonymous_type_index;
9467 name = o.str();
9468 }
9469 return name;
9470}
9471
9472
9473/// Build a full internal anonymous type name.
9474///
9475/// @param die the DIE representing the anonymous type to consider.
9476///
9477/// @param anonymous_type_index the index of the anonymous type
9478/// represented by @p DIE, in its scope. That is, if there are
9479/// several different anonymous types of the same kind as @p die, this
9480/// index is what tells them appart.
9481///
9482/// @return the internal name of the anonymous type represented by @p
9483/// DIE.
9484static string
9485get_internal_anonymous_die_name(Dwarf_Die *die,
9486 size_t anonymous_type_index)
9487{
9488 string name = get_internal_anonymous_die_prefix_name(die);
9489 name = build_internal_anonymous_die_name(name, anonymous_type_index);
9490 return name;
9491}
9492
9493// ------------------------------------
9494// <DIE pretty printer>
9495// ------------------------------------
9496
9497/// Compute the qualified name of a DIE that represents a type.
9498///
9499/// For instance, if the DIE tag is DW_TAG_subprogram then this
9500/// function computes the name of the function *type*.
9501///
9502/// @param rdr the DWARF reader.
9503///
9504/// @param die the DIE to consider.
9505///
9506/// @param where_offset where in the are logically are in the DIE
9507/// stream.
9508///
9509/// @return a copy of the qualified name of the type.
9510static string
9511die_qualified_type_name(const reader& rdr,
9512 const Dwarf_Die* die,
9513 size_t where_offset)
9514{
9515 if (!die)
9516 return "";
9517
9518 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9519 if (tag == DW_TAG_compile_unit
9520 || tag == DW_TAG_partial_unit
9521 || tag == DW_TAG_type_unit)
9522 return "";
9523
9524 string name = die_name(die);
9525
9526 Dwarf_Die scope_die;
9527 if (!get_scope_die(rdr, die, where_offset, scope_die))
9528 return "";
9529
9530 string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
9531 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9532 string separator = colon_colon ? "::" : ".";
9533
9534 string repr;
9535
9536 switch (tag)
9537 {
9538 case DW_TAG_unspecified_type:
9539 break;
9540
9541 case DW_TAG_base_type:
9542 {
9544 if (parse_integral_type(name, int_type))
9545 repr = int_type;
9546 else
9547 repr = name;
9548 }
9549 break;
9550
9551 case DW_TAG_typedef:
9552 case DW_TAG_enumeration_type:
9553 case DW_TAG_structure_type:
9554 case DW_TAG_class_type:
9555 case DW_TAG_union_type:
9556 {
9557 if (name.empty())
9558 // TODO: handle cases where there are more than one
9559 // anonymous type of the same kind in the same scope. In
9560 // that case, their name must be built with the function
9561 // get_internal_anonymous_die_name or something of the same
9562 // kind.
9563 name = get_internal_anonymous_die_prefix_name(die);
9564
9565 ABG_ASSERT(!name.empty());
9566 repr = parent_name.empty() ? name : parent_name + separator + name;
9567 }
9568 break;
9569
9570 case DW_TAG_const_type:
9571 case DW_TAG_volatile_type:
9572 case DW_TAG_restrict_type:
9573 {
9574 Dwarf_Die underlying_type_die;
9575 bool has_underlying_type_die =
9576 die_die_attribute(die, DW_AT_type, underlying_type_die);
9577
9578 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9579 break;
9580
9581 if (tag == DW_TAG_const_type)
9582 {
9583 if (has_underlying_type_die
9584 && die_is_reference_type(&underlying_type_die))
9585 // A reference is always const. So, to lower false
9586 // positive reports in diff computations, we consider a
9587 // const reference just as a reference. But we need to
9588 // keep the qualified-ness of the type. So we introduce
9589 // a 'no-op' qualifier here. Please remember that this
9590 // has to be kept in sync with what is done in
9591 // get_name_of_qualified_type. So if you change this
9592 // here, you have to change that code there too.
9593 repr = "";
9594 else if (!has_underlying_type_die
9595 || die_is_void_type(&underlying_type_die))
9596 {
9597 repr = "void";
9598 break;
9599 }
9600 else
9601 repr = "const";
9602 }
9603 else if (tag == DW_TAG_volatile_type)
9604 repr = "volatile";
9605 else if (tag == DW_TAG_restrict_type)
9606 repr = "restrict";
9607 else
9609
9610 string underlying_type_repr;
9611 if (has_underlying_type_die)
9612 underlying_type_repr =
9613 die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9614 else
9615 underlying_type_repr = "void";
9616
9617 if (underlying_type_repr.empty())
9618 repr.clear();
9619 else
9620 {
9621 if (has_underlying_type_die)
9622 {
9623 Dwarf_Die peeled;
9624 die_peel_qualified(&underlying_type_die, peeled);
9625 if (die_is_pointer_or_reference_type(&peeled))
9626 repr = underlying_type_repr + " " + repr;
9627 else
9628 repr += " " + underlying_type_repr;
9629 }
9630 else
9631 repr += " " + underlying_type_repr;
9632 }
9633 }
9634 break;
9635
9636 case DW_TAG_pointer_type:
9637 case DW_TAG_reference_type:
9638 case DW_TAG_rvalue_reference_type:
9639 {
9640 Dwarf_Die pointed_to_type_die;
9641 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9642 {
9643 if (tag == DW_TAG_pointer_type)
9644 repr = "void*";
9645 break;
9646 }
9647
9648 if (die_is_unspecified(&pointed_to_type_die))
9649 break;
9650
9651 string pointed_type_repr =
9652 die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9653
9654 repr = pointed_type_repr;
9655 if (repr.empty())
9656 break;
9657
9658 if (tag == DW_TAG_pointer_type)
9659 repr += "*";
9660 else if (tag == DW_TAG_reference_type)
9661 repr += "&";
9662 else if (tag == DW_TAG_rvalue_reference_type)
9663 repr += "&&";
9664 else
9666 }
9667 break;
9668
9669 case DW_TAG_subrange_type:
9670 {
9671 // In Ada, this one can be generated on its own, that is, not
9672 // as a sub-type of an array. So we need to support it on its
9673 // own. Note that when it's emitted as the sub-type of an
9674 // array like in C and C++, this is handled differently, for
9675 // now. But we try to make this usable by other languages
9676 // that are not Ada, even if we modelled it after Ada.
9677
9678 // So we build a subrange type for the sole purpose of using
9679 // the ::as_string() method of that type. So we don't add
9680 // that type to the current type tree being built.
9682 build_subrange_type(const_cast<reader&>(rdr),
9683 die, where_offset,
9684 /*associate_die_to_type=*/false);
9685 repr += s->as_string();
9686 break;
9687 }
9688
9689 case DW_TAG_array_type:
9690 {
9691 Dwarf_Die element_type_die;
9692 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9693 break;
9694 string element_type_name =
9695 die_qualified_type_name(rdr, &element_type_die, where_offset);
9696 if (element_type_name.empty())
9697 break;
9698
9700 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9701 die, subranges, where_offset,
9702 /*associate_type_to_die=*/false);
9703
9704 repr = element_type_name;
9705 repr += array_type_def::subrange_type::vector_as_string(subranges);
9706 }
9707 break;
9708
9709 case DW_TAG_subroutine_type:
9710 case DW_TAG_subprogram:
9711 {
9712 string return_type_name;
9713 string class_name;
9714 vector<string> parm_names;
9715 bool is_const = false;
9716 bool is_static = false;
9717
9718 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9719 /*pretty_print=*/true,
9720 return_type_name, class_name,
9721 parm_names, is_const,
9722 is_static);
9723 if (return_type_name.empty())
9724 return_type_name = "void";
9725
9726 repr = return_type_name;
9727
9728 if (!class_name.empty())
9729 {
9730 // This is a method, so print the class name.
9731 repr += " (" + class_name + "::*)";
9732 }
9733
9734 // Now parameters.
9735 repr += " (";
9736 for (vector<string>::const_iterator i = parm_names.begin();
9737 i != parm_names.end();
9738 ++i)
9739 {
9740 if (i != parm_names.begin())
9741 repr += ", ";
9742 repr += *i;
9743 }
9744 repr += ")";
9745
9746 }
9747 break;
9748
9749 case DW_TAG_string_type:
9750 case DW_TAG_ptr_to_member_type:
9751 case DW_TAG_set_type:
9752 case DW_TAG_file_type:
9753 case DW_TAG_packed_type:
9754 case DW_TAG_thrown_type:
9755 case DW_TAG_interface_type:
9756 case DW_TAG_shared_type:
9757 break;
9758 }
9759
9760 return repr;
9761}
9762
9763/// Compute the qualified name of a decl represented by a given DIE.
9764///
9765/// For instance, for a DIE of tag DW_TAG_subprogram this function
9766/// computes the signature of the function *declaration*.
9767///
9768/// @param rdr the DWARF reader.
9769///
9770/// @param die the DIE to consider.
9771///
9772/// @param where_offset where we are logically at in the DIE stream.
9773///
9774/// @return a copy of the computed name.
9775static string
9776die_qualified_decl_name(const reader& rdr,
9777 const Dwarf_Die* die,
9778 size_t where_offset)
9779{
9780 if (!die || !die_is_decl(die))
9781 return "";
9782
9783 string name = die_name(die);
9784
9785 Dwarf_Die scope_die;
9786 if (!get_scope_die(rdr, die, where_offset, scope_die))
9787 return "";
9788
9789 string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9790 string separator = "::";
9791
9792 string repr;
9793
9794 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9795 switch (tag)
9796 {
9797 case DW_TAG_namespace:
9798 case DW_TAG_member:
9799 case DW_TAG_variable:
9800 repr = scope_name.empty() ? name : scope_name + separator + name;
9801 break;
9802 case DW_TAG_subprogram:
9803 repr = die_function_signature(rdr, die, where_offset);
9804 break;
9805
9806 case DW_TAG_unspecified_parameters:
9807 repr = "...";
9808 break;
9809
9810 case DW_TAG_formal_parameter:
9811 case DW_TAG_imported_declaration:
9812 case DW_TAG_GNU_template_template_param:
9813 case DW_TAG_GNU_template_parameter_pack:
9814 case DW_TAG_GNU_formal_parameter_pack:
9815 break;
9816 }
9817 return repr;
9818}
9819
9820/// Compute the qualified name of the artifact represented by a given
9821/// DIE.
9822///
9823/// If the DIE represents a type, then the function computes the name
9824/// of the type. Otherwise, if the DIE represents a decl then the
9825/// function computes the name of the decl. Note that a DIE of tag
9826/// DW_TAG_subprogram is going to be considered as a "type" -- just
9827/// like if it was a DW_TAG_subroutine_type.
9828///
9829/// @param rdr the DWARF reader.
9830///
9831/// @param die the DIE to consider.
9832///
9833/// @param where_offset where we are logically at in the DIE stream.
9834///
9835/// @return a copy of the computed name.
9836static string
9837die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9838{
9839 if (die_is_type(die))
9840 return die_qualified_type_name(rdr, die, where);
9841 else if (die_is_decl(die))
9842 return die_qualified_decl_name(rdr, die, where);
9843 return "";
9844}
9845
9846/// Test if the qualified name of a given type should be empty.
9847///
9848/// The reason why the name of a DIE with a given tag would be empty
9849/// is that libabigail's internal representation doesn't yet support
9850/// that tag; or if the DIE's qualified name is built from names of
9851/// sub-types DIEs whose tags are not yet supported.
9852///
9853/// @param rdr the DWARF reader.
9854///
9855/// @param die the DIE to consider.
9856///
9857/// @param where where we are logically at, in the DIE stream.
9858///
9859/// @param qualified_name the qualified name of the DIE. This is set
9860/// only iff the function returns false.
9861///
9862/// @return true if the qualified name of the DIE is empty.
9863static bool
9864die_qualified_type_name_empty(const reader& rdr,
9865 const Dwarf_Die* die,
9866 size_t where, string &qualified_name)
9867{
9868 if (!die)
9869 return true;
9870
9871 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9872
9873 string qname;
9874 if (tag == DW_TAG_typedef
9875 || tag == DW_TAG_pointer_type
9876 || tag == DW_TAG_reference_type
9877 || tag == DW_TAG_rvalue_reference_type
9878 || tag == DW_TAG_array_type
9879 || tag == DW_TAG_const_type
9880 || tag == DW_TAG_volatile_type
9881 || tag == DW_TAG_restrict_type)
9882 {
9883 Dwarf_Die underlying_type_die;
9884 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9885 {
9886 string name =
9887 die_qualified_type_name(rdr, &underlying_type_die, where);
9888 if (name.empty())
9889 return true;
9890 }
9891 }
9892 else
9893 {
9894 string name = die_qualified_type_name(rdr, die, where);
9895 if (name.empty())
9896 return true;
9897 }
9898
9899 qname = die_qualified_type_name(rdr, die, where);
9900 if (qname.empty())
9901 return true;
9902
9903 qualified_name = qname;
9904 return false;
9905}
9906
9907/// Given the DIE that represents a function type, compute the names
9908/// of the following properties the function's type:
9909///
9910/// - return type
9911/// - enclosing class (if the function is a member function)
9912/// - function parameter types
9913///
9914/// When the function we are looking at is a member function, it also
9915/// tells if it's const.
9916///
9917/// @param rdr the DWARF reader.
9918///
9919/// @param die the DIE of the function or function type we are looking
9920/// at.
9921///
9922/// @param where_offset where we are logically at in the DIE stream.
9923///
9924/// @param pretty_print if set to yes, the type names are going to be
9925/// pretty-printed names; otherwise, they are just qualified type
9926/// names.
9927///
9928/// @param return_type_name out parameter. This contains the name of
9929/// the return type of the function.
9930///
9931/// @param class_name out parameter. If the function is a member
9932/// function, this contains the name of the enclosing class.
9933///
9934/// @param parm_names out parameter. This vector is set to the names
9935/// of the types of the parameters of the function.
9936///
9937/// @param is_const out parameter. If the function is a member
9938/// function, this is set to true iff the member function is const.
9939///
9940/// @param is_static out parameter. If the function is a static
9941/// member function, then this is set to true.
9942static void
9943die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9944 const Dwarf_Die* die,
9945 size_t where_offset,
9946 bool pretty_print,
9947 string &return_type_name,
9948 string &class_name,
9949 vector<string>& parm_names,
9950 bool& is_const,
9951 bool& is_static)
9952{
9953 Dwarf_Die child;
9954 Dwarf_Die ret_type_die;
9955 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9956 return_type_name = "void";
9957 else
9958 return_type_name =
9959 pretty_print
9960 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9961 : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9962
9963 if (return_type_name.empty())
9964 return_type_name = "void";
9965
9966 Dwarf_Die object_pointer_die, class_die;
9967 bool is_method_type =
9968 die_function_type_is_method_type(rdr, die, where_offset,
9969 object_pointer_die,
9970 class_die, is_static);
9971
9972 is_const = false;
9973 if (is_method_type)
9974 {
9975 class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9976
9977 Dwarf_Die this_pointer_die;
9978 Dwarf_Die pointed_to_die;
9979 if (!is_static
9980 && die_die_attribute(&object_pointer_die, DW_AT_type,
9981 this_pointer_die))
9982 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9983 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9984 is_const = true;
9985
9986 string fn_name = die_name(die);
9987 string non_qualified_class_name = die_name(&class_die);
9988 bool is_ctor = fn_name == non_qualified_class_name;
9989 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9990
9991 if (is_ctor || is_dtor)
9992 return_type_name.clear();
9993 }
9994
9995 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9996 do
9997 {
9998 int child_tag = dwarf_tag(&child);
9999 if (child_tag == DW_TAG_formal_parameter)
10000 {
10001 Dwarf_Die parm_type_die;
10002 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
10003 continue;
10004 string qualified_name =
10005 pretty_print
10006 ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
10007 : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
10008
10009 if (qualified_name.empty())
10010 continue;
10011 parm_names.push_back(qualified_name);
10012 }
10013 else if (child_tag == DW_TAG_unspecified_parameters)
10014 {
10015 // This is a variadic function parameter.
10016 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
10017 // After a DW_TAG_unspecified_parameters tag, we shouldn't
10018 // keep reading for parameters. The
10019 // unspecified_parameters TAG should be the last parameter
10020 // that we record. For instance, if there are multiple
10021 // DW_TAG_unspecified_parameters DIEs then we should care
10022 // only for the first one.
10023 break;
10024 }
10025 }
10026 while (dwarf_siblingof(&child, &child) == 0);
10027
10028 if (class_name.empty())
10029 {
10030 Dwarf_Die parent_die;
10031 if (get_parent_die(rdr, die, parent_die, where_offset))
10032 {
10033 if (die_is_class_type(&parent_die))
10034 class_name =
10035 rdr.get_die_qualified_type_name(&parent_die, where_offset);
10036 }
10037 }
10038}
10039
10040/// This computes the signature of the a function declaration
10041/// represented by a DIE.
10042///
10043/// @param rdr the DWARF reader.
10044///
10045/// @param fn_die the DIE of the function to consider.
10046///
10047/// @param where_offset where we are logically at in the stream of
10048/// DIEs.
10049///
10050/// @return a copy of the computed function signature string.
10051static string
10052die_function_signature(const reader& rdr,
10053 const Dwarf_Die *fn_die,
10054 size_t where_offset)
10055{
10056
10058 bool has_lang = false;
10059 if ((has_lang = get_die_language(fn_die, lang)))
10060 {
10061 // In a binary originating from the C language, it's OK to use
10062 // the linkage name of the function as a key for the map which
10063 // is meant to reduce the number of DIE comparisons involved
10064 // during DIE canonicalization computation.
10065 if (is_c_language(lang))
10066 {
10067 string fn_name = die_linkage_name(fn_die);
10068 if (fn_name.empty())
10069 fn_name = die_name(fn_die);
10070 return fn_name;
10071 }
10072 }
10073
10074 // TODO: When we can structurally compare DIEs originating from C++
10075 // as well, we can use the linkage name of functions in C++ too, to
10076 // reduce the number of comparisons involved during DIE
10077 // canonicalization.
10078
10079 string return_type_name;
10080 Dwarf_Die ret_type_die;
10081 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
10082 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
10083 where_offset);
10084
10085 if (return_type_name.empty())
10086 return_type_name = "void";
10087
10088 Dwarf_Die scope_die;
10089 string scope_name;
10090 if (get_scope_die(rdr, fn_die, where_offset, scope_die))
10091 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
10092 string fn_name = die_name(fn_die);
10093 if (!scope_name.empty())
10094 fn_name = scope_name + "::" + fn_name;
10095
10096 string class_name;
10097 vector<string> parm_names;
10098 bool is_const = false;
10099 bool is_static = false;
10100
10101 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
10102 /*pretty_print=*/false,
10103 return_type_name, class_name,
10104 parm_names, is_const, is_static);
10105
10106 bool is_virtual = die_is_virtual(fn_die);
10107
10108 string repr = class_name.empty() ? "function" : "method";
10109 if (is_virtual)
10110 repr += " virtual";
10111
10112 if (!return_type_name.empty())
10113 repr += " " + return_type_name;
10114
10115 repr += " " + fn_name;
10116
10117 // Now parameters.
10118 repr += "(";
10119 bool some_parm_emitted = false;
10120 for (vector<string>::const_iterator i = parm_names.begin();
10121 i != parm_names.end();
10122 ++i)
10123 {
10124 if (i != parm_names.begin())
10125 {
10126 if (some_parm_emitted)
10127 repr += ", ";
10128 }
10129 else
10130 if (!is_static && !class_name.empty())
10131 // We are printing a non-static method name, skip the implicit "this"
10132 // parameter type.
10133 continue;
10134 repr += *i;
10135 some_parm_emitted = true;
10136 }
10137 repr += ")";
10138
10139 if (is_const)
10140 {
10141 ABG_ASSERT(!class_name.empty());
10142 repr += " const";
10143 }
10144
10145 return repr;
10146}
10147
10148/// Return a pretty string representation of a type, for internal purposes.
10149///
10150/// By internal purpose, we mean things like key-ing types for lookup
10151/// purposes and so on.
10152///
10153/// Note that this function is also used to pretty print functions.
10154/// For functions, it prints the *type* of the function.
10155///
10156/// @param rdr the context to use.
10157///
10158/// @param the DIE of the type to pretty print.
10159///
10160/// @param where_offset where we logically are placed when calling
10161/// this. It's useful to handle inclusion of DW_TAG_compile_unit
10162/// entries.
10163///
10164/// @return the resulting pretty representation.
10165static string
10166die_pretty_print_type(reader& rdr,
10167 const Dwarf_Die* die,
10168 size_t where_offset)
10169{
10170 if (!die
10171 || (!die_is_type(die)
10172 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
10173 return "";
10174
10175 string repr;
10176
10177 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10178 switch (tag)
10179 {
10180 case DW_TAG_string_type:
10181 // For now, we won't try to go get the actual representation of
10182 // the string because this would make things more complicated;
10183 // for that we'd need to interpret some location expressions to
10184 // get the length of the string. And for dynamically allocated
10185 // strings, the result of the location expression evaluation
10186 // might not even be a constant. So at the moment I consider
10187 // this to be a lot of hassle for no great return. Until proven
10188 // otherwise, of course.
10189 repr = "string type";
10190
10191 case DW_TAG_unspecified_type:
10192 case DW_TAG_ptr_to_member_type:
10193 break;
10194
10195 case DW_TAG_namespace:
10196 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
10197 break;
10198
10199 case DW_TAG_base_type:
10200 repr = rdr.get_die_qualified_type_name(die, where_offset);
10201 break;
10202
10203 case DW_TAG_typedef:
10204 {
10205 string qualified_name;
10206 if (!die_qualified_type_name_empty(rdr, die,
10207 where_offset,
10208 qualified_name))
10209 repr = "typedef " + qualified_name;
10210 }
10211 break;
10212
10213 case DW_TAG_const_type:
10214 case DW_TAG_volatile_type:
10215 case DW_TAG_restrict_type:
10216 case DW_TAG_pointer_type:
10217 case DW_TAG_reference_type:
10218 case DW_TAG_rvalue_reference_type:
10219 repr = rdr.get_die_qualified_type_name(die, where_offset);
10220 break;
10221
10222 case DW_TAG_enumeration_type:
10223 {
10224 string qualified_name =
10225 rdr.get_die_qualified_type_name(die, where_offset);
10226 repr = "enum " + qualified_name;
10227 }
10228 break;
10229
10230 case DW_TAG_structure_type:
10231 case DW_TAG_class_type:
10232 {
10233 string qualified_name =
10234 rdr.get_die_qualified_type_name(die, where_offset);
10235 repr = "class " + qualified_name;
10236 }
10237 break;
10238
10239 case DW_TAG_union_type:
10240 {
10241 string qualified_name =
10242 rdr.get_die_qualified_type_name(die, where_offset);
10243 repr = "union " + qualified_name;
10244 }
10245 break;
10246
10247 case DW_TAG_array_type:
10248 {
10249 Dwarf_Die element_type_die;
10250 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10251 break;
10252 string element_type_name =
10253 rdr.get_die_qualified_type_name(&element_type_die, where_offset);
10254 if (element_type_name.empty())
10255 break;
10256
10258 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
10259 /*associate_type_to_die=*/false);
10260
10261 repr = element_type_name;
10262 repr += array_type_def::subrange_type::vector_as_string(subranges);
10263 }
10264 break;
10265
10266 case DW_TAG_subrange_type:
10267 {
10268 // So this can be generated by Ada, on its own; that is, not
10269 // as a subtype of an array. In that case we need to handle
10270 // it properly.
10271
10272 // For now, we consider that the pretty printed name of the
10273 // subrange type is its name. We might need something more
10274 // advance, should the needs of the users get more
10275 // complicated.
10276 repr += die_qualified_type_name(rdr, die, where_offset);
10277 }
10278 break;
10279
10280 case DW_TAG_subroutine_type:
10281 case DW_TAG_subprogram:
10282 {
10283 string return_type_name;
10284 string class_name;
10285 vector<string> parm_names;
10286 bool is_const = false;
10287 bool is_static = false;
10288
10289 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10290 /*pretty_print=*/true,
10291 return_type_name, class_name,
10292 parm_names, is_const,
10293 is_static);
10294 if (class_name.empty())
10295 repr = "function type";
10296 else
10297 repr = "method type";
10298 repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
10299 }
10300 break;
10301
10302 case DW_TAG_set_type:
10303 case DW_TAG_file_type:
10304 case DW_TAG_packed_type:
10305 case DW_TAG_thrown_type:
10306 case DW_TAG_interface_type:
10307 case DW_TAG_shared_type:
10309 }
10310
10311 return repr;
10312}
10313
10314/// Return a pretty string representation of a declaration, for
10315/// internal purposes.
10316///
10317/// By internal purpose, we mean things like key-ing declarations for
10318/// lookup purposes and so on.
10319///
10320/// Note that this function is also used to pretty print functions.
10321/// For functions, it prints the signature of the function.
10322///
10323/// @param rdr the context to use.
10324///
10325/// @param the DIE of the declaration to pretty print.
10326///
10327/// @param where_offset where we logically are placed when calling
10328/// this. It's useful to handle inclusion of DW_TAG_compile_unit
10329/// entries.
10330///
10331/// @return the resulting pretty representation.
10332static string
10333die_pretty_print_decl(reader& rdr,
10334 const Dwarf_Die* die,
10335 size_t where_offset)
10336{
10337 if (!die || !die_is_decl(die))
10338 return "";
10339
10340 string repr;
10341
10342 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10343 switch (tag)
10344 {
10345 case DW_TAG_namespace:
10346 repr = "namespace " + die_qualified_name(rdr, die, where_offset);
10347 break;
10348
10349 case DW_TAG_member:
10350 case DW_TAG_variable:
10351 {
10352 string type_repr = "void";
10353 Dwarf_Die type_die;
10354 if (die_die_attribute(die, DW_AT_type, type_die))
10355 type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
10356 repr = die_qualified_name(rdr, die, where_offset);
10357 if (!repr.empty())
10358 repr = type_repr + " " + repr;
10359 }
10360 break;
10361
10362 case DW_TAG_subprogram:
10363 repr = die_function_signature(rdr, die, where_offset);
10364 break;
10365
10366 default:
10367 break;
10368 }
10369 return repr;
10370}
10371
10372/// Compute the pretty printed representation of an artifact
10373/// represented by a DIE.
10374///
10375/// If the DIE is a type, compute the its pretty representation as a
10376/// type; otherwise, if it's a declaration, compute its pretty
10377/// representation as a declaration. Note for For instance, that a
10378/// DW_TAG_subprogram DIE is going to be represented as a function
10379/// *type*.
10380///
10381/// @param rdr the DWARF reader.
10382///
10383/// @param die the DIE to consider.
10384///
10385/// @param where_offset we in the DIE stream we are logically at.
10386///
10387/// @return a copy of the pretty printed artifact.
10388static string
10389die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
10390{
10391 if (die_is_type(die))
10392 return die_pretty_print_type(rdr, die, where_offset);
10393 else if (die_is_decl(die))
10394 return die_pretty_print_decl(rdr, die, where_offset);
10395 return "";
10396}
10397
10398// -----------------------------------
10399// </die pretty printer>
10400// -----------------------------------
10401
10402
10403// ----------------------------------
10404// <die comparison engine>
10405// ---------------------------------
10406
10407/// Compares two decls DIEs
10408///
10409/// This works only for DIEs emitted by the C language.
10410///
10411/// This implementation doesn't yet support namespaces.
10412///
10413/// This is a subroutine of compare_dies.
10414///
10415/// @return true iff @p l equals @p r.
10416static bool
10417compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10418{
10419 ABG_ASSERT(l && r);
10420
10421 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10422 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10423 if (l_tag != r_tag)
10424 return false;
10425
10426 bool result = false;
10427
10428 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
10429 {
10430 // Fast path for functions and global variables.
10431 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
10432 result)
10433 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
10434 result))
10435 {
10436 if (!result)
10437 return false;
10438 }
10439
10440 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10441 result))
10442 {
10443 if (!result)
10444 return false;
10445 }
10446 return true;
10447 }
10448
10449 // Fast path for types.
10450 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10451 result))
10452 return result;
10453 return true;
10454}
10455
10456/// Test if at least one of two ODR-relevant DIEs is decl-only.
10457///
10458/// @param rdr the DWARF reader to consider.
10459///
10460/// @param l the first type DIE to consider.
10461///
10462/// @param r the second type DIE to consider.
10463///
10464/// @return true iff either @p l or @p r is decl-only and both are
10465/// ODR-relevant.
10466static bool
10467at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
10468 const Dwarf_Die *l,
10469 const Dwarf_Die *r)
10470{
10471 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
10472 return false;
10473
10474 if ((die_is_declaration_only(l) && die_has_no_child(l))
10475 || (die_is_declaration_only(r) && die_has_no_child(r)))
10476 return true;
10477 return false;
10478}
10479
10480/// Compares two type DIEs
10481///
10482/// This is a subroutine of compare_dies.
10483///
10484/// Note that this function doesn't look at the name of the DIEs.
10485/// Naming is taken into account by the function compare_as_decl_dies.
10486///
10487/// If the two DIEs are from a translation unit that is subject to the
10488/// ONE Definition Rule, then the function considers that if one DIE
10489/// is a declaration, then it's equivalent to the second. In that
10490/// case, the sizes of the two DIEs are not compared. This is so that
10491/// a declaration of a type compares equal to the definition of the
10492/// type.
10493///
10494/// @param rdr the DWARF reader to consider.
10495///
10496/// @param l the left operand of the comparison operator.
10497///
10498/// @param r the right operand of the comparison operator.
10499///
10500/// @return true iff @p l equals @p r.
10501static bool
10502compare_as_type_dies(const reader& rdr,
10503 const Dwarf_Die *l,
10504 const Dwarf_Die *r)
10505{
10506 ABG_ASSERT(l && r);
10507 ABG_ASSERT(die_is_type(l));
10508 ABG_ASSERT(die_is_type(r));
10509
10510 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10511 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10512 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10513 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10514 // For now, we cannot compare DW_TAG_string_type because of its
10515 // string_length attribute that is a location descriptor that is
10516 // not necessarily a constant. So it's super hard to evaluate it
10517 // in a libabigail context. So for now, we just say that all
10518 // DW_TAG_string_type DIEs are different, by default.
10519 return false;
10520
10521 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10522 // A declaration of a type compares equal to the definition of the
10523 // type.
10524 return true;
10525
10526 uint64_t l_size = 0, r_size = 0;
10527 die_size_in_bits(l, l_size);
10528 die_size_in_bits(r, r_size);
10529
10530 return l_size == r_size;
10531}
10532
10533/// Compare two DIEs as decls (looking as their names etc) and as
10534/// types (looking at their size etc).
10535///
10536/// @param rdr the DWARF reader to consider.
10537///
10538/// @param l the first DIE to consider.
10539///
10540/// @param r the second DIE to consider.
10541///
10542/// @return TRUE iff @p l equals @p r as far as naming and size is
10543/// concerned.
10544static bool
10545compare_as_decl_and_type_dies(const reader &rdr,
10546 const Dwarf_Die *l,
10547 const Dwarf_Die *r)
10548{
10549 if (!compare_as_decl_dies(l, r)
10550 || !compare_as_type_dies(rdr, l, r))
10551 return false;
10552
10553 return true;
10554}
10555
10556/// Test if two DIEs representing function declarations have the same
10557/// linkage name, and thus are considered equal if they are C or C++,
10558/// because the two DIEs represent functions in the same binary.
10559///
10560/// If the DIEs don't have a linkage name, the function compares their
10561/// name. But in that case, the caller of the function must know that
10562/// in C++ for instance, that doesn't imply that the two functions are
10563/// equal.
10564///
10565/// @param l the first function DIE to consider.
10566///
10567/// @param r the second function DIE to consider.
10568///
10569/// @return true iff the function represented by @p l have the same
10570/// linkage name as the function represented by @p r.
10571static bool
10572fn_die_equal_by_linkage_name(const Dwarf_Die *l,
10573 const Dwarf_Die *r)
10574{
10575 if (!!l != !!r)
10576 return false;
10577
10578 if (!l)
10579 return false;
10580
10581 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10582 ABG_ASSERT(tag == DW_TAG_subprogram);
10583 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10584 ABG_ASSERT(tag == DW_TAG_subprogram);
10585
10586 string lname = die_name(l), rname = die_name(r);
10587 string llinkage_name = die_linkage_name(l),
10588 rlinkage_name = die_linkage_name(r);
10589
10590 if (die_is_in_c_or_cplusplus(l)
10591 && die_is_in_c_or_cplusplus(r))
10592 {
10593 if (!llinkage_name.empty() && !rlinkage_name.empty())
10594 return llinkage_name == rlinkage_name;
10595 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10596 return false;
10597 else
10598 return lname == rname;
10599 }
10600
10601 return (!llinkage_name.empty()
10602 && !rlinkage_name.empty()
10603 && llinkage_name == rlinkage_name);
10604}
10605
10606/// Compare two DIEs in the context of DIE canonicalization.
10607///
10608/// If DIE canonicalization is on, the function compares the DIEs
10609/// canonically and structurally. The two types of comparison should
10610/// be equal, of course.
10611///
10612/// @param rdr the DWARF reader.
10613///
10614/// @param l_offset the offset of the first canonical DIE to compare.
10615///
10616/// @param r_offset the offset of the second canonical DIE to compare.
10617///
10618/// @param l_die_source the source of the DIE denoted by the offset @p
10619/// l_offset.
10620///
10621/// @param r_die_source the source of the DIE denoted by the offset @p
10622/// r_offset.
10623///
10624/// @param l_has_canonical_die_offset output parameter. Is set to
10625/// true if @p l_offset has a canonical DIE.
10626///
10627/// @param r_has_canonical_die_offset output parameter. Is set to
10628/// true if @p r_offset has a canonical DIE.
10629///
10630/// @param l_canonical_die_offset output parameter. If @p
10631/// l_has_canonical_die_offset is set to true, then this parameter is
10632/// set to the offset of the canonical DIE of the DIE designated by @p
10633/// l_offset.
10634static bool
10635try_canonical_die_comparison(const reader& rdr,
10636 Dwarf_Off l_offset, Dwarf_Off r_offset,
10637 die_source l_die_source, die_source r_die_source,
10638 bool& l_has_canonical_die_offset,
10639 bool& r_has_canonical_die_offset,
10640 Dwarf_Off& l_canonical_die_offset,
10641 Dwarf_Off& r_canonical_die_offset,
10642 bool& result)
10643{
10644#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10645 if (rdr.debug_die_canonicalization_is_on_
10646 && !rdr.use_canonical_die_comparison_)
10647 return false;
10648#endif
10649
10650
10651 l_has_canonical_die_offset =
10652 (l_canonical_die_offset =
10653 rdr.get_canonical_die_offset(l_offset, l_die_source,
10654 /*die_as_type=*/true));
10655
10656 r_has_canonical_die_offset =
10657 (r_canonical_die_offset =
10658 rdr.get_canonical_die_offset(r_offset, r_die_source,
10659 /*die_as_type=*/true));
10660
10661 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10662 {
10663 result = (l_canonical_die_offset == r_canonical_die_offset);
10664 return true;
10665 }
10666
10667 return false;
10668}
10669
10670#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10671/// This function is called whenever a DIE comparison fails.
10672///
10673/// This function is intended for debugging purposes. The idea is for
10674/// hackers to set a breakpoint on this function so that they can
10675/// discover why exactly the comparison failed. They then can execute
10676/// the program from compare_dies_during_canonicalization, for
10677/// instance.
10678///
10679/// @param @l the left-hand side of the DIE comparison.
10680///
10681/// @param @r the right-hand side of the DIE comparison.
10682static void
10683notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10684{
10685}
10686
10687#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10688 notify_die_comparison_failed(l, r)
10689#else
10690#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10691#endif
10692
10693/// A macro used to return from DIE comparison routines.
10694///
10695/// If the return value is false, the macro invokes the
10696/// notify_die_comparison_failed signalling function before returning.
10697/// That way, hackers willing to learn more about why the comparison
10698/// routine returned "false" can just set a breakpoint on
10699/// notify_die_comparison_failed and execute the program from
10700/// compare_dies_during_canonicalization, for instance.
10701///
10702/// @param value the value to return from the DIE comparison routines.
10703#define ABG_RETURN(value) \
10704 do \
10705 { \
10706 if ((value) == COMPARISON_RESULT_DIFFERENT) \
10707 { \
10708 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10709 } \
10710 return return_comparison_result(l, r, dies_being_compared, \
10711 value, aggregates_being_compared, \
10712 update_canonical_dies_on_the_fly); \
10713 } \
10714 while(false)
10715
10716/// A macro used to return the "false" boolean from DIE comparison
10717/// routines.
10718///
10719/// As the return value is false, the macro invokes the
10720/// notify_die_comparison_failed signalling function before returning.
10721///
10722/// @param value the value to return from the DIE comparison routines.
10723#define ABG_RETURN_FALSE \
10724 do \
10725 { \
10726 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10727 return return_comparison_result(l, r, dies_being_compared, \
10728 COMPARISON_RESULT_DIFFERENT, \
10729 aggregates_being_compared, \
10730 update_canonical_dies_on_the_fly); \
10731 } while(false)
10732
10733/// A macro to set the 'result' variable to 'false'.
10734///
10735/// The macro invokes the notify_die_comparison_failed function so
10736/// that the hacker can set a debugging breakpoint on
10737/// notify_die_comparison_failed to know where a DIE comparison failed
10738/// during compare_dies_during_canonicalization for instance.
10739///
10740/// @param result the 'result' variable to set.
10741///
10742/// @param l the first DIE of the comparison operation.
10743///
10744/// @param r the second DIE of the comparison operation.
10745#define SET_RESULT_TO_FALSE(result, l , r) \
10746 do \
10747 { \
10748 result = COMPARISON_RESULT_DIFFERENT; \
10749 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10750 } while(false)
10751
10752/// A macro to set the 'result' variable to a given value.
10753///
10754/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10755/// invokes the notify_die_comparison_failed function so that the
10756/// hacker can set a debugging breakpoint on
10757/// notify_die_comparison_failed to know where a DIE comparison failed
10758/// during compare_dies_during_canonicalization for instance.
10759///
10760/// @param result the 'result' variable to set.
10761///
10762/// @param l the first DIE of the comparison operation.
10763///
10764/// @param r the second DIE of the comparison operation.
10765#define SET_RESULT_TO(result, value, l , r) \
10766 do \
10767 { \
10768 result = (value); \
10769 if (result == COMPARISON_RESULT_DIFFERENT) \
10770 { \
10771 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10772 } \
10773 } while(false)
10774
10775#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10776 do \
10777 { \
10778 if (aggregates_being_compared.contains(dies_being_compared)) \
10779 { \
10780 result = COMPARISON_RESULT_CYCLE_DETECTED; \
10781 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10782 ABG_RETURN(result); \
10783 } \
10784 } \
10785 while(false)
10786
10787/// Get the next member sibling of a given class or union member DIE.
10788///
10789/// @param die the DIE to consider.
10790///
10791/// @param member out parameter. This is set to the next member
10792/// sibling, iff the function returns TRUE.
10793///
10794/// @return TRUE iff the function set @p member to the next member
10795/// sibling DIE.
10796static bool
10797get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10798{
10799 if (!die)
10800 return false;
10801
10802 bool found_member = false;
10803 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10804 member) == 0);
10805 found_member;
10806 found_member = (dwarf_siblingof(member, member) == 0))
10807 {
10808 int tag = dwarf_tag(member);
10809 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10810 break;
10811 }
10812
10813 return found_member;
10814}
10815
10816/// Get the first child DIE of a class/struct/union DIE that is a
10817/// member DIE.
10818///
10819/// @param die the DIE to consider.
10820///
10821/// @param child out parameter. This is set to the first child DIE of
10822/// @p iff this function returns TRUE.
10823///
10824/// @return TRUE iff @p child is set to the first child DIE of @p die
10825/// that is a member DIE.
10826static bool
10827get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10828{
10829 if (!die)
10830 return false;
10831
10832 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10833 ABG_ASSERT(tag == DW_TAG_structure_type
10834 || tag == DW_TAG_union_type
10835 || tag == DW_TAG_class_type);
10836
10837 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10838 child) == 0);
10839
10840 if (!found_child)
10841 return false;
10842
10843 tag = dwarf_tag(child);
10844
10845 if (!(tag == DW_TAG_member
10846 || tag == DW_TAG_inheritance
10847 || tag == DW_TAG_subprogram))
10848 found_child = get_next_member_sibling_die(child, child);
10849
10850 return found_child;
10851}
10852
10853/// This is a sub-routine of return_comparison_result.
10854///
10855/// Propagate the canonical type of a the right-hand-side DIE to the
10856/// lef-hand-side DIE. This is a optimization that is done when the
10857/// two DIEs compare equal.
10858///
10859/// If the right-hand-side DIE is not canonicalized, the function
10860/// performs its canonicalization.
10861///
10862/// This optimization is performed only if
10863/// is_canon_type_to_be_propagated_tag returns true.
10864///
10865/// @param rdr the current context to consider.
10866///
10867/// @param l the left-hand-side DIE of the comparison. It's going to
10868/// receive the canonical type of the other DIE.
10869///
10870/// @param r the right-hand-side DIE of the comparison. Its canonical
10871/// type is propagated to @p l.
10872static void
10873maybe_propagate_canonical_type(const reader& rdr,
10874 const Dwarf_Die* l,
10875 const Dwarf_Die* r)
10876{
10877 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10878 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10879
10880 if (l_tag != r_tag)
10881 return;
10882
10883 if (is_canon_type_to_be_propagated_tag(l_tag))
10884 propagate_canonical_type(rdr, l, r);
10885}
10886
10887/// Propagate the canonical type of a the right-hand-side DIE to the
10888/// left-hand-side DIE. This is a optimization that is done when the
10889/// two DIEs compare equal.
10890///
10891/// If the right-hand-side DIE is not canonicalized, the function
10892/// performs its canonicalization.
10893///
10894/// @param rdr the current context to consider.
10895///
10896/// @param l the left-hand-side DIE of the comparison. It's going to
10897/// receive the canonical type of the other DIE.
10898///
10899/// @param r the right-hand-side DIE of the comparison. Its canonical
10900/// type is propagated to @p l.
10901static void
10902propagate_canonical_type(const reader& rdr,
10903 const Dwarf_Die* l,
10904 const Dwarf_Die* r)
10905{
10906 ABG_ASSERT(l && r);
10907
10908 // If 'l' has no canonical DIE and if 'r' has one, then propagage
10909 // the canonical DIE of 'r' to 'l'.
10910 //
10911 // In case 'r' has no canonical DIE, then compute it, and then
10912 // propagate that canonical DIE to 'r'.
10913 const die_source l_source = rdr.get_die_source(l);
10914 const die_source r_source = rdr.get_die_source(r);
10915
10916 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10917 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10918 bool l_has_canonical_die_offset = false;
10919 bool r_has_canonical_die_offset = false;
10920 Dwarf_Off l_canonical_die_offset = 0;
10921 Dwarf_Off r_canonical_die_offset = 0;
10922
10923 l_has_canonical_die_offset =
10924 (l_canonical_die_offset =
10925 rdr.get_canonical_die_offset(l_offset, l_source,
10926 /*die_as_type=*/true));
10927
10928 r_has_canonical_die_offset =
10929 (r_canonical_die_offset =
10930 rdr.get_canonical_die_offset(r_offset, r_source,
10931 /*die_as_type=*/true));
10932
10933
10934 if (!l_has_canonical_die_offset
10935 && r_has_canonical_die_offset
10936 // A DIE can be equivalent only to another DIE of the same
10937 // source.
10938 && l_source == r_source)
10939 {
10940 ABG_ASSERT(r_canonical_die_offset);
10941 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10942 /*die_as_type=*/true);
10943 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10944 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10945 rdr.canonical_propagated_count_++;
10946 }
10947}
10948
10949/// This function does the book keeping of comparison pairs necessary
10950/// to handle
10951///
10952/// * the detection of cycles during the comparison of aggregate
10953/// types, in conjuction with the macro
10954/// RETURN_IF_COMPARISON_CYCLE_DETECTED
10955///
10956/// * the handling of the canonical type propagation optimisation
10957/// to speed-up type canonicalization.
10958///
10959///
10960/// Note that this function is essentially a sub-routine of
10961/// compare_dies.
10962///
10963/// @param l the left-hand-side DIE being compared.
10964///
10965/// @param r the right-hand-side DIE being compared.
10966///
10967/// @param cur_dies the pair of die offsets of l and r. This is
10968/// redundant as it can been computed from @p l and @p r. However,
10969/// getting it as an argument is an optimization to avoid computing it
10970/// over and over again, given how often this function is invoked from
10971/// compare_dies.
10972///
10973/// @param return the result of comparing @p l against @p r.
10974///
10975/// @param comparison_stack the stack of pair of type DIEs being
10976/// compared.
10977///
10978/// @param do_propagate_canonical_type if true then the function
10979/// performs canonical DIEs propagation, meaning that if @p l equals
10980/// @p r and if @p r has a canonical type, then the canonical type of
10981/// @p l is set to the canonical type of @p r.
10982static comparison_result
10983return_comparison_result(const Dwarf_Die* l,
10984 const Dwarf_Die* r,
10985 const offset_pair_type& cur_dies,
10986 comparison_result result,
10987 offset_pairs_stack_type& comparison_stack,
10988 bool do_propagate_canonical_type = true)
10989{
10990 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10991
10992 if (result == COMPARISON_RESULT_EQUAL)
10993 {
10994 // The result comparing the two types is "true", basically. So
10995 // let's propagate the canonical type of r onto l, so that we
10996 // don't need to compute the canonical type of r.
10997 if (do_propagate_canonical_type)
10998 {
10999 // Propagate canonical type.
11000 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
11001
11002 // TODO: do we need to confirm any tentative canonical
11003 // propagation?
11004 }
11005 }
11006 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
11007 {
11008 // So upon detection of the comparison cycle, compare_dies
11009 // returned early with the comparison result
11010 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
11011 // carry on with the comparison of all the OTHER sub-types of
11012 // the redundant type. If they all compare equal, then it means
11013 // the redundant type pair compared equal. Otherwise, it
11014 // compared different.
11015 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
11016 // Let's fall through to let the end of this function set the
11017 // result to COMPARISON_RESULT_UNKNOWN;
11018 }
11019 else if (result == COMPARISON_RESULT_UNKNOWN)
11020 {
11021 // Here is an introductory comment describing what we are going
11022 // to do in this case where the result of the comparison of the
11023 // current pair of type is not "false", basically.
11024 //
11025 // This means that we don't yet know what the result of
11026 // comparing these two types is, because one of the sub-types of
11027 // the types being compared is "redundant", meaning it appears
11028 // more than once in the comparison stack, so if we were to
11029 // naively try to carry on with the comparison member-wise, we'd
11030 // end up with an endless loop, a.k.a "comparison cycle".
11031 //
11032 // If the current type pair is redundant then:
11033 //
11034 // * This is a redundant type that has just been fully
11035 // compared. In that case, all the types that depend on
11036 // this redundant type and that have been tentatively
11037 // canonical-type-propagated must see their canonical types
11038 // "confirmed". This means that this type is going to be
11039 // considered as not being redundant anymore, meaning all
11040 // the types that depend on it must be updated as not being
11041 // dependant on it anymore, and the type itsef must be
11042 // removed from the map of redundant types.
11043 //
11044 // After the type's canonical-type-propagation is confirmed,
11045 // the result of its comparison must also be changed into
11046 // COMPARISON_RESULT_EQUAL.
11047 //
11048 // After that, If the current type depends on a redundant type,
11049 // then propagate its canonical type AND track it as having its
11050 // type being canonical-type-propagated.
11051 //
11052 // If the current type is not redundant however, then it must be
11053 // dependant on a redundant type. If it's not dependant on a
11054 // redundant type, then it must be of those types which
11055 // comparisons are not tracked for cycle, probably because they
11056 // are not aggregates. Otherwise, ABORT to understand why. I
11057 // believe this should not happen. In any case, after that
11058 // safety check is passed, we just need to return at this point.
11059
11060 if (comparison_stack.is_redundant(cur_dies)
11061 && comparison_stack.vect_.back() == cur_dies)
11062 {
11063 // We are in the case described above of a redundant type
11064 // that has been fully compared.
11065 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
11066 comparison_stack.confirm_canonical_propagated_type(cur_dies);
11067
11068 result = COMPARISON_RESULT_EQUAL;
11069 }
11070 else if (is_canon_type_to_be_propagated_tag(l_tag)
11071 && comparison_stack.vect_.back() == cur_dies)
11072 {
11073 // The current type is not redundant. So, as described in
11074 // the introductory comment above, it must be dependant on a
11075 // redundant type.
11076 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
11077 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
11078 // Then pass through.
11079 }
11080 }
11081 else if (result == COMPARISON_RESULT_DIFFERENT)
11082 {
11083 // Here is an introductory comment describing what we are going
11084 // to do in this case where the result of the comparison of the
11085 // current pair of type is "false", basically.
11086 //
11087 // If the type pair {l,r} is redundant then cancel the
11088 // canonical-type-propagation of all the dependant pairs that
11089 // depends on this redundant {l, r}. This means walk the types
11090 // that depends on {l, r} and cancel their
11091 // canonical-propagate-type, that means remove their canonical
11092 // types and mark them as not being canonically-propagated.
11093 // Also, erase their cached comparison results that was likely
11094 // set to COMPARISON_RESULT_UNKNOWN.
11095 //
11096 // Also, update the cached result for this pair, that was likely
11097 // to be COMPARISON_RESULT_UNKNOWN.
11098 if (comparison_stack.is_redundant(cur_dies)
11099 && comparison_stack.vect_.back() == cur_dies)
11100 comparison_stack.cancel_canonical_propagated_type(cur_dies);
11101 }
11102 else
11103 {
11104 // We should never reach here.
11106 }
11107
11108 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
11109 result = COMPARISON_RESULT_UNKNOWN;
11110 else if (is_canon_type_to_be_propagated_tag(l_tag)
11111 && !comparison_stack.vect_.empty()
11112 && comparison_stack.vect_.back() == cur_dies)
11113 //Finally pop the pair types being compared from comparison_stack
11114 //iff {l,r} is on the top of the stack. If it's not, then it means
11115 //we are looking at a type that was detected as a being redundant
11116 //and thus hasn't been pushed to the stack yet gain.
11117 comparison_stack.erase(cur_dies);
11118
11119 maybe_cache_type_comparison_result(comparison_stack.rdr_,
11120 l_tag, cur_dies, result);
11121
11122 return result;
11123}
11124
11125/// Compare two DIEs emitted by a C compiler.
11126///
11127/// @param rdr the DWARF reader used to load the DWARF information.
11128///
11129/// @param l the left-hand-side argument of this comparison operator.
11130///
11131/// @param r the righ-hand-side argument of this comparison operator.
11132///
11133/// @param aggregates_being_compared this holds the names of the set
11134/// of aggregates being compared. It's used by the comparison
11135/// function to avoid recursing infinitely when faced with types
11136/// referencing themselves through pointers or references. By
11137/// default, just pass an empty instance of @ref istring_set_type to
11138/// it.
11139///
11140/// @param update_canonical_dies_on_the_fly if true, when two
11141/// sub-types compare equal (during the comparison of @p l and @p r)
11142/// update their canonical type. That way, two types of the same name
11143/// are structurally compared to each other only once. So the
11144/// non-linear structural comparison of two types of the same name
11145/// only happen once.
11146///
11147/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11148static comparison_result
11149compare_dies(const reader& rdr,
11150 const Dwarf_Die *l, const Dwarf_Die *r,
11151 offset_pairs_stack_type& aggregates_being_compared,
11152 bool update_canonical_dies_on_the_fly)
11153{
11154 ABG_ASSERT(l);
11155 ABG_ASSERT(r);
11156
11157 const die_source l_die_source = rdr.get_die_source(l);
11158 const die_source r_die_source = rdr.get_die_source(r);
11159
11160 offset_type l_offset =
11161 {
11162 l_die_source,
11163 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11164 };
11165
11166 offset_type r_offset =
11167 {
11168 r_die_source,
11169 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11170 };
11171
11172 offset_pair_type dies_being_compared(l_offset, r_offset);
11173
11174 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
11175 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11176
11177 if (l_tag != r_tag)
11179
11180 if (l_offset == r_offset)
11181 return COMPARISON_RESULT_EQUAL;
11182
11183 if (rdr.leverage_dwarf_factorization()
11184 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
11185 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
11186 if (l_offset != r_offset)
11187 return COMPARISON_RESULT_DIFFERENT;
11188
11189 comparison_result result = COMPARISON_RESULT_EQUAL;
11190 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
11191 dies_being_compared,
11192 result))
11193 return result;
11194
11195 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
11196 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
11197
11198 // If 'l' and 'r' already have canonical DIEs, then just compare the
11199 // offsets of their canonical DIEs.
11200 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
11201 {
11202 bool canonical_compare_result = false;
11203 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
11204 l_die_source, r_die_source,
11205 l_has_canonical_die_offset,
11206 r_has_canonical_die_offset,
11207 l_canonical_die_offset,
11208 r_canonical_die_offset,
11209 canonical_compare_result))
11210 {
11211 comparison_result result;
11212 SET_RESULT_TO(result,
11213 (canonical_compare_result
11214 ? COMPARISON_RESULT_EQUAL
11215 : COMPARISON_RESULT_DIFFERENT),
11216 l, r);
11217 return result;
11218 }
11219 }
11220
11221
11222
11223 switch (l_tag)
11224 {
11225 case DW_TAG_base_type:
11226 case DW_TAG_string_type:
11227 case DW_TAG_unspecified_type:
11228 if (!compare_as_decl_and_type_dies(rdr, l, r))
11229 SET_RESULT_TO_FALSE(result, l, r);
11230 break;
11231
11232 case DW_TAG_typedef:
11233 case DW_TAG_pointer_type:
11234 case DW_TAG_reference_type:
11235 case DW_TAG_rvalue_reference_type:
11236 case DW_TAG_const_type:
11237 case DW_TAG_volatile_type:
11238 case DW_TAG_restrict_type:
11239 {
11240 if (!compare_as_type_dies(rdr, l, r))
11241 {
11242 SET_RESULT_TO_FALSE(result, l, r);
11243 break;
11244 }
11245
11246 bool from_the_same_tu = false;
11247 if (!pointer_or_qual_die_of_anonymous_class_type(l)
11248 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
11249 && from_the_same_tu)
11250 {
11251 // These two typedefs, pointer, reference, or qualified
11252 // types have the same name and are defined in the same TU.
11253 // They thus ought to be the same.
11254 //
11255 // Note that pointers, reference or qualified types to
11256 // anonymous types are not taking into account here because
11257 // those always need to be structurally compared.
11258 SET_RESULT_TO_FALSE(result, l, r);
11259 break;
11260 }
11261 }
11262
11263 {
11264 // No fancy optimization in this case. We need to
11265 // structurally compare the two DIEs.
11266 Dwarf_Die lu_type_die, ru_type_die;
11267 bool lu_is_void, ru_is_void;
11268
11269 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
11270 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
11271
11272 if (lu_is_void && ru_is_void)
11273 result = COMPARISON_RESULT_EQUAL;
11274 else if (lu_is_void != ru_is_void)
11275 SET_RESULT_TO_FALSE(result, l, r);
11276 else
11277 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
11278 aggregates_being_compared,
11279 update_canonical_dies_on_the_fly);
11280 }
11281 break;
11282
11283 case DW_TAG_enumeration_type:
11284 if (!compare_as_decl_and_type_dies(rdr, l, r))
11285 SET_RESULT_TO_FALSE(result, l, r);
11286 else
11287 {
11288 // Walk the enumerators.
11289 Dwarf_Die l_enumtor, r_enumtor;
11290 bool found_l_enumtor = true, found_r_enumtor = true;
11291
11292 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11293 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
11294 &l_enumtor) == 0,
11295 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
11296 &r_enumtor) == 0;
11297 found_l_enumtor && found_r_enumtor;
11298 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
11299 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
11300 {
11301 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
11302 if ( l_tag != r_tag)
11303 {
11304 SET_RESULT_TO_FALSE(result, l, r);
11305 break;
11306 }
11307
11308 if (l_tag != DW_TAG_enumerator)
11309 continue;
11310
11311 uint64_t l_val = 0, r_val = 0;
11312 die_unsigned_constant_attribute(&l_enumtor,
11313 DW_AT_const_value,
11314 l_val);
11315 die_unsigned_constant_attribute(&r_enumtor,
11316 DW_AT_const_value,
11317 r_val);
11318 if (l_val != r_val)
11319 {
11320 SET_RESULT_TO_FALSE(result, l, r);
11321 break;
11322 }
11323 }
11324 if (found_l_enumtor != found_r_enumtor )
11325 SET_RESULT_TO_FALSE(result, l, r);
11326 }
11327 break;
11328
11329 case DW_TAG_structure_type:
11330 case DW_TAG_union_type:
11331 case DW_TAG_class_type:
11332 {
11333 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11334
11335 rdr.compare_count_++;
11336
11337 if (!compare_as_decl_and_type_dies(rdr, l, r))
11338 SET_RESULT_TO_FALSE(result, l, r);
11339 else if (rdr.options().assume_odr_for_cplusplus
11340 && rdr.odr_is_relevant(l)
11341 && rdr.odr_is_relevant(r)
11342 && !die_is_anonymous(l)
11343 && !die_is_anonymous(r))
11344 result = COMPARISON_RESULT_EQUAL;
11345 else
11346 {
11347 aggregates_being_compared.add(dies_being_compared);
11348
11349 Dwarf_Die l_member, r_member;
11350 bool found_l_member = true, found_r_member = true;
11351
11352 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11353 for (found_l_member = get_member_child_die(l, &l_member),
11354 found_r_member = get_member_child_die(r, &r_member);
11355 found_l_member && found_r_member;
11356 found_l_member = get_next_member_sibling_die(&l_member,
11357 &l_member),
11358 found_r_member = get_next_member_sibling_die(&r_member,
11359 &r_member))
11360 {
11361 int l_tag = dwarf_tag(&l_member),
11362 r_tag = dwarf_tag(&r_member);
11363
11364 if (l_tag != r_tag)
11365 {
11366 SET_RESULT_TO_FALSE(result, l, r);
11367 break;
11368 }
11369
11370 ABG_ASSERT(l_tag == DW_TAG_member
11371 || l_tag == DW_TAG_variable
11372 || l_tag == DW_TAG_inheritance
11373 || l_tag == DW_TAG_subprogram);
11374
11375 comparison_result local_result =
11376 compare_dies(rdr, &l_member, &r_member,
11377 aggregates_being_compared,
11378 update_canonical_dies_on_the_fly);
11379
11380 if (local_result == COMPARISON_RESULT_UNKNOWN)
11381 // Note that if the result of comparing any
11382 // sub-type is COMPARISON_RESULT_EQUAL, just
11383 // because we have at least one sub-type's
11384 // comparison being COMPARISON_RESULT_UNKNOWN
11385 // means that the comparison of this type will
11386 // return COMPARISON_RESULT_UNKNOWN to show
11387 // callers that this type (and all the types that
11388 // depend on it) depends on a redundant type
11389 result = local_result;
11390
11391 if (local_result == COMPARISON_RESULT_DIFFERENT)
11392 {
11393 SET_RESULT_TO_FALSE(result, l, r);
11394 break;
11395 }
11396 }
11397 if (found_l_member != found_r_member)
11398 {
11399 SET_RESULT_TO_FALSE(result, l, r);
11400 break;
11401 }
11402 }
11403 }
11404 break;
11405
11406 case DW_TAG_array_type:
11407 {
11408 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11409
11410 aggregates_being_compared.add(dies_being_compared);
11411
11412 rdr.compare_count_++;
11413
11414 Dwarf_Die l_child, r_child;
11415 bool found_l_child, found_r_child;
11416 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11417 &l_child) == 0,
11418 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11419 &r_child) == 0;
11420 found_l_child && found_r_child;
11421 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
11422 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
11423 {
11424 int l_child_tag = dwarf_tag(&l_child),
11425 r_child_tag = dwarf_tag(&r_child);
11426 if (l_child_tag == DW_TAG_subrange_type
11427 || r_child_tag == DW_TAG_subrange_type)
11428 {
11429 result = compare_dies(rdr, &l_child, &r_child,
11430 aggregates_being_compared,
11431 update_canonical_dies_on_the_fly);
11432 if (!result)
11433 {
11434 SET_RESULT_TO_FALSE(result, l, r);
11435 break;
11436 }
11437 }
11438 }
11439 if (found_l_child != found_r_child)
11440 SET_RESULT_TO_FALSE(result, l, r);
11441 // Compare the types of the elements of the array.
11442 Dwarf_Die ltype_die, rtype_die;
11443 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
11444 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
11445 ABG_ASSERT(found_ltype && found_rtype);
11446
11447 result = compare_dies(rdr, &ltype_die, &rtype_die,
11448 aggregates_being_compared,
11449 update_canonical_dies_on_the_fly);
11450 if (!result)
11452 }
11453 break;
11454
11455 case DW_TAG_subrange_type:
11456 {
11457 uint64_t l_lower_bound = 0, r_lower_bound = 0,
11458 l_upper_bound = 0, r_upper_bound = 0;
11459 bool l_lower_bound_set = false, r_lower_bound_set = false,
11460 l_upper_bound_set = false, r_upper_bound_set = false;
11461
11462 l_lower_bound_set =
11463 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
11464 r_lower_bound_set =
11465 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
11466
11467 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
11468 l_upper_bound))
11469 {
11470 uint64_t l_count = 0;
11471 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
11472 {
11473 l_upper_bound = l_lower_bound + l_count;
11474 l_upper_bound_set = true;
11475 if (l_upper_bound)
11476 --l_upper_bound;
11477 }
11478 }
11479 else
11480 l_upper_bound_set = true;
11481
11482 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
11483 r_upper_bound))
11484 {
11485 uint64_t r_count = 0;
11486 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
11487 {
11488 r_upper_bound = r_lower_bound + r_count;
11489 r_upper_bound_set = true;
11490 if (r_upper_bound)
11491 --r_upper_bound;
11492 }
11493 }
11494 else
11495 r_upper_bound_set = true;
11496
11497 if ((l_lower_bound_set != r_lower_bound_set)
11498 || (l_upper_bound_set != r_upper_bound_set)
11499 || (l_lower_bound != r_lower_bound)
11500 || (l_upper_bound != r_upper_bound))
11501 SET_RESULT_TO_FALSE(result, l, r);
11502 }
11503 break;
11504
11505 case DW_TAG_subroutine_type:
11506 case DW_TAG_subprogram:
11507 {
11508 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11509
11510 aggregates_being_compared.add(dies_being_compared);
11511
11512 rdr.compare_count_++;
11513
11514 if (l_tag == DW_TAG_subprogram
11515 && !fn_die_equal_by_linkage_name(l, r))
11516 {
11517 SET_RESULT_TO_FALSE(result, l, r);
11518 break;
11519 }
11520 else if (l_tag == DW_TAG_subprogram
11521 && die_is_in_c(l) && die_is_in_c(r))
11522 {
11523 result = COMPARISON_RESULT_EQUAL;
11524 break;
11525 }
11526 else if (!die_is_in_c(l) && !die_is_in_c(r))
11527 {
11528 // In C, we cannot have two different functions with the
11529 // same linkage name in a given binary. But here we are
11530 // looking at DIEs that don't originate from C. So we
11531 // need to compare return types and parameter types.
11532 Dwarf_Die l_return_type, r_return_type;
11533 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
11534 l_return_type);
11535 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
11536 r_return_type);
11537 if (l_return_type_is_void != r_return_type_is_void
11538 || (!l_return_type_is_void
11539 && !compare_dies(rdr,
11540 &l_return_type, &r_return_type,
11541 aggregates_being_compared,
11542 update_canonical_dies_on_the_fly)))
11543 SET_RESULT_TO_FALSE(result, l, r);
11544 else
11545 {
11546 Dwarf_Die l_child, r_child;
11547 bool found_l_child, found_r_child;
11548 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11549 &l_child) == 0,
11550 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11551 &r_child) == 0;
11552 found_l_child && found_r_child;
11553 found_l_child = dwarf_siblingof(&l_child,
11554 &l_child) == 0,
11555 found_r_child = dwarf_siblingof(&r_child,
11556 &r_child)==0)
11557 {
11558 int l_child_tag = dwarf_tag(&l_child);
11559 int r_child_tag = dwarf_tag(&r_child);
11560 comparison_result local_result =
11561 COMPARISON_RESULT_EQUAL;
11562 if (l_child_tag != r_child_tag)
11563 local_result = COMPARISON_RESULT_DIFFERENT;
11564 if (l_child_tag == DW_TAG_formal_parameter)
11565 local_result =
11566 compare_dies(rdr, &l_child, &r_child,
11567 aggregates_being_compared,
11568 update_canonical_dies_on_the_fly);
11569 if (local_result == COMPARISON_RESULT_DIFFERENT)
11570 {
11571 result = local_result;
11572 SET_RESULT_TO_FALSE(result, l, r);
11573 break;
11574 }
11575 if (local_result == COMPARISON_RESULT_UNKNOWN)
11576 // Note that if the result of comparing any
11577 // sub-type is COMPARISON_RESULT_EQUAL, just
11578 // because we have at least one sub-type's
11579 // comparison being COMPARISON_RESULT_UNKNOWN
11580 // means that the comparison of this type will
11581 // return COMPARISON_RESULT_UNKNOWN to show
11582 // callers that this type (and all the types
11583 // that depend on it) depends on a redundant
11584 // type and so, can't be
11585 // canonical-type-propagated.
11586 result = local_result;
11587 }
11588 if (found_l_child != found_r_child)
11589 {
11590 SET_RESULT_TO_FALSE(result, l, r);
11591 break;
11592 }
11593 }
11594 }
11595 }
11596 break;
11597
11598 case DW_TAG_formal_parameter:
11599 {
11600 Dwarf_Die l_type, r_type;
11601 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
11602 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
11603 if (l_type_is_void != r_type_is_void)
11604 SET_RESULT_TO_FALSE(result, l, r);
11605 else if (!l_type_is_void)
11606 {
11607 comparison_result local_result =
11608 compare_dies(rdr, &l_type, &r_type,
11609 aggregates_being_compared,
11610 update_canonical_dies_on_the_fly);
11611 SET_RESULT_TO(result, local_result, l, r);
11612 }
11613 }
11614 break;
11615
11616 case DW_TAG_variable:
11617 case DW_TAG_member:
11618 if (compare_as_decl_dies(l, r))
11619 {
11620 // Compare the offsets of the data members
11621 if (l_tag == DW_TAG_member)
11622 {
11623 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11624 die_member_offset(rdr, l, l_offset_in_bits);
11625 die_member_offset(rdr, r, r_offset_in_bits);
11626 if (l_offset_in_bits != r_offset_in_bits)
11627 SET_RESULT_TO_FALSE(result, l, r);
11628 }
11629 if (result)
11630 {
11631 // Compare the types of the data members or variables.
11632 Dwarf_Die l_type, r_type;
11633 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11634 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11635 comparison_result local_result =
11636 compare_dies(rdr, &l_type, &r_type,
11637 aggregates_being_compared,
11638 update_canonical_dies_on_the_fly);
11639 SET_RESULT_TO(result, local_result, l, r);
11640 }
11641 }
11642 else
11643 SET_RESULT_TO_FALSE(result, l, r);
11644 break;
11645
11646 case DW_TAG_inheritance:
11647 {
11648 Dwarf_Die l_type, r_type;
11649 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11650 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11651 result = compare_dies(rdr, &l_type, &r_type,
11652 aggregates_being_compared,
11653 update_canonical_dies_on_the_fly);
11654 if (!result)
11655 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11656
11657 uint64_t l_a = 0, r_a = 0;
11658 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11659 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11660 if (l_a != r_a)
11661 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11662
11663 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11664 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11665 if (l_a != r_a)
11666 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11667
11668 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11669 die_member_offset(rdr, l, l_offset_in_bits);
11670 die_member_offset(rdr, r, r_offset_in_bits);
11671 if (l_offset_in_bits != r_offset_in_bits)
11672 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11673 }
11674 break;
11675
11676 case DW_TAG_ptr_to_member_type:
11677 {
11678 bool comp_result = false;
11679 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11680 if (!comp_result)
11681 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11682
11683 Dwarf_Die l_type, r_type;
11684 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11685 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11686 result = compare_dies(rdr, &l_type, &r_type,
11687 aggregates_being_compared,
11688 update_canonical_dies_on_the_fly);
11689 if (!result)
11690 ABG_RETURN(result);
11691
11692 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11693 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11694 result = compare_dies(rdr, &l_type, &r_type,
11695 aggregates_being_compared,
11696 update_canonical_dies_on_the_fly);
11697 if (!result)
11698 ABG_RETURN(result);
11699 }
11700 break;
11701
11702 case DW_TAG_enumerator:
11703 case DW_TAG_packed_type:
11704 case DW_TAG_set_type:
11705 case DW_TAG_file_type:
11706 case DW_TAG_thrown_type:
11707 case DW_TAG_interface_type:
11708 case DW_TAG_shared_type:
11709 case DW_TAG_compile_unit:
11710 case DW_TAG_namespace:
11711 case DW_TAG_module:
11712 case DW_TAG_constant:
11713 case DW_TAG_partial_unit:
11714 case DW_TAG_imported_unit:
11715 case DW_TAG_dwarf_procedure:
11716 case DW_TAG_imported_declaration:
11717 case DW_TAG_entry_point:
11718 case DW_TAG_label:
11719 case DW_TAG_lexical_block:
11720 case DW_TAG_unspecified_parameters:
11721 case DW_TAG_variant:
11722 case DW_TAG_common_block:
11723 case DW_TAG_common_inclusion:
11724 case DW_TAG_inlined_subroutine:
11725 case DW_TAG_with_stmt:
11726 case DW_TAG_access_declaration:
11727 case DW_TAG_catch_block:
11728 case DW_TAG_friend:
11729 case DW_TAG_namelist:
11730 case DW_TAG_namelist_item:
11731 case DW_TAG_template_type_parameter:
11732 case DW_TAG_template_value_parameter:
11733 case DW_TAG_try_block:
11734 case DW_TAG_variant_part:
11735 case DW_TAG_imported_module:
11736 case DW_TAG_condition:
11737 case DW_TAG_type_unit:
11738 case DW_TAG_template_alias:
11739 case DW_TAG_lo_user:
11740 case DW_TAG_MIPS_loop:
11741 case DW_TAG_format_label:
11742 case DW_TAG_function_template:
11743 case DW_TAG_class_template:
11744 case DW_TAG_GNU_BINCL:
11745 case DW_TAG_GNU_EINCL:
11746 case DW_TAG_GNU_template_template_param:
11747 case DW_TAG_GNU_template_parameter_pack:
11748 case DW_TAG_GNU_formal_parameter_pack:
11749 case DW_TAG_GNU_call_site:
11750 case DW_TAG_GNU_call_site_parameter:
11751 case DW_TAG_hi_user:
11752#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11753 if (rdr.debug_die_canonicalization_is_on_)
11755#endif
11757 break;
11758 }
11759
11760 ABG_RETURN(result);
11761}
11762
11763/// Compare two DIEs emitted by a C compiler.
11764///
11765/// @param rdr the DWARF reader used to load the DWARF information.
11766///
11767/// @param l the left-hand-side argument of this comparison operator.
11768///
11769/// @param r the righ-hand-side argument of this comparison operator.
11770///
11771/// @param update_canonical_dies_on_the_fly if yes, then this function
11772/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11773/// comparing l and r. This helps in making so that sub-type DIEs of
11774/// 'l' and 'r' are compared structurally only once. This is how we
11775/// turn this exponential comparison problem into a problem that is a
11776/// closer to a linear one.
11777///
11778/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11779static comparison_result
11780compare_dies(const reader& rdr,
11781 const Dwarf_Die *l,
11782 const Dwarf_Die *r,
11783 bool update_canonical_dies_on_the_fly)
11784{
11785 offset_pairs_stack_type aggregates_being_compared(rdr);
11786 return compare_dies(rdr, l, r, aggregates_being_compared,
11787 update_canonical_dies_on_the_fly);
11788}
11789
11790/// Compare two DIEs for the purpose of canonicalization.
11791///
11792/// This is a sub-routine of reader::get_canonical_die.
11793///
11794/// When DIE canonicalization debugging is on, this function performs
11795/// both structural and canonical comparison. It expects that both
11796/// comparison yield the same result.
11797///
11798/// @param rdr the DWARF reader.
11799///
11800/// @param l the left-hand-side comparison operand DIE.
11801///
11802/// @param r the right-hand-side comparison operand DIE.
11803///
11804/// @param update_canonical_dies_on_the_fly if true, then some
11805/// aggregate DIEs will see their canonical types propagated.
11806///
11807/// @return true iff @p l equals @p r.
11808static bool
11809compare_dies_during_canonicalization(reader& rdr,
11810 const Dwarf_Die *l,
11811 const Dwarf_Die *r,
11812 bool update_canonical_dies_on_the_fly)
11813{
11814#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11815 if (rdr.debug_die_canonicalization_is_on_)
11816 {
11817 bool canonical_equality = false, structural_equality = false;
11818 rdr.use_canonical_die_comparison_ = false;
11819 structural_equality = compare_dies(rdr, l, r,
11820 /*update_canonical_dies_on_the_fly=*/false);
11821 rdr.use_canonical_die_comparison_ = true;
11822 canonical_equality = compare_dies(rdr, l, r,
11823 update_canonical_dies_on_the_fly);
11824 if (canonical_equality != structural_equality)
11825 {
11826 std::cerr << "structural & canonical equality different for DIEs: "
11827 << std::hex
11828 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11829 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11830 << std::dec
11831 << ", repr: '"
11832 << rdr.get_die_pretty_type_representation(l, 0)
11833 << "'"
11834 << std::endl;
11836 }
11837 return structural_equality;
11838 }
11839#endif
11840 return compare_dies(rdr, l, r,
11841 update_canonical_dies_on_the_fly);
11842}
11843
11844// ----------------------------------
11845// </die comparison engine>
11846// ---------------------------------
11847
11848/// Get the point where a DW_AT_import DIE is used to import a given
11849/// (unit) DIE, between two DIEs.
11850///
11851/// @param rdr the dwarf reader to consider.
11852///
11853/// @param partial_unit_offset the imported unit for which we want to
11854/// know the insertion point. This is usually a partial unit (with
11855/// tag DW_TAG_partial_unit) but it does not necessarily have to be
11856/// so.
11857///
11858/// @param first_die_offset the offset of the DIE from which this
11859/// function starts looking for the import point of
11860/// @partial_unit_offset. Note that this offset is excluded from the
11861/// set of potential solutions.
11862///
11863/// @param first_die_cu_offset the offset of the (compilation) unit
11864/// that @p first_die_cu_offset belongs to.
11865///
11866/// @param source where the DIE of first_die_cu_offset unit comes
11867/// from.
11868///
11869/// @param last_die_offset the offset of the last DIE of the up to
11870/// which this function looks for the import point of @p
11871/// partial_unit_offset. Note that this offset is excluded from the
11872/// set of potential solutions.
11873///
11874/// @param imported_point_offset. The resulting
11875/// imported_point_offset. Note that if the imported DIE @p
11876/// partial_unit_offset is not found between @p first_die_offset and
11877/// @p last_die_offset, this parameter is left untouched by this
11878/// function.
11879///
11880/// @return true iff an imported unit is found between @p
11881/// first_die_offset and @p last_die_offset.
11882static bool
11883find_import_unit_point_between_dies(const reader& rdr,
11884 size_t partial_unit_offset,
11885 Dwarf_Off first_die_offset,
11886 Dwarf_Off first_die_cu_offset,
11887 die_source source,
11888 size_t last_die_offset,
11889 size_t& imported_point_offset)
11890{
11891 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11892 rdr.tu_die_imported_unit_points_map(source);
11893
11894 tu_die_imported_unit_points_map_type::const_iterator iter =
11895 tu_die_imported_unit_points_map.find(first_die_cu_offset);
11896
11897 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11898
11899 const imported_unit_points_type& imported_unit_points = iter->second;
11900 if (imported_unit_points.empty())
11901 return false;
11902
11903 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11904 imported_unit_points_type::const_iterator e = imported_unit_points.end();
11905
11906 find_lower_bound_in_imported_unit_points(imported_unit_points,
11907 first_die_offset,
11908 b);
11909
11910 if (last_die_offset != static_cast<size_t>(-1))
11911 find_lower_bound_in_imported_unit_points(imported_unit_points,
11912 last_die_offset,
11913 e);
11914
11915 if (e != imported_unit_points.end())
11916 {
11917 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11918 if (i->imported_unit_die_off == partial_unit_offset)
11919 {
11920 imported_point_offset = i->offset_of_import ;
11921 return true;
11922 }
11923
11924 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11925 {
11926 if (find_import_unit_point_between_dies(rdr,
11927 partial_unit_offset,
11928 i->imported_unit_child_off,
11929 i->imported_unit_cu_off,
11930 i->imported_unit_die_source,
11931 /*(Dwarf_Off)*/-1,
11932 imported_point_offset))
11933 return true;
11934 }
11935 }
11936 else
11937 {
11938 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11939 if (i->imported_unit_die_off == partial_unit_offset)
11940 {
11941 imported_point_offset = i->offset_of_import ;
11942 return true;
11943 }
11944
11945 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11946 {
11947 if (find_import_unit_point_between_dies(rdr,
11948 partial_unit_offset,
11949 i->imported_unit_child_off,
11950 i->imported_unit_cu_off,
11951 i->imported_unit_die_source,
11952 /*(Dwarf_Off)*/-1,
11953 imported_point_offset))
11954 return true;
11955 }
11956 }
11957
11958 return false;
11959}
11960
11961/// In the current translation unit, get the last point where a
11962/// DW_AT_import DIE is used to import a given (unit) DIE, before a
11963/// given DIE is found. That given DIE is called the limit DIE.
11964///
11965/// Said otherwise, this function returns the last import point of a
11966/// unit, before a limit.
11967///
11968/// @param rdr the dwarf reader to consider.
11969///
11970/// @param partial_unit_offset the imported unit for which we want to
11971/// know the insertion point of. This is usually a partial unit (with
11972/// tag DW_TAG_partial_unit) but it does not necessarily have to be
11973/// so.
11974///
11975/// @param where_offset the offset of the limit DIE.
11976///
11977/// @param imported_point_offset. The resulting imported_point_offset.
11978/// Note that if the imported DIE @p partial_unit_offset is not found
11979/// before @p die_offset, this is set to the last @p
11980/// partial_unit_offset found under @p parent_die.
11981///
11982/// @return true iff an imported unit is found before @p die_offset.
11983/// Note that if an imported unit is found after @p die_offset then @p
11984/// imported_point_offset is set and the function return false.
11985static bool
11986find_import_unit_point_before_die(const reader& rdr,
11987 size_t partial_unit_offset,
11988 size_t where_offset,
11989 size_t& imported_point_offset)
11990{
11991 size_t import_point_offset = 0;
11992 Dwarf_Die first_die_of_tu;
11993
11994 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11995 &first_die_of_tu) != 0)
11996 return false;
11997
11998 Dwarf_Die cu_die_memory;
11999 Dwarf_Die *cu_die;
12000
12001 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
12002 &cu_die_memory, 0, 0);
12003
12004 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
12005 dwarf_dieoffset(&first_die_of_tu),
12006 dwarf_dieoffset(cu_die),
12007 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
12008 where_offset,
12009 import_point_offset))
12010 {
12011 imported_point_offset = import_point_offset;
12012 return true;
12013 }
12014
12015 if (import_point_offset)
12016 {
12017 imported_point_offset = import_point_offset;
12018 return true;
12019 }
12020
12021 return false;
12022}
12023
12024/// Return the parent DIE for a given DIE.
12025///
12026/// Note that the function build_die_parent_map() must have been
12027/// called before this one can work. This function either succeeds or
12028/// aborts the current process.
12029///
12030/// @param rdr the DWARF reader to consider.
12031///
12032/// @param die the DIE for which we want the parent.
12033///
12034/// @param parent_die the output parameter set to the parent die of
12035/// @p die. Its memory must be allocated and handled by the caller.
12036///
12037/// @param where_offset the offset of the DIE where we are "logically"
12038/// positionned at, in the DIE tree. This is useful when @p die is
12039/// e.g, DW_TAG_partial_unit that can be included in several places in
12040/// the DIE tree.
12041///
12042/// @return true if the function could get a parent DIE, false
12043/// otherwise.
12044static bool
12045get_parent_die(const reader& rdr,
12046 const Dwarf_Die* die,
12047 Dwarf_Die& parent_die,
12048 size_t where_offset)
12049{
12050 ABG_ASSERT(rdr.dwarf_debug_info());
12051
12052 const die_source source = rdr.get_die_source(die);
12053
12054 const offset_offset_map_type& m = rdr.die_parent_map(source);
12055 offset_offset_map_type::const_iterator i =
12056 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
12057
12058 if (i == m.end())
12059 return false;
12060
12061 switch (source)
12062 {
12063 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
12064 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
12065 i->second, &parent_die));
12066 break;
12067 case ALT_DEBUG_INFO_DIE_SOURCE:
12068 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
12069 i->second, &parent_die));
12070 break;
12071 case TYPE_UNIT_DIE_SOURCE:
12072 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
12073 i->second, &parent_die));
12074 break;
12075 case NO_DEBUG_INFO_DIE_SOURCE:
12076 case NUMBER_OF_DIE_SOURCES:
12078 }
12079
12080 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
12081 {
12082 if (where_offset == 0)
12083 {
12084 parent_die = *rdr.cur_tu_die();
12085 return true;
12086 }
12087 size_t import_point_offset = 0;
12088 bool found =
12089 find_import_unit_point_before_die(rdr,
12090 dwarf_dieoffset(&parent_die),
12091 where_offset,
12092 import_point_offset);
12093 if (!found)
12094 // It looks like parent_die (which comes from the alternate
12095 // debug info file) hasn't been imported into this TU. So,
12096 // Let's assume its logical parent is the DIE of the current
12097 // TU.
12098 parent_die = *rdr.cur_tu_die();
12099 else
12100 {
12101 ABG_ASSERT(import_point_offset);
12102 Dwarf_Die import_point_die;
12103 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
12104 import_point_offset,
12105 &import_point_die));
12106 return get_parent_die(rdr, &import_point_die,
12107 parent_die, where_offset);
12108 }
12109 }
12110
12111 return true;
12112}
12113
12114/// Get the DIE representing the scope of a given DIE.
12115///
12116/// Please note that when the DIE we are looking at has a
12117/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
12118/// DIE is the parent DIE of the DIE referred to by that attribute.
12119/// In other words, this function returns the scope of the origin DIE
12120/// of the current DIE.
12121///
12122/// So, the scope DIE can be different from the parent DIE of a given
12123/// DIE.
12124///
12125/// Also note that if the current translation unit is from C, then
12126/// this returns the global scope.
12127///
12128/// @param rdr the DWARF reader to use.
12129///
12130/// @param dye the DIE to consider.
12131///
12132/// @param where_offset where we are logically at in the DIE stream.
12133///
12134/// @param scope_die out parameter. This is set to the resulting
12135/// scope DIE iff the function returns true.
12136///
12137/// @return true iff the scope was found and returned in the @p
12138/// scope_die parameter.
12139static bool
12140get_scope_die(const reader& rdr,
12141 const Dwarf_Die* dye,
12142 size_t where_offset,
12143 Dwarf_Die& scope_die)
12144{
12145 Dwarf_Die origin_die_mem;
12146 Dwarf_Die *die = &origin_die_mem;
12147 if (!die_origin_die(dye, origin_die_mem))
12148 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
12149
12150 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
12151 get_die_language(die, die_lang);
12152 if (is_c_language(die_lang)
12153 || rdr.die_parent_map(rdr.get_die_source(die)).empty())
12154 {
12155 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
12156 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
12157 }
12158
12159 if (!get_parent_die(rdr, die, scope_die, where_offset))
12160 return false;
12161
12162 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
12163 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
12164 || dwarf_tag(&scope_die) == DW_TAG_array_type)
12165 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
12166
12167 return true;
12168}
12169
12170/// Return the abigail IR node representing the scope of a given DIE.
12171///
12172/// Note that it is the logical scope that is returned. That is, if
12173/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
12174/// attribute, it's the scope of the referred-to DIE (via these
12175/// attributes) that is returned. In other words, its the scope of
12176/// the origin DIE that is returned.
12177///
12178/// Also note that if the current translation unit is from C, then
12179/// this returns the global scope.
12180///
12181/// @param rdr the dwarf reader to use.
12182///
12183/// @param dye the DIE to get the scope for.
12184///
12185/// @param called_from_public_decl is true if this function has been
12186/// initially called within the context of a public decl.
12187///
12188/// @param where_offset the offset of the DIE where we are "logically"
12189/// positionned at, in the DIE tree. This is useful when @p die is
12190/// e.g, DW_TAG_partial_unit that can be included in several places in
12191/// the DIE tree.
12192///
12193/// @return the resulting scope, or nil if could not be computed.
12194static scope_decl_sptr
12195get_scope_for_die(reader& rdr,
12196 Dwarf_Die* dye,
12197 bool called_for_public_decl,
12198 size_t where_offset)
12199{
12200 Dwarf_Die origin_die_mem;
12201 Dwarf_Die *die = &origin_die_mem;
12202
12203 if (!die_origin_die(dye, origin_die_mem))
12204 // There was no origin DIE found, so let's make the "die" pointer
12205 // above point to the content of the input "dye".
12206 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
12207
12208 const die_source source_of_die = rdr.get_die_source(die);
12209
12210 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
12211 get_die_language(die, die_lang);
12212 if (is_c_language(die_lang)
12213 || rdr.die_parent_map(source_of_die).empty())
12214 {
12215 // In units for the C languages all decls belong to the global
12216 // namespace. This is generally the case if Libabigail
12217 // determined that no DIE -> parent map was needed.
12218 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
12219 return rdr.global_scope();
12220 }
12221
12222 Dwarf_Die parent_die;
12223
12224 if (!get_parent_die(rdr, die, parent_die, where_offset))
12225 return rdr.nil_scope();
12226
12227 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
12228 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
12229 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
12230 {
12231 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
12232 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
12233 {
12234 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
12235 || source_of_die == TYPE_UNIT_DIE_SOURCE);
12236 return rdr.cur_transl_unit()->get_global_scope();
12237 }
12238
12239 // For top level DIEs like DW_TAG_compile_unit, we just want to
12240 // return the global scope for the corresponding translation
12241 // unit. This must have been set by
12242 // build_translation_unit_and_add_to_ir if we already started to
12243 // build the translation unit of parent_die. Otherwise, just
12244 // return the global scope of the current translation unit.
12245 die_tu_map_type::const_iterator i =
12246 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
12247 if (i != rdr.die_tu_map().end())
12248 return i->second->get_global_scope();
12249 return rdr.cur_transl_unit()->get_global_scope();
12250 }
12251
12254 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
12255 || dwarf_tag(&parent_die) == DW_TAG_array_type
12256 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
12257 // this is an entity defined in a scope that is a function.
12258 // Normally, I would say that this should be dropped. But I have
12259 // seen a case where a typedef DIE needed by a function parameter
12260 // was defined right before the parameter, under the scope of the
12261 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
12262 // the function parm too. So for that case, let's say that the
12263 // scope is the scope of the function itself. Note that this is
12264 // an error of the DWARF emitter. We should never see this DIE in
12265 // this context.
12266 {
12267 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
12268 called_for_public_decl,
12269 where_offset);
12270 if (is_anonymous_type_die(die))
12271 // For anonymous type that have nothing to do in a function or
12272 // array type context, let's put it in the containing
12273 // namespace. That is, do not let it be in a containing class
12274 // or union where it has nothing to do.
12275 while (is_class_or_union_type(s))
12276 {
12277 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
12278 return rdr.nil_scope();
12279 s = get_scope_for_die(rdr, &parent_die,
12280 called_for_public_decl,
12281 where_offset);
12282 }
12283 return s;
12284 }
12285 else
12286 d = build_ir_node_from_die(rdr, &parent_die,
12287 called_for_public_decl,
12288 where_offset);
12289 s = dynamic_pointer_cast<scope_decl>(d);
12290 if (!s)
12291 // this is an entity defined in someting that is not a scope.
12292 // Let's drop it.
12293 return rdr.nil_scope();
12294
12295 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
12296 if (cl && cl->get_is_declaration_only())
12297 {
12298 scope_decl_sptr scop =
12299 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
12300 if (scop)
12301 s = scop;
12302 else
12303 s = cl;
12304 }
12305 return s;
12306}
12307
12308/// Convert a DWARF constant representing the value of the
12309/// DW_AT_language property into the translation_unit::language
12310/// enumerator.
12311///
12312/// @param l the DWARF constant to convert.
12313///
12314/// @return the resulting translation_unit::language enumerator.
12316dwarf_language_to_tu_language(size_t l)
12317{
12318 switch (l)
12319 {
12320 case DW_LANG_C89:
12321 return translation_unit::LANG_C89;
12322 case DW_LANG_C:
12323 return translation_unit::LANG_C;
12324 case DW_LANG_Ada83:
12325 return translation_unit::LANG_Ada83;
12326 case DW_LANG_C_plus_plus:
12327 return translation_unit::LANG_C_plus_plus;
12328 case DW_LANG_Cobol74:
12329 return translation_unit::LANG_Cobol74;
12330 case DW_LANG_Cobol85:
12331 return translation_unit::LANG_Cobol85;
12332 case DW_LANG_Fortran77:
12333 return translation_unit::LANG_Fortran77;
12334 case DW_LANG_Fortran90:
12335 return translation_unit::LANG_Fortran90;
12336 case DW_LANG_Pascal83:
12337 return translation_unit::LANG_Pascal83;
12338 case DW_LANG_Modula2:
12339 return translation_unit::LANG_Modula2;
12340 case DW_LANG_Java:
12341 return translation_unit::LANG_Java;
12342 case DW_LANG_C99:
12343 return translation_unit::LANG_C99;
12344 case DW_LANG_Ada95:
12345 return translation_unit::LANG_Ada95;
12346 case DW_LANG_Fortran95:
12347 return translation_unit::LANG_Fortran95;
12348 case DW_LANG_PLI:
12349 return translation_unit::LANG_PLI;
12350 case DW_LANG_ObjC:
12351 return translation_unit::LANG_ObjC;
12352 case DW_LANG_ObjC_plus_plus:
12353 return translation_unit::LANG_ObjC_plus_plus;
12354
12355#ifdef HAVE_DW_LANG_Rust_enumerator
12356 case DW_LANG_Rust:
12357 return translation_unit::LANG_Rust;
12358#endif
12359
12360#ifdef HAVE_DW_LANG_UPC_enumerator
12361 case DW_LANG_UPC:
12362 return translation_unit::LANG_UPC;
12363#endif
12364
12365#ifdef HAVE_DW_LANG_D_enumerator
12366 case DW_LANG_D:
12367 return translation_unit::LANG_D;
12368#endif
12369
12370#ifdef HAVE_DW_LANG_Python_enumerator
12371 case DW_LANG_Python:
12372 return translation_unit::LANG_Python;
12373#endif
12374
12375#ifdef HAVE_DW_LANG_Go_enumerator
12376 case DW_LANG_Go:
12377 return translation_unit::LANG_Go;
12378#endif
12379
12380#ifdef HAVE_DW_LANG_C11_enumerator
12381 case DW_LANG_C11:
12382 return translation_unit::LANG_C11;
12383#endif
12384
12385#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
12386 case DW_LANG_C_plus_plus_03:
12387 return translation_unit::LANG_C_plus_plus_03;
12388#endif
12389
12390#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
12391 case DW_LANG_C_plus_plus_11:
12392 return translation_unit::LANG_C_plus_plus_11;
12393#endif
12394
12395#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
12396 case DW_LANG_C_plus_plus_14:
12397 return translation_unit::LANG_C_plus_plus_14;
12398#endif
12399
12400#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
12401 case DW_LANG_Mips_Assembler:
12402 return translation_unit::LANG_Mips_Assembler;
12403#endif
12404
12405 default:
12406 return translation_unit::LANG_UNKNOWN;
12407 }
12408}
12409
12410/// Get the default array lower bound value as defined by the DWARF
12411/// specification, version 4, depending on the language of the
12412/// translation unit.
12413///
12414/// @param l the language of the translation unit.
12415///
12416/// @return the default array lower bound value.
12417static uint64_t
12418get_default_array_lower_bound(translation_unit::language l)
12419{
12420 int value = 0;
12421 switch (l)
12422 {
12423 case translation_unit::LANG_UNKNOWN:
12424 value = 0;
12425 break;
12426 case translation_unit::LANG_Cobol74:
12427 case translation_unit::LANG_Cobol85:
12428 value = 1;
12429 break;
12430 case translation_unit::LANG_C89:
12431 case translation_unit::LANG_C99:
12432 case translation_unit::LANG_C11:
12433 case translation_unit::LANG_C:
12434 case translation_unit::LANG_C_plus_plus_03:
12435 case translation_unit::LANG_C_plus_plus_11:
12436 case translation_unit::LANG_C_plus_plus_14:
12437 case translation_unit::LANG_C_plus_plus:
12438 case translation_unit::LANG_ObjC:
12439 case translation_unit::LANG_ObjC_plus_plus:
12440 case translation_unit::LANG_Rust:
12441 value = 0;
12442 break;
12443 case translation_unit::LANG_Fortran77:
12444 case translation_unit::LANG_Fortran90:
12445 case translation_unit::LANG_Fortran95:
12446 case translation_unit::LANG_Ada83:
12447 case translation_unit::LANG_Ada95:
12448 case translation_unit::LANG_Pascal83:
12449 case translation_unit::LANG_Modula2:
12450 value = 1;
12451 break;
12452 case translation_unit::LANG_Java:
12453 value = 0;
12454 break;
12455 case translation_unit::LANG_PLI:
12456 value = 1;
12457 break;
12458 case translation_unit::LANG_UPC:
12459 case translation_unit::LANG_D:
12460 case translation_unit::LANG_Python:
12461 case translation_unit::LANG_Go:
12462 case translation_unit::LANG_Mips_Assembler:
12463 value = 0;
12464 break;
12465 }
12466
12467 return value;
12468}
12469
12470/// For a given offset, find the lower bound of a sorted vector of
12471/// imported unit point offset.
12472///
12473/// The lower bound is the smallest point (the point with the smallest
12474/// offset) which is the greater than a given offset.
12475///
12476/// @param imported_unit_points_type the sorted vector of imported
12477/// unit points.
12478///
12479/// @param val the offset to consider when looking for the lower
12480/// bound.
12481///
12482/// @param r an iterator to the lower bound found. This parameter is
12483/// set iff the function returns true.
12484///
12485/// @return true iff the lower bound has been found.
12486static bool
12487find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
12488 Dwarf_Off val,
12489 imported_unit_points_type::const_iterator& r)
12490{
12491 imported_unit_point v(val);
12492 imported_unit_points_type::const_iterator result =
12493 std::lower_bound(p.begin(), p.end(), v);
12494
12495 bool is_ok = result != p.end();
12496
12497 if (is_ok)
12498 r = result;
12499
12500 return is_ok;
12501}
12502
12503/// Given a DW_TAG_compile_unit, build and return the corresponding
12504/// abigail::translation_unit ir node. Note that this function
12505/// recursively reads the children dies of the current DIE and
12506/// populates the resulting translation unit.
12507///
12508/// @param rdr the DWARF reader to use.
12509///
12510/// @param die the DW_TAG_compile_unit DIE to consider.
12511///
12512/// @param address_size the size of the addresses expressed in this
12513/// translation unit in general.
12514///
12515/// @return a pointer to the resulting translation_unit.
12517build_translation_unit_and_add_to_ir(reader& rdr,
12518 Dwarf_Die* die,
12519 char address_size)
12520{
12521 translation_unit_sptr result;
12522
12523 if (!die)
12524 return result;
12525 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
12526
12527 // Clear the part of the context that is dependent on the translation
12528 // unit we are reading.
12529 rdr.clear_per_translation_unit_data();
12530
12531 rdr.cur_tu_die(die);
12532
12533 string path = die_string_attribute(die, DW_AT_name);
12534 if (path == "<artificial>")
12535 {
12536 // This is a file artificially generated by the compiler, so its
12537 // name is '<artificial>'. As we want all different translation
12538 // units to have unique path names, let's suffix this path name
12539 // with its die offset.
12540 std::ostringstream o;
12541 o << path << "-" << std::hex << dwarf_dieoffset(die);
12542 path = o.str();
12543 }
12544 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
12545
12546 // See if the same translation unit exits already in the current
12547 // corpus. Sometimes, the same translation unit can be present
12548 // several times in the same debug info. The content of the
12549 // different instances of the translation unit are different. So to
12550 // represent that, we are going to re-use the same translation
12551 // unit. That is, it's going to be the union of all the translation
12552 // units of the same path.
12553 {
12554 const string& abs_path =
12555 compilation_dir.empty() ? path : compilation_dir + "/" + path;
12556 result = rdr.corpus()->find_translation_unit(abs_path);
12557 }
12558
12559 if (!result)
12560 {
12561 result.reset(new translation_unit(rdr.env(),
12562 path,
12563 address_size));
12564 result->set_compilation_dir_path(compilation_dir);
12565 rdr.corpus()->add(result);
12566 uint64_t l = 0;
12567 die_unsigned_constant_attribute(die, DW_AT_language, l);
12568 result->set_language(dwarf_language_to_tu_language(l));
12569 }
12570
12571 rdr.cur_transl_unit(result);
12572 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
12573
12574 Dwarf_Die child;
12575 if (dwarf_child(die, &child) != 0)
12576 return result;
12577
12578 result->set_is_constructed(false);
12579 int tag = dwarf_tag(&child);
12580 do
12581 if (rdr.load_undefined_interfaces()
12582 && (rdr.is_decl_die_with_undefined_symbol(&child)
12583 || tag == DW_TAG_class_type // Top-level classes might
12584 // have undefined interfaces
12585 // that need to be
12586 // represented, so let's
12587 // analyze them as well.
12588 || ((tag == DW_TAG_union_type || tag == DW_TAG_structure_type)
12589 && die_is_in_cplus_plus(&child))))
12590 {
12591 // Analyze undefined functions & variables for the purpose of
12592 // analyzing compatibility matters.
12593 build_ir_node_from_die(rdr, &child,
12594 // Pretend the DIE is publicly defined
12595 // so that types that are reachable
12596 // from it get analyzed as well.
12597 /*die_is_public=*/true,
12598 dwarf_dieoffset(&child));
12599 }
12600 else if (!rdr.env().analyze_exported_interfaces_only()
12601 || rdr.is_decl_die_with_exported_symbol(&child))
12602 {
12603 // Analyze all the DIEs we encounter unless we are asked to only
12604 // analyze exported interfaces and the types reachables from them.
12605 build_ir_node_from_die(rdr, &child,
12606 die_is_public_decl(&child),
12607 dwarf_dieoffset(&child));
12608 }
12609 while (dwarf_siblingof(&child, &child) == 0);
12610
12611 if (!rdr.var_decls_to_re_add_to_tree().empty())
12612 for (list<var_decl_sptr>::const_iterator v =
12613 rdr.var_decls_to_re_add_to_tree().begin();
12614 v != rdr.var_decls_to_re_add_to_tree().end();
12615 ++v)
12616 {
12617 if (is_member_decl(*v))
12618 continue;
12619
12620 ABG_ASSERT((*v)->get_scope());
12621 string demangled_name =
12622 demangle_cplus_mangled_name((*v)->get_linkage_name());
12623 if (!demangled_name.empty())
12624 {
12625 std::list<string> fqn_comps;
12626 fqn_to_components(demangled_name, fqn_comps);
12627 string mem_name = fqn_comps.back();
12628 fqn_comps.pop_back();
12629 class_decl_sptr class_type;
12630 string ty_name;
12631 if (!fqn_comps.empty())
12632 {
12633 ty_name = components_to_type_name(fqn_comps);
12634 class_type =
12635 lookup_class_type(ty_name, *rdr.cur_transl_unit());
12636 }
12637 if (class_type)
12638 {
12639 // So we are seeing a member variable for which there
12640 // is a global variable definition DIE not having a
12641 // reference attribute pointing back to the member
12642 // variable declaration DIE. Thus remove the global
12643 // variable definition from its current non-class
12644 // scope ...
12645 decl_base_sptr d;
12646 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12647 // This is the data member with the same name in cl.
12648 // We just need to flag it as static.
12649 ;
12650 else
12651 {
12652 // In this case there is no data member with the
12653 // same name in cl already. Let's add it there then
12654 // ...
12656 d = add_decl_to_scope(*v, class_type);
12657 }
12658
12659 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12660 // Let's flag the data member as static.
12661 set_member_is_static(d, true);
12662 }
12663 }
12664 }
12665 rdr.var_decls_to_re_add_to_tree().clear();
12666
12667 result->set_is_constructed(true);
12668
12669 return result;
12670}
12671
12672/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12673/// DW_TAG_module (for fortran) DIE.
12674///
12675/// Note that this function connects the DW_TAG_namespace to the IR
12676/// being currently created, reads the children of the DIE and
12677/// connects them to the IR as well.
12678///
12679/// @param rdr the DWARF reader to use.
12680///
12681/// @param die the DIE to read from. Must be either DW_TAG_namespace
12682/// or DW_TAG_module.
12683///
12684/// @param where_offset the offset of the DIE where we are "logically"
12685/// positionned at, in the DIE tree. This is useful when @p die is
12686/// e.g, DW_TAG_partial_unit that can be included in several places in
12687/// the DIE tree.
12688///
12689/// @return the resulting @ref abigail::namespace_decl or NULL if it
12690/// couldn't be created.
12692build_namespace_decl_and_add_to_ir(reader& rdr,
12693 Dwarf_Die* die,
12694 size_t where_offset)
12695{
12696 namespace_decl_sptr result;
12697
12698 if (!die)
12699 return result;
12700
12701 unsigned tag = dwarf_tag(die);
12702 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12703 return result;
12704
12705 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12706 /*called_for_public_decl=*/false,
12707 where_offset);
12708
12709 string name, linkage_name;
12710 location loc;
12711 die_loc_and_name(rdr, die, loc, name, linkage_name);
12712
12713 result.reset(new namespace_decl(rdr.env(), name, loc));
12714 add_decl_to_scope(result, scope.get());
12715 rdr.associate_die_to_decl(die, result, where_offset);
12716
12717 Dwarf_Die child;
12718 if (dwarf_child(die, &child) != 0)
12719 return result;
12720
12721 rdr.scope_stack().push(result.get());
12722 do
12723 build_ir_node_from_die(rdr, &child,
12724 // If this namespace DIE is private
12725 // (anonymous) then all its content is
12726 // considered private. Otherwise, its
12727 // public decls are considered public.
12728 /*called_from_public_decl=*/
12729 die_is_public_decl(die) && die_is_public_decl(&child),
12730 where_offset);
12731 while (dwarf_siblingof(&child, &child) == 0);
12732 rdr.scope_stack().pop();
12733
12734 return result;
12735}
12736
12737/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12738///
12739/// @param rdr the DWARF reader to use.
12740///
12741/// @param die the DW_TAG_base_type to consider.
12742///
12743/// @param where_offset where we are logically at in the DIE stream.
12744///
12745/// @return the resulting decl_base_sptr.
12746static type_decl_sptr
12747build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12748{
12749 type_decl_sptr result;
12750
12751 if (!die)
12752 return result;
12753 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12754
12755 uint64_t byte_size = 0, bit_size = 0;
12756 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12757 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12758 return result;
12759
12760 if (bit_size == 0 && byte_size != 0)
12761 // Update the bit size.
12762 bit_size = byte_size * 8;
12763
12764 string type_name, linkage_name;
12765 location loc;
12766 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12767
12768 if (byte_size == 0)
12769 {
12770 // The size of the type is zero, that must mean that we are
12771 // looking at the definition of the void type.
12772 if (type_name == "void")
12773 result = is_type_decl(build_ir_node_for_void_type(rdr));
12774 else
12775 // A type of size zero that is not void? Hmmh, I am not sure
12776 // what that means. Return nil for now.
12777 return result;
12778 }
12779
12780 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12781 {
12782 string normalized_type_name = type_name;
12783 integral_type int_type;
12784 if (parse_integral_type(type_name, int_type))
12785 normalized_type_name = int_type.to_string();
12786 result = lookup_basic_type(normalized_type_name, *corp);
12787 }
12788
12789 if (!result)
12790 if (corpus_sptr corp = rdr.corpus())
12791 result = lookup_basic_type(type_name, *corp);
12792 if (!result)
12793 result.reset(new type_decl(rdr.env(), type_name, bit_size,
12794 /*alignment=*/0, loc, linkage_name));
12795 rdr.associate_die_to_type(die, result, where_offset);
12796 return result;
12797}
12798
12799/// Construct the type that is to be used as the underlying type of an
12800/// enum.
12801///
12802/// @param rdr the DWARF reader to use.
12803///
12804/// @param enum_name the name of the enum that this type is going to
12805/// be the underlying type of.
12806///
12807/// @param enum_size the size of the enum.
12808///
12809/// @param is_anonymous whether the underlying type is anonymous or
12810/// not. By default, this should be set to true as before c++11 (and
12811/// in C), it's almost the case.
12812static type_decl_sptr
12813build_enum_underlying_type(reader& rdr,
12814 string enum_name,
12815 uint64_t enum_size,
12816 bool is_anonymous = true)
12817{
12818 string underlying_type_name =
12819 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12820 enum_size);
12821
12822 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12823 enum_size, enum_size, location()));
12824 result->set_is_anonymous(is_anonymous);
12825 result->set_is_artificial(true);
12826 translation_unit_sptr tu = rdr.cur_transl_unit();
12827 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12828 result = dynamic_pointer_cast<type_decl>(d);
12829 ABG_ASSERT(result);
12830 canonicalize(result);
12831 return result;
12832}
12833
12834/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12835///
12836/// @param rdr the DWARF reader to use.
12837///
12838/// @param die the DIE to read from.
12839///
12840/// @param scope the scope of the final enum. Note that this function
12841/// does *NOT* add the built type to this scope. The scope is just so
12842/// that the function knows how to name anonymous enums.
12843///
12844/// @param is_declaration_only is true if the DIE denoted by @p die is
12845/// a declaration-only DIE.
12846///
12847/// @return the built enum_type_decl or NULL if it could not be built.
12849build_enum_type(reader& rdr,
12850 Dwarf_Die* die,
12851 scope_decl* scope,
12852 size_t where_offset,
12853 bool is_declaration_only)
12854{
12855 enum_type_decl_sptr result;
12856 if (!die)
12857 return result;
12858
12859 unsigned tag = dwarf_tag(die);
12860 if (tag != DW_TAG_enumeration_type)
12861 return result;
12862
12863 string name, linkage_name;
12864 location loc;
12865 die_loc_and_name(rdr, die, loc, name, linkage_name);
12866
12867 bool is_anonymous = false;
12868 // If the enum is anonymous, let's give it a name.
12869 if (name.empty())
12870 {
12871 name = get_internal_anonymous_die_prefix_name(die);
12872 ABG_ASSERT(!name.empty());
12873 // But we remember that the type is anonymous.
12874 is_anonymous = true;
12875
12876 if (size_t s = scope->get_num_anonymous_member_enums())
12877 name = build_internal_anonymous_die_name(name, s);
12878 }
12879
12880 bool use_odr = rdr.odr_is_relevant(die);
12881 // If the type has location, then associate it to its
12882 // representation. This way, all occurences of types with the same
12883 // representation (name) and location can be later detected as being
12884 // for the same type.
12885
12886 if (!is_anonymous)
12887 {
12888 if (use_odr)
12889 {
12890 if (enum_type_decl_sptr pre_existing_enum =
12891 is_enum_type(rdr.lookup_artifact_from_die(die)))
12892 result = pre_existing_enum;
12893 }
12894 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12895 {
12896 if (loc)
12897 result = lookup_enum_type_per_location(loc.expand(), *corp);
12898 }
12899 else if (loc)
12900 {
12901 if (enum_type_decl_sptr pre_existing_enum =
12902 is_enum_type(rdr.lookup_artifact_from_die(die)))
12903 if (pre_existing_enum->get_location() == loc)
12904 result = pre_existing_enum;
12905 }
12906
12907 if (result)
12908 {
12909 rdr.associate_die_to_type(die, result, where_offset);
12910 return result;
12911 }
12912 }
12913 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12914 // we can look them up?
12915
12916 uint64_t size = 0;
12917 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12918 size *= 8;
12919 bool is_artificial = die_is_artificial(die);
12920
12921 // for now we consider that underlying types of enums are all anonymous
12922 bool enum_underlying_type_is_anonymous= true;
12923
12925 Dwarf_Die child;
12926 if (dwarf_child(die, &child) == 0)
12927 {
12928 do
12929 {
12930 if (dwarf_tag(&child) != DW_TAG_enumerator)
12931 continue;
12932
12933 string n, m;
12934 location l;
12935 die_loc_and_name(rdr, &child, l, n, m);
12936 uint64_t val = 0;
12937 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12938 enms.push_back(enum_type_decl::enumerator(n, val));
12939 }
12940 while (dwarf_siblingof(&child, &child) == 0);
12941 }
12942
12943 // DWARF up to version 4 (at least) doesn't seem to carry the
12944 // underlying type, so let's create an artificial one here, which
12945 // sole purpose is to be passed to the constructor of the
12946 // enum_type_decl type.
12947 type_decl_sptr t =
12948 build_enum_underlying_type(rdr, name, size,
12949 enum_underlying_type_is_anonymous);
12950 t->set_is_declaration_only(is_declaration_only);
12951
12952 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12953 result->set_is_anonymous(is_anonymous);
12954 result->set_is_declaration_only(is_declaration_only);
12955 result->set_is_artificial(is_artificial);
12956 rdr.associate_die_to_type(die, result, where_offset);
12957
12958 rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12959
12960 return result;
12961}
12962
12963/// Once a function_decl has been built and added to a class as a
12964/// member function, this function updates the information of the
12965/// function_decl concerning the properties of its relationship with
12966/// the member class. That is, it updates properties like
12967/// virtualness, access, constness, cdtorness, etc ...
12968///
12969/// @param die the DIE of the function_decl that has been just built.
12970///
12971/// @param f the function_decl that has just been built from @p die.
12972///
12973/// @param klass the @ref class_or_union that @p f belongs to.
12974///
12975/// @param rdr the context used to read the ELF/DWARF information.
12976static void
12977finish_member_function_reading(Dwarf_Die* die,
12978 const function_decl_sptr& f,
12979 const class_or_union_sptr klass,
12980 reader& rdr)
12981{
12982 ABG_ASSERT(klass);
12983
12984 method_decl_sptr m = is_method_decl(f);
12985 ABG_ASSERT(m);
12986
12987 method_type_sptr method_t = is_method_type(m->get_type());
12988 ABG_ASSERT(method_t);
12989
12990 size_t is_inline = die_is_declared_inline(die);
12991 bool is_ctor = (f->get_name() == klass->get_name());
12992 bool is_dtor = (!f->get_name().empty()
12993 && static_cast<string>(f->get_name())[0] == '~');
12994 bool is_virtual = die_is_virtual(die);
12995 int64_t vindex = -1;
12996 if (is_virtual)
12997 die_virtual_function_index(die, vindex);
12998 access_specifier access = public_access;
12999 if (class_decl_sptr c = is_class_type(klass))
13000 if (!c->is_struct())
13001 access = private_access;
13002 die_access_specifier(die, access);
13003
13004 m->is_declared_inline(is_inline);
13005 set_member_access_specifier(m, access);
13006 if (vindex != -1)
13008 if (is_virtual)
13009 set_member_function_is_virtual(m, is_virtual);
13010 bool is_static = method_t->get_is_for_static_method();
13011 set_member_is_static(m, is_static);
13012 set_member_function_is_ctor(m, is_ctor);
13013 set_member_function_is_dtor(m, is_dtor);
13014 set_member_function_is_const(m, method_t->get_is_const());
13015
13017
13018 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
13019 {
13020 // This is a virtual member function which has a linkage name
13021 // but has no underlying symbol set.
13022 //
13023 // The underlying elf symbol to set to this function can show up
13024 // later in the DWARF input or it can be that, because of some
13025 // compiler optimization, the relation between this function and
13026 // its underlying elf symbol is simply not emitted in the DWARF.
13027 //
13028 // Let's thus schedule this function for a later fixup pass
13029 // (performed by
13030 // reader::fixup_functions_with_no_symbols()) that will
13031 // set its underlying symbol.
13032 //
13033 // Note that if the underying symbol is encountered later in the
13034 // DWARF input, then the part of build_function_decl() that
13035 // updates the function to set its underlying symbol will
13036 // de-schedule this function wrt fixup pass.
13037 Dwarf_Off die_offset = dwarf_dieoffset(die);
13038 die_function_decl_map_type &fns_with_no_symbol =
13039 rdr.die_function_decl_with_no_symbol_map();
13040 die_function_decl_map_type::const_iterator i =
13041 fns_with_no_symbol.find(die_offset);
13042 if (i == fns_with_no_symbol.end())
13043 fns_with_no_symbol[die_offset] = f;
13044 }
13045
13046}
13047
13048/// If a function DIE has attributes which have not yet been read and
13049/// added to the internal representation that represents that function
13050/// then read those extra attributes and update the internal
13051/// representation.
13052///
13053/// @param rdr the DWARF reader to use.
13054///
13055/// @param die the function DIE to consider.
13056///
13057/// @param where_offset where we logical are, currently, in the stream
13058/// of DIEs. If you don't know what this is, you can just set it to zero.
13059///
13060/// @param existing_fn the representation of the function to update.
13061///
13062/// @return the updated function representation.
13063static function_decl_sptr
13064maybe_finish_function_decl_reading(reader& rdr,
13065 Dwarf_Die* die,
13066 size_t where_offset,
13067 const function_decl_sptr& existing_fn)
13068{
13069 function_decl_sptr result = build_function_decl(rdr, die,
13070 where_offset,
13071 existing_fn);
13072
13073 return result;
13074}
13075
13076/// Lookup a class or a typedef with a given qualified name in the
13077/// corpus that a given scope belongs to.
13078///
13079/// @param scope the scope to consider.
13080///
13081/// @param type_name the qualified name of the type to look for.
13082///
13083/// @return the typedef or class type found.
13084static type_base_sptr
13085lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
13086{
13087 string qname = build_qualified_name(scope, type_name);
13088 corpus* corp = scope->get_corpus();
13089 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
13090 return result;
13091}
13092
13093/// Lookup a class of typedef type from the current corpus being
13094/// constructed.
13095///
13096/// The type being looked for has the same name as a given DIE.
13097///
13098/// @param rdr the DWARF reader to use.
13099///
13100/// @param die the DIE which has the same name as the type we are
13101/// looking for.
13102///
13103/// @param called_for_public_decl whether this function is being
13104/// called from a a publicly defined declaration.
13105///
13106/// @param where_offset where we are logically at in the DIE stream.
13107///
13108/// @return the type found.
13109static type_base_sptr
13110lookup_class_or_typedef_from_corpus(reader& rdr,
13111 Dwarf_Die* die,
13112 bool called_for_public_decl,
13113 size_t where_offset)
13114{
13115 if (!die)
13116 return class_decl_sptr();
13117
13118 string class_name = die_string_attribute(die, DW_AT_name);
13119 if (class_name.empty())
13120 return class_decl_sptr();
13121
13122 scope_decl_sptr scope = get_scope_for_die(rdr, die,
13123 called_for_public_decl,
13124 where_offset);
13125 if (scope)
13126 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
13127
13128 return type_base_sptr();
13129}
13130
13131/// Lookup a class, typedef or enum type with a given qualified name
13132/// in the corpus that a given scope belongs to.
13133///
13134/// @param scope the scope to consider.
13135///
13136/// @param type_name the qualified name of the type to look for.
13137///
13138/// @return the typedef, enum or class type found.
13139static type_base_sptr
13140lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
13141 const string& type_name)
13142{
13143 string qname = build_qualified_name(scope, type_name);
13144 corpus* corp = scope->get_corpus();
13145 type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
13146 return result;
13147}
13148
13149/// Lookup a class, typedef or enum type in a given scope, in the
13150/// corpus that scope belongs to.
13151///
13152/// @param die the DIE of the class, typedef or enum to lookup.
13153///
13154/// @param anonymous_member_type_idx if @p DIE represents an anonymous
13155/// type, this is the index of that anonymous type in its scope, in
13156/// case there are several anonymous types of the same kind in that
13157/// scope.
13158///
13159/// @param scope the scope in which to look the type for.
13160///
13161/// @return the typedef, enum or class type found.
13162static type_base_sptr
13163lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
13164 size_t anonymous_member_type_idx,
13165 scope_decl* scope)
13166{
13167 if (!die)
13168 return class_decl_sptr();
13169
13170 string type_name = die_string_attribute(die, DW_AT_name);
13171 if (is_anonymous_type_die(die))
13172 type_name =
13173 get_internal_anonymous_die_name(die, anonymous_member_type_idx);
13174
13175 if (type_name.empty())
13176 return class_decl_sptr();
13177
13178 return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
13179}
13180
13181
13182/// Test if a DIE represents a function that is a member of a given
13183/// class type.
13184///
13185/// @param rdr the DWARF reader.
13186///
13187/// @param function_die the DIE of the function to consider.
13188///
13189/// @param class_type the class type to consider.
13190///
13191/// @param where_offset where we are logically at in the DIE stream.
13192///
13193/// @return the method declaration corresponding to the member
13194/// function of @p class_type, iff @p function_die is for a member
13195/// function of @p class_type.
13196static method_decl_sptr
13197is_function_for_die_a_member_of_class(reader& rdr,
13198 Dwarf_Die* function_die,
13199 const class_or_union_sptr& class_type)
13200{
13201 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
13202
13203 if (!artifact)
13204 return method_decl_sptr();
13205
13206 method_decl_sptr method = is_method_decl(artifact);
13207 method_type_sptr method_type;
13208
13209 if (method)
13210 method_type = method->get_type();
13211 else
13212 method_type = is_method_type(artifact);
13213 ABG_ASSERT(method_type);
13214
13215 class_or_union_sptr method_class = method_type->get_class_type();
13216 ABG_ASSERT(method_class);
13217
13218 string method_class_name = method_class->get_qualified_name(),
13219 class_type_name = class_type->get_qualified_name();
13220
13221 if (method_class_name == class_type_name)
13222 {
13223 //ABG_ASSERT(class_type.get() == method_class.get());
13224 return method;
13225 }
13226
13227 return method_decl_sptr();
13228}
13229
13230/// If a given function DIE represents an existing member function of
13231/// a given class, then update that member function with new
13232/// properties present in the DIE. Otherwise, if the DIE represents a
13233/// new member function that is not already present in the class then
13234/// add that new member function to the class.
13235///
13236/// @param rdr the DWARF reader.
13237///
13238/// @param function_die the DIE of the potential member function to
13239/// consider.
13240///
13241/// @param class_type the class type to consider.
13242///
13243/// @param called_from_public_decl is true iff this function was
13244/// called from a publicly defined and exported declaration.
13245///
13246/// @param where_offset where we are logically at in the DIE stream.
13247///
13248/// @return the method decl representing the member function.
13249static method_decl_sptr
13250add_or_update_member_function(reader& rdr,
13251 Dwarf_Die* function_die,
13252 const class_or_union_sptr& class_type,
13253 bool called_from_public_decl,
13254 size_t where_offset)
13255{
13256 method_decl_sptr method =
13257 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
13258
13259 if (!method)
13260 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
13261 class_type.get(),
13262 called_from_public_decl,
13263 where_offset));
13264 if (!method)
13265 return method_decl_sptr();
13266
13267 finish_member_function_reading(function_die,
13268 is_function_decl(method),
13269 class_type, rdr);
13270 return method;
13271}
13272
13273/// Build a an IR node for class type from a DW_TAG_structure_type or
13274/// DW_TAG_class_type DIE and add that node to the ABI corpus being
13275/// currently built.
13276///
13277/// If the represents class type that already exists, then update the
13278/// existing class type with the new properties found in the DIE.
13279///
13280/// It meanst that this function can also update an existing
13281/// class_decl node with data members, member functions and other
13282/// properties coming from the DIE.
13283///
13284/// @param rdr the DWARF reader to consider.
13285///
13286/// @param die the DIE to read information from. Must be either a
13287/// DW_TAG_structure_type or a DW_TAG_class_type.
13288///
13289/// @param scope a pointer to the scope_decl* under which this class
13290/// is to be added to.
13291///
13292/// @param is_struct whether the class was declared as a struct.
13293///
13294/// @param klass if non-null, this is a klass to append the members
13295/// to. Otherwise, this function just builds the class from scratch.
13296///
13297/// @param called_from_public_decl set to true if this class is being
13298/// called from a "Public declaration like vars or public symbols".
13299///
13300/// @param where_offset the offset of the DIE where we are "logically"
13301/// positionned at, in the DIE tree. This is useful when @p die is
13302/// e.g, DW_TAG_partial_unit that can be included in several places in
13303/// the DIE tree.
13304///
13305/// @param is_declaration_only is true if the DIE denoted by @p die is
13306/// a declaration-only DIE.
13307///
13308/// @return the resulting class_type.
13309static class_decl_sptr
13310add_or_update_class_type(reader& rdr,
13311 Dwarf_Die* die,
13312 scope_decl* scope,
13313 bool is_struct,
13314 class_decl_sptr klass,
13315 bool called_from_public_decl,
13316 size_t where_offset,
13317 bool is_declaration_only)
13318{
13319 class_decl_sptr result;
13320 if (!die)
13321 return result;
13322
13323 const die_source source = rdr.get_die_source(die);
13324
13325 unsigned tag = dwarf_tag(die);
13326
13327 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
13328 return result;
13329
13330 {
13331 die_class_or_union_map_type::const_iterator i =
13332 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13333 if (i != rdr.die_wip_classes_map(source).end())
13334 {
13335 class_decl_sptr class_type = is_class_type(i->second);
13336 ABG_ASSERT(class_type);
13337 return class_type;
13338 }
13339 }
13340
13341 string name, linkage_name;
13342 location loc;
13343 die_loc_and_name(rdr, die, loc, name, linkage_name);
13344
13345 bool is_anonymous = false;
13346 if (name.empty())
13347 {
13348 // So we are looking at an anonymous struct. Let's
13349 // give it a name.
13350 name = get_internal_anonymous_die_prefix_name(die);
13351 ABG_ASSERT(!name.empty());
13352 // But we remember that the type is anonymous.
13353 is_anonymous = true;
13354
13355 if (size_t s = scope->get_num_anonymous_member_classes())
13356 name = build_internal_anonymous_die_name(name, s);
13357 }
13358
13359 if (!is_anonymous)
13360 {
13361 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13362 {
13363 if (loc)
13364 // TODO: if there is only one class defined in the corpus
13365 // for this location, then re-use it. But if there are
13366 // more than one, then do not re-use it, for now.
13367 result = lookup_class_type_per_location(loc.expand(), *corp);
13368 else
13369 // TODO: if there is just one class for that name defined,
13370 // then re-use it. Otherwise, don't.
13371 result = lookup_class_type(name, *corp);
13372 if (result
13373 // If we are seeing a declaration of a definition we
13374 // already had, or if we are seing a type with the same
13375 // declaration-only-ness that we had before, then keep
13376 // the one we already had.
13377 && (result->get_is_declaration_only() == is_declaration_only
13378 || (!result->get_is_declaration_only()
13379 && is_declaration_only)))
13380 {
13381 rdr.associate_die_to_type(die, result, where_offset);
13382 return result;
13383 }
13384 else
13385 // We might be seeing the definition of a declaration we
13386 // already had. In that case, keep the definition and
13387 // drop the declaration.
13388 result.reset();
13389 }
13390 }
13391
13392 // If we've already seen the same class as 'die', then let's re-use
13393 // that one, unless it's an anonymous class. We can't really safely
13394 // re-use anonymous classes as they have no name, by construction.
13395 // What we can do, rather, is to reuse the typedef that name them,
13396 // when they do have a naming typedef.
13397 if (!is_anonymous)
13398 if (class_decl_sptr pre_existing_class =
13399 is_class_type(rdr.lookup_type_artifact_from_die(die)))
13400 klass = pre_existing_class;
13401
13402 uint64_t size = 0;
13403 die_size_in_bits(die, size);
13404 bool is_artificial = die_is_artificial(die);
13405
13406 Dwarf_Die child;
13407 bool has_child = (dwarf_child(die, &child) == 0);
13408
13409 decl_base_sptr res;
13410 if (klass)
13411 {
13412 res = result = klass;
13413 if (has_child && klass->get_is_declaration_only()
13414 && klass->get_definition_of_declaration())
13415 res = result = is_class_type(klass->get_definition_of_declaration());
13416 if (loc)
13417 result->set_location(loc);
13418 }
13419 else
13420 {
13421 result.reset(new class_decl(rdr.env(), name, size,
13422 /*alignment=*/0, is_struct, loc,
13423 decl_base::VISIBILITY_DEFAULT,
13424 is_anonymous));
13425
13426 result->set_is_declaration_only(is_declaration_only);
13427
13428 res = add_decl_to_scope(result, scope);
13429 result = dynamic_pointer_cast<class_decl>(res);
13430 ABG_ASSERT(result);
13431 }
13432
13433 if (!klass || klass->get_is_declaration_only())
13434 if (size != result->get_size_in_bits())
13435 result->set_size_in_bits(size);
13436
13437 if (klass)
13438 // We are amending a class that was built before. So let's check
13439 // if we need to amend its "declaration-only-ness" status.
13440 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
13441 // The size of the class doesn't match its
13442 // 'declaration-only-ness". We might have a non-zero sized
13443 // class which is declaration-only, or a zero sized class that
13444 // is not declaration-only. Let's set the declaration-only-ness
13445 // according to what we are instructed to.
13446 //
13447 // Note however that there are binaries out there emitted by
13448 // compilers (Clang, in C++) emit declarations-only classes that
13449 // have non-zero size. So we must honor these too. That is why
13450 // we are not forcing the declaration-only-ness to false when a
13451 // class has non-zero size. An example of such binary is
13452 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
13453 result->set_is_declaration_only(is_declaration_only);
13454
13455 // If a non-decl-only class has children node and is advertized as
13456 // having a non-zero size let's trust that.
13457 if (!result->get_is_declaration_only() && has_child)
13458 if (result->get_size_in_bits() == 0 && size != 0)
13459 result->set_size_in_bits(size);
13460
13461 result->set_is_artificial(is_artificial);
13462
13463 rdr.associate_die_to_type(die, result, where_offset);
13464
13465 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13466
13467 if (!has_child)
13468 // TODO: set the access specifier for the declaration-only class
13469 // here.
13470 return result;
13471
13472 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13473
13474 bool is_incomplete_type = false;
13475 if (is_declaration_only && size == 0 && has_child)
13476 // this is an incomplete DWARF type as defined by [5.7.1]
13477 //
13478 // An incomplete structure, union or class type is represented by
13479 // a structure, union or class entry that does not have a byte
13480 // size attribute and that has a DW_AT_declaration attribute.
13481 //
13482 // Let's consider that it's thus a decl-only class, likely
13483 // referred to by a pointer. If we later encounter a definition
13484 // for this decl-only class type, then this decl-only class will
13485 // be resolved to it by the code in
13486 // reader::resolve_declaration_only_classes.
13487 is_incomplete_type = true;
13488
13489 scope_decl_sptr scop =
13490 dynamic_pointer_cast<scope_decl>(res);
13491 ABG_ASSERT(scop);
13492 rdr.scope_stack().push(scop.get());
13493
13494 if (has_child && !is_incomplete_type)
13495 {
13496 int anonymous_member_class_index = -1;
13497 int anonymous_member_union_index = -1;
13498 int anonymous_member_enum_index = -1;
13499
13500 do
13501 {
13502 tag = dwarf_tag(&child);
13503
13504 // Handle base classes.
13505 if (tag == DW_TAG_inheritance)
13506 {
13507 result->set_is_declaration_only(false);
13508
13509 Dwarf_Die type_die;
13510 if (!die_die_attribute(&child, DW_AT_type, type_die))
13511 continue;
13512
13513 type_base_sptr base_type;
13514 if (!(base_type =
13515 lookup_class_or_typedef_from_corpus(rdr, &type_die,
13516 called_from_public_decl,
13517 where_offset)))
13518 {
13519 base_type =
13520 is_type(build_ir_node_from_die(rdr, &type_die,
13521 called_from_public_decl,
13522 where_offset));
13523 }
13524 // Sometimes base_type can be a typedef. Let's make
13525 // sure that typedef is compatible with a class type.
13527 if (!b)
13528 continue;
13529
13530 access_specifier access =
13531 is_struct
13532 ? public_access
13533 : private_access;
13534
13535 die_access_specifier(&child, access);
13536
13537 bool is_virt= die_is_virtual(&child);
13538 int64_t offset = 0;
13539 bool is_offset_present =
13540 die_member_offset(rdr, &child, offset);
13541
13542 class_decl::base_spec_sptr base(new class_decl::base_spec
13543 (b, access,
13544 is_offset_present ? offset : -1,
13545 is_virt));
13546 if (b->get_is_declaration_only()
13547 // Only non-anonymous decl-only classes are
13548 // scheduled for resolution to their definition.
13549 // Anonymous classes that are decl-only are likely
13550 // only artificially created by
13551 // get_opaque_version_of_type, from anonymous fully
13552 // defined classes. Those are never defined.
13553 && !b->get_qualified_name().empty())
13554 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
13555 if (result->find_base_class(b->get_qualified_name()))
13556 continue;
13557 result->add_base_specifier(base);
13558 }
13559 // Handle data members.
13560 else if (tag == DW_TAG_member
13561 || tag == DW_TAG_variable)
13562 {
13563 Dwarf_Die type_die;
13564 if (!die_die_attribute(&child, DW_AT_type, type_die))
13565 continue;
13566
13567 string n, m;
13568 location loc;
13569 die_loc_and_name(rdr, &child, loc, n, m);
13570 /// For now, we skip the hidden vtable pointer.
13571 /// Currently, we're looking for a member starting with
13572 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
13573 /// use as a name for the hidden vtable pointer.
13574 if (n.substr(0, 5) == "_vptr"
13575 && n.size() > 5
13576 && !std::isalnum(n.at(5))
13577 && n.at(5) != '_')
13578 continue;
13579
13580 // If the variable is already a member of this class,
13581 // move on. If it's an anonymous data member, we need
13582 // to handle it differently. We'll do that later below.
13583 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13584 continue;
13585
13586 int64_t offset_in_bits = 0;
13587 bool is_laid_out = die_member_offset(rdr, &child,
13588 offset_in_bits);
13589 // For now, is_static == !is_laid_out. When we have
13590 // templates, we'll try to be more specific. For now,
13591 // this approximation should do OK.
13592 bool is_static = !is_laid_out;
13593
13594 if (is_static && variable_is_suppressed(rdr,
13595 result.get(),
13596 &child,
13597 is_declaration_only))
13598 continue;
13599
13600 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
13601 called_from_public_decl,
13602 where_offset));
13603 type_base_sptr t = is_type(ty);
13604 if (!t)
13605 continue;
13606
13607 if (n.empty() && !die_is_anonymous_data_member(&child))
13608 {
13609 // We must be in a case where the data member has an
13610 // empty name because the DWARF emitter has a bug.
13611 // Let's generate an artificial name for that data
13612 // member.
13613 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
13614 ABG_ASSERT(!n.empty());
13615 }
13616
13617 // The call to build_ir_node_from_die above could have
13618 // triggered the adding of a data member named 'n' into
13619 // result. So let's check again if the variable is
13620 // already a member of this class. Here again, if it's
13621 // an anonymous data member, we need to handle it
13622 // differently. We'll do that later below.
13623 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13624 continue;
13625
13626 if (!is_static)
13627 // We have a non-static data member. So this class
13628 // cannot be a declaration-only class anymore, even if
13629 // some DWARF emitters might consider it otherwise.
13630 result->set_is_declaration_only(false);
13631 access_specifier access =
13632 is_struct
13633 ? public_access
13634 : private_access;
13635
13636 die_access_specifier(&child, access);
13637
13638 var_decl_sptr dm(new var_decl(n, t, loc, m));
13639 if (n.empty()
13641 // dm is an anonymous data member that was already
13642 // present in the current class so let's not add it.
13643 continue;
13644 result->add_data_member(dm, access, is_laid_out,
13645 is_static, offset_in_bits);
13646 ABG_ASSERT(has_scope(dm));
13647 rdr.associate_die_to_decl(&child, dm, where_offset,
13648 /*associate_by_repr=*/false);
13649 }
13650 // Handle member functions;
13651 else if (tag == DW_TAG_subprogram)
13652 {
13653 decl_base_sptr r =
13654 add_or_update_member_function(rdr, &child, result,
13655 called_from_public_decl,
13656 where_offset);
13658 rdr.associate_die_to_decl(&child, f, where_offset,
13659 /*associate_by_repr=*/true);
13660 }
13661 // Handle member types
13662 else if (die_is_type(&child))
13663 {
13664 // Track the anonymous type index in the current
13665 // scope. Look for what this means by reading the
13666 // comment of the function
13667 // build_internal_anonymous_die_name.
13668 int anonymous_member_type_index = 0;
13669 if (is_anonymous_type_die(&child))
13670 {
13671 // Update the anonymous type index.
13672 if (die_is_class_type(&child))
13673 anonymous_member_type_index =
13674 ++anonymous_member_class_index;
13675 else if (dwarf_tag(&child) == DW_TAG_union_type)
13676 anonymous_member_type_index =
13677 ++anonymous_member_union_index;
13678 else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13679 anonymous_member_type_index =
13680 ++anonymous_member_enum_index;
13681 }
13682 // if the type is not already a member of this class,
13683 // then add it to the class.
13684 if ((is_anonymous_type_die(&child)
13685 && !lookup_class_typedef_or_enum_type_from_corpus
13686 (&child, anonymous_member_type_index, result.get()))
13687 || !result->find_member_type(die_name(&child)))
13688 build_ir_node_from_die(rdr, &child, result.get(),
13689 called_from_public_decl,
13690 where_offset);
13691 }
13692 } while (dwarf_siblingof(&child, &child) == 0);
13693 }
13694
13695 rdr.scope_stack().pop();
13696
13697 {
13698 die_class_or_union_map_type::const_iterator i =
13699 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13700 if (i != rdr.die_wip_classes_map(source).end())
13701 {
13702 if (is_member_type(i->second))
13704 get_member_access_specifier(i->second));
13705 rdr.die_wip_classes_map(source).erase(i);
13706 }
13707 }
13708
13709 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13710 return result;
13711}
13712
13713/// Build an @ref union_decl from a DW_TAG_union_type DIE.
13714///
13715/// @param rdr the DWARF reader to use.
13716///
13717/// @param die the DIE to read from.
13718///
13719/// @param scope the scope the resulting @ref union_decl belongs to.
13720///
13721/// @param union_type if this parameter is non-nil, then this function
13722/// updates the @ref union_decl that it points to, rather than
13723/// creating a new @ref union_decl.
13724///
13725/// @param called_from_public_decl is true if this function has been
13726/// initially called within the context of a public decl.
13727///
13728/// @param where_offset the offset of the DIE where we are "logically"
13729/// positionned at, in the DIE tree. This is useful when @p die is
13730/// e.g, DW_TAG_partial_unit that can be included in several places in
13731/// the DIE tree.
13732///
13733/// @param is_declaration_only is true if the DIE denoted by @p die is
13734/// a declaration-only DIE.
13735///
13736/// @return the resulting @ref union_decl type.
13737static union_decl_sptr
13738add_or_update_union_type(reader& rdr,
13739 Dwarf_Die* die,
13740 scope_decl* scope,
13741 union_decl_sptr union_type,
13742 bool called_from_public_decl,
13743 size_t where_offset,
13744 bool is_declaration_only)
13745{
13746 union_decl_sptr result;
13747 if (!die)
13748 return result;
13749
13750 unsigned tag = dwarf_tag(die);
13751
13752 if (tag != DW_TAG_union_type)
13753 return result;
13754
13755 const die_source source = rdr.get_die_source(die);
13756 {
13757 die_class_or_union_map_type::const_iterator i =
13758 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13759 if (i != rdr.die_wip_classes_map(source).end())
13760 {
13761 union_decl_sptr u = is_union_type(i->second);
13762 ABG_ASSERT(u);
13763 return u;
13764 }
13765 }
13766
13767 string name, linkage_name;
13768 location loc;
13769 die_loc_and_name(rdr, die, loc, name, linkage_name);
13770
13771 bool is_anonymous = false;
13772 if (name.empty())
13773 {
13774 // So we are looking at an anonymous union. Let's give it a
13775 // name.
13776 name = get_internal_anonymous_die_prefix_name(die);
13777 ABG_ASSERT(!name.empty());
13778 // But we remember that the type is anonymous.
13779 is_anonymous = true;
13780
13781 if (size_t s = scope->get_num_anonymous_member_unions())
13782 name = build_internal_anonymous_die_name(name, s);
13783 }
13784
13785 // If the type has location, then associate it to its
13786 // representation. This way, all occurences of types with the same
13787 // representation (name) and location can be later detected as being
13788 // for the same type.
13789
13790 if (!is_anonymous)
13791 {
13792 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13793 {
13794 if (loc)
13795 result = lookup_union_type_per_location(loc.expand(), *corp);
13796 else
13797 result = lookup_union_type(name, *corp);
13798
13799 if (result)
13800 {
13801 rdr.associate_die_to_type(die, result, where_offset);
13802 return result;
13803 }
13804 }
13805 }
13806
13807 // if we've already seen a union with the same union as 'die' then
13808 // let's re-use that one. We can't really safely re-use anonymous
13809 // unions as they have no name, by construction. What we can do,
13810 // rather, is to reuse the typedef that name them, when they do have
13811 // a naming typedef.
13812 if (!is_anonymous)
13813 if (union_decl_sptr pre_existing_union =
13814 is_union_type(rdr.lookup_artifact_from_die(die)))
13815 union_type = pre_existing_union;
13816
13817 uint64_t size = 0;
13818 die_size_in_bits(die, size);
13819 bool is_artificial = die_is_artificial(die);
13820
13821 if (union_type)
13822 {
13823 result = union_type;
13824 result->set_location(loc);
13825 }
13826 else
13827 {
13828 result.reset(new union_decl(rdr.env(), name, size, loc,
13829 decl_base::VISIBILITY_DEFAULT,
13830 is_anonymous));
13831 if (is_declaration_only)
13832 result->set_is_declaration_only(true);
13833 result = is_union_type(add_decl_to_scope(result, scope));
13834 ABG_ASSERT(result);
13835 }
13836
13837 if (size)
13838 {
13839 result->set_size_in_bits(size);
13840 result->set_is_declaration_only(false);
13841 }
13842
13843 result->set_is_artificial(is_artificial);
13844
13845 rdr.associate_die_to_type(die, result, where_offset);
13846
13847 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13848
13849 Dwarf_Die child;
13850 bool has_child = (dwarf_child(die, &child) == 0);
13851 if (!has_child)
13852 return result;
13853
13854 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13855
13856 scope_decl_sptr scop =
13857 dynamic_pointer_cast<scope_decl>(result);
13858 ABG_ASSERT(scop);
13859 rdr.scope_stack().push(scop.get());
13860
13861 if (has_child)
13862 {
13863 do
13864 {
13865 tag = dwarf_tag(&child);
13866 // Handle data members.
13867 if (tag == DW_TAG_member || tag == DW_TAG_variable)
13868 {
13869 Dwarf_Die type_die;
13870 if (!die_die_attribute(&child, DW_AT_type, type_die))
13871 continue;
13872
13873 string n, m;
13874 location loc;
13875 die_loc_and_name(rdr, &child, loc, n, m);
13876
13877 // Because we can be updating an existing union, let's
13878 // make sure we don't already have a member of the same
13879 // name. Anonymous member are handled a bit later below
13880 // so let's not consider them here.
13881 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13882 continue;
13883
13884 ssize_t offset_in_bits = 0;
13885 decl_base_sptr ty =
13886 is_decl(build_ir_node_from_die(rdr, &type_die,
13887 called_from_public_decl,
13888 where_offset));
13889 type_base_sptr t = is_type(ty);
13890 if (!t)
13891 continue;
13892
13893 // We have a non-static data member. So this union
13894 // cannot be a declaration-only union anymore, even if
13895 // some DWARF emitters might consider it otherwise.
13896 result->set_is_declaration_only(false);
13897 access_specifier access = public_access;
13898
13899 die_access_specifier(&child, access);
13900
13901 var_decl_sptr dm(new var_decl(n, t, loc, m));
13902 // If dm is an anonymous data member, let's make sure
13903 // the current union doesn't already have it as a data
13904 // member.
13905 if (n.empty() && result->find_data_member(dm))
13906 continue;
13907
13908 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13909 continue;
13910
13911 result->add_data_member(dm, access, /*is_laid_out=*/true,
13912 /*is_static=*/false,
13913 offset_in_bits);
13914 ABG_ASSERT(has_scope(dm));
13915 rdr.associate_die_to_decl(&child, dm, where_offset,
13916 /*associate_by_repr=*/false);
13917 }
13918 // Handle member functions;
13919 else if (tag == DW_TAG_subprogram)
13920 {
13921 decl_base_sptr r =
13922 is_decl(build_ir_node_from_die(rdr, &child,
13923 result.get(),
13924 called_from_public_decl,
13925 where_offset));
13926 if (!r)
13927 continue;
13928
13929 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13930 ABG_ASSERT(f);
13931
13932 finish_member_function_reading(&child, f, result, rdr);
13933
13934 rdr.associate_die_to_decl(&child, f, where_offset,
13935 /*associate_by_repr=*/false);
13936 }
13937 // Handle member types
13938 else if (die_is_type(&child))
13939 decl_base_sptr td =
13940 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13941 called_from_public_decl,
13942 where_offset));
13943 } while (dwarf_siblingof(&child, &child) == 0);
13944 }
13945
13946 rdr.scope_stack().pop();
13947
13948 {
13949 die_class_or_union_map_type::const_iterator i =
13950 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13951 if (i != rdr.die_wip_classes_map(source).end())
13952 {
13953 if (is_member_type(i->second))
13955 get_member_access_specifier(i->second));
13956 rdr.die_wip_classes_map(source).erase(i);
13957 }
13958 }
13959
13960 return result;
13961}
13962
13963/// build a qualified type from a DW_TAG_const_type,
13964/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13965///
13966/// @param rdr the DWARF reader to consider.
13967///
13968/// @param die the input DIE to read from.
13969///
13970/// @param called_from_public_decl true if this function was called
13971/// from a context where either a public function or a public variable
13972/// is being built.
13973///
13974/// @param where_offset the offset of the DIE where we are "logically"
13975/// positionned at, in the DIE tree. This is useful when @p die is
13976/// e.g, DW_TAG_partial_unit that can be included in several places in
13977/// the DIE tree.
13978///
13979/// @return the resulting qualified_type_def.
13980static type_base_sptr
13981build_qualified_type(reader& rdr,
13982 Dwarf_Die* die,
13983 bool called_from_public_decl,
13984 size_t where_offset)
13985{
13986 type_base_sptr result;
13987 if (!die)
13988 return result;
13989
13990 unsigned tag = dwarf_tag(die);
13991
13992 if (tag != DW_TAG_const_type
13993 && tag != DW_TAG_volatile_type
13994 && tag != DW_TAG_restrict_type)
13995 return result;
13996
13997 Dwarf_Die underlying_type_die;
13998 decl_base_sptr utype_decl;
13999 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14000 // So, if no DW_AT_type is present, then this means (if we are
14001 // looking at a debug info emitted by GCC) that we are looking
14002 // at a qualified void type.
14003 utype_decl = build_ir_node_for_void_type(rdr);
14004
14005 if (!utype_decl)
14006 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
14007 called_from_public_decl,
14008 where_offset));
14009 if (!utype_decl)
14010 return result;
14011
14012 // The call to build_ir_node_from_die() could have triggered the
14013 // creation of the type for this DIE. In that case, just return it.
14014 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14015 {
14016 result = t;
14017 rdr.associate_die_to_type(die, result, where_offset);
14018 return result;
14019 }
14020
14021 type_base_sptr utype = is_type(utype_decl);
14022 ABG_ASSERT(utype);
14023
14024 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
14025 if (tag == DW_TAG_const_type)
14026 qual |= qualified_type_def::CV_CONST;
14027 else if (tag == DW_TAG_volatile_type)
14028 qual |= qualified_type_def::CV_VOLATILE;
14029 else if (tag == DW_TAG_restrict_type)
14030 qual |= qualified_type_def::CV_RESTRICT;
14031 else
14033
14034 if (!result)
14035 result.reset(new qualified_type_def(utype, qual, location()));
14036
14037 rdr.associate_die_to_type(die, result, where_offset);
14038
14039 return result;
14040}
14041
14042/// Walk a tree of typedef of qualified arrays and schedule all type
14043/// nodes for canonicalization.
14044///
14045/// This is to be used after an array tree has been cloned. In that
14046/// case, the newly cloned type nodes have to be scheduled for
14047/// canonicalization.
14048///
14049/// This is a subroutine of maybe_strip_qualification.
14050///
14051/// @param t the type node to be scheduled for canonicalization.
14052///
14053/// @param rdr the DWARF reader to use.
14054static void
14055schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
14056 reader &rdr)
14057{
14058 if (typedef_decl_sptr type = is_typedef(t))
14059 {
14060 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
14061 rdr);
14062 rdr.schedule_type_for_late_canonicalization(t);
14063 }
14064 else if (qualified_type_def_sptr type = is_qualified_type(t))
14065 {
14066 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
14067 rdr);
14068 rdr.schedule_type_for_late_canonicalization(t);
14069 }
14070 else if (array_type_def_sptr type = is_array_type(t))
14071 {
14072 for (vector<array_type_def::subrange_sptr>::const_iterator i =
14073 type->get_subranges().begin();
14074 i != type->get_subranges().end();
14075 ++i)
14076 {
14077 if (!(*i)->get_scope())
14078 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
14079 rdr.schedule_type_for_late_canonicalization(*i);
14080
14081 }
14082 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
14083 rdr);
14084 rdr.schedule_type_for_late_canonicalization(type);
14085 }
14086}
14087
14088/// Strip qualification from a qualified type, when it makes sense.
14089///
14090/// DWARF constructs "const reference". This is redundant because a
14091/// reference is always const. The issue is these redundant types then
14092/// leak into the IR and make for bad diagnostics.
14093///
14094/// This function thus strips the const qualifier from the type in
14095/// that case. It might contain code to strip other cases like this
14096/// in the future.
14097///
14098/// @param t the type to strip const qualification from.
14099///
14100/// @param rdr the @ref reader to use.
14101///
14102/// @return the stripped type or just return @p t.
14103static decl_base_sptr
14104maybe_strip_qualification(const qualified_type_def_sptr t,
14105 reader &rdr)
14106{
14107 if (!t)
14108 return t;
14109
14110 decl_base_sptr result = t;
14111 type_base_sptr u = t->get_underlying_type();
14112
14115 if (result.get() != t.get())
14116 return result;
14117
14119 {
14120 array_type_def_sptr array;
14121 scope_decl * scope = 0;
14122 if ((array = is_array_type(u)))
14123 {
14124 scope = array->get_scope();
14125 ABG_ASSERT(scope);
14126 array = is_array_type(clone_array_tree(array));
14127 schedule_array_tree_for_late_canonicalization(array, rdr);
14128 add_decl_to_scope(array, scope);
14129 t->set_underlying_type(array);
14130 u = t->get_underlying_type();
14131 }
14132 else if (is_typedef_of_array(u))
14133 {
14134 scope = is_decl(u)->get_scope();
14135 ABG_ASSERT(scope);
14136 typedef_decl_sptr typdef =
14138 schedule_array_tree_for_late_canonicalization(typdef, rdr);
14139 ABG_ASSERT(typdef);
14140 add_decl_to_scope(typdef, scope);
14141 t->set_underlying_type(typdef);
14142 u = t->get_underlying_type();
14143 array = is_typedef_of_array(u);
14144 }
14145 else
14147
14148 ABG_ASSERT(array);
14149 // We should not be editing types that are already canonicalized.
14150 ABG_ASSERT(!array->get_canonical_type());
14151 type_base_sptr element_type = array->get_element_type();
14152
14153 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
14154 {
14155 // We should not be editing types that are already canonicalized.
14156 ABG_ASSERT(!qualified->get_canonical_type());
14157 qualified_type_def::CV quals = qualified->get_cv_quals();
14158 quals |= t->get_cv_quals();
14159 qualified->set_cv_quals(quals);
14161 result = is_decl(u);
14162 }
14163 else
14164 {
14165 qualified_type_def_sptr qual_type
14166 (new qualified_type_def(element_type,
14167 t->get_cv_quals(),
14168 t->get_location()));
14170 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
14171 array->set_element_type(qual_type);
14172 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
14173 result = is_decl(u);
14174 }
14175 }
14176
14177 return result;
14178}
14179
14180/// Build a pointer type from a DW_TAG_pointer_type DIE.
14181///
14182/// @param rdr the DWARF reader to consider.
14183///
14184/// @param die the DIE to read information from.
14185///
14186/// @param called_from_public_decl true if this function was called
14187/// from a context where either a public function or a public variable
14188/// is being built.
14189///
14190/// @param where_offset the offset of the DIE where we are "logically"
14191/// positionned at, in the DIE tree. This is useful when @p die is
14192/// e.g, DW_TAG_partial_unit that can be included in several places in
14193/// the DIE tree.
14194///
14195/// @return the resulting pointer to pointer_type_def.
14197build_pointer_type_def(reader& rdr,
14198 Dwarf_Die* die,
14199 bool called_from_public_decl,
14200 size_t where_offset)
14201{
14202 pointer_type_def_sptr result;
14203
14204 if (!die)
14205 return result;
14206
14207 unsigned tag = dwarf_tag(die);
14208 if (tag != DW_TAG_pointer_type)
14209 return result;
14210
14211 type_or_decl_base_sptr utype_decl;
14212 Dwarf_Die underlying_type_die;
14213 bool has_underlying_type_die = false;
14214 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14215 // If the DW_AT_type attribute is missing, that means we are
14216 // looking at a pointer to "void".
14217 utype_decl = build_ir_node_for_void_type(rdr);
14218 else
14219 has_underlying_type_die = true;
14220
14221 if (!utype_decl && has_underlying_type_die)
14222 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
14223 called_from_public_decl,
14224 where_offset);
14225 if (!utype_decl)
14226 return result;
14227
14228 // The call to build_ir_node_from_die() could have triggered the
14229 // creation of the type for this DIE. In that case, just return it.
14230 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14231 {
14232 result = is_pointer_type(t);
14233 ABG_ASSERT(result);
14234 return result;
14235 }
14236
14237 type_base_sptr utype = is_type(utype_decl);
14238 ABG_ASSERT(utype);
14239
14240 // if the DIE for the pointer type doesn't have a byte_size
14241 // attribute then we assume the size of the pointer is the address
14242 // size of the current translation unit.
14243 uint64_t size = rdr.cur_transl_unit()->get_address_size();
14244 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14245 // The size as expressed by DW_AT_byte_size is in byte, so let's
14246 // convert it to bits.
14247 size *= 8;
14248
14249 // And the size of the pointer must be the same as the address size
14250 // of the current translation unit.
14251 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
14252
14253 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
14254 ABG_ASSERT(result->get_pointed_to_type());
14255
14256 if (is_void_pointer_type(result))
14257 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
14258
14259 rdr.associate_die_to_type(die, result, where_offset);
14260 return result;
14261}
14262
14263/// Build a reference type from either a DW_TAG_reference_type or
14264/// DW_TAG_rvalue_reference_type DIE.
14265///
14266/// @param rdr the DWARF reader to consider.
14267///
14268/// @param die the DIE to read from.
14269///
14270/// @param called_from_public_decl true if this function was called
14271/// from a context where either a public function or a public variable
14272/// is being built.
14273///
14274/// @param where_offset the offset of the DIE where we are "logically"
14275/// positionned at, in the DIE tree. This is useful when @p die is
14276/// e.g, DW_TAG_partial_unit that can be included in several places in
14277/// the DIE tree.
14278///
14279/// @return a pointer to the resulting reference_type_def.
14281build_reference_type(reader& rdr,
14282 Dwarf_Die* die,
14283 bool called_from_public_decl,
14284 size_t where_offset)
14285{
14287
14288 if (!die)
14289 return result;
14290
14291 unsigned tag = dwarf_tag(die);
14292 if (tag != DW_TAG_reference_type
14293 && tag != DW_TAG_rvalue_reference_type)
14294 return result;
14295
14296 Dwarf_Die underlying_type_die;
14297 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14298 return result;
14299
14300 type_or_decl_base_sptr utype_decl =
14301 build_ir_node_from_die(rdr, &underlying_type_die,
14302 called_from_public_decl,
14303 where_offset);
14304 if (!utype_decl)
14305 return result;
14306
14307 // The call to build_ir_node_from_die() could have triggered the
14308 // creation of the type for this DIE. In that case, just return it.
14309 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14310 {
14311 result = is_reference_type(t);
14312 ABG_ASSERT(result);
14313 return result;
14314 }
14315
14316 type_base_sptr utype = is_type(utype_decl);
14317 ABG_ASSERT(utype);
14318
14319 // if the DIE for the reference type doesn't have a byte_size
14320 // attribute then we assume the size of the reference is the address
14321 // size of the current translation unit.
14322 uint64_t size = rdr.cur_transl_unit()->get_address_size();
14323 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14324 size *= 8;
14325
14326 // And the size of the pointer must be the same as the address size
14327 // of the current translation unit.
14328 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
14329
14330 bool is_lvalue = tag == DW_TAG_reference_type;
14331
14332 result.reset(new reference_type_def(utype, is_lvalue, size,
14333 /*alignment=*/0,
14334 location()));
14335 if (corpus_sptr corp = rdr.corpus())
14336 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
14337 result = t;
14338 rdr.associate_die_to_type(die, result, where_offset);
14339 return result;
14340}
14341
14342/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
14343/// DW_TAG_ptr_to_member_type.
14344///
14345/// @param the DWARF reader touse.
14346///
14347/// @param the DIE to consider. It must carry the tag
14348/// DW_TAG_ptr_to_member_type.
14349///
14350/// @param called_from_public_decl true if this function was called
14351/// from a context where either a public function or a public variable
14352/// is being built.
14353///
14354/// @param where_offset the offset of the DIE where we are "logically"
14355/// positionned at, in the DIE tree. This is useful when @p die is
14356/// e.g, DW_TAG_partial_unit that can be included in several places in
14357/// the DIE tree.
14358///
14359/// @return a pointer to the resulting @ref ptr_to_mbr_type.
14361build_ptr_to_mbr_type(reader& rdr,
14362 Dwarf_Die* die,
14363 bool called_from_public_decl,
14364 size_t where_offset)
14365{
14366 ptr_to_mbr_type_sptr result;
14367
14368 if (!die)
14369 return result;
14370
14371 unsigned tag = dwarf_tag(die);
14372 if (tag != DW_TAG_ptr_to_member_type)
14373 return result;
14374
14375 Dwarf_Die data_member_type_die, containing_type_die;
14376
14377 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
14378 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
14379 return result;
14380
14381 type_or_decl_base_sptr data_member_type =
14382 build_ir_node_from_die(rdr, &data_member_type_die,
14383 called_from_public_decl, where_offset);
14384 if (!data_member_type)
14385 return result;
14386
14387 type_or_decl_base_sptr containing_type =
14388 build_ir_node_from_die(rdr, &containing_type_die,
14389 called_from_public_decl, where_offset);
14390 if (!containing_type)
14391 return result;
14392
14394 (is_type(containing_type)))
14395 return result;
14396
14397 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14398 {
14399 result = is_ptr_to_mbr_type(t);
14400 ABG_ASSERT(result);
14401 return result;
14402 }
14403
14404 uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
14405
14406 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
14407 is_type(data_member_type),
14408 is_type(containing_type),
14409 size_in_bits,
14410 /*alignment=*/0,
14411 location()));
14412
14413 rdr.associate_die_to_type(die, result, where_offset);
14414 return result;
14415}
14416
14417/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
14418///
14419/// @param rdr the DWARF reader to consider.
14420///
14421/// @param die the DIE to read from.
14422///
14423/// @param is_method points to a class or union declaration iff we're
14424/// building the type for a method. This is the enclosing class or
14425/// union of the method.
14426///
14427/// @param where_offset the offset of the DIE where we are "logically"
14428/// positioned at, in the DIE tree. This is useful when @p die is
14429/// e.g, DW_TAG_partial_unit that can be included in several places in
14430/// the DIE tree.
14431///
14432/// @return a pointer to the resulting function_type_sptr.
14433static function_type_sptr
14434build_function_type(reader& rdr,
14435 Dwarf_Die* die,
14436 class_or_union_sptr is_method,
14437 size_t where_offset)
14438{
14439 function_type_sptr result;
14440
14441 if (!die)
14442 return result;
14443
14444 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
14445 || dwarf_tag(die) == DW_TAG_subprogram);
14446
14447 const die_source source = rdr.get_die_source(die);
14448
14449 {
14450 size_t off = dwarf_dieoffset(die);
14451 auto i = rdr.die_wip_function_types_map(source).find(off);
14452 if (i != rdr.die_wip_function_types_map(source).end())
14453 {
14454 function_type_sptr fn_type = is_function_type(i->second);
14455 ABG_ASSERT(fn_type);
14456 return fn_type;
14457 }
14458 }
14459
14460 decl_base_sptr type_decl;
14461
14462 translation_unit_sptr tu = rdr.cur_transl_unit();
14463 ABG_ASSERT(tu);
14464
14465 /// If, inside the current translation unit, we've already seen a
14466 /// function type with the same text representation, then reuse that
14467 /// one instead.
14468 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
14469 {
14470 result = is_function_type(t);
14471 ABG_ASSERT(result);
14472 rdr.associate_die_to_type(die, result, where_offset);
14473 return result;
14474 }
14475
14476 bool odr_is_relevant = rdr.odr_is_relevant(die);
14477 if (odr_is_relevant)
14478 {
14479 // So we can rely on the One Definition Rule to say that if
14480 // several different function types have the same name (or
14481 // rather, representation) across the entire binary, then they
14482 // ought to designate the same function type. So let's ensure
14483 // that if we've already seen a function type with the same
14484 // representation as the function type 'die', then it's the same
14485 // type as the one denoted by 'die'.
14486 if (function_type_sptr fn_type =
14487 is_function_type(rdr.lookup_type_artifact_from_die(die)))
14488 {
14489 rdr.associate_die_to_type(die, fn_type, where_offset);
14490 return fn_type;
14491 }
14492 }
14493
14494 // Let's look at the DIE to detect if it's the DIE for a method
14495 // (type). If it is, we can deduce the name of its enclosing class
14496 // and if it's a static or const.
14497 bool is_const = false;
14498 bool is_static = false;
14499 Dwarf_Die object_pointer_die;
14500 Dwarf_Die class_type_die;
14501 bool has_this_parm_die =
14502 die_function_type_is_method_type(rdr, die, where_offset,
14503 object_pointer_die,
14504 class_type_die,
14505 is_static);
14506 if (has_this_parm_die)
14507 {
14508 // The function (type) has a "this" parameter DIE. It means it's
14509 // a member function DIE.
14510 if (!is_static)
14511 if (die_object_pointer_is_for_const_method(&object_pointer_die))
14512 is_const = true;
14513
14514 if (!is_method)
14515 {
14516 // We were initially called as if the function represented
14517 // by DIE was *NOT* a member function. But now we know it's
14518 // a member function. Let's take that into account.
14519 class_or_union_sptr klass_type =
14520 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
14521 /*called_from_pub_decl=*/true,
14522 where_offset));
14523 ABG_ASSERT(klass_type);
14524 is_method = klass_type;
14525 }
14526 }
14527
14528 // Let's create the type early and record it as being for the DIE
14529 // 'die'. This way, when building the sub-type triggers the
14530 // creation of a type matching the same 'die', then we'll reuse this
14531 // one.
14532
14533 result.reset(is_method
14534 ? new method_type(is_method, is_const,
14535 tu->get_address_size(),
14536 /*alignment=*/0)
14537 : new function_type(rdr.env(), tu->get_address_size(),
14538 /*alignment=*/0));
14539 rdr.associate_die_to_type(die, result, where_offset);
14540 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
14541
14542 type_base_sptr return_type;
14543 Dwarf_Die ret_type_die;
14544 if (die_die_attribute(die, DW_AT_type, ret_type_die))
14545 return_type =
14546 is_type(build_ir_node_from_die(rdr, &ret_type_die,
14547 /*called_from_public_decl=*/true,
14548 where_offset));
14549 if (!return_type)
14550 return_type = is_type(build_ir_node_for_void_type(rdr));
14551 result->set_return_type(return_type);
14552
14553 Dwarf_Die child;
14554 function_decl::parameters function_parms;
14555
14556 if (dwarf_child(die, &child) == 0)
14557 do
14558 {
14559 int child_tag = dwarf_tag(&child);
14560 if (child_tag == DW_TAG_formal_parameter)
14561 {
14562 // This is a "normal" function parameter.
14563 string name, linkage_name;
14564 location loc;
14565 die_loc_and_name(rdr, &child, loc, name, linkage_name);
14567 // Sometimes, bogus compiler emit names that are
14568 // non-ascii garbage. Let's just ditch that for now.
14569 name.clear();
14570 bool is_artificial = die_is_artificial(&child);
14571 type_base_sptr parm_type;
14572 Dwarf_Die parm_type_die;
14573 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
14574 parm_type =
14575 is_type(build_ir_node_from_die(rdr, &parm_type_die,
14576 /*called_from_public_decl=*/true,
14577 where_offset));
14578 if (!parm_type)
14579 continue;
14580 if (is_method
14581 && is_const_qualified_type(parm_type)
14582 && function_parms.empty())
14583 // We are looking at the first (implicit) parameter of a
14584 // method. This is basically the "this pointer". For
14585 // concrete instances of abstract methods, GCC sometimes
14586 // represents that pointer as a const pointer, whereas
14587 // in the abstract interface representing that method
14588 // the this-pointer is represented as a non-qualified
14589 // pointer. Let's trim the const qualifier away. That
14590 // will minize the chance to have spurious
14591 // const-qualifier changes on implicit parameters when
14592 // comparing methods that otherwise have no meaningful
14593 // ABI changes.
14594 parm_type =
14596
14598 (new function_decl::parameter(parm_type, name, loc,
14599 /*variadic_marker=*/false,
14600 is_artificial));
14601 function_parms.push_back(p);
14602 }
14603 else if (child_tag == DW_TAG_unspecified_parameters)
14604 {
14605 // This is a variadic function parameter.
14606 bool is_artificial = die_is_artificial(&child);
14607
14608 type_base_sptr parm_type =
14609 is_type(build_ir_node_for_variadic_parameter_type(rdr));
14611 (new function_decl::parameter(parm_type,
14612 /*name=*/"",
14613 location(),
14614 /*variadic_marker=*/true,
14615 is_artificial));
14616 function_parms.push_back(p);
14617 // After a DW_TAG_unspecified_parameters tag, we shouldn't
14618 // keep reading for parameters. The
14619 // unspecified_parameters TAG should be the last parameter
14620 // that we record. For instance, if there are multiple
14621 // DW_TAG_unspecified_parameters DIEs then we should care
14622 // only for the first one.
14623 break;
14624 }
14625 }
14626 while (dwarf_siblingof(&child, &child) == 0);
14627
14628 result->set_parameters(function_parms);
14629
14630 tu->bind_function_type_life_time(result);
14631
14632 result->set_is_artificial(true);
14633
14634 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
14635
14636 {
14637 die_function_type_map_type::const_iterator i =
14638 rdr.die_wip_function_types_map(source).
14639 find(dwarf_dieoffset(die));
14640 if (i != rdr.die_wip_function_types_map(source).end())
14641 rdr.die_wip_function_types_map(source).erase(i);
14642 }
14643
14644 maybe_canonicalize_type(result, rdr);
14645 return result;
14646}
14647
14648/// Build a subrange type from a DW_TAG_subrange_type.
14649///
14650/// @param rdr the DWARF reader to consider.
14651///
14652/// @param die the DIE to read from.
14653///
14654/// @param where_offset the offset of the DIE where we are "logically"
14655/// positionned at in the DIE tree. This is useful when @p die is
14656/// e,g, DW_TAG_partial_unit that can be included in several places in
14657/// the DIE tree.
14658///
14659/// @param associate_die_to_type if this is true then the resulting
14660/// type is associated to the @p die, so that next time when the
14661/// system looks up the type associated to it, the current resulting
14662/// type is returned. If false, then no association is done and the
14663/// resulting type can be destroyed right after. This can be useful
14664/// when the sole purpose of building the @ref
14665/// array_type_def::subrange_type is to use some of its method like,
14666/// e.g, its name pretty printing methods.
14667///
14668/// @return the newly built instance of @ref
14669/// array_type_def::subrange_type, or nil if no type could be built.
14671build_subrange_type(reader& rdr,
14672 const Dwarf_Die* die,
14673 size_t where_offset,
14674 bool associate_type_to_die)
14675{
14677
14678 if (!die)
14679 return result;
14680
14681 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
14682 if (tag != DW_TAG_subrange_type)
14683 return result;
14684
14685 string name = die_name(die);
14686
14687 // load the underlying type.
14688 Dwarf_Die underlying_type_die;
14689 type_base_sptr underlying_type;
14690 /* Unless there is an underlying type which says differently. */
14691 bool is_signed = false;
14692 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
14693 underlying_type =
14694 is_type(build_ir_node_from_die(rdr,
14695 &underlying_type_die,
14696 /*called_from_public_decl=*/true,
14697 where_offset));
14698
14699 if (underlying_type)
14700 {
14701 uint64_t ate;
14702 if (die_unsigned_constant_attribute (&underlying_type_die,
14703 DW_AT_encoding,
14704 ate))
14705 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
14706 }
14707
14708 // The DW_TAG_subrange_type DIE may have some size related
14709 // attributes (DW_AT_byte_size or DW_AT_bit_size). If not, then the
14710 // size is deduced from the size of its underlying type.
14711 bool has_size_info = false;
14712 uint64_t size = 0;
14713 if ((has_size_info = die_unsigned_constant_attribute(die,
14714 DW_AT_byte_size, size)))
14715 size *= 8;
14716 else
14717 has_size_info = die_unsigned_constant_attribute(die,
14718 DW_AT_bit_size, size);
14719
14720 translation_unit::language language = rdr.cur_transl_unit()->get_language();
14721 array_type_def::subrange_type::bound_value lower_bound =
14722 get_default_array_lower_bound(language);
14723 array_type_def::subrange_type::bound_value upper_bound;
14724 uint64_t count = 0;
14725 bool is_non_finite = false;
14726 bool non_zero_count_present = false;
14727
14728 // The DWARF 4 specifications says, in [5.11 Subrange
14729 // Type Entries]:
14730 //
14731 // The subrange entry may have the attributes
14732 // DW_AT_lower_bound and DW_AT_upper_bound to
14733 // specify, respectively, the lower and upper bound
14734 // values of the subrange.
14735 //
14736 // So let's look for DW_AT_lower_bound first.
14737 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14738
14739 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
14740 is_signed, upper_bound);
14741 if (!found_upper_bound)
14742 found_upper_bound = subrange_die_indirect_bound_value(die,
14743 DW_AT_upper_bound,
14744 upper_bound,
14745 is_signed);
14746 // Then, DW_AT_upper_bound.
14747 if (!found_upper_bound)
14748 {
14749 // The DWARF 4 spec says, in [5.11 Subrange Type
14750 // Entries]:
14751 //
14752 // The DW_AT_upper_bound attribute may be replaced
14753 // by a DW_AT_count attribute, whose value
14754 // describes the number of elements in the
14755 // subrange rather than the value of the last
14756 // element."
14757 //
14758 // So, as DW_AT_upper_bound is not present in this
14759 // case, let's see if there is a DW_AT_count.
14760 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
14761 {
14762 if (count)
14763 // DW_AT_count can be present and be set to zero. This is
14764 // for instance the case to model this gcc extension to
14765 // represent flexible arrays:
14766 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
14767 // For instance: int flex_array[0];
14768 non_zero_count_present = true;
14769
14770 // When the count is present and non-zero, we can deduce the
14771 // upper_bound from the lower_bound and the number of
14772 // elements of the array:
14773 int64_t u = lower_bound.get_signed_value() + count;
14774 if (u)
14775 upper_bound = u - 1;
14776 }
14777
14778 if (!non_zero_count_present)
14779 // No upper_bound nor count was present on the DIE, this means
14780 // the array is considered to have an infinite (or rather not
14781 // known) size.
14782 is_non_finite = true;
14783 }
14784
14785 if (UINT64_MAX == upper_bound.get_unsigned_value())
14786 // If the upper_bound size is the max of the integer value
14787 // then it most certainly means unknown size.
14788 is_non_finite = true;
14789
14790 result.reset
14791 (new array_type_def::subrange_type(rdr.env(),
14792 name,
14793 lower_bound,
14794 upper_bound,
14795 underlying_type,
14796 location()));
14797 result->is_non_finite(is_non_finite);
14798
14799 if (has_size_info)
14800 result->set_size_in_bits(size);
14801 else
14802 {
14803 // The DW_TAG_subrange_type doesn't appear to have any size
14804 // attribute. In that case, the size is deduced from the size
14805 // of the underlying type. If there is no underlying type
14806 // specified, then the size of the subrange type is the size
14807 if (!underlying_type)
14808 result->set_size_in_bits(rdr.cur_transl_unit()->get_address_size());
14809 }
14810
14811 // Let's ensure the resulting subrange looks metabolically healthy.
14812 ABG_ASSERT(result->is_non_finite()
14813 || (result->get_length() ==
14814 (uint64_t) (result->get_upper_bound()
14815 - result->get_lower_bound() + 1)));
14816
14817 if (associate_type_to_die)
14818 rdr.associate_die_to_type(die, result, where_offset);
14819
14820 return result;
14821}
14822
14823/// Build the sub-ranges of an array type.
14824///
14825/// This is a sub-routine of build_array_type().
14826///
14827/// @param rdr the context to read from.
14828///
14829/// @param die the DIE of tag DW_TAG_array_type which contains
14830/// children DIEs that represent the sub-ranges.
14831///
14832/// @param subranges out parameter. This is set to the sub-ranges
14833/// that are built from @p die.
14834///
14835/// @param where_offset the offset of the DIE where we are "logically"
14836/// positioned at, in the DIE tree. This is useful when @p die is
14837/// e.g, DW_TAG_partial_unit that can be included in several places in
14838/// the DIE tree.
14839static void
14840build_subranges_from_array_type_die(reader& rdr,
14841 const Dwarf_Die* die,
14843 size_t where_offset,
14844 bool associate_type_to_die)
14845{
14846 Dwarf_Die child;
14847
14848 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14849 {
14850 do
14851 {
14852 int child_tag = dwarf_tag(&child);
14853 if (child_tag == DW_TAG_subrange_type)
14854 {
14856 if (associate_type_to_die)
14857 {
14858 // We are being called to create the type, add it to
14859 // the current type graph and associate it to the
14860 // DIE it's been created from.
14862 build_ir_node_from_die(rdr, &child,
14863 /*called_from_public_decl=*/true,
14864 where_offset);
14865 s = is_subrange_type(t);
14866 }
14867 else
14868 // We are being called to create the type but *NOT*
14869 // add it to the current tyupe tree, *NOR* associate
14870 // it to the DIE it's been created from.
14871 s = build_subrange_type(rdr, &child,
14872 where_offset,
14873 /*associate_type_to_die=*/false);
14874 if (s)
14875 subranges.push_back(s);
14876 }
14877 }
14878 while (dwarf_siblingof(&child, &child) == 0);
14879 }
14880}
14881
14882/// Build an array type from a DW_TAG_array_type DIE.
14883///
14884/// @param rdr the DWARF reader to consider.
14885///
14886/// @param die the DIE to read from.
14887///
14888/// @param called_from_public_decl true if this function was called
14889/// from a context where either a public function or a public variable
14890/// is being built.
14891///
14892/// @param where_offset the offset of the DIE where we are "logically"
14893/// positioned at, in the DIE tree. This is useful when @p die is
14894/// e.g, DW_TAG_partial_unit that can be included in several places in
14895/// the DIE tree.
14896///
14897/// @return a pointer to the resulting array_type_def.
14899build_array_type(reader& rdr,
14900 Dwarf_Die* die,
14901 bool called_from_public_decl,
14902 size_t where_offset)
14903{
14904 array_type_def_sptr result;
14905
14906 if (!die)
14907 return result;
14908
14909 unsigned tag = dwarf_tag(die);
14910 if (tag != DW_TAG_array_type)
14911 return result;
14912
14913 decl_base_sptr type_decl;
14914 Dwarf_Die type_die;
14915
14916 if (die_die_attribute(die, DW_AT_type, type_die))
14917 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14918 called_from_public_decl,
14919 where_offset));
14920 if (!type_decl)
14921 return result;
14922
14923 // The call to build_ir_node_from_die() could have triggered the
14924 // creation of the type for this DIE. In that case, just return it.
14925 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14926 {
14927 result = is_array_type(t);
14928 ABG_ASSERT(result);
14929 return result;
14930 }
14931
14932 type_base_sptr type = is_type(type_decl);
14933 ABG_ASSERT(type);
14934
14936
14937 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14938
14939 result.reset(new array_type_def(type, subranges, location()));
14940
14941 return result;
14942}
14943
14944/// Create a typedef_decl from a DW_TAG_typedef DIE.
14945///
14946/// @param rdr the DWARF reader to consider.
14947///
14948/// @param die the DIE to read from.
14949///
14950/// @param called_from_public_decl true if this function was called
14951/// from a context where either a public function or a public variable
14952/// is being built.
14953///
14954/// @param where_offset the offset of the DIE where we are "logically"
14955/// positionned at, in the DIE tree. This is useful when @p die is
14956/// e.g, DW_TAG_partial_unit that can be included in several places in
14957/// the DIE tree.
14958///
14959/// @return the newly created typedef_decl.
14960static typedef_decl_sptr
14961build_typedef_type(reader& rdr,
14962 Dwarf_Die* die,
14963 bool called_from_public_decl,
14964 size_t where_offset)
14965{
14966 typedef_decl_sptr result;
14967
14968 if (!die)
14969 return result;
14970
14971 unsigned tag = dwarf_tag(die);
14972 if (tag != DW_TAG_typedef)
14973 return result;
14974
14975 string name, linkage_name;
14976 location loc;
14977 die_loc_and_name(rdr, die, loc, name, linkage_name);
14978
14979 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14980 if (loc)
14981 result = lookup_typedef_type_per_location(loc.expand(), *corp);
14982
14983 if (!result)
14984 {
14985 type_base_sptr utype;
14986 Dwarf_Die underlying_type_die;
14987 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14988 // A typedef DIE with no underlying type means a typedef to
14989 // void type.
14990 utype = rdr.env().get_void_type();
14991
14992 if (!utype)
14993 utype =
14994 is_type(build_ir_node_from_die(rdr,
14995 &underlying_type_die,
14996 called_from_public_decl,
14997 where_offset));
14998 if (!utype)
14999 return result;
15000
15001 ABG_ASSERT(utype);
15002 result.reset(new typedef_decl(name, utype, loc, linkage_name));
15003
15004 if ((is_class_or_union_type(utype) || is_enum_type(utype))
15005 && is_anonymous_type(utype))
15006 {
15007 // This is a naming typedef for an enum or a class. Let's
15008 // mark the underlying decl as such.
15009 decl_base_sptr decl = is_decl(utype);
15010 ABG_ASSERT(decl);
15011 decl->set_naming_typedef(result);
15012 if (is_class_or_union_type(utype))
15013 rdr.maybe_schedule_declaration_only_class_for_resolution
15014 (is_class_or_union_type(utype));
15015 else if (is_enum_type(utype))
15016 rdr.maybe_schedule_declaration_only_enum_for_resolution
15017 (is_enum_type(utype));
15018 }
15019 }
15020
15021 rdr.associate_die_to_type(die, result, where_offset);
15022
15023 return result;
15024}
15025
15026/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
15027/// denoted by the DIE is not suppressed by a suppression
15028/// specification associated to the current DWARF reader.
15029///
15030/// Note that if a member variable declaration with the same name as
15031/// the name of the DIE we are looking at exists, this function returns
15032/// that existing variable declaration.
15033///
15034/// @param rdr the DWARF reader to use.
15035///
15036/// @param die the DIE representing the variable we are looking at.
15037///
15038/// @param where_offset the offset of the DIE where we are "logically"
15039/// positionned at, in the DIE tree. This is useful when @p die is
15040/// e.g, DW_TAG_partial_unit that can be included in several places in
15041/// the DIE tree.
15042///
15043/// @param is_declaration_only if true, it means the variable DIE has
15044/// the is_declaration_only only attribute.
15045///
15046/// @param result if this is set to an existing var_decl, this means
15047/// that the function will append the new properties it sees on @p die
15048/// to that exising var_decl. Otherwise, if this parameter is NULL, a
15049/// new var_decl is going to be allocated and returned.
15050///
15051/// @param is_required_decl_spec this is true iff the variable to
15052/// build is referred to as being the specification of another
15053/// variable.
15054///
15055/// @return a pointer to the newly created var_decl. If the var_decl
15056/// could not be built, this function returns NULL.
15057static var_decl_sptr
15058build_or_get_var_decl_if_not_suppressed(reader& rdr,
15059 scope_decl *scope,
15060 Dwarf_Die *die,
15061 size_t where_offset,
15062 bool is_declaration_only,
15063 var_decl_sptr result,
15064 bool is_required_decl_spec)
15065{
15066 var_decl_sptr var;
15067 if (variable_is_suppressed(rdr, scope, die,
15068 is_declaration_only,
15069 is_required_decl_spec))
15070 return var;
15071
15072 if (class_decl* class_type = is_class_type(scope))
15073 {
15074 string var_name = die_name(die);
15075 if (!var_name.empty())
15076 if ((var = class_type->find_data_member(var_name)))
15077 return var;
15078 }
15079 var = build_var_decl(rdr, die, where_offset, result);
15080 return var;
15081}
15082
15083/// Build a @ref var_decl out of a DW_TAG_variable DIE.
15084///
15085/// @param rdr the DWARF reader to use.
15086///
15087/// @param die the DIE representing the variable we are looking at.
15088///
15089/// @param where_offset the offset of the DIE where we are "logically"
15090/// positionned at, in the DIE tree. This is useful when @p die is
15091/// e.g, DW_TAG_partial_unit that can be included in several places in
15092/// the DIE tree.
15093///
15094/// @param result if this is set to an existing var_decl, this means
15095/// that the function will append the new properties it sees on @p die
15096/// to that exising var_decl. Otherwise, if this parameter is NULL, a
15097/// new var_decl is going to be allocated and returned.
15098///
15099/// @return a pointer to the newly created var_decl. If the var_decl
15100/// could not be built, this function returns NULL.
15101static var_decl_sptr
15102build_var_decl(reader& rdr,
15103 Dwarf_Die *die,
15104 size_t where_offset,
15105 var_decl_sptr result)
15106{
15107 if (!die)
15108 return result;
15109
15110 int tag = dwarf_tag(die);
15111 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
15112
15113 if (!die_is_public_decl(die))
15114 return result;
15115
15116 type_base_sptr type;
15117 Dwarf_Die type_die;
15118 if (die_die_attribute(die, DW_AT_type, type_die))
15119 {
15120 decl_base_sptr ty =
15121 is_decl(build_ir_node_from_die(rdr, &type_die,
15122 /*called_from_public_decl=*/true,
15123 where_offset));
15124 if (!ty)
15125 return result;
15126 type = is_type(ty);
15127 ABG_ASSERT(type);
15128 }
15129
15130 if (!type && !result)
15131 return result;
15132
15133 string name, linkage_name;
15134 location loc;
15135 die_loc_and_name(rdr, die, loc, name, linkage_name);
15136
15137 if (!result)
15138 result.reset(new var_decl(name, type, loc, linkage_name));
15139 else
15140 {
15141 // We were called to append properties that might have been
15142 // missing from the first version of the variable. And usually
15143 // that missing property is the mangled name or the type.
15144 if (!linkage_name.empty())
15145 result->set_linkage_name(linkage_name);
15146
15147 if (type)
15148 result->set_type(type);
15149 }
15150
15151 // Check if a variable symbol with this name is exported by the elf
15152 // binary. If it is, then set the symbol of the variable, if it's
15153 // not set already.
15154 if (!result->get_symbol())
15155 {
15156 elf_symbol_sptr var_sym;
15157 Dwarf_Addr var_addr;
15158
15159 if (rdr.get_variable_address(die, var_addr))
15160 {
15161 rdr.symtab()->
15162 update_main_symbol(var_addr,
15163 result->get_linkage_name().empty()
15164 ? result->get_name()
15165 : result->get_linkage_name());
15166 var_sym = rdr.variable_symbol_is_exported(var_addr);
15167 }
15168
15169 if (var_sym)
15170 {
15171 result->set_symbol(var_sym);
15172 // If the linkage name is not set or is wrong, set it to
15173 // the name of the underlying symbol.
15174 string linkage_name = result->get_linkage_name();
15175 if (linkage_name.empty()
15176 || !var_sym->get_alias_from_name(linkage_name))
15177 result->set_linkage_name(var_sym->get_name());
15178 result->set_is_in_public_symbol_table(true);
15179 }
15180
15181 if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
15182 {
15183 // We are looking at a global variable which symbol is
15184 // undefined. Let's set its symbol.
15185 string n = result->get_linkage_name();
15186 if (n.empty())
15187 n = result->get_name();
15188 var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
15189 if (var_sym)
15190 {
15191 result->set_symbol(var_sym);
15192 result->set_is_in_public_symbol_table(false);
15193 }
15194 }
15195 }
15196
15197 return result;
15198}
15199
15200/// Test if a given function denoted by its DIE and its scope is
15201/// suppressed by any of the suppression specifications associated to
15202/// a given context of ELF/DWARF reading.
15203///
15204/// Note that a non-member function which symbol is not exported is
15205/// also suppressed.
15206///
15207/// @param rdr the ELF/DWARF reading content of interest.
15208///
15209/// @param scope of the scope of the function.
15210///
15211/// @param function_die the DIE representing the function.
15212///
15213/// @param is_declaration_only is true if the DIE denoted by @p die is
15214/// a declaration-only DIE.
15215///
15216/// @return true iff @p function_die is suppressed by at least one
15217/// suppression specification attached to the @p rdr.
15218static bool
15219function_is_suppressed(const reader& rdr,
15220 const scope_decl* scope,
15221 Dwarf_Die *function_die,
15222 bool is_declaration_only)
15223{
15224 if (function_die == 0
15225 || dwarf_tag(function_die) != DW_TAG_subprogram)
15226 return false;
15227
15228 string fname = die_string_attribute(function_die, DW_AT_name);
15229 string flinkage_name = die_linkage_name(function_die);
15230 if (flinkage_name.empty() && die_is_in_c(function_die))
15231 flinkage_name = fname;
15232 string qualified_name = build_qualified_name(scope, fname);
15233
15234 // A non-member non-static function which symbol is not exported is
15235 // suppressed.
15236 //
15237 // Note that if the non-member non-static function has an undefined
15238 // symbol, by default, it's not suppressed. Unless we are asked to
15239 // drop undefined symbols too.
15240 if (!is_class_type(scope)
15241 && (!is_declaration_only || rdr.drop_undefined_syms()))
15242 {
15243 Dwarf_Addr fn_addr;
15244 if (!rdr.get_function_address(function_die, fn_addr))
15245 return true;
15246
15247 elf_symbol_sptr symbol =
15248 rdr.function_symbol_is_exported(fn_addr);
15249 if (!symbol)
15250 return true;
15251 if (!symbol->is_suppressed())
15252 return false;
15253
15254 // Since there is only one symbol in DWARF associated with an elf_symbol,
15255 // we can assume this is the main symbol then. Otherwise the main hinting
15256 // did not work as expected.
15257 ABG_ASSERT(symbol->is_main_symbol());
15258 if (symbol->has_aliases())
15259 for (elf_symbol_sptr a = symbol->get_next_alias();
15260 !a->is_main_symbol(); a = a->get_next_alias())
15261 if (!a->is_suppressed())
15262 return false;
15263 }
15264
15265 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
15266 /*require_drop_property=*/true);
15267}
15268
15269/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
15270/// function denoted by the DIE is not suppressed by a suppression
15271/// specification associated to the current DWARF reader.
15272///
15273/// Note that if a member function declaration with the same signature
15274/// (pretty representation) as one of the DIE we are looking at
15275/// exists, this function returns that existing function declaration.
15276/// Similarly, if there is already a constructed member function with
15277/// the same linkage name as the one on the DIE, this function returns
15278/// that member function.
15279///
15280/// Also note that the function_decl IR returned by this function must
15281/// be passed to finish_member_function_reading because several
15282/// properties from the DIE are actually read by that function, and
15283/// the corresponding properties on the function_decl IR are updated
15284/// accordingly. This is done to support "updating" a function_decl
15285/// IR with properties scathered across several DIEs.
15286///
15287/// @param rdr the DWARF reader to use.
15288///
15289/// @param scope the scope of the function we are looking at.
15290///
15291/// @param fn_die the DIE representing the function we are looking at.
15292///
15293/// @param where_offset the offset of the DIE where we are "logically"
15294/// positionned at, in the DIE tree. This is useful when @p die is
15295/// e.g, DW_TAG_partial_unit that can be included in several places in
15296/// the DIE tree.
15297///
15298/// @param is_declaration_only is true if the DIE denoted by @p fn_die
15299/// is a declaration-only DIE.
15300///
15301/// @param result if this is set to an existing function_decl, this
15302/// means that the function will append the new properties it sees on
15303/// @p fn_die to that exising function_decl. Otherwise, if this
15304/// parameter is NULL, a new function_decl is going to be allocated
15305/// and returned.
15306///
15307/// @return a pointer to the newly created var_decl. If the var_decl
15308/// could not be built, this function returns NULL.
15309static function_decl_sptr
15310build_or_get_fn_decl_if_not_suppressed(reader& rdr,
15311 scope_decl *scope,
15312 Dwarf_Die *fn_die,
15313 size_t where_offset,
15314 bool is_declaration_only,
15315 function_decl_sptr result)
15316{
15318 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
15319 return fn;
15320
15321 string name = die_name(fn_die);
15322 string linkage_name = die_linkage_name(fn_die);
15323 bool is_dtor = !name.empty() && name[0]== '~';
15324 bool is_virtual = false;
15325 if (is_dtor)
15326 {
15327 Dwarf_Attribute attr;
15328 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
15329 DW_AT_vtable_elem_location,
15330 &attr))
15331 is_virtual = true;
15332 }
15333
15334
15335 // If we've already built an IR for a function with the same
15336 // signature (from another DIE), reuse it, unless that function is a
15337 // virtual C++ destructor. Several virtual C++ destructors with the
15338 // same signature can be implemented by several different ELF
15339 // symbols. So re-using C++ destructors like that can lead to us
15340 // missing some destructors.
15341 if (!result && (!(is_dtor && is_virtual)))
15342 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
15343 {
15344 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
15345 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
15346 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
15347 return fn;
15348 }
15349
15350 // If a member function with the same linkage name as the one
15351 // carried by the DIE already exists, then return it.
15352 if (class_decl* klass = is_class_type(scope))
15353 {
15354 string linkage_name = die_linkage_name(fn_die);
15355 fn = klass->find_member_function_sptr(linkage_name);
15356 if (fn)
15357 // We found a member function that has the same signature.
15358 // Let's mark it for update.
15359 result = fn;
15360 }
15361
15362 if (!fn || !fn->get_symbol())
15363 // We haven't yet been able to construct a function IR, or, we
15364 // have one 'partial' function IR that doesn't have any associated
15365 // symbol yet. Note that in the later case, a function IR without
15366 // any associated symbol will be dropped on the floor by
15367 // potential_member_fn_should_be_dropped. So let's build or a new
15368 // function IR or complete the existing partial IR.
15369 fn = build_function_decl(rdr, fn_die, where_offset, result);
15370
15371 return fn;
15372}
15373
15374/// Test if a given variable denoted by its DIE and its scope is
15375/// suppressed by any of the suppression specifications associated to
15376/// a given context of ELF/DWARF reading.
15377///
15378/// @param rdr the ELF/DWARF reading content of interest.
15379///
15380/// @param scope of the scope of the variable.
15381///
15382/// @param variable_die the DIE representing the variable.
15383///
15384/// @param is_declaration_only true if the variable is supposed to be
15385/// decl-only.
15386///
15387/// @param is_required_decl_spec if true, means that the @p
15388/// variable_die being considered is for a variable decl that is a
15389/// specification for a concrete variable being built.
15390///
15391/// @return true iff @p variable_die is suppressed by at least one
15392/// suppression specification attached to the @p rdr.
15393static bool
15394variable_is_suppressed(const reader& rdr,
15395 const scope_decl* scope,
15396 Dwarf_Die *variable_die,
15397 bool is_declaration_only,
15398 bool is_required_decl_spec)
15399{
15400 if (variable_die == 0
15401 || (dwarf_tag(variable_die) != DW_TAG_variable
15402 && dwarf_tag(variable_die) != DW_TAG_member))
15403 return false;
15404
15405 string name = die_string_attribute(variable_die, DW_AT_name);
15406 string linkage_name = die_linkage_name(variable_die);
15407 if (linkage_name.empty() && die_is_in_c(variable_die))
15408 linkage_name = name;
15409 string qualified_name = build_qualified_name(scope, name);
15410
15411 // If a non member variable that is a declaration (has no defined
15412 // and exported symbol) and is not the specification of another
15413 // concrete variable, then it's suppressed. This is a size
15414 // optimization; it removes useless declaration-only variables from
15415 // the IR.
15416 if (!is_class_type(scope)
15417 && !is_required_decl_spec
15418 // If we are asked to load undefined interfaces, then we don't
15419 // suppress declaration-only variables as they might have
15420 // undefined elf-symbols.
15421 && (!is_declaration_only || !rdr.load_undefined_interfaces()))
15422 {
15423 Dwarf_Addr var_addr = 0;
15424 if (!rdr.get_variable_address(variable_die, var_addr))
15425 return true;
15426
15427 elf_symbol_sptr symbol =
15428 rdr.variable_symbol_is_exported(var_addr);
15429 if (!symbol)
15430 return true;
15431 if (!symbol->is_suppressed())
15432 return false;
15433
15434 // Since there is only one symbol in DWARF associated with an elf_symbol,
15435 // we can assume this is the main symbol then. Otherwise the main hinting
15436 // did not work as expected.
15437 ABG_ASSERT(symbol->is_main_symbol());
15438 if (symbol->has_aliases())
15439 for (elf_symbol_sptr a = symbol->get_next_alias();
15440 !a->is_main_symbol(); a = a->get_next_alias())
15441 if (!a->is_suppressed())
15442 return false;
15443 }
15444
15446 qualified_name,
15447 linkage_name,
15448 /*require_drop_property=*/true);
15449}
15450
15451/// Test if a type (designated by a given DIE) in a given scope is
15452/// suppressed by the suppression specifications that are associated
15453/// to a given DWARF reader.
15454///
15455/// @param rdr the DWARF reader to consider.
15456///
15457/// @param scope of the scope of the type DIE to consider.
15458///
15459/// @param type_die the DIE that designates the type to consider.
15460///
15461/// @param type_is_opaque out parameter. If this function returns
15462/// true (the type @p type_die is suppressed) and if the type was
15463/// suppressed because it's opaque then this parameter is set to
15464/// true.
15465///
15466/// @return true iff the type designated by the DIE @p type_die, in
15467/// the scope @p scope is suppressed by at the suppression
15468/// specifications associated to the current DWARF reader.
15469static bool
15470type_is_suppressed(const reader& rdr,
15471 const scope_decl* scope,
15472 Dwarf_Die *type_die,
15473 bool &type_is_opaque)
15474{
15475 if (type_die == 0
15476 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
15477 && dwarf_tag(type_die) != DW_TAG_class_type
15478 && dwarf_tag(type_die) != DW_TAG_structure_type
15479 && dwarf_tag(type_die) != DW_TAG_union_type))
15480 return false;
15481
15482 string type_name, linkage_name;
15483 location type_location;
15484 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15485 string qualified_name = build_qualified_name(scope, type_name);
15486
15487 return suppr::is_type_suppressed(rdr,
15488 qualified_name,
15489 type_location,
15490 type_is_opaque,
15491 /*require_drop_property=*/true);
15492}
15493
15494/// Test if a type (designated by a given DIE) in a given scope is
15495/// suppressed by the suppression specifications that are associated
15496/// to a given DWARF reader.
15497///
15498/// @param rdr the DWARF reader to consider.
15499///
15500/// @param scope of the scope of the type DIE to consider.
15501///
15502/// @param type_die the DIE that designates the type to consider.
15503///
15504/// @return true iff the type designated by the DIE @p type_die, in
15505/// the scope @p scope is suppressed by at the suppression
15506/// specifications associated to the current DWARF reader.
15507static bool
15508type_is_suppressed(const reader& rdr,
15509 const scope_decl* scope,
15510 Dwarf_Die *type_die)
15511{
15512 bool type_is_opaque = false;
15513 return type_is_suppressed(rdr, scope, type_die, type_is_opaque);
15514}
15515
15516/// Get the opaque version of a type that was suppressed because it's
15517/// a private type.
15518///
15519/// The opaque version version of the type is just a declared-only
15520/// version of the type (class, union or enum type) denoted by @p
15521/// type_die.
15522///
15523/// @param rdr the DWARF reader in use.
15524///
15525/// @param scope the scope of the type die we are looking at.
15526///
15527/// @param type_die the type DIE we are looking at.
15528///
15529/// @param where_offset the offset of the DIE where we are "logically"
15530/// positionned at, in the DIE tree. This is useful when @p die is
15531/// e.g, DW_TAG_partial_unit that can be included in several places in
15532/// the DIE tree.
15533///
15534/// @return the opaque version of the type denoted by @p type_die or
15535/// nil if no opaque version was found.
15537get_opaque_version_of_type(reader &rdr,
15538 scope_decl *scope,
15539 Dwarf_Die *type_die,
15540 size_t where_offset)
15541{
15543
15544 if (type_die == 0)
15545 return result;
15546
15547 unsigned tag = dwarf_tag(type_die);
15548 if (tag != DW_TAG_class_type
15549 && tag != DW_TAG_structure_type
15550 && tag != DW_TAG_union_type
15551 && tag != DW_TAG_enumeration_type)
15552 return result;
15553
15554 string type_name, linkage_name;
15555 location type_location;
15556 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15557
15558 string qualified_name = build_qualified_name(scope, type_name);
15559
15560 //
15561 // TODO: also handle declaration-only unions. To do that, we mostly
15562 // need to adapt add_or_update_union_type to make it schedule
15563 // declaration-only unions for resolution too.
15564 //
15565 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
15566 {
15567 string_classes_or_unions_map::const_iterator i =
15568 rdr.declaration_only_classes().find(qualified_name);
15569 if (i != rdr.declaration_only_classes().end())
15570 result = i->second.back();
15571
15572 if (!result)
15573 {
15574 // So we didn't find any pre-existing forward-declared-only
15575 // class for the class definition that we could return as an
15576 // opaque type. So let's build one.
15577 //
15578 // TODO: we need to be able to do this for unions too!
15579 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
15580 /*alignment=*/0, /*size=*/0,
15581 tag == DW_TAG_structure_type,
15582 type_location,
15583 decl_base::VISIBILITY_DEFAULT));
15584 klass->set_is_declaration_only(true);
15585 klass->set_is_artificial(die_is_artificial(type_die));
15586 add_decl_to_scope(klass, scope);
15587 rdr.associate_die_to_type(type_die, klass, where_offset);
15588 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
15589 result = klass;
15590 }
15591 }
15592
15593 if (tag == DW_TAG_enumeration_type)
15594 {
15595 string_enums_map::const_iterator i =
15596 rdr.declaration_only_enums().find(qualified_name);
15597 if (i != rdr.declaration_only_enums().end())
15598 result = i->second.back();
15599
15600 if (!result)
15601 {
15602 uint64_t size = 0;
15603 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
15604 size *= 8;
15605 type_decl_sptr underlying_type =
15606 build_enum_underlying_type(rdr, type_name, size,
15607 /*anonymous=*/true);
15608 enum_type_decl::enumerators enumeratorz;
15609 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
15610 type_location,
15611 underlying_type,
15612 enumeratorz,
15613 linkage_name));
15614 enum_type->set_is_artificial(die_is_artificial(type_die));
15615 add_decl_to_scope(enum_type, scope);
15616 result = enum_type;
15617 }
15618 }
15619
15620 return result;
15621}
15622
15623/// Create a function symbol with a given name.
15624///
15625/// @param sym_name the name of the symbol to create.
15626///
15627/// @param env the environment to create the symbol in.
15628///
15629/// @return the newly created symbol.
15631create_default_fn_sym(const string& sym_name, const environment& env)
15632{
15634 elf_symbol_sptr result =
15636 /*symbol index=*/ 0,
15637 /*symbol size=*/ 0,
15638 sym_name,
15639 /*symbol type=*/ elf_symbol::FUNC_TYPE,
15640 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
15641 /*symbol is defined=*/ true,
15642 /*symbol is common=*/ false,
15643 /*symbol version=*/ ver,
15644 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
15645 return result;
15646}
15647
15648/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
15649///
15650/// @param rdr the DWARF reader to use
15651///
15652/// @param die the DW_TAG_subprogram DIE to read from.
15653///
15654/// @param where_offset the offset of the DIE where we are "logically"
15655/// positionned at, in the DIE tree. This is useful when @p die is
15656/// e.g, DW_TAG_partial_unit that can be included in several places in
15657/// the DIE tree.
15658///
15659/// @param called_for_public_decl this is set to true if the function
15660/// was called for a public (function) decl.
15661static function_decl_sptr
15662build_function_decl(reader& rdr,
15663 Dwarf_Die* die,
15664 size_t where_offset,
15666{
15667 function_decl_sptr result = fn;
15668 if (!die)
15669 return result;
15670 int tag = dwarf_tag(die);
15671 ABG_ASSERT(tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine);
15672
15673 if (!die_is_public_decl(die))
15674 return result;
15675
15676 translation_unit_sptr tu = rdr.cur_transl_unit();
15677 ABG_ASSERT(tu);
15678
15679 string fname, flinkage_name;
15680 location floc;
15681 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
15682
15683 size_t is_inline = die_is_declared_inline(die);
15684 class_or_union_sptr is_method =
15685 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
15686
15687 if (result)
15688 {
15689 // Add the properties that might have been missing from the
15690 // first declaration of the function. For now, it usually is
15691 // the mangled name that goes missing in the first declarations.
15692 //
15693 // Also note that if 'fn' has just been cloned, the current
15694 // linkage name (of the current DIE) might be different from the
15695 // linkage name of 'fn'. In that case, update the linkage name
15696 // of 'fn' too.
15697 if (!flinkage_name.empty()
15698 && result->get_linkage_name() != flinkage_name)
15699 result->set_linkage_name(flinkage_name);
15700 if (floc)
15701 if (!result->get_location())
15702 result->set_location(floc);
15703 result->is_declared_inline(is_inline);
15704 }
15705 else
15706 {
15707 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
15708 where_offset));
15709 if (!fn_type)
15710 return result;
15711
15712 maybe_canonicalize_type(fn_type, rdr);
15713
15714 result.reset(is_method
15715 ? new method_decl(fname, fn_type,
15716 is_inline, floc,
15717 flinkage_name)
15718 : new function_decl(fname, fn_type,
15719 is_inline, floc,
15720 flinkage_name));
15721 }
15722
15723 // Set the symbol of the function. If the linkage name is not set
15724 // or is wrong, set it to the name of the underlying symbol.
15725 if (!result->get_symbol())
15726 {
15727 elf_symbol_sptr fn_sym;
15728 Dwarf_Addr fn_addr;
15729 if (rdr.get_function_address(die, fn_addr))
15730 {
15731 rdr.symtab()->
15732 update_main_symbol(fn_addr,
15733 result->get_linkage_name().empty()
15734 ? result->get_name()
15735 : result->get_linkage_name());
15736 fn_sym = rdr.function_symbol_is_exported(fn_addr);
15737 }
15738
15739 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
15740 {
15741 result->set_symbol(fn_sym);
15742 string linkage_name = result->get_linkage_name();
15743 if (linkage_name.empty())
15744 result->set_linkage_name(fn_sym->get_name());
15745 result->set_is_in_public_symbol_table(true);
15746 }
15747
15748 if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
15749 {
15750 // We are looking at a function which symbol is undefined.
15751 // let's set its symbol.
15752 string n = result->get_linkage_name();
15753 if (n.empty())
15754 n = result->get_name();
15755 fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
15756 if (fn_sym)
15757 {
15758 result->set_symbol(fn_sym);
15759 result->set_is_in_public_symbol_table(false);
15760 }
15761 }
15762 }
15763
15764 rdr.associate_die_to_type(die, result->get_type(), where_offset);
15765
15766 size_t die_offset = dwarf_dieoffset(die);
15767
15768 if (fn
15769 && is_member_function(fn)
15771 && !result->get_linkage_name().empty())
15772 // This function is a virtual member function which has its
15773 // linkage name *and* and has its underlying symbol correctly set.
15774 // It thus doesn't need any fixup related to elf symbol. So
15775 // remove it from the set of virtual member functions with linkage
15776 // names and no elf symbol that need to be fixed up.
15777 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
15778 return result;
15779}
15780
15781/// Canonicalize a type if it's suitable for early canonicalizing, or,
15782/// if it's not, schedule it for late canonicalization, after the
15783/// debug info of the current translation unit has been fully read.
15784///
15785/// A (composite) type is deemed suitable for early canonicalizing iff
15786/// all of its sub-types are canonicalized themselve. Non composite
15787/// types are always deemed suitable for early canonicalization.
15788///
15789/// Note that this function knows how to deal with anonymous classes,
15790/// structs and enums, unlike the overload below:
15791///
15792/// @param t the type DIE to consider for canonicalization.
15793///
15794/// @param rdr the @ref reader to use.
15795static void
15796maybe_canonicalize_type(const type_base_sptr& t,
15797 reader& rdr)
15798{
15799 if (!t)
15800 return;
15801
15802 type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15803 if (is_class_type(peeled_type)
15804 || is_union_type(peeled_type)
15805 || is_function_type(peeled_type)
15806 || is_array_type(peeled_type)
15807 || is_qualified_type(peeled_type)
15808 || is_enum_type(peeled_type)
15809 ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15810 // We delay canonicalization of classes/unions or typedef,
15811 // pointers, references and array to classes/unions. This is
15812 // because the (underlying) class might not be finished yet and we
15813 // might not be able to able detect it here (thinking about
15814 // classes that are work-in-progress, or classes that might be
15815 // later amended by some DWARF construct). So we err on the safe
15816 // side. We also delay canonicalization for array and qualified
15817 // types because they can be edited (in particular by
15818 // maybe_strip_qualification) after they are initially built.
15819 rdr.schedule_type_for_late_canonicalization(t);
15821 rdr.schedule_type_for_late_canonicalization(t);
15822 else
15823 canonicalize(t);
15824}
15825
15826/// If a given decl is a member type declaration, set its access
15827/// specifier from the DIE that represents it.
15828///
15829/// @param member_type_declaration the member type declaration to
15830/// consider.
15831static void
15832maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15833 Dwarf_Die* die)
15834{
15835 if (is_type(member_type_declaration)
15836 && is_member_decl(member_type_declaration))
15837 {
15838 class_or_union* scope =
15839 is_class_or_union_type(member_type_declaration->get_scope());
15840 ABG_ASSERT(scope);
15841
15842 access_specifier access = public_access;
15843 if (class_decl* cl = is_class_type(scope))
15844 if (!cl->is_struct())
15845 access = private_access;
15846
15847 die_access_specifier(die, access);
15848 set_member_access_specifier(member_type_declaration, access);
15849 }
15850}
15851
15852/// This function tests if a given function which might be intented to
15853/// be added to a class scope (to become a member function) should be
15854/// dropped on the floor instead and not be added to the class.
15855///
15856/// This is a subroutine of build_ir_node_from_die.
15857///
15858/// @param fn the function to consider.
15859///
15860/// @param fn_die the DWARF die of @p fn.
15861///
15862/// @param scope the scope in which @p fn is to be added.
15863///
15864/// @return true iff @p fn should be dropped on the floor.
15865static bool
15866potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15867 const Dwarf_Die *fn_die)
15868{
15869 if (!fn || fn->get_scope())
15870 return false;
15871
15872 if (// A function that is not virtual ...
15873 !die_is_virtual(fn_die)
15874 // .. and yet has no defined ELF symbol associated ...
15875 && !fn->get_symbol())
15876 // Should not be added to its class scope.
15877 //
15878 // Why would it? It's not part of the ABI anyway, as it doesn't
15879 // have any ELF symbol associated and is not a virtual member
15880 // function. It just constitutes bloat in the IR and might even
15881 // induce spurious change reports down the road.
15882 return true;
15883
15884 return false;
15885}
15886
15887/// Build an IR node from a given DIE and add the node to the current
15888/// IR being build and held in the DWARF reader. Doing that is called
15889/// "emitting an IR node for the DIE".
15890///
15891/// @param rdr the DWARF reader.
15892///
15893/// @param die the DIE to consider.
15894///
15895/// @param scope the scope under which the resulting IR node has to be
15896/// added.
15897///
15898/// @param called_from_public_decl set to yes if this function is
15899/// called from the functions used to build a public decl (functions
15900/// and variables). In that case, this function accepts building IR
15901/// nodes representing types. Otherwise, this function only creates
15902/// IR nodes representing public decls (functions and variables).
15903/// This is done to avoid emitting IR nodes for types that are not
15904/// referenced by public functions or variables.
15905///
15906/// @param where_offset the offset of the DIE where we are "logically"
15907/// positionned at, in the DIE tree. This is useful when @p die is
15908/// e.g, DW_TAG_partial_unit that can be included in several places in
15909/// the DIE tree.
15910///
15911/// @param is_required_decl_spec if true, it means the ir node to
15912/// build is for a decl that is a specification for another decl that
15913/// is concrete. If you don't know what this is, set it to false.
15914///
15915/// @param is_declaration_only is true if the DIE denoted by @p die is
15916/// a declaration-only DIE.
15917///
15918/// @return the resulting IR node.
15920build_ir_node_from_die(reader& rdr,
15921 Dwarf_Die* die,
15922 scope_decl* scope,
15923 bool called_from_public_decl,
15924 size_t where_offset,
15925 bool is_declaration_only,
15926 bool is_required_decl_spec)
15927{
15929
15930 if (!die || !scope)
15931 return result;
15932
15933 int tag = dwarf_tag(die);
15934
15935 if (!called_from_public_decl)
15936 {
15937 if (rdr.load_all_types() && die_is_type(die))
15938 /* We were instructed to load debug info for all types,
15939 included those that are not reachable from a public
15940 declaration. So load the debug info for this type. */;
15941 else if (tag != DW_TAG_subprogram
15942 && tag != DW_TAG_variable
15943 && tag != DW_TAG_member
15944 && tag != DW_TAG_namespace)
15945 return result;
15946 }
15947
15948 const die_source source_of_die = rdr.get_die_source(die);
15949
15950 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15951 source_of_die)))
15952 {
15953 if (rdr.load_all_types())
15954 if (called_from_public_decl)
15955 if (type_base_sptr t = is_type(result))
15956 if (corpus *abi_corpus = scope->get_corpus())
15957 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15958
15959 return result;
15960 }
15961
15962 // This is *the* bit of code that ensures we have the right notion
15963 // of "declared" at any point in a DIE chain formed from
15964 // DW_AT_abstract_origin and DW_AT_specification links. There should
15965 // be no other callers of die_is_declaration_only.
15966 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15967
15968 switch (tag)
15969 {
15970 // Type DIEs we support.
15971 case DW_TAG_base_type:
15972 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15973 {
15974 result =
15975 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15976 canonicalize(t);
15977 }
15978 break;
15979
15980 case DW_TAG_typedef:
15981 {
15982 typedef_decl_sptr t = build_typedef_type(rdr, die,
15983 called_from_public_decl,
15984 where_offset);
15985
15986 result = add_decl_to_scope(t, scope);
15987 if (result)
15988 {
15989 maybe_set_member_type_access_specifier(is_decl(result), die);
15990 maybe_canonicalize_type(t, rdr);
15991 }
15992 }
15993 break;
15994
15995 case DW_TAG_pointer_type:
15996 {
15998 build_pointer_type_def(rdr, die,
15999 called_from_public_decl,
16000 where_offset);
16001 if (p)
16002 {
16003 result =
16004 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
16005 ABG_ASSERT(result->get_translation_unit());
16006 maybe_canonicalize_type(p, rdr);
16007 }
16008 }
16009 break;
16010
16011 case DW_TAG_reference_type:
16012 case DW_TAG_rvalue_reference_type:
16013 {
16015 build_reference_type(rdr, die,
16016 called_from_public_decl,
16017 where_offset);
16018 if (r)
16019 {
16020 result =
16021 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
16022
16023 rdr.associate_die_to_type(die, r, where_offset);
16024 maybe_canonicalize_type(r, rdr);
16025 }
16026 }
16027 break;
16028
16029 case DW_TAG_ptr_to_member_type:
16030 {
16032 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
16033 where_offset);
16034 if (p)
16035 {
16036 result =
16037 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
16038 maybe_canonicalize_type(p, rdr);
16039 }
16040 }
16041 break;
16042
16043 case DW_TAG_const_type:
16044 case DW_TAG_volatile_type:
16045 case DW_TAG_restrict_type:
16046 {
16047 type_base_sptr q =
16048 build_qualified_type(rdr, die,
16049 called_from_public_decl,
16050 where_offset);
16051 if (q)
16052 {
16053 // Strip some potentially redundant type qualifiers from
16054 // the qualified type we just built.
16055 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
16056 rdr);
16057 if (!d)
16058 d = get_type_declaration(q);
16059 ABG_ASSERT(d);
16060 type_base_sptr ty = is_type(d);
16061 // Associate the die to type ty again because 'ty'might be
16062 // different from 'q', because 'ty' is 'q' possibly
16063 // stripped from some redundant type qualifier.
16064 rdr.associate_die_to_type(die, ty, where_offset);
16065 result =
16066 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
16067 maybe_canonicalize_type(is_type(result), rdr);
16068 }
16069 }
16070 break;
16071
16072 case DW_TAG_enumeration_type:
16073 {
16074 bool type_is_opaque = false;
16075 bool type_suppressed =
16076 type_is_suppressed(rdr, scope, die, type_is_opaque);
16077 if (type_suppressed && type_is_opaque)
16078 {
16079 // The type is suppressed because it's private. If other
16080 // non-suppressed and declaration-only instances of this
16081 // type exist in the current corpus, then it means those
16082 // non-suppressed instances are opaque versions of the
16083 // suppressed private type. Lets return one of these opaque
16084 // types then.
16085 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
16086 maybe_canonicalize_type(is_type(result), rdr);
16087 }
16088 else if (!type_suppressed)
16089 {
16090 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
16091 where_offset,
16092 is_declaration_only);
16093 result = add_decl_to_scope(e, scope);
16094 if (result)
16095 {
16096 maybe_set_member_type_access_specifier(is_decl(result), die);
16097 maybe_canonicalize_type(is_type(result), rdr);
16098 }
16099 }
16100 }
16101 break;
16102
16103 case DW_TAG_class_type:
16104 case DW_TAG_structure_type:
16105 {
16106 bool type_is_opaque = false;
16107 bool type_suppressed=
16108 type_is_suppressed(rdr, scope, die, type_is_opaque);
16109
16110 if (type_suppressed && type_is_opaque)
16111 {
16112 // The type is suppressed because it's private. If other
16113 // non-suppressed and declaration-only instances of this
16114 // type exist in the current corpus, then it means those
16115 // non-suppressed instances are opaque versions of the
16116 // suppressed private type. Lets return one of these opaque
16117 // types then.
16118 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
16119 maybe_canonicalize_type(is_type(result), rdr);
16120 }
16121 else if (!type_suppressed)
16122 {
16123 Dwarf_Die spec_die;
16124 scope_decl_sptr scop;
16125 class_decl_sptr klass;
16126 if (die_die_attribute(die, DW_AT_specification, spec_die))
16127 {
16128 scope_decl_sptr skope =
16129 get_scope_for_die(rdr, &spec_die,
16130 called_from_public_decl,
16131 where_offset);
16132 ABG_ASSERT(skope);
16133 decl_base_sptr cl =
16134 is_decl(build_ir_node_from_die(rdr, &spec_die,
16135 skope.get(),
16136 called_from_public_decl,
16137 where_offset,
16138 is_declaration_only,
16139 /*is_required_decl_spec=*/false));
16140 ABG_ASSERT(cl);
16141 klass = dynamic_pointer_cast<class_decl>(cl);
16142 ABG_ASSERT(klass);
16143
16144 klass =
16145 add_or_update_class_type(rdr, die,
16146 skope.get(),
16147 tag == DW_TAG_structure_type,
16148 klass,
16149 called_from_public_decl,
16150 where_offset,
16151 is_declaration_only);
16152 }
16153 else
16154 klass =
16155 add_or_update_class_type(rdr, die, scope,
16156 tag == DW_TAG_structure_type,
16158 called_from_public_decl,
16159 where_offset,
16160 is_declaration_only);
16161 result = klass;
16162 if (klass)
16163 {
16164 maybe_set_member_type_access_specifier(klass, die);
16165 maybe_canonicalize_type(klass, rdr);
16166 }
16167 }
16168 }
16169 break;
16170 case DW_TAG_union_type:
16171 if (!type_is_suppressed(rdr, scope, die))
16172 {
16173 union_decl_sptr union_type =
16174 add_or_update_union_type(rdr, die, scope,
16175 union_decl_sptr(),
16176 called_from_public_decl,
16177 where_offset,
16178 is_declaration_only);
16179 if (union_type)
16180 {
16181 maybe_set_member_type_access_specifier(union_type, die);
16182 maybe_canonicalize_type(union_type, rdr);
16183 }
16184 result = union_type;
16185 }
16186 break;
16187 case DW_TAG_string_type:
16188 break;
16189 case DW_TAG_subroutine_type:
16190 {
16191 function_type_sptr f = build_function_type(rdr, die,
16193 where_offset);
16194 if (f)
16195 {
16196 result = f;
16197 result->set_is_artificial(false);
16198 maybe_canonicalize_type(f, rdr);
16199 }
16200 }
16201 break;
16202 case DW_TAG_array_type:
16203 {
16204 array_type_def_sptr a = build_array_type(rdr,
16205 die,
16206 called_from_public_decl,
16207 where_offset);
16208 if (a)
16209 {
16210 result =
16211 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
16212 rdr.associate_die_to_type(die, a, where_offset);
16213 maybe_canonicalize_type(a, rdr);
16214 }
16215 break;
16216 }
16217 case DW_TAG_subrange_type:
16218 {
16219 // If we got here, this means the subrange type is a "free
16220 // form" defined in the global namespace of the current
16221 // translation unit, like what is found in Ada.
16223 build_subrange_type(rdr, die, where_offset);
16224 if (s)
16225 {
16226 result =
16227 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
16228 rdr.associate_die_to_type(die, s, where_offset);
16229 maybe_canonicalize_type(s, rdr);
16230 }
16231 }
16232 break;
16233 case DW_TAG_packed_type:
16234 break;
16235 case DW_TAG_set_type:
16236 break;
16237 case DW_TAG_file_type:
16238 break;
16239 case DW_TAG_thrown_type:
16240 break;
16241 case DW_TAG_interface_type:
16242 break;
16243 case DW_TAG_unspecified_type:
16244 break;
16245 case DW_TAG_shared_type:
16246 break;
16247
16248 case DW_TAG_compile_unit:
16249 // We shouldn't reach this point b/c this should be handled by
16250 // build_translation_unit.
16252
16253 case DW_TAG_namespace:
16254 case DW_TAG_module:
16255 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
16256 break;
16257
16258 case DW_TAG_variable:
16259 case DW_TAG_member:
16260 {
16261 Dwarf_Die spec_die;
16262 bool var_is_cloned = false;
16263
16264 if (tag == DW_TAG_member)
16265 ABG_ASSERT(!die_is_in_c(die));
16266
16267 if (die_die_attribute(die, DW_AT_specification, spec_die, false)
16268 || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
16269 spec_die, false)))
16270 {
16271 scope_decl_sptr spec_scope =
16272 get_scope_for_die(rdr, &spec_die,
16273 /*called_from_public_decl=*/
16274 die_is_effectively_public_decl(rdr, die),
16275 where_offset);
16276 if (spec_scope)
16277 {
16278 decl_base_sptr d =
16279 is_decl(build_ir_node_from_die(rdr, &spec_die,
16280 spec_scope.get(),
16281 called_from_public_decl,
16282 where_offset,
16283 is_declaration_only,
16284 /*is_required_decl_spec=*/true));
16285 if (d)
16286 {
16287 var_decl_sptr m =
16288 dynamic_pointer_cast<var_decl>(d);
16289 if (var_is_cloned)
16290 m = m->clone();
16291 m = build_var_decl(rdr, die, where_offset, m);
16292 if (is_data_member(m))
16293 {
16294 set_member_is_static(m, true);
16295 rdr.associate_die_to_decl(die, m, where_offset,
16296 /*associate_by_repr=*/false);
16297 }
16298 else
16299 {
16301 rdr.var_decls_to_re_add_to_tree().push_back(m);
16302 }
16303 ABG_ASSERT(m->get_scope());
16304 rdr.add_var_to_exported_or_undefined_decls(m.get());
16305 result = m;
16306 }
16307 }
16308 }
16309 else if (var_decl_sptr v =
16310 build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
16311 where_offset,
16312 is_declaration_only,
16313 /*result=*/var_decl_sptr(),
16314 is_required_decl_spec))
16315 {
16316 result = add_decl_to_scope(v, scope);
16317 ABG_ASSERT(is_decl(result)->get_scope());
16318 v = dynamic_pointer_cast<var_decl>(result);
16319 ABG_ASSERT(v);
16320 ABG_ASSERT(v->get_scope());
16321 rdr.var_decls_to_re_add_to_tree().push_back(v);
16322 rdr.add_var_to_exported_or_undefined_decls(v.get());
16323 }
16324 }
16325 break;
16326
16327 case DW_TAG_subprogram:
16328 case DW_TAG_inlined_subroutine:
16329 {
16330 if (die_is_artificial(die))
16331 break;
16332
16333 Dwarf_Die abstract_origin_die;
16334 bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
16335 abstract_origin_die,
16336 /*recursive=*/true);
16337
16338
16339 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
16340 where_offset);
16341 scope_decl* interface_scope = scope ? scope : s.get();
16342
16343 class_decl* class_scope = is_class_type(interface_scope);
16344 string linkage_name = die_linkage_name(die);
16345 string spec_linkage_name;
16346 function_decl_sptr existing_fn;
16347
16348 if (class_scope)
16349 {
16350 // The scope of the function DIE we are looking at is a
16351 // class. So we are looking at a member function.
16352 if (!linkage_name.empty())
16353 {
16354 if ((existing_fn =
16355 class_scope->find_member_function_sptr(linkage_name)))
16356 {
16357 // A function with the same linkage name has
16358 // already been created. Let's see if we are a
16359 // clone of it or not.
16360 spec_linkage_name = existing_fn->get_linkage_name();
16361 if (has_abstract_origin
16362 && !spec_linkage_name.empty()
16363 && linkage_name != spec_linkage_name)
16364 {
16365 // The current DIE has 'existing_fn' as
16366 // abstract orign, and has a linkage name that
16367 // is different from from the linkage name of
16368 // 'existing_fn'. That means, the current DIE
16369 // represents a clone of 'existing_fn'.
16370 existing_fn = existing_fn->clone();
16371 }
16372 }
16373 }
16374 }
16375
16376 rdr.scope_stack().push(interface_scope);
16377
16378 // Either we create a branch new IR for the current function
16379 // DIE we are looking at, or we complete an existing IR node
16380 // with the new completementary information carried by this
16381 // DIE for that IR node.
16382 result =
16383 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
16384 die, where_offset,
16385 is_declaration_only,
16386 existing_fn);
16387
16388 if (result && !existing_fn)
16389 {
16390 // We built a brand new IR for the function DIE. Now
16391 // there should be enough information on that IR to know
16392 // if we should drop it on the floor or keep it ...
16393 if (potential_member_fn_should_be_dropped(is_function_decl(result), die)
16394 && !is_required_decl_spec)
16395 {
16396 // So apparently we should drop that function IR on
16397 // the floor. Let's do so.
16398 result.reset();
16399 break;
16400 }
16401 // OK so we came to the conclusion that we need to keep
16402 // the function. So let's add it to its scope.
16403 result = add_decl_to_scope(is_decl(result), interface_scope);
16404 }
16405
16407 if (fn && is_member_function(fn))
16408 {
16409 class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
16410 sptr_utils::noop_deleter());
16411 ABG_ASSERT(klass);
16412 finish_member_function_reading(die, fn, klass, rdr);
16413 }
16414
16415 if (fn)
16416 {
16417 if (!is_member_function(fn)
16419 // Virtual member functions are added to the set of
16420 // functions exported by the current ABI corpus *after*
16421 // the canonicalization of their parent type. So let's
16422 // not do it here.
16423 rdr.add_fn_to_exported_or_undefined_decls(fn.get());
16424 rdr.associate_die_to_decl(die, fn, where_offset,
16425 /*associate_by_repr=*/false);
16426 maybe_canonicalize_type(fn->get_type(), rdr);
16427 }
16428
16429 rdr.scope_stack().pop();
16430 }
16431 break;
16432
16433 case DW_TAG_formal_parameter:
16434 // We should not read this case as it should have been dealt
16435 // with by build_function_decl above.
16437
16438 case DW_TAG_constant:
16439 break;
16440 case DW_TAG_enumerator:
16441 break;
16442
16443 case DW_TAG_partial_unit:
16444 case DW_TAG_imported_unit:
16445 // For now, the DIEs under these are read lazily when they are
16446 // referenced by a public decl DIE that is under a
16447 // DW_TAG_compile_unit, so we shouldn't get here.
16449
16450 // Other declaration we don't really intend to support yet.
16451 case DW_TAG_dwarf_procedure:
16452 case DW_TAG_imported_declaration:
16453 case DW_TAG_entry_point:
16454 case DW_TAG_label:
16455 case DW_TAG_lexical_block:
16456 case DW_TAG_unspecified_parameters:
16457 case DW_TAG_variant:
16458 case DW_TAG_common_block:
16459 case DW_TAG_common_inclusion:
16460 case DW_TAG_inheritance:
16461 case DW_TAG_with_stmt:
16462 case DW_TAG_access_declaration:
16463 case DW_TAG_catch_block:
16464 case DW_TAG_friend:
16465 case DW_TAG_namelist:
16466 case DW_TAG_namelist_item:
16467 case DW_TAG_template_type_parameter:
16468 case DW_TAG_template_value_parameter:
16469 case DW_TAG_try_block:
16470 case DW_TAG_variant_part:
16471 case DW_TAG_imported_module:
16472 case DW_TAG_condition:
16473 case DW_TAG_type_unit:
16474 case DW_TAG_template_alias:
16475 case DW_TAG_lo_user:
16476 case DW_TAG_MIPS_loop:
16477 case DW_TAG_format_label:
16478 case DW_TAG_function_template:
16479 case DW_TAG_class_template:
16480 case DW_TAG_GNU_BINCL:
16481 case DW_TAG_GNU_EINCL:
16482 case DW_TAG_GNU_template_template_param:
16483 case DW_TAG_GNU_template_parameter_pack:
16484 case DW_TAG_GNU_formal_parameter_pack:
16485 case DW_TAG_GNU_call_site:
16486 case DW_TAG_GNU_call_site_parameter:
16487 case DW_TAG_hi_user:
16488 default:
16489 break;
16490 }
16491
16492 if (result && tag != DW_TAG_subroutine_type)
16493 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
16494 /*associate_by_repr=*/false);
16495
16496 if (result)
16497 if (rdr.load_all_types())
16498 if (called_from_public_decl)
16499 if (type_base_sptr t = is_type(result))
16500 if (corpus *abi_corpus = scope->get_corpus())
16501 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
16502
16503 return result;
16504}
16505
16506/// Build the IR node for a void type.
16507///
16508/// @param rdr the DWARF reader to use.
16509///
16510/// @return the void type node.
16511static decl_base_sptr
16512build_ir_node_for_void_type(reader& rdr)
16513{
16514 const environment& env = rdr.env();
16515
16516 type_base_sptr t = env.get_void_type();
16517 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16518 decl_base_sptr type_declaration = get_type_declaration(t);
16519 canonicalize(t);
16520 return type_declaration;
16521}
16522
16523/// Build the IR node for a "pointer to void type".
16524///
16525/// That IR node is shared across the ABI corpus.
16526///
16527/// Note that this function just gets that IR node from the
16528/// environment and, if it's not added to any scope yet, adds it to
16529/// the global scope associated to the current translation unit.
16530///
16531/// @param rdr the DWARF reader to consider.
16532///
16533/// @return the IR node.
16535build_ir_node_for_void_pointer_type(reader& rdr)
16536{
16537 const environment& env = rdr.env();
16538
16539 type_base_sptr t = env.get_void_pointer_type();
16540 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16541 decl_base_sptr type_declaration = get_type_declaration(t);
16542 canonicalize(t);
16543 return type_declaration;
16544}
16545
16546/// Build the IR node for a variadic parameter type.
16547///
16548/// @param rdr the DWARF reader to use.
16549///
16550/// @return the variadic parameter type.
16551static decl_base_sptr
16552build_ir_node_for_variadic_parameter_type(reader &rdr)
16553{
16554
16555 const environment& env = rdr.env();
16556
16557 type_base_sptr t = env.get_variadic_parameter_type();
16558 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16559 decl_base_sptr type_declaration = get_type_declaration(t);
16560 canonicalize(t);
16561 return type_declaration;
16562}
16563
16564/// Build an IR node from a given DIE and add the node to the current
16565/// IR being build and held in the DWARF reader. Doing that is called
16566/// "emitting an IR node for the DIE".
16567///
16568/// @param rdr the DWARF reader.
16569///
16570/// @param die the DIE to consider.
16571///
16572/// @param called_from_public_decl set to yes if this function is
16573/// called from the functions used to build a public decl (functions
16574/// and variables). In that case, this function accepts building IR
16575/// nodes representing types. Otherwise, this function only creates
16576/// IR nodes representing public decls (functions and variables).
16577/// This is done to avoid emitting IR nodes for types that are not
16578/// referenced by public functions or variables.
16579///
16580/// @param where_offset the offset of the DIE where we are "logically"
16581/// positionned at, in the DIE tree. This is useful when @p die is
16582/// e.g, DW_TAG_partial_unit that can be included in several places in
16583/// the DIE tree.
16584///
16585/// @return the resulting IR node.
16587build_ir_node_from_die(reader& rdr,
16588 Dwarf_Die* die,
16589 bool called_from_public_decl,
16590 size_t where_offset)
16591{
16592 if (!die)
16593 return decl_base_sptr();
16594
16595 // Normaly, a decl that is meant to be external has a DW_AT_external
16596 // set. But then some compilers fail to always emit that flag. For
16597 // instance, for static data members, some compilers won't emit the
16598 // DW_AT_external. In that case, we assume that if the variable is
16599 // at global or named namespace scope, then we can assume it's
16600 // external. If the variable doesn't have any ELF symbol associated
16601 // to it, it'll be dropped on the floor anyway. Those variable
16602 // decls are considered as being "effectively public".
16603 bool consider_as_called_from_public_decl =
16604 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
16605 scope_decl_sptr scope = get_scope_for_die(rdr, die,
16606 consider_as_called_from_public_decl,
16607 where_offset);
16608 return build_ir_node_from_die(rdr, die, scope.get(),
16609 called_from_public_decl,
16610 where_offset, true);
16611}
16612
16613/// Create a dwarf::reader.
16614///
16615/// @param elf_path the path to the elf file the reader is to be used
16616/// for.
16617///
16618/// @param debug_info_root_paths a vector to the paths to the
16619/// directories under which the debug info is to be found for @p
16620/// elf_path. Pass an empty vector if the debug info is not in a
16621/// split file.
16622///
16623/// @param environment the environment used by the current context.
16624/// This environment contains resources needed by the DWARF reader and by
16625/// the types and declarations that are to be created later. Note
16626/// that ABI artifacts that are to be compared all need to be created
16627/// within the same environment.
16628///
16629/// Please also note that the life time of this environment object
16630/// must be greater than the life time of the resulting @ref
16631/// reader the context uses resources that are allocated in the
16632/// environment.
16633///
16634/// @param load_all_types if set to false only the types that are
16635/// reachable from publicly exported declarations (of functions and
16636/// variables) are read. If set to true then all types found in the
16637/// debug information are loaded.
16638///
16639/// @param linux_kernel_mode if set to true, then consider the special
16640/// linux kernel symbol tables when determining if a symbol is
16641/// exported or not.
16642///
16643/// @return a smart pointer to the resulting dwarf::reader.
16644elf_based_reader_sptr
16645create_reader(const std::string& elf_path,
16646 const vector<char**>& debug_info_root_paths,
16648 bool load_all_types,
16649 bool linux_kernel_mode)
16650{
16651
16652 reader_sptr r = reader::create(elf_path,
16653 debug_info_root_paths,
16655 load_all_types,
16656 linux_kernel_mode);
16657 return static_pointer_cast<elf_based_reader>(r);
16658}
16659
16660/// Re-initialize a reader so that it can re-used to read
16661/// another binary.
16662///
16663/// @param rdr the context to re-initialize.
16664///
16665/// @param elf_path the path to the elf file the context is to be used
16666/// for.
16667///
16668/// @param debug_info_root_path a pointer to the path to the root
16669/// directory under which the debug info is to be found for @p
16670/// elf_path. Leave this to NULL if the debug info is not in a split
16671/// file.
16672///
16673/// @param environment the environment used by the current context.
16674/// This environment contains resources needed by the DWARF reader and by
16675/// the types and declarations that are to be created later. Note
16676/// that ABI artifacts that are to be compared all need to be created
16677/// within the same environment.
16678///
16679/// Please also note that the life time of this environment object
16680/// must be greater than the life time of the resulting @ref
16681/// reader the context uses resources that are allocated in the
16682/// environment.
16683///
16684/// @param load_all_types if set to false only the types that are
16685/// reachable from publicly exported declarations (of functions and
16686/// variables) are read. If set to true then all types found in the
16687/// debug information are loaded.
16688///
16689/// @param linux_kernel_mode if set to true, then consider the special
16690/// linux kernel symbol tables when determining if a symbol is
16691/// exported or not.
16692///
16693/// @return a smart pointer to the resulting dwarf::reader.
16694void
16696 const std::string& elf_path,
16697 const vector<char**>&debug_info_root_path,
16698 bool read_all_types,
16699 bool linux_kernel_mode)
16700{
16701 reader& r = dynamic_cast<reader&>(rdr);
16702 r.initialize(elf_path, debug_info_root_path,
16703 read_all_types, linux_kernel_mode);
16704}
16705
16706/// Read all @ref abigail::translation_unit possible from the debug info
16707/// accessible from an elf file, stuff them into a libabigail ABI
16708/// Corpus and return it.
16709///
16710/// @param elf_path the path to the elf file.
16711///
16712/// @param debug_info_root_paths a vector of pointers to root paths
16713/// under which to look for the debug info of the elf files that are
16714/// later handled by the Dwfl. This for cases where the debug info is
16715/// split into a different file from the binary we want to inspect.
16716/// On Red Hat compatible systems, this root path is usually
16717/// /usr/lib/debug by default. If this argument is set to NULL, then
16718/// "./debug" and /usr/lib/debug will be searched for sub-directories
16719/// containing the debug info file.
16720///
16721/// @param environment the environment used by the current context.
16722/// This environment contains resources needed by the DWARF reader and by
16723/// the types and declarations that are to be created later. Note
16724/// that ABI artifacts that are to be compared all need to be created
16725/// within the same environment. Also, the lifetime of the
16726/// environment must be greater than the lifetime of the resulting
16727/// corpus because the corpus uses resources that are allocated in the
16728/// environment.
16729///
16730/// @param load_all_types if set to false only the types that are
16731/// reachable from publicly exported declarations (of functions and
16732/// variables) are read. If set to true then all types found in the
16733/// debug information are loaded.
16734///
16735/// @param resulting_corp a pointer to the resulting abigail::corpus.
16736///
16737/// @return the resulting status.
16738corpus_sptr
16739read_corpus_from_elf(const std::string& elf_path,
16740 const vector<char**>& debug_info_root_paths,
16742 bool load_all_types,
16743 fe_iface::status& status)
16744{
16745 elf_based_reader_sptr rdr =
16746 dwarf::reader::create(elf_path, debug_info_root_paths,
16747 environment, load_all_types,
16748 /*linux_kernel_mode=*/false);
16749
16750 return rdr->read_corpus(status);
16751}
16752
16753/// Look into the symbol tables of a given elf file and see if we find
16754/// a given symbol.
16755///
16756/// @param env the environment we are operating from.
16757///
16758/// @param elf_path the path to the elf file to consider.
16759///
16760/// @param symbol_name the name of the symbol to look for.
16761///
16762/// @param demangle if true, try to demangle the symbol name found in
16763/// the symbol table.
16764///
16765/// @param syms the vector of symbols found with the name @p symbol_name.
16766///
16767/// @return true iff the symbol was found among the publicly exported
16768/// symbols of the ELF file.
16769bool
16770lookup_symbol_from_elf(const environment& env,
16771 const string& elf_path,
16772 const string& symbol_name,
16773 bool demangle,
16774 vector<elf_symbol_sptr>& syms)
16775
16776{
16777 if (elf_version(EV_CURRENT) == EV_NONE)
16778 return false;
16779
16780 int fd = open(elf_path.c_str(), O_RDONLY);
16781 if (fd < 0)
16782 return false;
16783
16784 struct stat s;
16785 if (fstat(fd, &s))
16786 return false;
16787
16788 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16789 if (elf == 0)
16790 return false;
16791
16792 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
16793 demangle, syms);
16794 elf_end(elf);
16795 close(fd);
16796
16797 return value;
16798}
16799
16800/// Look into the symbol tables of an elf file to see if a public
16801/// function of a given name is found.
16802///
16803/// @param env the environment we are operating from.
16804///
16805/// @param elf_path the path to the elf file to consider.
16806///
16807/// @param symbol_name the name of the function to look for.
16808///
16809/// @param syms the vector of public function symbols found with the
16810/// name @p symname.
16811///
16812/// @return true iff a function with symbol name @p symbol_name is
16813/// found.
16814bool
16816 const string& path,
16817 const string& symname,
16818 vector<elf_symbol_sptr>& syms)
16819{
16820 if (elf_version(EV_CURRENT) == EV_NONE)
16821 return false;
16822
16823 int fd = open(path.c_str(), O_RDONLY);
16824 if (fd < 0)
16825 return false;
16826
16827 struct stat s;
16828 if (fstat(fd, &s))
16829 return false;
16830
16831 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16832 if (elf == 0)
16833 return false;
16834
16835 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16836 elf_end(elf);
16837 close(fd);
16838
16839 return value;
16840}
16841
16842}// end namespace dwarf
16843
16844}// 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...
elf_symbol_sptr function_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined function symbol.
const string & elf_architecture() const
Get the value of the 'ARCHITECTURE' property of the current ELF file.
const Dwarf * dwarf_debug_info() const
Getter of the handle used to access DWARF information from the current ELF file.
virtual ir::corpus_sptr read_corpus(status &status)
Read the ELF information associated to the current ELF file and construct an ABI representation from ...
const Dwarf * alternate_dwarf_debug_info() const
Getter of the handle use to access DWARF information from the alternate split DWARF information.
bool refers_to_alt_debug_info(string &alt_di_path) const
Check if the underlying elf file refers to an alternate debug info file associated to it.
elf_symbol_sptr variable_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined variable symbol.
elf_symbol_sptr variable_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given variable symbol has been exported.
elf_symbol_sptr function_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given function symbol has been exported.
const vector< string > & dt_needed() const
Get the value of the DT_NEEDED property of the current ELF file.
The common interface of readers based on ELF.
elf_based_reader(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &env)
Readers that implement this interface must provide a factory method to create a reader instance as th...
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
@ STATUS_DEBUG_INFO_NOT_FOUND
This status is for when the debug info could not be read.
Definition: abg-fe-iface.h:46
@ STATUS_ALT_DEBUG_INFO_NOT_FOUND
This status is for when the alternate debug info could not be found.
Definition: abg-fe-iface.h:50
@ STATUS_UNKNOWN
The status is in an unknown state.
Definition: abg-fe-iface.h:40
const options_type & options() const
Getter of the the options of the current Front End Interface.
Definition: abg-fe-iface.cc:92
corpus_sptr corpus()
Getter for the ABI corpus being built by the current front-end.
corpus_group_sptr & corpus_group()
Getter for the ABI corpus group being built by the current front-end.
const std::string & corpus_path() const
Getter of the path to the file which an ABI corpus is to be created for.
const string & dt_soname() const
Getter for the SONAME of the analyzed binary.
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:2537
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:4251
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:4777
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:1993
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:2766
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:3135
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:47
string to_string(bool internal=false) const
Return the string representation of the current instance of integral_type.
Definition: abg-ir.cc:16218
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.
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.
bool is_anonymous_type_die(Dwarf_Die *die)
Test if a given DIE represents an anonymous 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...
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...
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.
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...
elf_symbol_sptr create_default_fn_sym(const string &sym_name, const environment &env)
Create a function symbol with a given name.
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.
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,...
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:236
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:11972
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition: abg-fwd.h:222
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:11836
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition: abg-fwd.h:270
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:13553
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:13715
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6665
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:11314
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10410
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5385
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:11742
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:888
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6597
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:10660
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8394
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:10024
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:11238
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:11862
comparison_result
The result of structural comparison of type ABI artifacts.
Definition: abg-ir-priv.h:34
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition: abg-ir.cc:10692
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:245
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition: abg-ir.cc:10461
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:13740
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:7270
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:6100
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:13416
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6468
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:12303
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:10923
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:194
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:7346
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:6524
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:6852
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:1610
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:10512
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:11385
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5530
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10570
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:13583
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition: abg-fwd.h:211
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition: abg-fwd.h:168
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:12012
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:11141
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:11178
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1742
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:16136
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:10642
unordered_map< interned_string, type_base_wptrs_type, hash_interned_string > istring_type_base_wptrs_map_type
A convenience typedef for a map which key is an interned_string and which value is a vector of type_b...
Definition: abg-fwd.h:149
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8462
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:257
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:240
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition: abg-fwd.h:265
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:121
shared_ptr< translation_unit > translation_unit_sptr
Convenience typedef for a shared pointer on a translation_unit type.
Definition: abg-fwd.h:137
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:12502
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:27567
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1756
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:12049
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:15627
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:227
bool is_const_qualified_type(const qualified_type_def_sptr &t)
Test if a given qualified type is const.
Definition: abg-ir.cc:7238
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6354
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1728
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10350
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:24906
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5450
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:8370
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:28238
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5501
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:176
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6627
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:11006
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:11445
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:10972
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:13371
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:11721
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5599
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:13320
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:10043
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:26030
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:11650
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:162
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:6975
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition: abg-fwd.h:285
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1765
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:14955
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:7605
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:13678
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10298
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:11415
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:11365
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:12082
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:8651
method_decl_sptr copy_member_function(const class_or_union_sptr &t, const method_decl_sptr &method)
Copy a method of a class_or_union into a new class_or_union.
Definition: abg-ir.cc:23904
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:11475
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5403
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:6411
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.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition: abg-ir.cc:152
fe_iface::status operator&(fe_iface::status l, fe_iface::status r)
The bitwise AND operator for the fe_iface::status type.
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition: abg-ir.cc:186
fe_iface::status operator|(fe_iface::status l, fe_iface::status r)
The bitwise OR operator for the fe_iface::status type.
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:169
bool load_undefined_interfaces
If this option is set to true, then the functions and variables that have an undefined symbol are goi...
Definition: abg-fe-iface.h:67
A functor to hash instances of interned_string.