libabigail
Loading...
Searching...
No Matches
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(hash_t(p.first), hash_t(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 {
187 return *abigail::hashing::combine_hashes(hash_t(p.source_),
188 hash_t(p.offset_));
189 }
190};// end struct offset_hash
191
192/// A hasher for a pair of offset_type. This is used as a hasher for
193/// the type @ref offset_pair_set_type, for instance.
194struct offset_pair_hash
195{
196 size_t
197 operator()(const std::pair<offset_type, offset_type>& p) const
198 {
199 hash_t h1 = abigail::hashing::combine_hashes(hash_t(p.first.source_),
200 hash_t(p.first.offset_));
201 hash_t h2 = abigail::hashing::combine_hashes(hash_t(p.second.source_),
202 hash_t(p.second.offset_));
203 return *abigail::hashing::combine_hashes(h1, h2);
204 }
205};// end struct offset_pair_hash
206
207/// A convenience typedef for an unordered set of DIE offsets.
208typedef unordered_set<offset_type, offset_hash> offset_set_type;
209
210///A convenience typedef for an unordered set of pairs of offset_type.
211typedef unordered_set<std::pair<offset_type,
212 offset_type>,
213 offset_pair_hash> offset_pair_set_type;
214
215/// A convenience typedef for a vector of pairs of offset_type.
216typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
217
218/// A convenience typedef for an unordered map that associates a pair
219/// of offset_type to a vector of pairs offset_type.
220typedef unordered_map<std::pair<offset_type, offset_type>,
222 offset_pair_hash> offset_pair_vect_map_type;
223
224/// A convenience typedef for an unordered_map that associates a pair
225/// of offset_type to a set of pairs of offset_type.
226typedef unordered_map<std::pair<offset_type, offset_type>,
228 offset_pair_hash> offset_pair_set_map_type;
229
230/// A convenience typedef for a vector of pairs of offset_type.
231typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
232
233class reader;
234
236build_translation_unit_and_add_to_ir(reader& rdr,
237 Dwarf_Die* die,
238 char address_size);
239
240static void
241maybe_propagate_canonical_type(const reader& rdr,
242 const Dwarf_Die* l,
243 const Dwarf_Die* r);
244
245static void
246propagate_canonical_type(const reader& rdr,
247 const Dwarf_Die* l,
248 const Dwarf_Die* r);
249
250static void
251maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
252 Dwarf_Die* die);
253
254/// Convenience typedef for a shared pointer to an
255/// addr_elf_symbol_sptr_map_type.
256typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
257
258/// Convenience typedef for a map that associates an @ref
259/// interned_string to a @ref function_type_sptr.
260typedef unordered_map<interned_string,
263
264/// Convenience typedef for a stack containing the scopes up to the
265/// current point in the abigail Internal Representation (aka IR) tree
266/// that is being built.
267typedef stack<scope_decl*> scope_stack_type;
268
269/// Convenience typedef for a map which key is a dwarf offset. The
270/// value is also a dwarf offset.
271typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
272
273/// Convenience typedef for a map which key is a string and which
274/// value is a vector of smart pointer to a class_or_union_sptr.
275typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_map;
276
277/// Convenience typedef for a map which key is a string and which
278/// value is a vector of smart pointer to a class.
279typedef unordered_map<string, classes_type> string_classes_map;
280
281/// Convenience typedef for a map which key is a string and which
282/// value is a vector of smart pointer to a enum.
283typedef unordered_map<string, enums_type> string_enums_map;
284
285/// The abstraction of the place where a partial unit has been
286/// imported. This is what the DW_TAG_imported_unit DIE expresses.
287///
288/// This type thus contains:
289/// - the offset to which the partial unit is imported
290/// - the offset of the imported partial unit.
291/// - the offset of the imported partial unit.
292struct imported_unit_point
293{
294 Dwarf_Off offset_of_import;
295 // The boolean below is true iff the imported unit comes from the
296 // alternate debug info file.
297 die_source imported_unit_die_source;
298 Dwarf_Off imported_unit_die_off;
299 Dwarf_Off imported_unit_cu_off;
300 Dwarf_Off imported_unit_child_off;
301
302 /// Default constructor for @ref the type imported_unit_point.
303 imported_unit_point()
304 : offset_of_import(),
305 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
306 imported_unit_die_off(),
307 imported_unit_cu_off(),
308 imported_unit_child_off()
309 {}
310
311 /// Constructor of @ref the type imported_unit_point.
312 ///
313 /// @param import_off the offset of the point at which the unit has
314 /// been imported.
315 imported_unit_point(Dwarf_Off import_off)
316 : offset_of_import(import_off),
317 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
318 imported_unit_die_off(),
319 imported_unit_cu_off(),
320 imported_unit_child_off()
321 {}
322
323 /// Constructor of @ref the type imported_unit_point.
324 ///
325 /// @param import_off the offset of the point at which the unit has
326 /// been imported.
327 ///
328 /// @param from where the imported DIE comes from.
329 ///
330 /// @param imported_die the die of the unit that has been imported.
331 imported_unit_point(Dwarf_Off import_off,
332 const Dwarf_Die& imported_die,
333 die_source from)
334 : offset_of_import(import_off),
335 imported_unit_die_source(from),
336 imported_unit_die_off(dwarf_dieoffset
337 (const_cast<Dwarf_Die*>(&imported_die))),
338 imported_unit_cu_off(),
339 imported_unit_child_off()
340 {
341 Dwarf_Die imported_unit_child;
342
343 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
344 &imported_unit_child) == 0);
345
346 imported_unit_child_off =
347 dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
348
349 Dwarf_Die cu_die_memory;
350 Dwarf_Die *cu_die;
351
352 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
353 &cu_die_memory, 0, 0);
354 imported_unit_cu_off = dwarf_dieoffset(cu_die);
355 }
356}; // struct imported_unit_point
357
358/// Convenience typedef for a vector of @ref imported_unit_point.
359typedef vector<imported_unit_point> imported_unit_points_type;
360
361/// Convenience typedef for a vector of @ref imported_unit_point.
362typedef unordered_map<Dwarf_Off, imported_unit_points_type>
364
365/// "Less than" operator for instances of @ref imported_unit_point
366/// type.
367///
368/// @param the left hand side operand of the "Less than" operator.
369///
370/// @param the right hand side operand of the "Less than" operator.
371///
372/// @return true iff @p l is less than @p r.
373static bool
374operator<(const imported_unit_point& l, const imported_unit_point& r)
375{return l.offset_of_import < r.offset_of_import;}
376
377static bool
378get_parent_die(const reader& rdr,
379 const Dwarf_Die* die,
380 Dwarf_Die& parent_die,
381 size_t where_offset);
382
383static bool
384get_scope_die(const reader& rdr,
385 const Dwarf_Die* die,
386 size_t where_offset,
387 Dwarf_Die& scope_die);
388
389static bool
390get_die_language(const Dwarf_Die *die, translation_unit::language &lang) ;
391
392static bool
393die_is_in_c(const Dwarf_Die *die);
394
395static bool
396die_is_in_cplus_plus(const Dwarf_Die *die);
397
398static bool
399die_is_in_c_or_cplusplus(const Dwarf_Die *die);
400
401static bool
402die_is_anonymous(const Dwarf_Die* die);
403
404static bool
405die_is_anonymous_data_member(const Dwarf_Die* die);
406
407static bool
408die_is_type(const Dwarf_Die* die);
409
410static bool
411die_is_decl(const Dwarf_Die* die);
412
413static bool
414die_is_declaration_only(Dwarf_Die* die);
415
416static bool
417die_is_variable_decl(const Dwarf_Die *die);
418
419static bool
420die_is_function_decl(const Dwarf_Die *die);
421
422static bool
423die_has_size_attribute(const Dwarf_Die *die);
424
425static bool
426die_has_no_child(const Dwarf_Die *die);
427
428static bool
429die_is_namespace(const Dwarf_Die* die);
430
431static bool
432die_is_unspecified(Dwarf_Die* die);
433
434static bool
435die_is_void_type(Dwarf_Die* die);
436
437static bool
438die_is_pointer_type(const Dwarf_Die* die);
439
440static bool
441pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
442
443static bool
444die_is_reference_type(const Dwarf_Die* die);
445
446static bool
447die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
448
449static bool
450die_is_pointer_or_reference_type(const Dwarf_Die* die);
451
452static bool
453die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
454
455static bool
456die_is_class_type(const Dwarf_Die* die);
457
458static bool
459die_is_qualified_type(const Dwarf_Die* die);
460
461static bool
462die_is_function_type(const Dwarf_Die *die);
463
464static bool
465die_has_object_pointer(const Dwarf_Die* die,
466 Dwarf_Die& object_pointer);
467
468static bool
469die_has_children(const Dwarf_Die* die);
470
471static bool
472fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die);
473
474static bool
475member_fn_die_has_this_pointer(const reader& rdr,
476 const Dwarf_Die* die,
477 size_t where_offset,
478 Dwarf_Die& class_die,
479 Dwarf_Die& object_pointer_die);
480
481static bool
482die_this_pointer_from_object_pointer(Dwarf_Die* die,
483 Dwarf_Die& this_pointer);
484
485static bool
486die_this_pointer_is_const(Dwarf_Die* die);
487
488static bool
489die_object_pointer_is_for_const_method(Dwarf_Die* die);
490
491static bool
492is_type_die_to_be_canonicalized(const Dwarf_Die *die);
493
494static bool
495die_is_at_class_scope(const reader& rdr,
496 const Dwarf_Die* die,
497 size_t where_offset,
498 Dwarf_Die& class_scope_die);
499static bool
500eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
501 size_t expr_len,
502 int64_t& value,
503 bool& is_tls_address);
504
506dwarf_language_to_tu_language(size_t l);
507
508static bool
509die_unsigned_constant_attribute(const Dwarf_Die* die,
510 unsigned attr_name,
511 uint64_t& cst);
512
513static bool
514die_signed_constant_attribute(const Dwarf_Die*die,
515 unsigned attr_name,
516 int64_t& cst);
517
518static bool
519die_constant_attribute(const Dwarf_Die *die,
520 unsigned attr_name,
521 bool is_signed,
523
524static bool
525die_member_offset(const reader& rdr,
526 const Dwarf_Die* die,
527 int64_t& offset);
528
529static bool
530form_is_DW_FORM_strx(unsigned form);
531
532static bool
533form_is_DW_FORM_line_strp(unsigned form);
534
535static bool
536die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
537
538static string
539die_name(const Dwarf_Die* die);
540
541static void
542die_name_and_linkage_name(const Dwarf_Die* die,
543 string& name,
544 string& linkage_name);
545static location
546die_location(const reader& rdr, const Dwarf_Die* die);
547
548static bool
549die_location_address(Dwarf_Die* die,
550 Dwarf_Addr& address,
551 bool& is_tls_address);
552
553static bool
554die_die_attribute(const Dwarf_Die* die,
555 unsigned attr_name,
556 Dwarf_Die& result,
557 bool recursively = true);
558
559static bool
560die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
561
562static bool
563subrange_die_indirect_bound_value(const Dwarf_Die *die,
564 unsigned attr_name,
566 bool& is_signed);
567
568static bool
569subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
570 unsigned attr_name,
571 Dwarf_Die& referenced_subrange);
572static string
573get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
574
575static string
576build_internal_anonymous_die_name(const string &base_name,
577 size_t anonymous_type_index);
578
579static string
580die_qualified_type_name(const reader& rdr,
581 const Dwarf_Die* die,
582 size_t where,
583 unordered_set<uint64_t>& guard);
584
585static string
586die_qualified_decl_name(const reader& rdr,
587 const Dwarf_Die* die,
588 size_t where,
589 unordered_set<uint64_t>& guard);
590
591static string
592die_qualified_name(const reader& rdr,
593 const Dwarf_Die* die,
594 size_t where,
595 unordered_set<uint64_t>& guard);
596
597static string
598die_qualified_name(const reader& rdr,
599 const Dwarf_Die* die,
600 size_t where);
601
602static string
603die_type_name(const reader& rdr, const Dwarf_Die* die,
604 bool qualified_name, size_t where_offset,
605 unordered_set<uint64_t>& infinite_loop_guard);
606
607static string
608die_type_name(const reader& rdr, const Dwarf_Die* die,
609 bool qualified_name, size_t where_offset);
610
611static bool
612die_qualified_type_name_empty(const reader& rdr,
613 const Dwarf_Die* die, size_t where,
614 string &qualified_name,
615 unordered_set<uint64_t>& infinite_loop_guard);
616
617static void
618die_return_and_parm_names_from_fn_type_die(const reader& rdr,
619 const Dwarf_Die* die,
620 size_t where_offset,
621 bool pretty_print,
622 bool qualified_name,
623 bool &is_method_type,
624 string &return_type_name,
625 string &class_name,
626 vector<string>& parm_names,
627 bool& is_const,
628 bool& is_static,
629 unordered_set<uint64_t>& infinite_loop_guard);
630
631static string
632die_function_signature(const reader& rdr,
633 const Dwarf_Die *die,
634 bool qualified_name,
635 size_t where_offset,
636 unordered_set<uint64_t>& infinite_loop_guard);
637
638static bool
639die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
640
641static bool
642die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
643
644static bool
645die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die);
646
647static bool
648die_function_type_is_method_type(const reader& rdr,
649 const Dwarf_Die *die,
650 size_t where_offset,
651 Dwarf_Die& object_pointer_die,
652 Dwarf_Die& class_die,
653 bool& is_static);
654
655static string
656die_enum_flat_representation(const reader& rdr,
657 const Dwarf_Die* die,
658 const string& indent,
659 bool one_line,
660 bool qualified_names,
661 size_t where_offset);
662
663static string
664die_class_flat_representation(const reader& rdr,
665 const Dwarf_Die* die,
666 const string& indent,
667 bool one_line,
668 bool qualified_names,
669 size_t where_offset,
670 unordered_set<uint64_t>& infinite_loop_guard);
671
672static string
673die_class_or_enum_flat_representation(const reader& rdr,
674 const Dwarf_Die* die,
675 const string& indent,
676 bool one_line,
677 bool qualified_names,
678 size_t where_offset,
679 unordered_set<uint64_t>& infinite_loop_guard);
680
681static string
682die_class_or_enum_flat_representation(const reader& rdr,
683 const Dwarf_Die* die,
684 const string& indent,
685 bool one_line,
686 bool qualified_names,
687 size_t where_offset);
688
689static string
690die_pretty_print_type(const reader& rdr,
691 const Dwarf_Die* die,
692 size_t where_offset,
693 unordered_set<uint64_t>& guard);
694
695static string
696die_pretty_print_decl(const reader& rdr,
697 const Dwarf_Die* die,
698 bool qualified_name,
699 bool include_fns,
700 size_t where_offset,
701 unordered_set<uint64_t>& infinite_loop_guard);
702
703static string
704die_pretty_print(reader& rdr,
705 const Dwarf_Die* die,
706 size_t where_offset,
707 unordered_set<uint64_t>& infinite_loop_guard);
708
709static void
710maybe_canonicalize_type(const type_base_sptr& t,
711 reader& rdr);
712
713static uint64_t
714get_default_array_lower_bound(translation_unit::language l);
715
716static bool
717find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
718 Dwarf_Off,
719 imported_unit_points_type::const_iterator&);
720
722build_subrange_type(reader& rdr,
723 const Dwarf_Die* die,
724 size_t where_offset,
725 bool associate_type_to_die = true);
726
727static void
728build_subranges_from_array_type_die(const reader& rdr,
729 const Dwarf_Die* die,
731 size_t where_offset,
732 bool associate_type_to_die = true);
733
735compare_dies(const reader& rdr,
736 const Dwarf_Die *l, const Dwarf_Die *r,
737 bool update_canonical_dies_on_the_fly);
738
739static bool
740compare_dies_during_canonicalization(reader& rdr,
741 const Dwarf_Die *l, const Dwarf_Die *r,
742 bool update_canonical_dies_on_the_fly);
743
744static bool
745get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
746
747static bool
748get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member);
749
750/// Get the language used to generate a given DIE.
751///
752/// @param die the DIE to consider.
753///
754/// @param lang the resulting language.
755///
756/// @return true iff the language of the DIE was found.
757static bool
758get_die_language(const Dwarf_Die *die, translation_unit::language &lang)
759{
760 Dwarf_Die cu_die;
761 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
762
763 uint64_t l = 0;
764 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
765 return false;
766
767 lang = dwarf_language_to_tu_language(l);
768 return true;
769}
770
771/// Test if a given DIE originates from a program written in the C
772/// language.
773///
774/// @param die the DIE to consider.
775///
776/// @return true iff @p die originates from a program in the C
777/// language.
778static bool
779die_is_in_c(const Dwarf_Die *die)
780{
781 translation_unit::language l = translation_unit::LANG_UNKNOWN;
782 if (!get_die_language(die, l))
783 return false;
784 return is_c_language(l);
785}
786
787/// Test if a given DIE originates from a program written in the C++
788/// language.
789///
790/// @param die the DIE to consider.
791///
792/// @return true iff @p die originates from a program in the C++
793/// language.
794static bool
795die_is_in_cplus_plus(const Dwarf_Die *die)
796{
797 translation_unit::language l = translation_unit::LANG_UNKNOWN;
798 if (!get_die_language(die, l))
799 return false;
800 return is_cplus_plus_language(l);
801}
802
803/// Test if a given DIE originates from a program written either in
804/// C or C++.
805///
806/// @param die the DIE to consider.
807///
808/// @return true iff @p die originates from a program written either in
809/// C or C++.
810static bool
811die_is_in_c_or_cplusplus(const Dwarf_Die *die)
812{
813 translation_unit::language l = translation_unit::LANG_UNKNOWN;
814 if (!get_die_language(die, l))
815 return false;
816 return (is_cplus_plus_language(l) || is_c_language(l));
817}
818
819/// Compare a symbol name against another name, possibly demangling
820/// the symbol_name before performing the comparison.
821///
822/// @param symbol_name the symbol_name to take in account.
823///
824/// @param name the second name to take in account.
825///
826/// @param demangle if true, demangle @p symbol_name and compare the
827/// result of the demangling with @p name.
828///
829/// @return true iff symbol_name equals name.
830static bool
831compare_symbol_name(const string& symbol_name,
832 const string& name,
833 bool demangle)
834{
835 if (demangle)
836 {
837 string m = demangle_cplus_mangled_name(symbol_name);
838 return m == name;
839 }
840 return symbol_name == name;
841}
842
843/// Lookup a symbol using the SysV ELF hash table.
844///
845/// Note that this function hasn't been tested. So it hasn't been
846/// debugged yet. IOW, it is not known to work. Or rather, it's
847/// almost like it's surely doesn't work ;-)
848///
849/// Use it at your own risks. :-)
850///
851///@parm env the environment we are operating from.
852///
853/// @param elf_handle the elf_handle to use.
854///
855/// @param sym_name the symbol name to look for.
856///
857/// @param ht_index the index (in the section headers table) of the
858/// hash table section to use.
859///
860/// @param sym_tab_index the index (in the section headers table) of
861/// the symbol table to use.
862///
863/// @param demangle if true, demangle @p sym_name before comparing it
864/// to names from the symbol table.
865///
866/// @param syms_found a vector of symbols found with the name @p
867/// sym_name. table.
868static bool
869lookup_symbol_from_sysv_hash_tab(const environment& env,
870 Elf* elf_handle,
871 const string& sym_name,
872 size_t ht_index,
873 size_t sym_tab_index,
874 bool demangle,
875 vector<elf_symbol_sptr>& syms_found)
876{
877 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
878 ABG_ASSERT(sym_tab_section);
879
880 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
881 ABG_ASSERT(sym_tab_data);
882
883 GElf_Shdr sheader_mem;
884 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
885 &sheader_mem);
886 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
887 ABG_ASSERT(hash_section);
888
889 // Poke at the different parts of the hash table and get them ready
890 // to be used.
891 unsigned long hash = elf_hash(sym_name.c_str());
892 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
893 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
894 size_t nb_buckets = ht_data[0];
895 size_t nb_chains = ht_data[1];
896
897 if (nb_buckets == 0)
898 // An empty hash table. Not sure if that is possible, but it
899 // would mean an empty table of exported symbols.
900 return false;
901
902 //size_t nb_chains = ht_data[1];
903 Elf32_Word* ht_buckets = &ht_data[2];
904 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
905
906 // Now do the real work.
907 size_t bucket = hash % nb_buckets;
908 size_t symbol_index = ht_buckets[bucket];
909
910 GElf_Sym symbol;
911 const char* sym_name_str;
912 size_t sym_size;
913 elf_symbol::type sym_type;
914 elf_symbol::binding sym_binding;
915 elf_symbol::visibility sym_visibility;
916 bool found = false;
917
918 do
919 {
920 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
921 sym_name_str = elf_strptr(elf_handle,
922 sym_tab_section_header->sh_link,
923 symbol.st_name);
924 if (sym_name_str
925 && compare_symbol_name(sym_name_str, sym_name, demangle))
926 {
927 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
928 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
929 sym_visibility =
930 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
931 sym_size = symbol.st_size;
932 elf_symbol::version ver;
933 if (get_version_for_symbol(elf_handle, symbol_index,
934 /*get_def_version=*/true, ver))
935 ABG_ASSERT(!ver.str().empty());
936 elf_symbol_sptr symbol_found =
938 symbol_index,
939 sym_size,
940 sym_name_str,
941 sym_type,
942 sym_binding,
943 symbol.st_shndx != SHN_UNDEF,
944 symbol.st_shndx == SHN_COMMON,
945 ver, sym_visibility);
946 syms_found.push_back(symbol_found);
947 found = true;
948 }
949 symbol_index = ht_chains[symbol_index];
950 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
951
952 return found;
953}
954
955/// Get the size of the elf class, in bytes.
956///
957/// @param elf_handle the elf handle to use.
958///
959/// @return the size computed.
960static char
961get_elf_class_size_in_bytes(Elf* elf_handle)
962{
963 char result = 0;
964 GElf_Ehdr hdr;
965
966 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
967 int c = hdr.e_ident[EI_CLASS];
968
969 switch (c)
970 {
971 case ELFCLASS32:
972 result = 4;
973 break;
974 case ELFCLASS64:
975 result = 8;
976 break;
977 default:
979 }
980
981 return result;
982}
983
984/// Get a given word of a bloom filter, referred to by the index of
985/// the word.
986///
987/// The bloom word size depends on the current elf class (32 bits for
988/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
989/// abstracts that nicely.
990///
991/// @param elf_handle the elf handle to use.
992///
993/// @param bloom_filter the bloom filter to consider.
994///
995/// @param index the index of the bloom filter to return.
996///
997/// @return a 64 bits work containing the bloom word found at index @p
998/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
999/// most significant bytes of the result are going to be zero.
1000static Elf64_Xword
1001bloom_word_at(Elf* elf_handle,
1002 Elf32_Word* bloom_filter,
1003 size_t index)
1004{
1005 Elf64_Xword result = 0;
1006 GElf_Ehdr h;
1007 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
1008 int c;
1009 c = h.e_ident[EI_CLASS];
1010
1011 switch(c)
1012 {
1013 case ELFCLASS32:
1014 result = bloom_filter[index];
1015 break ;
1016 case ELFCLASS64:
1017 {
1018 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
1019 result = f[index];
1020 }
1021 break;
1022 default:
1023 abort();
1024 }
1025
1026 return result;
1027}
1028
1029/// The abstraction of the gnu elf hash table.
1030///
1031/// The members of this struct are explained at
1032/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1033/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1034struct gnu_ht
1035{
1036 size_t nb_buckets;
1037 Elf32_Word* buckets;
1038 Elf32_Word* chain;
1039 size_t first_sym_index;
1040 size_t bf_nwords;
1041 size_t bf_size;
1042 Elf32_Word* bloom_filter;
1043 size_t shift;
1044 size_t sym_count;
1045 Elf_Scn* sym_tab_section;
1046 GElf_Shdr sym_tab_section_header;
1047
1048 gnu_ht()
1049 : nb_buckets(0),
1050 buckets(0),
1051 chain(0),
1052 first_sym_index(0),
1053 bf_nwords(0),
1054 bf_size(0),
1055 bloom_filter(0),
1056 shift(0),
1057 sym_count(0),
1058 sym_tab_section(0)
1059 {}
1060}; // end struct gnu_ht
1061
1062/// Setup the members of the gnu hash table.
1063///
1064/// @param elf_handle a handle on the elf file to use.
1065///
1066/// @param ht_index the index (into the elf section headers table) of
1067/// the hash table section to use.
1068///
1069/// @param sym_tab_index the index (into the elf section headers
1070/// table) of the symbol table the gnu hash table is about.
1071///
1072/// @param ht the resulting hash table.
1073///
1074/// @return true iff the hash table @ ht could be setup.
1075static bool
1076setup_gnu_ht(Elf* elf_handle,
1077 size_t ht_index,
1078 size_t sym_tab_index,
1079 gnu_ht& ht)
1080{
1081 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1082 ABG_ASSERT(ht.sym_tab_section);
1083 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
1084 ht.sym_count =
1085 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
1086 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1087 ABG_ASSERT(hash_section);
1088
1089 // Poke at the different parts of the hash table and get them ready
1090 // to be used.
1091 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1092 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1093
1094 ht.nb_buckets = ht_data[0];
1095 if (ht.nb_buckets == 0)
1096 // An empty hash table. Not sure if that is possible, but it
1097 // would mean an empty table of exported symbols.
1098 return false;
1099 ht.first_sym_index = ht_data[1];
1100 // The number of words used by the bloom filter. A size of a word
1101 // is ELFCLASS.
1102 ht.bf_nwords = ht_data[2];
1103 // The shift used by the bloom filter code.
1104 ht.shift = ht_data[3];
1105 // The data of the bloom filter proper.
1106 ht.bloom_filter = &ht_data[4];
1107 // The size of the bloom filter in 4 bytes word. This is going to
1108 // be used to index the 'bloom_filter' above, which is of type
1109 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1110 // words.
1111 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1112 // The buckets of the hash table.
1113 ht.buckets = ht.bloom_filter + ht.bf_size;
1114 // The chain of the hash table.
1115 ht.chain = ht.buckets + ht.nb_buckets;
1116
1117 return true;
1118}
1119
1120/// Look into the symbol tables of the underlying elf file and find
1121/// the symbol we are being asked.
1122///
1123/// This function uses the GNU hash table for the symbol lookup.
1124///
1125/// The reference of for the implementation of this function can be
1126/// found at:
1127/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1128/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1129///
1130/// @param elf_handle the elf handle to use.
1131///
1132/// @param sym_name the name of the symbol to look for.
1133///
1134/// @param ht_index the index of the hash table header to use.
1135///
1136/// @param sym_tab_index the index of the symbol table header to use
1137/// with this hash table.
1138///
1139/// @param demangle if true, demangle @p sym_name.
1140///
1141/// @param syms_found the vector of symbols found with the name @p
1142/// sym_name.
1143///
1144/// @return true if a symbol was actually found.
1145static bool
1146lookup_symbol_from_gnu_hash_tab(const environment& env,
1147 Elf* elf_handle,
1148 const string& sym_name,
1149 size_t ht_index,
1150 size_t sym_tab_index,
1151 bool demangle,
1152 vector<elf_symbol_sptr>& syms_found)
1153{
1154 gnu_ht ht;
1155 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
1156 return false;
1157
1158 // Now do the real work.
1159
1160 // Compute bloom hashes (GNU hash and second bloom specific hashes).
1161 size_t h1 = elf_gnu_hash(sym_name.c_str());
1162 size_t h2 = h1 >> ht.shift;
1163 // The size of one of the words used in the bloom
1164 // filter, in bits.
1165 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1166 int n = (h1 / c) % ht.bf_nwords;
1167 // The bitmask of the bloom filter has a size of either 32-bits on
1168 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1169 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1170 // type used here. When dealing with 32bits binaries, the upper
1171 // bits of the bitmask will be zero anyway.
1172 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1173
1174 // Test if the symbol is *NOT* present in this ELF file.
1175 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1176 return false;
1177
1178 size_t i = ht.buckets[h1 % ht.nb_buckets];
1179 if (i == STN_UNDEF)
1180 return false;
1181
1182 Elf32_Word stop_word, *stop_wordp;
1183 elf_symbol::version ver;
1184 GElf_Sym symbol;
1185 const char* sym_name_str;
1186 bool found = false;
1187
1188 elf_symbol::type sym_type;
1189 elf_symbol::binding sym_binding;
1190 elf_symbol::visibility sym_visibility;
1191
1192 // Let's walk the hash table and record the versions of all the
1193 // symbols which name equal sym_name.
1194 for (i = ht.buckets[h1 % ht.nb_buckets],
1195 stop_wordp = &ht.chain[i - ht.first_sym_index];
1196 i != STN_UNDEF
1197 && (stop_wordp
1198 < ht.chain + (ht.sym_count - ht.first_sym_index));
1199 ++i, ++stop_wordp)
1200 {
1201 stop_word = *stop_wordp;
1202 if ((stop_word & ~ 1)!= (h1 & ~1))
1203 // A given bucket can reference several hashes. Here we
1204 // stumbled across a hash value different from the one we are
1205 // looking for. Let's keep walking.
1206 continue;
1207
1208 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1209 i, &symbol));
1210 sym_name_str = elf_strptr(elf_handle,
1211 ht.sym_tab_section_header.sh_link,
1212 symbol.st_name);
1213 if (sym_name_str
1214 && compare_symbol_name(sym_name_str, sym_name, demangle))
1215 {
1216 // So we found a symbol (in the symbol table) that equals
1217 // sym_name. Now lets try to get its version and record it.
1218 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1219 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1220 sym_visibility =
1221 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1222
1223 if (get_version_for_symbol(elf_handle, i,
1224 /*get_def_version=*/true,
1225 ver))
1226 ABG_ASSERT(!ver.str().empty());
1227
1228 elf_symbol_sptr symbol_found =
1229 elf_symbol::create(env, i,
1230 symbol.st_size,
1231 sym_name_str,
1232 sym_type, sym_binding,
1233 symbol.st_shndx != SHN_UNDEF,
1234 symbol.st_shndx == SHN_COMMON,
1235 ver, sym_visibility);
1236 syms_found.push_back(symbol_found);
1237 found = true;
1238 }
1239
1240 if (stop_word & 1)
1241 // The last bit of the stop_word is 1. That means we need to
1242 // stop here. We reached the end of the chain of values
1243 // referenced by the hask bucket.
1244 break;
1245 }
1246 return found;
1247}
1248
1249/// Look into the symbol tables of the underlying elf file and find
1250/// the symbol we are being asked.
1251///
1252/// This function uses the elf hash table (be it the GNU hash table or
1253/// the sysv hash table) for the symbol lookup.
1254///
1255/// @param env the environment we are operating from.
1256///
1257/// @param elf_handle the elf handle to use.
1258///
1259/// @param ht_kind the kind of hash table to use. This is returned by
1260/// the function function find_hash_table_section_index.
1261///
1262/// @param ht_index the index (in the section headers table) of the
1263/// hash table section to use.
1264///
1265/// @param sym_tab_index the index (in section headers table) of the
1266/// symbol table index to use with this hash table.
1267///
1268/// @param symbol_name the name of the symbol to look for.
1269///
1270/// @param demangle if true, demangle @p sym_name.
1271///
1272/// @param syms_found the symbols that were actually found with the
1273/// name @p symbol_name.
1274///
1275/// @return true iff the function found the symbol from the elf hash
1276/// table.
1277static bool
1278lookup_symbol_from_elf_hash_tab(const environment& env,
1279 Elf* elf_handle,
1280 hash_table_kind ht_kind,
1281 size_t ht_index,
1282 size_t symtab_index,
1283 const string& symbol_name,
1284 bool demangle,
1285 vector<elf_symbol_sptr>& syms_found)
1286{
1287 if (elf_handle == 0 || symbol_name.empty())
1288 return false;
1289
1290 if (ht_kind == NO_HASH_TABLE_KIND)
1291 return false;
1292
1293 if (ht_kind == SYSV_HASH_TABLE_KIND)
1294 return lookup_symbol_from_sysv_hash_tab(env,
1295 elf_handle, symbol_name,
1296 ht_index,
1297 symtab_index,
1298 demangle,
1299 syms_found);
1300 else if (ht_kind == GNU_HASH_TABLE_KIND)
1301 return lookup_symbol_from_gnu_hash_tab(env,
1302 elf_handle, symbol_name,
1303 ht_index,
1304 symtab_index,
1305 demangle,
1306 syms_found);
1307 return false;
1308}
1309
1310/// Lookup a symbol from the symbol table directly.
1311///
1312///
1313/// @param env the environment we are operating from.
1314///
1315/// @param elf_handle the elf handle to use.
1316///
1317/// @param sym_name the name of the symbol to look up.
1318///
1319/// @param sym_tab_index the index (in the section headers table) of
1320/// the symbol table section.
1321///
1322/// @param demangle if true, demangle the names found in the symbol
1323/// table before comparing them with @p sym_name.
1324///
1325/// @param sym_name_found the actual name of the symbol found.
1326///
1327/// @param sym_type the type of the symbol found.
1328///
1329/// @param sym_binding the binding of the symbol found.
1330///
1331/// @param sym_versions the versions of the symbol found.
1332///
1333/// @return true iff the symbol was found.
1334static bool
1335lookup_symbol_from_symtab(const environment& env,
1336 Elf* elf_handle,
1337 const string& sym_name,
1338 size_t sym_tab_index,
1339 bool demangle,
1340 vector<elf_symbol_sptr>& syms_found)
1341{
1342 // TODO: read all of the symbol table, store it in memory in a data
1343 // structure that associates each symbol with its versions and in
1344 // which lookups of a given symbol is fast.
1345 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1346 ABG_ASSERT(sym_tab_section);
1347
1348 GElf_Shdr header_mem;
1349 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1350 &header_mem);
1351
1352 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1353 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1354 GElf_Sym* sym;
1355 char* name_str = 0;
1356 elf_symbol::version ver;
1357 bool found = false;
1358
1359 for (size_t i = 0; i < symcount; ++i)
1360 {
1361 GElf_Sym sym_mem;
1362 sym = gelf_getsym(symtab, i, &sym_mem);
1363 name_str = elf_strptr(elf_handle,
1364 sym_tab_header->sh_link,
1365 sym->st_name);
1366
1367 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1368 {
1369 elf_symbol::type sym_type =
1370 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1371 elf_symbol::binding sym_binding =
1372 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1373 elf_symbol::visibility sym_visibility =
1374 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1375 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1376 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1377
1378 if (get_version_for_symbol(elf_handle, i,
1379 /*get_def_version=*/sym_is_defined,
1380 ver))
1381 ABG_ASSERT(!ver.str().empty());
1382 elf_symbol_sptr symbol_found =
1383 elf_symbol::create(env, i, sym->st_size,
1384 name_str, sym_type,
1385 sym_binding, sym_is_defined,
1386 sym_is_common, ver, sym_visibility);
1387 syms_found.push_back(symbol_found);
1388 found = true;
1389 }
1390 }
1391
1392 if (found)
1393 return true;
1394
1395 return false;
1396}
1397
1398/// Look into the symbol tables of the underlying elf file and see
1399/// if we find a given symbol.
1400///
1401/// @param env the environment we are operating from.
1402///
1403/// @param symbol_name the name of the symbol to look for.
1404///
1405/// @param demangle if true, try to demangle the symbol name found in
1406/// the symbol table before comparing it to @p symbol_name.
1407///
1408/// @param syms_found the list of symbols found, with the name @p
1409/// symbol_name.
1410///
1411/// @param sym_type this is set to the type of the symbol found. This
1412/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1413/// STT_FUNC, STT_IFUNC, etc ...
1414///
1415/// Note that this parameter is set iff the function returns true.
1416///
1417/// @param sym_binding this is set to the binding of the symbol found.
1418/// This is a standard elf.h value of the symbol binding kind, that
1419/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1420///
1421/// @param symbol_versions the versions of the symbol @p symbol_name,
1422/// if it was found.
1423///
1424/// @return true iff a symbol with the name @p symbol_name was found.
1425static bool
1426lookup_symbol_from_elf(const environment& env,
1427 Elf* elf_handle,
1428 const string& symbol_name,
1429 bool demangle,
1430 vector<elf_symbol_sptr>& syms_found)
1431{
1432 size_t hash_table_index = 0, symbol_table_index = 0;
1433 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1434
1435 if (!demangle)
1436 ht_kind = find_hash_table_section_index(elf_handle,
1437 hash_table_index,
1438 symbol_table_index);
1439
1440 if (ht_kind == NO_HASH_TABLE_KIND)
1441 {
1442 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1443 return false;
1444
1445 return lookup_symbol_from_symtab(env,
1446 elf_handle,
1447 symbol_name,
1448 symbol_table_index,
1449 demangle,
1450 syms_found);
1451 }
1452
1453 return lookup_symbol_from_elf_hash_tab(env,
1454 elf_handle,
1455 ht_kind,
1456 hash_table_index,
1457 symbol_table_index,
1458 symbol_name,
1459 demangle,
1460 syms_found);
1461}
1462
1463/// Look into the symbol tables of the underlying elf file and see if
1464/// we find a given public (global or weak) symbol of function type.
1465///
1466/// @param env the environment we are operating from.
1467///
1468/// @param elf_handle the elf handle to use for the query.
1469///
1470/// @param symbol_name the function symbol to look for.
1471///
1472/// @param func_syms the vector of public functions symbols found, if
1473/// any.
1474///
1475/// @return true iff the symbol was found.
1476static bool
1477lookup_public_function_symbol_from_elf(environment& env,
1478 Elf* elf_handle,
1479 const string& symbol_name,
1480 vector<elf_symbol_sptr>& func_syms)
1481{
1482 vector<elf_symbol_sptr> syms_found;
1483 bool found = false;
1484
1485 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1486 /*demangle=*/false, syms_found))
1487 {
1488 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1489 i != syms_found.end();
1490 ++i)
1491 {
1492 elf_symbol::type type = (*i)->get_type();
1493 elf_symbol::binding binding = (*i)->get_binding();
1494
1495 if ((type == elf_symbol::FUNC_TYPE
1496 || type == elf_symbol::GNU_IFUNC_TYPE
1497 || type == elf_symbol::COMMON_TYPE)
1498 && (binding == elf_symbol::GLOBAL_BINDING
1499 || binding == elf_symbol::WEAK_BINDING))
1500 {
1501 func_syms.push_back(*i);
1502 found = true;
1503 }
1504 }
1505 }
1506
1507 return found;
1508}
1509
1510// ---------------------------------------
1511// <location expression evaluation types>
1512// ---------------------------------------
1513
1514/// An abstraction of a value representing the result of the
1515/// evaluation of a dwarf expression. This is abstraction represents
1516/// a partial view on the possible values because we are only
1517/// interested in extracting the latest and longuest constant
1518/// sub-expression of a given dwarf expression.
1519class expr_result
1520{
1521 bool is_const_;
1522 int64_t const_value_;
1523
1524public:
1525 expr_result()
1526 : is_const_(true),
1527 const_value_(0)
1528 {}
1529
1530 expr_result(bool is_const)
1531 : is_const_(is_const),
1532 const_value_(0)
1533 {}
1534
1535 explicit expr_result(int64_t v)
1536 :is_const_(true),
1537 const_value_(v)
1538 {}
1539
1540 /// @return true if the value is a constant. Otherwise, return
1541 /// false, meaning the value represents a quantity for which we need
1542 /// inferior (a running program) state to determine the value.
1543 bool
1544 is_const() const
1545 {return is_const_;}
1546
1547
1548 /// @param f a flag saying if the value is set to a constant or not.
1549 void
1550 is_const(bool f)
1551 {is_const_ = f;}
1552
1553 /// Get the current constant value iff this represents a
1554 /// constant.
1555 ///
1556 /// @param value the out parameter. Is set to the constant value of
1557 /// the @ref expr_result. This is set iff the function return true.
1558 ///
1559 ///@return true if this has a constant value, false otherwise.
1560 bool
1561 const_value(int64_t& value)
1562 {
1563 if (is_const())
1564 {
1565 value = const_value_;
1566 return true;
1567 }
1568 return false;
1569 }
1570
1571 /// Getter of the constant value of the current @ref expr_result.
1572 ///
1573 /// Note that the current @ref expr_result must be constant,
1574 /// otherwise the current process is aborted.
1575 ///
1576 /// @return the constant value of the current @ref expr_result.
1577 int64_t
1578 const_value() const
1579 {
1580 ABG_ASSERT(is_const());
1581 return const_value_;
1582 }
1583
1584 operator int64_t() const
1585 {return const_value();}
1586
1587 expr_result&
1588 operator=(const int64_t v)
1589 {
1590 const_value_ = v;
1591 return *this;
1592 }
1593
1594 bool
1595 operator==(const expr_result& o) const
1596 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1597
1598 bool
1599 operator>=(const expr_result& o) const
1600 {return const_value_ >= o.const_value_;}
1601
1602 bool
1603 operator<=(const expr_result& o) const
1604 {return const_value_ <= o.const_value_;}
1605
1606 bool
1607 operator>(const expr_result& o) const
1608 {return const_value_ > o.const_value_;}
1609
1610 bool
1611 operator<(const expr_result& o) const
1612 {return const_value_ < o.const_value_;}
1613
1614 expr_result
1615 operator+(const expr_result& v) const
1616 {
1617 expr_result r(*this);
1618 r.const_value_ += v.const_value_;
1619 r.is_const_ = r.is_const_ && v.is_const_;
1620 return r;
1621 }
1622
1623 expr_result&
1624 operator+=(int64_t v)
1625 {
1626 const_value_ += v;
1627 return *this;
1628 }
1629
1630 expr_result
1631 operator-(const expr_result& v) const
1632 {
1633 expr_result r(*this);
1634 r.const_value_ -= v.const_value_;
1635 r.is_const_ = r.is_const_ && v.is_const_;
1636 return r;
1637 }
1638
1639 expr_result
1640 operator%(const expr_result& v) const
1641 {
1642 expr_result r(*this);
1643 r.const_value_ %= v.const_value_;
1644 r.is_const_ = r.is_const_ && v.is_const();
1645 return r;
1646 }
1647
1648 expr_result
1649 operator*(const expr_result& v) const
1650 {
1651 expr_result r(*this);
1652 r.const_value_ *= v.const_value_;
1653 r.is_const_ = r.is_const_ && v.is_const();
1654 return r;
1655 }
1656
1657 expr_result
1658 operator|(const expr_result& v) const
1659 {
1660 expr_result r(*this);
1661 r.const_value_ |= v.const_value_;
1662 r.is_const_ = r.is_const_ && v.is_const_;
1663 return r;
1664 }
1665
1666 expr_result
1667 operator^(const expr_result& v) const
1668 {
1669 expr_result r(*this);
1670 r.const_value_ ^= v.const_value_;
1671 r.is_const_ = r.is_const_ && v.is_const_;
1672 return r;
1673 }
1674
1675 expr_result
1676 operator>>(const expr_result& v) const
1677 {
1678 expr_result r(*this);
1679 r.const_value_ = r.const_value_ >> v.const_value_;
1680 r.is_const_ = r.is_const_ && v.is_const_;
1681 return r;
1682 }
1683
1684 expr_result
1685 operator<<(const expr_result& v) const
1686 {
1687 expr_result r(*this);
1688 r.const_value_ = r.const_value_ << v.const_value_;
1689 r.is_const_ = r.is_const_ && v.is_const_;
1690 return r;
1691 }
1692
1693 expr_result
1694 operator~() const
1695 {
1696 expr_result r(*this);
1697 r.const_value_ = ~r.const_value_;
1698 return r;
1699 }
1700
1701 expr_result
1702 neg() const
1703 {
1704 expr_result r(*this);
1705 r.const_value_ = -r.const_value_;
1706 return r;
1707 }
1708
1709 expr_result
1710 abs() const
1711 {
1712 expr_result r = *this;
1713 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1714 return r;
1715 }
1716
1717 expr_result
1718 operator&(const expr_result& o)
1719 {
1720 expr_result r(*this);
1721 r.const_value_ &= o.const_value_;
1722 r.is_const_ = r.is_const_ && o.is_const_;
1723 return r;
1724 }
1725
1726 expr_result
1727 operator/(const expr_result& o)
1728 {
1729 expr_result r(*this);
1730 r.is_const_ = r.is_const_ && o.is_const_;
1731 return r.const_value() / o.const_value();
1732 }
1733};// class end expr_result;
1734
1735/// A class that implements a stack of @ref expr_result, to be used in
1736/// the engine evaluating DWARF expressions.
1737class expr_result_stack_type
1738{
1739 vector<expr_result> elems_;
1740
1741public:
1742
1743 expr_result_stack_type()
1744 {elems_.reserve(4);}
1745
1746 expr_result&
1747 operator[](unsigned i)
1748 {
1749 unsigned s = elems_.size();
1750 ABG_ASSERT(s > i);
1751 return elems_[s - 1 -i];
1752 }
1753
1754 const expr_result&
1755 operator[](unsigned i) const
1756 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1757
1758 unsigned
1759 size() const
1760 {return elems_.size();}
1761
1762 vector<expr_result>::reverse_iterator
1763 begin()
1764 {return elems_.rbegin();}
1765
1766 const vector<expr_result>::reverse_iterator
1767 begin() const
1768 {return const_cast<expr_result_stack_type*>(this)->begin();}
1769
1770 vector<expr_result>::reverse_iterator
1771 end()
1772 {return elems_.rend();}
1773
1774 const vector<expr_result>::reverse_iterator
1775 end() const
1776 {return const_cast<expr_result_stack_type*>(this)->end();}
1777
1778 expr_result&
1779 front()
1780 {return elems_.back();}
1781
1782 const expr_result&
1783 front() const
1784 {return const_cast<expr_result_stack_type*>(this)->front();}
1785
1786 void
1787 push_front(expr_result e)
1788 {elems_.push_back(e);}
1789
1790 expr_result
1791 pop_front()
1792 {
1793 expr_result r = front();
1794 elems_.pop_back();
1795 return r;
1796 }
1797
1798 void
1799 erase(vector<expr_result>::reverse_iterator i)
1800 {elems_.erase(--i.base());}
1801
1802 void
1803 clear()
1804 {elems_.clear();}
1805}; // end class expr_result_stack_type
1806
1807/// Abstraction of the evaluation context of a dwarf expression.
1808struct dwarf_expr_eval_context
1809{
1810 expr_result accum;
1811 expr_result_stack_type stack;
1812 // Is set to true if the result of the expression that got evaluated
1813 // is a TLS address.
1814 bool set_tls_addr;
1815
1816 dwarf_expr_eval_context()
1817 : accum(/*is_const=*/false),
1818 set_tls_addr(false)
1819 {
1820 stack.push_front(expr_result(true));
1821 }
1822
1823 void
1824 reset()
1825 {
1826 stack.clear();
1827 stack.push_front(expr_result(true));
1828 accum = expr_result(false);
1829 set_tls_addr = false;
1830 }
1831
1832 /// Set a flag to to tell that the result of the expression that got
1833 /// evaluated is a TLS address.
1834 ///
1835 /// @param f true iff the result of the expression that got
1836 /// evaluated is a TLS address, false otherwise.
1837 void
1838 set_tls_address(bool f)
1839 {set_tls_addr = f;}
1840
1841 /// Getter for the flag that tells if the result of the expression
1842 /// that got evaluated is a TLS address.
1843 ///
1844 /// @return true iff the result of the expression that got evaluated
1845 /// is a TLS address.
1846 bool
1847 set_tls_address() const
1848 {return set_tls_addr;}
1849
1850 expr_result
1851 pop()
1852 {
1853 expr_result r = stack.front();
1854 stack.pop_front();
1855 return r;
1856 }
1857
1858 void
1859 push(const expr_result& v)
1860 {stack.push_front(v);}
1861};//end class dwarf_expr_eval_context
1862
1863// ---------------------------------------
1864// </location expression evaluation types>
1865// ---------------------------------------
1866
1867class reader;
1868
1869typedef shared_ptr<reader> reader_sptr;
1870
1871/// The DWARF reader used to build the ABI corpus from debug info in
1872/// DWARF format.
1873///
1874/// This type is to be instanciated
1875/// abigail::dwarf::reader::create().
1876class reader : public elf_based_reader
1877{
1878public:
1879
1880 /// A set of containers that contains one container per kind of @ref
1881 /// die_source. This allows to associate DIEs to things, depending
1882 /// on the source of the DIE.
1883 template <typename ContainerType>
1884 class die_source_dependant_container_set
1885 {
1886 ContainerType primary_debug_info_container_;
1887 ContainerType alt_debug_info_container_;
1888 ContainerType type_unit_container_;
1889
1890 public:
1891
1892 /// Getter for the container associated to DIEs coming from a
1893 /// given @ref die_source.
1894 ///
1895 /// @param source the die_source for which we want the container.
1896 ///
1897 /// @return the container that associates DIEs coming from @p
1898 /// source to something.
1899 ContainerType&
1900 get_container(die_source source)
1901 {
1902 ContainerType *result = 0;
1903 switch (source)
1904 {
1905 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1906 result = &primary_debug_info_container_;
1907 break;
1908 case ALT_DEBUG_INFO_DIE_SOURCE:
1909 result = &alt_debug_info_container_;
1910 break;
1911 case TYPE_UNIT_DIE_SOURCE:
1912 result = &type_unit_container_;
1913 break;
1914 case NO_DEBUG_INFO_DIE_SOURCE:
1915 case NUMBER_OF_DIE_SOURCES:
1917 }
1918 return *result;
1919 }
1920
1921 /// Getter for the container associated to DIEs coming from a
1922 /// given @ref die_source.
1923 ///
1924 /// @param source the die_source for which we want the container.
1925 ///
1926 /// @return the container that associates DIEs coming from @p
1927 /// source to something.
1928 const ContainerType&
1929 get_container(die_source source) const
1930 {
1931 return const_cast<die_source_dependant_container_set*>(this)->
1932 get_container(source);
1933 }
1934
1935 /// Getter for the container associated to DIEs coming from the
1936 /// same source as a given DIE.
1937 ///
1938 /// @param rdr the DWARF reader to consider.
1939 ///
1940 /// @param die the DIE which should have the same source as the
1941 /// source of the container we want.
1942 ///
1943 /// @return the container that associates DIEs coming from the
1944 /// same source as @p die.
1945 ContainerType&
1946 get_container(const reader& rdr, const Dwarf_Die *die)
1947 {
1948 const die_source source = rdr.get_die_source(die);
1949 return get_container(source);
1950 }
1951
1952 /// Getter for the container associated to DIEs coming from the
1953 /// same source as a given DIE.
1954 ///
1955 /// @param rdr the DWARF reader to consider.
1956 ///
1957 /// @param die the DIE which should have the same source as the
1958 /// source of the container we want.
1959 ///
1960 /// @return the container that associates DIEs coming from the
1961 /// same source as @p die.
1962 const ContainerType&
1963 get_container(const reader& rdr, const Dwarf_Die *die) const
1964 {
1965 return const_cast<die_source_dependant_container_set*>(this)->
1966 get_container(rdr, die);
1967 }
1968
1969 /// Clear the container set.
1970 void
1971 clear()
1972 {
1973 primary_debug_info_container_.clear();
1974 alt_debug_info_container_.clear();
1975 type_unit_container_.clear();
1976 }
1977 }; // end die_dependant_container_set
1978
1979 /// Statistics to help for debugging purposes.
1980 struct stats
1981 {
1982 unsigned number_of_suppressed_functions = 0;
1983 unsigned number_of_suppressed_variables = 0;
1984 unsigned number_of_allowed_functions = 0;
1985 unsigned number_of_allowed_variables = 0;
1986
1987 /// Clear the statistic data members.
1988 void
1989 clear()
1990 {
1991 number_of_suppressed_functions = 0;
1992 number_of_suppressed_variables = 0;
1993 number_of_allowed_functions = 0;
1994 number_of_allowed_variables = 0;
1995 }
1996 };
1997
1998 unsigned short dwarf_version_;
1999 Dwarf_Die* cur_tu_die_;
2000 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
2001 // A set of maps (one per kind of die source) that associates a decl
2002 // string representation with the DIEs (offsets) representing that
2003 // decl.
2004 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2005 decl_die_repr_die_offsets_maps_;
2006 // A set of maps (one per kind of die source) that associates a type
2007 // string representation with the DIEs (offsets) representing that
2008 // type.
2009 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2010 type_die_repr_die_offsets_maps_;
2011 mutable die_source_dependant_container_set<die_istring_map_type>
2012 die_qualified_name_maps_;
2013 mutable die_source_dependant_container_set<die_istring_map_type>
2014 die_pretty_repr_maps_;
2015 mutable die_source_dependant_container_set<die_istring_map_type>
2016 die_pretty_type_repr_maps_;
2017 // A set of maps (one per kind of die source) that associates the
2018 // offset of a decl die to its corresponding decl artifact.
2019 mutable die_source_dependant_container_set<die_artefact_map_type>
2020 decl_die_artefact_maps_;
2021 // A set of maps (one per kind of die source) that associates the
2022 // offset of a type die to its corresponding type artifact.
2023 mutable die_source_dependant_container_set<die_artefact_map_type>
2024 type_die_artefact_maps_;
2025 /// A set of vectors (one per kind of die source) that associates
2026 /// the offset of a type DIE to the offset of its canonical DIE.
2027 mutable die_source_dependant_container_set<offset_offset_map_type>
2028 canonical_type_die_offsets_;
2029 /// A set of vectors (one per kind of die source) that associates
2030 /// the offset of a decl DIE to the offset of its canonical DIE.
2031 mutable die_source_dependant_container_set<offset_offset_map_type>
2032 canonical_decl_die_offsets_;
2033 /// A map that associates a function type representations to
2034 /// function types, inside a translation unit.
2035 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
2036 /// A map that associates a pair of DIE offsets to the result of the
2037 /// comparison of that pair.
2038 mutable std::unordered_map<std::pair<offset_type,offset_type>,
2040 dwarf_offset_pair_hash> die_comparison_results_;
2041 // The set of types pair that have been canonical-type-propagated.
2042 mutable offset_pair_set_type propagated_types_;
2043 die_class_or_union_map_type die_wip_classes_map_;
2044 die_class_or_union_map_type alternate_die_wip_classes_map_;
2045 die_class_or_union_map_type type_unit_die_wip_classes_map_;
2046 die_function_type_map_type die_wip_function_types_map_;
2047 die_function_type_map_type alternate_die_wip_function_types_map_;
2048 die_function_type_map_type type_unit_die_wip_function_types_map_;
2049 die_function_decl_map_type die_function_with_no_symbol_map_;
2050 vector<type_base_sptr> types_to_canonicalize_;
2051 string_classes_or_unions_map decl_only_classes_map_;
2052 string_enums_map decl_only_enums_map_;
2053 die_tu_map_type die_tu_map_;
2054 translation_unit_sptr cur_tu_;
2055 scope_decl_sptr nil_scope_;
2056 scope_stack_type scope_stack_;
2057 offset_offset_map_type primary_die_parent_map_;
2058 // A map that associates each tu die to a vector of unit import
2059 // points, in the main debug info
2060 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
2061 // A map that associates each tu die to a vector of unit import
2062 // points, in the alternate debug info
2063 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
2064 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
2065 // A DIE -> parent map for DIEs coming from the alternate debug info
2066 // file.
2067 offset_offset_map_type alternate_die_parent_map_;
2068 offset_offset_map_type type_section_die_parent_map_;
2069 list<var_decl_sptr> var_decls_to_add_;
2070#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2071 bool debug_die_canonicalization_is_on_;
2072 bool use_canonical_die_comparison_;
2073#endif
2074 mutable size_t compare_count_;
2075 mutable size_t canonical_propagated_count_;
2076 mutable size_t cancelled_propagation_count_;
2077 mutable optional<bool> leverage_dwarf_factorization_;
2078 mutable stats stats_;
2079
2080protected:
2081
2082 reader() = delete;
2083
2084 /// Constructor of reader.
2085 ///
2086 /// @param elf_path the path to the elf file the context is to be
2087 /// used for.
2088 ///
2089 /// @param debug_info_root_paths a vector of pointers to the path to
2090 /// the root directory under which the debug info is to be found for
2091 /// @p elf_path. Leave this empty if the debug info is not in a
2092 /// split file.
2093 ///
2094 /// @param environment the environment used by the current context.
2095 /// This environment contains resources needed by the DWARF reader and by
2096 /// the types and declarations that are to be created later. Note
2097 /// that ABI artifacts that are to be compared all need to be
2098 /// created within the same environment.
2099 ///
2100 /// Please also note that the life time of this environment object
2101 /// must be greater than the life time of the resulting @ref
2102 /// reader the context uses resources that are allocated in
2103 /// the environment.
2104 ///
2105 /// @param load_all_types if set to false only the types that are
2106 /// reachable from publicly exported declarations (of functions and
2107 /// variables) are read. If set to true then all types found in the
2108 /// debug information are loaded.
2109 ///
2110 /// @param linux_kernel_mode if set to true, then consider the special
2111 /// linux kernel symbol tables when determining if a symbol is
2112 /// exported or not.
2113 reader(const string& elf_path,
2114 const vector<char**>& debug_info_root_paths,
2115 environment& environment,
2116 bool load_all_types,
2117 bool linux_kernel_mode)
2118 : elf_based_reader(elf_path,
2120 environment)
2121 {
2122 initialize(load_all_types, linux_kernel_mode);
2123 }
2124
2125 /// Clear the statistics for reading the current corpus.
2126 void
2127 clear_stats()
2128 {
2129 stats_.clear();
2130 }
2131
2132public:
2133
2134 /// Initializer of reader.
2135 ///
2136 /// Resets the reader so that it can be re-used to read another binary.
2137 ///
2138 /// @param load_all_types if set to false only the types that are
2139 /// reachable from publicly exported declarations (of functions and
2140 /// variables) are read. If set to true then all types found in the
2141 /// debug information are loaded.
2142 ///
2143 /// @param linux_kernel_mode if set to true, then consider the
2144 /// special linux kernel symbol tables when determining if a symbol
2145 /// is exported or not.
2146 void
2147 initialize(bool load_all_types, bool linux_kernel_mode)
2148 {
2149 dwarf_version_ = 0;
2150 cur_tu_die_ = 0;
2151 decl_die_repr_die_offsets_maps_.clear();
2152 type_die_repr_die_offsets_maps_.clear();
2153 die_qualified_name_maps_.clear();
2154 die_pretty_repr_maps_.clear();
2155 die_pretty_type_repr_maps_.clear();
2156 decl_die_artefact_maps_.clear();
2157 type_die_artefact_maps_.clear();
2158 canonical_type_die_offsets_.clear();
2159 canonical_decl_die_offsets_.clear();
2160 die_wip_classes_map_.clear();
2161 alternate_die_wip_classes_map_.clear();
2162 type_unit_die_wip_classes_map_.clear();
2163 die_wip_function_types_map_.clear();
2164 alternate_die_wip_function_types_map_.clear();
2165 type_unit_die_wip_function_types_map_.clear();
2166 die_function_with_no_symbol_map_.clear();
2167 types_to_canonicalize_.clear();
2168 decl_only_classes_map_.clear();
2169 die_tu_map_.clear();
2170 corpus().reset();
2171 corpus_group().reset();
2172 cur_tu_.reset();
2173 primary_die_parent_map_.clear();
2174 tu_die_imported_unit_points_map_.clear();
2175 alt_tu_die_imported_unit_points_map_.clear();
2176 type_units_tu_die_imported_unit_points_map_.clear();
2177 alternate_die_parent_map_.clear();
2178 type_section_die_parent_map_.clear();
2179 var_decls_to_add_.clear();
2180 clear_per_translation_unit_data();
2181 clear_per_corpus_data();
2182 options().load_in_linux_kernel_mode = linux_kernel_mode;
2183 options().load_all_types = load_all_types;
2184#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2185 debug_die_canonicalization_is_on_ =
2186 env().debug_die_canonicalization_is_on();
2187 use_canonical_die_comparison_ = true;
2188#endif
2189 compare_count_ = 0;
2190 canonical_propagated_count_ = 0;
2191 cancelled_propagation_count_ = 0;
2192 load_in_linux_kernel_mode(linux_kernel_mode);
2193 clear_stats();
2194 }
2195
2196 /// Initializer of reader.
2197 ///
2198 /// Resets the reader so that it can be re-used to read another binary.
2199 ///
2200 /// @param elf_path the path to the new ELF file.
2201 ///
2202 /// @param debug_info_root_paths the vector of debug-info path to
2203 /// look for split debug info.
2204 ///
2205 /// @param load_all_types if set to false only the types that are
2206 /// reachable from publicly exported declarations (of functions and
2207 /// variables) are read. If set to true then all types found in the
2208 /// debug information are loaded.
2209 ///
2210 /// @param linux_kernel_mode if set to true, then consider the
2211 /// special linux kernel symbol tables when determining if a symbol
2212 /// is exported or not.
2213 void
2214 initialize(const string& elf_path,
2215 const vector<char**>& debug_info_root_paths,
2216 bool load_all_types,
2217 bool linux_kernel_mode)
2218 {
2220 initialize(load_all_types, linux_kernel_mode);
2221 }
2222
2223 /// Create an instance of DWARF Reader.
2224 ///
2225 /// @param elf_path the path to the ELF file to read from.
2226 ///
2227 /// @param debug_info_root_paths a vector of paths where to look up
2228 /// split debug info files.
2229 ///
2230 /// @param environment the environment to be used by the reader.
2231 ///
2232 /// @param load_all_types if set to false only the types that are
2233 /// reachable from publicly exported declarations (of functions and
2234 /// variables) are read. If set to true then all types found in the
2235 /// debug information are loaded.
2236 ///
2237 /// @param linux_kernel_mode if set to true, then consider the
2238 /// special linux kernel symbol tables when determining if a symbol
2239 /// is exported or not.
2240 static dwarf::reader_sptr
2241 create(const std::string& elf_path,
2242 const vector<char**>& debug_info_root_paths,
2243 environment& environment,
2244 bool load_all_types,
2245 bool linux_kernel_mode)
2246 {
2247 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2248 environment, load_all_types,
2249 linux_kernel_mode));
2250 return result;
2251 }
2252
2253 /// Destructor of the @ref reader type.
2254 ~reader()
2255 {
2256 }
2257
2258 /// Read and analyze the ELF and DWARF information associated with
2259 /// the underlying ELF file and build an ABI corpus out of it.
2260 ///
2261 /// @param status output parameter. This is set to the status of
2262 /// the analysis of the debug info.
2263 ///
2264 /// @return the resulting ABI corpus.
2265 corpus_sptr
2266 read_corpus(status& status)
2267 {
2269
2270 // Load the generic ELF parts of the corpus.
2272
2273 if (!(status & STATUS_OK))
2274 {
2275 // Something went badly wrong. There is nothing we can do
2276 // with this ELF file. Bail out.
2277 return corpus_sptr();
2278 }
2279
2280 // If we couldn't find debug info from the elf path, then say it.
2281 if (dwarf_debug_info() == nullptr)
2283
2284 {
2285 string alt_di_path;
2286 if (refers_to_alt_debug_info(alt_di_path)
2289 }
2290
2291 if (// If debug info was found but not the required alternate debug
2292 // info ...
2295 // ... then we cannot handle the binary.
2296 return corpus_sptr();
2297
2298 // Read the variable and function descriptions from the debug info
2299 // we have, through the dwfl handle.
2300 corpus_sptr corp = read_debug_info_into_corpus();
2301
2302 status |= STATUS_OK;
2303
2304 return corp;
2305 }
2306
2307 /// Read an analyze the DWARF information.
2308 ///
2309 /// Construct an ABI corpus from it.
2310 ///
2311 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2312 ///
2313 /// @return the resulting ABI corpus.
2314 corpus_sptr
2315 read_debug_info_into_corpus()
2316 {
2317 // First set some mundane properties of the corpus gathered from
2318 // ELF.
2319 corpus::origin origin = corpus()->get_origin();
2320 origin |= corpus::DWARF_ORIGIN;
2321 corpus()->set_origin(origin);
2322 if (corpus_group())
2323 {
2324 origin |= corpus_group()->get_origin();
2325 corpus_group()->set_origin(origin);
2326 }
2327
2328 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2329 && !env().user_set_analyze_exported_interfaces_only())
2330 // So we are looking at the Linux Kernel and the user has not set
2331 // any particular option regarding the amount of types to analyse.
2332 // In that case, we need to only analyze types that are reachable
2333 // from exported interfaces otherwise we get such a massive amount
2334 // of type DIEs to look at that things are just too slow down the
2335 // road.
2336 env().analyze_exported_interfaces_only(true);
2337
2338 corpus()->set_soname(dt_soname());
2339 corpus()->set_needed(dt_needed());
2340 corpus()->set_architecture_name(elf_architecture());
2341 // Set symbols information to the corpus.
2342 corpus()->set_symtab(symtab());
2343
2344 // Get out now if no debug info is found or if the symbol table is
2345 // empty.
2346 if (!dwarf_debug_info()
2347 || !corpus()->get_symtab()
2348 || !corpus()->get_symtab()->has_symbols())
2349 return corpus();
2350
2351 uint8_t address_size = 0;
2352 size_t header_size = 0;
2353
2354#ifdef WITH_DEBUG_SELF_COMPARISON
2355 if (env().self_comparison_debug_is_on())
2356 {
2357 corpus_group_sptr g = corpus_group();
2358 if (g)
2359 env().set_self_comparison_debug_input(g);
2360 else
2361 env().set_self_comparison_debug_input(corpus());
2362 }
2363#endif
2364
2365 env().priv_->do_log(do_log());
2366
2367 // Walk all the DIEs of the debug info to build a DIE -> parent map
2368 // useful for get_die_parent() to work.
2369 {
2370 tools_utils::timer t;
2371 if (do_log())
2372 {
2373 cerr << "building die -> parent maps ...";
2374 t.start();
2375 }
2376
2377 build_die_parent_maps();
2378
2379 if (do_log())
2380 {
2381 t.stop();
2382 cerr << " DONE@" << corpus()->get_path()
2383 << ":"
2384 << t
2385 << "\n";
2386 }
2387 }
2388
2389 env().canonicalization_is_done(false);
2390
2391 {
2392 tools_utils::timer t;
2393 if (do_log())
2394 {
2395 cerr << "DWARF Reader: building the "
2396 "libabigail internal representation ...\n";
2397 t.start();
2398 }
2399 // And now walk all the DIEs again to build the libabigail IR.
2400 Dwarf_Half dwarf_vers = 0;
2401 for (Dwarf_Off offset = 0, next_offset = 0;
2402 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2403 offset, &next_offset, &header_size,
2404 &dwarf_vers, NULL, &address_size, NULL,
2405 NULL, NULL) == 0);
2406 offset = next_offset)
2407 {
2408 Dwarf_Off die_offset = offset + header_size;
2409 Dwarf_Die unit;
2410 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2411 die_offset, &unit)
2412 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2413 continue;
2414
2415 dwarf_version(dwarf_vers);
2416
2417 address_size *= 8;
2418
2419 // Build a translation_unit IR node from cu; note that cu must
2420 // be a DW_TAG_compile_unit die.
2421 translation_unit_sptr ir_node =
2422 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2423 ABG_ASSERT(ir_node);
2424 }
2425 if (do_log())
2426 {
2427 t.stop();
2428 cerr << "DWARF Reader: building "
2429 << "the libabigail internal representation "
2430 << "DONE for corpus " << corpus()->get_path()
2431 << " in: "
2432 << t
2433 << "\n";
2434
2435 cerr << "DWARF Reader: Number of aggregate types compared: "
2436 << compare_count_ << "\n"
2437 << "Number of canonical types propagated: "
2438 << canonical_propagated_count_ << "\n"
2439 << "Number of cancelled propagated canonical types:"
2440 << cancelled_propagation_count_ << "\n"
2441 << "Number of suppressed functions: "
2442 << stats_.number_of_suppressed_functions << "\n"
2443 << "Number of allowed functions: "
2444 << stats_.number_of_allowed_functions << "\n"
2445 << "Total number of fns in the corpus: "
2446 << corpus()->get_functions().size() << "\n"
2447 << "Total number of variables in the corpus: "
2448 << corpus()->get_variables().size() << "\n";
2449 }
2450 }
2451
2452 {
2453 tools_utils::timer t;
2454 if (do_log())
2455 {
2456 cerr << "DWARF Reader: resolving declaration only classes ...";
2457 t.start();
2458 }
2459 resolve_declaration_only_classes();
2460 if (do_log())
2461 {
2462 t.stop();
2463 cerr << " DONE@" << corpus()->get_path()
2464 << " in :"
2465 << t
2466 <<"\n";
2467 }
2468 }
2469
2470 {
2471 tools_utils::timer t;
2472 if (do_log())
2473 {
2474 cerr << "resolving declaration only enums ...";
2475 t.start();
2476 }
2477 resolve_declaration_only_enums();
2478 if (do_log())
2479 {
2480 t.stop();
2481 cerr << " DONE@" << corpus()->get_path()
2482 << ":"
2483 << t
2484 <<"\n";
2485 }
2486 }
2487
2488 {
2489 tools_utils::timer t;
2490 if (do_log())
2491 {
2492 cerr << "DWARF Reader: fixing up functions with linkage name but "
2493 << "no advertised underlying symbols ....";
2494 t.start();
2495 }
2496 fixup_functions_with_no_symbols();
2497 if (do_log())
2498 {
2499 t.stop();
2500 cerr << " DONE@" << corpus()->get_path()
2501 <<" in :"
2502 << t
2503 <<"\n";
2504 }
2505 }
2506
2507 merge_member_functions_in_classes_of_same_names();
2508
2509 /// Now, look at the types that needs to be canonicalized after the
2510 /// translation has been constructed (which is just now) and
2511 /// canonicalize them.
2512 ///
2513 /// These types need to be constructed at the end of the translation
2514 /// unit reading phase because some types are modified by some DIEs
2515 /// even after the principal DIE describing the type has been read;
2516 /// this happens for clones of virtual destructors (for instance) or
2517 /// even for some static data members. We need to do that for types
2518 /// are in the alternate debug info section and for types that in
2519 /// the main debug info section.
2520 {
2521 tools_utils::timer t;
2522 if (do_log())
2523 {
2524 cerr << "DWARF Reader: perform late type canonicalizing ...\n";
2525 t.start();
2526 }
2527
2528 perform_late_type_canonicalizing();
2529 if (do_log())
2530 {
2531 t.stop();
2532 cerr << "DWARF Reader: late type canonicalizing DONE for "
2533 << corpus()->get_path()
2534 << " in :"
2535 << t
2536 << "\n";
2537 }
2538 }
2539
2540 env().canonicalization_is_done(true);
2541
2542 {
2543 tools_utils::timer t;
2544 if (do_log())
2545 {
2546 cerr << "DWARF Reader: sort functions and variables ...";
2547 t.start();
2548 }
2549 corpus()->sort_functions();
2550 corpus()->sort_variables();
2551 if (do_log())
2552 {
2553 t.stop();
2554 cerr << " DONE@" << corpus()->get_path()
2555 << ":"
2556 << t
2557 <<" \n";
2558 }
2559 }
2560
2561 return corpus();
2562 }
2563
2564 /// Clear the data that is relevant only for the current translation
2565 /// unit being read. The rest of the data is relevant for the
2566 /// entire ABI corpus.
2567 void
2568 clear_per_translation_unit_data()
2569 {
2570 while (!scope_stack().empty())
2571 scope_stack().pop();
2572 var_decls_to_re_add_to_tree().clear();
2573 per_tu_repr_to_fn_type_maps().clear();
2574 }
2575
2576 /// Clear the data that is relevant for the current corpus being
2577 /// read.
2578 void
2579 clear_per_corpus_data()
2580 {
2581 die_qualified_name_maps_.clear();
2582 die_pretty_repr_maps_.clear();
2583 die_pretty_type_repr_maps_.clear();
2584 clear_types_to_canonicalize();
2585 }
2586
2587 /// Getter for the current environment.
2588 ///
2589 /// @return the current environment.
2590 environment&
2591 env()
2592 {return options().env;}
2593
2594 /// Getter for the current environment.
2595 ///
2596 /// @return the current environment.
2597 const environment&
2598 env() const
2599 {return const_cast<reader*>(this)->env();}
2600
2601 /// Getter for the flag that tells us if we are dropping functions
2602 /// and variables that have undefined symbols.
2603 ///
2604 /// @return true iff we are dropping functions and variables that have
2605 /// undefined symbols.
2606 bool
2607 drop_undefined_syms() const
2608 {return options().drop_undefined_syms;}
2609
2610 /// Setter for the flag that tells us if we are dropping functions
2611 /// and variables that have undefined symbols.
2612 ///
2613 /// @param f the new value of the flag.
2614 void
2615 drop_undefined_syms(bool f)
2616 {options().drop_undefined_syms = f;}
2617
2618 /// Getter of the DWARF version.
2619 unsigned short
2620 dwarf_version() const
2621 {return dwarf_version_;}
2622
2623 void
2624 dwarf_version(unsigned short v)
2625 {dwarf_version_ = v;}
2626
2627 /// Return the ELF descriptor used for DWARF access.
2628 ///
2629 /// This can be the same as reader::elf_handle() above, if the
2630 /// DWARF info is in the same ELF file as the one of the binary we
2631 /// are analizing. It is different if e.g, the debug info is split
2632 /// from the ELF file we are analizing.
2633 ///
2634 /// @return a pointer to the ELF descriptor used to access debug
2635 /// info.
2636 Elf*
2637 dwarf_elf_handle() const
2638 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2639
2640 /// Test if the debug information is in a separate ELF file wrt the
2641 /// main ELF file of the program (application or shared library) we
2642 /// are analizing.
2643 ///
2644 /// @return true if the debug information is in a separate ELF file
2645 /// compared to the main ELF file of the program (application or
2646 /// shared library) that we are looking at.
2647 bool
2648 dwarf_is_splitted() const
2649 {return dwarf_elf_handle() != elf_handle();}
2650
2651 /// Return the correct debug info, depending on the DIE source we
2652 /// are looking at.
2653 ///
2654 /// @param source the DIE source to consider.
2655 ///
2656 /// @return the right debug info, depending on @p source.
2657 const Dwarf*
2658 dwarf_per_die_source(die_source source) const
2659 {
2660 const Dwarf *result = 0;
2661 switch(source)
2662 {
2663 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2664 case TYPE_UNIT_DIE_SOURCE:
2665 result = dwarf_debug_info();
2666 break;
2667 case ALT_DEBUG_INFO_DIE_SOURCE:
2668 result = alternate_dwarf_debug_info();
2669 break;
2670 case NO_DEBUG_INFO_DIE_SOURCE:
2671 case NUMBER_OF_DIE_SOURCES:
2673 }
2674 return result;
2675 }
2676
2677 /// Return the path to the ELF path we are reading.
2678 ///
2679 /// @return the elf path.
2680 const string&
2681 elf_path() const
2682 {return corpus_path();}
2683
2684 const Dwarf_Die*
2685 cur_tu_die() const
2686 {return cur_tu_die_;}
2687
2688 void
2689 cur_tu_die(Dwarf_Die* cur_tu_die)
2690 {cur_tu_die_ = cur_tu_die;}
2691
2692 dwarf_expr_eval_context&
2693 dwarf_expr_eval_ctxt() const
2694 {return dwarf_expr_eval_context_;}
2695
2696 /// Getter of the maps set that associates a representation of a
2697 /// decl DIE to a vector of offsets of DIEs having that representation.
2698 ///
2699 /// @return the maps set that associates a representation of a decl
2700 /// DIE to a vector of offsets of DIEs having that representation.
2701 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2702 decl_die_repr_die_offsets_maps() const
2703 {return decl_die_repr_die_offsets_maps_;}
2704
2705 /// Getter of the maps set that associates a representation of a
2706 /// decl DIE to a vector of offsets of DIEs having that representation.
2707 ///
2708 /// @return the maps set that associates a representation of a decl
2709 /// DIE to a vector of offsets of DIEs having that representation.
2710 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2711 decl_die_repr_die_offsets_maps()
2712 {return decl_die_repr_die_offsets_maps_;}
2713
2714 /// Getter of the maps set that associate a representation of a type
2715 /// DIE to a vector of offsets of DIEs having that representation.
2716 ///
2717 /// @return the maps set that associate a representation of a type
2718 /// DIE to a vector of offsets of DIEs having that representation.
2719 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2720 type_die_repr_die_offsets_maps() const
2721 {return type_die_repr_die_offsets_maps_;}
2722
2723 /// Getter of the maps set that associate a representation of a type
2724 /// DIE to a vector of offsets of DIEs having that representation.
2725 ///
2726 /// @return the maps set that associate a representation of a type
2727 /// DIE to a vector of offsets of DIEs having that representation.
2728 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2729 type_die_repr_die_offsets_maps()
2730 {return type_die_repr_die_offsets_maps_;}
2731
2732
2733 /// Compute the offset of the canonical DIE of a given DIE.
2734 ///
2735 /// @param die the DIE to consider.
2736 ///
2737 /// @param canonical_die_offset out parameter. This is set to the
2738 /// resulting canonical DIE that was computed.
2739 ///
2740 /// @param die_as_type if yes, it means @p die has to be considered
2741 /// as a type.
2742 void
2743 compute_canonical_die_offset(const Dwarf_Die *die,
2744 Dwarf_Off &canonical_die_offset,
2745 bool die_as_type) const
2746 {
2747 offset_offset_map_type &canonical_dies =
2748 die_as_type
2749 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2750 get_container(*this, die)
2751 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2752 get_container(*this, die);
2753
2754 Dwarf_Die canonical_die;
2755 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2756
2757 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2758 }
2759
2760 /// Compute (find) the canonical DIE of a given DIE.
2761 ///
2762 /// @param die the DIE to consider.
2763 ///
2764 /// @param canonical_dies the vector in which the canonical dies ar
2765 /// stored. The index of each element is the offset of the DIE we
2766 /// want the canonical DIE for. And the value of the element at
2767 /// that index is the canonical DIE offset we are looking for.
2768 ///
2769 /// @param canonical_die_offset out parameter. This is set to the
2770 /// resulting canonical DIE that was computed.
2771 ///
2772 /// @param die_as_type if yes, it means @p die has to be considered
2773 /// as a type.
2774 void
2775 compute_canonical_die(const Dwarf_Die *die,
2776 offset_offset_map_type& canonical_dies,
2777 Dwarf_Die &canonical_die,
2778 bool die_as_type) const
2779 {
2780 const die_source source = get_die_source(die);
2781
2782 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2783
2784 compute_canonical_die(die_offset, source,
2785 canonical_dies,
2786 canonical_die, die_as_type);
2787 }
2788
2789 /// Compute (find) the canonical DIE of a given DIE.
2790 ///
2791 /// @param die_offset the offset of the DIE to consider.
2792 ///
2793 /// @param source the source of the DIE to consider.
2794 ///
2795 /// @param canonical_dies the vector in which the canonical dies ar
2796 /// stored. The index of each element is the offset of the DIE we
2797 /// want the canonical DIE for. And the value of the element at
2798 /// that index is the canonical DIE offset we are looking for.
2799 ///
2800 /// @param canonical_die_offset out parameter. This is set to the
2801 /// resulting canonical DIE that was computed.
2802 ///
2803 /// @param die_as_type if yes, it means @p die has to be considered
2804 /// as a type.
2805 void
2806 compute_canonical_die(Dwarf_Off die_offset,
2807 die_source source,
2808 offset_offset_map_type& canonical_dies,
2809 Dwarf_Die &canonical_die,
2810 bool die_as_type) const
2811 {
2812 // The map that associates the string representation of 'die'
2813 // with a vector of offsets of potentially equivalent DIEs.
2815 die_as_type
2816 ? (const_cast<reader*>(this)->
2817 type_die_repr_die_offsets_maps().get_container(source))
2818 : (const_cast<reader*>(this)->
2819 decl_die_repr_die_offsets_maps().get_container(source));
2820
2821 Dwarf_Die die;
2822 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2823 die_offset, &die));
2824
2825 // The variable repr is the the string representation of 'die'.
2826 //
2827 // Even if die_as_type is true -- which means that 'die' is said
2828 // to be considered as a type -- we always consider a
2829 // DW_TAG_subprogram DIE as a decl here, as far as its string
2830 // representation is concerned.
2831 interned_string name =
2832 (die_as_type)
2833 ? get_die_pretty_type_representation(&die, /*where=*/0)
2834 : get_die_pretty_representation(&die, /*where=*/0);
2835
2836 Dwarf_Off canonical_die_offset = 0;
2837 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2838 if (i == map.end())
2839 {
2840 dwarf_offsets_type offsets;
2841 offsets.push_back(die_offset);
2842 map[name] = offsets;
2843 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2844 get_die_from_offset(source, die_offset, &canonical_die);
2845 return;
2846 }
2847
2848 Dwarf_Off cur_die_offset;
2849 Dwarf_Die potential_canonical_die;
2850 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2851 o != i->second.end();
2852 ++o)
2853 {
2854 cur_die_offset = *o;
2855 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2856 if (compare_dies(*this, &die, &potential_canonical_die,
2857 /*update_canonical_dies_on_the_fly=*/false))
2858 {
2859 canonical_die_offset = cur_die_offset;
2860 set_canonical_die_offset(canonical_dies, die_offset,
2861 canonical_die_offset);
2862 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2863 return;
2864 }
2865 }
2866
2867 canonical_die_offset = die_offset;
2868 i->second.push_back(die_offset);
2869 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2870 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2871 }
2872
2873 /// Getter of the canonical DIE of a given DIE.
2874 ///
2875 /// @param die the DIE to consider.
2876 ///
2877 /// @param canonical_die output parameter. Is set to the resulting
2878 /// canonical die, if this function returns true.
2879 ///
2880 /// @param where the offset of the logical DIE we are supposed to be
2881 /// calling this function from. If set to zero this means this is
2882 /// to be ignored.
2883 ///
2884 /// @param die_as_type if set to yes, it means @p die is to be
2885 /// considered as a type DIE.
2886 ///
2887 /// @return true iff a canonical DIE was found for @p die.
2888 bool
2889 get_canonical_die(const Dwarf_Die *die,
2890 Dwarf_Die &canonical_die,
2891 size_t where,
2892 bool die_as_type)
2893 {
2894 const die_source source = get_die_source(die);
2895
2896 offset_offset_map_type &canonical_dies =
2897 die_as_type
2898 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2899 get_container(source)
2900 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2901 get_container(source);
2902
2903 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2904 if (Dwarf_Off canonical_die_offset =
2905 get_canonical_die_offset(canonical_dies, die_offset))
2906 {
2907 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2908 return true;
2909 }
2910
2911 // The map that associates the string representation of 'die'
2912 // with a vector of offsets of potentially equivalent DIEs.
2914 die_as_type
2915 ? (const_cast<reader*>(this)->
2916 type_die_repr_die_offsets_maps().get_container(*this, die))
2917 : (const_cast<reader*>(this)->
2918 decl_die_repr_die_offsets_maps().get_container(*this, die));
2919
2920 // The variable repr is the the string representation of 'die'.
2921 //
2922 // Even if die_as_type is true -- which means that 'die' is said
2923 // to be considered as a type -- we always consider a
2924 // DW_TAG_subprogram DIE as a decl here, as far as its string
2925 // representation is concerned.
2926 interned_string name =
2927 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2928 ? get_die_pretty_type_representation(die, where)
2929 : get_die_pretty_representation(die, where);
2930
2931 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2932 if (i == map.end())
2933 return false;
2934
2935 Dwarf_Off cur_die_offset;
2936 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2937 o != i->second.end();
2938 ++o)
2939 {
2940 cur_die_offset = *o;
2941 get_die_from_offset(source, cur_die_offset, &canonical_die);
2942 // compare die and canonical_die.
2943 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2944 die, &canonical_die,
2945 /*update_canonical_dies_on_the_fly=*/true))
2946 {
2947 set_canonical_die_offset(canonical_dies,
2948 die_offset,
2949 cur_die_offset);
2950 return true;
2951 }
2952 }
2953
2954 return false;
2955 }
2956
2957 /// Retrieve the canonical DIE of a given DIE.
2958 ///
2959 /// The canonical DIE is a DIE that is structurally equivalent to
2960 /// this one.
2961 ///
2962 /// Note that this function caches the canonical DIE that was
2963 /// computed. Subsequent invocations of this function on the same
2964 /// DIE return the same cached DIE.
2965 ///
2966 /// @param die the DIE to get a canonical type for.
2967 ///
2968 /// @param canonical_die the resulting canonical DIE.
2969 ///
2970 /// @param where the offset of the logical DIE we are supposed to be
2971 /// calling this function from. If set to zero this means this is
2972 /// to be ignored.
2973 ///
2974 /// @param die_as_type if true, consider DIE is a type.
2975 ///
2976 /// @return true if an *existing* canonical DIE was found.
2977 /// Otherwise, @p die is considered as being a canonical DIE for
2978 /// itself. @p canonical_die is thus set to the canonical die in
2979 /// either cases.
2980 bool
2981 get_or_compute_canonical_die(const Dwarf_Die* die,
2982 Dwarf_Die& canonical_die,
2983 size_t where,
2984 bool die_as_type) const
2985 {
2986 const die_source source = get_die_source(die);
2987
2988 offset_offset_map_type &canonical_dies =
2989 die_as_type
2990 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2991 get_container(source)
2992 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2993 get_container(source);
2994
2995 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2996
2997 if (Dwarf_Off canonical_die_offset =
2998 get_canonical_die_offset(canonical_dies,
2999 initial_die_offset))
3000 {
3001 get_die_from_offset(source, canonical_die_offset, &canonical_die);
3002 return true;
3003 }
3004
3005 if (!is_type_die_to_be_canonicalized(die))
3006 return false;
3007
3008 // The map that associates the string representation of 'die'
3009 // with a vector of offsets of potentially equivalent DIEs.
3011 die_as_type
3012 ? (const_cast<reader*>(this)->
3013 type_die_repr_die_offsets_maps().get_container(*this, die))
3014 : (const_cast<reader*>(this)->
3015 decl_die_repr_die_offsets_maps().get_container(*this, die));
3016
3017 // The variable repr is the the string representation of 'die'.
3018 //
3019 // Even if die_as_type is true -- which means that 'die' is said
3020 // to be considered as a type -- we always consider a
3021 // DW_TAG_subprogram DIE as a decl here, as far as its string
3022 // representation is concerned.
3023 interned_string name =
3024 (die_as_type)
3025 ? get_die_pretty_type_representation(die, where)
3026 : get_die_pretty_representation(die, where);
3027
3028 istring_dwarf_offsets_map_type::iterator i = map.find(name);
3029 if (i == map.end())
3030 {
3031 dwarf_offsets_type offsets;
3032 offsets.push_back(initial_die_offset);
3033 map[name] = offsets;
3034 get_die_from_offset(source, initial_die_offset, &canonical_die);
3035 set_canonical_die_offset(canonical_dies,
3036 initial_die_offset,
3037 initial_die_offset);
3038 return false;
3039 }
3040
3041 // walk i->second without any iterator (using a while loop rather
3042 // than a for loop) because compare_dies might add new content to
3043 // the end of the i->second vector during the walking.
3044 dwarf_offsets_type::size_type n = 0, s = i->second.size();
3045 while (n < s)
3046 {
3047 Dwarf_Off die_offset = i->second[n];
3048 get_die_from_offset(source, die_offset, &canonical_die);
3049 // compare die and canonical_die.
3050 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
3051 die, &canonical_die,
3052 /*update_canonical_dies_on_the_fly=*/true))
3053 {
3054 set_canonical_die_offset(canonical_dies,
3055 initial_die_offset,
3056 die_offset);
3057 return true;
3058 }
3059 ++n;
3060 }
3061
3062 // We didn't find a canonical DIE for 'die'. So let's consider
3063 // that it is its own canonical DIE.
3064 get_die_from_offset(source, initial_die_offset, &canonical_die);
3065 i->second.push_back(initial_die_offset);
3066 set_canonical_die_offset(canonical_dies,
3067 initial_die_offset,
3068 initial_die_offset);
3069
3070 return false;
3071 }
3072
3073 /// Get the source of the DIE.
3074 ///
3075 /// The function returns an enumerator value saying if the DIE comes
3076 /// from the .debug_info section of the primary debug info file, the
3077 /// .debug_info section of the alternate debug info file, or the
3078 /// .debug_types section.
3079 ///
3080 /// @param die the DIE to get the source of.
3081 ///
3082 /// @return the source of the DIE if it could be determined,
3083 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
3084 die_source
3085 get_die_source(const Dwarf_Die *die) const
3086 {
3087 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
3088 ABG_ASSERT(die);
3089 ABG_ASSERT(get_die_source(*die, source));
3090 return source;
3091 }
3092
3093 /// Get the source of the DIE.
3094 ///
3095 /// The function returns an enumerator value saying if the DIE comes
3096 /// from the .debug_info section of the primary debug info file, the
3097 /// .debug_info section of the alternate debug info file, or the
3098 /// .debug_types section.
3099 ///
3100 /// @param die the DIE to get the source of.
3101 ///
3102 /// @param source out parameter. The function sets this parameter
3103 /// to the source of the DIE @p iff it returns true.
3104 ///
3105 /// @return true iff the source of the DIE could be determined and
3106 /// returned.
3107 bool
3108 get_die_source(const Dwarf_Die &die, die_source &source) const
3109 {
3110 Dwarf_Die cu_die;
3111 Dwarf_Die cu_kind;
3112 uint8_t address_size = 0, offset_size = 0;
3113 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
3114 &cu_die, &address_size,
3115 &offset_size))
3116 return false;
3117
3118 Dwarf_Half version = 0;
3119 Dwarf_Off abbrev_offset = 0;
3120 uint64_t type_signature = 0;
3121 Dwarf_Off type_offset = 0;
3122 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
3123 &version, &abbrev_offset,
3124 &address_size, &offset_size,
3125 &type_signature, &type_offset))
3126 return false;
3127
3128 int tag = dwarf_tag(&cu_kind);
3129
3130 if (tag == DW_TAG_compile_unit
3131 || tag == DW_TAG_partial_unit)
3132 {
3133 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
3134 if (dwarf_debug_info() == die_dwarf)
3135 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
3136 else if (alternate_dwarf_debug_info() == die_dwarf)
3137 source = ALT_DEBUG_INFO_DIE_SOURCE;
3138 else
3140 }
3141 else if (tag == DW_TAG_type_unit)
3142 source = TYPE_UNIT_DIE_SOURCE;
3143 else
3144 return false;
3145
3146 return true;
3147 }
3148
3149 /// Getter for the DIE designated by an offset.
3150 ///
3151 /// @param source the source of the DIE to get.
3152 ///
3153 /// @param offset the offset of the DIE to get.
3154 ///
3155 /// @param die the resulting DIE. The pointer has to point to an
3156 /// allocated memory region.
3157 void
3158 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
3159 {
3160 if (source == TYPE_UNIT_DIE_SOURCE)
3161 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3162 offset, die));
3163 else
3164 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3165 offset, die));
3166 }
3167
3168public:
3169
3170 /// Add an entry to the relevant die->decl map.
3171 ///
3172 /// @param die the DIE to add the the map.
3173 ///
3174 /// @param decl the decl to consider.
3175 ///
3176 /// @param where_offset where in the DIE stream we logically are.
3177 ///
3178 /// @param do_associate_by_repr if true then this function
3179 /// associates the representation string of @p die with the
3180 /// declaration @p decl, in a corpus-wide manner. That is, in the
3181 /// entire current corpus, there is going to be just one declaration
3182 /// associated with a DIE of the string representation of @p die.
3183 ///
3184 /// @param do_associate_by_repr_per_tu if true, then this function
3185 /// associates the representation string of @p die with the
3186 /// declaration @p decl in a translation unit wide manner. That is,
3187 /// in the entire current translation unit, there is going to be
3188 /// just one declaration associated with a DIE of the string
3189 /// representation of @p die.
3190 void
3191 associate_die_to_decl(Dwarf_Die* die,
3192 decl_base_sptr decl,
3193 size_t where_offset,
3194 bool do_associate_by_repr = false)
3195 {
3196 const die_source source = get_die_source(die);
3197
3199 decl_die_artefact_maps().get_container(source);
3200
3201 size_t die_offset;
3202 if (do_associate_by_repr)
3203 {
3204 Dwarf_Die equiv_die;
3205 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
3206 /*die_as_type=*/false))
3207 return;
3208 die_offset = dwarf_dieoffset(&equiv_die);
3209 }
3210 else
3211 die_offset = dwarf_dieoffset(die);
3212
3213 m[die_offset] = decl;
3214 }
3215
3216 /// Lookup the decl for a given DIE.
3217 ///
3218 /// The returned decl is either the decl of the DIE that as the
3219 /// exact offset @p die_offset
3220 /// die_offset, or
3221 /// give
3222 ///
3223 /// @param die_offset the offset of the DIE to consider.
3224 ///
3225 /// @param source where the DIE represented by @p die_offset comes
3226 /// from.
3227 ///
3228 /// Note that "alternate debug info sections" is a GNU extension as
3229 /// of DWARF4 and is described at
3230 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3231 ///
3232 /// @return the resulting decl, or null if no decl is associated to
3233 /// the DIE represented by @p die_offset.
3234 decl_base_sptr
3235 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3236 {
3237 decl_base_sptr result =
3238 is_decl(lookup_artifact_from_die_offset(die_offset, source,
3239 /*die_as_type=*/false));
3240
3241 return result;
3242 }
3243
3244 /// Get the qualified name of a given DIE.
3245 ///
3246 /// If the name of the DIE was already computed before just return
3247 /// that name from a cache. Otherwise, build the name, cache it and
3248 /// return it.
3249 ///
3250 /// @param die the DIE to consider.
3251 ///
3252 /// @param where_offset where in the DIE stream we logically are.
3253 ///
3254 /// @param guard the set of DIE offsets of the stack of DIEs
3255 /// involved in the construction of the qualified name of the type.
3256 /// This set is used to detect (and avoid) cycles in the stack of
3257 /// DIEs that is going to be walked to compute the qualified type
3258 /// name.
3259 ///
3260 /// @return the interned string representing the qualified name of
3261 /// @p die.
3262 interned_string
3263 get_die_qualified_name(Dwarf_Die *die, size_t where_offset,
3264 unordered_set<uint64_t>& guard) const
3265 {
3266 ABG_ASSERT(die);
3268 die_qualified_name_maps_.get_container(*this, die);
3269
3270 size_t die_offset = dwarf_dieoffset(die);
3271 die_istring_map_type::const_iterator i = map.find(die_offset);
3272
3273 if (i == map.end())
3274 {
3275 reader& rdr = *const_cast<reader*>(this);
3276 string qualified_name = die_qualified_name(rdr, die,
3277 where_offset,
3278 guard);
3279 interned_string istr = env().intern(qualified_name);
3280 map[die_offset] = istr;
3281 return istr;
3282 }
3283
3284 return i->second;
3285 }
3286
3287 /// Get the qualified name of a given DIE.
3288 ///
3289 /// If the name of the DIE was already computed before just return
3290 /// that name from a cache. Otherwise, build the name, cache it and
3291 /// return it.
3292 ///
3293 /// @param die the DIE to consider.
3294 ///
3295 /// @param where_offset where in the DIE stream we logically are.
3296 ///
3297 /// @return the interned string representing the qualified name of
3298 /// @p die.
3299 interned_string
3300 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3301 {
3302 return const_cast<reader*>(this)->
3303 get_die_qualified_name(die, where_offset);
3304 }
3305
3306 /// Get the qualified name of a given DIE which is considered to be
3307 /// the DIE for a type.
3308 ///
3309 /// For instance, for a DW_TAG_subprogram DIE, this function
3310 /// computes the name of the function *type* that corresponds to the
3311 /// function.
3312 ///
3313 /// If the name of the DIE was already computed before just return
3314 /// that name from a cache. Otherwise, build the name, cache it and
3315 /// return it.
3316 ///
3317 /// @param die the DIE to consider.
3318 ///
3319 /// @param where_offset where in the DIE stream we logically are.
3320 ///
3321 /// @param guard the set of DIE offsets of the stack of DIEs
3322 /// involved in the construction of the qualified name of the type.
3323 /// This set is used to detect (and avoid) cycles in the stack of
3324 /// DIEs that is going to be walked to compute the qualified type
3325 /// name.
3326 ///
3327 /// @return the interned string representing the qualified name of
3328 /// @p die.
3329 interned_string
3330 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset,
3331 unordered_set<uint64_t>& guard) const
3332 {
3333 ABG_ASSERT(die);
3334
3335 // The name of the translation unit die is "".
3336 if (die == cur_tu_die())
3337 return env().intern("");
3338
3340 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3341 die);
3342
3343 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3344 die_istring_map_type::const_iterator i =
3345 map.find(die_offset);
3346
3347 if (i == map.end())
3348 {
3349 reader& rdr = *const_cast<reader*>(this);
3350 string qualified_name;
3351 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3352 if ((tag == DW_TAG_structure_type
3353 || tag == DW_TAG_class_type
3354 || tag == DW_TAG_union_type)
3355 && die_is_anonymous(die))
3356 qualified_name =
3357 die_class_or_enum_flat_representation(*this, die, /*indent=*/"",
3358 /*one_line=*/true,
3359 /*qualified_name=*/false,
3360 where_offset,
3361 guard);
3362 else
3363 qualified_name = die_qualified_type_name(rdr, die,
3364 where_offset,
3365 guard);
3366
3367 interned_string istr = env().intern(qualified_name);
3368 map[die_offset] = istr;
3369 return istr;
3370 }
3371
3372 return i->second;
3373 }
3374
3375 /// Get the pretty representation of a DIE that represents a type.
3376 ///
3377 /// For instance, for the DW_TAG_subprogram, this function computes
3378 /// the pretty representation of the type of the function, not the
3379 /// pretty representation of the function declaration.
3380 ///
3381 /// Once the pretty representation is computed, it's stored in a
3382 /// cache. Subsequent invocations of this function on the same DIE
3383 /// will yield the cached name.
3384 ///
3385 /// @param die the DIE to consider.
3386 ///
3387 /// @param where_offset where in the DIE stream we logically are.
3388 ///
3389 /// @param guard the set of DIE offsets of the stack of DIEs
3390 /// involved in the construction of the pretty representation of the
3391 /// type. This set is used to detect (and avoid) cycles in the
3392 /// stack of DIEs that is going to be walked to compute the
3393 /// pretty representation.
3394 ///
3395 /// @return the interned_string that represents the pretty
3396 /// representation.
3397 interned_string
3398 get_die_pretty_type_representation(const Dwarf_Die *die,
3399 size_t where_offset,
3400 unordered_set<uint64_t>& guard) const
3401 {
3402 ABG_ASSERT(die);
3404 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3405 die);
3406
3407 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3408 die_istring_map_type::const_iterator i = map.find(die_offset);
3409
3410 if (i == map.end())
3411 {
3412 reader& rdr = *const_cast<reader*>(this);
3413 string pretty_representation =
3414 die_pretty_print_type(rdr, die, where_offset, guard);
3415 interned_string istr = env().intern(pretty_representation);
3416 map[die_offset] = istr;
3417 return istr;
3418 }
3419
3420 return i->second;
3421 }
3422
3423
3424 /// Get the pretty representation of a DIE that represents a type.
3425 ///
3426 /// For instance, for the DW_TAG_subprogram, this function computes
3427 /// the pretty representation of the type of the function, not the
3428 /// pretty representation of the function declaration.
3429 ///
3430 /// Once the pretty representation is computed, it's stored in a
3431 /// cache. Subsequent invocations of this function on the same DIE
3432 /// will yield the cached name.
3433 ///
3434 /// @param die the DIE to consider.
3435 ///
3436 /// @param where_offset where in the DIE stream we logically are.
3437 ///
3438 /// @return the interned_string that represents the pretty
3439 /// representation.
3440 interned_string
3441 get_die_pretty_type_representation(const Dwarf_Die *die,
3442 size_t where_offset) const
3443 {
3444 unordered_set<uint64_t> guard;
3445 return get_die_pretty_type_representation(die, where_offset, guard);
3446 }
3447
3448 /// Get the pretty representation of a DIE.
3449 ///
3450 /// Once the pretty representation is computed, it's stored in a
3451 /// cache. Subsequent invocations of this function on the same DIE
3452 /// will yield the cached name.
3453 ///
3454 /// @param die the DIE to consider.
3455 ///
3456 /// @param where_offset where in the DIE stream we logically are.
3457 ///
3458 /// @param guard the set of DIE offsets of the stack of DIEs
3459 /// involved in the construction of the pretty representation of the
3460 /// type. This set is used to detect (and avoid) cycles in the
3461 /// stack of DIEs that is going to be walked to compute the
3462 /// pretty representation.
3463 ///
3464 /// @return the interned_string that represents the pretty
3465 /// representation.
3466 interned_string
3467 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset,
3468 unordered_set<uint64_t>& guard) const
3469 {
3470 ABG_ASSERT(die);
3471
3473 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3474 die);
3475
3476 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3477 die_istring_map_type::const_iterator i = map.find(die_offset);
3478
3479 if (i == map.end())
3480 {
3481 reader& rdr = *const_cast<reader*>(this);
3482 string pretty_representation =
3483 die_pretty_print(rdr, die, where_offset, guard);
3484 interned_string istr = env().intern(pretty_representation);
3485 map[die_offset] = istr;
3486 return istr;
3487 }
3488
3489 return i->second;
3490 }
3491
3492 /// Get the pretty representation of a DIE.
3493 ///
3494 /// Once the pretty representation is computed, it's stored in a
3495 /// cache. Subsequent invocations of this function on the same DIE
3496 /// will yield the cached name.
3497 ///
3498 /// @param die the DIE to consider.
3499 ///
3500 /// @param where_offset where in the DIE stream we logically are.
3501 ///
3502 /// @return the interned_string that represents the pretty
3503 /// representation.
3504 interned_string
3505 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3506 {
3507 unordered_set<uint64_t> guard;
3508 return get_die_pretty_representation(die, where_offset, guard);
3509 }
3510
3511 /// Lookup the artifact that was built to represent a type that has
3512 /// the same pretty representation as the type denoted by a given
3513 /// DIE.
3514 ///
3515 /// Note that the DIE must have previously been associated with the
3516 /// artifact using the functions associate_die_to_decl or
3517 /// associate_die_to_type.
3518 ///
3519 /// Also, note that the scope of the lookup is the current ABI
3520 /// corpus.
3521 ///
3522 /// @param die the DIE to consider.
3523 ///
3524 /// @param where_offset where in the DIE stream we logically are.
3525 ///
3526 /// @return the type artifact found.
3527 type_or_decl_base_sptr
3528 lookup_type_artifact_from_die(Dwarf_Die *die) const
3529 {
3530 type_or_decl_base_sptr artifact =
3531 lookup_artifact_from_die(die, /*type_as_die=*/true);
3532 if (function_decl_sptr fn = is_function_decl(artifact))
3533 return fn->get_type();
3534 return artifact;
3535 }
3536
3537 /// Lookup the artifact that was built to represent a type or a
3538 /// declaration that has the same pretty representation as the type
3539 /// denoted by a given DIE.
3540 ///
3541 /// Note that the DIE must have previously been associated with the
3542 /// artifact using the functions associate_die_to_decl or
3543 /// associate_die_to_type.
3544 ///
3545 /// Also, note that the scope of the lookup is the current ABI
3546 /// corpus.
3547 ///
3548 /// @param die the DIE to consider.
3549 ///
3550 /// @param where_offset where in the DIE stream we logically are.
3551 ///
3552 /// @param die_as_type if true, it means the DIE is to be considered
3553 /// as a type.
3554 ///
3555 /// @return the artifact found.
3556 type_or_decl_base_sptr
3557 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3558 {
3559 Dwarf_Die equiv_die;
3560 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3561 return type_or_decl_base_sptr();
3562
3563 const die_artefact_map_type& m =
3564 die_as_type
3565 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3566 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3567
3568 size_t die_offset = dwarf_dieoffset(&equiv_die);
3569 die_artefact_map_type::const_iterator i = m.find(die_offset);
3570
3571 if (i == m.end())
3572 return type_or_decl_base_sptr();
3573 return i->second;
3574 }
3575
3576 /// Lookup the artifact that was built to represent a type or a
3577 /// declaration that has the same pretty representation as the type
3578 /// denoted by the offset of a given DIE.
3579 ///
3580 /// Note that the DIE must have previously been associated with the
3581 /// artifact using either associate_die_to_decl or
3582 /// associate_die_to_type.
3583 ///
3584 /// Also, note that the scope of the lookup is the current ABI
3585 /// corpus.
3586 ///
3587 /// @param die the DIE to consider.
3588 ///
3589 /// @param where_offset where in the DIE stream we logically are.
3590 ///
3591 /// @param die_as_type if true, it means the DIE is to be considered
3592 /// as a type.
3593 ///
3594 /// @return the artifact found.
3595 type_or_decl_base_sptr
3596 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3597 die_source source,
3598 bool die_as_type = false) const
3599 {
3600 const die_artefact_map_type& m =
3601 die_as_type
3602 ? type_die_artefact_maps().get_container(source)
3603 : decl_die_artefact_maps().get_container(source);
3604
3605 die_artefact_map_type::const_iterator i = m.find(die_offset);
3606 if (i == m.end())
3607 return type_or_decl_base_sptr();
3608 return i->second;
3609 }
3610
3611 /// Check if we can assume the One Definition Rule[1] to be relevant
3612 /// for the current translation unit.
3613 ///
3614 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3615 ///
3616 /// At the moment this returns true if the current translation unit
3617 /// is in C++ language. In that case, it's relevant to assume that
3618 /// we use optimizations based on the ODR.
3619 bool
3620 odr_is_relevant() const
3621 {return odr_is_relevant(cur_transl_unit()->get_language());}
3622
3623 /// Check if we can assume the One Definition Rule[1] to be relevant
3624 /// for a given language.
3625 ///
3626 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3627 ///
3628 /// At the moment this returns true if the language considered
3629 /// is C++, Java or Ada.
3630 bool
3632 {
3633 return (is_cplus_plus_language(l)
3634 || is_java_language(l)
3635 || is_ada_language(l));
3636 }
3637
3638 /// Check if we can assume the One Definition Rule to be relevant
3639 /// for a given DIE.
3640 ///
3641 /// @param die the DIE to consider.
3642 ///
3643 /// @return true if the ODR is relevant for @p die.
3644 bool
3645 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3646 {
3647 Dwarf_Die die;
3648 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3649 die_offset, &die));
3650 return odr_is_relevant(&die);
3651 }
3652
3653 /// Check if we can assume the One Definition Rule to be relevant
3654 /// for a given DIE.
3655 ///
3656 /// @param die the DIE to consider.
3657 ///
3658 /// @return true if the ODR is relevant for @p die.
3659 bool
3660 odr_is_relevant(const Dwarf_Die *die) const
3661 {
3663 if (!get_die_language(die, lang))
3664 return odr_is_relevant();
3665
3666 return odr_is_relevant(lang);
3667 }
3668
3669 /// Getter for the maps set that associates a decl DIE offset to an
3670 /// artifact.
3671 ///
3672 /// @return the maps set that associates a decl DIE offset to an
3673 /// artifact.
3674 die_source_dependant_container_set<die_artefact_map_type>&
3675 decl_die_artefact_maps()
3676 {return decl_die_artefact_maps_;}
3677
3678 /// Getter for the maps set that associates a decl DIE offset to an
3679 /// artifact.
3680 ///
3681 /// @return the maps set that associates a decl DIE offset to an
3682 /// artifact.
3683 const die_source_dependant_container_set<die_artefact_map_type>&
3684 decl_die_artefact_maps() const
3685 {return decl_die_artefact_maps_;}
3686
3687 /// Getter for the maps set that associates a type DIE offset to an
3688 /// artifact.
3689 ///
3690 /// @return the maps set that associates a type DIE offset to an
3691 /// artifact.
3692 die_source_dependant_container_set<die_artefact_map_type>&
3693 type_die_artefact_maps()
3694 {return type_die_artefact_maps_;}
3695
3696 /// Getter for the maps set that associates a type DIE offset to an
3697 /// artifact.
3698 ///
3699 /// @return the maps set that associates a type DIE offset to an
3700 /// artifact.
3701 const die_source_dependant_container_set<die_artefact_map_type>&
3702 type_die_artefact_maps() const
3703 {return type_die_artefact_maps_;}
3704
3705 /// Getter of the maps that associates function type representations
3706 /// to function types, inside a translation unit.
3707 ///
3708 /// @return the maps that associates function type representations
3709 /// to function types, inside a translation unit.
3711 per_tu_repr_to_fn_type_maps()
3712 {return per_tu_repr_to_fn_type_maps_;}
3713
3714 /// Getter of the maps that associates function type representations
3715 /// to function types, inside a translation unit.
3716 ///
3717 /// @return the maps that associates function type representations
3718 /// to function types, inside a translation unit.
3720 per_tu_repr_to_fn_type_maps() const
3721 {return per_tu_repr_to_fn_type_maps_;}
3722
3723 /// Associate the representation of a function type DIE to a given
3724 /// function type, inside the current translation unit.
3725 ///
3726 /// @param die the DIE to associate to the function type, using its
3727 /// representation.
3728 ///
3729 /// @param fn_type the function type to associate to @p die.
3730 void
3731 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3732 const function_type_sptr &fn_type)
3733 {
3734 if (!die_is_function_type(die))
3735 return;
3736
3737 interned_string repr =
3738 get_die_pretty_type_representation(die, /*where=*/0);
3739 ABG_ASSERT(!repr.empty());
3740
3741 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3742 }
3743
3744 /// Lookup the function type associated to a given function type
3745 /// DIE, in the current translation unit.
3746 ///
3747 /// @param die the DIE of function type to consider.
3748 ///
3749 /// @return the @ref function_type_sptr associated to @p die, or nil
3750 /// of no function_type is associated to @p die.
3752 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3753 {
3754 if (!die_is_function_type(die))
3755 return function_type_sptr();
3756
3757 interned_string repr = die_name(die).empty() ?
3758 get_die_pretty_type_representation(die, /*where=*/0)
3759 : get_die_pretty_representation(die, /*where=*/0);
3760 ABG_ASSERT(!repr.empty());
3761
3762 istring_fn_type_map_type::const_iterator i =
3763 per_tu_repr_to_fn_type_maps().find(repr);
3764
3765 if (i == per_tu_repr_to_fn_type_maps().end())
3766 return function_type_sptr();
3767
3768 return i->second;
3769 }
3770
3771 /// Set the canonical DIE offset of a given DIE.
3772 ///
3773 /// @param canonical_dies the vector that holds canonical DIEs.
3774 ///
3775 /// @param die_offset the offset of the DIE to set the canonical DIE
3776 /// for.
3777 ///
3778 /// @param canonical_die_offset the canonical DIE offset to
3779 /// associate to @p die_offset.
3780 void
3781 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3782 Dwarf_Off die_offset,
3783 Dwarf_Off canonical_die_offset) const
3784 {
3785 canonical_dies[die_offset] = canonical_die_offset;}
3786
3787 /// Set the canonical DIE offset of a given DIE.
3788 ///
3789 ///
3790 /// @param die_offset the offset of the DIE to set the canonical DIE
3791 /// for.
3792 ///
3793 /// @param source the source of the DIE denoted by @p die_offset.
3794 ///
3795 /// @param canonical_die_offset the canonical DIE offset to
3796 /// associate to @p die_offset.
3797 ///
3798 /// @param die_as_type if true, it means that @p die_offset has to
3799 /// be considered as a type.
3800 void
3801 set_canonical_die_offset(Dwarf_Off die_offset,
3802 die_source source,
3803 Dwarf_Off canonical_die_offset,
3804 bool die_as_type) const
3805 {
3806 offset_offset_map_type &canonical_dies =
3807 die_as_type
3808 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3809 get_container(source)
3810 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3811 get_container(source);
3812
3813 set_canonical_die_offset(canonical_dies,
3814 die_offset,
3815 canonical_die_offset);
3816 }
3817
3818 /// Set the canonical DIE offset of a given DIE.
3819 ///
3820 ///
3821 /// @param die the DIE to set the canonical DIE for.
3822 ///
3823 /// @param canonical_die_offset the canonical DIE offset to
3824 /// associate to @p die_offset.
3825 ///
3826 /// @param die_as_type if true, it means that @p die has to be
3827 /// considered as a type.
3828 void
3829 set_canonical_die_offset(const Dwarf_Die *die,
3830 Dwarf_Off canonical_die_offset,
3831 bool die_as_type) const
3832 {
3833 const die_source source = get_die_source(die);
3834
3835 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3836
3837 set_canonical_die_offset(die_offset, source,
3838 canonical_die_offset,
3839 die_as_type);
3840 }
3841
3842 /// Get the canonical DIE offset of a given DIE.
3843 ///
3844 /// @param canonical_dies the vector that contains canonical DIES.
3845 ///
3846 /// @param die_offset the offset of the DIE to consider.
3847 ///
3848 /// @return the canonical of the DIE denoted by @p die_offset, or
3849 /// zero if no canonical DIE was found.
3850 Dwarf_Off
3851 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3852 Dwarf_Off die_offset) const
3853 {
3854 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3855 if (it == canonical_dies.end())
3856 return 0;
3857 return it->second;
3858 }
3859
3860 /// Get the canonical DIE offset of a given DIE.
3861 ///
3862 /// @param die_offset the offset of the DIE to consider.
3863 ///
3864 /// @param source the source of the DIE denoted by @p die_offset.
3865 ///
3866 /// @param die_as_type if true, it means that @p is to be considered
3867 /// as a type DIE.
3868 ///
3869 /// @return the canonical of the DIE denoted by @p die_offset, or
3870 /// zero if no canonical DIE was found.
3871 Dwarf_Off
3872 get_canonical_die_offset(Dwarf_Off die_offset,
3873 die_source source,
3874 bool die_as_type) const
3875 {
3876 offset_offset_map_type &canonical_dies =
3877 die_as_type
3878 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3879 get_container(source)
3880 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3881 get_container(source);
3882
3883 return get_canonical_die_offset(canonical_dies, die_offset);
3884 }
3885
3886 /// Erase the canonical type of a given DIE.
3887 ///
3888 /// @param die_offset the offset of the DIE to consider.
3889 ///
3890 /// @param source the source of the canonical type.
3891 ///
3892 /// @param die_as_type if true, it means that @p is to be considered
3893 /// as a type DIE.
3894 ///
3895 /// @return the canonical of the DIE denoted by @p die_offset, or
3896 /// zero if no canonical DIE was found and erased..
3897 bool
3898 erase_canonical_die_offset(Dwarf_Off die_offset,
3899 die_source source,
3900 bool die_as_type) const
3901 {
3902 offset_offset_map_type &canonical_dies =
3903 die_as_type
3904 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3905 get_container(source)
3906 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3907 get_container(source);
3908
3909 return canonical_dies.erase(die_offset);
3910 }
3911
3912
3913 /// Associate a DIE (representing a type) to the type that it
3914 /// represents.
3915 ///
3916 /// @param die the DIE to consider.
3917 ///
3918 /// @param type the type to associate the DIE to.
3919 ///
3920 /// @param where_offset where in the DIE stream we logically are.
3921 void
3922 associate_die_to_type(const Dwarf_Die *die,
3923 type_base_sptr type,
3924 size_t where)
3925 {
3926 if (!type)
3927 return;
3928
3929 Dwarf_Die equiv_die;
3930 if (!get_or_compute_canonical_die(die, equiv_die, where,
3931 /*die_as_type=*/true))
3932 return;
3933
3935 type_die_artefact_maps().get_container(*this, &equiv_die);
3936
3937 size_t die_offset = dwarf_dieoffset(&equiv_die);
3938 m[die_offset] = type;
3939 }
3940
3941 /// Lookup the type associated to a given DIE.
3942 ///
3943 /// Note that the DIE must have been associated to type by a
3944 /// previous invocation of the function
3945 /// reader::associate_die_to_type().
3946 ///
3947 /// @param die the DIE to consider.
3948 ///
3949 /// @return the type associated to the DIE or NULL if no type is
3950 /// associated to the DIE.
3951 type_base_sptr
3952 lookup_type_from_die(const Dwarf_Die* die) const
3953 {
3954 type_or_decl_base_sptr artifact =
3955 lookup_artifact_from_die(die, /*die_as_type=*/true);
3956 if (function_decl_sptr fn = is_function_decl(artifact))
3957 return fn->get_type();
3958 return is_type(artifact);
3959 }
3960
3961 /// Lookup the type associated to a DIE at a given offset, from a
3962 /// given source.
3963 ///
3964 /// Note that the DIE must have been associated to type by a
3965 /// previous invocation of the function
3966 /// reader::associate_die_to_type().
3967 ///
3968 /// @param die_offset the offset of the DIE to consider.
3969 ///
3970 /// @param source the source of the DIE to consider.
3971 ///
3972 /// @return the type associated to the DIE or NULL if no type is
3973 /// associated to the DIE.
3974 type_base_sptr
3975 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3976 {
3977 type_base_sptr result;
3978 const die_artefact_map_type& m =
3979 type_die_artefact_maps().get_container(source);
3980 die_artefact_map_type::const_iterator i = m.find(die_offset);
3981 if (i != m.end())
3982 {
3983 if (function_decl_sptr fn = is_function_decl(i->second))
3984 return fn->get_type();
3985 result = is_type(i->second);
3986 }
3987
3988 if (!result)
3989 {
3990 // Maybe we are looking for a class type being constructed?
3991 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3992 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3993
3994 if (i != m.end())
3995 result = i->second;
3996 }
3997
3998 if (!result)
3999 {
4000 // Maybe we are looking for a function type being constructed?
4002 die_wip_function_types_map(source);
4003 die_function_type_map_type::const_iterator i = m.find(die_offset);
4004
4005 if (i != m.end())
4006 result = i->second;
4007 }
4008
4009 return result;
4010 }
4011
4012 /// Getter of a map that associates a die that represents a
4013 /// class/struct with the declaration of the class, while the class
4014 /// is being constructed.
4015 ///
4016 /// @param source where the DIE is from.
4017 ///
4018 /// @return the map that associates a DIE to the class that is being
4019 /// built.
4021 die_wip_classes_map(die_source source) const
4022 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
4023
4024 /// Getter of a map that associates a die that represents a
4025 /// class/struct with the declaration of the class, while the class
4026 /// is being constructed.
4027 ///
4028 /// @param source where the DIE comes from.
4029 ///
4030 /// @return the map that associates a DIE to the class that is being
4031 /// built.
4033 die_wip_classes_map(die_source source)
4034 {
4035 switch (source)
4036 {
4037 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4038 break;
4039 case ALT_DEBUG_INFO_DIE_SOURCE:
4040 return alternate_die_wip_classes_map_;
4041 case TYPE_UNIT_DIE_SOURCE:
4042 return type_unit_die_wip_classes_map_;
4043 case NO_DEBUG_INFO_DIE_SOURCE:
4044 case NUMBER_OF_DIE_SOURCES:
4046 }
4047 return die_wip_classes_map_;
4048 }
4049
4050 /// Getter for a map that associates a die (that represents a
4051 /// function type) whith a function type, while the function type is
4052 /// being constructed (WIP == work in progress).
4053 ///
4054 /// @param source where the DIE comes from.n
4055 ///
4056 /// @return the map of wip function types.
4058 die_wip_function_types_map(die_source source) const
4059 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
4060
4061 /// Getter for a map that associates a die (that represents a
4062 /// function type) whith a function type, while the function type is
4063 /// being constructed (WIP == work in progress).
4064 ///
4065 /// @param source where DIEs of the map come from.
4066 ///
4067 /// @return the map of wip function types.
4069 die_wip_function_types_map(die_source source)
4070 {
4071 switch (source)
4072 {
4073 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4074 break;
4075 case ALT_DEBUG_INFO_DIE_SOURCE:
4076 return alternate_die_wip_function_types_map_;
4077 case TYPE_UNIT_DIE_SOURCE:
4078 return type_unit_die_wip_function_types_map_;
4079 case NO_DEBUG_INFO_DIE_SOURCE:
4080 case NUMBER_OF_DIE_SOURCES:
4082 }
4083 return die_wip_function_types_map_;
4084 }
4085
4086 /// Getter for a map that associates a die with a function decl
4087 /// which has a linkage name but no elf symbol yet.
4088 ///
4089 /// This is to fixup function decls with linkage names, but with no
4090 /// link to their underlying elf symbol. There are some DIEs like
4091 /// that in DWARF sometimes, especially when the compiler optimizes
4092 /// stuff aggressively.
4094 die_function_decl_with_no_symbol_map()
4095 {return die_function_with_no_symbol_map_;}
4096
4097 /// Return true iff a given offset is for the DIE of a class that is
4098 /// being built, but that is not fully built yet. WIP == "work in
4099 /// progress".
4100 ///
4101 /// @param offset the DIE offset to consider.
4102 ///
4103 /// @param source where the DIE of the map come from.
4104 ///
4105 /// @return true iff @p offset is the offset of the DIE of a class
4106 /// that is being currently built.
4107 bool
4108 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
4109 {
4110 die_class_or_union_map_type::const_iterator i =
4111 die_wip_classes_map(source).find(offset);
4112 return (i != die_wip_classes_map(source).end());
4113 }
4114
4115 /// Return true iff a given offset is for the DIE of a function type
4116 /// that is being built at the moment, but is not fully built yet.
4117 /// WIP == work in progress.
4118 ///
4119 /// @param offset DIE offset to consider.
4120 ///
4121 /// @param source where the DIE comes from.
4122 ///
4123 /// @return true iff @p offset is the offset of the DIE of a
4124 /// function type that is being currently built.
4125 bool
4126 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
4127 {
4128 die_function_type_map_type::const_iterator i =
4129 die_wip_function_types_map(source).find(offset);
4130 return (i != die_wip_function_types_map(source).end());
4131 }
4132
4133 /// Sometimes, a data member die can erroneously have an empty name as
4134 /// a result of a bug of the DWARF emitter.
4135 ///
4136 /// This is what happens in
4137 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
4138 ///
4139 /// In that case, this function constructs an artificial name for that
4140 /// data member. The pattern of the name is as follows:
4141 ///
4142 /// "unnamed-@-<location>".
4143 ///
4144 ///location is either the value of the data member location of the
4145 ///data member if it has one or concatenation of its source location
4146 ///if it has none. If no location can be calculated then the function
4147 ///returns the empty string.
4148 string
4149 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
4150 {
4151 string result;
4152 // Let's make sure we are looking at a data member with an empty
4153 // name ...
4154 if (!die
4155 || dwarf_tag(die) != DW_TAG_member
4156 || !die_name(die).empty())
4157 return result;
4158
4159 // ... and yet, it's not an anonymous data member (aka unnamed
4160 // field) as described in
4161 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
4162 if (die_is_anonymous_data_member(die))
4163 return result;
4164
4165 // If we come this far, it means we are looking at a buggy data
4166 // member with no name. Let's build a name for it so that it can be
4167 // addressed.
4168 int64_t offset_in_bits = 0;
4169 bool has_offset = die_member_offset(*this, die, offset_in_bits);
4170 location loc;
4171 if (!has_offset)
4172 {
4173 loc = die_location(*this, die);
4174 if (!loc)
4175 return result;
4176 }
4177
4178 std::ostringstream o;
4179 o << "unnamed-dm-@-";
4180 if (has_offset)
4181 o << "offset-" << offset_in_bits << "bits";
4182 else
4183 o << "loc-" << loc.expand();
4184
4185 return o.str();
4186 }
4187
4188 /// Getter for the map of declaration-only classes that are to be
4189 /// resolved to their definition classes by the end of the corpus
4190 /// loading.
4191 ///
4192 /// @return a map of string -> vector of classes where the key is
4193 /// the fully qualified name of the class and the value is the
4194 /// vector of declaration-only class.
4196 declaration_only_classes() const
4197 {return decl_only_classes_map_;}
4198
4199 /// Getter for the map of declaration-only classes that are to be
4200 /// resolved to their definition classes by the end of the corpus
4201 /// loading.
4202 ///
4203 /// @return a map of string -> vector of classes where the key is
4204 /// the fully qualified name of the class and the value is the
4205 /// vector of declaration-only class.
4207 declaration_only_classes()
4208 {return decl_only_classes_map_;}
4209
4210 /// If a given artifact is a class, union or enum that is
4211 /// declaration-only, then stash it on the side so that at the end
4212 /// of the construction of the IR for the ABI corpus, we can resolve
4213 /// that declaration to its definition.
4214 ///
4215 /// @parameter t the ABI artifact to consider.
4216 void
4217 maybe_schedule_decl_only_type_for_resolution(const type_or_decl_base_sptr& t)
4218 {
4219 if (class_or_union_sptr cou = is_class_or_union_type(t))
4220 maybe_schedule_declaration_only_class_for_resolution(cou);
4221 else if (enum_type_decl_sptr e = is_enum_type(t))
4222 maybe_schedule_declaration_only_enum_for_resolution(e);
4223 }
4224
4225 /// If a given class is a declaration-only class then stash it on
4226 /// the side so that at the end of the corpus reading we can resolve
4227 /// it to its definition.
4228 ///
4229 /// @param klass the class to consider.
4230 void
4231 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
4232 {
4233 if (cou->get_is_declaration_only()
4234 && cou->get_definition_of_declaration() == 0
4235 // Make sure the class is not anonymous. Anonymous classes
4236 // are usually later named by a typedef. At that time, after
4237 // being named by a typedef, this method is going to be called
4238 // with the class being named by the typedef.
4239 && !cou->get_qualified_name().empty())
4240 {
4241 string qn = cou->get_qualified_name();
4242 string_classes_or_unions_map::iterator record =
4243 declaration_only_classes().find(qn);
4244 if (record == declaration_only_classes().end())
4245 declaration_only_classes()[qn].push_back(cou);
4246 else
4247 record->second.push_back(cou);
4248 }
4249 }
4250
4251 /// Test if a given declaration-only class has been scheduled for
4252 /// resolution to a defined class.
4253 ///
4254 /// @param klass the class to consider for the test.
4255 ///
4256 /// @return true iff @p klass is a declaration-only class and if
4257 /// it's been scheduled for resolution to a defined class.
4258 bool
4259 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4260 {
4261 if (cou->get_is_declaration_only())
4262 return ((declaration_only_classes().find(cou->get_qualified_name())
4263 != declaration_only_classes().end())
4264 || (declaration_only_classes().find(cou->get_name())
4265 != declaration_only_classes().end()));
4266
4267 return false;
4268 }
4269
4270 /// Compare two ABI artifacts in a context which canonicalization
4271 /// has not be done yet.
4272 ///
4273 /// Please note that this should only be called on IR nodes that
4274 /// belong to the same binary.
4275 ///
4276 /// @param l the left-hand-side operand of the comparison
4277 ///
4278 /// @param r the right-hand-side operand of the comparison.
4279 ///
4280 /// @return true if @p l equals @p r.
4281 bool
4282 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4283 const type_or_decl_base_sptr &r)
4284 {
4285 if (!l || !r)
4286 return !!l == !!r;
4287
4288 const environment& e = l->get_environment();
4289 ABG_ASSERT(!e.canonicalization_is_done());
4290
4291 if (is_decl(l) && is_decl(r)
4292 && l->kind() == r->kind()
4293 && ((l->get_corpus() && r->get_corpus()
4294 && (l->get_corpus() == r->get_corpus()))
4295 ||(l->get_translation_unit()
4296 && r->get_translation_unit()
4297 && l->get_translation_unit() == r->get_translation_unit())))
4298 {
4299 // Fast path optimization. If the two types are declared at
4300 // the same location (in the same binary) then it very likely
4301 // means the two types are equal.
4302 //
4303 // We really need every bit of optimization here because
4304 // otherwise, comparing types before canonicalization can take
4305 // forever.*
4306 decl_base *ld = is_decl(l.get());
4307 decl_base *rd = is_decl(r.get());
4308 ABG_ASSERT(ld && rd);
4309 if (ld->get_qualified_name() != rd->get_qualified_name())
4310 return false;
4311
4312 location ll = ld->get_location(), rl = rd->get_location();
4313 if (ll && rl)
4314 {
4315 string l1 = ll.expand();
4316 string l2 = rl.expand();
4317 if (l1 == l2)
4318 return true;
4319 }
4320 }
4321
4322 e.priv_->allow_type_comparison_results_caching(true);
4323 bool s0 = e.decl_only_class_equals_definition();
4324 e.decl_only_class_equals_definition(true);
4325 bool equal = l == r;
4326 e.decl_only_class_equals_definition(s0);
4327 e.priv_->clear_type_comparison_results_cache();
4328 e.priv_->allow_type_comparison_results_caching(false);
4329 return equal;
4330 }
4331
4332 /// Walk the declaration-only classes that have been found during
4333 /// the building of the corpus and resolve them to their definitions.
4334 void
4335 resolve_declaration_only_classes()
4336 {
4337 vector<string> resolved_classes;
4338
4339 for (string_classes_or_unions_map::iterator i =
4340 declaration_only_classes().begin();
4341 i != declaration_only_classes().end();
4342 ++i)
4343 {
4344 bool to_resolve = false;
4345 for (classes_or_unions_type::iterator j = i->second.begin();
4346 j != i->second.end();
4347 ++j)
4348 if ((*j)->get_is_declaration_only()
4349 && ((*j)->get_definition_of_declaration() == 0))
4350 to_resolve = true;
4351
4352 if (!to_resolve)
4353 {
4354 resolved_classes.push_back(i->first);
4355 continue;
4356 }
4357
4358 // Now, for each decl-only class that have the current name
4359 // 'i->first', let's try to poke at the fully defined class
4360 // that is defined in the same translation unit as the
4361 // declaration.
4362 //
4363 // If we find one class (defined in the TU of the declaration)
4364 // that defines the declaration, then the declaration can be
4365 // resolved to that class.
4366 //
4367 // If no defining class is found in the TU of the declaration,
4368 // then there are possibly three cases to consider:
4369 //
4370 // 1/ There is exactly one class that defines the
4371 // declaration and that class is defined in another TU. In
4372 // this case, the declaration is resolved to that
4373 // definition.
4374 //
4375 // 2/ There are more than one class that define that
4376 // declaration and none of them is defined in the TU of the
4377 // declaration. If those classes are all different, then
4378 // the declaration is left unresolved.
4379 //
4380 // 3/ No class defines the declaration. In this case, the
4381 // declaration is left unresoved.
4382
4383 // So get the classes that might define the current
4384 // declarations which name is i->first.
4385 const type_base_wptrs_type *classes =
4386 lookup_class_types(i->first, *corpus());
4387 if (!classes)
4388 classes = lookup_union_types(i->first, *corpus());
4389
4390 if (!classes)
4391 continue;
4392
4393 // This is a map that associates the translation unit path to
4394 // the class (that potentially defines the declarations that
4395 // we consider) that are defined in that translation unit. It
4396 // should stay ordered by using the TU path as key to ensure
4397 // stability of the order of classe definitions in ABIXML
4398 // output.
4399 map<string, class_or_union_sptr> per_tu_class_map;
4400 for (type_base_wptrs_type::const_iterator c = classes->begin();
4401 c != classes->end();
4402 ++c)
4403 {
4404 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4405 ABG_ASSERT(klass);
4406
4408 if (klass->get_is_declaration_only())
4409 continue;
4410
4411 string tu_path = klass->get_translation_unit()->get_absolute_path();
4412 if (tu_path.empty())
4413 continue;
4414
4415 // Build a map that associates the translation unit path
4416 // to the class (that potentially defines the declarations
4417 // that we consider) that are defined in that translation unit.
4418 per_tu_class_map[tu_path] = klass;
4419 }
4420
4421 if (!per_tu_class_map.empty())
4422 {
4423 // Walk the declarations to resolve and resolve them
4424 // either to the definitions that are in the same TU as
4425 // the declaration, or to the definition found elsewhere,
4426 // if there is only one such definition.
4427 for (classes_or_unions_type::iterator j = i->second.begin();
4428 j != i->second.end();
4429 ++j)
4430 {
4431 if ((*j)->get_is_declaration_only()
4432 && ((*j)->get_definition_of_declaration() == 0))
4433 {
4434 string tu_path =
4435 (*j)->get_translation_unit()->get_absolute_path();
4436 map<string, class_or_union_sptr>::const_iterator e =
4437 per_tu_class_map.find(tu_path);
4438 if (e != per_tu_class_map.end())
4439 (*j)->set_definition_of_declaration(e->second);
4440 else if (per_tu_class_map.size() == 1)
4441 (*j)->set_definition_of_declaration
4442 (per_tu_class_map.begin()->second);
4443 else
4444 {
4445 // We are in case where there are more than
4446 // one definition for the declaration. Let's
4447 // see if they are all equal. If they are,
4448 // then the declaration resolves to the
4449 // definition. Otherwise, we are in the case
4450 // 3/ described above.
4451 map<string,
4452 class_or_union_sptr>::const_iterator it;
4453 class_or_union_sptr first_class =
4454 per_tu_class_map.begin()->second;
4455 bool all_class_definitions_are_equal = true;
4456 for (it = per_tu_class_map.begin();
4457 it != per_tu_class_map.end();
4458 ++it)
4459 {
4460 if (it == per_tu_class_map.begin())
4461 continue;
4462 else
4463 {
4464 if (!compare_before_canonicalisation(it->second,
4465 first_class))
4466 {
4467 all_class_definitions_are_equal = false;
4468 break;
4469 }
4470 }
4471 }
4472 if (all_class_definitions_are_equal)
4473 (*j)->set_definition_of_declaration(first_class);
4474 }
4475 }
4476 }
4477 resolved_classes.push_back(i->first);
4478 }
4479 }
4480
4481 size_t num_decl_only_classes = declaration_only_classes().size(),
4482 num_resolved = resolved_classes.size();
4483 if (show_stats())
4484 cerr << "resolved " << num_resolved
4485 << " class declarations out of "
4486 << num_decl_only_classes
4487 << "\n";
4488
4489 for (vector<string>::const_iterator i = resolved_classes.begin();
4490 i != resolved_classes.end();
4491 ++i)
4492 declaration_only_classes().erase(*i);
4493
4494 if (show_stats() && !declaration_only_classes().empty())
4495 {
4496 cerr << "Here are the "
4497 << num_decl_only_classes - num_resolved
4498 << " unresolved class declarations:\n";
4499 for (string_classes_or_unions_map::iterator i =
4500 declaration_only_classes().begin();
4501 i != declaration_only_classes().end();
4502 ++i)
4503 cerr << " " << i->first << "\n";
4504 }
4505 }
4506
4507 /// Getter for the map of declaration-only enums that are to be
4508 /// resolved to their definition enums by the end of the corpus
4509 /// loading.
4510 ///
4511 /// @return a map of string -> vector of enums where the key is
4512 /// the fully qualified name of the enum and the value is the
4513 /// vector of declaration-only enum.
4514 const string_enums_map&
4515 declaration_only_enums() const
4516 {return decl_only_enums_map_;}
4517
4518 /// Getter for the map of declaration-only enums that are to be
4519 /// resolved to their definition enums by the end of the corpus
4520 /// loading.
4521 ///
4522 /// @return a map of string -> vector of enums where the key is
4523 /// the fully qualified name of the enum and the value is the
4524 /// vector of declaration-only enum.
4526 declaration_only_enums()
4527 {return decl_only_enums_map_;}
4528
4529 /// If a given enum is a declaration-only enum then stash it on
4530 /// the side so that at the end of the corpus reading we can resolve
4531 /// it to its definition.
4532 ///
4533 /// @param enom the enum to consider.
4534 void
4535 maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4536 {
4537 if (enom->get_is_declaration_only()
4538 && enom->get_definition_of_declaration() == 0
4539 // Make sure the enum is not anonymous. Anonymous enums are
4540 // usually later named by a typedef. At that time, after
4541 // being named by a typedef, this method is going to be called
4542 // with the enum being named by the typedef.
4543 && !enom->get_qualified_name().empty())
4544 {
4545 string qn = enom->get_qualified_name();
4546 string_enums_map::iterator record =
4547 declaration_only_enums().find(qn);
4548 if (record == declaration_only_enums().end())
4549 declaration_only_enums()[qn].push_back(enom);
4550 else
4551 record->second.push_back(enom);
4552 }
4553 }
4554
4555 /// Test if a given declaration-only enum has been scheduled for
4556 /// resolution to a defined enum.
4557 ///
4558 /// @param enom the enum to consider for the test.
4559 ///
4560 /// @return true iff @p enom is a declaration-only enum and if
4561 /// it's been scheduled for resolution to a defined enum.
4562 bool
4563 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4564 {
4565 if (enom->get_is_declaration_only())
4566 return (declaration_only_enums().find(enom->get_qualified_name())
4567 != declaration_only_enums().end());
4568
4569 return false;
4570 }
4571
4572 /// Walk the declaration-only enums that have been found during
4573 /// the building of the corpus and resolve them to their definitions.
4574 ///
4575 /// TODO: Do away with this function by factorizing it with
4576 /// resolve_declaration_only_classes. All declaration-only decls
4577 /// could be handled the same way as declaration-only-ness is a
4578 /// property of abigail::ir::decl_base now.
4579 void
4580 resolve_declaration_only_enums()
4581 {
4582 vector<string> resolved_enums;
4583
4584 for (string_enums_map::iterator i =
4585 declaration_only_enums().begin();
4586 i != declaration_only_enums().end();
4587 ++i)
4588 {
4589 bool to_resolve = false;
4590 for (enums_type::iterator j = i->second.begin();
4591 j != i->second.end();
4592 ++j)
4593 if ((*j)->get_is_declaration_only()
4594 && ((*j)->get_definition_of_declaration() == 0))
4595 to_resolve = true;
4596
4597 if (!to_resolve)
4598 {
4599 resolved_enums.push_back(i->first);
4600 continue;
4601 }
4602
4603 // Now, for each decl-only enum that have the current name
4604 // 'i->first', let's try to poke at the fully defined enum
4605 // that is defined in the same translation unit as the
4606 // declaration.
4607 //
4608 // If we find one enum (defined in the TU of the declaration)
4609 // that defines the declaration, then the declaration can be
4610 // resolved to that enum.
4611 //
4612 // If no defining enum is found in the TU of the declaration,
4613 // then there are possibly three cases to consider:
4614 //
4615 // 1/ There is exactly one enum that defines the
4616 // declaration and that enum is defined in another TU. In
4617 // this case, the declaration is resolved to that
4618 // definition.
4619 //
4620 // 2/ There are more than one (different) enum that define
4621 // that declaration and none of them is defined in the TU of
4622 // the declaration. In this case, the declaration is left
4623 // unresolved.
4624 //
4625 // 3/ No enum defines the declaration. In this case, the
4626 // declaration is left unresolved.
4627
4628 // So get the enums that might define the current
4629 // declarations which name is i->first.
4630 const type_base_wptrs_type *enums =
4631 lookup_enum_types(i->first, *corpus());
4632 if (!enums)
4633 continue;
4634
4635 // This is a map that associates the translation unit path to
4636 // the enum (that potentially defines the declarations that
4637 // we consider) that are defined in that translation unit. It
4638 // should stay ordered by using the TU path as key to ensure
4639 // stability of the order of enum definitions in ABIXML
4640 // output.
4641 map<string, enum_type_decl_sptr> per_tu_enum_map;
4642 for (type_base_wptrs_type::const_iterator c = enums->begin();
4643 c != enums->end();
4644 ++c)
4645 {
4646 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4647 ABG_ASSERT(enom);
4648
4650 if (enom->get_is_declaration_only())
4651 continue;
4652
4653 string tu_path = enom->get_translation_unit()->get_absolute_path();
4654 if (tu_path.empty())
4655 continue;
4656
4657 // Build a map that associates the translation unit path
4658 // to the enum (that potentially defines the declarations
4659 // that we consider) that are defined in that translation unit.
4660 per_tu_enum_map[tu_path] = enom;
4661 }
4662
4663 if (!per_tu_enum_map.empty())
4664 {
4665 // Walk the declarations to resolve and resolve them
4666 // either to the definitions that are in the same TU as
4667 // the declaration, or to the definition found elsewhere,
4668 // if there is only one such definition.
4669 for (enums_type::iterator j = i->second.begin();
4670 j != i->second.end();
4671 ++j)
4672 {
4673 if ((*j)->get_is_declaration_only()
4674 && ((*j)->get_definition_of_declaration() == 0))
4675 {
4676 string tu_path =
4677 (*j)->get_translation_unit()->get_absolute_path();
4678 map<string, enum_type_decl_sptr>::const_iterator e =
4679 per_tu_enum_map.find(tu_path);
4680 if (e != per_tu_enum_map.end())
4681 (*j)->set_definition_of_declaration(e->second);
4682 else if (per_tu_enum_map.size() == 1)
4683 (*j)->set_definition_of_declaration
4684 (per_tu_enum_map.begin()->second);
4685 else
4686 {
4687 // We are in case where there are more than
4688 // one definition for the declaration. Let's
4689 // see if they are all equal. If they are,
4690 // then the declaration resolves to the
4691 // definition. Otherwise, we are in the case
4692 // 3/ described above.
4693 map<string,
4694 enum_type_decl_sptr>::const_iterator it;
4695 enum_type_decl_sptr first_enum =
4696 per_tu_enum_map.begin()->second;
4697 bool all_enum_definitions_are_equal = true;
4698 for (it = per_tu_enum_map.begin();
4699 it != per_tu_enum_map.end();
4700 ++it)
4701 {
4702 if (it == per_tu_enum_map.begin())
4703 continue;
4704 else
4705 {
4706 if (!compare_before_canonicalisation(it->second,
4707 first_enum))
4708 {
4709 all_enum_definitions_are_equal = false;
4710 break;
4711 }
4712 }
4713 }
4714 if (all_enum_definitions_are_equal)
4715 (*j)->set_definition_of_declaration(first_enum);
4716 }
4717 }
4718 }
4719 resolved_enums.push_back(i->first);
4720 }
4721 }
4722
4723 size_t num_decl_only_enums = declaration_only_enums().size(),
4724 num_resolved = resolved_enums.size();
4725 if (show_stats())
4726 cerr << "resolved " << num_resolved
4727 << " enum declarations out of "
4728 << num_decl_only_enums
4729 << "\n";
4730
4731 for (vector<string>::const_iterator i = resolved_enums.begin();
4732 i != resolved_enums.end();
4733 ++i)
4734 declaration_only_enums().erase(*i);
4735
4736 if (show_stats() && !declaration_only_enums().empty())
4737 {
4738 cerr << "Here are the "
4739 << num_decl_only_enums - num_resolved
4740 << " unresolved enum declarations:\n";
4741 for (string_enums_map::iterator i = declaration_only_enums().begin();
4742 i != declaration_only_enums().end();
4743 ++i)
4744 cerr << " " << i->first << "\n";
4745 }
4746 }
4747
4748 /// Test if a symbol belongs to a function of the current ABI
4749 /// corpus.
4750 ///
4751 /// This is a sub-routine of fixup_functions_with_no_symbols.
4752 ///
4753 /// @param fn the function symbol to consider.
4754 ///
4755 /// @returnt true if @p fn belongs to a function of the current ABI
4756 /// corpus.
4757 bool
4758 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4759 {
4760 corpus_sptr corp = corpus();
4761 if (!corp)
4762 return false;
4763
4764 interned_string id = corp->get_environment().intern(fn->get_id_string());
4765
4766 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4767 if (!fns)
4768 return false;
4769
4770 for (auto f : *fns)
4771 if (f->get_symbol())
4772 return true;
4773
4774 return false;
4775 }
4776
4777 /// Some functions described by DWARF may have their linkage name
4778 /// set, but no link to their actual underlying elf symbol. When
4779 /// these are virtual member functions, comparing the enclosing type
4780 /// against another one which has its underlying symbol properly set
4781 /// might lead to spurious type changes.
4782 ///
4783 /// If the corpus contains a symbol with the same name as the
4784 /// linkage name of the function, then set up the link between the
4785 /// function and its underlying symbol.
4786 ///
4787 /// Note that for the moment, only virtual member functions are
4788 /// fixed up like this. This is because they really are the only
4789 /// fuctions of functions that can affect types (in spurious ways).
4790 void
4791 fixup_functions_with_no_symbols()
4792 {
4793 corpus_sptr corp = corpus();
4794 if (!corp)
4795 return;
4796
4797 die_function_decl_map_type &fns_with_no_symbol =
4798 die_function_decl_with_no_symbol_map();
4799
4800 if (do_log())
4801 cerr << fns_with_no_symbol.size()
4802 << " functions to fixup, potentially\n";
4803
4804 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4805 i != fns_with_no_symbol.end();
4806 ++i)
4807 if (elf_symbol_sptr sym =
4808 corp->lookup_function_symbol(i->second->get_linkage_name()))
4809 {
4810 // So i->second is a virtual member function that was
4811 // previously scheduled to be set a function symbol.
4812 //
4813 // But if it appears that it now has a symbol already set,
4814 // then do not set a symbol to it again.
4815 //
4816 // Or if it appears that another virtual member function
4817 // from the current ABI Corpus, with the same linkage
4818 // (mangled) name has already been set a symbol, then do not
4819 // set a symbol to this function either. Otherwise, there
4820 // will be two virtual member functions with the same symbol
4821 // in the class and that leads to spurious hard-to-debug
4822 // change reports later down the road.
4823 if (i->second->get_symbol()
4824 || symbol_already_belongs_to_a_function(sym))
4825 continue;
4826
4827 ABG_ASSERT(is_member_function(i->second));
4829 i->second->set_symbol(sym);
4830
4831 if (do_log())
4832 cerr << "fixed up '"
4833 << i->second->get_pretty_representation()
4834 << "' with symbol '"
4835 << sym->get_id_string()
4836 << "'\n";
4837 }
4838
4839 fns_with_no_symbol.clear();
4840 }
4841
4842 /// Copy missing member functions from a source @ref class_decl to a
4843 /// destination one.
4844 ///
4845 /// If a function is present on the source @ref class_decl and not
4846 /// on the destination one, then it's copied from the source class
4847 /// to the destination one.
4848 void
4849 copy_missing_member_functions(const class_decl_sptr& dest_class,
4850 const class_decl_sptr& src_class)
4851 {
4852 for (auto method : src_class->get_member_functions())
4853 if (!method->get_linkage_name().empty())
4854 if (!dest_class->find_member_function(method->get_linkage_name()))
4855 {
4856 method_decl_sptr copied_method =
4857 copy_member_function(dest_class, method);
4858 ABG_ASSERT(copied_method);
4859 schedule_type_for_late_canonicalization(copied_method->get_type());
4860 }
4861 }
4862
4863 /// Test if there is an interator in a given range that points to
4864 /// an anonymous class.
4865 ///
4866 /// @param begin the start of the iterator range to consider.
4867 ///
4868 /// @param end the end of the iterator range to consider. This
4869 /// points to after the range.
4870 template <typename iterator_type>
4871 bool
4872 contains_anonymous_class(const iterator_type& begin,
4873 const iterator_type& end)
4874 {
4875 for (auto i = begin; i < end; ++i)
4876 {
4877 type_base_sptr t(*i);
4879 if (c && c->get_is_anonymous())
4880 return true;
4881 }
4882 return false;
4883 }
4884
4885 /// Ensure that all classes of the same name have the same virtual
4886 /// member functions. So copy the virtual member functions from a
4887 /// class C that have them to another class C that doesn't.
4888 ///
4889 /// @param begin an iterator to the first member of the set of
4890 /// classes which to merge virtual member functions for.
4891 ///
4892 /// @param end an iterator to the last member (one past the end
4893 /// actually) of the set of classes which to merge virtual member
4894 /// functions for.
4895 template <typename iterator_type>
4896 void
4897 merge_member_functions_of_classes(const iterator_type& begin,
4898 const iterator_type& end)
4899 {
4900 if (contains_anonymous_class(begin, end))
4901 return;
4902
4903 for (auto i = begin; i < end; ++i)
4904 {
4905 type_base_sptr t(*i);
4906 class_decl_sptr reference_class = is_class_type(t);
4907 if (!reference_class)
4908 continue;
4909
4910 string n1 = reference_class->get_pretty_representation(true, true);
4911 string n2;
4912 for (auto j = begin; j < end; ++j)
4913 {
4914 if (j == i)
4915 continue;
4916
4917 type_base_sptr type(*j);
4918 class_decl_sptr klass = is_class_type(type);
4919 if (!klass)
4920 continue;
4921
4922 n2 = klass->get_pretty_representation(true, true);
4923 ABG_ASSERT(n1 == n2);
4924
4925 copy_missing_member_functions(reference_class, klass);
4926 copy_missing_member_functions(klass, reference_class);
4927 }
4928 }
4929 }
4930
4931 /// Ensure that all classes of the same name have the same virtual
4932 /// member functions. So copy the virtual member functions from a
4933 /// class C that have them to another class C that doesn't.
4934 void
4935 merge_member_functions_in_classes_of_same_names()
4936 {
4937 corpus_sptr abi = corpus();
4938 if (!abi)
4939 return;
4940
4942 abi->get_types().class_types();
4943
4944 for (auto entry : class_types)
4945 {
4946 auto& classes = entry.second;
4947 if (classes.size() > 1)
4948 {
4949 bool a_class_has_member_fns = false;
4950 for (auto& c : classes)
4951 {
4952 type_base_sptr t(c);
4953 if (class_decl_sptr klass = is_class_type(t))
4954 if (!klass->get_member_functions().empty())
4955 {
4956 a_class_has_member_fns = true;
4957 break;
4958 }
4959 }
4960 if (a_class_has_member_fns)
4961 merge_member_functions_of_classes(classes.begin(),
4962 classes.end());
4963 }
4964 }
4965 }
4966
4967 /// @return vectors of types created during the analysis of the
4968 /// DWARF and in the need of being canonicalized.
4969 const vector<type_base_sptr>&
4970 types_to_canonicalize() const
4971 {return types_to_canonicalize_;}
4972
4973 /// @return vectors of types created during the analysis of the
4974 /// DWARF and in the need of being canonicalized.
4975 vector<type_base_sptr>&
4976 types_to_canonicalize()
4977 {return types_to_canonicalize_;}
4978
4979 /// Clear the containers holding types to canonicalize.
4980 void
4981 clear_types_to_canonicalize()
4982 {
4983 types_to_canonicalize_.clear();
4984 }
4985
4986 /// Types that were created but not tied to a particular DIE, must
4987 /// be scheduled for late canonicalization using this method.
4988 ///
4989 /// @param t the type to schedule for late canonicalization.
4990 void
4991 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4992 {
4993 types_to_canonicalize_.push_back(t);
4994 }
4995
4996 /// Canonicalize types which DIE offsets are stored in vectors on
4997 /// the side. This is a sub-routine of
4998 /// reader::perform_late_type_canonicalizing().
4999 ///
5000 /// @param source where the DIE of the types to canonicalize are
5001 /// from.
5002 void
5003 canonicalize_types_scheduled()
5004 {
5005 tools_utils::timer cn_timer;
5006 if (do_log())
5007 {
5008 cerr << "DWARF Reader is going to canonicalize "
5009 << std::dec
5010 << types_to_canonicalize().size()
5011 << " types";
5012 corpus_sptr c = corpus();
5013 if (c)
5014 cerr << " from corpus " << corpus()->get_path() << "\n";
5015 cn_timer.start();
5016 }
5017
5019 (types_to_canonicalize().begin(),
5020 types_to_canonicalize().end(),
5021 [](const vector<type_base_sptr>::const_iterator& i)
5022 {return *i;}, do_log(), show_stats());
5023
5024 if (do_log())
5025 {
5026 cn_timer.stop();
5027 cerr << "DWARF Reader finished types "
5028 << "sorting, hashing & canonicalizing in: "
5029 << cn_timer << "\n";
5030 }
5031 }
5032
5033 /// Compute the number of canonicalized and missed types in the late
5034 /// canonicalization phase.
5035 ///
5036 /// @param source where the DIEs of the canonicalized types are
5037 /// from.
5038 ///
5039 /// @param canonicalized the number of types that got canonicalized
5040 /// is added to the value already present in this parameter.
5041 ///
5042 /// @param missed the number of types scheduled for late
5043 /// canonicalization and which couldn't be canonicalized (for a
5044 /// reason) is added to the value already present in this parameter.
5045 void
5046 add_late_canonicalized_types_stats(size_t& canonicalized,
5047 size_t& missed) const
5048 {
5049 for (auto t : types_to_canonicalize())
5050 {
5051 if (t->get_canonical_type())
5052 ++canonicalized;
5053 else
5054 ++missed;
5055 }
5056 }
5057
5058 // Look at the types that need to be canonicalized after the
5059 // translation unit has been constructed and canonicalize them.
5060 void
5061 perform_late_type_canonicalizing()
5062 {
5063 canonicalize_types_scheduled();
5064
5065 if (show_stats())
5066 {
5067 size_t num_canonicalized = 0, num_missed = 0, total = 0;
5068 add_late_canonicalized_types_stats(num_canonicalized,
5069 num_missed);
5070 total = num_canonicalized + num_missed;
5071 cerr << "binary: "
5072 << elf_path()
5073 << "\n";
5074 cerr << " # late canonicalized types: "
5075 << num_canonicalized;
5076 if (total)
5077 cerr << " (" << num_canonicalized * 100 / total << "%)";
5078 cerr << "\n"
5079 << " # missed canonicalization opportunities: "
5080 << num_missed;
5081 if (total)
5082 cerr << " (" << num_missed * 100 / total << "%)";
5083 cerr << "\n";
5084 }
5085
5086 }
5087
5088 const die_tu_map_type&
5089 die_tu_map() const
5090 {return die_tu_map_;}
5091
5093 die_tu_map()
5094 {return die_tu_map_;}
5095
5096 /// Getter for the map that associates a translation unit DIE to the
5097 /// vector of imported unit points that it contains.
5098 ///
5099 /// @param source where the DIEs are from.
5100 ///
5101 /// @return the map.
5103 tu_die_imported_unit_points_map(die_source source) const
5104 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
5105
5106 /// Getter for the map that associates a translation unit DIE to the
5107 /// vector of imported unit points that it contains.
5108 ///
5109 /// @param source where the DIEs are from.
5110 ///
5111 /// @return the map.
5113 tu_die_imported_unit_points_map(die_source source)
5114 {
5115 switch (source)
5116 {
5117 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5118 break;
5119 case ALT_DEBUG_INFO_DIE_SOURCE:
5120 return alt_tu_die_imported_unit_points_map_;
5121 case TYPE_UNIT_DIE_SOURCE:
5122 return type_units_tu_die_imported_unit_points_map_;
5123 case NO_DEBUG_INFO_DIE_SOURCE:
5124 case NUMBER_OF_DIE_SOURCES:
5125 // We cannot reach this point.
5127 }
5128 return tu_die_imported_unit_points_map_;
5129 }
5130
5131 /// Reset the current corpus being constructed.
5132 ///
5133 /// This actually deletes the current corpus being constructed.
5134 void
5135 reset_corpus()
5136 {corpus().reset();}
5137
5138 /// Get the map that associates each DIE to its parent DIE. This is
5139 /// for DIEs coming from the main debug info sections.
5140 ///
5141 /// @param source where the DIEs in the map come from.
5142 ///
5143 /// @return the DIE -> parent map.
5145 die_parent_map(die_source source) const
5146 {return const_cast<reader*>(this)->die_parent_map(source);}
5147
5148 /// Get the map that associates each DIE to its parent DIE. This is
5149 /// for DIEs coming from the main debug info sections.
5150 ///
5151 /// @param source where the DIEs in the map come from.
5152 ///
5153 /// @return the DIE -> parent map.
5155 die_parent_map(die_source source)
5156 {
5157 switch (source)
5158 {
5159 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5160 break;
5161 case ALT_DEBUG_INFO_DIE_SOURCE:
5162 return alternate_die_parent_map_;
5163 case TYPE_UNIT_DIE_SOURCE:
5164 return type_section_die_parent_map();
5165 case NO_DEBUG_INFO_DIE_SOURCE:
5166 case NUMBER_OF_DIE_SOURCES:
5168 }
5169 return primary_die_parent_map_;
5170 }
5171
5173 type_section_die_parent_map() const
5174 {return type_section_die_parent_map_;}
5175
5177 type_section_die_parent_map()
5178 {return type_section_die_parent_map_;}
5179
5180 /// Getter of the current translation unit.
5181 ///
5182 /// @return the current translation unit being constructed.
5184 cur_transl_unit() const
5185 {return cur_tu_;}
5186
5187 /// Getter of the current translation unit.
5188 ///
5189 /// @return the current translation unit being constructed.
5191 cur_transl_unit()
5192 {return cur_tu_;}
5193
5194 /// Setter of the current translation unit.
5195 ///
5196 /// @param tu the current translation unit being constructed.
5197 void
5198 cur_transl_unit(translation_unit_sptr tu)
5199 {
5200 if (tu)
5201 cur_tu_ = tu;
5202 }
5203
5204 /// Return the global scope of the current translation unit.
5205 ///
5206 /// @return the global scope of the current translation unit.
5207 const scope_decl_sptr&
5208 global_scope() const
5209 {return cur_transl_unit()->get_global_scope();}
5210
5211 /// Return a scope that is nil.
5212 ///
5213 /// @return a scope that is nil.
5214 const scope_decl_sptr&
5215 nil_scope() const
5216 {return nil_scope_;}
5217
5218 const scope_stack_type&
5219 scope_stack() const
5220 {return scope_stack_;}
5221
5223 scope_stack()
5224 {return scope_stack_;}
5225
5226 scope_decl*
5227 current_scope()
5228 {
5229 if (scope_stack().empty())
5230 {
5231 if (cur_transl_unit())
5232 scope_stack().push(cur_transl_unit()->get_global_scope().get());
5233 }
5234 return scope_stack().top();
5235 }
5236
5237 list<var_decl_sptr>&
5238 var_decls_to_re_add_to_tree()
5239 {return var_decls_to_add_;}
5240
5241 /// Test if a DIE represents a decl (function or variable) that has
5242 /// a symbol that is exported, whatever that means. This is
5243 /// supposed to work for Linux Kernel binaries as well.
5244 ///
5245 /// This is useful to limit the amount of DIEs taken into account to
5246 /// the strict limit of what an ABI actually means. Limiting the
5247 /// volume of DIEs analyzed this way is an important optimization to
5248 /// keep big binaries "manageable" by libabigail.
5249 ///
5250 /// @param DIE the die to consider.
5251 bool
5252 is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
5253 {
5254 if (!die || !die_is_decl(die))
5255 return false;
5256
5257 bool result = false, address_found = false, symbol_is_exported = false;;
5258 Dwarf_Addr decl_symbol_address = 0;
5259
5260 if (die_is_variable_decl(die))
5261 {
5262 if ((address_found = get_variable_address(die, decl_symbol_address)))
5263 symbol_is_exported =
5264 !!variable_symbol_is_exported(decl_symbol_address);
5265 }
5266 else if (die_is_function_decl(die))
5267 {
5268 if ((address_found = get_function_address(die, decl_symbol_address)))
5269 symbol_is_exported =
5270 !!function_symbol_is_exported(decl_symbol_address);
5271 }
5272
5273 if (address_found)
5274 result = symbol_is_exported;
5275
5276 return result;
5277 }
5278
5279 /// Test if a DIE is a variable or function DIE which name denotes
5280 /// an undefined ELF symbol.
5281 ///
5282 /// @return true iff @p die represents a function or variable that
5283 /// has an undefined symbol.
5284 bool
5285 is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
5286 {
5287 if (is_decl_die_with_exported_symbol(die))
5288 return false;
5289
5290 string name, linkage_name;
5291 die_name_and_linkage_name(die, name, linkage_name);
5292 if (linkage_name.empty())
5293 linkage_name = name;
5294
5295 bool result = false;
5296 if ((die_is_variable_decl(die)
5297 && symtab()->variable_symbol_is_undefined(linkage_name))
5298 ||
5299 (die_is_function_decl(die)
5300 && symtab()->function_symbol_is_undefined(linkage_name)))
5301 result = true;
5302
5303 return result;
5304 }
5305
5306 /// This is a sub-routine of maybe_adjust_fn_sym_address and
5307 /// maybe_adjust_var_sym_address.
5308 ///
5309 /// Given an address that we got by looking at some debug
5310 /// information (e.g, a symbol's address referred to by a DWARF
5311 /// TAG), If the ELF file we are interested in is a shared library
5312 /// or an executable, then adjust the address to be coherent with
5313 /// where the executable (or shared library) is loaded. That way,
5314 /// the address can be used to look for symbols in the executable or
5315 /// shared library.
5316 ///
5317 /// @return the adjusted address, or the same address as @p addr if
5318 /// it didn't need any adjustment.
5319 Dwarf_Addr
5320 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
5321 {
5322 if (addr == 0)
5323 return addr;
5324
5325 GElf_Ehdr eh_mem;
5326 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5327
5328 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
5329 {
5330 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
5331 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
5332 dwarf_elf_load_address));
5334 elf_load_address));
5335 if (dwarf_is_splitted()
5336 && (dwarf_elf_load_address != elf_load_address))
5337 // This means that in theory the DWARF and the executable are
5338 // not loaded at the same address. And addr is meaningful
5339 // only in the context of the DWARF.
5340 //
5341 // So let's transform addr into an offset relative to where
5342 // the DWARF is loaded, and let's add that relative offset
5343 // to the load address of the executable. That way, addr
5344 // becomes meaningful in the context of the executable and
5345 // can thus be used to compare against the address of
5346 // symbols of the executable, for instance.
5347 addr = addr - dwarf_elf_load_address + elf_load_address;
5348 }
5349
5350 return addr;
5351 }
5352
5353 /// For a relocatable (*.o) elf file, this function expects an
5354 /// absolute address, representing a function symbol. It then
5355 /// extracts the address of the .text section from the symbol
5356 /// absolute address to get the relative address of the function
5357 /// from the beginning of the .text section.
5358 ///
5359 /// For executable or shared library, this function expects an
5360 /// address of a function symbol that was retrieved by looking at a
5361 /// DWARF "file". The function thus adjusts the address to make it
5362 /// be meaningful in the context of the ELF file.
5363 ///
5364 /// In both cases, the address can then be compared against the
5365 /// st_value field of a function symbol from the ELF file.
5366 ///
5367 /// @param addr an adress for a function symbol that was retrieved
5368 /// from a DWARF file.
5369 ///
5370 /// @return the (possibly) adjusted address, or just @p addr if no
5371 /// adjustment took place.
5372 Dwarf_Addr
5373 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
5374 {
5375 if (addr == 0)
5376 return addr;
5377
5378 Elf* elf = elf_handle();
5379 GElf_Ehdr eh_mem;
5380 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5381
5382 if (elf_header->e_type == ET_REL)
5383 // We are looking at a relocatable file. In this case, we don't
5384 // do anything because:
5385 //
5386 // 1/ the addresses from DWARF are absolute (relative to the
5387 // beginning of the relocatable file)
5388 //
5389 // 2/ The ELF symbol addresses that we store in our lookup
5390 // tables are translated from section-related to absolute as
5391 // well. So we don't have anything to do at this point for
5392 // ET_REL files.
5393 ;
5394 else
5395 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5396
5397 return addr;
5398 }
5399
5400 /// For a relocatable (*.o) elf file, this function expects an
5401 /// absolute address, representing a global variable symbol. It
5402 /// then extracts the address of the {.data,.data1,.rodata,.bss}
5403 /// section from the symbol absolute address to get the relative
5404 /// address of the variable from the beginning of the data section.
5405 ///
5406 /// For executable or shared library, this function expects an
5407 /// address of a variable symbol that was retrieved by looking at a
5408 /// DWARF "file". The function thus adjusts the address to make it
5409 /// be meaningful in the context of the ELF file.
5410 ///
5411 /// In both cases, the address can then be compared against the
5412 /// st_value field of a function symbol from the ELF file.
5413 ///
5414 /// @param addr an address for a global variable symbol that was
5415 /// retrieved from a DWARF file.
5416 ///
5417 /// @return the (possibly) adjusted address, or just @p addr if no
5418 /// adjustment took place.
5419 Dwarf_Addr
5420 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5421 {
5422 Elf* elf = elf_handle();
5423 GElf_Ehdr eh_mem;
5424 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5425
5426 if (elf_header->e_type == ET_REL)
5427 // We are looking at a relocatable file. In this case, we don't
5428 // do anything because:
5429 //
5430 // 1/ the addresses from DWARF are absolute (relative to the
5431 // beginning of the relocatable file)
5432 //
5433 // 2/ The ELF symbol addresses that we store in our lookup
5434 // tables are translated from section-related to absolute as
5435 // well. So we don't have anything to do at this point for
5436 // ET_REL files.
5437 ;
5438 else
5439 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5440
5441 return addr;
5442 }
5443
5444 /// Get the first exported function address in the set of addresses
5445 /// referred to by the DW_AT_ranges attribute of a given DIE.
5446 ///
5447 /// @param die the DIE we are considering.
5448 ///
5449 /// @param address output parameter. This is set to the first
5450 /// address found in the sequence pointed to by the DW_AT_ranges
5451 /// attribute found on the DIE @p die, iff the function returns
5452 /// true. Otherwise, no value is set into this output parameter.
5453 ///
5454 /// @return true iff the DIE @p die does have a DW_AT_ranges
5455 /// attribute and an address of an exported function was found in
5456 /// its sequence value.
5457 bool
5458 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5459 Dwarf_Addr& address) const
5460 {
5461 Dwarf_Addr base;
5462 Dwarf_Addr end_addr;
5463 ptrdiff_t offset = 0;
5464
5465 do
5466 {
5467 Dwarf_Addr addr = 0, fn_addr = 0;
5468 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5469 {
5470 fn_addr = maybe_adjust_fn_sym_address(addr);
5471 if (function_symbol_is_exported(fn_addr))
5472 {
5473 address = fn_addr;
5474 return true;
5475 }
5476 }
5477 } while (offset > 0);
5478 return false;
5479 }
5480
5481 /// Get the address of the function.
5482 ///
5483 /// The address of the function is considered to be the value of the
5484 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5485 /// only) to not point to an absolute address anymore, but rather to
5486 /// the address of the function inside the .text segment.
5487 ///
5488 /// @param function_die the die of the function to consider.
5489 ///
5490 /// @param address the resulting address iff the function returns
5491 /// true.
5492 ///
5493 /// @return true if the function address was found.
5494 bool
5495 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5496 {
5497 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5498 DW_AT_low_pc, address))
5499 // So no DW_AT_low_pc was found. Let's see if the function DIE
5500 // has got a DW_AT_ranges attribute instead. If it does, the
5501 // first address of the set of addresses represented by the
5502 // value of that DW_AT_ranges represents the function (symbol)
5503 // address we are looking for.
5504 if (!get_first_exported_fn_address_from_DW_AT_ranges
5505 (const_cast<Dwarf_Die*>(function_die),
5506 address))
5507 return false;
5508
5509 address = maybe_adjust_fn_sym_address(address);
5510 return true;
5511 }
5512
5513 /// Get the address of the global variable.
5514 ///
5515 /// The address of the global variable is considered to be the value
5516 /// of the DW_AT_location attribute, possibly adjusted (in
5517 /// relocatable files only) to not point to an absolute address
5518 /// anymore, but rather to the address of the global variable inside
5519 /// the data segment.
5520 ///
5521 /// @param variable_die the die of the function to consider.
5522 ///
5523 /// @param address the resulting address iff this function returns
5524 /// true.
5525 ///
5526 /// @return true if the variable address was found.
5527 bool
5528 get_variable_address(const Dwarf_Die* variable_die,
5529 Dwarf_Addr& address) const
5530 {
5531 bool is_tls_address = false;
5532 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5533 address, is_tls_address))
5534 return false;
5535 if (!is_tls_address)
5536 address = maybe_adjust_var_sym_address(address);
5537 return true;
5538 }
5539
5540 /// Getter of the exported decls builder object.
5541 ///
5542 /// @return the exported decls builder.
5543 corpus::exported_decls_builder*
5544 exported_decls_builder()
5545 {return corpus()->get_exported_decls_builder().get();}
5546
5547 /// Getter of the "load_all_types" flag. This flag tells if all the
5548 /// types (including those not reachable by public declarations) are
5549 /// to be read and represented in the final ABI corpus.
5550 ///
5551 /// @return the load_all_types flag.
5552 bool
5553 load_all_types() const
5554 {return options().load_all_types;}
5555
5556 /// Setter of the "load_all_types" flag. This flag tells if all the
5557 /// types (including those not reachable by public declarations) are
5558 /// to be read and represented in the final ABI corpus.
5559 ///
5560 /// @param f the new load_all_types flag.
5561 void
5562 load_all_types(bool f)
5563 {options().load_all_types = f;}
5564
5565 bool
5566 load_in_linux_kernel_mode() const
5567 {return options().load_in_linux_kernel_mode;}
5568
5569 void
5570 load_in_linux_kernel_mode(bool f)
5571 {options().load_in_linux_kernel_mode = f;}
5572
5573 /// Getter of the 'load-undefined-interface' property.
5574 ///
5575 /// That property tells the reader if it should load the interfaces
5576 /// that are undefined in the binary. An undefined interface is a
5577 /// variable or function which has a symbol that is not defined in
5578 /// the binary.
5579 ///
5580 /// @return true iff the front-end has to load the undefined
5581 /// interfaces.
5582 bool
5583 load_undefined_interfaces() const
5584 {return options().load_undefined_interfaces;}
5585
5586 /// Test if it's allowed to assume that the DWARF debug info has
5587 /// been factorized (for instance, with the DWZ tool) so that if two
5588 /// type DIEs originating from the .gnu_debugaltlink section have
5589 /// different offsets, they represent different types.
5590 ///
5591 /// @return true iff we can assume that the DWARF debug info has
5592 /// been factorized.
5593 bool
5594 leverage_dwarf_factorization() const
5595 {
5596 if (!leverage_dwarf_factorization_.has_value())
5597 {
5598 if (options().leverage_dwarf_factorization
5599 && elf_helpers::find_section_by_name(elf_handle(),
5600 ".gnu_debugaltlink"))
5601 leverage_dwarf_factorization_ = true;
5602 else
5603 leverage_dwarf_factorization_ = false;
5604 }
5605 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5606
5607 return *leverage_dwarf_factorization_;
5608 }
5609
5610 /// Getter of the "show_stats" flag.
5611 ///
5612 /// This flag tells if we should emit statistics about various
5613 /// internal stuff.
5614 ///
5615 /// @return the value of the flag.
5616 bool
5617 show_stats() const
5618 {return options().show_stats;}
5619
5620 /// Setter of the "show_stats" flag.
5621 ///
5622 /// This flag tells if we should emit statistics about various
5623 /// internal stuff.
5624 ///
5625 /// @param f the value of the flag.
5626 void
5627 show_stats(bool f)
5628 {options().show_stats = f;}
5629
5630 /// Getter of the "do_log" flag.
5631 ///
5632 /// This flag tells if we should log about various internal
5633 /// details.
5634 ///
5635 /// return the "do_log" flag.
5636 bool
5637 do_log() const
5638 {return options().do_log;}
5639
5640 /// Setter of the "do_log" flag.
5641 ///
5642 /// This flag tells if we should log about various internal details.
5643 ///
5644 /// @param f the new value of the flag.
5645 void
5646 do_log(bool f)
5647 {options().do_log = f;}
5648
5649 /// Walk the DIEs under a given die and for each child, populate the
5650 /// die -> parent map to record the child -> parent relationship
5651 /// that
5652 /// exists between the child and the given die.
5653 ///
5654 /// The function also builds the vector of places where units are
5655 /// imported.
5656 ///
5657 /// This is done recursively as for each child DIE, this function
5658 /// walks its children as well.
5659 ///
5660 /// @param die the DIE whose children to walk recursively.
5661 ///
5662 /// @param source where the DIE @p die comes from.
5663 ///
5664 /// @param imported_units a vector containing all the offsets of the
5665 /// points where unit have been imported, under @p die.
5666 void
5667 build_die_parent_relations_under(Dwarf_Die* die,
5668 die_source source,
5669 imported_unit_points_type & imported_units)
5670 {
5671 if (!die)
5672 return;
5673
5674 offset_offset_map_type& parent_of = die_parent_map(source);
5675
5676 Dwarf_Die child;
5677 if (dwarf_child(die, &child) != 0)
5678 return;
5679
5680 do
5681 {
5682 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5683 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5684 {
5685 Dwarf_Die imported_unit;
5686 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5687 // If the imported_unit has a sub-tree, let's record
5688 // this point at which the sub-tree is imported into
5689 // the current debug info.
5690 //
5691 // Otherwise, if the imported_unit has no sub-tree,
5692 // there is no point in recording where a non-existent
5693 // sub-tree is being imported.
5694 //
5695 // Note that the imported_unit_points_type type below
5696 // expects the imported_unit to have a sub-tree.
5697 && die_has_children(&imported_unit))
5698 {
5699 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5700 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5701 imported_units.push_back
5702 (imported_unit_point(dwarf_dieoffset(&child),
5703 imported_unit,
5704 imported_unit_die_source));
5705 }
5706 }
5707 build_die_parent_relations_under(&child, source, imported_units);
5708 }
5709 while (dwarf_siblingof(&child, &child) == 0);
5710
5711 }
5712
5713 /// Determine if we do have to build a DIE -> parent map, depending
5714 /// on a given language.
5715 ///
5716 /// Some languages like C++, Ada etc, do have the concept of
5717 /// namespace and yet, the DIE data structure doesn't provide us
5718 /// with a way to get the parent namespace of a given DIE. So for
5719 /// those languages, we need to build a DIE -> parent map so that we
5720 /// can get the namespace DIE (or more generally the scope DIE) of a given
5721 /// DIE as we need it.
5722 ///
5723 /// But then some more basic languages like C or assembly don't have
5724 /// that need.
5725 ///
5726 /// This function, depending on the language, tells us if we need to
5727 /// build the DIE -> parent map or not.
5728 ///
5729 /// @param lang the language to consider.
5730 ///
5731 /// @return true iff we need to build the DIE -> parent map for this
5732 /// language.
5733 bool
5734 do_we_build_die_parent_maps(translation_unit::language lang)
5735 {
5736 if (is_c_language(lang))
5737 return false;
5738
5739 switch (lang)
5740 {
5741 case translation_unit::LANG_UNKNOWN:
5742#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5743 case translation_unit::LANG_Mips_Assembler:
5744#endif
5745 return false;
5746 default:
5747 break;
5748 }
5749 return true;
5750 }
5751
5752 /// Walk all the DIEs accessible in the debug info (and in the
5753 /// alternate debug info as well) and build maps representing the
5754 /// relationship DIE -> parent. That is, make it so that we can get
5755 /// the parent for a given DIE.
5756 ///
5757 /// Note that the goal of this map is to be able to get the parent
5758 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5759 /// when we get a DIE of a type, and we want to build an internal
5760 /// representation for it, we need to get its fully qualified name.
5761 /// For that, we need to know what is the parent DIE of that type
5762 /// DIE, so that we can know what the namespace of that type is.
5763 ///
5764 /// Note that as the C language doesn't have namespaces (all types
5765 /// are defined in the same global namespace), this function doesn't
5766 /// build the DIE -> parent map if the current translation unit
5767 /// comes from C. This saves time on big C ELF files with a lot of
5768 /// DIEs.
5769 void
5770 build_die_parent_maps()
5771 {
5772 bool we_do_have_to_build_die_parent_map = false;
5773 uint8_t address_size = 0;
5774 size_t header_size = 0;
5775 // Get the DIE of the current translation unit, look at it to get
5776 // its language. If that language is in C, then all types are in
5777 // the global namespace so we don't need to build the DIE ->
5778 // parent map. So we dont build it in that case.
5779 for (Dwarf_Off offset = 0, next_offset = 0;
5780 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5781 offset, &next_offset, &header_size,
5782 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5783 offset = next_offset)
5784 {
5785 Dwarf_Off die_offset = offset + header_size;
5786 Dwarf_Die cu;
5787 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5788 die_offset, &cu))
5789 continue;
5790
5791 uint64_t l = 0;
5792 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5793 translation_unit::language lang = dwarf_language_to_tu_language(l);
5794 if (do_we_build_die_parent_maps(lang))
5795 we_do_have_to_build_die_parent_map = true;
5796 }
5797
5798 if (!we_do_have_to_build_die_parent_map)
5799 return;
5800
5801 // Build the DIE -> parent relation for DIEs coming from the
5802 // .debug_info section in the alternate debug info file.
5803 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5804 for (Dwarf_Off offset = 0, next_offset = 0;
5805 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5806 offset, &next_offset, &header_size,
5807 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5808 offset = next_offset)
5809 {
5810 Dwarf_Off die_offset = offset + header_size;
5811 Dwarf_Die cu;
5812 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5813 die_offset, &cu))
5814 continue;
5815 cur_tu_die(&cu);
5816
5817 imported_unit_points_type& imported_units =
5818 tu_die_imported_unit_points_map(source)[die_offset] =
5820 build_die_parent_relations_under(&cu, source, imported_units);
5821 }
5822
5823 // Build the DIE -> parent relation for DIEs coming from the
5824 // .debug_info section of the main debug info file.
5825 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5826 address_size = 0;
5827 header_size = 0;
5828 for (Dwarf_Off offset = 0, next_offset = 0;
5829 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5830 offset, &next_offset, &header_size,
5831 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5832 offset = next_offset)
5833 {
5834 Dwarf_Off die_offset = offset + header_size;
5835 Dwarf_Die cu;
5836 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5837 die_offset, &cu))
5838 continue;
5839 cur_tu_die(&cu);
5840 imported_unit_points_type& imported_units =
5841 tu_die_imported_unit_points_map(source)[die_offset] =
5843 build_die_parent_relations_under(&cu, source, imported_units);
5844 }
5845
5846 // Build the DIE -> parent relation for DIEs coming from the
5847 // .debug_types section.
5848 source = TYPE_UNIT_DIE_SOURCE;
5849 address_size = 0;
5850 header_size = 0;
5851 uint64_t type_signature = 0;
5852 Dwarf_Off type_offset;
5853 for (Dwarf_Off offset = 0, next_offset = 0;
5854 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5855 offset, &next_offset, &header_size,
5856 NULL, NULL, &address_size, NULL,
5857 &type_signature, &type_offset) == 0);
5858 offset = next_offset)
5859 {
5860 Dwarf_Off die_offset = offset + header_size;
5861 Dwarf_Die cu;
5862
5863 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5864 die_offset, &cu))
5865 continue;
5866 cur_tu_die(&cu);
5867 imported_unit_points_type& imported_units =
5868 tu_die_imported_unit_points_map(source)[die_offset] =
5870 build_die_parent_relations_under(&cu, source, imported_units);
5871 }
5872 }
5873};// end class reader.
5874
5875/// The type of the aggregates being compared during a DIE comparison.
5876///
5877/// This encapsulates the stack of aggregates being compared at any
5878/// single point.
5879///
5880/// This is useful to detect "comparison cycles" and thus avoid the
5881/// resulting infinite loops.
5882///
5883/// This is also useful for implementing a very important optimization
5884/// that takes place during the canonicalization
5885struct offset_pairs_stack_type
5886{
5887 // The DWARF DWARF reader that is useful for so many things.
5888 const reader& rdr_;
5889 // The set of types that are being compared. This is to speed up
5890 // searches.
5892 // The stack of types that are being compared. The top of the
5893 // stack is the back of the vector.
5895 // A map that associates a redundant type pair to the vector of
5896 // types that depends on it.
5897 offset_pair_vect_map_type redundant_types_;
5898 // A map that associates a dependant type to the vector of redundant
5899 // types it depends on.
5900 offset_pair_vect_map_type dependant_types_;
5901
5902 offset_pairs_stack_type(const reader& rdr)
5903 : rdr_ (rdr)
5904 {}
5905
5906 /// Add a pair of types being compared to the stack of aggregates
5907 /// being compared.
5908 ///
5909 /// @param p the pair of offsets of the type DIEs to consider.
5910 void
5911 add(const offset_pair_type& p)
5912 {
5913 set_.insert(p);
5914 vect_.push_back(p);
5915 }
5916
5917 /// Erase a pair of types being compared from the stack of
5918 /// aggregates being compared.
5919 ///
5920 /// @param p the pair of offsets of the type DIEs to consider.
5921 ///
5922 /// @return true iff @p was found and erased from the stack.
5923 bool
5924 erase(const offset_pair_type& p)
5925 {
5926 if (set_.erase(p))
5927 {
5928 offset_pair_vector_type::iterator i;
5929
5930 for (i = vect_.begin();i < vect_.end(); ++i)
5931 if (*i == p)
5932 break;
5933
5934 if (i != vect_.end())
5935 vect_.erase(i);
5936
5937 return true;
5938 }
5939
5940 return false;
5941 }
5942
5943 /// Test if a pair of type DIEs is part of the stack of type DIEs
5944 /// being compared.
5945 ///
5946 /// @param p the pair of offsets of the type DIEs to consider.
5947 ///
5948 /// @return true iff @p was found in the stack of types being
5949 /// compared.
5950 bool
5951 contains(const offset_pair_type &p) const
5952 {
5953 if (set_.find(p) == set_.end())
5954 return false;
5955 return true;
5956 }
5957
5958 /// Get the set of comparison pair that depends on a given
5959 /// comparison pair.
5960 ///
5961 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5962 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5963 /// words, the pair T appears in the comparison stack BEFORE the
5964 /// pair P.
5965 ///
5966 /// So, this function returns the vector of comparison pairs that
5967 /// appear in the comparison stack AFTER a given comparison pair.
5968 ///
5969 /// @param p the comparison pair to consider.
5970 ///
5971 /// @param pairs out parameter. This is filled with the comparison
5972 /// pairs that depend on @p, iff the function returns true.
5973 ///
5974 /// @return true iff comparison pairs depending on @p have been
5975 /// found and collected in @pairs.
5976 bool
5977 get_pairs_that_depend_on(const offset_pair_type& p,
5978 offset_pair_vector_type& pairs) const
5979 {
5980 bool result = false;
5981 if (!contains(p))
5982 return result;
5983
5984 // First, get an iterator on the position of 'p'.
5985 offset_pair_vector_type::const_iterator i;
5986 for (i = vect_.begin(); i != vect_.end(); ++i)
5987 if (*i == p)
5988 break;
5989
5990 if (i == vect_.end())
5991 return result;
5992
5993 // Then, harvest all the comparison pairs that come after the
5994 // position of 'p'.
5995 for (++i; i != vect_.end(); ++i)
5996 {
5997 pairs.push_back(*i);
5998 result = true;
5999 }
6000
6001 return result;
6002 }
6003
6004 /// Record the fact that a set of comparison pairs depends on a
6005 /// given comparison pair.
6006 ///
6007 /// Set a map that associates each dependant comparison pair to the
6008 /// pair it depends on.
6009 ///
6010 /// @param p the comparison pair that the set depends on.
6011 ///
6012 /// @param dependant_types the set of types that depends on @p.
6013 void
6014 record_dependant_types(const offset_pair_type& p,
6015 const offset_pair_vector_type& dependant_types)
6016 {
6017 for (auto type_pair : dependant_types)
6018 dependant_types_[type_pair].push_back(p);
6019 }
6020
6021 /// Record a comparison pair as being redundant.
6022 ///
6023 ///
6024 /// @param p the comparison pair to record as redundant.
6025 void
6026 record_redundant_type_die_pair(const offset_pair_type& p)
6027 {
6028 offset_pair_vector_type dependant_types;
6029 get_pairs_that_depend_on(p, dependant_types);
6030
6031 // First, record the relationship "p -> [pairs that depend on p]".
6032 auto it = redundant_types_.find(p);
6033 if (it == redundant_types_.end())
6034 {
6035 auto entry = std::make_pair(p, dependant_types);
6036 redundant_types_.insert(entry);
6037 }
6038 else
6039 it->second.insert(it->second.end(),
6040 dependant_types.begin(),
6041 dependant_types.end());
6042
6043 // For each dependant type pair, record the association:
6044 // dependant_pair --> [vect of redundant types]
6045 record_dependant_types(p, dependant_types);
6046 }
6047
6048 /// Test if a given pair has been detected as redundant.
6049 ///
6050 /// @param p the pair of DIEs to consider.
6051 ///
6052 /// @return iff @p is redundant.
6053 bool
6054 is_redundant(const offset_pair_type& p)
6055 {
6056 auto i = redundant_types_.find(p);
6057 if (i != redundant_types_.end())
6058 return true;
6059 return false;
6060 }
6061
6062 /// Test if a given pair is dependant on at least a redundant type.
6063 ///
6064 /// @param p the pair to consider.
6065 ///
6066 /// @return true iff @p depends on a redundant type.
6067 bool
6068 depends_on_redundant_types(const offset_pair_type& p)
6069 {
6070 auto i = dependant_types_.find(p);
6071 if (i == dependant_types_.end())
6072 return false;
6073 return true;
6074 }
6075
6076 /// Remove a redundant pair from the system.
6077 ///
6078 /// This needs updating the system to also remove the dependant
6079 /// types that depend on the redundant pair (if they depend only on
6080 /// that redundant pair).
6081 ///
6082 /// @param p the pair to consider.
6083 ///
6084 /// @param erase_canonical_die_offset if true then erase the cached
6085 /// comparison results for the redundant pair and its dependant
6086 /// types.
6087 void
6088 erase_redundant_type_pair_entry(const offset_pair_type& p,
6089 bool erase_cached_results = false)
6090 {
6091 // First, update the dependant types that depend on the redundant
6092 // type pair
6093 auto redundant_type = redundant_types_.find(p);
6094 if (redundant_type != redundant_types_.end())
6095 {
6096 for (auto dependant_type : redundant_type->second)
6097 {
6098 // Each dependant_type depends on the redundant type 'p',
6099 // among others.
6100 auto dependant_types_it = dependant_types_.find(dependant_type);
6101 ABG_ASSERT(dependant_types_it != dependant_types_.end());
6102 // Erase the redundant type 'p' from the redundant types
6103 // that dependant_type depends on.
6104 {
6105 auto i = dependant_types_it->second.begin();
6106 for (; i!= dependant_types_it->second.end();++i)
6107 if (*i == p)
6108 break;
6109 if (i != dependant_types_it->second.end())
6110 dependant_types_it->second.erase(i);
6111 }
6112 // If the dependant type itself doesn't depend on ANY
6113 // redundant type anymore, then remove the depend type
6114 // from the map of the dependant types.
6115 if (dependant_types_it->second.empty())
6116 {
6117 if (erase_cached_results)
6118 rdr_.die_comparison_results_.erase(dependant_type);
6119 dependant_types_.erase(dependant_types_it);
6120 }
6121 }
6122 }
6123 if (erase_cached_results)
6124 rdr_.die_comparison_results_.erase(p);
6125 redundant_types_.erase(p);
6126 }
6127
6128 /// If a comparison pair has been detected as redundant, stop
6129 /// tracking it as well as its dependant pairs. That will
6130 /// essentially make it impossible to reset/cancel the canonical
6131 /// propagated types for those depdant pairs, but will also save
6132 /// ressources.
6133 ///
6134 /// @param p the comparison pair to consider.
6135 void
6136 confirm_canonical_propagated_type(const offset_pair_type& p)
6137 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
6138
6139 /// Walk the types that depend on a comparison pair and cancel their
6140 /// canonical-propagate-type, that means remove their canonical
6141 /// types and mark them as not being canonically-propagated. Also,
6142 /// erase their cached comparison results that was likely set to
6143 /// COMPARISON_RESULT_UNKNOWN.
6144 ///
6145 /// @param p the pair to consider.
6146 void
6147 cancel_canonical_propagated_type(const offset_pair_type& p)
6148 {
6149 offset_pair_set_type dependant_types;
6150 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
6151 for (auto dependant_type : dependant_types)
6152 {
6153 // If this dependant type was canonical-type-propagated then
6154 // erase that canonical type.
6155 if (rdr_.propagated_types_.find(dependant_type)
6156 != rdr_.propagated_types_.end())
6157 {
6158 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
6159 dependant_type.first.source_,
6160 /*die_as_type=*/true);
6161 rdr_.propagated_types_.erase(dependant_type);
6162 rdr_.cancelled_propagation_count_++;
6163 }
6164 // Update the cached result. We know the comparison result
6165 // must now be different.
6166 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
6167 if (comp_result_it != rdr_.die_comparison_results_.end())
6168 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6169 }
6170
6171 // Update the cached result of the root type to cancel too.
6172 auto comp_result_it = rdr_.die_comparison_results_.find(p);
6173 if (comp_result_it != rdr_.die_comparison_results_.end())
6174 {
6175 // At this point, the result of p is either
6176 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
6177 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
6178 // Make sure it's the cached result is now
6179 // COMPARISON_RESULT_DIFFERENT.
6180 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
6181 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6182 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
6183 }
6184
6185 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
6186 {
6187 rdr_.erase_canonical_die_offset(p.first.offset_,
6188 p.first.source_,
6189 /*die_as_type=*/true);
6190 rdr_.propagated_types_.erase(p);
6191 rdr_.cancelled_propagation_count_++;
6192 }
6193 }
6194
6195 /// Get the set of comparison pairs that depend on a given pair.
6196 ///
6197 /// @param p the pair to consider.
6198 ///
6199 /// @param result this is set to the pairs that depend on @p, iff
6200 /// the function returned true.
6201 ///
6202 /// @param transitive_closure if set to true, the transitive closure
6203 /// of the @result is set to it.
6204 ///
6205 /// @return true iff @result could be filled with the dependant
6206 /// types.
6207 bool
6208 get_dependant_types(const offset_pair_type& p,
6209 offset_pair_set_type& result,
6210 bool transitive_closure = false)
6211 {
6212 auto i = redundant_types_.find(p);
6213 if (i != redundant_types_.end())
6214 {
6215 for (auto dependant_type : i->second)
6216 if (result.find(dependant_type) == result.end())
6217 {
6218 result.insert(dependant_type);
6219 if (transitive_closure)
6220 get_dependant_types(p, result, /*transitive_closure=*/true);
6221 }
6222 return true;
6223 }
6224 return false;
6225 }
6226}; // end struct offset_pairs_stack_type
6227
6228static type_or_decl_base_sptr
6229build_ir_node_from_die(reader& rdr,
6230 Dwarf_Die* die,
6231 scope_decl* scope,
6232 bool called_from_public_decl,
6233 size_t where_offset,
6234 bool is_declaration_only = true,
6235 bool is_required_decl_spec = false);
6236
6237static type_or_decl_base_sptr
6238build_ir_node_from_die(reader& rdr,
6239 Dwarf_Die* die,
6240 bool called_from_public_decl,
6241 size_t where_offset);
6242
6243static decl_base_sptr
6244build_ir_node_for_void_type(reader& rdr);
6245
6246static type_or_decl_base_sptr
6247build_ir_node_for_void_pointer_type(reader& rdr);
6248
6249static class_decl_sptr
6250add_or_update_class_type(reader& rdr,
6251 Dwarf_Die* die,
6252 scope_decl* scope,
6253 bool is_struct,
6254 class_decl_sptr klass,
6255 bool called_from_public_decl,
6256 size_t where_offset,
6257 bool is_declaration_only);
6258
6259static union_decl_sptr
6260add_or_update_union_type(reader& rdr,
6261 Dwarf_Die* die,
6262 scope_decl* scope,
6263 union_decl_sptr union_type,
6264 bool called_from_public_decl,
6265 size_t where_offset,
6266 bool is_declaration_only);
6267
6268static decl_base_sptr
6269build_ir_node_for_void_type(reader& rdr);
6270
6271static decl_base_sptr
6272build_ir_node_for_variadic_parameter_type(reader &rdr);
6273
6274static function_decl_sptr
6275build_function_decl(reader& rdr,
6276 Dwarf_Die* die,
6277 size_t where_offset,
6279
6280static bool
6281function_is_suppressed(const reader& rdr,
6282 const scope_decl* scope,
6283 Dwarf_Die *function_die,
6284 bool is_declaration_only);
6285
6286static function_decl_sptr
6287build_or_get_fn_decl_if_not_suppressed(reader& rdr,
6288 scope_decl *scope,
6289 Dwarf_Die *die,
6290 size_t where_offset,
6291 bool is_declaration_only,
6293
6294static var_decl_sptr
6295build_var_decl(reader& rdr,
6296 Dwarf_Die *die,
6297 size_t where_offset,
6298 var_decl_sptr result = var_decl_sptr());
6299
6300static var_decl_sptr
6301build_or_get_var_decl_if_not_suppressed(reader& rdr,
6302 scope_decl *scope,
6303 Dwarf_Die *die,
6304 size_t where_offset,
6305 bool is_declaration_only,
6307 bool is_required_decl_spec = false);
6308static bool
6309variable_is_suppressed(const reader& rdr,
6310 const scope_decl* scope,
6311 Dwarf_Die *variable_die,
6312 bool is_declaration_only,
6313 bool is_required_decl_spec = false);
6314
6315static void
6316finish_member_function_reading(Dwarf_Die* die,
6317 const function_decl_sptr& f,
6318 const class_or_union_sptr klass,
6319 reader& rdr);
6320
6321/// Test if a given DIE is anonymous
6322///
6323/// @param die the DIE to consider.
6324///
6325/// @return true iff @p die is anonymous.
6326static bool
6327die_is_anonymous(const Dwarf_Die* die)
6328{
6329 Dwarf_Attribute attr;
6330 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
6331 return true;
6332 return false;
6333}
6334
6335/// Test if a DIE is an anonymous data member, aka, "unnamed field".
6336///
6337/// Unnamed fields are specified at
6338/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
6339///
6340/// @param die the DIE to consider.
6341///
6342/// @return true iff @p die is an anonymous data member.
6343static bool
6344die_is_anonymous_data_member(const Dwarf_Die* die)
6345{
6346 if (!die
6347 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
6348 || !die_name(die).empty())
6349 return false;
6350
6351 Dwarf_Die type_die;
6352 if (!die_die_attribute(die, DW_AT_type, type_die))
6353 return false;
6354
6355 if (dwarf_tag(&type_die) != DW_TAG_structure_type
6356 && dwarf_tag(&type_die) != DW_TAG_union_type)
6357 return false;
6358
6359 return true;
6360}
6361
6362/// Get the value of an attribute that is supposed to be a string, or
6363/// an empty string if the attribute could not be found.
6364///
6365/// @param die the DIE to get the attribute value from.
6366///
6367/// @param attr_name the attribute name. Must come from dwarf.h and
6368/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6369///
6370/// @return the string representing the value of the attribute, or an
6371/// empty string if no string attribute could be found.
6372static string
6373die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
6374{
6375 if (!die)
6376 return "";
6377
6378 Dwarf_Attribute attr;
6379 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6380 return "";
6381
6382 const char* str = dwarf_formstring(&attr);
6383 return str ? str : "";
6384}
6385
6386/// Get the value of an attribute that is supposed to be a string, or
6387/// an empty string if the attribute could not be found.
6388///
6389/// @param die the DIE to get the attribute value from.
6390///
6391/// @param attr_name the attribute name. Must come from dwarf.h and
6392/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6393///
6394/// @return the char* representing the value of the attribute, or an
6395/// empty string if no string attribute could be found.
6396static const char*
6397die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
6398{
6399 if (!die)
6400 return nullptr;
6401
6402 Dwarf_Attribute attr;
6403 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6404 return nullptr;
6405
6406 const char* str = dwarf_formstring(&attr);
6407 return str;
6408}
6409
6410/// Get the value of an attribute that is supposed to be an unsigned
6411/// constant.
6412///
6413/// @param die the DIE to read the information from.
6414///
6415/// @param attr_name the DW_AT_* name of the attribute. Must come
6416/// from dwarf.h and be an enumerator representing an attribute like,
6417/// e.g, DW_AT_decl_line.
6418///
6419///@param cst the output parameter that is set to the value of the
6420/// attribute @p attr_name. This parameter is set iff the function
6421/// return true.
6422///
6423/// @return true if there was an attribute of the name @p attr_name
6424/// and with a value that is a constant, false otherwise.
6425static bool
6426die_unsigned_constant_attribute(const Dwarf_Die* die,
6427 unsigned attr_name,
6428 uint64_t& cst)
6429{
6430 if (!die)
6431 return false;
6432
6433 Dwarf_Attribute attr;
6434 Dwarf_Word result = 0;
6435 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6436 || dwarf_formudata(&attr, &result))
6437 return false;
6438
6439 cst = result;
6440 return true;
6441}
6442
6443/// Read a signed constant value from a given attribute.
6444///
6445/// The signed constant expected must be of constant form.
6446///
6447/// @param die the DIE to get the attribute from.
6448///
6449/// @param attr_name the attribute name.
6450///
6451/// @param cst the resulting signed constant read.
6452///
6453/// @return true iff a signed constant attribute of the name @p
6454/// attr_name was found on the DIE @p die.
6455static bool
6456die_signed_constant_attribute(const Dwarf_Die *die,
6457 unsigned attr_name,
6458 int64_t& cst)
6459{
6460 if (!die)
6461 return false;
6462
6463 Dwarf_Attribute attr;
6464 Dwarf_Sword result = 0;
6465 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6466 || dwarf_formsdata(&attr, &result))
6467 return false;
6468
6469 cst = result;
6470 return true;
6471}
6472
6473/// Read the value of a constant attribute that is either signed or
6474/// unsigned into a array_type_def::subrange_type::bound_value value.
6475///
6476/// The bound_value instance will capture the actual signedness of the
6477/// read attribute.
6478///
6479/// @param die the DIE from which to read the value of the attribute.
6480///
6481/// @param attr_name the attribute name to consider.
6482///
6483/// @param is_signed true if the attribute value has to read as
6484/// signed.
6485///
6486/// @param value the resulting value read from attribute @p attr_name
6487/// on DIE @p die.
6488///
6489/// @return true iff DIE @p die has an attribute named @p attr_name
6490/// with a constant value.
6491static bool
6492die_constant_attribute(const Dwarf_Die *die,
6493 unsigned attr_name,
6494 bool is_signed,
6495 array_type_def::subrange_type::bound_value &value)
6496{
6497 if (!is_signed)
6498 {
6499 uint64_t l = 0;
6500 if (!die_unsigned_constant_attribute(die, attr_name, l))
6501 return false;
6502 value.set_unsigned(l);
6503 }
6504 else
6505 {
6506 int64_t l = 0;
6507 if (!die_signed_constant_attribute(die, attr_name, l))
6508 return false;
6509 value.set_signed(l);
6510 }
6511 return true;
6512}
6513
6514/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6515///
6516/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6517/// enum in dwarf.h so we have to use an unsigned int for the form,
6518/// grrr.
6519///
6520/// @param form the form to consider.
6521///
6522/// @return true iff @p form is DW_FORM_strx{1,4}.
6523static bool
6524form_is_DW_FORM_strx(unsigned form)
6525{
6526 if (form)
6527 {
6528#if defined HAVE_DW_FORM_strx1 \
6529 && defined HAVE_DW_FORM_strx2 \
6530 && defined HAVE_DW_FORM_strx3 \
6531 && defined HAVE_DW_FORM_strx4
6532 if (form == DW_FORM_strx1
6533 || form == DW_FORM_strx2
6534 || form == DW_FORM_strx3
6535 ||form == DW_FORM_strx4)
6536 return true;
6537#endif
6538 }
6539 return false;
6540}
6541
6542/// Test if a given DWARF form is DW_FORM_line_strp.
6543///
6544/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6545/// enum in dwarf.h so we have to use an unsigned int for the form,
6546/// grrr.
6547///
6548/// @param form the form to consider.
6549///
6550/// @return true iff @p form is DW_FORM_line_strp.
6551static bool
6552form_is_DW_FORM_line_strp(unsigned form)
6553{
6554 if (form)
6555 {
6556#if defined HAVE_DW_FORM_line_strp
6557 if (form == DW_FORM_line_strp)
6558 return true;
6559#endif
6560 }
6561 return false;
6562}
6563
6564/// Get the value of a DIE attribute; that value is meant to be a
6565/// flag.
6566///
6567/// @param die the DIE to get the attribute from.
6568///
6569/// @param attr_name the DW_AT_* name of the attribute. Must come
6570/// from dwarf.h and be an enumerator representing an attribute like,
6571/// e.g, DW_AT_external.
6572///
6573/// @param flag the output parameter to store the flag value into.
6574/// This is set iff the function returns true.
6575///
6576/// @param recursively if true, the function looks through the
6577/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6578/// all the way down to the initial DIE that is cloned and look on
6579/// that DIE to see if it has the @p attr_name attribute.
6580///
6581/// @return true if the DIE has a flag attribute named @p attr_name,
6582/// false otherwise.
6583static bool
6584die_flag_attribute(const Dwarf_Die* die,
6585 unsigned attr_name,
6586 bool& flag,
6587 bool recursively = true)
6588{
6589 Dwarf_Attribute attr;
6590 if (recursively
6591 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6592 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6593 return false;
6594
6595 bool f = false;
6596 if (dwarf_formflag(&attr, &f))
6597 return false;
6598
6599 flag = f;
6600 return true;
6601}
6602
6603/// Get the mangled name from a given DIE.
6604///
6605/// @param die the DIE to read the mangled name from.
6606///
6607/// @return the mangled name if it's present in the DIE, or just an
6608/// empty string if it's not.
6609static string
6610die_linkage_name(const Dwarf_Die* die)
6611{
6612 if (!die)
6613 return "";
6614
6615 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6616 if (linkage_name.empty())
6617 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6618 return linkage_name;
6619}
6620
6621/// Get the file path that is the value of the DW_AT_decl_file
6622/// attribute on a given DIE, if the DIE is a decl DIE having that
6623/// attribute.
6624///
6625/// @param die the DIE to consider.
6626///
6627/// @return a string containing the file path that is the logical
6628/// value of the DW_AT_decl_file attribute. If the DIE @p die
6629/// doesn't have a DW_AT_decl_file attribute, then the return value is
6630/// just an empty string.
6631static string
6632die_decl_file_attribute(const Dwarf_Die* die)
6633{
6634 if (!die)
6635 return "";
6636
6637 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6638
6639 return str ? str : "";
6640}
6641
6642/// Get the value of an attribute which value is supposed to be a
6643/// reference to a DIE.
6644///
6645/// @param die the DIE to read the value from.
6646///
6647/// @param attr_name the DW_AT_* attribute name to read.
6648///
6649/// @param result the DIE resulting from reading the attribute value.
6650/// This is set iff the function returns true.
6651///
6652/// @param recursively if true, the function looks through the
6653/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6654/// all the way down to the initial DIE that is cloned and look on
6655/// that DIE to see if it has the @p attr_name attribute.
6656///
6657/// @return true if the DIE @p die contains an attribute named @p
6658/// attr_name that is a DIE reference, false otherwise.
6659static bool
6660die_die_attribute(const Dwarf_Die* die,
6661 unsigned attr_name,
6662 Dwarf_Die& result,
6663 bool recursively)
6664{
6665 Dwarf_Attribute attr;
6666 if (recursively
6667 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6668 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6669 return false;
6670
6671 return dwarf_formref_die(&attr, &result);
6672}
6673
6674/// Get the DIE that is the "origin" of the current one.
6675///
6676/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6677/// attribute. Those DIEs represent a concrete instance of an
6678/// abstract entity. The concrete instance can be a concrete instance
6679/// of an inline function, or the concrete implementation of an
6680/// abstract interface. On both cases, we call the abstract instance
6681/// from which the concrete instance derives the "origin".
6682///
6683/// This function returns the ultimate origin DIE of a given DIE by
6684/// following the chain of its DW_AT_abstract_origin and
6685/// DW_AT_specification attributes.
6686///
6687/// @param die the DIE to consider.
6688///
6689/// @param origin_die this is an output parameter that is set by this
6690/// function to the resulting origin DIE iff the function returns
6691/// true.
6692///
6693/// @return true iff the function actually found an origin DIE and
6694/// set it to the @p origin_die parameter.
6695static bool
6696die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6697{
6698 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6699 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6700 {
6701 while (die_die_attribute(&origin_die,
6702 DW_AT_specification,
6703 origin_die, true)
6704 || die_die_attribute(&origin_die,
6705 DW_AT_abstract_origin,
6706 origin_die, true))
6707 {
6708 // Keep looking for the origin die ...
6709 ;
6710 }
6711 return true;
6712 }
6713 return false;
6714}
6715
6716/// Test if a subrange DIE indirectly references another subrange DIE
6717/// through a given attribute.
6718///
6719/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6720/// attribute be a reference to either a data member or a variable
6721/// which type is itself a DW_TAG_subrange_type. This latter subrange
6722/// DIE is said to be "indirectly referenced" by the former subrange
6723/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6724/// the value we want for the DW_AT_upper_bound of the former.
6725///
6726/// This function tests if the former subrange DIE does indirectly
6727/// reference another subrange DIE through a given attribute (not
6728/// necessarily DW_AT_upper_bound).
6729///
6730/// @param die the DIE to consider. Note that It must be a
6731/// DW_TAG_subrange_type.
6732///
6733/// @param attr_name the name of the attribute to look through for the
6734/// indirectly referenced subrange DIE.
6735///
6736/// @param referenced_subrange if the function returns true, then the
6737/// argument of this parameter is set to the indirectly referenced
6738/// DW_TAG_subrange_type DIE.
6739///
6740/// @return true iff @p DIE indirectly references a subrange DIE
6741/// through the attribute @p attr_name.
6742static bool
6743subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6744 unsigned attr_name,
6745 Dwarf_Die& referenced_subrange)
6746{
6747 bool result = false;
6748
6749 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6750 return result;
6751
6752 Dwarf_Die referenced_die;
6753 if (die_die_attribute(die, attr_name, referenced_die))
6754 {
6755 unsigned tag = dwarf_tag(&referenced_die);
6756 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6757 {
6758 Dwarf_Die type_die;
6759 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6760 {
6761 tag = dwarf_tag(&type_die);
6762 if (tag == DW_TAG_subrange_type)
6763 {
6764 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6765 result = true;
6766 }
6767 }
6768 }
6769 }
6770 return result;
6771}
6772
6773/// Return the bound value of subrange die by looking at an indirectly
6774/// referenced subrange DIE.
6775///
6776/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6777/// attribute be a reference to either a data member or a variable
6778/// which type is itself a DW_TAG_subrange_type. This latter subrange
6779/// DIE is said to be "indirectly referenced" by the former subrange
6780/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6781/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6782///
6783/// This function gets the DW_AT_{lower,upper}_bound value of a
6784/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6785/// the indirectly referenced subrange type, if it exists.
6786///
6787/// @param die the subrange DIE to consider.
6788///
6789/// @param attr_name the name of the attribute to consider, typically,
6790/// DW_AT_{lower,upper}_bound.
6791///
6792/// @param v the found value, iff this function returned true.
6793///
6794/// @param is_signed, this is set to true if @p v is signed. This
6795/// parameter is set at all only if the function returns true.
6796///
6797/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6798/// indirectly referenced subrange type.
6799static bool
6800subrange_die_indirect_bound_value(const Dwarf_Die *die,
6801 unsigned attr_name,
6802 array_type_def::subrange_type::bound_value& v,
6803 bool& is_signed)
6804{
6805 bool result = false;
6806
6807 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6808 return result;
6809
6810 Dwarf_Die subrange_die;
6811 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6812 subrange_die))
6813 {
6814 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6815 result = true;
6816 }
6817 return result;
6818}
6819
6820/// Read and return an addresss class attribute from a given DIE.
6821///
6822/// @param die the DIE to consider.
6823///
6824/// @param attr_name the name of the address class attribute to read
6825/// the value from.
6826///
6827/// @param the resulting address.
6828///
6829/// @return true iff the attribute could be read, was of the expected
6830/// address class and could thus be translated into the @p result.
6831static bool
6832die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6833{
6834 Dwarf_Attribute attr;
6835 if (!dwarf_attr_integrate(die, attr_name, &attr))
6836 return false;
6837 return dwarf_formaddr(&attr, &result) == 0;
6838}
6839
6840/// Returns the source location associated with a decl DIE.
6841///
6842/// @param rdr the @ref reader to use.
6843///
6844/// @param die the DIE the read the source location from.
6845///
6846/// @return the location associated with @p die.
6847static location
6848die_location(const reader& rdr, const Dwarf_Die* die)
6849{
6850 if (!die)
6851 return location();
6852
6853 string file = die_decl_file_attribute(die);
6854 uint64_t line = 0;
6855 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6856
6857 if (!file.empty() && line != 0)
6858 {
6859 translation_unit_sptr tu = rdr.cur_transl_unit();
6860 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6861 return l;
6862 }
6863 return location();
6864}
6865
6866/// Return a copy of the name of a DIE.
6867///
6868/// @param die the DIE to consider.
6869///
6870/// @return a copy of the name of the DIE.
6871static string
6872die_name(const Dwarf_Die* die)
6873{
6874 string name = die_string_attribute(die, DW_AT_name);
6875 return name;
6876}
6877
6878/// Return the location, the name and the mangled name of a given DIE.
6879///
6880/// @param rdr the DWARF reader to use.
6881///
6882/// @param die the DIE to read location and names from.
6883///
6884/// @param loc the location output parameter to set.
6885///
6886/// @param name the name output parameter to set.
6887///
6888/// @param linkage_name the linkage_name output parameter to set.
6889static void
6890die_loc_and_name(const reader& rdr,
6891 Dwarf_Die* die,
6892 location& loc,
6893 string& name,
6894 string& linkage_name)
6895{
6896 loc = die_location(rdr, die);
6897 name = die_name(die);
6898 linkage_name = die_linkage_name(die);
6899}
6900
6901/// Return the name and the mangled name of a given DIE.
6902///
6903/// @param die the DIE to read location and names from.
6904///
6905/// @param name the name output parameter to set.
6906///
6907/// @param linkage_name the linkage_name output parameter to set.
6908static void
6909die_name_and_linkage_name(const Dwarf_Die* die,
6910 string& name,
6911 string& linkage_name)
6912{
6913 name = die_name(die);
6914 linkage_name = die_linkage_name(die);
6915}
6916
6917/// Get the size of a (type) DIE as the value for the parameter
6918/// DW_AT_byte_size or DW_AT_bit_size.
6919///
6920/// @param die the DIE to read the information from.
6921///
6922/// @param size the resulting size in bits. This is set iff the
6923/// function return true.
6924///
6925/// @return true if the size attribute was found.
6926static bool
6927die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6928{
6929 if (!die)
6930 return false;
6931
6932 uint64_t byte_size = 0, bit_size = 0;
6933
6934 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6935 {
6936 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6937 return false;
6938 }
6939 else
6940 bit_size = byte_size * 8;
6941
6942 size = bit_size;
6943
6944 return true;
6945}
6946
6947/// Get the access specifier (from the DW_AT_accessibility attribute
6948/// value) of a given DIE.
6949///
6950/// @param die the DIE to consider.
6951///
6952/// @param access the resulting access. This is set iff the function
6953/// returns true.
6954///
6955/// @return bool if the DIE contains the DW_AT_accessibility die.
6956static bool
6957die_access_specifier(Dwarf_Die * die, access_specifier& access)
6958{
6959 if (!die)
6960 return false;
6961
6962 uint64_t a = 0;
6963 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6964 return false;
6965
6966 access_specifier result = private_access;
6967
6968 switch (a)
6969 {
6970 case private_access:
6971 result = private_access;
6972 break;
6973
6974 case protected_access:
6975 result = protected_access;
6976 break;
6977
6978 case public_access:
6979 result = public_access;
6980 break;
6981
6982 default:
6983 break;
6984 }
6985
6986 access = result;
6987 return true;
6988}
6989
6990/// Test whether a given DIE represents a decl that is public. That
6991/// is, one with the DW_AT_external attribute set.
6992///
6993/// @param die the DIE to consider for testing.
6994///
6995/// @return true if a DW_AT_external attribute is present and its
6996/// value is set to the true; return false otherwise.
6997static bool
6998die_is_public_decl(const Dwarf_Die* die)
6999{
7000 if (!die)
7001 return false;
7002 bool is_public = false;
7003
7004 // If this is a DW_TAG_subprogram DIE, look for the
7005 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
7006 // then it's public. In all other cases, this should return false.
7007
7008 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7009 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
7010 die_flag_attribute(die, DW_AT_external, is_public);
7011 else if (tag == DW_TAG_namespace)
7012 {
7013 string name = die_name(die);
7014 is_public = !name.empty();
7015 }
7016
7017 return is_public;
7018}
7019
7020/// Test if a DIE is effectively public.
7021///
7022/// This is meant to return true when either the DIE is public or when
7023/// it's a variable DIE that is at (global) namespace level.
7024///
7025/// @return true iff either the DIE is public or is a variable DIE
7026/// that is at (global) namespace level.
7027static bool
7028die_is_effectively_public_decl(const reader& rdr,
7029 const Dwarf_Die* die)
7030{
7031 if (die_is_public_decl(die))
7032 return true;
7033
7034 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7035 if (tag == DW_TAG_variable || tag == DW_TAG_member)
7036 {
7037 // The DIE is a variable.
7038 Dwarf_Die parent_die;
7039 size_t where_offset = 0;
7040 if (!get_parent_die(rdr, die, parent_die, where_offset))
7041 return false;
7042
7043 tag = dwarf_tag(&parent_die);
7044 if (tag == DW_TAG_compile_unit
7045 || tag == DW_TAG_partial_unit
7046 || tag == DW_TAG_type_unit)
7047 // The DIE is at global scope.
7048 return true;
7049
7050 if (tag == DW_TAG_namespace)
7051 {
7052 string name = die_name(&parent_die);
7053 if (name.empty())
7054 // The DIE at unnamed namespace scope, so it's not public.
7055 return false;
7056 // The DIE is at namespace scope.
7057 return true;
7058 }
7059 }
7060 return false;
7061}
7062
7063/// Test whether a given DIE represents a declaration-only DIE.
7064///
7065/// That is, if the DIE has the DW_AT_declaration flag set.
7066///
7067/// @param die the DIE to consider.
7068//
7069/// @return true if a DW_AT_declaration is present, false otherwise.
7070static bool
7071die_is_declaration_only(Dwarf_Die* die)
7072{
7073 bool is_declaration = false;
7074 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
7075 if (is_declaration && (!die_has_size_attribute(die)
7076 || !die_has_children(die)))
7077 return true;
7078 return false;
7079}
7080
7081/// Test if a DIE is for a function decl.
7082///
7083/// @param die the DIE to consider.
7084///
7085/// @return true iff @p die represents a function decl.
7086static bool
7087die_is_function_decl(const Dwarf_Die *die)
7088{
7089 if (!die)
7090 return false;
7091
7092 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7093 if (tag == DW_TAG_subprogram)
7094 return true;
7095 return false;
7096}
7097
7098/// Test if a DIE is for a variable decl.
7099///
7100/// @param die the DIE to consider.
7101///
7102/// @return true iff @p die represents a variable decl.
7103static bool
7104die_is_variable_decl(const Dwarf_Die *die)
7105{
7106 if (!die)
7107 return false;
7108
7109 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7110 if (tag == DW_TAG_variable)
7111 return true;
7112 return false;
7113}
7114
7115/// Test if a DIE has size attribute.
7116///
7117/// @param die the DIE to consider.
7118///
7119/// @return true if the DIE has a size attribute.
7120static bool
7121die_has_size_attribute(const Dwarf_Die *die)
7122{
7123 uint64_t s;
7124 if (die_size_in_bits(die, s))
7125 return true;
7126 return false;
7127}
7128
7129/// Test that a DIE has no child DIE.
7130///
7131/// @param die the DIE to consider.
7132///
7133/// @return true iff @p die has no child DIE.
7134static bool
7135die_has_no_child(const Dwarf_Die *die)
7136{
7137 if (!die)
7138 return true;
7139
7140 Dwarf_Die child;
7141 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7142 return false;
7143 return true;
7144}
7145
7146/// Test whether a given DIE represents a declaration-only DIE.
7147///
7148/// That is, if the DIE has the DW_AT_declaration flag set.
7149///
7150/// @param die the DIE to consider.
7151//
7152/// @return true if a DW_AT_declaration is present, false otherwise.
7153static bool
7154die_is_declaration_only(const Dwarf_Die* die)
7155{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
7156
7157/// Tests whether a given DIE is artificial.
7158///
7159/// @param die the test to test for.
7160///
7161/// @return true if the DIE is artificial, false otherwise.
7162static bool
7163die_is_artificial(Dwarf_Die* die)
7164{
7165 bool is_artificial;
7166 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
7167}
7168
7169///@return true if a tag represents a type, false otherwise.
7170///
7171///@param tag the tag to consider.
7172static bool
7173is_type_tag(unsigned tag)
7174{
7175 bool result = false;
7176
7177 switch (tag)
7178 {
7179 case DW_TAG_array_type:
7180 case DW_TAG_class_type:
7181 case DW_TAG_enumeration_type:
7182 case DW_TAG_pointer_type:
7183 case DW_TAG_reference_type:
7184 case DW_TAG_string_type:
7185 case DW_TAG_structure_type:
7186 case DW_TAG_subroutine_type:
7187 case DW_TAG_typedef:
7188 case DW_TAG_union_type:
7189 case DW_TAG_ptr_to_member_type:
7190 case DW_TAG_set_type:
7191 case DW_TAG_subrange_type:
7192 case DW_TAG_base_type:
7193 case DW_TAG_const_type:
7194 case DW_TAG_file_type:
7195 case DW_TAG_packed_type:
7196 case DW_TAG_thrown_type:
7197 case DW_TAG_volatile_type:
7198 case DW_TAG_restrict_type:
7199 case DW_TAG_interface_type:
7200 case DW_TAG_unspecified_type:
7201 case DW_TAG_shared_type:
7202 case DW_TAG_rvalue_reference_type:
7203 case DW_TAG_coarray_type:
7204 case DW_TAG_atomic_type:
7205 case DW_TAG_immutable_type:
7206 result = true;
7207 break;
7208
7209 default:
7210 result = false;
7211 break;
7212 }
7213
7214 return result;
7215}
7216
7217/// Test if a given DIE is a type whose canonical type is to be
7218/// propagated during DIE canonicalization
7219///
7220/// This is a sub-routine of compare_dies.
7221///
7222/// @param tag the tag of the DIE to consider.
7223///
7224/// @return true iff the DIE of tag @p tag is can see its canonical
7225/// type be propagated during the type comparison that happens during
7226/// DIE canonicalization.
7227static bool
7228is_canon_type_to_be_propagated_tag(unsigned tag)
7229{
7230 bool result = false;
7231
7232 switch (tag)
7233 {
7234 case DW_TAG_class_type:
7235 case DW_TAG_structure_type:
7236 case DW_TAG_union_type:
7237 case DW_TAG_subroutine_type:
7238 case DW_TAG_subprogram:
7239 result = true;
7240 break;
7241
7242 default:
7243 result = false;
7244 break;
7245 }
7246
7247 return result;
7248}
7249
7250/// Test if a given kind of DIE ought to have its comparison result
7251/// cached by compare_dies, so that subsequent invocations of
7252/// compare_dies can be faster.
7253///
7254/// @param tag the tag of the DIE to consider.
7255///
7256/// @return true iff DIEs of the tag @p tag ought to have its
7257/// comparison results cached.
7258static bool
7259type_comparison_result_to_be_cached(unsigned tag)
7260{
7261 bool r = false;
7262 switch (tag)
7263 {
7264 case DW_TAG_class_type:
7265 case DW_TAG_structure_type:
7266 case DW_TAG_union_type:
7267 case DW_TAG_subroutine_type:
7268 case DW_TAG_subprogram:
7269 r = true;
7270 break;
7271
7272 default:
7273 r = false;
7274 break;
7275 }
7276 return r;
7277}
7278
7279/// Cache the result of comparing to type DIEs.
7280///
7281/// @param rdr the context to consider.
7282///
7283/// @param tag the tag of the DIEs to consider.
7284///
7285/// @param p the offsets of the pair of DIEs being compared.
7286///
7287/// @param result the comparison result to be cached.
7288static bool
7289maybe_cache_type_comparison_result(const reader& rdr,
7290 int tag,
7291 const offset_pair_type& p,
7292 comparison_result result)
7293{
7294 if (!type_comparison_result_to_be_cached(tag)
7295 || (result != COMPARISON_RESULT_EQUAL
7296 && result != COMPARISON_RESULT_DIFFERENT))
7297 return false;
7298
7299 rdr.die_comparison_results_[p] = result;
7300
7301 return true;
7302
7303}
7304
7305/// Get the cached result of the comparison of a pair of DIEs.
7306///
7307/// @param rdr the context to consider.
7308///
7309/// @param tag the tag of the pair of DIEs to consider.
7310///
7311/// @param p the offsets of the pair of DIEs to consider.
7312///
7313/// @param result out parameter set to the cached result of the
7314/// comparison of @p p if it has been found.
7315///
7316/// @return true iff a cached result for the comparisonof @p has been
7317/// found and set into @p result.
7318static bool
7319get_cached_type_comparison_result(const reader& rdr,
7320 const offset_pair_type& p,
7321 comparison_result& result)
7322{
7323 auto i = rdr.die_comparison_results_.find(p);
7324 if (i != rdr.die_comparison_results_.end())
7325 {
7326 result = i->second;
7327 return true;
7328 }
7329 return false;
7330}
7331
7332/// Get the cached result of the comparison of a pair of DIEs, if the
7333/// kind of DIEs ought to have its comparison results cached.
7334///
7335/// @param rdr the context to consider.
7336///
7337/// @param tag the tag of the pair of DIEs to consider.
7338///
7339/// @param p the offsets of the pair of DIEs to consider.
7340///
7341/// @param result out parameter set to the cached result of the
7342/// comparison of @p p if it has been found.
7343///
7344/// @return true iff a cached result for the comparisonof @p has been
7345/// found and set into @p result.
7346static bool
7347maybe_get_cached_type_comparison_result(const reader& rdr,
7348 int tag,
7349 const offset_pair_type& p,
7350 comparison_result& result)
7351{
7352 if (type_comparison_result_to_be_cached(tag))
7353 {
7354 // Types of this kind might have their comparison result cached
7355 // when they are not canonicalized. So let's see if we have a
7356 // cached comparison result.
7357 if (get_cached_type_comparison_result(rdr, p, result))
7358 return true;
7359 }
7360 return false;
7361}
7362
7363/// Test if a given DIE is to be canonicalized.
7364///
7365/// @param die the DIE to consider.
7366///
7367/// @return true iff @p die is to be canonicalized.
7368static bool
7369is_type_die_to_be_canonicalized(const Dwarf_Die *die)
7370{
7371 bool result = false;
7372 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7373
7374 if (!is_type_tag(tag))
7375 return false;
7376
7377 switch (tag)
7378 {
7379 case DW_TAG_class_type:
7380 case DW_TAG_structure_type:
7381 case DW_TAG_union_type:
7382 result = !die_is_declaration_only(die);
7383 break;
7384
7385 case DW_TAG_subroutine_type:
7386 case DW_TAG_subprogram:
7387 case DW_TAG_array_type:
7388 result = true;
7389
7390 default:
7391 break;
7392 }
7393
7394 return result;
7395}
7396
7397/// Test if a DIE tag represents a declaration.
7398///
7399/// @param tag the DWARF tag to consider.
7400///
7401/// @return true iff @p tag is for a declaration.
7402static bool
7403is_decl_tag(unsigned tag)
7404{
7405 switch (tag)
7406 {
7407 case DW_TAG_formal_parameter:
7408 case DW_TAG_imported_declaration:
7409 case DW_TAG_member:
7410 case DW_TAG_unspecified_parameters:
7411 case DW_TAG_subprogram:
7412 case DW_TAG_variable:
7413 case DW_TAG_namespace:
7414 case DW_TAG_GNU_template_template_param:
7415 case DW_TAG_GNU_template_parameter_pack:
7416 case DW_TAG_GNU_formal_parameter_pack:
7417 return true;
7418 }
7419 return false;
7420}
7421
7422/// Test if a DIE represents a type DIE.
7423///
7424/// @param die the DIE to consider.
7425///
7426/// @return true if @p die represents a type, false otherwise.
7427static bool
7428die_is_type(const Dwarf_Die* die)
7429{
7430 if (!die)
7431 return false;
7432 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7433}
7434
7435/// Test if a DIE represents a declaration.
7436///
7437/// @param die the DIE to consider.
7438///
7439/// @return true if @p die represents a decl, false otherwise.
7440static bool
7441die_is_decl(const Dwarf_Die* die)
7442{
7443 if (!die)
7444 return false;
7445 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7446}
7447
7448/// Test if a DIE represents a namespace.
7449///
7450/// @param die the DIE to consider.
7451///
7452/// @return true if @p die represents a namespace, false otherwise.
7453static bool
7454die_is_namespace(const Dwarf_Die* die)
7455{
7456 if (!die)
7457 return false;
7458 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7459}
7460
7461/// Test if a DIE has tag DW_TAG_unspecified_type.
7462///
7463/// @param die the DIE to consider.
7464///
7465/// @return true if @p die has tag DW_TAG_unspecified_type.
7466static bool
7467die_is_unspecified(Dwarf_Die* die)
7468{
7469 if (!die)
7470 return false;
7471 return (dwarf_tag(die) == DW_TAG_unspecified_type);
7472}
7473
7474/// Test if a DIE represents a void type.
7475///
7476/// @param die the DIE to consider.
7477///
7478/// @return true if @p die represents a void type, false otherwise.
7479static bool
7480die_is_void_type(Dwarf_Die* die)
7481{
7482 if (!die || dwarf_tag(die) != DW_TAG_base_type)
7483 return false;
7484
7485 string name = die_name(die);
7486 if (name == "void")
7487 return true;
7488
7489 return false;
7490}
7491
7492/// Test if a DIE represents a pointer type.
7493///
7494/// @param die the die to consider.
7495///
7496/// @return true iff @p die represents a pointer type.
7497static bool
7498die_is_pointer_type(const Dwarf_Die* die)
7499{
7500 if (!die)
7501 return false;
7502
7503 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7504 if (tag == DW_TAG_pointer_type)
7505 return true;
7506
7507 return false;
7508}
7509
7510/// Test if a DIE is for a pointer, reference or qualified type to
7511/// anonymous class or struct.
7512///
7513/// @param die the DIE to consider.
7514///
7515/// @return true iff @p is for a pointer, reference or qualified type
7516/// to anonymous class or struct.
7517static bool
7518pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7519{
7520 if (!die_is_pointer_array_or_reference_type(die)
7521 && !die_is_qualified_type(die))
7522 return false;
7523
7524 Dwarf_Die underlying_type_die;
7525 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7526 return false;
7527
7528 if (!die_is_class_type(&underlying_type_die))
7529 return false;
7530
7531 string name = die_name(&underlying_type_die);
7532
7533 return name.empty();
7534}
7535
7536/// Test if a DIE represents a reference type.
7537///
7538/// @param die the die to consider.
7539///
7540/// @return true iff @p die represents a reference type.
7541static bool
7542die_is_reference_type(const Dwarf_Die* die)
7543{
7544 if (!die)
7545 return false;
7546
7547 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7548 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7549 return true;
7550
7551 return false;
7552}
7553
7554/// Test if a DIE represents an array type.
7555///
7556/// @param die the die to consider.
7557///
7558/// @return true iff @p die represents an array type.
7559static bool
7560die_is_array_type(const Dwarf_Die* die)
7561{
7562 if (!die)
7563 return false;
7564
7565 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7566 if (tag == DW_TAG_array_type)
7567 return true;
7568
7569 return false;
7570}
7571
7572/// Test if a DIE represents a pointer, reference or array type.
7573///
7574/// @param die the die to consider.
7575///
7576/// @return true iff @p die represents a pointer or reference type.
7577static bool
7578die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7579{return (die_is_pointer_type(die)
7580 || die_is_reference_type(die)
7581 || die_is_array_type(die));}
7582
7583/// Test if a DIE represents a pointer or a reference type.
7584///
7585/// @param die the die to consider.
7586///
7587/// @return true iff @p die represents a pointer or reference type.
7588static bool
7589die_is_pointer_or_reference_type(const Dwarf_Die* die)
7590{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7591
7592/// Test if a DIE represents a pointer, a reference or a typedef type.
7593///
7594/// @param die the die to consider.
7595///
7596/// @return true iff @p die represents a pointer, a reference or a
7597/// typedef type.
7598static bool
7599die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7600{return (die_is_pointer_array_or_reference_type(die)
7601 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7602
7603/// Test if a DIE represents a class type.
7604///
7605/// @param die the die to consider.
7606///
7607/// @return true iff @p die represents a class type.
7608static bool
7609die_is_class_type(const Dwarf_Die* die)
7610{
7611 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7612
7613 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7614 return true;
7615
7616 return false;
7617}
7618
7619/// Test if a DIE is for a qualified type.
7620///
7621/// @param die the DIE to consider.
7622///
7623/// @return true iff @p die is for a qualified type.
7624static bool
7625die_is_qualified_type(const Dwarf_Die* die)
7626{
7627 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7628 if (tag == DW_TAG_const_type
7629 || tag == DW_TAG_volatile_type
7630 || tag == DW_TAG_restrict_type)
7631 return true;
7632
7633 return false;
7634}
7635
7636/// Test if a DIE is for a function type.
7637///
7638/// @param die the DIE to consider.
7639///
7640/// @return true iff @p die is for a function type.
7641static bool
7642die_is_function_type(const Dwarf_Die *die)
7643{
7644 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7645 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7646 return true;
7647
7648 return false;
7649}
7650
7651/// Test if a DIE for a function pointer or member function has an
7652/// DW_AT_object_pointer attribute.
7653///
7654/// @param die the DIE to consider.
7655///
7656/// @param object_pointer out parameter. It's set to the DIE for the
7657/// object pointer iff the function returns true.
7658///
7659/// @return true iff the DIE @p die has an object pointer. In that
7660/// case, the parameter @p object_pointer is set to the DIE of that
7661/// object pointer.
7662static bool
7663die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7664{
7665 if (!die)
7666 return false;
7667
7668 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7669 return true;
7670
7671 return false;
7672}
7673
7674/// Test if a DIE has children DIEs.
7675///
7676/// @param die the DIE to consider.
7677///
7678/// @return true iff @p DIE has at least one child node.
7679static bool
7680die_has_children(const Dwarf_Die* die)
7681{
7682 if (!die)
7683 return false;
7684
7685 Dwarf_Die child;
7686 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7687 return true;
7688
7689 return false;
7690}
7691
7692/// Get the DIE representing the first parameter of the function
7693/// denoted by a given DIE.
7694///
7695/// @param die the function DIE to consider. Note that if this
7696/// parameter is neither a DW_TAG_subprogram nor a
7697/// DW_TAG_subroutine_type, then the current process is aborted.
7698///
7699/// @param first_parm_die output parameter. This is set to the DIE of
7700/// the first parameter of the function denoted by @p die. This
7701/// output parameter is set iff the function returns true.
7702///
7703/// @return true iff the first parameter of the function denoted by @p
7704/// die is returned in output parameter @p first_parm_die.
7705static bool
7706fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die)
7707{
7708 if (!die)
7709 return false;
7710
7711 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7712 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7713
7714 Dwarf_Die child;
7715 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7716 {
7717 int child_tag = dwarf_tag(&child);
7718 if (child_tag == DW_TAG_formal_parameter)
7719 {
7720 memcpy(&first_parm_die, &child, sizeof(Dwarf_Die));
7721 return true;
7722 }
7723 }
7724 return false;
7725}
7726
7727/// Test if a member function denoted by a given DIE has a parameter
7728/// which is a "this pointer".
7729///
7730/// Please note that if the member function denotes a static member
7731/// function or if the DIE does not denote a member function to begin
7732/// with, then the function will return false because no "this
7733/// pointer" will be found.
7734///
7735/// @param rdr the current DWARF reader in use.
7736///
7737/// @param die the DIE of the member function this function should
7738/// inspect.
7739///
7740/// @param where_offset where in the DIE stream we logically are.
7741///
7742/// @param class_die output parameter. This is set iff a "this
7743/// pointer" was found as the first parameters of the member function
7744/// denoted by @p die, and thus the function returns true If set, this
7745/// then points to the DIE of the class containing the member function
7746/// denoted by @p die.
7747///
7748/// @param object_pointer_die output parameter. This is set to the
7749/// DIE of the function parameter that carries the "this pointe".
7750/// This is set iff this function return true.
7751///
7752/// @return true iff the first parameter of the member function
7753/// denoted by @p die points to a "this pointer".
7754static bool
7755member_fn_die_has_this_pointer(const reader& rdr,
7756 const Dwarf_Die* die,
7757 size_t where_offset,
7758 Dwarf_Die& class_die,
7759 Dwarf_Die& object_pointer_die)
7760{
7761 if (!die)
7762 return false;
7763
7764 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7765 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
7766 return false;
7767
7768 if (tag == DW_TAG_subprogram
7769 && !die_is_at_class_scope(rdr, die, where_offset, class_die))
7770 return false;
7771
7772 Dwarf_Die first_parm_die;
7773 Dwarf_Die parm_type_die;
7774 if (die_has_object_pointer(die, object_pointer_die))
7775 {
7776 // This can be either a member function with a
7777 // DW_AT_object_pointer attribute or a DW_TAG_subroutine_type
7778 // with a DW_AT_object_pointer. In the later case, we are
7779 // looking at a member function type.
7780 memcpy(&first_parm_die, &object_pointer_die, sizeof(Dwarf_Die));
7781 if (!die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7782 return false;
7783 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7784 die_peel_typedef(&parm_type_die, parm_type_die);
7785 }
7786 else if (fn_die_first_parameter_die(die, first_parm_die))
7787 {
7788 memcpy(&object_pointer_die, &first_parm_die, sizeof(Dwarf_Die));
7789 bool is_artificial = false;
7790 if (die_flag_attribute(&first_parm_die, DW_AT_artificial, is_artificial))
7791 {
7792 if (die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7793 {
7794 tag = dwarf_tag(&parm_type_die);
7795 if (tag == DW_TAG_pointer_type)
7796 {
7797 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7798 die_peel_typedef(&parm_type_die, parm_type_die);
7799 }
7800 else
7801 return false;
7802 }
7803 else
7804 return false;
7805 }
7806 else
7807 return false;
7808 }
7809 else
7810 return false;
7811
7812 tag = dwarf_tag(&parm_type_die);
7813 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7814 {
7815 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7816 return true;
7817 }
7818 return false;
7819}
7820
7821/// When given the object pointer DIE of a function type or member
7822/// function DIE, this function returns the "this" pointer that points
7823/// to the associated class.
7824///
7825/// @param die the DIE of the object pointer of the function or member
7826/// function to consider.
7827///
7828/// @param this_pointer_die out parameter. This is set to the DIE of
7829/// the "this" pointer iff the function returns true.
7830///
7831/// @return true iff the function found the "this" pointer from the
7832/// object pointer DIE @p die. In that case, the parameter @p
7833/// this_pointer_die is set to the DIE of that "this" pointer.
7834static bool
7835die_this_pointer_from_object_pointer(Dwarf_Die* die,
7836 Dwarf_Die& this_pointer_die)
7837{
7838 ABG_ASSERT(die);
7839 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7840
7841 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7842 return true;
7843
7844 return false;
7845}
7846
7847/// Test if a given "this" pointer that points to a particular class
7848/// type is for a const class or not. If it's for a const class, then
7849/// it means the function type or the member function associated to
7850/// that "this" pointer is const.
7851///
7852/// @param dye the DIE of the "this" pointer to consider.
7853///
7854/// @return true iff @p die points to a const class type.
7855static bool
7856die_this_pointer_is_const(Dwarf_Die* dye)
7857{
7858 ABG_ASSERT(dye);
7859
7860 Dwarf_Die die;
7861 memcpy(&die, dye, sizeof(Dwarf_Die));
7862 if (dwarf_tag(&die) == DW_TAG_const_type)
7863 ABG_ASSERT(die_peel_qualified(&die, die));
7864
7865 if (dwarf_tag(&die) == DW_TAG_pointer_type)
7866 {
7867 Dwarf_Die pointed_to_type_die;
7868 if (die_die_attribute(&die, DW_AT_type, pointed_to_type_die))
7869 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7870 return true;
7871 }
7872
7873 return false;
7874}
7875
7876/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7877/// attribute) points to a const implicit class and so is for a const
7878/// method or or a const member function type.
7879///
7880/// @param die the DIE of the object pointer to consider.
7881///
7882/// @return true iff the object pointer represented by @p die is for a
7883/// a const method or const member function type.
7884static bool
7885die_object_pointer_is_for_const_method(Dwarf_Die* die)
7886{
7887 ABG_ASSERT(die);
7888 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7889
7890 Dwarf_Die this_pointer_die;
7891 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7892 if (die_this_pointer_is_const(&this_pointer_die))
7893 return true;
7894
7895 return false;
7896}
7897
7898/// Test if a DIE represents an entity that is at class scope.
7899///
7900/// @param rdr the DWARF reader to use.
7901///
7902/// @param die the DIE to consider.
7903///
7904/// @param where_offset where we are logically at in the DIE stream.
7905///
7906/// @param class_scope_die out parameter. Set to the DIE of the
7907/// containing class iff @p die happens to be at class scope; that is,
7908/// iff the function returns true.
7909///
7910/// @return true iff @p die is at class scope. In that case, @p
7911/// class_scope_die is set to the DIE of the class that contains @p
7912/// die.
7913static bool
7914die_is_at_class_scope(const reader& rdr,
7915 const Dwarf_Die* die,
7916 size_t where_offset,
7917 Dwarf_Die& class_scope_die)
7918{
7919 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7920 return false;
7921
7922 int tag = dwarf_tag(&class_scope_die);
7923
7924 return (tag == DW_TAG_structure_type
7925 || tag == DW_TAG_class_type
7926 || tag == DW_TAG_union_type);
7927}
7928
7929/// Return the leaf object under a pointer, reference or qualified
7930/// type DIE.
7931///
7932/// @param die the DIE of the type to consider.
7933///
7934/// @param peeled_die out parameter. Set to the DIE of the leaf
7935/// object iff the function actually peeled anything.
7936///
7937/// @return true upon successful completion.
7938static bool
7939die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7940{
7941 if (!die)
7942 return false;
7943
7944 int tag = dwarf_tag(die);
7945
7946 if (tag == DW_TAG_const_type
7947 || tag == DW_TAG_volatile_type
7948 || tag == DW_TAG_restrict_type
7949 || tag == DW_TAG_pointer_type
7950 || tag == DW_TAG_reference_type
7951 || tag == DW_TAG_rvalue_reference_type)
7952 {
7953 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7954 return false;
7955 }
7956 else
7957 return false;
7958
7959 memcpy(&peeled_die, die, sizeof(peeled_die));
7960
7961 while (tag == DW_TAG_const_type
7962 || tag == DW_TAG_volatile_type
7963 || tag == DW_TAG_restrict_type
7964 || tag == DW_TAG_pointer_type
7965 || tag == DW_TAG_reference_type
7966 || tag == DW_TAG_rvalue_reference_type)
7967 {
7968 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7969 break;
7970 tag = dwarf_tag(&peeled_die);
7971 }
7972
7973 return true;
7974}
7975
7976/// Return the leaf object under a qualified type DIE.
7977///
7978/// @param die the DIE of the type to consider.
7979///
7980/// @param peeled_die out parameter. Set to the DIE of the leaf
7981/// object iff the function actually peeled anything.
7982///
7983/// @return true upon successful completion.
7984static bool
7985die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7986{
7987 if (!die)
7988 return false;
7989
7990 memcpy(&peeled_die, die, sizeof(peeled_die));
7991
7992 int tag = dwarf_tag(&peeled_die);
7993
7994 bool result = false;
7995 while (tag == DW_TAG_const_type
7996 || tag == DW_TAG_volatile_type
7997 || tag == DW_TAG_restrict_type)
7998 {
7999 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8000 break;
8001 tag = dwarf_tag(&peeled_die);
8002 result = true;
8003 }
8004
8005 return result;
8006}
8007
8008/// Return the leaf object under a typedef type DIE.
8009///
8010/// @param die the DIE of the type to consider.
8011///
8012/// @param peeled_die out parameter. Set to the DIE of the leaf
8013/// object iff the function actually peeled anything.
8014///
8015/// @return true upon successful completion.
8016static bool
8017die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
8018{
8019 if (!die)
8020 return false;
8021
8022 int tag = dwarf_tag(die);
8023
8024 memcpy(&peeled_die, die, sizeof(peeled_die));
8025
8026 if (tag == DW_TAG_typedef)
8027 {
8028 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8029 return false;
8030 }
8031 else
8032 return false;
8033
8034 while (tag == DW_TAG_typedef)
8035 {
8036 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8037 break;
8038 tag = dwarf_tag(&peeled_die);
8039 }
8040
8041 return true;
8042
8043}
8044
8045/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
8046///
8047/// @param die the DIE to consider.
8048///
8049/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
8050/// iff the function returned true.
8051///
8052/// @return true iff the function could peel @p die.
8053static bool
8054die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
8055{
8056 if (!die)
8057 return false;
8058
8059 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8060
8061 if (tag == DW_TAG_pointer_type
8062 || tag == DW_TAG_reference_type
8063 || tag == DW_TAG_rvalue_reference_type
8064 || tag == DW_TAG_typedef)
8065 {
8066 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8067 return false;
8068 }
8069 else
8070 return false;
8071
8072 while (tag == DW_TAG_pointer_type
8073 || tag == DW_TAG_reference_type
8074 || tag == DW_TAG_rvalue_reference_type
8075 || tag == DW_TAG_typedef)
8076 {
8077 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8078 break;
8079 tag = dwarf_tag(&peeled_die);
8080 }
8081 return true;
8082}
8083
8084/// Test if a DIE for a function type represents a method type.
8085///
8086/// @param rdr the DWARF reader.
8087///
8088/// @param die the DIE to consider.
8089///
8090/// @param where_offset where we logically are in the stream of DIEs.
8091///
8092/// @param object_pointer_die out parameter. This is set by the
8093/// function to the DIE that refers to the formal function parameter
8094/// which holds the implicit "this" pointer of the method. That die
8095/// is called the object pointer DIE. This is set iff the member
8096/// function is a non-static member function and if the function
8097/// returns true. In other words, this is only set if the is_static
8098/// out parameter is set to false and the function returns true.
8099///
8100/// @param class_die out parameter. This is set by the function to
8101/// the DIE that represents the class of the method type. This is set
8102/// iff the function returns true.
8103///
8104/// @param is_static out parameter. This is set to true by the
8105/// function if @p die is a static method or a the type of a static
8106/// method. This is set iff the function returns true.
8107///
8108/// @return true iff @p die is a DIE for a method type.
8109static bool
8110die_function_type_is_method_type(const reader& rdr,
8111 const Dwarf_Die *die,
8112 size_t where_offset,
8113 Dwarf_Die& object_pointer_die,
8114 Dwarf_Die& class_die,
8115 bool& is_static)
8116{
8117 if (!die)
8118 return false;
8119
8120 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8121 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
8122
8123 if (member_fn_die_has_this_pointer(rdr, die, where_offset, class_die, object_pointer_die))
8124 {
8125 is_static = false;
8126 return true;
8127 }
8128 else if (die_is_at_class_scope(rdr, die, where_offset, class_die))
8129 {
8130 is_static = true;
8131 return true;
8132 }
8133
8134 return false;
8135}
8136
8137enum virtuality
8138{
8139 VIRTUALITY_NOT_VIRTUAL,
8140 VIRTUALITY_VIRTUAL,
8141 VIRTUALITY_PURE_VIRTUAL
8142};
8143
8144/// Get the virtual-ness of a given DIE, that is, the value of the
8145/// DW_AT_virtuality attribute.
8146///
8147/// @param die the DIE to read from.
8148///
8149/// @param virt the resulting virtuality attribute. This is set iff
8150/// the function returns true.
8151///
8152/// @return true if the virtual-ness could be determined.
8153static bool
8154die_virtuality(const Dwarf_Die* die, virtuality& virt)
8155{
8156 if (!die)
8157 return false;
8158
8159 uint64_t v = 0;
8160 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
8161
8162 if (v == DW_VIRTUALITY_virtual)
8163 virt = VIRTUALITY_VIRTUAL;
8164 else if (v == DW_VIRTUALITY_pure_virtual)
8165 virt = VIRTUALITY_PURE_VIRTUAL;
8166 else
8167 virt = VIRTUALITY_NOT_VIRTUAL;
8168
8169 return true;
8170}
8171
8172/// Test whether the DIE represent either a virtual base or function.
8173///
8174/// @param die the DIE to consider.
8175///
8176/// @return bool if the DIE represents a virtual base or function,
8177/// false othersise.
8178static bool
8179die_is_virtual(const Dwarf_Die* die)
8180{
8181 virtuality v;
8182 if (!die_virtuality(die, v))
8183 return false;
8184
8185 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
8186}
8187
8188/// Test if the DIE represents an entity that was declared inlined.
8189///
8190/// @param die the DIE to test for.
8191///
8192/// @return true if the DIE represents an entity that was declared
8193/// inlined.
8194static bool
8195die_is_declared_inline(Dwarf_Die* die)
8196{
8197 uint64_t inline_value = 0;
8198 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
8199 return false;
8200 return (inline_value == DW_INL_declared_inlined
8201 || inline_value == DW_INL_declared_not_inlined);
8202}
8203
8204/// Compare two DWARF strings using the most accurate (and slowest)
8205/// method possible.
8206///
8207/// @param l the DIE that carries the first string to consider, as an
8208/// attribute value.
8209///
8210/// @param attr_name the name of the attribute which value is the
8211/// string to compare.
8212///
8213/// @return true iff the string carried by @p l equals the one carried
8214/// by @p r.
8215static bool
8216slowly_compare_strings(const Dwarf_Die *l,
8217 const Dwarf_Die *r,
8218 unsigned attr_name)
8219{
8220 const char *l_str = die_char_str_attribute(l, attr_name),
8221 *r_str = die_char_str_attribute(r, attr_name);
8222 if (!l_str && !r_str)
8223 return true;
8224 return l_str && r_str && !strcmp(l_str, r_str);
8225}
8226
8227/// This function is a fast routine (optimization) to compare the
8228/// values of two string attributes of two DIEs.
8229///
8230/// @param l the first DIE to consider.
8231///
8232/// @param r the second DIE to consider.
8233///
8234/// @param attr_name the name of the attribute to compare, on the two
8235/// DIEs above.
8236///
8237/// @param result out parameter. This is set to the result of the
8238/// comparison. If the value of attribute @p attr_name on DIE @p l
8239/// equals the value of attribute @p attr_name on DIE @p r, then the
8240/// the argument of this parameter is set to true. Otherwise, it's
8241/// set to false. Note that the argument of this parameter is set iff
8242/// the function returned true.
8243///
8244/// @return true iff the comparison could be performed. There are
8245/// cases in which the comparison cannot be performed. For instance,
8246/// if one of the DIEs does not have the attribute @p attr_name. In
8247/// any case, if this function returns true, then the parameter @p
8248/// result is set to the result of the comparison.
8249static bool
8250compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
8251 unsigned attr_name,
8252 bool &result)
8253{
8254 Dwarf_Attribute l_attr, r_attr;
8255 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
8256 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
8257 return false;
8258
8259 ABG_ASSERT(l_attr.form == DW_FORM_strp
8260 || l_attr.form == DW_FORM_string
8261 || l_attr.form == DW_FORM_GNU_strp_alt
8262 || form_is_DW_FORM_strx(l_attr.form)
8263 || form_is_DW_FORM_line_strp(l_attr.form));
8264
8265 ABG_ASSERT(r_attr.form == DW_FORM_strp
8266 || r_attr.form == DW_FORM_string
8267 || r_attr.form == DW_FORM_GNU_strp_alt
8268 || form_is_DW_FORM_strx(r_attr.form)
8269 || form_is_DW_FORM_line_strp(r_attr.form));
8270
8271 if ((l_attr.form == DW_FORM_strp
8272 && r_attr.form == DW_FORM_strp)
8273 || (l_attr.form == DW_FORM_GNU_strp_alt
8274 && r_attr.form == DW_FORM_GNU_strp_alt)
8275 || (form_is_DW_FORM_strx(l_attr.form)
8276 && form_is_DW_FORM_strx(r_attr.form))
8277 || (form_is_DW_FORM_line_strp(l_attr.form)
8278 && form_is_DW_FORM_line_strp(r_attr.form)))
8279 {
8280 // So these string attributes are actually pointers into a
8281 // string table. The string table is most likely de-duplicated
8282 // so comparing the *values* of the pointers should be enough.
8283 //
8284 // This is the fast path.
8285 if (l_attr.valp == r_attr.valp)
8286 {
8287#if WITH_DEBUG_TYPE_CANONICALIZATION
8288 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
8289#endif
8290 result = true;
8291 return true;
8292 }
8293 }
8294
8295 // If we reached this point it means we couldn't use the fast path
8296 // because the string atttributes are strings that are "inline" in
8297 // the debug info section. Let's just compare them the slow and
8298 // obvious way.
8299 result = slowly_compare_strings(l, r, attr_name);
8300 return true;
8301}
8302
8303/// Compare the file path of the compilation units (aka CUs)
8304/// associated to two DIEs.
8305///
8306/// If the DIEs are for pointers or typedefs, this function also
8307/// compares the file paths of the CUs of the leaf DIEs (underlying
8308/// DIEs of the pointer or the typedef).
8309///
8310/// @param l the first type DIE to consider.
8311///
8312/// @param r the second type DIE to consider.
8313///
8314/// @return true iff the file paths of the DIEs of the two types are
8315/// equal.
8316static bool
8317compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
8318{
8319 Dwarf_Die l_cu, r_cu;
8320 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
8321 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
8322 return false;
8323
8324 bool compared =
8325 compare_dies_string_attribute_value(&l_cu, &r_cu,
8326 DW_AT_name,
8327 result);
8328 if (compared && result)
8329 {
8330 Dwarf_Die peeled_l, peeled_r;
8331 if (die_is_pointer_reference_or_typedef_type(l)
8332 && die_is_pointer_reference_or_typedef_type(r)
8333 && die_peel_pointer_and_typedef(l, peeled_l)
8334 && die_peel_pointer_and_typedef(r, peeled_r))
8335 {
8336 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
8337 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
8338 return false;
8339 compared =
8340 compare_dies_string_attribute_value(&l_cu, &r_cu,
8341 DW_AT_name,
8342 result);
8343 }
8344 }
8345
8346 return compared;
8347}
8348
8349// -----------------------------------
8350// <location expression evaluation>
8351// -----------------------------------
8352
8353/// Get the value of a given DIE attribute, knowing that it must be a
8354/// location expression.
8355///
8356/// @param die the DIE to read the attribute from.
8357///
8358/// @param attr_name the name of the attribute to read the value for.
8359///
8360/// @param expr the pointer to allocate and fill with the resulting
8361/// array of operators + operands forming a dwarf expression. This is
8362/// set iff the function returns true.
8363///
8364/// @param expr_len the length of the resulting dwarf expression.
8365/// This is set iff the function returns true.
8366///
8367/// @return true if the attribute exists and has a non-empty dwarf expression
8368/// as value. In that case the expr and expr_len arguments are set to the
8369/// resulting dwarf expression.
8370static bool
8371die_location_expr(const Dwarf_Die* die,
8372 unsigned attr_name,
8373 Dwarf_Op** expr,
8374 size_t* expr_len)
8375{
8376 if (!die)
8377 return false;
8378
8379 Dwarf_Attribute attr;
8380 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
8381 return false;
8382
8383 size_t len = 0;
8384 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
8385
8386 // Ignore location expressions where reading them succeeded but
8387 // their length is 0.
8388 result &= len > 0;
8389
8390 if (result)
8391 *expr_len = len;
8392
8393 return result;
8394}
8395
8396/// If the current operation in the dwarf expression represents a push
8397/// of a constant value onto the dwarf expr virtual machine (aka
8398/// DEVM), perform the operation and update the DEVM.
8399///
8400/// If the result of the operation is a constant, update the DEVM
8401/// accumulator with its value. Otherwise, the DEVM accumulator is
8402/// left with its previous value.
8403///
8404/// @param ops the array of the dwarf expression operations to consider.
8405///
8406/// @param ops_len the lengths of @p ops array above.
8407///
8408/// @param index the index of the operation to interpret, in @p ops.
8409///
8410/// @param next_index the index of the operation to interpret at the
8411/// next step, after this function completed and returned. This is
8412/// set an output parameter that is set iff the function returns true.
8413///
8414/// @param ctxt the DEVM evaluation context.
8415///
8416/// @return true if the current operation actually pushes a constant
8417/// value onto the DEVM stack, false otherwise.
8418static bool
8419op_pushes_constant_value(Dwarf_Op* ops,
8420 size_t ops_len,
8421 size_t index,
8422 size_t& next_index,
8423 dwarf_expr_eval_context& ctxt)
8424{
8425 ABG_ASSERT(index < ops_len);
8426
8427 Dwarf_Op& op = ops[index];
8428 int64_t value = 0;
8429
8430 switch (op.atom)
8431 {
8432 case DW_OP_addr:
8433 value = ops[index].number;
8434 break;
8435
8436 case DW_OP_const1u:
8437 case DW_OP_const1s:
8438 case DW_OP_const2u:
8439 case DW_OP_const2s:
8440 case DW_OP_const4u:
8441 case DW_OP_const4s:
8442 case DW_OP_const8u:
8443 case DW_OP_const8s:
8444 case DW_OP_constu:
8445 case DW_OP_consts:
8446 value = ops[index].number;
8447 break;
8448
8449 case DW_OP_lit0:
8450 value = 0;
8451 break;
8452 case DW_OP_lit1:
8453 value = 1;
8454 break;
8455 case DW_OP_lit2:
8456 value = 2;
8457 break;
8458 case DW_OP_lit3:
8459 value = 3;
8460 break;
8461 case DW_OP_lit4:
8462 value = 4;
8463 break;
8464 case DW_OP_lit5:
8465 value = 5;
8466 break;
8467 case DW_OP_lit6:
8468 value = 6;
8469 break;
8470 case DW_OP_lit7:
8471 value = 7;
8472 break;
8473 case DW_OP_lit8:
8474 value = 8;
8475 break;
8476 case DW_OP_lit9:
8477 value = 9;
8478 break;
8479 case DW_OP_lit10:
8480 value = 10;
8481 break;
8482 case DW_OP_lit11:
8483 value = 11;
8484 break;
8485 case DW_OP_lit12:
8486 value = 12;
8487 break;
8488 case DW_OP_lit13:
8489 value = 13;
8490 break;
8491 case DW_OP_lit14:
8492 value = 14;
8493 break;
8494 case DW_OP_lit15:
8495 value = 15;
8496 break;
8497 case DW_OP_lit16:
8498 value = 16;
8499 break;
8500 case DW_OP_lit17:
8501 value = 17;
8502 break;
8503 case DW_OP_lit18:
8504 value = 18;
8505 break;
8506 case DW_OP_lit19:
8507 value = 19;
8508 break;
8509 case DW_OP_lit20:
8510 value = 20;
8511 break;
8512 case DW_OP_lit21:
8513 value = 21;
8514 break;
8515 case DW_OP_lit22:
8516 value = 22;
8517 break;
8518 case DW_OP_lit23:
8519 value = 23;
8520 break;
8521 case DW_OP_lit24:
8522 value = 24;
8523 break;
8524 case DW_OP_lit25:
8525 value = 25;
8526 break;
8527 case DW_OP_lit26:
8528 value = 26;
8529 break;
8530 case DW_OP_lit27:
8531 value = 27;
8532 break;
8533 case DW_OP_lit28:
8534 value = 28;
8535 break;
8536 case DW_OP_lit29:
8537 value = 29;
8538 break;
8539 case DW_OP_lit30:
8540 value = 30;
8541 break;
8542 case DW_OP_lit31:
8543 value = 31;
8544 break;
8545
8546 default:
8547 return false;
8548 }
8549
8550 expr_result r(value);
8551 ctxt.push(r);
8552 ctxt.accum = r;
8553 next_index = index + 1;
8554
8555 return true;
8556}
8557
8558/// If the current operation in the dwarf expression represents a push
8559/// of a non-constant value onto the dwarf expr virtual machine (aka
8560/// DEVM), perform the operation and update the DEVM. A non-constant
8561/// is namely a quantity for which we need inferior (a running program
8562/// image) state to know the exact value.
8563///
8564/// Upon successful completion, as the result of the operation is a
8565/// non-constant the DEVM accumulator value is left to its state as of
8566/// before the invocation of this function.
8567///
8568/// @param ops the array of the dwarf expression operations to consider.
8569///
8570/// @param ops_len the lengths of @p ops array above.
8571///
8572/// @param index the index of the operation to interpret, in @p ops.
8573///
8574/// @param next_index the index of the operation to interpret at the
8575/// next step, after this function completed and returned. This is
8576/// set an output parameter that is set iff the function returns true.
8577///
8578/// @param ctxt the DEVM evaluation context.
8579///
8580/// @return true if the current operation actually pushes a
8581/// non-constant value onto the DEVM stack, false otherwise.
8582static bool
8583op_pushes_non_constant_value(Dwarf_Op* ops,
8584 size_t ops_len,
8585 size_t index,
8586 size_t& next_index,
8587 dwarf_expr_eval_context& ctxt)
8588{
8589 ABG_ASSERT(index < ops_len);
8590 Dwarf_Op& op = ops[index];
8591
8592 switch (op.atom)
8593 {
8594 case DW_OP_reg0:
8595 case DW_OP_reg1:
8596 case DW_OP_reg2:
8597 case DW_OP_reg3:
8598 case DW_OP_reg4:
8599 case DW_OP_reg5:
8600 case DW_OP_reg6:
8601 case DW_OP_reg7:
8602 case DW_OP_reg8:
8603 case DW_OP_reg9:
8604 case DW_OP_reg10:
8605 case DW_OP_reg11:
8606 case DW_OP_reg12:
8607 case DW_OP_reg13:
8608 case DW_OP_reg14:
8609 case DW_OP_reg15:
8610 case DW_OP_reg16:
8611 case DW_OP_reg17:
8612 case DW_OP_reg18:
8613 case DW_OP_reg19:
8614 case DW_OP_reg20:
8615 case DW_OP_reg21:
8616 case DW_OP_reg22:
8617 case DW_OP_reg23:
8618 case DW_OP_reg24:
8619 case DW_OP_reg25:
8620 case DW_OP_reg26:
8621 case DW_OP_reg27:
8622 case DW_OP_reg28:
8623 case DW_OP_reg29:
8624 case DW_OP_reg30:
8625 case DW_OP_reg31:
8626 next_index = index + 1;
8627 break;
8628
8629 case DW_OP_breg0:
8630 case DW_OP_breg1:
8631 case DW_OP_breg2:
8632 case DW_OP_breg3:
8633 case DW_OP_breg4:
8634 case DW_OP_breg5:
8635 case DW_OP_breg6:
8636 case DW_OP_breg7:
8637 case DW_OP_breg8:
8638 case DW_OP_breg9:
8639 case DW_OP_breg10:
8640 case DW_OP_breg11:
8641 case DW_OP_breg12:
8642 case DW_OP_breg13:
8643 case DW_OP_breg14:
8644 case DW_OP_breg15:
8645 case DW_OP_breg16:
8646 case DW_OP_breg17:
8647 case DW_OP_breg18:
8648 case DW_OP_breg19:
8649 case DW_OP_breg20:
8650 case DW_OP_breg21:
8651 case DW_OP_breg22:
8652 case DW_OP_breg23:
8653 case DW_OP_breg24:
8654 case DW_OP_breg25:
8655 case DW_OP_breg26:
8656 case DW_OP_breg27:
8657 case DW_OP_breg28:
8658 case DW_OP_breg29:
8659 case DW_OP_breg30:
8660 case DW_OP_breg31:
8661 next_index = index + 1;
8662 break;
8663
8664 case DW_OP_regx:
8665 next_index = index + 2;
8666 break;
8667
8668 case DW_OP_fbreg:
8669 next_index = index + 1;
8670 break;
8671
8672 case DW_OP_bregx:
8673 next_index = index + 1;
8674 break;
8675
8676 case DW_OP_GNU_variable_value:
8677 next_index = index + 1;
8678 break;
8679
8680 default:
8681 return false;
8682 }
8683
8684 expr_result r(false);
8685 ctxt.push(r);
8686
8687 return true;
8688}
8689
8690/// If the current operation in the dwarf expression represents a
8691/// manipulation of the stack of the DWARF Expression Virtual Machine
8692/// (aka DEVM), this function performs the operation and updates the
8693/// state of the DEVM. If the result of the operation represents a
8694/// constant value, then the accumulator of the DEVM is set to that
8695/// result's value, Otherwise, the DEVM accumulator is left with its
8696/// previous value.
8697///
8698/// @param expr the array of the dwarf expression operations to consider.
8699///
8700/// @param expr_len the lengths of @p ops array above.
8701///
8702/// @param index the index of the operation to interpret, in @p ops.
8703///
8704/// @param next_index the index of the operation to interpret at the
8705/// next step, after this function completed and returned. This is
8706/// set an output parameter that is set iff the function returns true.
8707///
8708/// @param ctxt the DEVM evaluation context.
8709///
8710/// @return true if the current operation actually manipulates the
8711/// DEVM stack, false otherwise.
8712static bool
8713op_manipulates_stack(Dwarf_Op* expr,
8714 size_t expr_len,
8715 size_t index,
8716 size_t& next_index,
8717 dwarf_expr_eval_context& ctxt)
8718{
8719 Dwarf_Op& op = expr[index];
8720 expr_result v;
8721
8722 switch (op.atom)
8723 {
8724 case DW_OP_dup:
8725 v = ctxt.stack.front();
8726 ctxt.push(v);
8727 break;
8728
8729 case DW_OP_drop:
8730 v = ctxt.stack.front();
8731 ctxt.pop();
8732 break;
8733
8734 case DW_OP_over:
8735 ABG_ASSERT(ctxt.stack.size() > 1);
8736 v = ctxt.stack[1];
8737 ctxt.push(v);
8738 break;
8739
8740 case DW_OP_pick:
8741 ABG_ASSERT(index + 1 < expr_len);
8742 v = op.number;
8743 ctxt.push(v);
8744 break;
8745
8746 case DW_OP_swap:
8747 ABG_ASSERT(ctxt.stack.size() > 1);
8748 v = ctxt.stack[1];
8749 ctxt.stack.erase(ctxt.stack.begin() + 1);
8750 ctxt.push(v);
8751 break;
8752
8753 case DW_OP_rot:
8754 ABG_ASSERT(ctxt.stack.size() > 2);
8755 v = ctxt.stack[2];
8756 ctxt.stack.erase(ctxt.stack.begin() + 2);
8757 ctxt.push(v);
8758 break;
8759
8760 case DW_OP_deref:
8761 case DW_OP_deref_size:
8762 ABG_ASSERT(ctxt.stack.size() > 0);
8763 ctxt.pop();
8764 v.is_const(false);
8765 ctxt.push(v);
8766 break;
8767
8768 case DW_OP_xderef:
8769 case DW_OP_xderef_size:
8770 ABG_ASSERT(ctxt.stack.size() > 1);
8771 ctxt.pop();
8772 ctxt.pop();
8773 v.is_const(false);
8774 ctxt.push(v);
8775 break;
8776
8777 case DW_OP_push_object_address:
8778 v.is_const(false);
8779 ctxt.push(v);
8780 break;
8781
8782 case DW_OP_form_tls_address:
8783 case DW_OP_GNU_push_tls_address:
8784 ABG_ASSERT(ctxt.stack.size() > 0);
8785 v = ctxt.pop();
8786 if (op.atom == DW_OP_form_tls_address)
8787 v.is_const(false);
8788 ctxt.push(v);
8789 break;
8790
8791 case DW_OP_call_frame_cfa:
8792 v.is_const(false);
8793 ctxt.push(v);
8794 break;
8795
8796 default:
8797 return false;
8798 }
8799
8800 if (v.is_const())
8801 ctxt.accum = v;
8802
8803 if (op.atom == DW_OP_form_tls_address
8804 || op.atom == DW_OP_GNU_push_tls_address)
8805 ctxt.set_tls_address(true);
8806 else
8807 ctxt.set_tls_address(false);
8808
8809 next_index = index + 1;
8810
8811 return true;
8812}
8813
8814/// If the current operation in the dwarf expression represents a push
8815/// of an arithmetic or logic operation onto the dwarf expr virtual
8816/// machine (aka DEVM), perform the operation and update the DEVM.
8817///
8818/// If the result of the operation is a constant, update the DEVM
8819/// accumulator with its value. Otherwise, the DEVM accumulator is
8820/// left with its previous value.
8821///
8822/// @param expr the array of the dwarf expression operations to consider.
8823///
8824/// @param expr_len the lengths of @p expr array above.
8825///
8826/// @param index the index of the operation to interpret, in @p expr.
8827///
8828/// @param next_index the index of the operation to interpret at the
8829/// next step, after this function completed and returned. This is
8830/// set an output parameter that is set iff the function returns true.
8831///
8832/// @param ctxt the DEVM evaluation context.
8833///
8834/// @return true if the current operation actually represent an
8835/// arithmetic or logic operation.
8836static bool
8837op_is_arith_logic(Dwarf_Op* expr,
8838 size_t expr_len,
8839 size_t index,
8840 size_t& next_index,
8841 dwarf_expr_eval_context& ctxt)
8842{
8843 ABG_ASSERT(index < expr_len);
8844
8845 Dwarf_Op& op = expr[index];
8846 expr_result val1, val2;
8847 bool result = false;
8848
8849 switch (op.atom)
8850 {
8851 case DW_OP_abs:
8852 ABG_ASSERT(ctxt.stack.size() > 0);
8853 val1 = ctxt.pop();
8854 val1 = val1.abs();
8855 ctxt.push(val1);
8856 result = true;
8857 break;
8858
8859 case DW_OP_and:
8860 ABG_ASSERT(ctxt.stack.size() > 1);
8861 val1 = ctxt.pop();
8862 val2 = ctxt.pop();
8863 ctxt.push(val1 & val2);
8864 break;
8865
8866 case DW_OP_div:
8867 ABG_ASSERT(ctxt.stack.size() > 1);
8868 val1 = ctxt.pop();
8869 val2 = ctxt.pop();
8870 if (!val1.is_const())
8871 val1 = 1;
8872 ctxt.push(val2 / val1);
8873 result = true;
8874 break;
8875
8876 case DW_OP_minus:
8877 ABG_ASSERT(ctxt.stack.size() > 1);
8878 val1 = ctxt.pop();
8879 val2 = ctxt.pop();
8880 ctxt.push(val2 - val1);
8881 result = true;
8882 break;
8883
8884 case DW_OP_mod:
8885 ABG_ASSERT(ctxt.stack.size() > 1);
8886 val1 = ctxt.pop();
8887 val2 = ctxt.pop();
8888 ctxt.push(val2 % val1);
8889 result = true;
8890 break;
8891
8892 case DW_OP_mul:
8893 ABG_ASSERT(ctxt.stack.size() > 1);
8894 val1 = ctxt.pop();
8895 val2 = ctxt.pop();
8896 ctxt.push(val2 * val1);
8897 result = true;
8898 break;
8899
8900 case DW_OP_neg:
8901 ABG_ASSERT(ctxt.stack.size() > 0);
8902 val1 = ctxt.pop();
8903 ctxt.push(-val1);
8904 result = true;
8905 break;
8906
8907 case DW_OP_not:
8908 ABG_ASSERT(ctxt.stack.size() > 0);
8909 val1 = ctxt.pop();
8910 ctxt.push(~val1);
8911 result = true;
8912 break;
8913
8914 case DW_OP_or:
8915 ABG_ASSERT(ctxt.stack.size() > 1);
8916 val1 = ctxt.pop();
8917 val2 = ctxt.pop();
8918 ctxt.push(val1 | val2);
8919 result = true;
8920 break;
8921
8922 case DW_OP_plus:
8923 ABG_ASSERT(ctxt.stack.size() > 1);
8924 val1 = ctxt.pop();
8925 val2 = ctxt.pop();
8926 ctxt.push(val2 + val1);
8927 result = true;
8928 break;
8929
8930 case DW_OP_plus_uconst:
8931 ABG_ASSERT(ctxt.stack.size() > 0);
8932 val1 = ctxt.pop();
8933 val1 += op.number;
8934 ctxt.push(val1);
8935 result = true;
8936 break;
8937
8938 case DW_OP_shl:
8939 ABG_ASSERT(ctxt.stack.size() > 1);
8940 val1 = ctxt.pop();
8941 val2 = ctxt.pop();
8942 ctxt.push(val2 << val1);
8943 result = true;
8944 break;
8945
8946 case DW_OP_shr:
8947 case DW_OP_shra:
8948 ABG_ASSERT(ctxt.stack.size() > 1);
8949 val1 = ctxt.pop();
8950 val2 = ctxt.pop();
8951 ctxt.push(val2 >> val1);
8952 result = true;
8953 break;
8954
8955 case DW_OP_xor:
8956 ABG_ASSERT(ctxt.stack.size() > 1);
8957 val1 = ctxt.pop();
8958 val2 = ctxt.pop();
8959 ctxt.push(val2 ^ val1);
8960 result = true;
8961 break;
8962
8963 default:
8964 break;
8965 }
8966
8967 if (result == true)
8968 {
8969 if (ctxt.stack.front().is_const())
8970 ctxt.accum = ctxt.stack.front();
8971
8972 next_index = index + 1;
8973 }
8974 return result;;
8975}
8976
8977/// If the current operation in the dwarf expression represents a push
8978/// of a control flow operation onto the dwarf expr virtual machine
8979/// (aka DEVM), perform the operation and update the DEVM.
8980///
8981/// If the result of the operation is a constant, update the DEVM
8982/// accumulator with its value. Otherwise, the DEVM accumulator is
8983/// left with its previous value.
8984///
8985/// @param expr the array of the dwarf expression operations to consider.
8986///
8987/// @param expr_len the lengths of @p expr array above.
8988///
8989/// @param index the index of the operation to interpret, in @p expr.
8990///
8991/// @param next_index the index of the operation to interpret at the
8992/// next step, after this function completed and returned. This is
8993/// set an output parameter that is set iff the function returns true.
8994///
8995/// @param ctxt the DEVM evaluation context.
8996///
8997/// @return true if the current operation actually represents a
8998/// control flow operation, false otherwise.
8999static bool
9000op_is_control_flow(Dwarf_Op* expr,
9001 size_t expr_len,
9002 size_t index,
9003 size_t& next_index,
9004 dwarf_expr_eval_context& ctxt)
9005{
9006 ABG_ASSERT(index < expr_len);
9007
9008 Dwarf_Op& op = expr[index];
9009 expr_result val1, val2;
9010
9011 switch (op.atom)
9012 {
9013 case DW_OP_eq:
9014 case DW_OP_ge:
9015 case DW_OP_gt:
9016 case DW_OP_le:
9017 case DW_OP_lt:
9018 case DW_OP_ne:
9019 {
9020 bool value = true;
9021 val1 = ctxt.pop();
9022 val2 = ctxt.pop();
9023 if (op.atom == DW_OP_eq)
9024 value = val2 == val1;
9025 else if (op.atom == DW_OP_ge)
9026 value = val2 >= val1;
9027 else if (op.atom == DW_OP_gt)
9028 value = val2 > val1;
9029 else if (op.atom == DW_OP_le)
9030 value = val2 <= val1;
9031 else if (op.atom == DW_OP_lt)
9032 value = val2 < val1;
9033 else if (op.atom == DW_OP_ne)
9034 value = val2 != val1;
9035
9036 val1 = value ? 1 : 0;
9037 ctxt.push(val1);
9038 }
9039 break;
9040
9041 case DW_OP_skip:
9042 if (op.number > 0)
9043 index += op.number - 1;
9044 break;
9045
9046 case DW_OP_bra:
9047 val1 = ctxt.pop();
9048 if (val1.const_value() != 0)
9049 index += val1.const_value() - 1;
9050 break;
9051
9052 case DW_OP_call2:
9053 case DW_OP_call4:
9054 case DW_OP_call_ref:
9055 case DW_OP_nop:
9056 break;
9057
9058 default:
9059 return false;
9060 }
9061
9062 if (ctxt.stack.front().is_const())
9063 ctxt.accum = ctxt.stack.front();
9064
9065 next_index = index + 1;
9066 return true;
9067}
9068
9069/// This function quickly evaluates a DWARF expression that is a
9070/// constant.
9071///
9072/// This is a "fast path" function that quickly evaluates a DWARF
9073/// expression that is only made of a DW_OP_plus_uconst operator.
9074///
9075/// This is a sub-routine of die_member_offset.
9076///
9077/// @param expr the DWARF expression to evaluate.
9078///
9079/// @param expr_len the length of the expression @p expr.
9080///
9081/// @param value out parameter. This is set to the result of the
9082/// evaluation of @p expr, iff this function returns true.
9083///
9084/// @return true iff the evaluation of @p expr went OK.
9085static bool
9086eval_quickly(Dwarf_Op* expr,
9087 uint64_t expr_len,
9088 int64_t& value)
9089{
9090 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
9091 {
9092 value = expr[0].number;
9093 return true;
9094 }
9095 return false;
9096}
9097
9098/// Evaluate the value of the last sub-expression that is a constant,
9099/// inside a given DWARF expression.
9100///
9101/// @param expr the DWARF expression to consider.
9102///
9103/// @param expr_len the length of the expression to consider.
9104///
9105/// @param value the resulting value of the last constant
9106/// sub-expression of the DWARF expression. This is set iff the
9107/// function returns true.
9108///
9109/// @param is_tls_address out parameter. This is set to true iff
9110/// the resulting value of the evaluation is a TLS (thread local
9111/// storage) address.
9112///
9113/// @param eval_ctxt the evaluation context to (re)use. Note that
9114/// this function initializes this context before using it.
9115///
9116/// @return true if the function could find a constant sub-expression
9117/// to evaluate, false otherwise.
9118static bool
9119eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9120 size_t expr_len,
9121 int64_t& value,
9122 bool& is_tls_address,
9123 dwarf_expr_eval_context &eval_ctxt)
9124{
9125 // Reset the evaluation context before evaluating the constant sub
9126 // expression contained in the DWARF expression 'expr'.
9127 eval_ctxt.reset();
9128
9129 size_t index = 0, next_index = 0;
9130 do
9131 {
9132 if (op_is_arith_logic(expr, expr_len, index,
9133 next_index, eval_ctxt)
9134 || op_pushes_constant_value(expr, expr_len, index,
9135 next_index, eval_ctxt)
9136 || op_manipulates_stack(expr, expr_len, index,
9137 next_index, eval_ctxt)
9138 || op_pushes_non_constant_value(expr, expr_len, index,
9139 next_index, eval_ctxt)
9140 || op_is_control_flow(expr, expr_len, index,
9141 next_index, eval_ctxt))
9142 ;
9143 else
9144 next_index = index + 1;
9145
9146 ABG_ASSERT(next_index > index);
9147 index = next_index;
9148 } while (index < expr_len);
9149
9150 is_tls_address = eval_ctxt.set_tls_address();
9151 if (eval_ctxt.accum.is_const())
9152 {
9153 value = eval_ctxt.accum;
9154 return true;
9155 }
9156 return false;
9157}
9158
9159/// Evaluate the value of the last sub-expression that is a constant,
9160/// inside a given DWARF expression.
9161///
9162/// @param expr the DWARF expression to consider.
9163///
9164/// @param expr_len the length of the expression to consider.
9165///
9166/// @param value the resulting value of the last constant
9167/// sub-expression of the DWARF expression. This is set iff the
9168/// function returns true.
9169///
9170/// @return true if the function could find a constant sub-expression
9171/// to evaluate, false otherwise.
9172static bool
9173eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9174 size_t expr_len,
9175 int64_t& value,
9176 bool& is_tls_address)
9177{
9178 dwarf_expr_eval_context eval_ctxt;
9179 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
9180 is_tls_address, eval_ctxt);
9181}
9182
9183// -----------------------------------
9184// </location expression evaluation>
9185// -----------------------------------
9186
9187/// Convert a DW_AT_bit_offset attribute value into the same value as
9188/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
9189///
9190/// On big endian machines, the value of the DW_AT_bit_offset
9191/// attribute + 8 * the value of the DW_AT_data_member_location
9192/// attribute is the same as the value of the DW_AT_data_bit_offset
9193/// attribute.
9194///
9195/// On little endian machines however, the situation is different.
9196/// The DW_AT_bit_offset value for a bit field is the number of bits
9197/// to the left of the most significant bit of the bit field, within
9198/// the integer value at DW_AT_data_member_location.
9199///
9200/// The DW_AT_data_bit_offset offset value is the number of bits to
9201/// the right of the least significant bit of the bit field, again
9202/// relative to the containing integer value.
9203///
9204/// In other words, DW_AT_data_bit_offset is what everybody would
9205/// instinctively think of as being the "offset of the bit field". 8 *
9206/// DW_AT_data_member_location + DW_AT_bit_offset however is very
9207/// counter-intuitive on little endian machines.
9208///
9209/// This function thus reads the value of a DW_AT_bit_offset property
9210/// of a DIE and converts it into what the DW_AT_data_bit_offset would
9211/// have been if it was present, ignoring the contribution of
9212/// DW_AT_data_member_location.
9213///
9214/// Note that DW_AT_bit_offset has been made obsolete starting from
9215/// DWARF5 (for GCC; Clang still emits it).
9216///
9217/// If you like coffee and it's not too late, now might be a good time
9218/// to have a coffee break. Otherwise if it's late at night, you
9219/// might want to consider an herbal tea break. Then come back to
9220/// read this.
9221///
9222///
9223/// In what follows, the bit fields are all contained within the first
9224/// whole int of the struct, so DW_AT_data_member_location is 0.
9225///
9226/// Okay, to have a better idea of what DW_AT_bit_offset and
9227/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
9228/// have bit fields data members defined as:
9229///
9230/// struct S
9231/// {
9232/// int j:5;
9233/// int k:6;
9234/// int m:5;
9235/// int n:8;
9236/// };
9237///
9238/// The below wonderful (at least!) ASCII art sketch describes the
9239/// layout of the bitfields of 'struct S' on a little endian machine.
9240/// You need to read the sketch from the bottom-up.
9241///
9242/// So please scroll down to its bottom. Note how the 32 bits integer
9243/// word containing the bit fields is laid out with its least
9244/// significant bit starting on the right hand side, at index 0.
9245///
9246/// Then slowly scroll up starting from there, and take the time to
9247/// read each line and see how the bit fields are laid out and what
9248/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
9249/// the bit fields.
9250///
9251/// DW_AT_bit_offset(n)
9252/// < - - - - - - >
9253/// | | n |
9254/// ^ ^< - - - - >^
9255/// DW_AT_data_bit_offset(n)
9256/// < - - - - - - - - - - - - - - - >
9257/// | |
9258/// ^ ^
9259/// DW_AT_bit_offset(m)
9260/// <--------------------------------->
9261/// | | m |
9262/// ^ ^< - >^
9263/// DW_AT_data_bit_offset(m)
9264/// < - - - - - - - - - - >
9265/// | |
9266/// ^ ^
9267/// DW_AT_bit_offset(k)
9268/// <-------------------------------------------->
9269/// | | k |
9270/// ^ ^< - - >^
9271/// DW_AT_data_bit_offset(k)
9272/// < - - - - >
9273/// | |
9274/// ^ ^
9275/// DW_AT_bit_offset(j)
9276/// <-------------------------------------------------------->
9277/// | |
9278/// ^ ^
9279/// n m k j
9280/// < - - - - - - > < - - - > < - - - - > < - - - >
9281///
9282/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9283/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
9284/// 31 27 23 16 15 11 10 6 5 4 0
9285///
9286/// So, the different bit fields all fit in one 32 bits word, assuming
9287/// the bit fields are tightly packed.
9288///
9289/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
9290/// on this little endian machine and let's see how it relates to
9291/// DW_AT_data_bit_offset of j.
9292///
9293/// DW_AT_bit_offset(j) would be equal to the number of bits from the
9294/// left of the 32 bits word (i.e from bit number 31) to the most
9295/// significant bit of the j bit field (i.e, bit number 4). Thus:
9296///
9297/// DW_AT_bit_offset(j) =
9298/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
9299///
9300/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
9301/// 32 bits word (i.e, bit number 0) to the lest significant bit of
9302/// the 'j' bit field (ie, bit number 0). Thus:
9303///
9304/// DW_AT_data_bit_offset(j) = 0.
9305///
9306/// More generally, we can notice that:
9307///
9308/// sizeof_in_bits(int) =
9309/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
9310///
9311/// It follows that:
9312///
9313/// DW_AT_data_bit_offset(j) =
9314/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
9315///
9316/// Thus:
9317///
9318/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
9319///
9320/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
9321/// from the right hand side of the word. It is what we would
9322/// intuitively think it is. DW_AT_bit_offset however is super
9323/// counter-intuitive, pfff.
9324///
9325/// Anyway, this general equation holds true for all bit fields.
9326///
9327/// Similarly, it follows that:
9328///
9329/// DW_AT_bit_offset(k) =
9330/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
9331///
9332/// Thus:
9333/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
9334///
9335///
9336/// Likewise:
9337///
9338/// DW_AT_bit_offset(m) =
9339/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
9340///
9341///
9342/// Thus:
9343/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
9344///
9345/// And:
9346///
9347///
9348/// Lastly:
9349///
9350/// DW_AT_bit_offset(n) =
9351/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
9352///
9353/// Thus:
9354/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
9355///
9356/// Luckily, the body of the function is much smaller than this
9357/// comment. Enjoy!
9358///
9359/// @param die the DIE to consider.
9360///
9361/// @param is_big_endian this is true iff the machine we are looking at
9362/// is big endian.
9363///
9364/// @param offset this is the output parameter into which the value of
9365/// the DW_AT_bit_offset is put, converted as if it was the value of
9366/// the DW_AT_data_bit_offset parameter, less the contribution of
9367/// DW_AT_data_member_location. This parameter is set iff the
9368/// function returns true.
9369///
9370/// @return true if DW_AT_bit_offset was found on @p die.
9371static bool
9372read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
9373 bool is_big_endian,
9374 uint64_t &offset)
9375{
9376 uint64_t off = 0;
9377 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
9378 return false;
9379
9380 if (is_big_endian)
9381 {
9382 offset = off;
9383 return true;
9384 }
9385
9386 // Okay, we are looking at a little endian machine. We need to
9387 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
9388 // have been. To understand this, you really need to read the
9389 // preliminary comment of this function.
9390 uint64_t containing_anonymous_object_size = 0;
9391 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
9392 containing_anonymous_object_size));
9393 containing_anonymous_object_size *= 8;
9394
9395 uint64_t bitfield_size = 0;
9396 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
9397 bitfield_size));
9398
9399 // As noted in the the preliminary comment of this function if we
9400 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
9401 // its DW_AT_bit_offset value, the equation is:
9402 //
9403 // DW_AT_data_bit_offset(k) =
9404 // sizeof_in_bits(containing_anonymous_object_size)
9405 // - DW_AT_data_bit_offset(k)
9406 // - sizeof_in_bits(k)
9407 offset = containing_anonymous_object_size - off - bitfield_size;
9408
9409 return true;
9410}
9411
9412/// Get the value of the DW_AT_data_member_location of the given DIE
9413/// attribute as an constant.
9414///
9415/// @param die the DIE to read the attribute from.
9416///
9417/// @param offset the attribute as a constant value. This is set iff
9418/// the function returns true.
9419///
9420/// @return true if the attribute exists and has a constant value. In
9421/// that case the offset is set to the value.
9422static bool
9423die_constant_data_member_location(const Dwarf_Die *die,
9424 int64_t& offset)
9425{
9426 if (!die)
9427 return false;
9428
9429 Dwarf_Attribute attr;
9430 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
9431 DW_AT_data_member_location,
9432 &attr))
9433 return false;
9434
9435 Dwarf_Word val;
9436 if (dwarf_formudata(&attr, &val) != 0)
9437 return false;
9438
9439 offset = val;
9440 return true;
9441}
9442
9443/// Get the offset of a struct/class member as represented by the
9444/// value of the DW_AT_data_member_location attribute.
9445///
9446/// There is a huge gotcha in here. The value of the
9447/// DW_AT_data_member_location is not necessarily a constant that one
9448/// would just read and be done with it. Rather, it can be a DWARF
9449/// expression that one has to interpret. In general, the offset can
9450/// be given by the DW_AT_data_bit_offset or by the
9451/// DW_AT_data_member_location attribute and optionally the
9452/// DW_AT_bit_offset attribute. The bit offset attributes are
9453/// always simple constants, but the DW_AT_data_member_location
9454/// attribute is a DWARF location expression.
9455///
9456/// When it's the DW_AT_data_member_location that is present,
9457/// there are three cases to possibly take into account:
9458///
9459/// 1/ The offset in the vtable where the offset of a virtual base
9460/// can be found, aka vptr offset. Given the address of a
9461/// given object O, the vptr offset for B is given by the
9462/// (DWARF) expression:
9463///
9464/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
9465///
9466/// where VIRTUAL_OFFSET is a constant value; In this case,
9467/// this function returns the constant VIRTUAL_OFFSET, as this
9468/// is enough to detect changes in a given virtual base
9469/// relative to the other virtual bases.
9470///
9471/// 2/ The offset of a regular data member. Given the address of
9472/// a struct object named O, the memory location for a
9473/// particular data member is given by the (DWARF) expression:
9474///
9475/// address(O) + OFFSET
9476///
9477/// where OFFSET is a constant. In this case, this function
9478/// returns the OFFSET constant.
9479///
9480/// 3/ The offset of a virtual member function in the virtual
9481/// pointer. The DWARF expression is a constant that designates
9482/// the offset of the function in the vtable. In this case this
9483/// function returns that constant.
9484///
9485/// @param rdr the DWARF reader to consider.
9486///
9487/// @param die the DIE to read the information from.
9488///
9489/// @param offset the resulting constant offset, in bits. This
9490/// argument is set iff the function returns true.
9491static bool
9492die_member_offset(const reader& rdr,
9493 const Dwarf_Die* die,
9494 int64_t& offset)
9495{
9496 Dwarf_Op* expr = NULL;
9497 size_t expr_len = 0;
9498 uint64_t bit_offset = 0;
9499
9500 // First let's see if the DW_AT_data_bit_offset attribute is
9501 // present.
9502 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
9503 {
9504 offset = bit_offset;
9505 return true;
9506 }
9507
9508 // First try to read DW_AT_data_member_location as a plain constant.
9509 // We do this because the generic method using die_location_expr
9510 // might hit a bug in elfutils libdw dwarf_location_expression only
9511 // fixed in elfutils 0.184+. The bug only triggers if the attribute
9512 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
9513 // handle all constants here because that is more consistent (and
9514 // slightly faster in the general case where the attribute isn't a
9515 // full DWARF expression).
9516 if (!die_constant_data_member_location(die, offset))
9517 {
9518 // Otherwise, let's see if the DW_AT_data_member_location
9519 // attribute and, optionally, the DW_AT_bit_offset attributes
9520 // are present.
9521 if (!die_location_expr(die, DW_AT_data_member_location,
9522 &expr, &expr_len))
9523 return false;
9524
9525 // The DW_AT_data_member_location attribute is present. Let's
9526 // evaluate it and get its constant sub-expression and return
9527 // that one.
9528 if (!eval_quickly(expr, expr_len, offset))
9529 {
9530 bool is_tls_address = false;
9531 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
9532 offset, is_tls_address,
9533 rdr.dwarf_expr_eval_ctxt()))
9534 return false;
9535 }
9536 }
9537 offset *= 8;
9538
9539 // On little endian machines, we need to convert the
9540 // DW_AT_bit_offset attribute into a relative offset to 8 *
9541 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
9542 // would be if it were used instead.
9543 //
9544 // In other words, before adding it to 8 *
9545 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9546 // converted into a human-understandable form that represents the
9547 // offset of the bitfield data member it describes. For details
9548 // about the conversion, please read the extensive comments of
9549 // read_and_convert_DW_at_bit_offset.
9550 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9551 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9552 offset += bit_offset;
9553
9554 return true;
9555}
9556
9557/// Read the value of the DW_AT_location attribute from a DIE,
9558/// evaluate the resulting DWARF expression and, if it's a constant
9559/// expression, return it.
9560///
9561/// @param die the DIE to consider.
9562///
9563/// @param address the resulting constant address. This is set iff
9564/// the function returns true.
9565///
9566/// @return true iff the whole sequence of action described above
9567/// could be completed normally.
9568static bool
9569die_location_address(Dwarf_Die* die,
9570 Dwarf_Addr& address,
9571 bool& is_tls_address)
9572{
9573 Dwarf_Op* expr = NULL;
9574 size_t expr_len = 0;
9575
9576 is_tls_address = false;
9577
9578 if (!die)
9579 return false;
9580
9581 Dwarf_Attribute attr;
9582 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9583 return false;
9584
9585 if (dwarf_getlocation(&attr, &expr, &expr_len))
9586 return false;
9587 // Ignore location expressions where reading them succeeded but
9588 // their length is 0.
9589 if (expr_len == 0)
9590 return false;
9591
9592 Dwarf_Attribute result;
9593 if (!dwarf_getlocation_attr(&attr, expr, &result))
9594 // A location that has been interpreted as an address.
9595 return !dwarf_formaddr(&result, &address);
9596
9597 // Just get the address out of the number field.
9598 address = expr->number;
9599 return true;
9600}
9601
9602/// Return the index of a function in its virtual table. That is,
9603/// return the value of the DW_AT_vtable_elem_location attribute.
9604///
9605/// @param die the DIE of the function to consider.
9606///
9607/// @param vindex the resulting index. This is set iff the function
9608/// returns true.
9609///
9610/// @return true if the DIE has a DW_AT_vtable_elem_location
9611/// attribute.
9612static bool
9613die_virtual_function_index(Dwarf_Die* die,
9614 int64_t& vindex)
9615{
9616 if (!die)
9617 return false;
9618
9619 Dwarf_Op* expr = NULL;
9620 size_t expr_len = 0;
9621 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9622 &expr, &expr_len))
9623 return false;
9624
9625 int64_t i = 0;
9626 bool is_tls_addr = false;
9627 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9628 return false;
9629
9630 vindex = i;
9631 return true;
9632}
9633
9634/// Test if a given DIE represents an anonymous type.
9635///
9636/// Anonymous types we are interested in are classes, unions and
9637/// enumerations.
9638///
9639/// @param die the DIE to consider.
9640///
9641/// @return true iff @p die represents an anonymous type.
9642bool
9644{
9645 int tag = dwarf_tag(die);
9646
9647 if (tag == DW_TAG_class_type
9648 || tag == DW_TAG_structure_type
9649 || tag == DW_TAG_union_type
9650 || tag == DW_TAG_enumeration_type)
9651 return die_is_anonymous(die);
9652
9653 return false;
9654}
9655
9656/// Return the base of the internal name to represent an anonymous
9657/// type.
9658///
9659/// Typically, anonymous enums would be named
9660/// __anonymous_enum__<number>, anonymous struct or classes would be
9661/// named __anonymous_struct__<number> and anonymous unions would be
9662/// named __anonymous_union__<number>. The first part of these
9663/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9664/// the base name. This function returns that base name, depending on
9665/// the kind of type DIE we are looking at.
9666///
9667/// @param die the type DIE to look at. This function expects a type
9668/// DIE with an empty DW_AT_name property value (anonymous).
9669///
9670/// @return a string representing the base of the internal anonymous
9671/// name.
9672static string
9673get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9674{
9675 ABG_ASSERT(die_is_type(die));
9676 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9677
9678 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9679 string type_name;
9680 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9682 else if (tag == DW_TAG_union_type)
9684 else if (tag == DW_TAG_enumeration_type)
9686
9687 return type_name;
9688}
9689
9690/// Build a full internal anonymous type name.
9691///
9692/// @param base_name this is the base name as returned by the function
9693/// @ref get_internal_anonymous_die_prefix_name.
9694///
9695/// @param anonymous_type_index this is the index of the anonymous
9696/// type in its scope. That is, if there are more than one anonymous
9697/// types of a given kind in a scope, this index is what tells them
9698/// appart, starting from 0.
9699///
9700/// @return the built string, which is a concatenation of @p base_name
9701/// and @p anonymous_type_index.
9702static string
9703build_internal_anonymous_die_name(const string &base_name,
9704 size_t anonymous_type_index)
9705{
9706 string name = base_name;
9707 if (anonymous_type_index && !base_name.empty())
9708 {
9709 std::ostringstream o;
9710 o << base_name << anonymous_type_index;
9711 name = o.str();
9712 }
9713 return name;
9714}
9715
9716// ------------------------------------
9717// <DIE pretty printer>
9718// ------------------------------------
9719
9720/// Compute the qualified name of a DIE that represents a type.
9721///
9722/// For instance, if the DIE tag is DW_TAG_subprogram then this
9723/// function computes the name of the function *type*.
9724///
9725/// @param rdr the DWARF reader.
9726///
9727/// @param die the DIE to consider.
9728///
9729/// @param where_offset where in the are logically are in the DIE
9730/// stream.
9731///
9732/// @param guard the set of DIE offsets of the stack of DIEs involved
9733/// in the construction of the qualified name of the type. This set
9734/// is used to detect (and avoid) cycles in the stack of DIEs that is
9735/// going to be walked to compute the qualified type name.
9736///
9737/// @return a copy of the qualified name of the type.
9738static string
9739die_qualified_type_name(const reader& rdr,
9740 const Dwarf_Die* die,
9741 size_t where_offset,
9742 unordered_set<uint64_t>& guard)
9743{
9744 if (!die)
9745 return "";
9746
9747 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9748 if (tag == DW_TAG_compile_unit
9749 || tag == DW_TAG_partial_unit
9750 || tag == DW_TAG_type_unit)
9751 return "";
9752
9753 string name = die_name(die);
9754
9755 Dwarf_Die scope_die;
9756 if (!get_scope_die(rdr, die, where_offset, scope_die))
9757 return "";
9758
9759 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9760 string separator = colon_colon ? "::" : ".";
9761
9762 string repr;
9763
9764 switch (tag)
9765 {
9766 case DW_TAG_unspecified_type:
9767 break;
9768
9769 case DW_TAG_base_type:
9770 {
9771 abigail::ir::real_type real_type;
9772 if (parse_real_type(name, real_type))
9773 repr = real_type;
9774 else
9775 repr = name;
9776 }
9777 break;
9778
9779 case DW_TAG_typedef:
9780 ABG_ASSERT(!name.empty());
9781 // fall through
9782
9783 case DW_TAG_enumeration_type:
9784 case DW_TAG_structure_type:
9785 case DW_TAG_class_type:
9786 case DW_TAG_union_type:
9787 {
9788 if (die_is_anonymous(die))
9789 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
9790 /*one_line=*/true,
9791 /*qualed_name=*/false,
9792 where_offset, guard);
9793 else
9794 {
9795 string parent_name = die_qualified_name(rdr, &scope_die,
9796 where_offset, guard);
9797 repr = parent_name.empty() ? name : parent_name + separator + name;
9798 }
9799 }
9800 break;
9801
9802 case DW_TAG_const_type:
9803 case DW_TAG_volatile_type:
9804 case DW_TAG_restrict_type:
9805 {
9806 Dwarf_Die underlying_type_die;
9807 bool has_underlying_type_die =
9808 die_die_attribute(die, DW_AT_type, underlying_type_die);
9809
9810 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9811 break;
9812
9813 if (tag == DW_TAG_const_type)
9814 {
9815 if (has_underlying_type_die
9816 && die_is_reference_type(&underlying_type_die))
9817 // A reference is always const. So, to lower false
9818 // positive reports in diff computations, we consider a
9819 // const reference just as a reference. But we need to
9820 // keep the qualified-ness of the type. So we introduce
9821 // a 'no-op' qualifier here. Please remember that this
9822 // has to be kept in sync with what is done in
9823 // get_name_of_qualified_type. So if you change this
9824 // here, you have to change that code there too.
9825 repr = "";
9826 else if (!has_underlying_type_die
9827 || die_is_void_type(&underlying_type_die))
9828 {
9829 repr = "void";
9830 break;
9831 }
9832 else
9833 repr = "const";
9834 }
9835 else if (tag == DW_TAG_volatile_type)
9836 repr = "volatile";
9837 else if (tag == DW_TAG_restrict_type)
9838 repr = "restrict";
9839 else
9841
9842 string underlying_type_repr;
9843 if (has_underlying_type_die)
9844 underlying_type_repr =
9845 die_qualified_type_name(rdr, &underlying_type_die,
9846 where_offset, guard);
9847 else
9848 underlying_type_repr = "void";
9849
9850 if (underlying_type_repr.empty())
9851 repr.clear();
9852 else
9853 {
9854 if (has_underlying_type_die)
9855 {
9856 Dwarf_Die peeled;
9857 die_peel_qualified(&underlying_type_die, peeled);
9858 if (die_is_pointer_or_reference_type(&peeled))
9859 repr = underlying_type_repr + " " + repr;
9860 else
9861 repr += " " + underlying_type_repr;
9862 }
9863 else
9864 repr += " " + underlying_type_repr;
9865 }
9866 }
9867 break;
9868
9869 case DW_TAG_pointer_type:
9870 case DW_TAG_reference_type:
9871 case DW_TAG_rvalue_reference_type:
9872 {
9873 Dwarf_Die pointed_to_type_die;
9874 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9875 {
9876 if (tag == DW_TAG_pointer_type)
9877 repr = "void*";
9878 break;
9879 }
9880
9881 if (die_is_unspecified(&pointed_to_type_die))
9882 break;
9883
9884 string pointed_type_repr =
9885 die_qualified_type_name(rdr, &pointed_to_type_die,
9886 where_offset, guard);
9887
9888 repr = pointed_type_repr;
9889 if (repr.empty())
9890 break;
9891
9892 if (tag == DW_TAG_pointer_type)
9893 repr += "*";
9894 else if (tag == DW_TAG_reference_type)
9895 repr += "&";
9896 else if (tag == DW_TAG_rvalue_reference_type)
9897 repr += "&&";
9898 else
9900 }
9901 break;
9902
9903 case DW_TAG_subrange_type:
9904 {
9905 // In Ada, this one can be generated on its own, that is, not
9906 // as a sub-type of an array. So we need to support it on its
9907 // own. Note that when it's emitted as the sub-type of an
9908 // array like in C and C++, this is handled differently, for
9909 // now. But we try to make this usable by other languages
9910 // that are not Ada, even if we modelled it after Ada.
9911
9912 // So we build a subrange type for the sole purpose of using
9913 // the ::as_string() method of that type. So we don't add
9914 // that type to the current type tree being built.
9916 build_subrange_type(const_cast<reader&>(rdr),
9917 die, where_offset,
9918 /*associate_die_to_type=*/false);
9919 repr += s->as_string();
9920 break;
9921 }
9922
9923 case DW_TAG_array_type:
9924 {
9925 Dwarf_Die element_type_die;
9926 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9927 break;
9928 string element_type_name =
9929 die_qualified_type_name(rdr, &element_type_die, where_offset, guard);
9930 if (element_type_name.empty())
9931 break;
9932
9934 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9935 die, subranges, where_offset,
9936 /*associate_type_to_die=*/false);
9937
9938 repr = element_type_name;
9940 }
9941 break;
9942
9943 case DW_TAG_subroutine_type:
9944 case DW_TAG_subprogram:
9945 {
9946 string return_type_name;
9947 string class_name;
9948 vector<string> parm_names;
9949 bool is_const = false;
9950 bool is_static = false;
9951 bool is_method_type = false;
9952 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9953 /*pretty_print=*/true,
9954 /*qualified_name=*/true,
9956 return_type_name, class_name,
9957 parm_names, is_const,
9958 is_static, guard);
9959 if (return_type_name.empty())
9960 return_type_name = "void";
9961
9962 repr = return_type_name;
9963
9964 if (is_method_type)
9965 // This is a method, so print the class name.
9966 repr += " (" + class_name + "::*)";
9967
9968 // Now parameters.
9969 repr += " (";
9970 for (vector<string>::const_iterator i = parm_names.begin();
9971 i != parm_names.end();
9972 ++i)
9973 {
9974 if (i != parm_names.begin())
9975 repr += ", ";
9976 repr += *i;
9977 }
9978 repr += ")";
9979
9980 }
9981 break;
9982
9983 case DW_TAG_string_type:
9984 case DW_TAG_ptr_to_member_type:
9985 case DW_TAG_set_type:
9986 case DW_TAG_file_type:
9987 case DW_TAG_packed_type:
9988 case DW_TAG_thrown_type:
9989 case DW_TAG_interface_type:
9990 case DW_TAG_shared_type:
9991 break;
9992 }
9993
9994 return repr;
9995}
9996
9997/// Compute the name of a type represented by a DIE.
9998///
9999/// @param rdr the reader to use.
10000///
10001/// @param die the type DIE to consider.
10002///
10003/// @param qualified_name if true then compute a qualified name.
10004///
10005/// @param where_offset where in the are logically are in the DIE
10006/// stream.
10007///
10008/// @param guard the set of DIE offsets of the stack of DIEs involved
10009/// in the construction of the name of the type. This set is used to
10010/// detect (and avoid) cycles in the stack of DIEs that is going to be
10011/// walked to compute the type name.
10012///
10013/// @return a copy of the string representing the type represented by
10014/// @p die.
10015static string
10016die_type_name(const reader& rdr,
10017 const Dwarf_Die* die,
10018 bool qualified_name,
10019 size_t where_offset,
10020 unordered_set<uint64_t>& guard)
10021{
10022 if (!die)
10023 return "";
10024
10025 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
10026 if (tag == DW_TAG_compile_unit
10027 || tag == DW_TAG_partial_unit
10028 || tag == DW_TAG_type_unit)
10029 return "";
10030
10031 string name = die_name(die);
10032
10033 Dwarf_Die scope_die;
10034 if (!get_scope_die(rdr, die, where_offset, scope_die))
10035 return "";
10036
10037 bool colon_colon = die_is_type(die) || die_is_namespace(die);
10038 string separator = colon_colon ? "::" : ".";
10039
10040 string repr;
10041
10042 switch (tag)
10043 {
10044 case DW_TAG_unspecified_type:
10045 break;
10046
10047 case DW_TAG_base_type:
10048 {
10049 abigail::ir::real_type int_type;
10050 if (parse_real_type(name, int_type))
10051 repr = int_type;
10052 else
10053 repr = name;
10054 }
10055 break;
10056
10057 case DW_TAG_typedef:
10058 ABG_ASSERT(!name.empty());
10059 // fall through
10060
10061 case DW_TAG_enumeration_type:
10062 case DW_TAG_structure_type:
10063 case DW_TAG_class_type:
10064 case DW_TAG_union_type:
10065 {
10066 if (die_is_anonymous(die))
10067 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
10068 /*one_line=*/true,
10069 /*qualed_name=*/false,
10070 where_offset,
10071 guard);
10072 else
10073 {
10074 string parent_name;
10075 if (qualified_name)
10076 {
10077 if (!is_anonymous_type_die(&scope_die))
10078 parent_name = die_qualified_name(rdr, &scope_die,
10079 where_offset, guard);
10080 }
10081 repr = parent_name.empty() ? name : parent_name + separator + name;
10082 }
10083 }
10084 break;
10085
10086 case DW_TAG_const_type:
10087 case DW_TAG_volatile_type:
10088 case DW_TAG_restrict_type:
10089 {
10090 Dwarf_Die underlying_type_die;
10091 bool has_underlying_type_die =
10092 die_die_attribute(die, DW_AT_type, underlying_type_die);
10093
10094 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
10095 break;
10096
10097 if (tag == DW_TAG_const_type)
10098 {
10099 if (has_underlying_type_die
10100 && die_is_reference_type(&underlying_type_die))
10101 // A reference is always const. So, to lower false
10102 // positive reports in diff computations, we consider a
10103 // const reference just as a reference. But we need to
10104 // keep the qualified-ness of the type. So we introduce
10105 // a 'no-op' qualifier here. Please remember that this
10106 // has to be kept in sync with what is done in
10107 // get_name_of_qualified_type. So if you change this
10108 // here, you have to change that code there too.
10109 repr = "";
10110 else if (!has_underlying_type_die
10111 || die_is_void_type(&underlying_type_die))
10112 {
10113 repr = "void";
10114 break;
10115 }
10116 else
10117 repr = "const";
10118 }
10119 else if (tag == DW_TAG_volatile_type)
10120 repr = "volatile";
10121 else if (tag == DW_TAG_restrict_type)
10122 repr = "restrict";
10123 else
10125
10126 string underlying_type_repr;
10127 if (has_underlying_type_die)
10128 underlying_type_repr =
10129 die_type_name(rdr, &underlying_type_die,
10130 qualified_name, where_offset,
10131 guard);
10132 else
10133 underlying_type_repr = "void";
10134
10135 if (underlying_type_repr.empty())
10136 repr.clear();
10137 else
10138 {
10139 if (has_underlying_type_die)
10140 {
10141 Dwarf_Die peeled;
10142 die_peel_qualified(&underlying_type_die, peeled);
10143 if (die_is_pointer_or_reference_type(&peeled))
10144 repr = underlying_type_repr + " " + repr;
10145 else
10146 repr += " " + underlying_type_repr;
10147 }
10148 else
10149 repr += " " + underlying_type_repr;
10150 }
10151 }
10152 break;
10153
10154 case DW_TAG_pointer_type:
10155 case DW_TAG_reference_type:
10156 case DW_TAG_rvalue_reference_type:
10157 {
10158 Dwarf_Die pointed_to_type_die;
10159 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
10160 {
10161 if (tag == DW_TAG_pointer_type)
10162 repr = "void*";
10163 break;
10164 }
10165
10166 if (die_is_unspecified(&pointed_to_type_die))
10167 break;
10168
10169 string pointed_type_repr =
10170 die_type_name(rdr, &pointed_to_type_die,
10171 qualified_name, where_offset,
10172 guard);
10173
10174 repr = pointed_type_repr;
10175 if (repr.empty())
10176 break;
10177
10178 if (tag == DW_TAG_pointer_type)
10179 repr += "*";
10180 else if (tag == DW_TAG_reference_type)
10181 repr += "&";
10182 else if (tag == DW_TAG_rvalue_reference_type)
10183 repr += "&&";
10184 else
10186 }
10187 break;
10188
10189 case DW_TAG_subrange_type:
10190 {
10191 // In Ada, this one can be generated on its own, that is, not
10192 // as a sub-type of an array. So we need to support it on its
10193 // own. Note that when it's emitted as the sub-type of an
10194 // array like in C and C++, this is handled differently, for
10195 // now. But we try to make this usable by other languages
10196 // that are not Ada, even if we modelled it after Ada.
10197
10198 // So we build a subrange type for the sole purpose of using
10199 // the ::as_string() method of that type. So we don't add
10200 // that type to the current type tree being built.
10202 build_subrange_type(const_cast<reader&>(rdr),
10203 die, where_offset,
10204 /*associate_die_to_type=*/false);
10205 repr += s->as_string();
10206 break;
10207 }
10208
10209 case DW_TAG_array_type:
10210 {
10211 Dwarf_Die element_type_die;
10212 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10213 break;
10214 string element_type_name =
10215 die_type_name(rdr, &element_type_die,
10216 qualified_name, where_offset,
10217 guard);
10218 if (element_type_name.empty())
10219 break;
10220
10222 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
10223 die, subranges, where_offset,
10224 /*associate_type_to_die=*/false);
10225
10226 repr = element_type_name;
10228 }
10229 break;
10230
10231 case DW_TAG_subroutine_type:
10232 case DW_TAG_subprogram:
10233 {
10234 string return_type_name;
10235 string class_name;
10236 vector<string> parm_names;
10237 bool is_const = false;
10238 bool is_static = false;
10239 bool is_method_type = false;
10240 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10241 /*pretty_print=*/true,
10242 qualified_name,
10244 return_type_name,
10245 class_name,
10246 parm_names, is_const,
10247 is_static, guard);
10248 if (return_type_name.empty())
10249 return_type_name = "void";
10250
10251 repr = return_type_name;
10252
10253 if (is_method_type)
10254 {
10255 // This is a method, so print the class name.
10256 repr += " (" + class_name + "::*)";
10257 }
10258
10259 // Now parameters.
10260 repr += " (";
10261 for (vector<string>::const_iterator i = parm_names.begin();
10262 i != parm_names.end();
10263 ++i)
10264 {
10265 if (i != parm_names.begin())
10266 repr += ", ";
10267 repr += *i;
10268 }
10269 repr += ")";
10270
10271 }
10272 break;
10273
10274 case DW_TAG_string_type:
10275 case DW_TAG_ptr_to_member_type:
10276 case DW_TAG_set_type:
10277 case DW_TAG_file_type:
10278 case DW_TAG_packed_type:
10279 case DW_TAG_thrown_type:
10280 case DW_TAG_interface_type:
10281 case DW_TAG_shared_type:
10282 break;
10283 }
10284
10285 return repr;
10286}
10287
10288/// Compute the name of a type represented by a DIE.
10289///
10290/// @param rdr the reader to use.
10291///
10292/// @param die the type DIE to consider.
10293///
10294/// @param qualified_name if true then compute a qualified name.
10295///
10296/// @param where_offset where in the are logically are in the DIE
10297/// stream.
10298///
10299/// @return a copy of the string representing the type represented by
10300/// @p die.
10301static string
10302die_type_name(const reader& rdr,
10303 const Dwarf_Die* die,
10304 bool qualified_name,
10305 size_t where_offset)
10306{
10307 unordered_set<uint64_t> guard;
10308 return die_type_name(rdr, die, qualified_name, where_offset, guard);
10309}
10310
10311/// Compute the qualified name of a decl represented by a given DIE.
10312///
10313/// For instance, for a DIE of tag DW_TAG_subprogram this function
10314/// computes the signature of the function *declaration*.
10315///
10316/// @param rdr the DWARF reader.
10317///
10318/// @param die the DIE to consider.
10319///
10320/// @param where_offset where we are logically at in the DIE stream.
10321///
10322/// @param guard the set of DIE offsets of the stack of DIEs involved
10323/// in the construction of the qualified name of the decl. This set
10324/// is used to detect (and avoid) cycles in the stack of DIEs that is
10325/// going to be walked to compute the qualified decl name.
10326///
10327/// @return a copy of the computed name.
10328static string
10329die_qualified_decl_name(const reader& rdr,
10330 const Dwarf_Die* die,
10331 size_t where_offset,
10332 unordered_set<uint64_t>& guard)
10333{
10334 if (!die || !die_is_decl(die))
10335 return "";
10336
10337 string name = die_name(die);
10338
10339 Dwarf_Die scope_die;
10340 if (!get_scope_die(rdr, die, where_offset, scope_die))
10341 return "";
10342
10343 string scope_name = die_qualified_name(rdr, &scope_die, where_offset, guard);
10344 string separator = "::";
10345
10346 string repr;
10347
10348 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10349 switch (tag)
10350 {
10351 case DW_TAG_namespace:
10352 case DW_TAG_member:
10353 case DW_TAG_variable:
10354 repr = scope_name.empty() ? name : scope_name + separator + name;
10355 break;
10356 case DW_TAG_subprogram:
10357 repr = die_function_signature(rdr, die,
10358 /*qualified_name=*/true,
10359 where_offset, guard);
10360 break;
10361
10362 case DW_TAG_unspecified_parameters:
10363 repr = "...";
10364 break;
10365
10366 case DW_TAG_formal_parameter:
10367 case DW_TAG_imported_declaration:
10368 case DW_TAG_GNU_template_template_param:
10369 case DW_TAG_GNU_template_parameter_pack:
10370 case DW_TAG_GNU_formal_parameter_pack:
10371 break;
10372 }
10373 return repr;
10374}
10375
10376/// Compute the qualified name of the artifact represented by a given
10377/// DIE.
10378///
10379/// If the DIE represents a type, then the function computes the name
10380/// of the type. Otherwise, if the DIE represents a decl then the
10381/// function computes the name of the decl. Note that a DIE of tag
10382/// DW_TAG_subprogram is going to be considered as a "type" -- just
10383/// like if it was a DW_TAG_subroutine_type.
10384///
10385/// @param rdr the DWARF reader.
10386///
10387/// @param die the DIE to consider.
10388///
10389/// @param where_offset where we are logically at in the DIE stream.
10390///
10391/// @param guard the set of DIE offsets of the stack of DIEs involved
10392/// in the construction of the qualified name of the DIE. This set is
10393/// used to detect (and avoid) cycles in the stack of DIEs that is
10394/// going to be walked to compute the qualified DIE name.
10395///
10396/// @return a copy of the computed name.
10397static string
10398die_qualified_name(const reader& rdr, const Dwarf_Die* die,
10399 size_t where, unordered_set<uint64_t>& guard)
10400{
10401 if (die_is_type(die))
10402 return die_qualified_type_name(rdr, die, where, guard);
10403 else if (die_is_decl(die))
10404 return die_qualified_decl_name(rdr, die, where, guard);
10405 return "";
10406}
10407
10408/// Compute the qualified name of the artifact represented by a given
10409/// DIE.
10410///
10411/// If the DIE represents a type, then the function computes the name
10412/// of the type. Otherwise, if the DIE represents a decl then the
10413/// function computes the name of the decl. Note that a DIE of tag
10414/// DW_TAG_subprogram is going to be considered as a "type" -- just
10415/// like if it was a DW_TAG_subroutine_type.
10416///
10417/// @param rdr the DWARF reader.
10418///
10419/// @param die the DIE to consider.
10420///
10421/// @param where_offset where we are logically at in the DIE stream.
10422///
10423/// @return a copy of the computed name.
10424static string
10425die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
10426{
10427 unordered_set<uint64_t> guard;
10428 return die_qualified_name(rdr, die, where, guard);
10429}
10430
10431/// Test if the qualified name of a given type should be empty.
10432///
10433/// The reason why the name of a DIE with a given tag would be empty
10434/// is that libabigail's internal representation doesn't yet support
10435/// that tag; or if the DIE's qualified name is built from names of
10436/// sub-types DIEs whose tags are not yet supported.
10437///
10438/// @param rdr the DWARF reader.
10439///
10440/// @param die the DIE to consider.
10441///
10442/// @param where where we are logically at, in the DIE stream.
10443///
10444/// @param qualified_name the qualified name of the DIE. This is set
10445/// only iff the function returns false.
10446///
10447/// @param guard the set of DIE offsets of the stack of DIEs involved
10448/// in the construction of the qualified name of the type. This set
10449/// is used to detect (and avoid) cycles in the stack of DIEs that is
10450/// going to be walked to compute the qualified type name.
10451///
10452/// @return true if the qualified name of the DIE is empty.
10453static bool
10454die_qualified_type_name_empty(const reader& rdr,
10455 const Dwarf_Die* die,
10456 size_t where, string &qualified_name,
10457 unordered_set<uint64_t>& guard)
10458{
10459 if (!die)
10460 return true;
10461
10462 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10463
10464 string qname;
10465 if (tag == DW_TAG_typedef
10466 || tag == DW_TAG_pointer_type
10467 || tag == DW_TAG_reference_type
10468 || tag == DW_TAG_rvalue_reference_type
10469 || tag == DW_TAG_array_type
10470 || tag == DW_TAG_const_type
10471 || tag == DW_TAG_volatile_type
10472 || tag == DW_TAG_restrict_type)
10473 {
10474 Dwarf_Die underlying_type_die;
10475 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
10476 {
10477 string name =
10478 die_qualified_type_name(rdr, &underlying_type_die, where, guard);
10479 if (name.empty())
10480 return true;
10481 }
10482 }
10483 else
10484 {
10485 string name = die_qualified_type_name(rdr, die, where, guard);
10486 if (name.empty())
10487 return true;
10488 }
10489
10490 qname = die_qualified_type_name(rdr, die, where, guard);
10491 if (qname.empty())
10492 return true;
10493
10494 qualified_name = qname;
10495 return false;
10496}
10497
10498/// Given the DIE that represents a function type, compute the names
10499/// of the following properties the function's type:
10500///
10501/// - return type
10502/// - enclosing class (if the function is a member function)
10503/// - function parameter types
10504///
10505/// When the function we are looking at is a member function, it also
10506/// tells if it's const.
10507///
10508/// @param rdr the DWARF reader.
10509///
10510/// @param die the DIE of the function or function type we are looking
10511/// at.
10512///
10513/// @param where_offset where we are logically at in the DIE stream.
10514///
10515/// @param pretty_print if set to yes, the type names are going to be
10516/// pretty-printed names; otherwise, they are just qualified type
10517/// names.
10518///
10519/// @param qualified_name if true then the names returned are
10520/// qualified.
10521///
10522/// @param is_method_type output parameter. This is set by the
10523/// function to true iff the DIE @p die represents a method.
10524///
10525/// @param return_type_name out parameter. This contains the name of
10526/// the return type of the function.
10527///
10528/// @param class_name out parameter. If the function is a member
10529/// function, this contains the name of the enclosing class.
10530///
10531/// @param parm_names out parameter. This vector is set to the names
10532/// of the types of the parameters of the function.
10533///
10534/// @param is_const out parameter. If the function is a member
10535/// function, this is set to true iff the member function is const.
10536///
10537/// @param is_static out parameter. If the function is a static
10538/// member function, then this is set to true.
10539///
10540/// @param guard the set of DIE offsets of the stack of DIEs involved
10541/// in the construction of the qualified name of the function type.
10542/// This set is used to detect (and avoid) cycles in the stack of DIEs
10543/// that is going to be walked to compute the qualified function type
10544/// name.
10545static void
10546die_return_and_parm_names_from_fn_type_die(const reader& rdr,
10547 const Dwarf_Die* die,
10548 size_t where_offset,
10549 bool pretty_print,
10550 bool qualified_name,
10551 bool &is_method_type,
10552 string &return_type_name,
10553 string &class_name,
10554 vector<string>& parm_names,
10555 bool& is_const,
10556 bool& is_static,
10557 unordered_set<uint64_t>& guard)
10558{
10559 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10560 if (guard.find(off) != guard.end())
10561 return;
10562 guard.insert(off);
10563
10564 Dwarf_Die child;
10565 Dwarf_Die ret_type_die;
10566 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
10567 return_type_name = "void";
10568 else
10569 {
10570 return_type_name =
10571 pretty_print
10572 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset, guard)
10573 : die_type_name(rdr, &ret_type_die, qualified_name,
10574 where_offset, guard);
10575 }
10576
10577 if (return_type_name.empty())
10578 return_type_name = "void";
10579
10580 Dwarf_Die object_pointer_die, class_die;
10582 die_function_type_is_method_type(rdr, die, where_offset,
10583 object_pointer_die,
10584 class_die, is_static);
10585
10586 is_const = false;
10587 if (is_method_type)
10588 {
10589 if (!is_anonymous_type_die(&class_die))
10590 class_name = die_type_name(rdr, &class_die, qualified_name,
10591 where_offset, guard);
10592
10593 Dwarf_Die this_pointer_die;
10594 Dwarf_Die pointed_to_die;
10595 if (!is_static
10596 && die_die_attribute(&object_pointer_die, DW_AT_type,
10597 this_pointer_die))
10598 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
10599 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
10600 is_const = true;
10601
10602 string fn_name = die_name(die);
10603 string non_qualified_class_name = die_name(&class_die);
10604 bool is_ctor = fn_name == non_qualified_class_name;
10605 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
10606
10607 if (is_ctor || is_dtor)
10608 return_type_name.clear();
10609 }
10610
10611 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
10612 do
10613 {
10614 int child_tag = dwarf_tag(&child);
10615 bool first_parm = true;
10616 if (child_tag == DW_TAG_formal_parameter)
10617 {
10618 // Skip the first parameter of a method.
10619 if (first_parm)
10620 {
10621 first_parm = false;
10622 if (is_method_type)
10623 continue;
10624 }
10625 Dwarf_Die parm_type_die;
10626 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
10627 continue;
10628 string qname =
10629 pretty_print
10630 ? rdr.get_die_pretty_representation(&parm_type_die,
10631 where_offset, guard)
10632 : die_type_name(rdr, &parm_type_die,
10633 qualified_name, where_offset, guard);
10634
10635 if (qname.empty())
10636 continue;
10637 parm_names.push_back(qname);
10638 }
10639 else if (child_tag == DW_TAG_unspecified_parameters)
10640 {
10641 // This is a variadic function parameter.
10642 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
10643 // After a DW_TAG_unspecified_parameters tag, we shouldn't
10644 // keep reading for parameters. The
10645 // unspecified_parameters TAG should be the last parameter
10646 // that we record. For instance, if there are multiple
10647 // DW_TAG_unspecified_parameters DIEs then we should care
10648 // only for the first one.
10649 break;
10650 }
10651 }
10652 while (dwarf_siblingof(&child, &child) == 0);
10653
10654 if (class_name.empty())
10655 {
10656 Dwarf_Die parent_die;
10657 if (get_parent_die(rdr, die, parent_die, where_offset))
10658 {
10659 if (die_is_class_type(&parent_die)
10660 && !is_anonymous_type_die(&parent_die))
10661 class_name = die_type_name(rdr, &parent_die,
10662 qualified_name,
10663 where_offset,
10664 guard);
10665 }
10666 }
10667
10668 guard.erase(off);
10669}
10670
10671/// This computes the signature of the a function declaration
10672/// represented by a DIE.
10673///
10674/// @param rdr the DWARF reader.
10675///
10676/// @param fn_die the DIE of the function to consider.
10677///
10678/// @param qualified_name if set to true then a qualified name is
10679/// going to be computed.
10680///
10681/// @param where_offset where we are logically at in the stream of
10682/// DIEs.
10683///
10684/// @param guard the set of DIE offsets of the stack of DIEs involved
10685/// in the construction of the signature of the function type. This
10686/// set is used to detect (and avoid) cycles in the stack of DIEs that
10687/// is going to be walked to compute the signature.
10688///
10689/// @return a copy of the computed function signature string.
10690static string
10691die_function_signature(const reader& rdr,
10692 const Dwarf_Die *fn_die,
10693 bool qualified_name,
10694 size_t where_offset,
10695 unordered_set<uint64_t>& guard)
10696{
10697
10699 bool has_lang = false;
10700 if ((has_lang = get_die_language(fn_die, lang)))
10701 {
10702 // In a binary originating from the C language, it's OK to use
10703 // the linkage name of the function as a key for the map which
10704 // is meant to reduce the number of DIE comparisons involved
10705 // during DIE canonicalization computation.
10706 if (is_c_language(lang))
10707 {
10708 string fn_name = die_linkage_name(fn_die);
10709 if (fn_name.empty())
10710 fn_name = die_name(fn_die);
10711 return fn_name;
10712 }
10713 }
10714
10715 // TODO: When we can structurally compare DIEs originating from C++
10716 // as well, we can use the linkage name of functions in C++ too, to
10717 // reduce the number of comparisons involved during DIE
10718 // canonicalization.
10719
10720 string return_type_name;
10721 Dwarf_Die ret_type_die;
10722 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
10723 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
10724 where_offset,
10725 guard);
10726
10727 if (return_type_name.empty())
10728 return_type_name = "void";
10729
10730 Dwarf_Die scope_die;
10731 string scope_name;
10732 if (qualified_name && get_scope_die(rdr, fn_die, where_offset, scope_die))
10733 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset, guard);
10734 string fn_name = die_name(fn_die);
10735 if (!scope_name.empty())
10736 fn_name = scope_name + "::" + fn_name;
10737
10738 string class_name;
10739 vector<string> parm_names;
10740 bool is_const = false;
10741 bool is_static = false;
10742 bool is_method_type = false;
10743
10744 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
10745 /*pretty_print=*/false,
10746 qualified_name, is_method_type,
10747 return_type_name, class_name,
10748 parm_names, is_const, is_static,
10749 guard);
10750
10751 bool is_virtual = die_is_virtual(fn_die);
10752
10753 string repr = is_method_type? "method" : "function";
10754 if (is_virtual)
10755 repr += " virtual";
10756
10757 if (!return_type_name.empty())
10758 repr += " " + return_type_name;
10759
10760 repr += " " + fn_name;
10761
10762 // Now parameters.
10763 repr += "(";
10764 bool some_parm_emitted = false;
10765 for (vector<string>::const_iterator i = parm_names.begin();
10766 i != parm_names.end();
10767 ++i)
10768 {
10769 if (i != parm_names.begin())
10770 {
10771 if (some_parm_emitted)
10772 repr += ", ";
10773 }
10774 else
10775 if (!is_static && is_method_type)
10776 // We are printing a non-static method name, skip the implicit "this"
10777 // parameter type.
10778 continue;
10779 repr += *i;
10780 some_parm_emitted = true;
10781 }
10782 repr += ")";
10783
10784 if (is_const)
10785 {
10787 repr += " const";
10788 }
10789
10790 return repr;
10791}
10792
10793/// Compute the flat representation string of a struct, class or union
10794/// type represented by a DIE.
10795///
10796/// The flat representation string looks like:
10797/// "struct {int foo; char blah;}.
10798///
10799/// That is useful to designate a struct (class or union) that is
10800/// anonymous.
10801///
10802/// @param rdr the DWARF reader to consider.
10803///
10804/// @param die the DIE of the type to return the flat representation
10805/// for.
10806///
10807/// @param indent the indentation string to use for the
10808/// representation.
10809///
10810/// @param one_line if true then the flat representation is
10811/// constructed on one line. Otherwise, each data member is
10812/// represented on its own line.
10813///
10814/// @param qualified_names if true then the data member (and their
10815/// type) names using in the representation are qualified.
10816///
10817/// @param where_offset where in the are logically are in the DIE
10818/// stream.
10819///
10820/// @param guard the set of DIE offsets of the stack of DIEs involved
10821/// in the construction of the flat representation of the type. This
10822/// set is used to detect (and avoid) cycles in the stack of DIEs that
10823/// is going to be walked to compute the flat representation.
10824static string
10825die_class_flat_representation(const reader& rdr,
10826 const Dwarf_Die* die,
10827 const string& indent,
10828 bool one_line,
10829 bool qualified_names,
10830 size_t where_offset,
10831 unordered_set<uint64_t>& guard)
10832{
10833 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10834
10835 string repr = indent;
10836 string local_indent = " ";
10837 string real_indent;
10838
10839 if (tag == DW_TAG_union_type)
10840 repr += "union";
10841 else if (tag == DW_TAG_structure_type)
10842 repr += "struct";
10843 else if (tag == DW_TAG_class_type)
10844 repr += "class";
10845 else
10847
10848 repr += " ";
10849
10850 if (die_is_anonymous(die))
10851 {
10852 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10853 if (guard.find(off) != guard.end())
10854 {
10855 repr += "{}";
10856 return repr;
10857 }
10858 guard.insert(off);
10859 }
10860
10861 if (!die_is_anonymous(die))
10862 repr += die_qualified_name(rdr, die, where_offset, guard);
10863
10864 repr += "{";
10865
10866 if (!one_line)
10867 repr += "\n";
10868
10869 Dwarf_Die member_child_die;
10870 bool first_sibling = true;
10871 for (bool got_it = get_member_child_die(die, &member_child_die);
10872 got_it;
10873 got_it = get_next_member_sibling_die(&member_child_die,
10874 &member_child_die),
10875 first_sibling = false)
10876 {
10877 // A member of the class is either a declaration or an anonymous
10878 // type. Otherwise, let's skip it.
10879 if (!die_is_decl(&member_child_die)
10880 && !(die_is_type(&member_child_die)
10881 && die_is_anonymous(&member_child_die)))
10882 continue;
10883
10884 if (one_line)
10885 real_indent = first_sibling ? "" : " " ;
10886 else
10887 real_indent = (first_sibling ? "": "\n") + indent + local_indent;
10888
10889 repr += real_indent;
10890
10891 repr += die_pretty_print_decl(rdr, &member_child_die,
10892 qualified_names,
10893 /*include_fns=*/false,
10894 where_offset,
10895 guard);
10896 repr += ";";
10897 }
10898
10899 if (one_line)
10900 repr += "}";
10901 else
10902 repr += indent + "}";
10903
10904 if (die_is_anonymous(die))
10905 {
10906 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10907 guard.erase(off);
10908 }
10909 return repr;
10910}
10911
10912/// Compute the flat representation string of a enum type represented
10913/// by a DIE.
10914///
10915/// The flat representation string looks like:
10916/// "enum {int foo; char blah;}.
10917///
10918/// That is useful to designate an enum that is anonymous.
10919///
10920/// @param rdr the DWARF reader to consider.
10921///
10922/// @param die the DIE of the type to return the flat representation
10923/// for.
10924///
10925/// @param indent the indentation string to use for the
10926/// representation.
10927///
10928/// @param one_line if true then the flat representation is
10929/// constructed on one line. Otherwise, each data member is
10930/// represented on its own line.
10931///
10932/// @param qualified_names if true then the data member (and their
10933/// type) names using in the representation are qualified.
10934///
10935/// @param where_offset where in the are logically are in the DIE
10936/// stream.
10937static string
10938die_enum_flat_representation(const reader& rdr,
10939 const Dwarf_Die* die,
10940 const string& indent,
10941 bool one_line,
10942 bool qualified_names,
10943 size_t where_offset)
10944{
10945 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10946
10947 std::ostringstream o;
10948 string local_indent = " ";
10949 string real_indent;
10950
10951 if (tag == DW_TAG_enumeration_type)
10952 o << "enum";
10953 else
10955
10956 o << " ";
10957
10958 if (!die_is_anonymous(die))
10959 o << (qualified_names
10960 ? die_qualified_name(rdr, die, where_offset)
10961 : die_name(die));
10962
10963 o << "{";
10964
10965 if (!one_line)
10966 o << "\n";
10967
10969 Dwarf_Die child;
10970 bool first_enumerator= true;
10971 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
10972 {
10973 do
10974 {
10975 if (dwarf_tag(&child) != DW_TAG_enumerator)
10976 continue;
10977
10978 string name, m;
10979 location l;
10980 die_loc_and_name(rdr, &child, l, name, m);
10981 uint64_t val = 0;
10982 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
10983
10984 if (one_line)
10985 real_indent = first_enumerator ? "" : ", ";
10986 else
10987 real_indent = first_enumerator ? "" : ",\n" + indent + local_indent;
10988 o << name + " = " << val;
10989 first_enumerator = false;
10990 }
10991 while (dwarf_siblingof(&child, &child) == 0);
10992 }
10993
10994 o << one_line ? string("}") : "\n" + indent;
10995 o << "}";
10996
10997 return o.str();
10998}
10999
11000/// Compute the flat representation string of a class or enum type
11001/// represented by a DIE.
11002///
11003/// The flat representation string looks like:
11004/// "union {int foo; char blah;}.
11005///
11006/// That is useful to designate a class or enum type that is
11007/// anonymous.
11008///
11009/// @param rdr the DWARF reader to consider.
11010///
11011/// @param die the DIE of the type to return the flat representation
11012/// for.
11013///
11014/// @param indent the indentation string to use for the
11015/// representation.
11016///
11017/// @param one_line if true then the flat representation is
11018/// constructed on one line. Otherwise, each data member is
11019/// represented on its own line.
11020///
11021/// @param qualified_names if true then the data member (and their
11022/// type) names using in the representation are qualified.
11023///
11024/// @param where_offset where in the are logically are in the DIE
11025/// stream.
11026///
11027/// @param guard the set of DIE offsets of the stack of DIEs involved
11028/// in the construction of the flat representation of the type. This
11029/// set is used to detect (and avoid) cycles in the stack of DIEs that
11030/// is going to be walked to compute the flat representation.
11031static string
11032die_class_or_enum_flat_representation(const reader& rdr,
11033 const Dwarf_Die* die,
11034 const string& indent,
11035 bool one_line,
11036 bool qualified_names,
11037 size_t where_offset,
11038 unordered_set<uint64_t>& guard)
11039{
11040 if (!die)
11041 return string();
11042
11043 string result;
11044 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11045
11046 switch (tag)
11047 {
11048 case DW_TAG_class_type:
11049 case DW_TAG_structure_type:
11050 case DW_TAG_union_type:
11051 result = die_class_flat_representation(rdr, die, indent,
11052 one_line, qualified_names,
11053 where_offset,
11054 guard);
11055 break;
11056 case DW_TAG_enumeration_type:
11057 result = die_enum_flat_representation(rdr, die, indent,
11058 one_line, qualified_names,
11059 where_offset);
11060 break;
11061 default:
11063 }
11064
11065 return result;
11066}
11067
11068/// Compute the flat representation string of a class or enum type
11069/// represented by a DIE.
11070///
11071/// The flat representation string looks like:
11072/// "union {int foo; char blah;}.
11073///
11074/// That is useful to designate a class or enum type that is
11075/// anonymous.
11076///
11077/// @param rdr the DWARF reader to consider.
11078///
11079/// @param die the DIE of the type to return the flat representation
11080/// for.
11081///
11082/// @param indent the indentation string to use for the
11083/// representation.
11084///
11085/// @param one_line if true then the flat representation is
11086/// constructed on one line. Otherwise, each data member is
11087/// represented on its own line.
11088///
11089/// @param qualified_names if true then the data member (and their
11090/// type) names using in the representation are qualified.
11091///
11092/// @param where_offset where in the are logically are in the DIE
11093/// stream.
11094static string
11095die_class_or_enum_flat_representation(const reader& rdr,
11096 const Dwarf_Die* die,
11097 const string& indent,
11098 bool one_line,
11099 bool qualified_names,
11100 size_t where_offset)
11101{
11102 unordered_set<uint64_t> guard;
11103 return die_class_or_enum_flat_representation(rdr, die, indent,
11104 one_line, qualified_names,
11105 where_offset, guard);
11106}
11107
11108/// Return a pretty string representation of a type, for internal purposes.
11109///
11110/// By internal purpose, we mean things like key-ing types for lookup
11111/// purposes and so on.
11112///
11113/// Note that this function is also used to pretty print functions.
11114/// For functions, it prints the *type* of the function.
11115///
11116/// @param rdr the context to use.
11117///
11118/// @param the DIE of the type to pretty print.
11119///
11120/// @param where_offset where we logically are placed when calling
11121/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11122/// entries.
11123///
11124/// @param guard the set of DIE offsets of the stack of DIEs involved
11125/// in the construction of the pretty representation of the type.
11126/// This set is used to detect (and avoid) cycles in the stack of DIEs
11127/// that is going to be walked to compute the pretty representation.
11128///
11129/// @return the resulting pretty representation.
11130static string
11131die_pretty_print_type(const reader& rdr,
11132 const Dwarf_Die* die,
11133 size_t where_offset,
11134 unordered_set<uint64_t>& guard)
11135{
11136 if (!die
11137 || (!die_is_type(die)
11138 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
11139 return "";
11140
11141 string repr;
11142
11143 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11144 switch (tag)
11145 {
11146 case DW_TAG_string_type:
11147 // For now, we won't try to go get the actual representation of
11148 // the string because this would make things more complicated;
11149 // for that we'd need to interpret some location expressions to
11150 // get the length of the string. And for dynamically allocated
11151 // strings, the result of the location expression evaluation
11152 // might not even be a constant. So at the moment I consider
11153 // this to be a lot of hassle for no great return. Until proven
11154 // otherwise, of course.
11155 repr = "string type";
11156
11157 case DW_TAG_unspecified_type:
11158 case DW_TAG_ptr_to_member_type:
11159 break;
11160
11161 case DW_TAG_namespace:
11162 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset,
11163 guard);
11164 break;
11165
11166 case DW_TAG_base_type:
11167 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11168 break;
11169
11170 case DW_TAG_typedef:
11171 {
11172 string qualified_name;
11173 if (!die_qualified_type_name_empty(rdr, die,
11174 where_offset,
11175 qualified_name,
11176 guard))
11177 repr = "typedef " + qualified_name;
11178 }
11179 break;
11180
11181 case DW_TAG_const_type:
11182 case DW_TAG_volatile_type:
11183 case DW_TAG_restrict_type:
11184 case DW_TAG_pointer_type:
11185 case DW_TAG_reference_type:
11186 case DW_TAG_rvalue_reference_type:
11187 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11188 break;
11189
11190 case DW_TAG_enumeration_type:
11191 {
11192 string qualified_name =
11193 rdr.get_die_qualified_type_name(die, where_offset, guard);
11194 repr = "enum " + qualified_name;
11195 }
11196 break;
11197
11198 case DW_TAG_structure_type:
11199 case DW_TAG_class_type:
11200 {
11201 string qualified_name =
11202 rdr.get_die_qualified_type_name(die, where_offset, guard);
11203 repr = "class " + qualified_name;
11204 }
11205 break;
11206
11207 case DW_TAG_union_type:
11208 {
11209 string qualified_name =
11210 rdr.get_die_qualified_type_name(die, where_offset, guard);
11211 repr = "union " + qualified_name;
11212 }
11213 break;
11214
11215 case DW_TAG_array_type:
11216 {
11217 Dwarf_Die element_type_die;
11218 if (!die_die_attribute(die, DW_AT_type, element_type_die))
11219 break;
11220 string element_type_name =
11221 rdr.get_die_qualified_type_name(&element_type_die,
11222 where_offset, guard);
11223 if (element_type_name.empty())
11224 break;
11225
11227 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
11228 /*associate_type_to_die=*/false);
11229
11230 repr = element_type_name;
11232 }
11233 break;
11234
11235 case DW_TAG_subrange_type:
11236 {
11237 // So this can be generated by Ada, on its own; that is, not
11238 // as a subtype of an array. In that case we need to handle
11239 // it properly.
11240
11241 // For now, we consider that the pretty printed name of the
11242 // subrange type is its name. We might need something more
11243 // advance, should the needs of the users get more
11244 // complicated.
11245 repr += die_qualified_type_name(rdr, die, where_offset, guard);
11246 }
11247 break;
11248
11249 case DW_TAG_subroutine_type:
11250 case DW_TAG_subprogram:
11251 {
11252 string return_type_name;
11253 string class_name;
11254 vector<string> parm_names;
11255 bool is_const = false;
11256 bool is_static = false;
11257 bool is_method_type = false;
11258 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
11259 /*pretty_print=*/true,
11260 /*qualified_name=*/true,
11262 return_type_name, class_name,
11263 parm_names, is_const,
11264 is_static, guard);
11265 if (!is_method_type)
11266 repr = "function type";
11267 else
11268 repr = "method type";
11269 repr += " " + rdr.get_die_qualified_type_name(die, where_offset, guard);
11270 }
11271 break;
11272
11273 case DW_TAG_set_type:
11274 case DW_TAG_file_type:
11275 case DW_TAG_packed_type:
11276 case DW_TAG_thrown_type:
11277 case DW_TAG_interface_type:
11278 case DW_TAG_shared_type:
11280 }
11281
11282 return repr;
11283}
11284
11285/// Return a pretty string representation of a declaration, for
11286/// internal purposes.
11287///
11288/// By internal purpose, we mean things like key-ing declarations for
11289/// lookup purposes and so on.
11290///
11291/// Note that this function is also used to pretty print functions.
11292/// For functions, it prints the signature of the function.
11293///
11294/// @param rdr the context to use.
11295///
11296/// @param die the DIE of the declaration to pretty print.
11297///
11298/// @param qualified_name if true then use qualified names.
11299///
11300/// @param where_offset where we logically are placed when calling
11301/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11302/// entries.
11303///
11304/// @param guard the set of DIE offsets of the stack of DIEs involved
11305/// in the construction of the pretty representation of the decl.
11306/// This set is used to detect (and avoid) cycles in the stack of DIEs
11307/// that is going to be walked to compute the pretty representation.
11308///
11309/// @return the resulting pretty representation.
11310static string
11311die_pretty_print_decl(const reader& rdr,
11312 const Dwarf_Die* die,
11313 bool qualified_name,
11314 bool include_fns,
11315 size_t where_offset,
11316 unordered_set<uint64_t>& guard)
11317{
11318 if (!die || !die_is_decl(die))
11319 return "";
11320
11321 string repr;
11322
11323 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11324 switch (tag)
11325 {
11326 case DW_TAG_namespace:
11327 repr = "namespace " + die_qualified_name(rdr, die, where_offset, guard);
11328 break;
11329
11330 case DW_TAG_member:
11331 case DW_TAG_variable:
11332 {
11333 string type_repr = "void";
11334 Dwarf_Die type_die;
11335 if (die_die_attribute(die, DW_AT_type, type_die))
11336 type_repr = die_type_name(rdr, &type_die,
11337 qualified_name,
11338 where_offset,
11339 guard);
11340 repr = (qualified_name
11341 ? die_qualified_name(rdr, die, where_offset, guard)
11342 : die_name(die));
11343
11344 if (repr.empty())
11345 repr = type_repr;
11346 else
11347 repr = type_repr + " " + repr;
11348 }
11349 break;
11350
11351 case DW_TAG_subprogram:
11352 if (include_fns)
11353 repr = die_function_signature(rdr, die, qualified_name,
11354 where_offset, guard);
11355 break;
11356
11357 default:
11358 break;
11359 }
11360 return repr;
11361}
11362
11363/// Compute the pretty printed representation of an artifact
11364/// represented by a DIE.
11365///
11366/// If the DIE is a type, compute the its pretty representation as a
11367/// type; otherwise, if it's a declaration, compute its pretty
11368/// representation as a declaration. Note for For instance, that a
11369/// DW_TAG_subprogram DIE is going to be represented as a function
11370/// *type*.
11371///
11372/// @param rdr the DWARF reader.
11373///
11374/// @param die the DIE to consider.
11375///
11376/// @param where_offset we in the DIE stream we are logically at.
11377///
11378/// @param guard the set of DIE offsets of the stack of DIEs involved
11379/// in the construction of the pretty representation of the DIe. This
11380/// set is used to detect (and avoid) cycles in the stack of DIEs that
11381/// is going to be walked to compute the pretty representation.
11382///
11383/// @return a copy of the pretty printed artifact.
11384static string
11385die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset,
11386 unordered_set<uint64_t>& guard)
11387{
11388 if (die_is_type(die))
11389 return die_pretty_print_type(rdr, die, where_offset, guard);
11390 else if (die_is_decl(die))
11391 return die_pretty_print_decl(rdr, die,
11392 /*qualified_names=*/true,
11393 /*include_fns=*/true,
11394 where_offset, guard);
11395 return "";
11396}
11397
11398// -----------------------------------
11399// </die pretty printer>
11400// -----------------------------------
11401
11402
11403// ----------------------------------
11404// <die comparison engine>
11405// ---------------------------------
11406
11407/// Compares two decls DIEs
11408///
11409/// This works only for DIEs emitted by the C language.
11410///
11411/// This implementation doesn't yet support namespaces.
11412///
11413/// This is a subroutine of compare_dies.
11414///
11415/// @return true iff @p l equals @p r.
11416static bool
11417compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
11418{
11419 ABG_ASSERT(l && r);
11420
11421 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11422 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11423 if (l_tag != r_tag)
11424 return false;
11425
11426 bool result = false;
11427
11428 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
11429 {
11430 // Fast path for functions and global variables.
11431 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
11432 result)
11433 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
11434 result))
11435 {
11436 if (!result)
11437 return false;
11438 }
11439
11440 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11441 result))
11442 {
11443 if (!result)
11444 return false;
11445 }
11446 return true;
11447 }
11448
11449 // Fast path for types.
11450 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11451 result))
11452 return result;
11453 return true;
11454}
11455
11456/// Test if at least one of two ODR-relevant DIEs is decl-only.
11457///
11458/// @param rdr the DWARF reader to consider.
11459///
11460/// @param l the first type DIE to consider.
11461///
11462/// @param r the second type DIE to consider.
11463///
11464/// @return true iff either @p l or @p r is decl-only and both are
11465/// ODR-relevant.
11466static bool
11467at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
11468 const Dwarf_Die *l,
11469 const Dwarf_Die *r)
11470{
11471 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
11472 return false;
11473
11474 if ((die_is_declaration_only(l) && die_has_no_child(l))
11475 || (die_is_declaration_only(r) && die_has_no_child(r)))
11476 return true;
11477 return false;
11478}
11479
11480/// Compares two type DIEs
11481///
11482/// This is a subroutine of compare_dies.
11483///
11484/// Note that this function doesn't look at the name of the DIEs.
11485/// Naming is taken into account by the function compare_as_decl_dies.
11486///
11487/// If the two DIEs are from a translation unit that is subject to the
11488/// ONE Definition Rule, then the function considers that if one DIE
11489/// is a declaration, then it's equivalent to the second. In that
11490/// case, the sizes of the two DIEs are not compared. This is so that
11491/// a declaration of a type compares equal to the definition of the
11492/// type.
11493///
11494/// @param rdr the DWARF reader to consider.
11495///
11496/// @param l the left operand of the comparison operator.
11497///
11498/// @param r the right operand of the comparison operator.
11499///
11500/// @return true iff @p l equals @p r.
11501static bool
11502compare_as_type_dies(const reader& rdr,
11503 const Dwarf_Die *l,
11504 const Dwarf_Die *r)
11505{
11506 ABG_ASSERT(l && r);
11507 ABG_ASSERT(die_is_type(l));
11508 ABG_ASSERT(die_is_type(r));
11509
11510 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
11511 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
11512 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11513 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
11514 // For now, we cannot compare DW_TAG_string_type because of its
11515 // string_length attribute that is a location descriptor that is
11516 // not necessarily a constant. So it's super hard to evaluate it
11517 // in a libabigail context. So for now, we just say that all
11518 // DW_TAG_string_type DIEs are different, by default.
11519 return false;
11520
11521 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11522 // A declaration of a type compares equal to the definition of the
11523 // type.
11524 return true;
11525
11526 uint64_t l_size = 0, r_size = 0;
11527 die_size_in_bits(l, l_size);
11528 die_size_in_bits(r, r_size);
11529
11530 return l_size == r_size;
11531}
11532
11533/// Compare two DIEs as decls (looking as their names etc) and as
11534/// types (looking at their size etc).
11535///
11536/// @param rdr the DWARF reader to consider.
11537///
11538/// @param l the first DIE to consider.
11539///
11540/// @param r the second DIE to consider.
11541///
11542/// @return TRUE iff @p l equals @p r as far as naming and size is
11543/// concerned.
11544static bool
11545compare_as_decl_and_type_dies(const reader &rdr,
11546 const Dwarf_Die *l,
11547 const Dwarf_Die *r)
11548{
11549 if (!compare_as_decl_dies(l, r)
11550 || !compare_as_type_dies(rdr, l, r))
11551 return false;
11552
11553 return true;
11554}
11555
11556/// Test if two DIEs representing function declarations have the same
11557/// linkage name, and thus are considered equal if they are C or C++,
11558/// because the two DIEs represent functions in the same binary.
11559///
11560/// If the DIEs don't have a linkage name, the function compares their
11561/// name. But in that case, the caller of the function must know that
11562/// in C++ for instance, that doesn't imply that the two functions are
11563/// equal.
11564///
11565/// @param l the first function DIE to consider.
11566///
11567/// @param r the second function DIE to consider.
11568///
11569/// @return true iff the function represented by @p l have the same
11570/// linkage name as the function represented by @p r.
11571static bool
11572fn_die_equal_by_linkage_name(const Dwarf_Die *l,
11573 const Dwarf_Die *r)
11574{
11575 if (!!l != !!r)
11576 return false;
11577
11578 if (!l)
11579 return false;
11580
11581 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11582 ABG_ASSERT(tag == DW_TAG_subprogram);
11583 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11584 ABG_ASSERT(tag == DW_TAG_subprogram);
11585
11586 string lname = die_name(l), rname = die_name(r);
11587 string llinkage_name = die_linkage_name(l),
11588 rlinkage_name = die_linkage_name(r);
11589
11590 if (die_is_in_c_or_cplusplus(l)
11591 && die_is_in_c_or_cplusplus(r))
11592 {
11593 if (!llinkage_name.empty() && !rlinkage_name.empty())
11594 return llinkage_name == rlinkage_name;
11595 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
11596 return false;
11597 else
11598 return lname == rname;
11599 }
11600
11601 return (!llinkage_name.empty()
11602 && !rlinkage_name.empty()
11603 && llinkage_name == rlinkage_name);
11604}
11605
11606/// Compare two DIEs in the context of DIE canonicalization.
11607///
11608/// If DIE canonicalization is on, the function compares the DIEs
11609/// canonically and structurally. The two types of comparison should
11610/// be equal, of course.
11611///
11612/// @param rdr the DWARF reader.
11613///
11614/// @param l_offset the offset of the first canonical DIE to compare.
11615///
11616/// @param r_offset the offset of the second canonical DIE to compare.
11617///
11618/// @param l_die_source the source of the DIE denoted by the offset @p
11619/// l_offset.
11620///
11621/// @param r_die_source the source of the DIE denoted by the offset @p
11622/// r_offset.
11623///
11624/// @param l_has_canonical_die_offset output parameter. Is set to
11625/// true if @p l_offset has a canonical DIE.
11626///
11627/// @param r_has_canonical_die_offset output parameter. Is set to
11628/// true if @p r_offset has a canonical DIE.
11629///
11630/// @param l_canonical_die_offset output parameter. If @p
11631/// l_has_canonical_die_offset is set to true, then this parameter is
11632/// set to the offset of the canonical DIE of the DIE designated by @p
11633/// l_offset.
11634static bool
11635try_canonical_die_comparison(const reader& rdr,
11636 Dwarf_Off l_offset, Dwarf_Off r_offset,
11637 die_source l_die_source, die_source r_die_source,
11638 bool& l_has_canonical_die_offset,
11639 bool& r_has_canonical_die_offset,
11640 Dwarf_Off& l_canonical_die_offset,
11641 Dwarf_Off& r_canonical_die_offset,
11642 bool& result)
11643{
11644#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11645 if (rdr.debug_die_canonicalization_is_on_
11646 && !rdr.use_canonical_die_comparison_)
11647 return false;
11648#endif
11649
11650
11651 l_has_canonical_die_offset =
11652 (l_canonical_die_offset =
11653 rdr.get_canonical_die_offset(l_offset, l_die_source,
11654 /*die_as_type=*/true));
11655
11656 r_has_canonical_die_offset =
11657 (r_canonical_die_offset =
11658 rdr.get_canonical_die_offset(r_offset, r_die_source,
11659 /*die_as_type=*/true));
11660
11661 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
11662 {
11663 result = (l_canonical_die_offset == r_canonical_die_offset);
11664 return true;
11665 }
11666
11667 return false;
11668}
11669
11670#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11671/// This function is called whenever a DIE comparison fails.
11672///
11673/// This function is intended for debugging purposes. The idea is for
11674/// hackers to set a breakpoint on this function so that they can
11675/// discover why exactly the comparison failed. They then can execute
11676/// the program from compare_dies_during_canonicalization, for
11677/// instance.
11678///
11679/// @param @l the left-hand side of the DIE comparison.
11680///
11681/// @param @r the right-hand side of the DIE comparison.
11682static void
11683notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
11684{
11685}
11686
11687#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
11688 notify_die_comparison_failed(l, r)
11689#else
11690#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
11691#endif
11692
11693/// A macro used to return from DIE comparison routines.
11694///
11695/// If the return value is false, the macro invokes the
11696/// notify_die_comparison_failed signalling function before returning.
11697/// That way, hackers willing to learn more about why the comparison
11698/// routine returned "false" can just set a breakpoint on
11699/// notify_die_comparison_failed and execute the program from
11700/// compare_dies_during_canonicalization, for instance.
11701///
11702/// @param value the value to return from the DIE comparison routines.
11703#define ABG_RETURN(value) \
11704 do \
11705 { \
11706 if ((value) == COMPARISON_RESULT_DIFFERENT) \
11707 { \
11708 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11709 } \
11710 return return_comparison_result(l, r, dies_being_compared, \
11711 value, aggregates_being_compared, \
11712 update_canonical_dies_on_the_fly); \
11713 } \
11714 while(false)
11715
11716/// A macro used to return the "false" boolean from DIE comparison
11717/// routines.
11718///
11719/// As the return value is false, the macro invokes the
11720/// notify_die_comparison_failed signalling function before returning.
11721///
11722/// @param value the value to return from the DIE comparison routines.
11723#define ABG_RETURN_FALSE \
11724 do \
11725 { \
11726 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11727 return return_comparison_result(l, r, dies_being_compared, \
11728 COMPARISON_RESULT_DIFFERENT, \
11729 aggregates_being_compared, \
11730 update_canonical_dies_on_the_fly); \
11731 } while(false)
11732
11733/// A macro to set the 'result' variable to 'false'.
11734///
11735/// The macro invokes the notify_die_comparison_failed function so
11736/// that the hacker can set a debugging breakpoint on
11737/// notify_die_comparison_failed to know where a DIE comparison failed
11738/// during compare_dies_during_canonicalization for instance.
11739///
11740/// @param result the 'result' variable to set.
11741///
11742/// @param l the first DIE of the comparison operation.
11743///
11744/// @param r the second DIE of the comparison operation.
11745#define SET_RESULT_TO_FALSE(result, l , r) \
11746 do \
11747 { \
11748 result = COMPARISON_RESULT_DIFFERENT; \
11749 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11750 } while(false)
11751
11752/// A macro to set the 'result' variable to a given value.
11753///
11754/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
11755/// invokes the notify_die_comparison_failed function so that the
11756/// hacker can set a debugging breakpoint on
11757/// notify_die_comparison_failed to know where a DIE comparison failed
11758/// during compare_dies_during_canonicalization for instance.
11759///
11760/// @param result the 'result' variable to set.
11761///
11762/// @param l the first DIE of the comparison operation.
11763///
11764/// @param r the second DIE of the comparison operation.
11765#define SET_RESULT_TO(result, value, l , r) \
11766 do \
11767 { \
11768 result = (value); \
11769 if (result == COMPARISON_RESULT_DIFFERENT) \
11770 { \
11771 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11772 } \
11773 } while(false)
11774
11775#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
11776 do \
11777 { \
11778 if (aggregates_being_compared.contains(dies_being_compared)) \
11779 { \
11780 result = COMPARISON_RESULT_CYCLE_DETECTED; \
11781 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
11782 ABG_RETURN(result); \
11783 } \
11784 } \
11785 while(false)
11786
11787/// Get the next member sibling of a given class or union member DIE.
11788///
11789/// @param die the DIE to consider.
11790///
11791/// @param member out parameter. This is set to the next member
11792/// sibling, iff the function returns TRUE.
11793///
11794/// @return TRUE iff the function set @p member to the next member
11795/// sibling DIE.
11796static bool
11797get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
11798{
11799 if (!die)
11800 return false;
11801
11802 bool found_member = false;
11803 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
11804 member) == 0);
11805 found_member;
11806 found_member = (dwarf_siblingof(member, member) == 0))
11807 {
11808 int tag = dwarf_tag(member);
11809 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
11810 break;
11811 }
11812
11813 return found_member;
11814}
11815
11816/// Get the first child DIE of a class/struct/union DIE that is a
11817/// member DIE.
11818///
11819/// Note that a member DIE is represented by a DWARF tag that is
11820/// either DW_TAG_member, DW_TAG_inheritance.
11821///
11822/// @param die the DIE to consider.
11823///
11824/// @param child out parameter. This is set to the first child DIE of
11825/// @p iff this function returns TRUE.
11826///
11827/// @return TRUE iff @p child is set to the first child DIE of @p die
11828/// that is a member DIE.
11829static bool
11830get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
11831{
11832 if (!die)
11833 return false;
11834
11835 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11836 ABG_ASSERT(tag == DW_TAG_structure_type
11837 || tag == DW_TAG_union_type
11838 || tag == DW_TAG_class_type);
11839
11840 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die), child) == 0);
11841
11842 if (!found_child)
11843 return false;
11844
11845 tag = dwarf_tag(child);
11846
11847 if (!(tag == DW_TAG_member
11848 || tag == DW_TAG_inheritance
11849 || tag == DW_TAG_subprogram))
11850 found_child = get_next_member_sibling_die(child, child);
11851
11852 return found_child;
11853}
11854
11855/// This is a sub-routine of return_comparison_result.
11856///
11857/// Propagate the canonical type of a the right-hand-side DIE to the
11858/// lef-hand-side DIE. This is a optimization that is done when the
11859/// two DIEs compare equal.
11860///
11861/// If the right-hand-side DIE is not canonicalized, the function
11862/// performs its canonicalization.
11863///
11864/// This optimization is performed only if
11865/// is_canon_type_to_be_propagated_tag returns true.
11866///
11867/// @param rdr the current context to consider.
11868///
11869/// @param l the left-hand-side DIE of the comparison. It's going to
11870/// receive the canonical type of the other DIE.
11871///
11872/// @param r the right-hand-side DIE of the comparison. Its canonical
11873/// type is propagated to @p l.
11874static void
11875maybe_propagate_canonical_type(const reader& rdr,
11876 const Dwarf_Die* l,
11877 const Dwarf_Die* r)
11878{
11879 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
11880 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11881
11882 if (l_tag != r_tag)
11883 return;
11884
11885 if (is_canon_type_to_be_propagated_tag(l_tag))
11886 propagate_canonical_type(rdr, l, r);
11887}
11888
11889/// Propagate the canonical type of a the right-hand-side DIE to the
11890/// left-hand-side DIE. This is a optimization that is done when the
11891/// two DIEs compare equal.
11892///
11893/// If the right-hand-side DIE is not canonicalized, the function
11894/// performs its canonicalization.
11895///
11896/// @param rdr the current context to consider.
11897///
11898/// @param l the left-hand-side DIE of the comparison. It's going to
11899/// receive the canonical type of the other DIE.
11900///
11901/// @param r the right-hand-side DIE of the comparison. Its canonical
11902/// type is propagated to @p l.
11903static void
11904propagate_canonical_type(const reader& rdr,
11905 const Dwarf_Die* l,
11906 const Dwarf_Die* r)
11907{
11908 ABG_ASSERT(l && r);
11909
11910 // If 'l' has no canonical DIE and if 'r' has one, then propagage
11911 // the canonical DIE of 'r' to 'l'.
11912 //
11913 // In case 'r' has no canonical DIE, then compute it, and then
11914 // propagate that canonical DIE to 'r'.
11915 const die_source l_source = rdr.get_die_source(l);
11916 const die_source r_source = rdr.get_die_source(r);
11917
11918 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
11919 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
11920 bool l_has_canonical_die_offset = false;
11921 bool r_has_canonical_die_offset = false;
11922 Dwarf_Off l_canonical_die_offset = 0;
11923 Dwarf_Off r_canonical_die_offset = 0;
11924
11925 l_has_canonical_die_offset =
11926 (l_canonical_die_offset =
11927 rdr.get_canonical_die_offset(l_offset, l_source,
11928 /*die_as_type=*/true));
11929
11930 r_has_canonical_die_offset =
11931 (r_canonical_die_offset =
11932 rdr.get_canonical_die_offset(r_offset, r_source,
11933 /*die_as_type=*/true));
11934
11935
11936 if (!l_has_canonical_die_offset
11937 && r_has_canonical_die_offset
11938 // A DIE can be equivalent only to another DIE of the same
11939 // source.
11940 && l_source == r_source)
11941 {
11942 ABG_ASSERT(r_canonical_die_offset);
11943 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
11944 /*die_as_type=*/true);
11945 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
11946 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
11947 rdr.canonical_propagated_count_++;
11948 }
11949}
11950
11951/// This function does the book keeping of comparison pairs necessary
11952/// to handle
11953///
11954/// * the detection of cycles during the comparison of aggregate
11955/// types, in conjuction with the macro
11956/// RETURN_IF_COMPARISON_CYCLE_DETECTED
11957///
11958/// * the handling of the canonical type propagation optimisation
11959/// to speed-up type canonicalization.
11960///
11961///
11962/// Note that this function is essentially a sub-routine of
11963/// compare_dies.
11964///
11965/// @param l the left-hand-side DIE being compared.
11966///
11967/// @param r the right-hand-side DIE being compared.
11968///
11969/// @param cur_dies the pair of die offsets of l and r. This is
11970/// redundant as it can been computed from @p l and @p r. However,
11971/// getting it as an argument is an optimization to avoid computing it
11972/// over and over again, given how often this function is invoked from
11973/// compare_dies.
11974///
11975/// @param return the result of comparing @p l against @p r.
11976///
11977/// @param comparison_stack the stack of pair of type DIEs being
11978/// compared.
11979///
11980/// @param do_propagate_canonical_type if true then the function
11981/// performs canonical DIEs propagation, meaning that if @p l equals
11982/// @p r and if @p r has a canonical type, then the canonical type of
11983/// @p l is set to the canonical type of @p r.
11984static comparison_result
11985return_comparison_result(const Dwarf_Die* l,
11986 const Dwarf_Die* r,
11987 const offset_pair_type& cur_dies,
11988 comparison_result result,
11989 offset_pairs_stack_type& comparison_stack,
11990 bool do_propagate_canonical_type = true)
11991{
11992 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11993
11994 if (result == COMPARISON_RESULT_EQUAL)
11995 {
11996 // The result comparing the two types is "true", basically. So
11997 // let's propagate the canonical type of r onto l, so that we
11998 // don't need to compute the canonical type of r.
11999 if (do_propagate_canonical_type)
12000 {
12001 // Propagate canonical type.
12002 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12003
12004 // TODO: do we need to confirm any tentative canonical
12005 // propagation?
12006 }
12007 }
12008 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12009 {
12010 // So upon detection of the comparison cycle, compare_dies
12011 // returned early with the comparison result
12012 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
12013 // carry on with the comparison of all the OTHER sub-types of
12014 // the redundant type. If they all compare equal, then it means
12015 // the redundant type pair compared equal. Otherwise, it
12016 // compared different.
12017 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
12018 // Let's fall through to let the end of this function set the
12019 // result to COMPARISON_RESULT_UNKNOWN;
12020 }
12021 else if (result == COMPARISON_RESULT_UNKNOWN)
12022 {
12023 // Here is an introductory comment describing what we are going
12024 // to do in this case where the result of the comparison of the
12025 // current pair of type is not "false", basically.
12026 //
12027 // This means that we don't yet know what the result of
12028 // comparing these two types is, because one of the sub-types of
12029 // the types being compared is "redundant", meaning it appears
12030 // more than once in the comparison stack, so if we were to
12031 // naively try to carry on with the comparison member-wise, we'd
12032 // end up with an endless loop, a.k.a "comparison cycle".
12033 //
12034 // If the current type pair is redundant then:
12035 //
12036 // * This is a redundant type that has just been fully
12037 // compared. In that case, all the types that depend on
12038 // this redundant type and that have been tentatively
12039 // canonical-type-propagated must see their canonical types
12040 // "confirmed". This means that this type is going to be
12041 // considered as not being redundant anymore, meaning all
12042 // the types that depend on it must be updated as not being
12043 // dependant on it anymore, and the type itsef must be
12044 // removed from the map of redundant types.
12045 //
12046 // After the type's canonical-type-propagation is confirmed,
12047 // the result of its comparison must also be changed into
12048 // COMPARISON_RESULT_EQUAL.
12049 //
12050 // After that, If the current type depends on a redundant type,
12051 // then propagate its canonical type AND track it as having its
12052 // type being canonical-type-propagated.
12053 //
12054 // If the current type is not redundant however, then it must be
12055 // dependant on a redundant type. If it's not dependant on a
12056 // redundant type, then it must be of those types which
12057 // comparisons are not tracked for cycle, probably because they
12058 // are not aggregates. Otherwise, ABORT to understand why. I
12059 // believe this should not happen. In any case, after that
12060 // safety check is passed, we just need to return at this point.
12061
12062 if (comparison_stack.is_redundant(cur_dies)
12063 && comparison_stack.vect_.back() == cur_dies)
12064 {
12065 // We are in the case described above of a redundant type
12066 // that has been fully compared.
12067 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12068 comparison_stack.confirm_canonical_propagated_type(cur_dies);
12069
12070 result = COMPARISON_RESULT_EQUAL;
12071 }
12072 else if (is_canon_type_to_be_propagated_tag(l_tag)
12073 && comparison_stack.vect_.back() == cur_dies)
12074 {
12075 // The current type is not redundant. So, as described in
12076 // the introductory comment above, it must be dependant on a
12077 // redundant type.
12078 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
12079 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12080 // Then pass through.
12081 }
12082 }
12083 else if (result == COMPARISON_RESULT_DIFFERENT)
12084 {
12085 // Here is an introductory comment describing what we are going
12086 // to do in this case where the result of the comparison of the
12087 // current pair of type is "false", basically.
12088 //
12089 // If the type pair {l,r} is redundant then cancel the
12090 // canonical-type-propagation of all the dependant pairs that
12091 // depends on this redundant {l, r}. This means walk the types
12092 // that depends on {l, r} and cancel their
12093 // canonical-propagate-type, that means remove their canonical
12094 // types and mark them as not being canonically-propagated.
12095 // Also, erase their cached comparison results that was likely
12096 // set to COMPARISON_RESULT_UNKNOWN.
12097 //
12098 // Also, update the cached result for this pair, that was likely
12099 // to be COMPARISON_RESULT_UNKNOWN.
12100 if (comparison_stack.is_redundant(cur_dies)
12101 && comparison_stack.vect_.back() == cur_dies)
12102 comparison_stack.cancel_canonical_propagated_type(cur_dies);
12103 }
12104 else
12105 {
12106 // We should never reach here.
12108 }
12109
12110 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12111 result = COMPARISON_RESULT_UNKNOWN;
12112 else if (is_canon_type_to_be_propagated_tag(l_tag)
12113 && !comparison_stack.vect_.empty()
12114 && comparison_stack.vect_.back() == cur_dies)
12115 //Finally pop the pair types being compared from comparison_stack
12116 //iff {l,r} is on the top of the stack. If it's not, then it means
12117 //we are looking at a type that was detected as a being redundant
12118 //and thus hasn't been pushed to the stack yet gain.
12119 comparison_stack.erase(cur_dies);
12120
12121 maybe_cache_type_comparison_result(comparison_stack.rdr_,
12122 l_tag, cur_dies, result);
12123
12124 return result;
12125}
12126
12127/// Compare two DIEs emitted by a C compiler.
12128///
12129/// @param rdr the DWARF reader used to load the DWARF information.
12130///
12131/// @param l the left-hand-side argument of this comparison operator.
12132///
12133/// @param r the righ-hand-side argument of this comparison operator.
12134///
12135/// @param aggregates_being_compared this holds the names of the set
12136/// of aggregates being compared. It's used by the comparison
12137/// function to avoid recursing infinitely when faced with types
12138/// referencing themselves through pointers or references. By
12139/// default, just pass an empty instance of @ref istring_set_type to
12140/// it.
12141///
12142/// @param update_canonical_dies_on_the_fly if true, when two
12143/// sub-types compare equal (during the comparison of @p l and @p r)
12144/// update their canonical type. That way, two types of the same name
12145/// are structurally compared to each other only once. So the
12146/// non-linear structural comparison of two types of the same name
12147/// only happen once.
12148///
12149/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12150static comparison_result
12151compare_dies(const reader& rdr,
12152 const Dwarf_Die *l, const Dwarf_Die *r,
12153 offset_pairs_stack_type& aggregates_being_compared,
12154 bool update_canonical_dies_on_the_fly)
12155{
12156 ABG_ASSERT(l);
12157 ABG_ASSERT(r);
12158
12159 const die_source l_die_source = rdr.get_die_source(l);
12160 const die_source r_die_source = rdr.get_die_source(r);
12161
12162 offset_type l_offset =
12163 {
12164 l_die_source,
12165 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12166 };
12167
12168 offset_type r_offset =
12169 {
12170 r_die_source,
12171 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12172 };
12173
12174 offset_pair_type dies_being_compared(l_offset, r_offset);
12175
12176 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
12177 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12178
12179 if (l_tag != r_tag)
12181
12182 if (l_offset == r_offset)
12183 return COMPARISON_RESULT_EQUAL;
12184
12185 if (rdr.leverage_dwarf_factorization()
12186 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
12187 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
12188 if (l_offset != r_offset)
12189 return COMPARISON_RESULT_DIFFERENT;
12190
12191 comparison_result result = COMPARISON_RESULT_EQUAL;
12192 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
12193 dies_being_compared,
12194 result))
12195 return result;
12196
12197 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
12198 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
12199
12200 // If 'l' and 'r' already have canonical DIEs, then just compare the
12201 // offsets of their canonical DIEs.
12202 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
12203 {
12204 bool canonical_compare_result = false;
12205 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
12206 l_die_source, r_die_source,
12207 l_has_canonical_die_offset,
12208 r_has_canonical_die_offset,
12209 l_canonical_die_offset,
12210 r_canonical_die_offset,
12211 canonical_compare_result))
12212 {
12213 comparison_result result;
12214 SET_RESULT_TO(result,
12215 (canonical_compare_result
12216 ? COMPARISON_RESULT_EQUAL
12217 : COMPARISON_RESULT_DIFFERENT),
12218 l, r);
12219 return result;
12220 }
12221 }
12222
12223
12224
12225 switch (l_tag)
12226 {
12227 case DW_TAG_base_type:
12228 case DW_TAG_string_type:
12229 case DW_TAG_unspecified_type:
12230 if (!compare_as_decl_and_type_dies(rdr, l, r))
12231 SET_RESULT_TO_FALSE(result, l, r);
12232 break;
12233
12234 case DW_TAG_typedef:
12235 case DW_TAG_pointer_type:
12236 case DW_TAG_reference_type:
12237 case DW_TAG_rvalue_reference_type:
12238 case DW_TAG_const_type:
12239 case DW_TAG_volatile_type:
12240 case DW_TAG_restrict_type:
12241 {
12242 if (!compare_as_type_dies(rdr, l, r))
12243 {
12244 SET_RESULT_TO_FALSE(result, l, r);
12245 break;
12246 }
12247
12248 bool from_the_same_tu = false;
12249 if (!pointer_or_qual_die_of_anonymous_class_type(l)
12250 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
12251 && from_the_same_tu)
12252 {
12253 // These two typedefs, pointer, reference, or qualified
12254 // types have the same name and are defined in the same TU.
12255 // They thus ought to be the same.
12256 //
12257 // Note that pointers, reference or qualified types to
12258 // anonymous types are not taking into account here because
12259 // those always need to be structurally compared.
12260 SET_RESULT_TO_FALSE(result, l, r);
12261 break;
12262 }
12263 }
12264
12265 {
12266 // No fancy optimization in this case. We need to
12267 // structurally compare the two DIEs.
12268 Dwarf_Die lu_type_die, ru_type_die;
12269 bool lu_is_void, ru_is_void;
12270
12271 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
12272 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
12273
12274 if (lu_is_void && ru_is_void)
12275 result = COMPARISON_RESULT_EQUAL;
12276 else if (lu_is_void != ru_is_void)
12277 SET_RESULT_TO_FALSE(result, l, r);
12278 else
12279 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
12280 aggregates_being_compared,
12281 update_canonical_dies_on_the_fly);
12282 }
12283 break;
12284
12285 case DW_TAG_enumeration_type:
12286 if (!compare_as_decl_and_type_dies(rdr, l, r))
12287 SET_RESULT_TO_FALSE(result, l, r);
12288 else
12289 {
12290 // Walk the enumerators.
12291 Dwarf_Die l_enumtor, r_enumtor;
12292 bool found_l_enumtor = true, found_r_enumtor = true;
12293
12294 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12295 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
12296 &l_enumtor) == 0,
12297 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
12298 &r_enumtor) == 0;
12299 found_l_enumtor && found_r_enumtor;
12300 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
12301 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
12302 {
12303 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
12304 if ( l_tag != r_tag)
12305 {
12306 SET_RESULT_TO_FALSE(result, l, r);
12307 break;
12308 }
12309
12310 if (l_tag != DW_TAG_enumerator)
12311 continue;
12312
12313 uint64_t l_val = 0, r_val = 0;
12314 die_unsigned_constant_attribute(&l_enumtor,
12315 DW_AT_const_value,
12316 l_val);
12317 die_unsigned_constant_attribute(&r_enumtor,
12318 DW_AT_const_value,
12319 r_val);
12320 if (l_val != r_val)
12321 {
12322 SET_RESULT_TO_FALSE(result, l, r);
12323 break;
12324 }
12325 }
12326 if (found_l_enumtor != found_r_enumtor )
12327 SET_RESULT_TO_FALSE(result, l, r);
12328 }
12329 break;
12330
12331 case DW_TAG_structure_type:
12332 case DW_TAG_union_type:
12333 case DW_TAG_class_type:
12334 {
12335 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12336
12337 rdr.compare_count_++;
12338
12339 if (!compare_as_decl_and_type_dies(rdr, l, r))
12340 SET_RESULT_TO_FALSE(result, l, r);
12341 else if (rdr.options().assume_odr_for_cplusplus
12342 && rdr.odr_is_relevant(l)
12343 && rdr.odr_is_relevant(r)
12344 && !die_is_anonymous(l)
12345 && !die_is_anonymous(r))
12346 result = COMPARISON_RESULT_EQUAL;
12347 else
12348 {
12349 aggregates_being_compared.add(dies_being_compared);
12350
12351 Dwarf_Die l_member, r_member;
12352 bool found_l_member = true, found_r_member = true;
12353
12354 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12355 for (found_l_member = get_member_child_die(l, &l_member),
12356 found_r_member = get_member_child_die(r, &r_member);
12357 found_l_member && found_r_member;
12358 found_l_member = get_next_member_sibling_die(&l_member,
12359 &l_member),
12360 found_r_member = get_next_member_sibling_die(&r_member,
12361 &r_member))
12362 {
12363 int l_tag = dwarf_tag(&l_member),
12364 r_tag = dwarf_tag(&r_member);
12365
12366 if (l_tag != r_tag)
12367 {
12368 SET_RESULT_TO_FALSE(result, l, r);
12369 break;
12370 }
12371
12372 ABG_ASSERT(l_tag == DW_TAG_member
12373 || l_tag == DW_TAG_variable
12374 || l_tag == DW_TAG_inheritance
12375 || l_tag == DW_TAG_subprogram);
12376
12377 comparison_result local_result =
12378 compare_dies(rdr, &l_member, &r_member,
12379 aggregates_being_compared,
12380 update_canonical_dies_on_the_fly);
12381
12382 if (local_result == COMPARISON_RESULT_UNKNOWN)
12383 // Note that if the result of comparing any
12384 // sub-type is COMPARISON_RESULT_EQUAL, just
12385 // because we have at least one sub-type's
12386 // comparison being COMPARISON_RESULT_UNKNOWN
12387 // means that the comparison of this type will
12388 // return COMPARISON_RESULT_UNKNOWN to show
12389 // callers that this type (and all the types that
12390 // depend on it) depends on a redundant type
12391 result = local_result;
12392
12393 if (local_result == COMPARISON_RESULT_DIFFERENT)
12394 {
12395 SET_RESULT_TO_FALSE(result, l, r);
12396 break;
12397 }
12398 }
12399 if (found_l_member != found_r_member)
12400 {
12401 SET_RESULT_TO_FALSE(result, l, r);
12402 break;
12403 }
12404 }
12405 }
12406 break;
12407
12408 case DW_TAG_array_type:
12409 {
12410 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12411
12412 aggregates_being_compared.add(dies_being_compared);
12413
12414 rdr.compare_count_++;
12415
12416 Dwarf_Die l_child, r_child;
12417 bool found_l_child, found_r_child;
12418 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12419 &l_child) == 0,
12420 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12421 &r_child) == 0;
12422 found_l_child && found_r_child;
12423 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
12424 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
12425 {
12426 int l_child_tag = dwarf_tag(&l_child),
12427 r_child_tag = dwarf_tag(&r_child);
12428 if (l_child_tag == DW_TAG_subrange_type
12429 || r_child_tag == DW_TAG_subrange_type)
12430 {
12431 result = compare_dies(rdr, &l_child, &r_child,
12432 aggregates_being_compared,
12433 update_canonical_dies_on_the_fly);
12434 if (!result)
12435 {
12436 SET_RESULT_TO_FALSE(result, l, r);
12437 break;
12438 }
12439 }
12440 }
12441 if (found_l_child != found_r_child)
12442 SET_RESULT_TO_FALSE(result, l, r);
12443 // Compare the types of the elements of the array.
12444 Dwarf_Die ltype_die, rtype_die;
12445 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
12446 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
12447 ABG_ASSERT(found_ltype && found_rtype);
12448
12449 result = compare_dies(rdr, &ltype_die, &rtype_die,
12450 aggregates_being_compared,
12451 update_canonical_dies_on_the_fly);
12452 if (!result)
12454 }
12455 break;
12456
12457 case DW_TAG_subrange_type:
12458 {
12459 uint64_t l_lower_bound = 0, r_lower_bound = 0,
12460 l_upper_bound = 0, r_upper_bound = 0;
12461 bool l_lower_bound_set = false, r_lower_bound_set = false,
12462 l_upper_bound_set = false, r_upper_bound_set = false;
12463
12464 l_lower_bound_set =
12465 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
12466 r_lower_bound_set =
12467 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
12468
12469 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
12470 l_upper_bound))
12471 {
12472 uint64_t l_count = 0;
12473 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
12474 {
12475 l_upper_bound = l_lower_bound + l_count;
12476 l_upper_bound_set = true;
12477 if (l_upper_bound)
12478 --l_upper_bound;
12479 }
12480 }
12481 else
12482 l_upper_bound_set = true;
12483
12484 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
12485 r_upper_bound))
12486 {
12487 uint64_t r_count = 0;
12488 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
12489 {
12490 r_upper_bound = r_lower_bound + r_count;
12491 r_upper_bound_set = true;
12492 if (r_upper_bound)
12493 --r_upper_bound;
12494 }
12495 }
12496 else
12497 r_upper_bound_set = true;
12498
12499 if ((l_lower_bound_set != r_lower_bound_set)
12500 || (l_upper_bound_set != r_upper_bound_set)
12501 || (l_lower_bound != r_lower_bound)
12502 || (l_upper_bound != r_upper_bound))
12503 SET_RESULT_TO_FALSE(result, l, r);
12504 }
12505 break;
12506
12507 case DW_TAG_subroutine_type:
12508 case DW_TAG_subprogram:
12509 {
12510 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12511
12512 aggregates_being_compared.add(dies_being_compared);
12513
12514 rdr.compare_count_++;
12515
12516 if (l_tag == DW_TAG_subprogram
12517 && !fn_die_equal_by_linkage_name(l, r))
12518 {
12519 SET_RESULT_TO_FALSE(result, l, r);
12520 break;
12521 }
12522 else if (l_tag == DW_TAG_subprogram
12523 && die_is_in_c(l) && die_is_in_c(r))
12524 {
12525 result = COMPARISON_RESULT_EQUAL;
12526 break;
12527 }
12528 else if (!die_is_in_c(l) && !die_is_in_c(r))
12529 {
12530 // In C, we cannot have two different functions with the
12531 // same linkage name in a given binary. But here we are
12532 // looking at DIEs that don't originate from C. So we
12533 // need to compare return types and parameter types.
12534 Dwarf_Die l_return_type, r_return_type;
12535 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
12536 l_return_type);
12537 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
12538 r_return_type);
12539 if (l_return_type_is_void != r_return_type_is_void
12540 || (!l_return_type_is_void
12541 && !compare_dies(rdr,
12542 &l_return_type, &r_return_type,
12543 aggregates_being_compared,
12544 update_canonical_dies_on_the_fly)))
12545 SET_RESULT_TO_FALSE(result, l, r);
12546 else
12547 {
12548 Dwarf_Die l_child, r_child;
12549 bool found_l_child, found_r_child;
12550 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12551 &l_child) == 0,
12552 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12553 &r_child) == 0;
12554 found_l_child && found_r_child;
12555 found_l_child = dwarf_siblingof(&l_child,
12556 &l_child) == 0,
12557 found_r_child = dwarf_siblingof(&r_child,
12558 &r_child)==0)
12559 {
12560 int l_child_tag = dwarf_tag(&l_child);
12561 int r_child_tag = dwarf_tag(&r_child);
12562 comparison_result local_result =
12563 COMPARISON_RESULT_EQUAL;
12564 if (l_child_tag != r_child_tag)
12565 local_result = COMPARISON_RESULT_DIFFERENT;
12566 if (l_child_tag == DW_TAG_formal_parameter)
12567 local_result =
12568 compare_dies(rdr, &l_child, &r_child,
12569 aggregates_being_compared,
12570 update_canonical_dies_on_the_fly);
12571 if (local_result == COMPARISON_RESULT_DIFFERENT)
12572 {
12573 result = local_result;
12574 SET_RESULT_TO_FALSE(result, l, r);
12575 break;
12576 }
12577 if (local_result == COMPARISON_RESULT_UNKNOWN)
12578 // Note that if the result of comparing any
12579 // sub-type is COMPARISON_RESULT_EQUAL, just
12580 // because we have at least one sub-type's
12581 // comparison being COMPARISON_RESULT_UNKNOWN
12582 // means that the comparison of this type will
12583 // return COMPARISON_RESULT_UNKNOWN to show
12584 // callers that this type (and all the types
12585 // that depend on it) depends on a redundant
12586 // type and so, can't be
12587 // canonical-type-propagated.
12588 result = local_result;
12589 }
12590 if (found_l_child != found_r_child)
12591 {
12592 SET_RESULT_TO_FALSE(result, l, r);
12593 break;
12594 }
12595 }
12596 }
12597 }
12598 break;
12599
12600 case DW_TAG_formal_parameter:
12601 {
12602 Dwarf_Die l_type, r_type;
12603 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
12604 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
12605 if (l_type_is_void != r_type_is_void)
12606 SET_RESULT_TO_FALSE(result, l, r);
12607 else if (!l_type_is_void)
12608 {
12609 comparison_result local_result =
12610 compare_dies(rdr, &l_type, &r_type,
12611 aggregates_being_compared,
12612 update_canonical_dies_on_the_fly);
12613 SET_RESULT_TO(result, local_result, l, r);
12614 }
12615 }
12616 break;
12617
12618 case DW_TAG_variable:
12619 case DW_TAG_member:
12620 if (compare_as_decl_dies(l, r))
12621 {
12622 // Compare the offsets of the data members
12623 if (l_tag == DW_TAG_member)
12624 {
12625 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12626 die_member_offset(rdr, l, l_offset_in_bits);
12627 die_member_offset(rdr, r, r_offset_in_bits);
12628 if (l_offset_in_bits != r_offset_in_bits)
12629 SET_RESULT_TO_FALSE(result, l, r);
12630 }
12631 if (result)
12632 {
12633 // Compare the types of the data members or variables.
12634 Dwarf_Die l_type, r_type;
12635 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12636 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12637 comparison_result local_result =
12638 compare_dies(rdr, &l_type, &r_type,
12639 aggregates_being_compared,
12640 update_canonical_dies_on_the_fly);
12641 SET_RESULT_TO(result, local_result, l, r);
12642 }
12643 }
12644 else
12645 SET_RESULT_TO_FALSE(result, l, r);
12646 break;
12647
12648 case DW_TAG_inheritance:
12649 {
12650 Dwarf_Die l_type, r_type;
12651 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12652 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12653 result = compare_dies(rdr, &l_type, &r_type,
12654 aggregates_being_compared,
12655 update_canonical_dies_on_the_fly);
12656 if (!result)
12657 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12658
12659 uint64_t l_a = 0, r_a = 0;
12660 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
12661 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
12662 if (l_a != r_a)
12663 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12664
12665 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
12666 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
12667 if (l_a != r_a)
12668 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12669
12670 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12671 die_member_offset(rdr, l, l_offset_in_bits);
12672 die_member_offset(rdr, r, r_offset_in_bits);
12673 if (l_offset_in_bits != r_offset_in_bits)
12674 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12675 }
12676 break;
12677
12678 case DW_TAG_ptr_to_member_type:
12679 {
12680 bool comp_result = false;
12681 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
12682 if (!comp_result)
12683 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12684
12685 Dwarf_Die l_type, r_type;
12686 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12687 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12688 result = compare_dies(rdr, &l_type, &r_type,
12689 aggregates_being_compared,
12690 update_canonical_dies_on_the_fly);
12691 if (!result)
12692 ABG_RETURN(result);
12693
12694 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
12695 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
12696 result = compare_dies(rdr, &l_type, &r_type,
12697 aggregates_being_compared,
12698 update_canonical_dies_on_the_fly);
12699 if (!result)
12700 ABG_RETURN(result);
12701 }
12702 break;
12703
12704 case DW_TAG_enumerator:
12705 case DW_TAG_packed_type:
12706 case DW_TAG_set_type:
12707 case DW_TAG_file_type:
12708 case DW_TAG_thrown_type:
12709 case DW_TAG_interface_type:
12710 case DW_TAG_shared_type:
12711 case DW_TAG_compile_unit:
12712 case DW_TAG_namespace:
12713 case DW_TAG_module:
12714 case DW_TAG_constant:
12715 case DW_TAG_partial_unit:
12716 case DW_TAG_imported_unit:
12717 case DW_TAG_dwarf_procedure:
12718 case DW_TAG_imported_declaration:
12719 case DW_TAG_entry_point:
12720 case DW_TAG_label:
12721 case DW_TAG_lexical_block:
12722 case DW_TAG_unspecified_parameters:
12723 case DW_TAG_variant:
12724 case DW_TAG_common_block:
12725 case DW_TAG_common_inclusion:
12726 case DW_TAG_inlined_subroutine:
12727 case DW_TAG_with_stmt:
12728 case DW_TAG_access_declaration:
12729 case DW_TAG_catch_block:
12730 case DW_TAG_friend:
12731 case DW_TAG_namelist:
12732 case DW_TAG_namelist_item:
12733 case DW_TAG_template_type_parameter:
12734 case DW_TAG_template_value_parameter:
12735 case DW_TAG_try_block:
12736 case DW_TAG_variant_part:
12737 case DW_TAG_imported_module:
12738 case DW_TAG_condition:
12739 case DW_TAG_type_unit:
12740 case DW_TAG_template_alias:
12741 case DW_TAG_lo_user:
12742 case DW_TAG_MIPS_loop:
12743 case DW_TAG_format_label:
12744 case DW_TAG_function_template:
12745 case DW_TAG_class_template:
12746 case DW_TAG_GNU_BINCL:
12747 case DW_TAG_GNU_EINCL:
12748 case DW_TAG_GNU_template_template_param:
12749 case DW_TAG_GNU_template_parameter_pack:
12750 case DW_TAG_GNU_formal_parameter_pack:
12751 case DW_TAG_GNU_call_site:
12752 case DW_TAG_GNU_call_site_parameter:
12753 case DW_TAG_hi_user:
12754#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12755 if (rdr.debug_die_canonicalization_is_on_)
12757#endif
12759 break;
12760 }
12761
12762 ABG_RETURN(result);
12763}
12764
12765/// Compare two DIEs emitted by a C compiler.
12766///
12767/// @param rdr the DWARF reader used to load the DWARF information.
12768///
12769/// @param l the left-hand-side argument of this comparison operator.
12770///
12771/// @param r the righ-hand-side argument of this comparison operator.
12772///
12773/// @param update_canonical_dies_on_the_fly if yes, then this function
12774/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
12775/// comparing l and r. This helps in making so that sub-type DIEs of
12776/// 'l' and 'r' are compared structurally only once. This is how we
12777/// turn this exponential comparison problem into a problem that is a
12778/// closer to a linear one.
12779///
12780/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12781static comparison_result
12782compare_dies(const reader& rdr,
12783 const Dwarf_Die *l,
12784 const Dwarf_Die *r,
12785 bool update_canonical_dies_on_the_fly)
12786{
12787 offset_pairs_stack_type aggregates_being_compared(rdr);
12788 return compare_dies(rdr, l, r, aggregates_being_compared,
12789 update_canonical_dies_on_the_fly);
12790}
12791
12792/// Compare two DIEs for the purpose of canonicalization.
12793///
12794/// This is a sub-routine of reader::get_canonical_die.
12795///
12796/// When DIE canonicalization debugging is on, this function performs
12797/// both structural and canonical comparison. It expects that both
12798/// comparison yield the same result.
12799///
12800/// @param rdr the DWARF reader.
12801///
12802/// @param l the left-hand-side comparison operand DIE.
12803///
12804/// @param r the right-hand-side comparison operand DIE.
12805///
12806/// @param update_canonical_dies_on_the_fly if true, then some
12807/// aggregate DIEs will see their canonical types propagated.
12808///
12809/// @return true iff @p l equals @p r.
12810static bool
12811compare_dies_during_canonicalization(reader& rdr,
12812 const Dwarf_Die *l,
12813 const Dwarf_Die *r,
12814 bool update_canonical_dies_on_the_fly)
12815{
12816#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12817 if (rdr.debug_die_canonicalization_is_on_)
12818 {
12819 bool canonical_equality = false, structural_equality = false;
12820 rdr.use_canonical_die_comparison_ = false;
12821 structural_equality = compare_dies(rdr, l, r,
12822 /*update_canonical_dies_on_the_fly=*/false);
12823 rdr.use_canonical_die_comparison_ = true;
12824 canonical_equality = compare_dies(rdr, l, r,
12825 update_canonical_dies_on_the_fly);
12826 if (canonical_equality != structural_equality)
12827 {
12828 std::cerr << "structural & canonical equality different for DIEs: "
12829 << std::hex
12830 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12831 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12832 << std::dec
12833 << ", repr: '"
12834 << rdr.get_die_pretty_type_representation(l, 0)
12835 << "'"
12836 << std::endl;
12838 }
12839 return structural_equality;
12840 }
12841#endif
12842 return compare_dies(rdr, l, r,
12843 update_canonical_dies_on_the_fly);
12844}
12845
12846// ----------------------------------
12847// </die comparison engine>
12848// ---------------------------------
12849
12850/// Get the point where a DW_AT_import DIE is used to import a given
12851/// (unit) DIE, between two DIEs.
12852///
12853/// @param rdr the dwarf reader to consider.
12854///
12855/// @param partial_unit_offset the imported unit for which we want to
12856/// know the insertion point. This is usually a partial unit (with
12857/// tag DW_TAG_partial_unit) but it does not necessarily have to be
12858/// so.
12859///
12860/// @param first_die_offset the offset of the DIE from which this
12861/// function starts looking for the import point of
12862/// @partial_unit_offset. Note that this offset is excluded from the
12863/// set of potential solutions.
12864///
12865/// @param first_die_cu_offset the offset of the (compilation) unit
12866/// that @p first_die_cu_offset belongs to.
12867///
12868/// @param source where the DIE of first_die_cu_offset unit comes
12869/// from.
12870///
12871/// @param last_die_offset the offset of the last DIE of the up to
12872/// which this function looks for the import point of @p
12873/// partial_unit_offset. Note that this offset is excluded from the
12874/// set of potential solutions.
12875///
12876/// @param imported_point_offset. The resulting
12877/// imported_point_offset. Note that if the imported DIE @p
12878/// partial_unit_offset is not found between @p first_die_offset and
12879/// @p last_die_offset, this parameter is left untouched by this
12880/// function.
12881///
12882/// @return true iff an imported unit is found between @p
12883/// first_die_offset and @p last_die_offset.
12884static bool
12885find_import_unit_point_between_dies(const reader& rdr,
12886 size_t partial_unit_offset,
12887 Dwarf_Off first_die_offset,
12888 Dwarf_Off first_die_cu_offset,
12889 die_source source,
12890 size_t last_die_offset,
12891 size_t& imported_point_offset)
12892{
12893 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
12894 rdr.tu_die_imported_unit_points_map(source);
12895
12896 tu_die_imported_unit_points_map_type::const_iterator iter =
12897 tu_die_imported_unit_points_map.find(first_die_cu_offset);
12898
12899 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
12900
12901 const imported_unit_points_type& imported_unit_points = iter->second;
12902 if (imported_unit_points.empty())
12903 return false;
12904
12905 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
12906 imported_unit_points_type::const_iterator e = imported_unit_points.end();
12907
12908 find_lower_bound_in_imported_unit_points(imported_unit_points,
12909 first_die_offset,
12910 b);
12911
12912 if (last_die_offset != static_cast<size_t>(-1))
12913 find_lower_bound_in_imported_unit_points(imported_unit_points,
12914 last_die_offset,
12915 e);
12916
12917 if (e != imported_unit_points.end())
12918 {
12919 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12920 if (i->imported_unit_die_off == partial_unit_offset)
12921 {
12922 imported_point_offset = i->offset_of_import ;
12923 return true;
12924 }
12925
12926 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12927 {
12928 if (find_import_unit_point_between_dies(rdr,
12929 partial_unit_offset,
12930 i->imported_unit_child_off,
12931 i->imported_unit_cu_off,
12932 i->imported_unit_die_source,
12933 /*(Dwarf_Off)*/-1,
12934 imported_point_offset))
12935 return true;
12936 }
12937 }
12938 else
12939 {
12940 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
12941 if (i->imported_unit_die_off == partial_unit_offset)
12942 {
12943 imported_point_offset = i->offset_of_import ;
12944 return true;
12945 }
12946
12947 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
12948 {
12949 if (find_import_unit_point_between_dies(rdr,
12950 partial_unit_offset,
12951 i->imported_unit_child_off,
12952 i->imported_unit_cu_off,
12953 i->imported_unit_die_source,
12954 /*(Dwarf_Off)*/-1,
12955 imported_point_offset))
12956 return true;
12957 }
12958 }
12959
12960 return false;
12961}
12962
12963/// In the current translation unit, get the last point where a
12964/// DW_AT_import DIE is used to import a given (unit) DIE, before a
12965/// given DIE is found. That given DIE is called the limit DIE.
12966///
12967/// Said otherwise, this function returns the last import point of a
12968/// unit, before a limit.
12969///
12970/// @param rdr the dwarf reader to consider.
12971///
12972/// @param partial_unit_offset the imported unit for which we want to
12973/// know the insertion point of. This is usually a partial unit (with
12974/// tag DW_TAG_partial_unit) but it does not necessarily have to be
12975/// so.
12976///
12977/// @param where_offset the offset of the limit DIE.
12978///
12979/// @param imported_point_offset. The resulting imported_point_offset.
12980/// Note that if the imported DIE @p partial_unit_offset is not found
12981/// before @p die_offset, this is set to the last @p
12982/// partial_unit_offset found under @p parent_die.
12983///
12984/// @return true iff an imported unit is found before @p die_offset.
12985/// Note that if an imported unit is found after @p die_offset then @p
12986/// imported_point_offset is set and the function return false.
12987static bool
12988find_import_unit_point_before_die(const reader& rdr,
12989 size_t partial_unit_offset,
12990 size_t where_offset,
12991 size_t& imported_point_offset)
12992{
12993 size_t import_point_offset = 0;
12994 Dwarf_Die first_die_of_tu;
12995
12996 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
12997 &first_die_of_tu) != 0)
12998 return false;
12999
13000 Dwarf_Die cu_die_memory;
13001 Dwarf_Die *cu_die;
13002
13003 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
13004 &cu_die_memory, 0, 0);
13005
13006 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
13007 dwarf_dieoffset(&first_die_of_tu),
13008 dwarf_dieoffset(cu_die),
13009 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
13010 where_offset,
13011 import_point_offset))
13012 {
13013 imported_point_offset = import_point_offset;
13014 return true;
13015 }
13016
13017 if (import_point_offset)
13018 {
13019 imported_point_offset = import_point_offset;
13020 return true;
13021 }
13022
13023 return false;
13024}
13025
13026/// Return the parent DIE for a given DIE.
13027///
13028/// Note that the function build_die_parent_map() must have been
13029/// called before this one can work. This function either succeeds or
13030/// aborts the current process.
13031///
13032/// @param rdr the DWARF reader to consider.
13033///
13034/// @param die the DIE for which we want the parent.
13035///
13036/// @param parent_die the output parameter set to the parent die of
13037/// @p die. Its memory must be allocated and handled by the caller.
13038///
13039/// @param where_offset the offset of the DIE where we are "logically"
13040/// positionned at, in the DIE tree. This is useful when @p die is
13041/// e.g, DW_TAG_partial_unit that can be included in several places in
13042/// the DIE tree.
13043///
13044/// @return true if the function could get a parent DIE, false
13045/// otherwise.
13046static bool
13047get_parent_die(const reader& rdr,
13048 const Dwarf_Die* die,
13049 Dwarf_Die& parent_die,
13050 size_t where_offset)
13051{
13052 ABG_ASSERT(rdr.dwarf_debug_info());
13053
13054 const die_source source = rdr.get_die_source(die);
13055
13056 const offset_offset_map_type& m = rdr.die_parent_map(source);
13057 offset_offset_map_type::const_iterator i =
13058 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
13059
13060 if (i == m.end())
13061 return false;
13062
13063 switch (source)
13064 {
13065 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
13066 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13067 i->second, &parent_die));
13068 break;
13069 case ALT_DEBUG_INFO_DIE_SOURCE:
13070 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
13071 i->second, &parent_die));
13072 break;
13073 case TYPE_UNIT_DIE_SOURCE:
13074 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13075 i->second, &parent_die));
13076 break;
13077 case NO_DEBUG_INFO_DIE_SOURCE:
13078 case NUMBER_OF_DIE_SOURCES:
13080 }
13081
13082 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
13083 {
13084 if (where_offset == 0)
13085 {
13086 parent_die = *rdr.cur_tu_die();
13087 return true;
13088 }
13089 size_t import_point_offset = 0;
13090 bool found =
13091 find_import_unit_point_before_die(rdr,
13092 dwarf_dieoffset(&parent_die),
13093 where_offset,
13094 import_point_offset);
13095 if (!found)
13096 // It looks like parent_die (which comes from the alternate
13097 // debug info file) hasn't been imported into this TU. So,
13098 // Let's assume its logical parent is the DIE of the current
13099 // TU.
13100 parent_die = *rdr.cur_tu_die();
13101 else
13102 {
13103 ABG_ASSERT(import_point_offset);
13104 Dwarf_Die import_point_die;
13105 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13106 import_point_offset,
13107 &import_point_die));
13108 return get_parent_die(rdr, &import_point_die,
13109 parent_die, where_offset);
13110 }
13111 }
13112
13113 return true;
13114}
13115
13116/// Get the DIE representing the scope of a given DIE.
13117///
13118/// Please note that when the DIE we are looking at has a
13119/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
13120/// DIE is the parent DIE of the DIE referred to by that attribute.
13121/// In other words, this function returns the scope of the origin DIE
13122/// of the current DIE.
13123///
13124/// So, the scope DIE can be different from the parent DIE of a given
13125/// DIE.
13126///
13127/// Also note that if the current translation unit is from C, then
13128/// this returns the global scope.
13129///
13130/// @param rdr the DWARF reader to use.
13131///
13132/// @param dye the DIE to consider.
13133///
13134/// @param where_offset where we are logically at in the DIE stream.
13135///
13136/// @param scope_die out parameter. This is set to the resulting
13137/// scope DIE iff the function returns true.
13138///
13139/// @return true iff the scope was found and returned in the @p
13140/// scope_die parameter.
13141static bool
13142get_scope_die(const reader& rdr,
13143 const Dwarf_Die* dye,
13144 size_t where_offset,
13145 Dwarf_Die& scope_die)
13146{
13147 Dwarf_Die origin_die_mem;
13148 Dwarf_Die *die = &origin_die_mem;
13149 if (!die_origin_die(dye, origin_die_mem))
13150 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13151
13152 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13153 get_die_language(die, die_lang);
13154 if (is_c_language(die_lang)
13155 || rdr.die_parent_map(rdr.get_die_source(die)).empty())
13156 {
13157 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
13158 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
13159 }
13160
13161 if (!get_parent_die(rdr, die, scope_die, where_offset))
13162 return false;
13163
13164 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
13165 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
13166 || dwarf_tag(&scope_die) == DW_TAG_array_type)
13167 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
13168
13169 return true;
13170}
13171
13172/// Return the abigail IR node representing the scope of a given DIE.
13173///
13174/// Note that it is the logical scope that is returned. That is, if
13175/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
13176/// attribute, it's the scope of the referred-to DIE (via these
13177/// attributes) that is returned. In other words, its the scope of
13178/// the origin DIE that is returned.
13179///
13180/// Also note that if the current translation unit is from C, then
13181/// this returns the global scope.
13182///
13183/// @param rdr the dwarf reader to use.
13184///
13185/// @param dye the DIE to get the scope for.
13186///
13187/// @param called_from_public_decl is true if this function has been
13188/// initially called within the context of a public decl.
13189///
13190/// @param where_offset the offset of the DIE where we are "logically"
13191/// positionned at, in the DIE tree. This is useful when @p die is
13192/// e.g, DW_TAG_partial_unit that can be included in several places in
13193/// the DIE tree.
13194///
13195/// @return the resulting scope, or nil if could not be computed.
13196static scope_decl_sptr
13197get_scope_for_die(reader& rdr,
13198 Dwarf_Die* dye,
13199 bool called_for_public_decl,
13200 size_t where_offset)
13201{
13202 Dwarf_Die origin_die_mem;
13203 Dwarf_Die *die = &origin_die_mem;
13204
13205 if (!die_origin_die(dye, origin_die_mem))
13206 // There was no origin DIE found, so let's make the "die" pointer
13207 // above point to the content of the input "dye".
13208 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13209
13210 const die_source source_of_die = rdr.get_die_source(die);
13211
13212 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13213 get_die_language(die, die_lang);
13214 if (is_c_language(die_lang)
13215 || rdr.die_parent_map(source_of_die).empty())
13216 {
13217 // In units for the C languages all decls belong to the global
13218 // namespace. This is generally the case if Libabigail
13219 // determined that no DIE -> parent map was needed.
13220 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
13221 return rdr.global_scope();
13222 }
13223
13224 Dwarf_Die parent_die;
13225
13226 if (!get_parent_die(rdr, die, parent_die, where_offset))
13227 return rdr.nil_scope();
13228
13229 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
13230 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
13231 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13232 {
13233 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
13234 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13235 {
13236 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
13237 || source_of_die == TYPE_UNIT_DIE_SOURCE);
13238 return rdr.cur_transl_unit()->get_global_scope();
13239 }
13240
13241 // For top level DIEs like DW_TAG_compile_unit, we just want to
13242 // return the global scope for the corresponding translation
13243 // unit. This must have been set by
13244 // build_translation_unit_and_add_to_ir if we already started to
13245 // build the translation unit of parent_die. Otherwise, just
13246 // return the global scope of the current translation unit.
13247 die_tu_map_type::const_iterator i =
13248 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
13249 if (i != rdr.die_tu_map().end())
13250 return i->second->get_global_scope();
13251 return rdr.cur_transl_unit()->get_global_scope();
13252 }
13253
13255 type_or_decl_base_sptr d;
13256 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
13257 || dwarf_tag(&parent_die) == DW_TAG_array_type
13258 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
13259 // this is an entity defined in a scope that is a function.
13260 // Normally, I would say that this should be dropped. But I have
13261 // seen a case where a typedef DIE needed by a function parameter
13262 // was defined right before the parameter, under the scope of the
13263 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
13264 // the function parm too. So for that case, let's say that the
13265 // scope is the scope of the function itself. Note that this is
13266 // an error of the DWARF emitter. We should never see this DIE in
13267 // this context.
13268 {
13269 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
13270 called_for_public_decl,
13271 where_offset);
13272 if (is_anonymous_type_die(die))
13273 // For anonymous type that have nothing to do in a function or
13274 // array type context, let's put it in the containing
13275 // namespace. That is, do not let it be in a containing class
13276 // or union where it has nothing to do.
13277 while (is_class_or_union_type(s))
13278 {
13279 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
13280 return rdr.nil_scope();
13281 s = get_scope_for_die(rdr, &parent_die,
13282 called_for_public_decl,
13283 where_offset);
13284 }
13285 return s;
13286 }
13287 else
13288 d = build_ir_node_from_die(rdr, &parent_die,
13289 called_for_public_decl,
13290 where_offset);
13291 s = dynamic_pointer_cast<scope_decl>(d);
13292 if (!s)
13293 // this is an entity defined in someting that is not a scope.
13294 // Let's drop it.
13295 return rdr.nil_scope();
13296
13297 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
13298 if (cl && cl->get_is_declaration_only())
13299 {
13300 scope_decl_sptr scop =
13301 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
13302 if (scop)
13303 s = scop;
13304 else
13305 s = cl;
13306 }
13307 return s;
13308}
13309
13310/// Convert a DWARF constant representing the value of the
13311/// DW_AT_language property into the translation_unit::language
13312/// enumerator.
13313///
13314/// @param l the DWARF constant to convert.
13315///
13316/// @return the resulting translation_unit::language enumerator.
13318dwarf_language_to_tu_language(size_t l)
13319{
13320 switch (l)
13321 {
13322 case DW_LANG_C89:
13323 return translation_unit::LANG_C89;
13324 case DW_LANG_C:
13325 return translation_unit::LANG_C;
13326 case DW_LANG_Ada83:
13327 return translation_unit::LANG_Ada83;
13328 case DW_LANG_C_plus_plus:
13329 return translation_unit::LANG_C_plus_plus;
13330 case DW_LANG_Cobol74:
13331 return translation_unit::LANG_Cobol74;
13332 case DW_LANG_Cobol85:
13333 return translation_unit::LANG_Cobol85;
13334 case DW_LANG_Fortran77:
13335 return translation_unit::LANG_Fortran77;
13336 case DW_LANG_Fortran90:
13337 return translation_unit::LANG_Fortran90;
13338 case DW_LANG_Pascal83:
13339 return translation_unit::LANG_Pascal83;
13340 case DW_LANG_Modula2:
13341 return translation_unit::LANG_Modula2;
13342 case DW_LANG_Java:
13343 return translation_unit::LANG_Java;
13344 case DW_LANG_C99:
13345 return translation_unit::LANG_C99;
13346 case DW_LANG_Ada95:
13347 return translation_unit::LANG_Ada95;
13348 case DW_LANG_Fortran95:
13349 return translation_unit::LANG_Fortran95;
13350 case DW_LANG_PLI:
13351 return translation_unit::LANG_PLI;
13352 case DW_LANG_ObjC:
13353 return translation_unit::LANG_ObjC;
13354 case DW_LANG_ObjC_plus_plus:
13355 return translation_unit::LANG_ObjC_plus_plus;
13356
13357#ifdef HAVE_DW_LANG_Rust_enumerator
13358 case DW_LANG_Rust:
13359 return translation_unit::LANG_Rust;
13360#endif
13361
13362#ifdef HAVE_DW_LANG_UPC_enumerator
13363 case DW_LANG_UPC:
13364 return translation_unit::LANG_UPC;
13365#endif
13366
13367#ifdef HAVE_DW_LANG_D_enumerator
13368 case DW_LANG_D:
13369 return translation_unit::LANG_D;
13370#endif
13371
13372#ifdef HAVE_DW_LANG_Python_enumerator
13373 case DW_LANG_Python:
13374 return translation_unit::LANG_Python;
13375#endif
13376
13377#ifdef HAVE_DW_LANG_Go_enumerator
13378 case DW_LANG_Go:
13379 return translation_unit::LANG_Go;
13380#endif
13381
13382#ifdef HAVE_DW_LANG_C11_enumerator
13383 case DW_LANG_C11:
13384 return translation_unit::LANG_C11;
13385#endif
13386
13387#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
13388 case DW_LANG_C_plus_plus_03:
13389 return translation_unit::LANG_C_plus_plus_03;
13390#endif
13391
13392#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
13393 case DW_LANG_C_plus_plus_11:
13394 return translation_unit::LANG_C_plus_plus_11;
13395#endif
13396
13397#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
13398 case DW_LANG_C_plus_plus_14:
13399 return translation_unit::LANG_C_plus_plus_14;
13400#endif
13401
13402#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
13403 case DW_LANG_Mips_Assembler:
13404 return translation_unit::LANG_Mips_Assembler;
13405#endif
13406
13407 default:
13408 return translation_unit::LANG_UNKNOWN;
13409 }
13410}
13411
13412/// Get the default array lower bound value as defined by the DWARF
13413/// specification, version 4, depending on the language of the
13414/// translation unit.
13415///
13416/// @param l the language of the translation unit.
13417///
13418/// @return the default array lower bound value.
13419static uint64_t
13420get_default_array_lower_bound(translation_unit::language l)
13421{
13422 int value = 0;
13423 switch (l)
13424 {
13425 case translation_unit::LANG_UNKNOWN:
13426 value = 0;
13427 break;
13428 case translation_unit::LANG_Cobol74:
13429 case translation_unit::LANG_Cobol85:
13430 value = 1;
13431 break;
13432 case translation_unit::LANG_C89:
13433 case translation_unit::LANG_C99:
13434 case translation_unit::LANG_C11:
13435 case translation_unit::LANG_C:
13436 case translation_unit::LANG_C_plus_plus_03:
13437 case translation_unit::LANG_C_plus_plus_11:
13438 case translation_unit::LANG_C_plus_plus_14:
13439 case translation_unit::LANG_C_plus_plus:
13440 case translation_unit::LANG_ObjC:
13441 case translation_unit::LANG_ObjC_plus_plus:
13442 case translation_unit::LANG_Rust:
13443 value = 0;
13444 break;
13445 case translation_unit::LANG_Fortran77:
13446 case translation_unit::LANG_Fortran90:
13447 case translation_unit::LANG_Fortran95:
13448 case translation_unit::LANG_Ada83:
13449 case translation_unit::LANG_Ada95:
13450 case translation_unit::LANG_Pascal83:
13451 case translation_unit::LANG_Modula2:
13452 value = 1;
13453 break;
13454 case translation_unit::LANG_Java:
13455 value = 0;
13456 break;
13457 case translation_unit::LANG_PLI:
13458 value = 1;
13459 break;
13460 case translation_unit::LANG_UPC:
13461 case translation_unit::LANG_D:
13462 case translation_unit::LANG_Python:
13463 case translation_unit::LANG_Go:
13464 case translation_unit::LANG_Mips_Assembler:
13465 value = 0;
13466 break;
13467 }
13468
13469 return value;
13470}
13471
13472/// For a given offset, find the lower bound of a sorted vector of
13473/// imported unit point offset.
13474///
13475/// The lower bound is the smallest point (the point with the smallest
13476/// offset) which is the greater than a given offset.
13477///
13478/// @param imported_unit_points_type the sorted vector of imported
13479/// unit points.
13480///
13481/// @param val the offset to consider when looking for the lower
13482/// bound.
13483///
13484/// @param r an iterator to the lower bound found. This parameter is
13485/// set iff the function returns true.
13486///
13487/// @return true iff the lower bound has been found.
13488static bool
13489find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
13490 Dwarf_Off val,
13491 imported_unit_points_type::const_iterator& r)
13492{
13493 imported_unit_point v(val);
13494 imported_unit_points_type::const_iterator result =
13495 std::lower_bound(p.begin(), p.end(), v);
13496
13497 bool is_ok = result != p.end();
13498
13499 if (is_ok)
13500 r = result;
13501
13502 return is_ok;
13503}
13504
13505/// Given a DW_TAG_compile_unit, build and return the corresponding
13506/// abigail::translation_unit ir node. Note that this function
13507/// recursively reads the children dies of the current DIE and
13508/// populates the resulting translation unit.
13509///
13510/// @param rdr the DWARF reader to use.
13511///
13512/// @param die the DW_TAG_compile_unit DIE to consider.
13513///
13514/// @param address_size the size of the addresses expressed in this
13515/// translation unit in general.
13516///
13517/// @return a pointer to the resulting translation_unit.
13519build_translation_unit_and_add_to_ir(reader& rdr,
13520 Dwarf_Die* die,
13521 char address_size)
13522{
13523 translation_unit_sptr result;
13524
13525 if (!die)
13526 return result;
13527 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
13528
13529 // Clear the part of the context that is dependent on the translation
13530 // unit we are reading.
13531 rdr.clear_per_translation_unit_data();
13532
13533 rdr.cur_tu_die(die);
13534
13535 string path = die_string_attribute(die, DW_AT_name);
13536 if (path == "<artificial>")
13537 {
13538 // This is a file artificially generated by the compiler, so its
13539 // name is '<artificial>'. As we want all different translation
13540 // units to have unique path names, let's suffix this path name
13541 // with its die offset.
13542 std::ostringstream o;
13543 o << path << "-" << std::hex << dwarf_dieoffset(die);
13544 path = o.str();
13545 }
13546 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
13547
13548 // See if the same translation unit exits already in the current
13549 // corpus. Sometimes, the same translation unit can be present
13550 // several times in the same debug info. The content of the
13551 // different instances of the translation unit are different. So to
13552 // represent that, we are going to re-use the same translation
13553 // unit. That is, it's going to be the union of all the translation
13554 // units of the same path.
13555 {
13556 const string& abs_path =
13557 compilation_dir.empty() ? path : compilation_dir + "/" + path;
13558 result = rdr.corpus()->find_translation_unit(abs_path);
13559 }
13560
13561 if (!result)
13562 {
13563 result.reset(new translation_unit(rdr.env(),
13564 path,
13565 address_size));
13566 result->set_compilation_dir_path(compilation_dir);
13567 rdr.corpus()->add(result);
13568 uint64_t l = 0;
13569 die_unsigned_constant_attribute(die, DW_AT_language, l);
13570 result->set_language(dwarf_language_to_tu_language(l));
13571 }
13572
13573 rdr.cur_transl_unit(result);
13574 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
13575
13576 Dwarf_Die child;
13577 if (dwarf_child(die, &child) != 0)
13578 return result;
13579
13580 result->set_is_constructed(false);
13581 int tag = dwarf_tag(&child);
13582 do
13583 if (rdr.load_undefined_interfaces()
13584 && (rdr.is_decl_die_with_undefined_symbol(&child)
13585 || tag == DW_TAG_class_type // Top-level classes might
13586 // have undefined interfaces
13587 // that need to be
13588 // represented, so let's
13589 // analyze them as well.
13590 || ((tag == DW_TAG_union_type || tag == DW_TAG_structure_type)
13591 && die_is_in_cplus_plus(&child))))
13592 {
13593 // Analyze undefined functions & variables for the purpose of
13594 // analyzing compatibility matters.
13595 build_ir_node_from_die(rdr, &child,
13596 // Pretend the DIE is publicly defined
13597 // so that types that are reachable
13598 // from it get analyzed as well.
13599 /*die_is_public=*/true,
13600 dwarf_dieoffset(&child));
13601 }
13602 else if (!rdr.env().analyze_exported_interfaces_only()
13603 || rdr.is_decl_die_with_exported_symbol(&child))
13604 {
13605 // Analyze all the DIEs we encounter unless we are asked to only
13606 // analyze exported interfaces and the types reachables from them.
13607 build_ir_node_from_die(rdr, &child,
13608 die_is_public_decl(&child),
13609 dwarf_dieoffset(&child));
13610 }
13611 while (dwarf_siblingof(&child, &child) == 0);
13612
13613 if (!rdr.var_decls_to_re_add_to_tree().empty())
13614 for (list<var_decl_sptr>::const_iterator v =
13615 rdr.var_decls_to_re_add_to_tree().begin();
13616 v != rdr.var_decls_to_re_add_to_tree().end();
13617 ++v)
13618 {
13619 if (is_member_decl(*v))
13620 continue;
13621
13622 ABG_ASSERT((*v)->get_scope());
13623 string demangled_name =
13624 demangle_cplus_mangled_name((*v)->get_linkage_name());
13625 if (!demangled_name.empty())
13626 {
13627 std::list<string> fqn_comps;
13628 fqn_to_components(demangled_name, fqn_comps);
13629 string mem_name = fqn_comps.back();
13630 fqn_comps.pop_back();
13631 class_decl_sptr class_type;
13632 string ty_name;
13633 if (!fqn_comps.empty())
13634 {
13635 ty_name = components_to_type_name(fqn_comps);
13636 class_type =
13637 lookup_class_type(ty_name, *rdr.cur_transl_unit());
13638 }
13639 if (class_type)
13640 {
13641 // So we are seeing a member variable for which there
13642 // is a global variable definition DIE not having a
13643 // reference attribute pointing back to the member
13644 // variable declaration DIE. Thus remove the global
13645 // variable definition from its current non-class
13646 // scope ...
13647 decl_base_sptr d;
13648 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
13649 // This is the data member with the same name in cl.
13650 // We just need to flag it as static.
13651 ;
13652 else
13653 {
13654 // In this case there is no data member with the
13655 // same name in cl already. Let's add it there then
13656 // ...
13658 d = add_decl_to_scope(*v, class_type);
13659 }
13660
13661 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
13662 // Let's flag the data member as static.
13663 set_member_is_static(d, true);
13664 }
13665 }
13666 }
13667 rdr.var_decls_to_re_add_to_tree().clear();
13668
13669 result->set_is_constructed(true);
13670
13671 return result;
13672}
13673
13674/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
13675/// DW_TAG_module (for fortran) DIE.
13676///
13677/// Note that this function connects the DW_TAG_namespace to the IR
13678/// being currently created, reads the children of the DIE and
13679/// connects them to the IR as well.
13680///
13681/// @param rdr the DWARF reader to use.
13682///
13683/// @param die the DIE to read from. Must be either DW_TAG_namespace
13684/// or DW_TAG_module.
13685///
13686/// @param where_offset the offset of the DIE where we are "logically"
13687/// positionned at, in the DIE tree. This is useful when @p die is
13688/// e.g, DW_TAG_partial_unit that can be included in several places in
13689/// the DIE tree.
13690///
13691/// @return the resulting @ref abigail::namespace_decl or NULL if it
13692/// couldn't be created.
13694build_namespace_decl_and_add_to_ir(reader& rdr,
13695 Dwarf_Die* die,
13696 size_t where_offset)
13697{
13698 namespace_decl_sptr result;
13699
13700 if (!die)
13701 return result;
13702
13703 unsigned tag = dwarf_tag(die);
13704 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
13705 return result;
13706
13707 scope_decl_sptr scope = get_scope_for_die(rdr, die,
13708 /*called_for_public_decl=*/false,
13709 where_offset);
13710
13711 string name, linkage_name;
13712 location loc;
13713 die_loc_and_name(rdr, die, loc, name, linkage_name);
13714
13715 result.reset(new namespace_decl(rdr.env(), name, loc));
13716 add_decl_to_scope(result, scope.get());
13717 rdr.associate_die_to_decl(die, result, where_offset);
13718
13719 Dwarf_Die child;
13720 if (dwarf_child(die, &child) != 0)
13721 return result;
13722
13723 rdr.scope_stack().push(result.get());
13724 do
13725 build_ir_node_from_die(rdr, &child,
13726 // If this namespace DIE is private
13727 // (anonymous) then all its content is
13728 // considered private. Otherwise, its
13729 // public decls are considered public.
13730 /*called_from_public_decl=*/
13731 die_is_public_decl(die) && die_is_public_decl(&child),
13732 where_offset);
13733 while (dwarf_siblingof(&child, &child) == 0);
13734 rdr.scope_stack().pop();
13735
13736 return result;
13737}
13738
13739/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
13740///
13741/// @param rdr the DWARF reader to use.
13742///
13743/// @param die the DW_TAG_base_type to consider.
13744///
13745/// @param where_offset where we are logically at in the DIE stream.
13746///
13747/// @return the resulting decl_base_sptr.
13748static type_decl_sptr
13749build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
13750{
13751 type_decl_sptr result;
13752
13753 if (!die)
13754 return result;
13755 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
13756
13757 uint64_t byte_size = 0, bit_size = 0;
13758 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
13759 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
13760 return result;
13761
13762 if (bit_size == 0 && byte_size != 0)
13763 // Update the bit size.
13764 bit_size = byte_size * 8;
13765
13766 string type_name, linkage_name;
13767 location loc;
13768 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
13769
13770 if (byte_size == 0)
13771 {
13772 // The size of the type is zero, that must mean that we are
13773 // looking at the definition of the void type.
13774 if (type_name == "void")
13775 result = is_type_decl(build_ir_node_for_void_type(rdr));
13776 else
13777 // A type of size zero that is not void? Hmmh, I am not sure
13778 // what that means. Return nil for now.
13779 return result;
13780 }
13781
13782 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13783 {
13784 string normalized_type_name = type_name;
13785 real_type real_type;
13786 if (parse_real_type(type_name, real_type))
13787 normalized_type_name = real_type.to_string();
13788 result = lookup_basic_type(normalized_type_name, *corp);
13789 }
13790
13791 if (!result)
13792 if (corpus_sptr corp = rdr.corpus())
13793 result = lookup_basic_type(type_name, *corp);
13794 if (!result)
13795 result.reset(new type_decl(rdr.env(), type_name, bit_size,
13796 /*alignment=*/0, loc, linkage_name));
13797 rdr.associate_die_to_type(die, result, where_offset);
13798 return result;
13799}
13800
13801/// Construct the type that is to be used as the underlying type of an
13802/// enum.
13803///
13804/// @param rdr the DWARF reader to use.
13805///
13806/// @param enum_name the name of the enum that this type is going to
13807/// be the underlying type of.
13808///
13809/// @param enum_size the size of the enum.
13810///
13811/// @param is_anonymous whether the underlying type is anonymous or
13812/// not. By default, this should be set to true as before c++11 (and
13813/// in C), it's almost the case.
13814static type_decl_sptr
13815build_enum_underlying_type(reader& rdr,
13816 string enum_name,
13817 uint64_t enum_size,
13818 bool is_anonymous = true)
13819{
13820 string underlying_type_name =
13821 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
13822 enum_size);
13823
13824 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
13825 enum_size, enum_size, location()));
13826 result->set_is_anonymous(is_anonymous);
13827 result->set_is_artificial(true);
13828 translation_unit_sptr tu = rdr.cur_transl_unit();
13829 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
13830 result = dynamic_pointer_cast<type_decl>(d);
13831 ABG_ASSERT(result);
13832 maybe_canonicalize_type(result, rdr);
13833 return result;
13834}
13835
13836/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
13837///
13838/// @param rdr the DWARF reader to use.
13839///
13840/// @param die the DIE to read from.
13841///
13842/// @param scope the scope of the final enum. Note that this function
13843/// does *NOT* add the built type to this scope. The scope is just so
13844/// that the function knows how to name anonymous enums.
13845///
13846/// @param is_declaration_only is true if the DIE denoted by @p die is
13847/// a declaration-only DIE.
13848///
13849/// @return the built enum_type_decl or NULL if it could not be built.
13851build_enum_type(reader& rdr,
13852 Dwarf_Die* die,
13853 scope_decl* scope,
13854 size_t where_offset,
13855 bool is_declaration_only)
13856{
13857 enum_type_decl_sptr result;
13858 if (!die)
13859 return result;
13860
13861 unsigned tag = dwarf_tag(die);
13862 if (tag != DW_TAG_enumeration_type)
13863 return result;
13864
13865 string name, linkage_name;
13866 location loc;
13867 die_loc_and_name(rdr, die, loc, name, linkage_name);
13868
13869 bool is_anonymous = false;
13870 // If the enum is anonymous, let's give it a name.
13871 if (name.empty())
13872 {
13873 name = get_internal_anonymous_die_prefix_name(die);
13874 ABG_ASSERT(!name.empty());
13875 // But we remember that the type is anonymous.
13876 is_anonymous = true;
13877
13878 scope_decl* sc = scope ? scope : rdr.global_scope().get();
13879 if (size_t s = sc->get_num_anonymous_member_enums())
13880 name = build_internal_anonymous_die_name(name, s);
13881 }
13882
13883 bool use_odr = rdr.odr_is_relevant(die);
13884 // If the type has location, then associate it to its
13885 // representation. This way, all occurences of types with the same
13886 // representation (name) and location can be later detected as being
13887 // for the same type.
13888
13889 if (!is_anonymous)
13890 {
13891 if (use_odr)
13892 {
13893 if (enum_type_decl_sptr pre_existing_enum =
13894 is_enum_type(rdr.lookup_artifact_from_die(die)))
13895 result = pre_existing_enum;
13896 }
13897 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13898 {
13899 if (loc)
13900 result = lookup_enum_type_per_location(loc.expand(), *corp);
13901 }
13902 else if (loc)
13903 {
13904 if (enum_type_decl_sptr pre_existing_enum =
13905 is_enum_type(rdr.lookup_artifact_from_die(die)))
13906 if (pre_existing_enum->get_location() == loc)
13907 result = pre_existing_enum;
13908 }
13909
13910 if (result)
13911 {
13912 rdr.associate_die_to_type(die, result, where_offset);
13913 return result;
13914 }
13915 }
13916 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
13917 // we can look them up?
13918
13919 uint64_t size = 0;
13920 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13921 size *= 8;
13922 bool is_artificial = die_is_artificial(die);
13923
13924 // for now we consider that underlying types of enums are all anonymous
13925 bool enum_underlying_type_is_anonymous= true;
13926
13928 Dwarf_Die child;
13929 if (dwarf_child(die, &child) == 0)
13930 {
13931 do
13932 {
13933 if (dwarf_tag(&child) != DW_TAG_enumerator)
13934 continue;
13935
13936 string n, m;
13937 location l;
13938 die_loc_and_name(rdr, &child, l, n, m);
13939 uint64_t val = 0;
13940 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
13941 enms.push_back(enum_type_decl::enumerator(n, val));
13942 }
13943 while (dwarf_siblingof(&child, &child) == 0);
13944 }
13945
13946 // DWARF up to version 4 (at least) doesn't seem to carry the
13947 // underlying type, so let's create an artificial one here, which
13948 // sole purpose is to be passed to the constructor of the
13949 // enum_type_decl type.
13950 type_decl_sptr t =
13951 build_enum_underlying_type(rdr, name, size,
13952 enum_underlying_type_is_anonymous);
13953 t->set_is_declaration_only(is_declaration_only);
13954
13955 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
13956 result->set_is_anonymous(is_anonymous);
13957 result->set_is_declaration_only(is_declaration_only);
13958 result->set_is_artificial(is_artificial);
13959 rdr.associate_die_to_type(die, result, where_offset);
13960
13961 return result;
13962}
13963
13964/// Once a function_decl has been built and added to a class as a
13965/// member function, this function updates the information of the
13966/// function_decl concerning the properties of its relationship with
13967/// the member class. That is, it updates properties like
13968/// virtualness, access, constness, cdtorness, etc ...
13969///
13970/// @param die the DIE of the function_decl that has been just built.
13971///
13972/// @param f the function_decl that has just been built from @p die.
13973///
13974/// @param klass the @ref class_or_union that @p f belongs to.
13975///
13976/// @param rdr the context used to read the ELF/DWARF information.
13977static void
13978finish_member_function_reading(Dwarf_Die* die,
13979 const function_decl_sptr& f,
13980 const class_or_union_sptr klass,
13981 reader& rdr)
13982{
13983 ABG_ASSERT(klass);
13984
13985 method_decl_sptr m = is_method_decl(f);
13986 ABG_ASSERT(m);
13987
13988 method_type_sptr method_t = is_method_type(m->get_type());
13989 ABG_ASSERT(method_t);
13990
13991 size_t is_inline = die_is_declared_inline(die);
13992 bool is_ctor = (f->get_name() == klass->get_name());
13993 bool is_dtor = (!f->get_name().empty()
13994 && static_cast<string>(f->get_name())[0] == '~');
13995 bool is_virtual = die_is_virtual(die);
13996 int64_t vindex = -1;
13997 if (is_virtual)
13998 die_virtual_function_index(die, vindex);
13999 access_specifier access = public_access;
14000 if (class_decl_sptr c = is_class_type(klass))
14001 if (!c->is_struct())
14002 access = private_access;
14003 die_access_specifier(die, access);
14004
14005 m->is_declared_inline(is_inline);
14006 set_member_access_specifier(m, access);
14007 if (is_virtual)
14008 set_member_function_virtuality(m, is_virtual, vindex);
14009 bool is_static = method_t->get_is_for_static_method();
14010 set_member_is_static(m, is_static);
14011 set_member_function_is_ctor(m, is_ctor);
14012 set_member_function_is_dtor(m, is_dtor);
14013 set_member_function_is_const(m, method_t->get_is_const());
14014
14016
14017 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol()
14019 {
14020 // This is a virtual member function which has a linkage name
14021 // but has no underlying symbol set.
14022 //
14023 // The underlying elf symbol to set to this function can show up
14024 // later in the DWARF input or it can be that, because of some
14025 // compiler optimization, the relation between this function and
14026 // its underlying elf symbol is simply not emitted in the DWARF.
14027 //
14028 // Let's thus schedule this function for a later fixup pass
14029 // (performed by
14030 // reader::fixup_functions_with_no_symbols()) that will
14031 // set its underlying symbol.
14032 //
14033 // Note that if the underying symbol is encountered later in the
14034 // DWARF input, then the part of build_function_decl() that
14035 // updates the function to set its underlying symbol will
14036 // de-schedule this function wrt fixup pass.
14037 Dwarf_Off die_offset = dwarf_dieoffset(die);
14038 die_function_decl_map_type &fns_with_no_symbol =
14039 rdr.die_function_decl_with_no_symbol_map();
14040 die_function_decl_map_type::const_iterator i =
14041 fns_with_no_symbol.find(die_offset);
14042 if (i == fns_with_no_symbol.end())
14043 fns_with_no_symbol[die_offset] = f;
14044 }
14045
14046}
14047
14048/// If a function DIE has attributes which have not yet been read and
14049/// added to the internal representation that represents that function
14050/// then read those extra attributes and update the internal
14051/// representation.
14052///
14053/// @param rdr the DWARF reader to use.
14054///
14055/// @param die the function DIE to consider.
14056///
14057/// @param where_offset where we logical are, currently, in the stream
14058/// of DIEs. If you don't know what this is, you can just set it to zero.
14059///
14060/// @param existing_fn the representation of the function to update.
14061///
14062/// @return the updated function representation.
14063static function_decl_sptr
14064maybe_finish_function_decl_reading(reader& rdr,
14065 Dwarf_Die* die,
14066 size_t where_offset,
14067 const function_decl_sptr& existing_fn)
14068{
14069 function_decl_sptr result = build_function_decl(rdr, die,
14070 where_offset,
14071 existing_fn);
14072
14073 return result;
14074}
14075
14076/// Lookup a class or a typedef with a given qualified name in the
14077/// corpus that a given scope belongs to.
14078///
14079/// @param scope the scope to consider.
14080///
14081/// @param type_name the qualified name of the type to look for.
14082///
14083/// @return the typedef or class type found.
14084static type_base_sptr
14085lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
14086{
14087 string qname = build_qualified_name(scope, type_name);
14088 corpus* corp = scope->get_corpus();
14089 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
14090 return result;
14091}
14092
14093/// Lookup a class of typedef type from the current corpus being
14094/// constructed.
14095///
14096/// The type being looked for has the same name as a given DIE.
14097///
14098/// @param rdr the DWARF reader to use.
14099///
14100/// @param die the DIE which has the same name as the type we are
14101/// looking for.
14102///
14103/// @param called_for_public_decl whether this function is being
14104/// called from a a publicly defined declaration.
14105///
14106/// @param where_offset where we are logically at in the DIE stream.
14107///
14108/// @return the type found.
14109static type_base_sptr
14110lookup_class_or_typedef_from_corpus(reader& rdr,
14111 Dwarf_Die* die,
14112 bool called_for_public_decl,
14113 size_t where_offset)
14114{
14115 if (!die)
14116 return class_decl_sptr();
14117
14118 string class_name = die_string_attribute(die, DW_AT_name);
14119 if (class_name.empty())
14120 return class_decl_sptr();
14121
14122 scope_decl_sptr scope = get_scope_for_die(rdr, die,
14123 called_for_public_decl,
14124 where_offset);
14125 if (scope)
14126 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
14127
14128 return type_base_sptr();
14129}
14130
14131/// Test if a DIE represents a function that is a member of a given
14132/// class type.
14133///
14134/// @param rdr the DWARF reader.
14135///
14136/// @param function_die the DIE of the function to consider.
14137///
14138/// @param class_type the class type to consider.
14139///
14140/// @param where_offset where we are logically at in the DIE stream.
14141///
14142/// @return the method declaration corresponding to the member
14143/// function of @p class_type, iff @p function_die is for a member
14144/// function of @p class_type.
14145static method_decl_sptr
14146is_function_for_die_a_member_of_class(reader& rdr,
14147 Dwarf_Die* function_die,
14148 const class_or_union_sptr& class_type)
14149{
14150 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
14151
14152 if (!artifact)
14153 return method_decl_sptr();
14154
14155 method_decl_sptr method = is_method_decl(artifact);
14156 method_type_sptr method_type;
14157
14158 if (method)
14159 method_type = method->get_type();
14160 else
14161 method_type = is_method_type(artifact);
14162 ABG_ASSERT(method_type);
14163
14164 class_or_union_sptr method_class = method_type->get_class_type();
14165 ABG_ASSERT(method_class);
14166
14167 string method_class_name = method_class->get_qualified_name(),
14168 class_type_name = class_type->get_qualified_name();
14169
14170 if (method_class_name == class_type_name)
14171 {
14172 //ABG_ASSERT(class_type.get() == method_class.get());
14173 return method;
14174 }
14175
14176 return method_decl_sptr();
14177}
14178
14179/// If a given function DIE represents an existing member function of
14180/// a given class, then update that member function with new
14181/// properties present in the DIE. Otherwise, if the DIE represents a
14182/// new member function that is not already present in the class then
14183/// add that new member function to the class.
14184///
14185/// @param rdr the DWARF reader.
14186///
14187/// @param function_die the DIE of the potential member function to
14188/// consider.
14189///
14190/// @param class_type the class type to consider.
14191///
14192/// @param called_from_public_decl is true iff this function was
14193/// called from a publicly defined and exported declaration.
14194///
14195/// @param where_offset where we are logically at in the DIE stream.
14196///
14197/// @return the method decl representing the member function.
14198static method_decl_sptr
14199add_or_update_member_function(reader& rdr,
14200 Dwarf_Die* function_die,
14201 const class_or_union_sptr& class_type,
14202 bool called_from_public_decl,
14203 size_t where_offset)
14204{
14205 method_decl_sptr method =
14206 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
14207
14208 if (!method)
14209 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
14210 class_type.get(),
14211 called_from_public_decl,
14212 where_offset));
14213 if (!method)
14214 return method_decl_sptr();
14215
14216 finish_member_function_reading(function_die,
14217 is_function_decl(method),
14218 class_type, rdr);
14219 return method;
14220}
14221
14222/// Build a an IR node for class type from a DW_TAG_structure_type or
14223/// DW_TAG_class_type DIE and add that node to the ABI corpus being
14224/// currently built.
14225///
14226/// If the represents class type that already exists, then update the
14227/// existing class type with the new properties found in the DIE.
14228///
14229/// It meanst that this function can also update an existing
14230/// class_decl node with data members, member functions and other
14231/// properties coming from the DIE.
14232///
14233/// @param rdr the DWARF reader to consider.
14234///
14235/// @param die the DIE to read information from. Must be either a
14236/// DW_TAG_structure_type or a DW_TAG_class_type.
14237///
14238/// @param scope a pointer to the scope_decl* under which this class
14239/// is to be added to.
14240///
14241/// @param is_struct whether the class was declared as a struct.
14242///
14243/// @param klass if non-null, this is a klass to append the members
14244/// to. Otherwise, this function just builds the class from scratch.
14245///
14246/// @param called_from_public_decl set to true if this class is being
14247/// called from a "Public declaration like vars or public symbols".
14248///
14249/// @param where_offset the offset of the DIE where we are "logically"
14250/// positionned at, in the DIE tree. This is useful when @p die is
14251/// e.g, DW_TAG_partial_unit that can be included in several places in
14252/// the DIE tree.
14253///
14254/// @param is_declaration_only is true if the DIE denoted by @p die is
14255/// a declaration-only DIE.
14256///
14257/// @return the resulting class_type.
14258static class_decl_sptr
14259add_or_update_class_type(reader& rdr,
14260 Dwarf_Die* die,
14261 scope_decl* scope,
14262 bool is_struct,
14263 class_decl_sptr klass,
14264 bool called_from_public_decl,
14265 size_t where_offset,
14266 bool is_declaration_only)
14267{
14268 class_decl_sptr result;
14269 if (!die)
14270 return result;
14271
14272 const die_source source = rdr.get_die_source(die);
14273
14274 unsigned tag = dwarf_tag(die);
14275
14276 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
14277 return result;
14278
14279 {
14280 die_class_or_union_map_type::const_iterator i =
14281 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14282 if (i != rdr.die_wip_classes_map(source).end())
14283 {
14284 class_decl_sptr class_type = is_class_type(i->second);
14285 ABG_ASSERT(class_type);
14286 return class_type;
14287 }
14288 }
14289
14290 string name, linkage_name;
14291 location loc;
14292 die_loc_and_name(rdr, die, loc, name, linkage_name);
14293
14294 bool is_anonymous = false;
14295 if (name.empty())
14296 {
14297 // So we are looking at an anonymous struct. Let's
14298 // give it a name.
14299 name = get_internal_anonymous_die_prefix_name(die);
14300 ABG_ASSERT(!name.empty());
14301 // But we remember that the type is anonymous.
14302 is_anonymous = true;
14303
14304 size_t s = 0;
14305 if (scope)
14306 s = scope->get_num_anonymous_member_classes();
14307 else
14308 s = rdr.global_scope()->get_num_anonymous_member_classes();
14309 name = build_internal_anonymous_die_name(name, s);
14310 }
14311
14312 if (!is_anonymous)
14313 {
14314 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14315 {
14316 if (loc)
14317 // TODO: if there is only one class defined in the corpus
14318 // for this location, then re-use it. But if there are
14319 // more than one, then do not re-use it, for now.
14320 result = lookup_class_type_per_location(loc.expand(), *corp);
14321 else
14322 // TODO: if there is just one class for that name defined,
14323 // then re-use it. Otherwise, don't.
14324 result = lookup_class_type(name, *corp);
14325 if (result
14326 // If we are seeing a declaration of a definition we
14327 // already had, or if we are seing a type with the same
14328 // declaration-only-ness that we had before, then keep
14329 // the one we already had.
14330 && (result->get_is_declaration_only() == is_declaration_only
14331 || (!result->get_is_declaration_only()
14332 && is_declaration_only)))
14333 {
14334 rdr.associate_die_to_type(die, result, where_offset);
14335 return result;
14336 }
14337 else
14338 // We might be seeing the definition of a declaration we
14339 // already had. In that case, keep the definition and
14340 // drop the declaration.
14341 result.reset();
14342 }
14343 }
14344
14345 // If we've already seen the same class as 'die', then let's re-use
14346 // that one, unless it's an anonymous class. We can't really safely
14347 // re-use anonymous classes as they have no name, by construction.
14348 // What we can do, rather, is to reuse the typedef that name them,
14349 // when they do have a naming typedef.
14350 if (!is_anonymous)
14351 if (class_decl_sptr pre_existing_class =
14352 is_class_type(rdr.lookup_type_artifact_from_die(die)))
14353 klass = pre_existing_class;
14354
14355 uint64_t size = 0;
14356 die_size_in_bits(die, size);
14357 bool is_artificial = die_is_artificial(die);
14358
14359 Dwarf_Die child;
14360 bool has_child = (dwarf_child(die, &child) == 0);
14361
14362 decl_base_sptr res;
14363 if (klass)
14364 {
14365 res = result = klass;
14366 if (has_child && klass->get_is_declaration_only()
14367 && klass->get_definition_of_declaration())
14368 res = result = is_class_type(klass->get_definition_of_declaration());
14369 if (loc)
14370 result->set_location(loc);
14371 }
14372 else
14373 {
14374 result.reset(new class_decl(rdr.env(), name, size,
14375 /*alignment=*/0, is_struct, loc,
14376 decl_base::VISIBILITY_DEFAULT,
14377 is_anonymous));
14378
14379 result->set_is_declaration_only(is_declaration_only);
14380
14381 res = add_decl_to_scope(result, scope);
14382 result = dynamic_pointer_cast<class_decl>(res);
14383 ABG_ASSERT(result);
14384 }
14385
14386 if (!klass || klass->get_is_declaration_only())
14387 if (size != result->get_size_in_bits())
14388 result->set_size_in_bits(size);
14389
14390 if (klass)
14391 // We are amending a class that was built before. So let's check
14392 // if we need to amend its "declaration-only-ness" status.
14393 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
14394 // The size of the class doesn't match its
14395 // 'declaration-only-ness". We might have a non-zero sized
14396 // class which is declaration-only, or a zero sized class that
14397 // is not declaration-only. Let's set the declaration-only-ness
14398 // according to what we are instructed to.
14399 //
14400 // Note however that there are binaries out there emitted by
14401 // compilers (Clang, in C++) emit declarations-only classes that
14402 // have non-zero size. So we must honor these too. That is why
14403 // we are not forcing the declaration-only-ness to false when a
14404 // class has non-zero size. An example of such binary is
14405 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
14406 result->set_is_declaration_only(is_declaration_only);
14407
14408 // If a non-decl-only class has children node and is advertized as
14409 // having a non-zero size let's trust that.
14410 if (!result->get_is_declaration_only() && has_child)
14411 if (result->get_size_in_bits() == 0 && size != 0)
14412 result->set_size_in_bits(size);
14413
14414 result->set_is_artificial(is_artificial);
14415
14416 rdr.associate_die_to_type(die, result, where_offset);
14417
14418 if (!has_child)
14419 // TODO: set the access specifier for the declaration-only class
14420 // here.
14421 return result;
14422
14423 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14424
14425 bool is_incomplete_type = false;
14426 if (is_declaration_only && size == 0 && has_child)
14427 // this is an incomplete DWARF type as defined by [5.7.1]
14428 //
14429 // An incomplete structure, union or class type is represented by
14430 // a structure, union or class entry that does not have a byte
14431 // size attribute and that has a DW_AT_declaration attribute.
14432 //
14433 // Let's consider that it's thus a decl-only class, likely
14434 // referred to by a pointer. If we later encounter a definition
14435 // for this decl-only class type, then this decl-only class will
14436 // be resolved to it by the code in
14437 // reader::resolve_declaration_only_classes.
14438 is_incomplete_type = true;
14439
14440 scope_decl_sptr scop =
14441 dynamic_pointer_cast<scope_decl>(res);
14442 ABG_ASSERT(scop);
14443 rdr.scope_stack().push(scop.get());
14444
14445 if (has_child && !is_incomplete_type)
14446 {
14447 do
14448 {
14449 tag = dwarf_tag(&child);
14450
14451 // Handle base classes.
14452 if (tag == DW_TAG_inheritance)
14453 {
14454 result->set_is_declaration_only(false);
14455
14456 Dwarf_Die type_die;
14457 if (!die_die_attribute(&child, DW_AT_type, type_die))
14458 continue;
14459
14460 string type_name = die_type_name(rdr, &type_die,
14461 /*qualified_name=*/true,
14462 where_offset);
14463 type_base_sptr base_type;
14464 if (!type_name.empty())
14465 {
14466 base_type = result->find_base_class(type_name);
14467 if (base_type)
14468 continue;
14469 }
14470
14471 base_type =
14472 lookup_class_or_typedef_from_corpus(rdr, &type_die,
14473 called_from_public_decl,
14474 where_offset);
14475 if (!base_type)
14476 base_type =
14477 is_type(build_ir_node_from_die(rdr, &type_die,
14478 called_from_public_decl,
14479 where_offset));
14480
14481 // Sometimes base_type can be a typedef. Let's make
14482 // sure that typedef is compatible with a class type.
14484 if (!b)
14485 continue;
14486
14487 access_specifier access =
14488 is_struct
14489 ? public_access
14490 : private_access;
14491
14492 die_access_specifier(&child, access);
14493
14494 bool is_virt= die_is_virtual(&child);
14495 int64_t offset = 0;
14496 bool is_offset_present =
14497 die_member_offset(rdr, &child, offset);
14498
14499 class_decl::base_spec_sptr base(new class_decl::base_spec
14500 (b, access,
14501 is_offset_present ? offset : -1,
14502 is_virt));
14503 if (b->get_is_declaration_only()
14504 // Only non-anonymous decl-only classes are
14505 // scheduled for resolution to their definition.
14506 // Anonymous classes that are decl-only are likely
14507 // only artificially created by
14508 // get_opaque_version_of_type, from anonymous fully
14509 // defined classes. Those are never defined.
14510 && !b->get_qualified_name().empty())
14511 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
14512 if (result->find_base_class(b->get_qualified_name()))
14513 continue;
14514 result->add_base_specifier(base);
14515 }
14516 // Handle data members.
14517 else if (tag == DW_TAG_member
14518 || tag == DW_TAG_variable)
14519 {
14520 Dwarf_Die type_die;
14521 if (!die_die_attribute(&child, DW_AT_type, type_die))
14522 continue;
14523
14524 string n, m;
14525 location loc;
14526 die_loc_and_name(rdr, &child, loc, n, m);
14527 /// For now, we skip the hidden vtable pointer.
14528 /// Currently, we're looking for a member starting with
14529 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
14530 /// use as a name for the hidden vtable pointer.
14531 if (n.substr(0, 5) == "_vptr"
14532 && n.size() > 5
14533 && !std::isalnum(n.at(5))
14534 && n.at(5) != '_')
14535 continue;
14536
14537 // If the variable is already a member of this class,
14538 // move on. If it's an anonymous data member, we need
14539 // to handle it differently. We'll do that later below.
14540 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14541 continue;
14542
14543 int64_t offset_in_bits = 0;
14544 bool is_laid_out = die_member_offset(rdr, &child,
14545 offset_in_bits);
14546 // For now, is_static == !is_laid_out. When we have
14547 // templates, we'll try to be more specific. For now,
14548 // this approximation should do OK.
14549 bool is_static = !is_laid_out;
14550
14551 if (is_static)
14552 // We are looking at the *declaration* of a static
14553 // data member. The definition comes later (or
14554 // somewhere else, rather)in the DWARF. It's the
14555 // definition that we are interested in because it has
14556 // attributes of the concrete representation of the
14557 // static data member like, the ELF symbol (storage
14558 // address) of the variable, etc. It's at that point
14559 // that the IR of the data member is going to be
14560 // created (by build_ir_node_from_die, in the
14561 // DW_TAG_variable case) and added to this class/struct
14562 // being created. So for now, just ignore it.
14563 continue;
14564
14565 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
14566 called_from_public_decl,
14567 where_offset));
14568 type_base_sptr t = is_type(ty);
14569 if (!t)
14570 continue;
14571
14572 if (n.empty() && !die_is_anonymous_data_member(&child))
14573 {
14574 // We must be in a case where the data member has an
14575 // empty name because the DWARF emitter has a bug.
14576 // Let's generate an artificial name for that data
14577 // member.
14578 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
14579 ABG_ASSERT(!n.empty());
14580 }
14581
14582 // The call to build_ir_node_from_die above could have
14583 // triggered the adding of a data member named 'n' into
14584 // result. So let's check again if the variable is
14585 // already a member of this class. Here again, if it's
14586 // an anonymous data member, we need to handle it
14587 // differently. We'll do that later below.
14588 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14589 continue;
14590
14591 if (!is_static)
14592 // We have a non-static data member. So this class
14593 // cannot be a declaration-only class anymore, even if
14594 // some DWARF emitters might consider it otherwise.
14595 result->set_is_declaration_only(false);
14596 access_specifier access =
14597 is_struct
14598 ? public_access
14599 : private_access;
14600
14601 die_access_specifier(&child, access);
14602
14603 var_decl_sptr dm(new var_decl(n, t, loc, m));
14604 if (n.empty()
14606 // dm is an anonymous data member that was already
14607 // present in the current class so let's not add it.
14608 continue;
14609 result->add_data_member(dm, access, is_laid_out,
14610 is_static, offset_in_bits);
14611 ABG_ASSERT(has_scope(dm));
14612 rdr.associate_die_to_decl(&child, dm, where_offset,
14613 /*associate_by_repr=*/false);
14614 }
14615 // Handle member functions;
14616 else if (tag == DW_TAG_subprogram)
14617 {
14618 decl_base_sptr r =
14619 add_or_update_member_function(rdr, &child, result,
14620 called_from_public_decl,
14621 where_offset);
14623 rdr.associate_die_to_decl(&child, f, where_offset,
14624 /*associate_by_repr=*/true);
14625 }
14626 // Handle member types
14627 else if (die_is_type(&child))
14628 {
14629 // if the type is not already a member of this class,
14630 // then add it to the class.
14631 if (!is_anonymous_type_die(&child)
14632 && !result->find_member_type(die_name(&child)))
14633 build_ir_node_from_die(rdr, &child, result.get(),
14634 called_from_public_decl,
14635 where_offset);
14636 else if (is_anonymous_type_die(&child))
14637 {
14638 // Lookup the anonymous type DIE direcly by building
14639 // its flat representation & using it as the name of
14640 // the anonymous struct/union.
14641 string anonymous_type_name =
14642 die_class_or_enum_flat_representation(rdr, &child,
14643 /*indent=*/"",
14644 /*one_line=*/true,
14645 /*qualed_name=*/false,
14646 where_offset);
14647 if (type_base_sptr member_t =
14648 result->find_member_type(anonymous_type_name))
14649 rdr.associate_die_to_decl(&child, is_decl(member_t),
14650 where_offset,
14651 /*Associate_by_repr=*/false);
14652 else
14653 {
14654 type_base_sptr t =
14655 is_type(build_ir_node_from_die(rdr, &child,
14656 /*scope=*/result.get(),
14657 called_from_public_decl,
14658 where_offset));
14659 if (t)
14660 {
14661 add_decl_to_scope(is_decl(t), result.get());
14662 maybe_set_member_type_access_specifier(result,
14663 &child);
14664 }
14665 }
14666 }
14667 }
14668 } while (dwarf_siblingof(&child, &child) == 0);
14669 }
14670
14671 rdr.scope_stack().pop();
14672
14673 {
14674 die_class_or_union_map_type::const_iterator i =
14675 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14676 if (i != rdr.die_wip_classes_map(source).end())
14677 {
14678 if (is_member_type(i->second))
14680 get_member_access_specifier(i->second));
14681 rdr.die_wip_classes_map(source).erase(i);
14682 }
14683 }
14684
14685 return result;
14686}
14687
14688/// Build an @ref union_decl from a DW_TAG_union_type DIE.
14689///
14690/// @param rdr the DWARF reader to use.
14691///
14692/// @param die the DIE to read from.
14693///
14694/// @param scope the scope the resulting @ref union_decl belongs to.
14695///
14696/// @param union_type if this parameter is non-nil, then this function
14697/// updates the @ref union_decl that it points to, rather than
14698/// creating a new @ref union_decl.
14699///
14700/// @param called_from_public_decl is true if this function has been
14701/// initially called within the context of a public decl.
14702///
14703/// @param where_offset the offset of the DIE where we are "logically"
14704/// positionned at, in the DIE tree. This is useful when @p die is
14705/// e.g, DW_TAG_partial_unit that can be included in several places in
14706/// the DIE tree.
14707///
14708/// @param is_declaration_only is true if the DIE denoted by @p die is
14709/// a declaration-only DIE.
14710///
14711/// @return the resulting @ref union_decl type.
14712static union_decl_sptr
14713add_or_update_union_type(reader& rdr,
14714 Dwarf_Die* die,
14715 scope_decl* scope,
14716 union_decl_sptr union_type,
14717 bool called_from_public_decl,
14718 size_t where_offset,
14719 bool is_declaration_only)
14720{
14721 union_decl_sptr result;
14722 if (!die)
14723 return result;
14724
14725 unsigned tag = dwarf_tag(die);
14726
14727 if (tag != DW_TAG_union_type)
14728 return result;
14729
14730 const die_source source = rdr.get_die_source(die);
14731 {
14732 die_class_or_union_map_type::const_iterator i =
14733 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14734 if (i != rdr.die_wip_classes_map(source).end())
14735 {
14736 union_decl_sptr u = is_union_type(i->second);
14737 ABG_ASSERT(u);
14738 return u;
14739 }
14740 }
14741
14742 string name, linkage_name;
14743 location loc;
14744 die_loc_and_name(rdr, die, loc, name, linkage_name);
14745
14746 bool is_anonymous = false;
14747 if (name.empty())
14748 {
14749 // So we are looking at an anonymous union. Let's give it a
14750 // name.
14751 name = get_internal_anonymous_die_prefix_name(die);
14752 ABG_ASSERT(!name.empty());
14753 // But we remember that the type is anonymous.
14754 is_anonymous = true;
14755
14756 size_t s = 0;
14757 if (scope)
14758 s = scope->get_num_anonymous_member_unions();
14759 else
14760 s = rdr.global_scope()->get_num_anonymous_member_classes();
14761 name = build_internal_anonymous_die_name(name, s);
14762 }
14763
14764 // If the type has location, then associate it to its
14765 // representation. This way, all occurences of types with the same
14766 // representation (name) and location can be later detected as being
14767 // for the same type.
14768
14769 if (!is_anonymous)
14770 {
14771 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14772 {
14773 if (loc)
14774 result = lookup_union_type_per_location(loc.expand(), *corp);
14775 else
14776 result = lookup_union_type(name, *corp);
14777
14778 if (result)
14779 {
14780 rdr.associate_die_to_type(die, result, where_offset);
14781 return result;
14782 }
14783 }
14784 }
14785
14786 // if we've already seen a union with the same union as 'die' then
14787 // let's re-use that one. We can't really safely re-use anonymous
14788 // unions as they have no name, by construction. What we can do,
14789 // rather, is to reuse the typedef that name them, when they do have
14790 // a naming typedef.
14791 if (!is_anonymous)
14792 if (union_decl_sptr pre_existing_union =
14793 is_union_type(rdr.lookup_artifact_from_die(die)))
14794 union_type = pre_existing_union;
14795
14796 uint64_t size = 0;
14797 die_size_in_bits(die, size);
14798 bool is_artificial = die_is_artificial(die);
14799
14800 if (union_type)
14801 {
14802 result = union_type;
14803 result->set_location(loc);
14804 }
14805 else
14806 {
14807 result.reset(new union_decl(rdr.env(), name, size, loc,
14808 decl_base::VISIBILITY_DEFAULT,
14809 is_anonymous));
14810 if (is_declaration_only)
14811 result->set_is_declaration_only(true);
14812 result = is_union_type(add_decl_to_scope(result, scope));
14813 ABG_ASSERT(result);
14814 }
14815
14816 if (size)
14817 {
14818 result->set_size_in_bits(size);
14819 result->set_is_declaration_only(false);
14820 }
14821
14822 result->set_is_artificial(is_artificial);
14823
14824 rdr.associate_die_to_type(die, result, where_offset);
14825
14826 Dwarf_Die child;
14827 bool has_child = (dwarf_child(die, &child) == 0);
14828 if (!has_child)
14829 return result;
14830
14831 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14832
14833 scope_decl_sptr scop =
14834 dynamic_pointer_cast<scope_decl>(result);
14835 ABG_ASSERT(scop);
14836 rdr.scope_stack().push(scop.get());
14837
14838 if (has_child)
14839 {
14840 do
14841 {
14842 tag = dwarf_tag(&child);
14843 // Handle data members.
14844 if (tag == DW_TAG_member || tag == DW_TAG_variable)
14845 {
14846 Dwarf_Die type_die;
14847 if (!die_die_attribute(&child, DW_AT_type, type_die))
14848 continue;
14849
14850 string n, m;
14851 location loc;
14852 die_loc_and_name(rdr, &child, loc, n, m);
14853
14854 // Because we can be updating an existing union, let's
14855 // make sure we don't already have a member of the same
14856 // name. Anonymous member are handled a bit later below
14857 // so let's not consider them here.
14858 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14859 continue;
14860
14861 ssize_t offset_in_bits = 0;
14862 decl_base_sptr ty =
14863 is_decl(build_ir_node_from_die(rdr, &type_die,
14864 called_from_public_decl,
14865 where_offset));
14866 type_base_sptr t = is_type(ty);
14867 if (!t)
14868 continue;
14869
14870 // We have a non-static data member. So this union
14871 // cannot be a declaration-only union anymore, even if
14872 // some DWARF emitters might consider it otherwise.
14873 result->set_is_declaration_only(false);
14874 access_specifier access = public_access;
14875
14876 die_access_specifier(&child, access);
14877
14878 var_decl_sptr dm(new var_decl(n, t, loc, m));
14879 // If dm is an anonymous data member, let's make sure
14880 // the current union doesn't already have it as a data
14881 // member.
14882 if (n.empty() && result->find_data_member(dm))
14883 continue;
14884
14885 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14886 continue;
14887
14888 result->add_data_member(dm, access, /*is_laid_out=*/true,
14889 /*is_static=*/false,
14890 offset_in_bits);
14891 ABG_ASSERT(has_scope(dm));
14892 rdr.associate_die_to_decl(&child, dm, where_offset,
14893 /*associate_by_repr=*/false);
14894 }
14895 // Handle member functions;
14896 else if (tag == DW_TAG_subprogram)
14897 {
14898 decl_base_sptr r =
14899 is_decl(build_ir_node_from_die(rdr, &child,
14900 result.get(),
14901 called_from_public_decl,
14902 where_offset));
14903 if (!r)
14904 continue;
14905
14906 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
14907 ABG_ASSERT(f);
14908
14909 finish_member_function_reading(&child, f, result, rdr);
14910
14911 rdr.associate_die_to_decl(&child, f, where_offset,
14912 /*associate_by_repr=*/false);
14913 }
14914 // Handle member types
14915 else if (die_is_type(&child))
14916 {
14917 string type_name = die_type_name(rdr, &child,
14918 /*qualified_name=*/false,
14919 where_offset);
14920 if (type_base_sptr member_t = result->find_member_type(type_name))
14921 rdr.associate_die_to_decl(&child, is_decl(member_t),
14922 where_offset,
14923 /*associate_by_repr=*/false);
14924 else
14925 decl_base_sptr td =
14926 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
14927 called_from_public_decl,
14928 where_offset));
14929 }
14930 } while (dwarf_siblingof(&child, &child) == 0);
14931 }
14932
14933 rdr.scope_stack().pop();
14934
14935 {
14936 die_class_or_union_map_type::const_iterator i =
14937 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14938 if (i != rdr.die_wip_classes_map(source).end())
14939 {
14940 if (is_member_type(i->second))
14942 get_member_access_specifier(i->second));
14943 rdr.die_wip_classes_map(source).erase(i);
14944 }
14945 }
14946
14947 return result;
14948}
14949
14950/// build a qualified type from a DW_TAG_const_type,
14951/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
14952///
14953/// @param rdr the DWARF reader to consider.
14954///
14955/// @param die the input DIE to read from.
14956///
14957/// @param called_from_public_decl true if this function was called
14958/// from a context where either a public function or a public variable
14959/// is being built.
14960///
14961/// @param where_offset the offset of the DIE where we are "logically"
14962/// positionned at, in the DIE tree. This is useful when @p die is
14963/// e.g, DW_TAG_partial_unit that can be included in several places in
14964/// the DIE tree.
14965///
14966/// @return the resulting qualified_type_def.
14967static type_base_sptr
14968build_qualified_type(reader& rdr,
14969 Dwarf_Die* die,
14970 bool called_from_public_decl,
14971 size_t where_offset)
14972{
14973 type_base_sptr result;
14974 if (!die)
14975 return result;
14976
14977 unsigned tag = dwarf_tag(die);
14978
14979 if (tag != DW_TAG_const_type
14980 && tag != DW_TAG_volatile_type
14981 && tag != DW_TAG_restrict_type)
14982 return result;
14983
14984 Dwarf_Die underlying_type_die;
14985 decl_base_sptr utype_decl;
14986 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14987 // So, if no DW_AT_type is present, then this means (if we are
14988 // looking at a debug info emitted by GCC) that we are looking
14989 // at a qualified void type.
14990 utype_decl = build_ir_node_for_void_type(rdr);
14991
14992 if (!utype_decl)
14993 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
14994 called_from_public_decl,
14995 where_offset));
14996 if (!utype_decl)
14997 return result;
14998
14999 // The call to build_ir_node_from_die() could have triggered the
15000 // creation of the type for this DIE. In that case, just return it.
15001 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15002 {
15003 result = t;
15004 rdr.associate_die_to_type(die, result, where_offset);
15005 return result;
15006 }
15007
15008 type_base_sptr utype = is_type(utype_decl);
15009 ABG_ASSERT(utype);
15010
15011 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
15012 if (tag == DW_TAG_const_type)
15013 qual |= qualified_type_def::CV_CONST;
15014 else if (tag == DW_TAG_volatile_type)
15015 qual |= qualified_type_def::CV_VOLATILE;
15016 else if (tag == DW_TAG_restrict_type)
15017 qual |= qualified_type_def::CV_RESTRICT;
15018 else
15020
15021 if (!result)
15022 result.reset(new qualified_type_def(utype, qual, location()));
15023
15024 rdr.associate_die_to_type(die, result, where_offset);
15025
15026 return result;
15027}
15028
15029/// Walk a tree of typedef of qualified arrays and schedule all type
15030/// nodes for canonicalization.
15031///
15032/// This is to be used after an array tree has been cloned. In that
15033/// case, the newly cloned type nodes have to be scheduled for
15034/// canonicalization.
15035///
15036/// This is a subroutine of maybe_strip_qualification.
15037///
15038/// @param t the type node to be scheduled for canonicalization.
15039///
15040/// @param rdr the DWARF reader to use.
15041static void
15042schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
15043 reader &rdr)
15044{
15045 if (typedef_decl_sptr type = is_typedef(t))
15046 {
15047 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15048 rdr);
15049 rdr.schedule_type_for_late_canonicalization(t);
15050 }
15051 else if (qualified_type_def_sptr type = is_qualified_type(t))
15052 {
15053 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15054 rdr);
15055 rdr.schedule_type_for_late_canonicalization(t);
15056 }
15057 else if (array_type_def_sptr type = is_array_type(t))
15058 {
15059 for (vector<array_type_def::subrange_sptr>::const_iterator i =
15060 type->get_subranges().begin();
15061 i != type->get_subranges().end();
15062 ++i)
15063 {
15064 if (!(*i)->get_scope())
15065 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
15066 rdr.schedule_type_for_late_canonicalization(*i);
15067
15068 }
15069 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
15070 rdr);
15071 rdr.schedule_type_for_late_canonicalization(type);
15072 }
15073}
15074
15075/// Strip qualification from a qualified type, when it makes sense.
15076///
15077/// DWARF constructs "const reference". This is redundant because a
15078/// reference is always const. The issue is these redundant types then
15079/// leak into the IR and make for bad diagnostics.
15080///
15081/// This function thus strips the const qualifier from the type in
15082/// that case. It might contain code to strip other cases like this
15083/// in the future.
15084///
15085/// @param t the type to strip const qualification from.
15086///
15087/// @param rdr the @ref reader to use.
15088///
15089/// @return the stripped type or just return @p t.
15090static decl_base_sptr
15091maybe_strip_qualification(const qualified_type_def_sptr t,
15092 reader &rdr)
15093{
15094 if (!t)
15095 return t;
15096
15097 decl_base_sptr result = t;
15098 type_base_sptr u = t->get_underlying_type();
15099
15102 if (result.get() != t.get())
15103 return result;
15104
15106 {
15107 array_type_def_sptr array;
15108 scope_decl * scope = 0;
15109 if ((array = is_array_type(u)))
15110 {
15111 scope = array->get_scope();
15112 ABG_ASSERT(scope);
15113 array = is_array_type(clone_array_tree(array));
15114 schedule_array_tree_for_late_canonicalization(array, rdr);
15115 add_decl_to_scope(array, scope);
15116 t->set_underlying_type(array);
15117 u = t->get_underlying_type();
15118 }
15119 else if (is_typedef_of_array(u))
15120 {
15121 scope = is_decl(u)->get_scope();
15122 ABG_ASSERT(scope);
15123 typedef_decl_sptr typdef =
15125 schedule_array_tree_for_late_canonicalization(typdef, rdr);
15126 ABG_ASSERT(typdef);
15127 add_decl_to_scope(typdef, scope);
15128 t->set_underlying_type(typdef);
15129 u = t->get_underlying_type();
15130 array = is_typedef_of_array(u);
15131 }
15132 else
15134
15135 ABG_ASSERT(array);
15136 // We should not be editing types that are already canonicalized.
15137 ABG_ASSERT(!array->get_canonical_type());
15138 type_base_sptr element_type = array->get_element_type();
15139
15140 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
15141 {
15142 // We should not be editing types that are already canonicalized.
15143 ABG_ASSERT(!qualified->get_canonical_type());
15144 qualified_type_def::CV quals = qualified->get_cv_quals();
15145 quals |= t->get_cv_quals();
15146 qualified->set_cv_quals(quals);
15148 result = is_decl(u);
15149 }
15150 else
15151 {
15152 qualified_type_def_sptr qual_type
15153 (new qualified_type_def(element_type,
15154 t->get_cv_quals(),
15155 t->get_location()));
15157 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
15158 array->set_element_type(qual_type);
15159 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
15160 result = is_decl(u);
15161 }
15162 }
15163
15164 return result;
15165}
15166
15167/// Build a pointer type from a DW_TAG_pointer_type DIE.
15168///
15169/// @param rdr the DWARF reader to consider.
15170///
15171/// @param die the DIE to read information from.
15172///
15173/// @param called_from_public_decl true if this function was called
15174/// from a context where either a public function or a public variable
15175/// is being built.
15176///
15177/// @param where_offset the offset of the DIE where we are "logically"
15178/// positionned at, in the DIE tree. This is useful when @p die is
15179/// e.g, DW_TAG_partial_unit that can be included in several places in
15180/// the DIE tree.
15181///
15182/// @return the resulting pointer to pointer_type_def.
15184build_pointer_type_def(reader& rdr,
15185 Dwarf_Die* die,
15186 bool called_from_public_decl,
15187 size_t where_offset)
15188{
15189 pointer_type_def_sptr result;
15190
15191 if (!die)
15192 return result;
15193
15194 unsigned tag = dwarf_tag(die);
15195 if (tag != DW_TAG_pointer_type)
15196 return result;
15197
15198 type_or_decl_base_sptr utype_decl;
15199 Dwarf_Die underlying_type_die;
15200 bool has_underlying_type_die = false;
15201 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15202 // If the DW_AT_type attribute is missing, that means we are
15203 // looking at a pointer to "void".
15204 utype_decl = build_ir_node_for_void_type(rdr);
15205 else
15206 has_underlying_type_die = true;
15207
15208 if (!utype_decl && has_underlying_type_die)
15209 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
15210 called_from_public_decl,
15211 where_offset);
15212 if (!utype_decl)
15213 return result;
15214
15215 // The call to build_ir_node_from_die() could have triggered the
15216 // creation of the type for this DIE. In that case, just return it.
15217 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15218 {
15219 result = is_pointer_type(t);
15220 ABG_ASSERT(result);
15221 return result;
15222 }
15223
15224 type_base_sptr utype = is_type(utype_decl);
15225 ABG_ASSERT(utype);
15226
15227 // if the DIE for the pointer type doesn't have a byte_size
15228 // attribute then we assume the size of the pointer is the address
15229 // size of the current translation unit.
15230 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15231 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15232 // The size as expressed by DW_AT_byte_size is in byte, so let's
15233 // convert it to bits.
15234 size *= 8;
15235
15236 // And the size of the pointer must be the same as the address size
15237 // of the current translation unit.
15238 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15239
15240 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
15241 ABG_ASSERT(result->get_pointed_to_type());
15242
15243 if (is_void_pointer_type(result))
15244 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
15245
15246 rdr.associate_die_to_type(die, result, where_offset);
15247 return result;
15248}
15249
15250/// Build a reference type from either a DW_TAG_reference_type or
15251/// DW_TAG_rvalue_reference_type DIE.
15252///
15253/// @param rdr the DWARF reader to consider.
15254///
15255/// @param die the DIE to read from.
15256///
15257/// @param called_from_public_decl true if this function was called
15258/// from a context where either a public function or a public variable
15259/// is being built.
15260///
15261/// @param where_offset the offset of the DIE where we are "logically"
15262/// positionned at, in the DIE tree. This is useful when @p die is
15263/// e.g, DW_TAG_partial_unit that can be included in several places in
15264/// the DIE tree.
15265///
15266/// @return a pointer to the resulting reference_type_def.
15268build_reference_type(reader& rdr,
15269 Dwarf_Die* die,
15270 bool called_from_public_decl,
15271 size_t where_offset)
15272{
15274
15275 if (!die)
15276 return result;
15277
15278 unsigned tag = dwarf_tag(die);
15279 if (tag != DW_TAG_reference_type
15280 && tag != DW_TAG_rvalue_reference_type)
15281 return result;
15282
15283 Dwarf_Die underlying_type_die;
15284 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15285 return result;
15286
15287 type_or_decl_base_sptr utype_decl =
15288 build_ir_node_from_die(rdr, &underlying_type_die,
15289 called_from_public_decl,
15290 where_offset);
15291 if (!utype_decl)
15292 return result;
15293
15294 // The call to build_ir_node_from_die() could have triggered the
15295 // creation of the type for this DIE. In that case, just return it.
15296 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15297 {
15298 result = is_reference_type(t);
15299 ABG_ASSERT(result);
15300 return result;
15301 }
15302
15303 type_base_sptr utype = is_type(utype_decl);
15304 ABG_ASSERT(utype);
15305
15306 // if the DIE for the reference type doesn't have a byte_size
15307 // attribute then we assume the size of the reference is the address
15308 // size of the current translation unit.
15309 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15310 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15311 size *= 8;
15312
15313 // And the size of the pointer must be the same as the address size
15314 // of the current translation unit.
15315 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15316
15317 bool is_lvalue = tag == DW_TAG_reference_type;
15318
15319 result.reset(new reference_type_def(utype, is_lvalue, size,
15320 /*alignment=*/0,
15321 location()));
15322 if (corpus_sptr corp = rdr.corpus())
15323 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
15324 result = t;
15325 rdr.associate_die_to_type(die, result, where_offset);
15326 return result;
15327}
15328
15329/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
15330/// DW_TAG_ptr_to_member_type.
15331///
15332/// @param the DWARF reader touse.
15333///
15334/// @param the DIE to consider. It must carry the tag
15335/// DW_TAG_ptr_to_member_type.
15336///
15337/// @param called_from_public_decl true if this function was called
15338/// from a context where either a public function or a public variable
15339/// is being built.
15340///
15341/// @param where_offset the offset of the DIE where we are "logically"
15342/// positionned at, in the DIE tree. This is useful when @p die is
15343/// e.g, DW_TAG_partial_unit that can be included in several places in
15344/// the DIE tree.
15345///
15346/// @return a pointer to the resulting @ref ptr_to_mbr_type.
15348build_ptr_to_mbr_type(reader& rdr,
15349 Dwarf_Die* die,
15350 bool called_from_public_decl,
15351 size_t where_offset)
15352{
15353 ptr_to_mbr_type_sptr result;
15354
15355 if (!die)
15356 return result;
15357
15358 unsigned tag = dwarf_tag(die);
15359 if (tag != DW_TAG_ptr_to_member_type)
15360 return result;
15361
15362 Dwarf_Die data_member_type_die, containing_type_die;
15363
15364 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
15365 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
15366 return result;
15367
15368 type_or_decl_base_sptr data_member_type =
15369 build_ir_node_from_die(rdr, &data_member_type_die,
15370 called_from_public_decl, where_offset);
15371 if (!data_member_type)
15372 return result;
15373
15374 type_or_decl_base_sptr containing_type =
15375 build_ir_node_from_die(rdr, &containing_type_die,
15376 called_from_public_decl, where_offset);
15377 if (!containing_type)
15378 return result;
15379
15381 (is_type(containing_type)))
15382 return result;
15383
15384 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15385 {
15386 result = is_ptr_to_mbr_type(t);
15387 ABG_ASSERT(result);
15388 return result;
15389 }
15390
15391 uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
15392
15393 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
15394 is_type(data_member_type),
15395 is_type(containing_type),
15396 size_in_bits,
15397 /*alignment=*/0,
15398 location()));
15399
15400 rdr.associate_die_to_type(die, result, where_offset);
15401 return result;
15402}
15403
15404/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
15405///
15406/// @param rdr the DWARF reader to consider.
15407///
15408/// @param die the DIE to read from.
15409///
15410/// @param is_method points to a class or union declaration iff we're
15411/// building the type for a method. This is the enclosing class or
15412/// union of the method.
15413///
15414/// @param where_offset the offset of the DIE where we are "logically"
15415/// positioned at, in the DIE tree. This is useful when @p die is
15416/// e.g, DW_TAG_partial_unit that can be included in several places in
15417/// the DIE tree.
15418///
15419/// @return a pointer to the resulting function_type_sptr.
15420static function_type_sptr
15421build_function_type(reader& rdr,
15422 Dwarf_Die* die,
15423 class_or_union_sptr is_method,
15424 size_t where_offset)
15425{
15426 function_type_sptr result;
15427
15428 if (!die)
15429 return result;
15430
15431 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
15432 || dwarf_tag(die) == DW_TAG_subprogram);
15433
15434 const die_source source = rdr.get_die_source(die);
15435
15436 {
15437 size_t off = dwarf_dieoffset(die);
15438 auto i = rdr.die_wip_function_types_map(source).find(off);
15439 if (i != rdr.die_wip_function_types_map(source).end())
15440 {
15441 function_type_sptr fn_type = is_function_type(i->second);
15442 ABG_ASSERT(fn_type);
15443 return fn_type;
15444 }
15445 }
15446
15447 decl_base_sptr type_decl;
15448
15449 translation_unit_sptr tu = rdr.cur_transl_unit();
15450 ABG_ASSERT(tu);
15451
15452 /// If, inside the current translation unit, we've already seen a
15453 /// function type with the same text representation, then reuse that
15454 /// one instead.
15455 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
15456 {
15457 result = is_function_type(t);
15458 ABG_ASSERT(result);
15459 rdr.associate_die_to_type(die, result, where_offset);
15460 return result;
15461 }
15462
15463 bool odr_is_relevant = rdr.odr_is_relevant(die);
15464 if (odr_is_relevant)
15465 {
15466 // So we can rely on the One Definition Rule to say that if
15467 // several different function types have the same name (or
15468 // rather, representation) across the entire binary, then they
15469 // ought to designate the same function type. So let's ensure
15470 // that if we've already seen a function type with the same
15471 // representation as the function type 'die', then it's the same
15472 // type as the one denoted by 'die'.
15473 if (function_type_sptr fn_type =
15474 is_function_type(rdr.lookup_type_artifact_from_die(die)))
15475 {
15476 rdr.associate_die_to_type(die, fn_type, where_offset);
15477 return fn_type;
15478 }
15479 }
15480
15481 // Let's look at the DIE to detect if it's the DIE for a method
15482 // (type). If it is, we can deduce the name of its enclosing class
15483 // and if it's a static or const.
15484 bool is_const = false;
15485 bool is_static = false;
15486 Dwarf_Die object_pointer_die;
15487 Dwarf_Die class_type_die;
15488 bool has_this_parm_die =
15489 die_function_type_is_method_type(rdr, die, where_offset,
15490 object_pointer_die,
15491 class_type_die,
15492 is_static);
15493 if (has_this_parm_die)
15494 {
15495 // The function (type) has a "this" parameter DIE. It means it's
15496 // a member function DIE.
15497 if (!is_static)
15498 if (die_object_pointer_is_for_const_method(&object_pointer_die))
15499 is_const = true;
15500
15501 if (!is_method)
15502 {
15503 // We were initially called as if the function represented
15504 // by DIE was *NOT* a member function. But now we know it's
15505 // a member function. Let's take that into account.
15506 class_or_union_sptr klass_type =
15507 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
15508 /*called_from_pub_decl=*/true,
15509 where_offset));
15510 if (!klass_type)
15511 {
15512 // We could not create the class type. For instance,
15513 // this can be due to the fact that the class is
15514 // suppressed. In those cases, we just bail out.
15515 return nullptr;
15516 }
15517 is_method = klass_type;
15518 }
15519 }
15520
15521 // Let's create the type early and record it as being for the DIE
15522 // 'die'. This way, when building the sub-type triggers the
15523 // creation of a type matching the same 'die', then we'll reuse this
15524 // one.
15525
15526 result.reset(is_method
15527 ? new method_type(is_method, is_const,
15528 tu->get_address_size(),
15529 /*alignment=*/0)
15530 : new function_type(rdr.env(), tu->get_address_size(),
15531 /*alignment=*/0));
15532 rdr.associate_die_to_type(die, result, where_offset);
15533 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
15534
15535 type_base_sptr return_type;
15536 Dwarf_Die ret_type_die;
15537 if (die_die_attribute(die, DW_AT_type, ret_type_die))
15538 return_type =
15539 is_type(build_ir_node_from_die(rdr, &ret_type_die,
15540 /*called_from_public_decl=*/true,
15541 where_offset));
15542 if (!return_type)
15543 return_type = is_type(build_ir_node_for_void_type(rdr));
15544 result->set_return_type(return_type);
15545
15546 Dwarf_Die child;
15547 function_decl::parameters function_parms;
15548
15549 if (dwarf_child(die, &child) == 0)
15550 do
15551 {
15552 int child_tag = dwarf_tag(&child);
15553 if (child_tag == DW_TAG_formal_parameter)
15554 {
15555 // This is a "normal" function parameter.
15556 string name, linkage_name;
15557 location loc;
15558 die_loc_and_name(rdr, &child, loc, name, linkage_name);
15560 // Sometimes, bogus compiler emit names that are
15561 // non-ascii garbage. Let's just ditch that for now.
15562 name.clear();
15563 bool is_artificial = die_is_artificial(&child);
15564 type_base_sptr parm_type;
15565 Dwarf_Die parm_type_die;
15566 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
15567 parm_type =
15568 is_type(build_ir_node_from_die(rdr, &parm_type_die,
15569 /*called_from_public_decl=*/true,
15570 where_offset));
15571 if (!parm_type)
15572 continue;
15573 if (is_method
15574 && is_const_qualified_type(parm_type)
15575 && function_parms.empty())
15576 // We are looking at the first (implicit) parameter of a
15577 // method. This is basically the "this pointer". For
15578 // concrete instances of abstract methods, GCC sometimes
15579 // represents that pointer as a const pointer, whereas
15580 // in the abstract interface representing that method
15581 // the this-pointer is represented as a non-qualified
15582 // pointer. Let's trim the const qualifier away. That
15583 // will minize the chance to have spurious
15584 // const-qualifier changes on implicit parameters when
15585 // comparing methods that otherwise have no meaningful
15586 // ABI changes.
15587 parm_type =
15589
15591 (new function_decl::parameter(parm_type, name, loc,
15592 /*variadic_marker=*/false,
15593 is_artificial));
15594 function_parms.push_back(p);
15595 }
15596 else if (child_tag == DW_TAG_unspecified_parameters)
15597 {
15598 // This is a variadic function parameter.
15599 bool is_artificial = die_is_artificial(&child);
15600
15601 type_base_sptr parm_type =
15602 is_type(build_ir_node_for_variadic_parameter_type(rdr));
15604 (new function_decl::parameter(parm_type,
15605 /*name=*/"",
15606 location(),
15607 /*variadic_marker=*/true,
15608 is_artificial));
15609 function_parms.push_back(p);
15610 // After a DW_TAG_unspecified_parameters tag, we shouldn't
15611 // keep reading for parameters. The
15612 // unspecified_parameters TAG should be the last parameter
15613 // that we record. For instance, if there are multiple
15614 // DW_TAG_unspecified_parameters DIEs then we should care
15615 // only for the first one.
15616 break;
15617 }
15618 }
15619 while (dwarf_siblingof(&child, &child) == 0);
15620
15621 result->set_parameters(function_parms);
15622
15623 tu->bind_function_type_life_time(result);
15624
15625 result->set_is_artificial(true);
15626
15627 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
15628
15629 {
15630 die_function_type_map_type::const_iterator i =
15631 rdr.die_wip_function_types_map(source).
15632 find(dwarf_dieoffset(die));
15633 if (i != rdr.die_wip_function_types_map(source).end())
15634 rdr.die_wip_function_types_map(source).erase(i);
15635 }
15636
15637 maybe_canonicalize_type(result, rdr);
15638 return result;
15639}
15640
15641/// Build a subrange type from a DW_TAG_subrange_type.
15642///
15643/// @param rdr the DWARF reader to consider.
15644///
15645/// @param die the DIE to read from.
15646///
15647/// @param where_offset the offset of the DIE where we are "logically"
15648/// positionned at in the DIE tree. This is useful when @p die is
15649/// e,g, DW_TAG_partial_unit that can be included in several places in
15650/// the DIE tree.
15651///
15652/// @param associate_die_to_type if this is true then the resulting
15653/// type is associated to the @p die, so that next time when the
15654/// system looks up the type associated to it, the current resulting
15655/// type is returned. If false, then no association is done and the
15656/// resulting type can be destroyed right after. This can be useful
15657/// when the sole purpose of building the @ref
15658/// array_type_def::subrange_type is to use some of its method like,
15659/// e.g, its name pretty printing methods.
15660///
15661/// @return the newly built instance of @ref
15662/// array_type_def::subrange_type, or nil if no type could be built.
15664build_subrange_type(reader& rdr,
15665 const Dwarf_Die* die,
15666 size_t where_offset,
15667 bool associate_type_to_die)
15668{
15670
15671 if (!die)
15672 return result;
15673
15674 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
15675 if (tag != DW_TAG_subrange_type)
15676 return result;
15677
15678 string name = die_name(die);
15679
15680 // load the underlying type.
15681 Dwarf_Die underlying_type_die;
15682 type_base_sptr underlying_type;
15683 /* Unless there is an underlying type which says differently. */
15684 bool is_signed = false;
15685 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
15686 underlying_type =
15687 is_type(build_ir_node_from_die(rdr,
15688 &underlying_type_die,
15689 /*called_from_public_decl=*/true,
15690 where_offset));
15691
15692 if (underlying_type)
15693 {
15694 uint64_t ate;
15695 if (die_unsigned_constant_attribute (&underlying_type_die,
15696 DW_AT_encoding,
15697 ate))
15698 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
15699 }
15700
15701 // The DW_TAG_subrange_type DIE may have some size related
15702 // attributes (DW_AT_byte_size or DW_AT_bit_size). If not, then the
15703 // size is deduced from the size of its underlying type.
15704 bool has_size_info = false;
15705 uint64_t size = 0;
15706 if ((has_size_info = die_unsigned_constant_attribute(die,
15707 DW_AT_byte_size, size)))
15708 size *= 8;
15709 else
15710 has_size_info = die_unsigned_constant_attribute(die,
15711 DW_AT_bit_size, size);
15712
15713 translation_unit::language language = rdr.cur_transl_unit()->get_language();
15714 array_type_def::subrange_type::bound_value lower_bound =
15715 get_default_array_lower_bound(language);
15716 array_type_def::subrange_type::bound_value upper_bound;
15717 uint64_t count = 0;
15718 bool is_non_finite = false;
15719 bool non_zero_count_present = false;
15720
15721 // The DWARF 4 specifications says, in [5.11 Subrange
15722 // Type Entries]:
15723 //
15724 // The subrange entry may have the attributes
15725 // DW_AT_lower_bound and DW_AT_upper_bound to
15726 // specify, respectively, the lower and upper bound
15727 // values of the subrange.
15728 //
15729 // So let's look for DW_AT_lower_bound first.
15730 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
15731
15732 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
15733 is_signed, upper_bound);
15734 if (!found_upper_bound)
15735 found_upper_bound = subrange_die_indirect_bound_value(die,
15736 DW_AT_upper_bound,
15737 upper_bound,
15738 is_signed);
15739 // Then, DW_AT_upper_bound.
15740 if (!found_upper_bound)
15741 {
15742 // The DWARF 4 spec says, in [5.11 Subrange Type
15743 // Entries]:
15744 //
15745 // The DW_AT_upper_bound attribute may be replaced
15746 // by a DW_AT_count attribute, whose value
15747 // describes the number of elements in the
15748 // subrange rather than the value of the last
15749 // element."
15750 //
15751 // So, as DW_AT_upper_bound is not present in this
15752 // case, let's see if there is a DW_AT_count.
15753 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
15754 {
15755 if (count)
15756 // DW_AT_count can be present and be set to zero. This is
15757 // for instance the case to model this gcc extension to
15758 // represent flexible arrays:
15759 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
15760 // For instance: int flex_array[0];
15761 non_zero_count_present = true;
15762
15763 // When the count is present and non-zero, we can deduce the
15764 // upper_bound from the lower_bound and the number of
15765 // elements of the array:
15766 int64_t u = lower_bound.get_signed_value() + count;
15767 if (u)
15768 upper_bound = u - 1;
15769 }
15770
15771 if (!non_zero_count_present)
15772 // No upper_bound nor count was present on the DIE, this means
15773 // the array is considered to have an infinite (or rather not
15774 // known) size.
15775 is_non_finite = true;
15776 }
15777
15778 if (UINT64_MAX == upper_bound.get_unsigned_value())
15779 // If the upper_bound size is the max of the integer value
15780 // then it most certainly means unknown size.
15781 is_non_finite = true;
15782
15783 result.reset
15784 (new array_type_def::subrange_type(rdr.env(),
15785 name,
15786 lower_bound,
15787 upper_bound,
15788 underlying_type,
15789 location()));
15790 result->is_non_finite(is_non_finite);
15791
15792 if (has_size_info)
15793 result->set_size_in_bits(size);
15794 else
15795 {
15796 // The DW_TAG_subrange_type doesn't appear to have any size
15797 // attribute. In that case, the size is deduced from the size
15798 // of the underlying type. If there is no underlying type
15799 // specified, then the size of the subrange type is the size
15800 if (!underlying_type)
15801 result->set_size_in_bits(rdr.cur_transl_unit()->get_address_size());
15802 }
15803
15804 // Let's ensure the resulting subrange looks metabolically healthy.
15805 ABG_ASSERT(result->is_non_finite()
15806 || (result->get_length() ==
15807 (uint64_t) (result->get_upper_bound()
15808 - result->get_lower_bound() + 1)));
15809
15810 if (associate_type_to_die)
15811 rdr.associate_die_to_type(die, result, where_offset);
15812
15813 return result;
15814}
15815
15816/// Build the sub-ranges of an array type.
15817///
15818/// This is a sub-routine of build_array_type().
15819///
15820/// @param rdr the context to read from.
15821///
15822/// @param die the DIE of tag DW_TAG_array_type which contains
15823/// children DIEs that represent the sub-ranges.
15824///
15825/// @param subranges out parameter. This is set to the sub-ranges
15826/// that are built from @p die.
15827///
15828/// @param where_offset the offset of the DIE where we are "logically"
15829/// positioned at, in the DIE tree. This is useful when @p die is
15830/// e.g, DW_TAG_partial_unit that can be included in several places in
15831/// the DIE tree.
15832static void
15833build_subranges_from_array_type_die(const reader& rdr,
15834 const Dwarf_Die* die,
15836 size_t where_offset,
15837 bool associate_type_to_die)
15838{
15839 Dwarf_Die child;
15840
15841 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
15842 {
15843 do
15844 {
15845 int child_tag = dwarf_tag(&child);
15846 if (child_tag == DW_TAG_subrange_type)
15847 {
15849 if (associate_type_to_die)
15850 {
15851 // We are being called to create the type, add it to
15852 // the current type graph and associate it to the
15853 // DIE it's been created from.
15854 type_or_decl_base_sptr t =
15855 build_ir_node_from_die(const_cast<reader&>(rdr), &child,
15856 /*called_from_public_decl=*/true,
15857 where_offset);
15858 s = is_subrange_type(t);
15859 }
15860 else
15861 // We are being called to create the type but *NOT*
15862 // add it to the current tyupe tree, *NOR* associate
15863 // it to the DIE it's been created from.
15864 s = build_subrange_type(const_cast<reader&>(rdr), &child,
15865 where_offset,
15866 /*associate_type_to_die=*/false);
15867 if (s)
15868 subranges.push_back(s);
15869 }
15870 }
15871 while (dwarf_siblingof(&child, &child) == 0);
15872 }
15873}
15874
15875/// Build an array type from a DW_TAG_array_type DIE.
15876///
15877/// @param rdr the DWARF reader to consider.
15878///
15879/// @param die the DIE to read from.
15880///
15881/// @param called_from_public_decl true if this function was called
15882/// from a context where either a public function or a public variable
15883/// is being built.
15884///
15885/// @param where_offset the offset of the DIE where we are "logically"
15886/// positioned at, in the DIE tree. This is useful when @p die is
15887/// e.g, DW_TAG_partial_unit that can be included in several places in
15888/// the DIE tree.
15889///
15890/// @return a pointer to the resulting array_type_def.
15892build_array_type(reader& rdr,
15893 Dwarf_Die* die,
15894 bool called_from_public_decl,
15895 size_t where_offset)
15896{
15897 array_type_def_sptr result;
15898
15899 if (!die)
15900 return result;
15901
15902 unsigned tag = dwarf_tag(die);
15903 if (tag != DW_TAG_array_type)
15904 return result;
15905
15906 decl_base_sptr type_decl;
15907 Dwarf_Die type_die;
15908
15909 if (die_die_attribute(die, DW_AT_type, type_die))
15910 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
15911 called_from_public_decl,
15912 where_offset));
15913 if (!type_decl)
15914 return result;
15915
15916 // The call to build_ir_node_from_die() could have triggered the
15917 // creation of the type for this DIE. In that case, just return it.
15918 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15919 {
15920 result = is_array_type(t);
15921 ABG_ASSERT(result);
15922 return result;
15923 }
15924
15925 type_base_sptr type = is_type(type_decl);
15926 ABG_ASSERT(type);
15927
15929
15930 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
15931
15932 result.reset(new array_type_def(type, subranges, location()));
15933 rdr.associate_die_to_type(die, result, where_offset);
15934 return result;
15935}
15936
15937/// Create a typedef_decl from a DW_TAG_typedef DIE.
15938///
15939/// @param rdr the DWARF reader to consider.
15940///
15941/// @param die the DIE to read from.
15942///
15943/// @param called_from_public_decl true if this function was called
15944/// from a context where either a public function or a public variable
15945/// is being built.
15946///
15947/// @param where_offset the offset of the DIE where we are "logically"
15948/// positionned at, in the DIE tree. This is useful when @p die is
15949/// e.g, DW_TAG_partial_unit that can be included in several places in
15950/// the DIE tree.
15951///
15952/// @return the newly created typedef_decl.
15953static typedef_decl_sptr
15954build_typedef_type(reader& rdr,
15955 Dwarf_Die* die,
15956 bool called_from_public_decl,
15957 size_t where_offset)
15958{
15959 typedef_decl_sptr result;
15960
15961 if (!die)
15962 return result;
15963
15964 unsigned tag = dwarf_tag(die);
15965 if (tag != DW_TAG_typedef)
15966 return result;
15967
15968 string name, linkage_name;
15969 location loc;
15970 die_loc_and_name(rdr, die, loc, name, linkage_name);
15971
15972 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
15973 if (loc)
15974 result = lookup_typedef_type_per_location(loc.expand(), *corp);
15975
15976 if (!result)
15977 {
15978 type_base_sptr utype;
15979 Dwarf_Die underlying_type_die;
15980 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15981 // A typedef DIE with no underlying type means a typedef to
15982 // void type.
15983 utype = rdr.env().get_void_type();
15984
15985 if (!utype)
15986 utype =
15987 is_type(build_ir_node_from_die(rdr,
15988 &underlying_type_die,
15989 called_from_public_decl,
15990 where_offset));
15991 if (!utype)
15992 return result;
15993
15994 ABG_ASSERT(utype);
15995 result.reset(new typedef_decl(name, utype, loc, linkage_name));
15996
15997 if ((is_class_or_union_type(utype) || is_enum_type(utype))
15998 && is_anonymous_type(utype))
15999 {
16000 // This is a naming typedef for an enum or a class. Let's
16001 // mark the underlying decl as such.
16002 decl_base_sptr decl = is_decl(utype);
16003 ABG_ASSERT(decl);
16004 decl->set_naming_typedef(result);
16005 rdr.maybe_schedule_decl_only_type_for_resolution(utype);
16006 }
16007 }
16008
16009 rdr.associate_die_to_type(die, result, where_offset);
16010
16011 return result;
16012}
16013
16014/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
16015/// denoted by the DIE is not suppressed by a suppression
16016/// specification associated to the current DWARF reader.
16017///
16018/// Note that if a member variable declaration with the same name as
16019/// the name of the DIE we are looking at exists, this function returns
16020/// that existing variable declaration.
16021///
16022/// @param rdr the DWARF reader to use.
16023///
16024/// @param die the DIE representing the variable we are looking at.
16025///
16026/// @param where_offset the offset of the DIE where we are "logically"
16027/// positionned at, in the DIE tree. This is useful when @p die is
16028/// e.g, DW_TAG_partial_unit that can be included in several places in
16029/// the DIE tree.
16030///
16031/// @param is_declaration_only if true, it means the variable DIE has
16032/// the is_declaration_only only attribute.
16033///
16034/// @param result if this is set to an existing var_decl, this means
16035/// that the function will append the new properties it sees on @p die
16036/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16037/// new var_decl is going to be allocated and returned.
16038///
16039/// @param is_required_decl_spec this is true iff the variable to
16040/// build is referred to as being the specification of another
16041/// variable.
16042///
16043/// @return a pointer to the newly created var_decl. If the var_decl
16044/// could not be built, this function returns NULL.
16045static var_decl_sptr
16046build_or_get_var_decl_if_not_suppressed(reader& rdr,
16047 scope_decl *scope,
16048 Dwarf_Die *die,
16049 size_t where_offset,
16050 bool is_declaration_only,
16051 var_decl_sptr result,
16052 bool is_required_decl_spec)
16053{
16054 var_decl_sptr var;
16055 if (variable_is_suppressed(rdr, scope, die,
16056 is_declaration_only,
16057 is_required_decl_spec))
16058 {
16059 ++rdr.stats_.number_of_suppressed_variables;
16060 return var;
16061 }
16062
16063 if (class_decl* class_type = is_class_type(scope))
16064 {
16065 string var_name = die_name(die);
16066 if (!var_name.empty())
16067 if ((var = class_type->find_data_member(var_name)))
16068 return var;
16069 }
16070
16071 // The variable was not suppressed.
16072 ++rdr.stats_.number_of_suppressed_variables;
16073
16074 var = build_var_decl(rdr, die, where_offset, result);
16075 return var;
16076}
16077
16078/// Build a @ref var_decl out of a DW_TAG_variable DIE.
16079///
16080/// @param rdr the DWARF reader to use.
16081///
16082/// @param die the DIE representing the variable we are looking at.
16083///
16084/// @param where_offset the offset of the DIE where we are "logically"
16085/// positionned at, in the DIE tree. This is useful when @p die is
16086/// e.g, DW_TAG_partial_unit that can be included in several places in
16087/// the DIE tree.
16088///
16089/// @param result if this is set to an existing var_decl, this means
16090/// that the function will append the new properties it sees on @p die
16091/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16092/// new var_decl is going to be allocated and returned.
16093///
16094/// @return a pointer to the newly created var_decl. If the var_decl
16095/// could not be built, this function returns NULL.
16096static var_decl_sptr
16097build_var_decl(reader& rdr,
16098 Dwarf_Die *die,
16099 size_t where_offset,
16100 var_decl_sptr result)
16101{
16102 if (!die)
16103 return result;
16104
16105 int tag = dwarf_tag(die);
16106 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
16107
16108 if (!die_is_public_decl(die))
16109 return result;
16110
16111 type_base_sptr type;
16112 Dwarf_Die type_die;
16113 if (die_die_attribute(die, DW_AT_type, type_die))
16114 {
16115 decl_base_sptr ty =
16116 is_decl(build_ir_node_from_die(rdr, &type_die,
16117 /*called_from_public_decl=*/true,
16118 where_offset));
16119 if (!ty)
16120 return result;
16121 type = is_type(ty);
16122 ABG_ASSERT(type);
16123 }
16124
16125 if (!type && !result)
16126 return result;
16127
16128 string name, linkage_name;
16129 location loc;
16130 die_loc_and_name(rdr, die, loc, name, linkage_name);
16131
16132 if (!result)
16133 result.reset(new var_decl(name, type, loc, linkage_name));
16134 else
16135 {
16136 // We were called to append properties that might have been
16137 // missing from the first version of the variable. And usually
16138 // that missing property is the mangled name or the type.
16139 if (!linkage_name.empty())
16140 result->set_linkage_name(linkage_name);
16141
16142 if (type)
16143 result->set_type(type);
16144 }
16145
16146 // Check if a variable symbol with this name is exported by the elf
16147 // binary. If it is, then set the symbol of the variable, if it's
16148 // not set already.
16149 if (!result->get_symbol())
16150 {
16151 elf_symbol_sptr var_sym;
16152 Dwarf_Addr var_addr;
16153
16154 if (rdr.get_variable_address(die, var_addr))
16155 {
16156 rdr.symtab()->
16157 update_main_symbol(var_addr,
16158 result->get_linkage_name().empty()
16159 ? result->get_name()
16160 : result->get_linkage_name());
16161 var_sym = rdr.variable_symbol_is_exported(var_addr);
16162 }
16163
16164 if (var_sym)
16165 {
16166 result->set_symbol(var_sym);
16167 // If the linkage name is not set or is wrong, set it to
16168 // the name of the underlying symbol.
16169 string linkage_name = result->get_linkage_name();
16170 if (linkage_name.empty()
16171 || !var_sym->get_alias_from_name(linkage_name))
16172 result->set_linkage_name(var_sym->get_name());
16173 result->set_is_in_public_symbol_table(true);
16174 }
16175
16176 if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
16177 {
16178 // We are looking at a global variable which symbol is
16179 // undefined. Let's set its symbol.
16180 string n = result->get_linkage_name();
16181 if (n.empty())
16182 n = result->get_name();
16183 var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
16184 if (var_sym)
16185 {
16186 result->set_symbol(var_sym);
16187 result->set_is_in_public_symbol_table(false);
16188 }
16189 }
16190 }
16191
16192 return result;
16193}
16194
16195/// Test if a given function denoted by its DIE and its scope is
16196/// suppressed by any of the suppression specifications associated to
16197/// a given context of ELF/DWARF reading.
16198///
16199/// Note that a non-member function which symbol is not exported is
16200/// also suppressed.
16201///
16202/// @param rdr the ELF/DWARF reading content of interest.
16203///
16204/// @param scope of the scope of the function.
16205///
16206/// @param function_die the DIE representing the function.
16207///
16208/// @param is_declaration_only is true if the DIE denoted by @p die is
16209/// a declaration-only DIE.
16210///
16211/// @return true iff @p function_die is suppressed by at least one
16212/// suppression specification attached to the @p rdr.
16213static bool
16214function_is_suppressed(const reader& rdr,
16215 const scope_decl* scope,
16216 Dwarf_Die *function_die,
16217 bool is_declaration_only)
16218{
16219 if (function_die == 0
16220 || dwarf_tag(function_die) != DW_TAG_subprogram)
16221 return false;
16222
16223 string fname = die_string_attribute(function_die, DW_AT_name);
16224 string flinkage_name = die_linkage_name(function_die);
16225 if (flinkage_name.empty() && die_is_in_c(function_die))
16226 flinkage_name = fname;
16227 string qualified_name = build_qualified_name(scope, fname);
16228
16229 // A non-member non-static function which symbol is not exported is
16230 // suppressed.
16231 //
16232 // Note that if the non-member non-static function has an undefined
16233 // symbol, by default, it's not suppressed. Unless we are asked to
16234 // drop undefined symbols too.
16235 if (!is_class_type(scope)
16236 && (!is_declaration_only || rdr.drop_undefined_syms()))
16237 {
16238 Dwarf_Addr fn_addr;
16239 if (!rdr.get_function_address(function_die, fn_addr))
16240 return true;
16241
16242 elf_symbol_sptr symbol =
16243 rdr.function_symbol_is_exported(fn_addr);
16244 if (!symbol)
16245 return true;
16246 if (symbol->is_suppressed())
16247 return true;
16248
16249 // Since there is only one symbol in DWARF associated with an elf_symbol,
16250 // we can assume this is the main symbol then. Otherwise the main hinting
16251 // did not work as expected.
16252 ABG_ASSERT(symbol->is_main_symbol());
16253 if (symbol->has_aliases())
16254 for (elf_symbol_sptr a = symbol->get_next_alias();
16255 !a->is_main_symbol(); a = a->get_next_alias())
16256 if (a->is_suppressed())
16257 return true;
16258 }
16259
16260 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
16261 /*require_drop_property=*/true);
16262}
16263
16264/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
16265/// function denoted by the DIE is not suppressed by a suppression
16266/// specification associated to the current DWARF reader.
16267///
16268/// Note that if a member function declaration with the same signature
16269/// (pretty representation) as one of the DIE we are looking at
16270/// exists, this function returns that existing function declaration.
16271/// Similarly, if there is already a constructed member function with
16272/// the same linkage name as the one on the DIE, this function returns
16273/// that member function.
16274///
16275/// Also note that the function_decl IR returned by this function must
16276/// be passed to finish_member_function_reading because several
16277/// properties from the DIE are actually read by that function, and
16278/// the corresponding properties on the function_decl IR are updated
16279/// accordingly. This is done to support "updating" a function_decl
16280/// IR with properties scathered across several DIEs.
16281///
16282/// @param rdr the DWARF reader to use.
16283///
16284/// @param scope the scope of the function we are looking at.
16285///
16286/// @param fn_die the DIE representing the function we are looking at.
16287///
16288/// @param where_offset the offset of the DIE where we are "logically"
16289/// positionned at, in the DIE tree. This is useful when @p die is
16290/// e.g, DW_TAG_partial_unit that can be included in several places in
16291/// the DIE tree.
16292///
16293/// @param is_declaration_only is true if the DIE denoted by @p fn_die
16294/// is a declaration-only DIE.
16295///
16296/// @param result if this is set to an existing function_decl, this
16297/// means that the function will append the new properties it sees on
16298/// @p fn_die to that exising function_decl. Otherwise, if this
16299/// parameter is NULL, a new function_decl is going to be allocated
16300/// and returned.
16301///
16302/// @return a pointer to the newly created var_decl. If the var_decl
16303/// could not be built, this function returns NULL.
16304static function_decl_sptr
16305build_or_get_fn_decl_if_not_suppressed(reader& rdr,
16306 scope_decl *scope,
16307 Dwarf_Die *fn_die,
16308 size_t where_offset,
16309 bool is_declaration_only,
16310 function_decl_sptr result)
16311{
16313 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
16314 {
16315 ++rdr.stats_.number_of_suppressed_functions;
16316 return fn;
16317 }
16318
16319 string name = die_name(fn_die);
16320 string linkage_name = die_linkage_name(fn_die);
16321 bool is_dtor = !name.empty() && name[0]== '~';
16322 bool is_virtual = false;
16323 if (is_dtor)
16324 {
16325 Dwarf_Attribute attr;
16326 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
16327 DW_AT_vtable_elem_location,
16328 &attr))
16329 is_virtual = true;
16330 }
16331
16332
16333 // If we've already built an IR for a function with the same
16334 // signature (from another DIE), reuse it, unless that function is a
16335 // virtual C++ destructor. Several virtual C++ destructors with the
16336 // same signature can be implemented by several different ELF
16337 // symbols. So re-using C++ destructors like that can lead to us
16338 // missing some destructors.
16339 if (!result && (!(is_dtor && is_virtual)))
16340 {
16341 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
16342 {
16343 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
16344 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
16345 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
16346 return fn;
16347 }
16348 }
16349
16350 // The function was not suppressed.
16351 ++rdr.stats_.number_of_allowed_functions;
16352
16353 // If a member function with the same linkage name as the one
16354 // carried by the DIE already exists, then return it.
16355 if (class_decl* klass = is_class_type(scope))
16356 {
16357 string linkage_name = die_linkage_name(fn_die);
16358 fn = klass->find_member_function_sptr(linkage_name);
16359 if (fn)
16360 // We found a member function that has the same signature.
16361 // Let's mark it for update.
16362 result = fn;
16363 }
16364
16365 if (!fn || !fn->get_symbol())
16366 // We haven't yet been able to construct a function IR, or, we
16367 // have one 'partial' function IR that doesn't have any associated
16368 // symbol yet. Note that in the later case, a function IR without
16369 // any associated symbol will be dropped on the floor by
16370 // potential_member_fn_should_be_dropped. So let's build or a new
16371 // function IR or complete the existing partial IR.
16372 fn = build_function_decl(rdr, fn_die, where_offset, result);
16373
16374 return fn;
16375}
16376
16377/// Test if a given variable denoted by its DIE and its scope is
16378/// suppressed by any of the suppression specifications associated to
16379/// a given context of ELF/DWARF reading.
16380///
16381/// @param rdr the ELF/DWARF reading content of interest.
16382///
16383/// @param scope of the scope of the variable.
16384///
16385/// @param variable_die the DIE representing the variable.
16386///
16387/// @param is_declaration_only true if the variable is supposed to be
16388/// decl-only.
16389///
16390/// @param is_required_decl_spec if true, means that the @p
16391/// variable_die being considered is for a variable decl that is a
16392/// specification for a concrete variable being built.
16393///
16394/// @return true iff @p variable_die is suppressed by at least one
16395/// suppression specification attached to the @p rdr.
16396static bool
16397variable_is_suppressed(const reader& rdr,
16398 const scope_decl* scope,
16399 Dwarf_Die *variable_die,
16400 bool is_declaration_only,
16401 bool is_required_decl_spec)
16402{
16403 if (variable_die == 0
16404 || (dwarf_tag(variable_die) != DW_TAG_variable
16405 && dwarf_tag(variable_die) != DW_TAG_member))
16406 return false;
16407
16408 string name = die_string_attribute(variable_die, DW_AT_name);
16409 string linkage_name = die_linkage_name(variable_die);
16410 if (linkage_name.empty() && die_is_in_c(variable_die))
16411 linkage_name = name;
16412 string qualified_name = build_qualified_name(scope, name);
16413
16414 // If a non member variable that is a declaration (has no defined
16415 // and exported symbol) and is not the specification of another
16416 // concrete variable, then it's suppressed. This is a size
16417 // optimization; it removes useless declaration-only variables from
16418 // the IR.
16419 if (!is_class_type(scope)
16420 && !is_required_decl_spec
16421 // If we are asked to load undefined interfaces, then we don't
16422 // suppress declaration-only variables as they might have
16423 // undefined elf-symbols.
16424 && (!is_declaration_only || !rdr.load_undefined_interfaces()))
16425 {
16426 Dwarf_Addr var_addr = 0;
16427 if (!rdr.get_variable_address(variable_die, var_addr))
16428 return true;
16429
16430 elf_symbol_sptr symbol =
16431 rdr.variable_symbol_is_exported(var_addr);
16432 if (!symbol)
16433 return true;
16434 if (symbol->is_suppressed())
16435 return true;
16436
16437 // Since there is only one symbol in DWARF associated with an elf_symbol,
16438 // we can assume this is the main symbol then. Otherwise the main hinting
16439 // did not work as expected.
16440 ABG_ASSERT(symbol->is_main_symbol());
16441 if (symbol->has_aliases())
16442 for (elf_symbol_sptr a = symbol->get_next_alias();
16443 !a->is_main_symbol(); a = a->get_next_alias())
16444 if (a->is_suppressed())
16445 return true;
16446 }
16447
16449 qualified_name,
16450 linkage_name,
16451 /*require_drop_property=*/true);
16452}
16453
16454/// Test if a type (designated by a given DIE) in a given scope is
16455/// suppressed by the suppression specifications that are associated
16456/// to a given DWARF reader.
16457///
16458/// @param rdr the DWARF reader to consider.
16459///
16460/// @param scope of the scope of the type DIE to consider.
16461///
16462/// @param type_die the DIE that designates the type to consider.
16463///
16464/// @param type_is_opaque out parameter. If this function returns
16465/// true (the type @p type_die is suppressed) and if the type was
16466/// suppressed because it's opaque then this parameter is set to
16467/// true.
16468///
16469/// @return true iff the type designated by the DIE @p type_die, in
16470/// the scope @p scope is suppressed by at the suppression
16471/// specifications associated to the current DWARF reader.
16472static bool
16473type_is_suppressed(const reader& rdr,
16474 const scope_decl* scope,
16475 Dwarf_Die *type_die,
16476 bool &type_is_opaque)
16477{
16478 if (type_die == 0
16479 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
16480 && dwarf_tag(type_die) != DW_TAG_class_type
16481 && dwarf_tag(type_die) != DW_TAG_structure_type
16482 && dwarf_tag(type_die) != DW_TAG_union_type))
16483 return false;
16484
16485 string type_name, linkage_name;
16486 location type_location;
16487 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16488 string qualified_name = build_qualified_name(scope, type_name);
16489
16490 return suppr::is_type_suppressed(rdr,
16491 qualified_name,
16492 type_location,
16493 type_is_opaque,
16494 /*require_drop_property=*/true);
16495}
16496
16497/// Test if a type (designated by a given DIE) in a given scope is
16498/// suppressed by the suppression specifications that are associated
16499/// to a given DWARF reader.
16500///
16501/// @param rdr the DWARF reader to consider.
16502///
16503/// @param scope of the scope of the type DIE to consider.
16504///
16505/// @param type_die the DIE that designates the type to consider.
16506///
16507/// @return true iff the type designated by the DIE @p type_die, in
16508/// the scope @p scope is suppressed by at the suppression
16509/// specifications associated to the current DWARF reader.
16510static bool
16511type_is_suppressed(const reader& rdr,
16512 const scope_decl* scope,
16513 Dwarf_Die *type_die)
16514{
16515 bool type_is_opaque = false;
16516 return type_is_suppressed(rdr, scope, type_die, type_is_opaque);
16517}
16518
16519/// Get the opaque version of a type that was suppressed because it's
16520/// a private type.
16521///
16522/// The opaque version version of the type is just a declared-only
16523/// version of the type (class, union or enum type) denoted by @p
16524/// type_die.
16525///
16526/// @param rdr the DWARF reader in use.
16527///
16528/// @param scope the scope of the type die we are looking at.
16529///
16530/// @param type_die the type DIE we are looking at.
16531///
16532/// @param where_offset the offset of the DIE where we are "logically"
16533/// positionned at, in the DIE tree. This is useful when @p die is
16534/// e.g, DW_TAG_partial_unit that can be included in several places in
16535/// the DIE tree.
16536///
16537/// @return the opaque version of the type denoted by @p type_die or
16538/// nil if no opaque version was found.
16539static type_or_decl_base_sptr
16540get_opaque_version_of_type(reader &rdr,
16541 scope_decl *scope,
16542 Dwarf_Die *type_die,
16543 size_t where_offset)
16544{
16545 type_or_decl_base_sptr result;
16546
16547 if (type_die == 0)
16548 return result;
16549
16550 unsigned tag = dwarf_tag(type_die);
16551 if (tag != DW_TAG_class_type
16552 && tag != DW_TAG_structure_type
16553 && tag != DW_TAG_union_type
16554 && tag != DW_TAG_enumeration_type)
16555 return result;
16556
16557 string type_name, linkage_name;
16558 location type_location;
16559 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16560
16561 string qualified_name = build_qualified_name(scope, type_name);
16562
16563 //
16564 // TODO: also handle declaration-only unions. To do that, we mostly
16565 // need to adapt add_or_update_union_type to make it schedule
16566 // declaration-only unions for resolution too.
16567 //
16568 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
16569 {
16570 string_classes_or_unions_map::const_iterator i =
16571 rdr.declaration_only_classes().find(qualified_name);
16572 if (i != rdr.declaration_only_classes().end())
16573 result = i->second.back();
16574
16575 if (!result)
16576 {
16577 // So we didn't find any pre-existing forward-declared-only
16578 // class for the class definition that we could return as an
16579 // opaque type. So let's build one.
16580 //
16581 // TODO: we need to be able to do this for unions too!
16582 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
16583 /*alignment=*/0, /*size=*/0,
16584 tag == DW_TAG_structure_type,
16585 type_location,
16586 decl_base::VISIBILITY_DEFAULT));
16587 klass->set_is_declaration_only(true);
16588 klass->set_is_artificial(die_is_artificial(type_die));
16589 add_decl_to_scope(klass, scope);
16590 rdr.associate_die_to_type(type_die, klass, where_offset);
16591 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
16592 result = klass;
16593 }
16594 }
16595
16596 if (tag == DW_TAG_enumeration_type)
16597 {
16598 string_enums_map::const_iterator i =
16599 rdr.declaration_only_enums().find(qualified_name);
16600 if (i != rdr.declaration_only_enums().end())
16601 result = i->second.back();
16602
16603 if (!result)
16604 {
16605 uint64_t size = 0;
16606 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
16607 size *= 8;
16608 type_decl_sptr underlying_type =
16609 build_enum_underlying_type(rdr, type_name, size,
16610 /*anonymous=*/true);
16611 enum_type_decl::enumerators enumeratorz;
16612 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
16613 type_location,
16614 underlying_type,
16615 enumeratorz,
16616 linkage_name));
16617 enum_type->set_is_artificial(die_is_artificial(type_die));
16618 add_decl_to_scope(enum_type, scope);
16619 result = enum_type;
16620 }
16621 }
16622
16623 return result;
16624}
16625
16626/// Create a function symbol with a given name.
16627///
16628/// @param sym_name the name of the symbol to create.
16629///
16630/// @param env the environment to create the symbol in.
16631///
16632/// @return the newly created symbol.
16634create_default_fn_sym(const string& sym_name, const environment& env)
16635{
16637 elf_symbol_sptr result =
16639 /*symbol index=*/ 0,
16640 /*symbol size=*/ 0,
16641 sym_name,
16642 /*symbol type=*/ elf_symbol::FUNC_TYPE,
16643 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
16644 /*symbol is defined=*/ true,
16645 /*symbol is common=*/ false,
16646 /*symbol version=*/ ver,
16647 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
16648 return result;
16649}
16650
16651/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
16652///
16653/// @param rdr the DWARF reader to use
16654///
16655/// @param die the DW_TAG_subprogram DIE to read from.
16656///
16657/// @param where_offset the offset of the DIE where we are "logically"
16658/// positionned at, in the DIE tree. This is useful when @p die is
16659/// e.g, DW_TAG_partial_unit that can be included in several places in
16660/// the DIE tree.
16661///
16662/// @param called_for_public_decl this is set to true if the function
16663/// was called for a public (function) decl.
16664static function_decl_sptr
16665build_function_decl(reader& rdr,
16666 Dwarf_Die* die,
16667 size_t where_offset,
16669{
16670 function_decl_sptr result = fn;
16671 if (!die)
16672 return result;
16673 int tag = dwarf_tag(die);
16674 ABG_ASSERT(tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine);
16675
16676 if (!die_is_public_decl(die))
16677 return result;
16678
16679 translation_unit_sptr tu = rdr.cur_transl_unit();
16680 ABG_ASSERT(tu);
16681
16682 string fname, flinkage_name;
16683 location floc;
16684 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
16685
16686 size_t is_inline = die_is_declared_inline(die);
16687 class_or_union_sptr is_method =
16688 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
16689
16690 if (result)
16691 {
16692 // Add the properties that might have been missing from the
16693 // first declaration of the function. For now, it usually is
16694 // the mangled name that goes missing in the first declarations.
16695 //
16696 // Also note that if 'fn' has just been cloned, the current
16697 // linkage name (of the current DIE) might be different from the
16698 // linkage name of 'fn'. In that case, update the linkage name
16699 // of 'fn' too.
16700 if (!flinkage_name.empty()
16701 && result->get_linkage_name() != flinkage_name)
16702 result->set_linkage_name(flinkage_name);
16703 if (floc)
16704 if (!result->get_location())
16705 result->set_location(floc);
16706 result->is_declared_inline(is_inline);
16707 }
16708 else
16709 {
16710 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
16711 where_offset));
16712 if (!fn_type)
16713 return result;
16714
16715 maybe_canonicalize_type(fn_type, rdr);
16716
16717 result.reset(is_method
16718 ? new method_decl(fname, fn_type,
16719 is_inline, floc,
16720 flinkage_name)
16721 : new function_decl(fname, fn_type,
16722 is_inline, floc,
16723 flinkage_name));
16724 }
16725
16726 // Set the symbol of the function. If the linkage name is not set
16727 // or is wrong, set it to the name of the underlying symbol.
16728 if (!result->get_symbol())
16729 {
16730 elf_symbol_sptr fn_sym;
16731 Dwarf_Addr fn_addr;
16732 if (rdr.get_function_address(die, fn_addr))
16733 {
16734 rdr.symtab()->
16735 update_main_symbol(fn_addr,
16736 result->get_linkage_name().empty()
16737 ? result->get_name()
16738 : result->get_linkage_name());
16739 fn_sym = rdr.function_symbol_is_exported(fn_addr);
16740 }
16741
16742 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
16743 {
16744 result->set_symbol(fn_sym);
16745 string linkage_name = result->get_linkage_name();
16746 if (linkage_name.empty())
16747 result->set_linkage_name(fn_sym->get_name());
16748 result->set_is_in_public_symbol_table(true);
16749 }
16750
16751 if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
16752 {
16753 // We are looking at a function which symbol is undefined.
16754 // let's set its symbol.
16755 string n = result->get_linkage_name();
16756 if (n.empty())
16757 n = result->get_name();
16758 fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
16759 if (fn_sym)
16760 {
16761 result->set_symbol(fn_sym);
16762 result->set_is_in_public_symbol_table(false);
16763 }
16764 }
16765 }
16766
16767 rdr.associate_die_to_type(die, result->get_type(), where_offset);
16768
16769 size_t die_offset = dwarf_dieoffset(die);
16770
16771 if (fn
16772 && is_member_function(fn)
16774 && !result->get_linkage_name().empty())
16775 // This function is a virtual member function which has its
16776 // linkage name *and* and has its underlying symbol correctly set.
16777 // It thus doesn't need any fixup related to elf symbol. So
16778 // remove it from the set of virtual member functions with linkage
16779 // names and no elf symbol that need to be fixed up.
16780 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
16781 return result;
16782}
16783
16784/// Canonicalize a type if it's suitable for early canonicalizing, or,
16785/// if it's not, schedule it for late canonicalization, after the
16786/// debug info of the current translation unit has been fully read.
16787///
16788/// A (composite) type is deemed suitable for early canonicalizing iff
16789/// all of its sub-types are canonicalized themselve. Non composite
16790/// types are always deemed suitable for early canonicalization.
16791///
16792/// Note that this function knows how to deal with anonymous classes,
16793/// structs and enums, unlike the overload below:
16794///
16795/// @param t the type DIE to consider for canonicalization.
16796///
16797/// @param rdr the @ref reader to use.
16798static void
16799maybe_canonicalize_type(const type_base_sptr& t,
16800 reader& rdr)
16801{
16802 if (!t)
16803 return;
16804
16805 rdr.schedule_type_for_late_canonicalization(t);
16806}
16807
16808/// If a given decl is a member type declaration, set its access
16809/// specifier from the DIE that represents it.
16810///
16811/// @param member_type_declaration the member type declaration to
16812/// consider.
16813static void
16814maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
16815 Dwarf_Die* die)
16816{
16817 if (is_type(member_type_declaration)
16818 && is_member_decl(member_type_declaration))
16819 {
16820 class_or_union* scope =
16821 is_class_or_union_type(member_type_declaration->get_scope());
16822 ABG_ASSERT(scope);
16823
16824 access_specifier access = public_access;
16825 if (class_decl* cl = is_class_type(scope))
16826 if (!cl->is_struct())
16827 access = private_access;
16828
16829 die_access_specifier(die, access);
16830 set_member_access_specifier(member_type_declaration, access);
16831 }
16832}
16833
16834/// This function tests if a given function which might be intented to
16835/// be added to a class scope (to become a member function) should be
16836/// dropped on the floor instead and not be added to the class.
16837///
16838/// This is a subroutine of build_ir_node_from_die.
16839///
16840/// @param fn the function to consider.
16841///
16842/// @param fn_die the DWARF die of @p fn.
16843///
16844/// @param scope the scope in which @p fn is to be added.
16845///
16846/// @return true iff @p fn should be dropped on the floor.
16847static bool
16848potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
16849 const Dwarf_Die *fn_die)
16850{
16851 if (!fn || fn->get_scope())
16852 return false;
16853
16854 if (// A function that is not virtual ...
16855 !die_is_virtual(fn_die)
16856 // .. and yet has no defined ELF symbol associated ...
16857 && !fn->get_symbol())
16858 // Should not be added to its class scope.
16859 //
16860 // Why would it? It's not part of the ABI anyway, as it doesn't
16861 // have any ELF symbol associated and is not a virtual member
16862 // function. It just constitutes bloat in the IR and might even
16863 // induce spurious change reports down the road.
16864 return true;
16865
16866 return false;
16867}
16868
16869/// Build an IR node from a given DIE and add the node to the current
16870/// IR being build and held in the DWARF reader. Doing that is called
16871/// "emitting an IR node for the DIE".
16872///
16873/// @param rdr the DWARF reader.
16874///
16875/// @param die the DIE to consider.
16876///
16877/// @param scope the scope under which the resulting IR node has to be
16878/// added.
16879///
16880/// @param called_from_public_decl set to yes if this function is
16881/// called from the functions used to build a public decl (functions
16882/// and variables). In that case, this function accepts building IR
16883/// nodes representing types. Otherwise, this function only creates
16884/// IR nodes representing public decls (functions and variables).
16885/// This is done to avoid emitting IR nodes for types that are not
16886/// referenced by public functions or variables.
16887///
16888/// @param where_offset the offset of the DIE where we are "logically"
16889/// positionned at, in the DIE tree. This is useful when @p die is
16890/// e.g, DW_TAG_partial_unit that can be included in several places in
16891/// the DIE tree.
16892///
16893/// @param is_required_decl_spec if true, it means the ir node to
16894/// build is for a decl that is a specification for another decl that
16895/// is concrete. If you don't know what this is, set it to false.
16896///
16897/// @param is_declaration_only is true if the DIE denoted by @p die is
16898/// a declaration-only DIE.
16899///
16900/// @return the resulting IR node.
16901static type_or_decl_base_sptr
16902build_ir_node_from_die(reader& rdr,
16903 Dwarf_Die* die,
16904 scope_decl* scope,
16905 bool called_from_public_decl,
16906 size_t where_offset,
16907 bool is_declaration_only,
16908 bool is_required_decl_spec)
16909{
16910 type_or_decl_base_sptr result;
16911
16912 if (!die || !scope)
16913 return result;
16914
16915 int tag = dwarf_tag(die);
16916
16917 if (!called_from_public_decl)
16918 {
16919 if (rdr.load_all_types() && die_is_type(die))
16920 /* We were instructed to load debug info for all types,
16921 included those that are not reachable from a public
16922 declaration. So load the debug info for this type. */;
16923 else if (tag != DW_TAG_subprogram
16924 && tag != DW_TAG_variable
16925 && tag != DW_TAG_member
16926 && tag != DW_TAG_namespace)
16927 return result;
16928 }
16929
16930 const die_source source_of_die = rdr.get_die_source(die);
16931
16932 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
16933 source_of_die)))
16934 {
16935 if (rdr.load_all_types())
16936 if (called_from_public_decl)
16937 if (type_base_sptr t = is_type(result))
16938 if (corpus *abi_corpus = rdr.corpus().get())
16939 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
16940
16941 return result;
16942 }
16943
16944 // This is *the* bit of code that ensures we have the right notion
16945 // of "declared" at any point in a DIE chain formed from
16946 // DW_AT_abstract_origin and DW_AT_specification links. There should
16947 // be no other callers of die_is_declaration_only.
16948 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
16949
16950 switch (tag)
16951 {
16952 // Type DIEs we support.
16953 case DW_TAG_base_type:
16954 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
16955 {
16956 result =
16957 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
16958 maybe_canonicalize_type(t, rdr);
16959 }
16960 break;
16961
16962 case DW_TAG_typedef:
16963 {
16964 typedef_decl_sptr t = build_typedef_type(rdr, die,
16965 called_from_public_decl,
16966 where_offset);
16967
16968 result = add_decl_to_scope(t, scope);
16969 if (result)
16970 {
16971 maybe_set_member_type_access_specifier(is_decl(result), die);
16972 maybe_canonicalize_type(t, rdr);
16973 }
16974 }
16975 break;
16976
16977 case DW_TAG_pointer_type:
16978 {
16980 build_pointer_type_def(rdr, die,
16981 called_from_public_decl,
16982 where_offset);
16983 if (p)
16984 {
16985 result =
16986 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
16987 ABG_ASSERT(result->get_translation_unit());
16988 maybe_canonicalize_type(p, rdr);
16989 }
16990 }
16991 break;
16992
16993 case DW_TAG_reference_type:
16994 case DW_TAG_rvalue_reference_type:
16995 {
16997 build_reference_type(rdr, die,
16998 called_from_public_decl,
16999 where_offset);
17000 if (r)
17001 {
17002 result =
17003 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
17004 maybe_canonicalize_type(r, rdr);
17005 }
17006 }
17007 break;
17008
17009 case DW_TAG_ptr_to_member_type:
17010 {
17012 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
17013 where_offset);
17014 if (p)
17015 {
17016 result =
17018 rdr.cur_transl_unit()->get_global_scope());
17019 maybe_canonicalize_type(p, rdr);
17020 }
17021 }
17022 break;
17023
17024 case DW_TAG_const_type:
17025 case DW_TAG_volatile_type:
17026 case DW_TAG_restrict_type:
17027 {
17028 type_base_sptr q =
17029 build_qualified_type(rdr, die,
17030 called_from_public_decl,
17031 where_offset);
17032 if (q)
17033 {
17034 // Strip some potentially redundant type qualifiers from
17035 // the qualified type we just built.
17036 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
17037 rdr);
17038 if (!d)
17039 d = get_type_declaration(q);
17040 ABG_ASSERT(d);
17041 type_base_sptr ty = is_type(d);
17042 // Associate the die to type ty again because 'ty'might be
17043 // different from 'q', because 'ty' is 'q' possibly
17044 // stripped from some redundant type qualifier.
17045 rdr.associate_die_to_type(die, ty, where_offset);
17046 result =
17047 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
17048 maybe_canonicalize_type(is_type(result), rdr);
17049 }
17050 }
17051 break;
17052
17053 case DW_TAG_enumeration_type:
17054 {
17055 bool type_is_opaque = false;
17056 bool type_suppressed =
17057 type_is_suppressed(rdr, scope, die, type_is_opaque);
17058 if (type_suppressed && type_is_opaque)
17059 {
17060 // The type is suppressed because it's private. If other
17061 // non-suppressed and declaration-only instances of this
17062 // type exist in the current corpus, then it means those
17063 // non-suppressed instances are opaque versions of the
17064 // suppressed private type. Lets return one of these opaque
17065 // types then.
17066 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17067 maybe_canonicalize_type(is_type(result), rdr);
17068 }
17069 else if (!type_suppressed)
17070 {
17071 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
17072 where_offset,
17073 is_declaration_only);
17074 result = add_decl_to_scope(e, scope);
17075 if (result)
17076 {
17077 maybe_set_member_type_access_specifier(is_decl(result), die);
17078 maybe_canonicalize_type(is_type(result), rdr);
17079 }
17080 }
17081 }
17082 break;
17083
17084 case DW_TAG_class_type:
17085 case DW_TAG_structure_type:
17086 {
17087 bool type_is_opaque = false;
17088 bool type_suppressed=
17089 type_is_suppressed(rdr, scope, die, type_is_opaque);
17090
17091 if (type_suppressed && type_is_opaque)
17092 {
17093 // The type is suppressed because it's private. If other
17094 // non-suppressed and declaration-only instances of this
17095 // type exist in the current corpus, then it means those
17096 // non-suppressed instances are opaque versions of the
17097 // suppressed private type. Lets return one of these opaque
17098 // types then.
17099 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17100 maybe_canonicalize_type(is_type(result), rdr);
17101 }
17102 else if (!type_suppressed)
17103 {
17104 class_decl_sptr klass;
17105 Dwarf_Die spec_die;
17106 if (die_die_attribute(die, DW_AT_specification, spec_die))
17107 {
17108 scope_decl_sptr skope =
17109 get_scope_for_die(rdr, &spec_die,
17110 called_from_public_decl,
17111 where_offset);
17112 ABG_ASSERT(skope);
17113 decl_base_sptr cl =
17114 is_decl(build_ir_node_from_die(rdr, &spec_die,
17115 skope.get(),
17116 called_from_public_decl,
17117 where_offset,
17118 is_declaration_only,
17119 /*is_required_decl_spec=*/false));
17120 ABG_ASSERT(cl);
17121 klass = dynamic_pointer_cast<class_decl>(cl);
17122 ABG_ASSERT(klass);
17123
17124 klass =
17125 add_or_update_class_type(rdr, die,
17126 skope.get(),
17127 tag == DW_TAG_structure_type,
17128 klass,
17129 called_from_public_decl,
17130 where_offset,
17131 is_declaration_only);
17132 }
17133 else
17134 {
17135 if (class_decl* class_sc = is_class_type(scope))
17136 {
17137 string type_name = die_type_name(rdr, die,
17138 /*qualified_name=*/false,
17139 where_offset);
17140 if (class_decl_sptr c =
17141 is_class_type(class_sc->find_member_type(type_name)))
17142 klass = c;
17143 else
17144 klass =
17145 add_or_update_class_type(rdr, die, scope,
17146 tag == DW_TAG_structure_type,
17148 called_from_public_decl,
17149 where_offset,
17150 is_declaration_only);
17151 }
17152 else
17153 klass =
17154 add_or_update_class_type(rdr, die, scope,
17155 tag == DW_TAG_structure_type,
17157 called_from_public_decl,
17158 where_offset,
17159 is_declaration_only);
17160 }
17161 if (klass)
17162 {
17163 maybe_set_member_type_access_specifier(klass, die);
17164 maybe_canonicalize_type(klass, rdr);
17165 }
17166 result = klass;
17167 }
17168 }
17169 break;
17170 case DW_TAG_union_type:
17171 if (!type_is_suppressed(rdr, scope, die))
17172 {
17173 union_decl_sptr union_type;
17174 if (class_decl* class_sc = is_class_type(scope))
17175 {
17176 string type_name = die_type_name(rdr, die,
17177 /*qualified_name=*/false,
17178 where_offset);
17179 if (union_decl_sptr u =
17180 is_union_type(class_sc->find_member_type(type_name)))
17181 union_type = u;
17182 }
17183
17184 if (!union_type)
17185 union_type =
17186 add_or_update_union_type(rdr, die, scope,
17187 union_decl_sptr(),
17188 called_from_public_decl,
17189 where_offset,
17190 is_declaration_only);
17191
17192 if (union_type)
17193 {
17194 maybe_set_member_type_access_specifier(union_type, die);
17195 maybe_canonicalize_type(union_type, rdr);
17196 result = union_type;
17197 }
17198 }
17199 break;
17200 case DW_TAG_string_type:
17201 break;
17202 case DW_TAG_subroutine_type:
17203 {
17204 function_type_sptr f = build_function_type(rdr, die,
17206 where_offset);
17207 if (f)
17208 {
17209 result = f;
17210 result->set_is_artificial(false);
17211 maybe_canonicalize_type(f, rdr);
17212 }
17213 }
17214 break;
17215 case DW_TAG_array_type:
17216 {
17217 array_type_def_sptr a = build_array_type(rdr,
17218 die,
17219 called_from_public_decl,
17220 where_offset);
17221 if (a)
17222 {
17223 result =
17224 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
17225 maybe_canonicalize_type(a, rdr);
17226 }
17227 break;
17228 }
17229 case DW_TAG_subrange_type:
17230 {
17231 // If we got here, this means the subrange type is a "free
17232 // form" defined in the global namespace of the current
17233 // translation unit, like what is found in Ada.
17235 build_subrange_type(rdr, die, where_offset,
17236 /*associate_type_to_die=*/true);
17237 if (s)
17238 {
17239 result =
17240 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
17241 maybe_canonicalize_type(s, rdr);
17242 }
17243 }
17244 break;
17245 case DW_TAG_packed_type:
17246 break;
17247 case DW_TAG_set_type:
17248 break;
17249 case DW_TAG_file_type:
17250 break;
17251 case DW_TAG_thrown_type:
17252 break;
17253 case DW_TAG_interface_type:
17254 break;
17255 case DW_TAG_unspecified_type:
17256 break;
17257 case DW_TAG_shared_type:
17258 break;
17259
17260 case DW_TAG_compile_unit:
17261 // We shouldn't reach this point b/c this should be handled by
17262 // build_translation_unit.
17264
17265 case DW_TAG_namespace:
17266 case DW_TAG_module:
17267 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
17268 break;
17269
17270 case DW_TAG_variable:
17271 case DW_TAG_member:
17272 {
17273 if (tag == DW_TAG_member)
17274 ABG_ASSERT(!die_is_in_c(die));
17275
17276 scope_decl_sptr var_scope =
17277 get_scope_for_die(rdr, die,
17278 /*called_from_public_decl=*/
17279 die_is_effectively_public_decl(rdr, die),
17280 where_offset);
17281 var_decl_sptr v =
17282 build_or_get_var_decl_if_not_suppressed(rdr, var_scope.get(), die,
17283 where_offset,
17284 is_declaration_only,
17285 /*result=*/var_decl_sptr(),
17286 is_required_decl_spec);
17287 if (v && is_data_member(v))
17288 // We might have gotten a pre-existing data member variable
17289 // that was already built. This means this DIE is a
17290 // concrete implementation of a previous specification.
17291 // Read the specific attributes of this concrete
17292 // implementation and add them to the existing IR node we
17293 // have.
17294 v = build_var_decl(rdr, die, where_offset, v);
17295
17296 if (v)
17297 {
17298 add_decl_to_scope(v, var_scope);
17299 if (is_data_member(v))
17300 // We are sure this is a static data member at this
17301 // point because a non-static data member would have
17302 // been encountered a a child of a class or union DIE
17303 // and thus handled by add_or_update_class_type or
17304 // add_or_update_union_type.
17305 set_member_is_static(v, true);
17306 else
17307 rdr.var_decls_to_re_add_to_tree().push_back(v);
17308 rdr.add_var_to_exported_or_undefined_decls(v);
17309 rdr.associate_die_to_decl(die, v, where_offset,
17310 /*associate_by_repr=*/false);
17311 result = v;
17312 }
17313 }
17314 break;
17315
17316 case DW_TAG_subprogram:
17317 case DW_TAG_inlined_subroutine:
17318 {
17319 if (die_is_artificial(die))
17320 break;
17321
17322 Dwarf_Die abstract_origin_die;
17323 bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
17324 abstract_origin_die,
17325 /*recursive=*/true);
17326
17327
17328 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
17329 where_offset);
17330 scope_decl* interface_scope = scope ? scope : s.get();
17331
17332 class_decl* class_scope = is_class_type(interface_scope);
17333 string linkage_name = die_linkage_name(die);
17334 string spec_linkage_name;
17335 function_decl_sptr existing_fn;
17336
17337 if (class_scope)
17338 {
17339 // The scope of the function DIE we are looking at is a
17340 // class. So we are looking at a member function.
17341 if (!linkage_name.empty())
17342 {
17343 if ((existing_fn =
17344 class_scope->find_member_function_sptr(linkage_name)))
17345 {
17346 // A function with the same linkage name has
17347 // already been created. Let's see if we are a
17348 // clone of it or not.
17349 spec_linkage_name = existing_fn->get_linkage_name();
17350 if (has_abstract_origin
17351 && !spec_linkage_name.empty()
17352 && linkage_name != spec_linkage_name)
17353 {
17354 // The current DIE has 'existing_fn' as
17355 // abstract orign, and has a linkage name that
17356 // is different from from the linkage name of
17357 // 'existing_fn'. That means, the current DIE
17358 // represents a clone of 'existing_fn'.
17359 existing_fn = existing_fn->clone();
17360 }
17361 }
17362 }
17363 }
17364 else if (has_abstract_origin)
17365 // Let's see if this function is the implementation of an
17366 // existing interface. In that case, let's read the
17367 // specification of the origin interface ...
17368 existing_fn = build_function_decl(rdr, &abstract_origin_die, where_offset,
17369 /*existing_fn=*/nullptr);
17370
17371 rdr.scope_stack().push(interface_scope);
17372
17373 // Either we create a brand new IR for the current function
17374 // DIE we are looking at, or we complete an existing IR node
17375 // with the new completementary information carried by this
17376 // DIE for that IR node.
17377 result =
17378 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
17379 die, where_offset,
17380 is_declaration_only,
17381 existing_fn);
17382
17383 if (result && !existing_fn)
17384 {
17385 // We built a brand new IR for the function DIE. Now
17386 // there should be enough information on that IR to know
17387 // if we should drop it on the floor or keep it ...
17388 if (potential_member_fn_should_be_dropped(is_function_decl(result), die)
17389 && !is_required_decl_spec)
17390 {
17391 // So apparently we should drop that function IR on
17392 // the floor. Let's do so.
17393 result.reset();
17394 break;
17395 }
17396 }
17397
17398 // OK so we came to the conclusion that we need to keep
17399 // the function. So let's add it to its scope.
17400 result = add_decl_to_scope(is_decl(result), interface_scope);
17401
17403 if (fn && is_member_function(fn))
17404 {
17405 class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
17406 sptr_utils::noop_deleter());
17407 ABG_ASSERT(klass);
17408 finish_member_function_reading(die, fn, klass, rdr);
17409 }
17410
17411 if (fn)
17412 {
17413 if (!is_member_function(fn)
17415 // Virtual member functions are added to the set of
17416 // functions exported by the current ABI corpus *after*
17417 // the canonicalization of their parent type. So let's
17418 // not do it here.
17419 rdr.add_fn_to_exported_or_undefined_decls(fn.get());
17420 rdr.associate_die_to_decl(die, fn, where_offset,
17421 /*associate_by_repr=*/false);
17422 maybe_canonicalize_type(fn->get_type(), rdr);
17423 }
17424
17425 rdr.scope_stack().pop();
17426 }
17427 break;
17428
17429 case DW_TAG_formal_parameter:
17430 // We should not read this case as it should have been dealt
17431 // with by build_function_decl above.
17433
17434 case DW_TAG_constant:
17435 break;
17436 case DW_TAG_enumerator:
17437 break;
17438
17439 case DW_TAG_partial_unit:
17440 case DW_TAG_imported_unit:
17441 // For now, the DIEs under these are read lazily when they are
17442 // referenced by a public decl DIE that is under a
17443 // DW_TAG_compile_unit, so we shouldn't get here.
17445
17446 // Other declaration we don't really intend to support yet.
17447 case DW_TAG_dwarf_procedure:
17448 case DW_TAG_imported_declaration:
17449 case DW_TAG_entry_point:
17450 case DW_TAG_label:
17451 case DW_TAG_lexical_block:
17452 case DW_TAG_unspecified_parameters:
17453 case DW_TAG_variant:
17454 case DW_TAG_common_block:
17455 case DW_TAG_common_inclusion:
17456 case DW_TAG_inheritance:
17457 case DW_TAG_with_stmt:
17458 case DW_TAG_access_declaration:
17459 case DW_TAG_catch_block:
17460 case DW_TAG_friend:
17461 case DW_TAG_namelist:
17462 case DW_TAG_namelist_item:
17463 case DW_TAG_template_type_parameter:
17464 case DW_TAG_template_value_parameter:
17465 case DW_TAG_try_block:
17466 case DW_TAG_variant_part:
17467 case DW_TAG_imported_module:
17468 case DW_TAG_condition:
17469 case DW_TAG_type_unit:
17470 case DW_TAG_template_alias:
17471 case DW_TAG_lo_user:
17472 case DW_TAG_MIPS_loop:
17473 case DW_TAG_format_label:
17474 case DW_TAG_function_template:
17475 case DW_TAG_class_template:
17476 case DW_TAG_GNU_BINCL:
17477 case DW_TAG_GNU_EINCL:
17478 case DW_TAG_GNU_template_template_param:
17479 case DW_TAG_GNU_template_parameter_pack:
17480 case DW_TAG_GNU_formal_parameter_pack:
17481 case DW_TAG_GNU_call_site:
17482 case DW_TAG_GNU_call_site_parameter:
17483 case DW_TAG_hi_user:
17484 default:
17485 break;
17486 }
17487
17488 if (result && tag != DW_TAG_subroutine_type)
17489 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
17490 /*associate_by_repr=*/false);
17491
17492 if (result)
17493 if (rdr.load_all_types())
17494 if (called_from_public_decl)
17495 if (type_base_sptr t = is_type(result))
17496 if (corpus *abi_corpus = scope->get_corpus())
17497 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17498
17499 rdr.maybe_schedule_decl_only_type_for_resolution(result);
17500
17501 return result;
17502}
17503
17504/// Build the IR node for a void type.
17505///
17506/// @param rdr the DWARF reader to use.
17507///
17508/// @return the void type node.
17509static decl_base_sptr
17510build_ir_node_for_void_type(reader& rdr)
17511{
17512 const environment& env = rdr.env();
17513
17514 type_base_sptr t = env.get_void_type();
17515 decl_base_sptr type_declaration = get_type_declaration(t);
17516 if (!has_scope(type_declaration))
17517 {
17518 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17519 rdr.schedule_type_for_late_canonicalization(t);
17520 }
17521 return type_declaration;
17522}
17523
17524/// Build the IR node for a "pointer to void type".
17525///
17526/// That IR node is shared across the ABI corpus.
17527///
17528/// Note that this function just gets that IR node from the
17529/// environment and, if it's not added to any scope yet, adds it to
17530/// the global scope associated to the current translation unit.
17531///
17532/// @param rdr the DWARF reader to consider.
17533///
17534/// @return the IR node.
17535static type_or_decl_base_sptr
17536build_ir_node_for_void_pointer_type(reader& rdr)
17537{
17538 const environment& env = rdr.env();
17539 type_base_sptr t = env.get_void_pointer_type();
17540 decl_base_sptr type_declaration = get_type_declaration(t);
17541 if (!has_scope(type_declaration))
17542 {
17543 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17544 rdr.schedule_type_for_late_canonicalization(t);
17545 }
17546 return type_declaration;
17547}
17548
17549/// Build the IR node for a variadic parameter type.
17550///
17551/// @param rdr the DWARF reader to use.
17552///
17553/// @return the variadic parameter type.
17554static decl_base_sptr
17555build_ir_node_for_variadic_parameter_type(reader &rdr)
17556{
17557
17558 const environment& env = rdr.env();
17559 type_base_sptr t = env.get_variadic_parameter_type();
17560 decl_base_sptr type_declaration = get_type_declaration(t);
17561 if (!has_scope(type_declaration))
17562 {
17563 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17564 rdr.schedule_type_for_late_canonicalization(t);
17565 }
17566 return type_declaration;
17567}
17568
17569/// Build an IR node from a given DIE and add the node to the current
17570/// IR being build and held in the DWARF reader. Doing that is called
17571/// "emitting an IR node for the DIE".
17572///
17573/// @param rdr the DWARF reader.
17574///
17575/// @param die the DIE to consider.
17576///
17577/// @param called_from_public_decl set to yes if this function is
17578/// called from the functions used to build a public decl (functions
17579/// and variables). In that case, this function accepts building IR
17580/// nodes representing types. Otherwise, this function only creates
17581/// IR nodes representing public decls (functions and variables).
17582/// This is done to avoid emitting IR nodes for types that are not
17583/// referenced by public functions or variables.
17584///
17585/// @param where_offset the offset of the DIE where we are "logically"
17586/// positionned at, in the DIE tree. This is useful when @p die is
17587/// e.g, DW_TAG_partial_unit that can be included in several places in
17588/// the DIE tree.
17589///
17590/// @return the resulting IR node.
17591static type_or_decl_base_sptr
17592build_ir_node_from_die(reader& rdr,
17593 Dwarf_Die* die,
17594 bool called_from_public_decl,
17595 size_t where_offset)
17596{
17597 if (!die)
17598 return decl_base_sptr();
17599
17600 // Normaly, a decl that is meant to be external has a DW_AT_external
17601 // set. But then some compilers fail to always emit that flag. For
17602 // instance, for static data members, some compilers won't emit the
17603 // DW_AT_external. In that case, we assume that if the variable is
17604 // at global or named namespace scope, then we can assume it's
17605 // external. If the variable doesn't have any ELF symbol associated
17606 // to it, it'll be dropped on the floor anyway. Those variable
17607 // decls are considered as being "effectively public".
17608 bool consider_as_called_from_public_decl =
17609 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
17610 scope_decl_sptr scope = get_scope_for_die(rdr, die,
17611 consider_as_called_from_public_decl,
17612 where_offset);
17613 if (!scope)
17614 scope = rdr.global_scope();
17615
17616 return build_ir_node_from_die(rdr, die, scope.get(),
17617 called_from_public_decl,
17618 where_offset, true);
17619}
17620
17621/// Create a dwarf::reader.
17622///
17623/// @param elf_path the path to the elf file the reader is to be used
17624/// for.
17625///
17626/// @param debug_info_root_paths a vector to the paths to the
17627/// directories under which the debug info is to be found for @p
17628/// elf_path. Pass an empty vector if the debug info is not in a
17629/// split file.
17630///
17631/// @param environment the environment used by the current context.
17632/// This environment contains resources needed by the DWARF reader and by
17633/// the types and declarations that are to be created later. Note
17634/// that ABI artifacts that are to be compared all need to be created
17635/// within the same environment.
17636///
17637/// Please also note that the life time of this environment object
17638/// must be greater than the life time of the resulting @ref
17639/// reader the context uses resources that are allocated in the
17640/// environment.
17641///
17642/// @param load_all_types if set to false only the types that are
17643/// reachable from publicly exported declarations (of functions and
17644/// variables) are read. If set to true then all types found in the
17645/// debug information are loaded.
17646///
17647/// @param linux_kernel_mode if set to true, then consider the special
17648/// linux kernel symbol tables when determining if a symbol is
17649/// exported or not.
17650///
17651/// @return a smart pointer to the resulting dwarf::reader.
17652elf_based_reader_sptr
17653create_reader(const std::string& elf_path,
17654 const vector<char**>& debug_info_root_paths,
17656 bool load_all_types,
17657 bool linux_kernel_mode)
17658{
17659
17660 reader_sptr r = reader::create(elf_path,
17661 debug_info_root_paths,
17663 load_all_types,
17664 linux_kernel_mode);
17665 return static_pointer_cast<elf_based_reader>(r);
17666}
17667
17668/// Re-initialize a reader so that it can re-used to read
17669/// another binary.
17670///
17671/// @param rdr the context to re-initialize.
17672///
17673/// @param elf_path the path to the elf file the context is to be used
17674/// for.
17675///
17676/// @param debug_info_root_path a pointer to the path to the root
17677/// directory under which the debug info is to be found for @p
17678/// elf_path. Leave this to NULL if the debug info is not in a split
17679/// file.
17680///
17681/// @param environment the environment used by the current context.
17682/// This environment contains resources needed by the DWARF reader and by
17683/// the types and declarations that are to be created later. Note
17684/// that ABI artifacts that are to be compared all need to be created
17685/// within the same environment.
17686///
17687/// Please also note that the life time of this environment object
17688/// must be greater than the life time of the resulting @ref
17689/// reader the context uses resources that are allocated in the
17690/// environment.
17691///
17692/// @param load_all_types if set to false only the types that are
17693/// reachable from publicly exported declarations (of functions and
17694/// variables) are read. If set to true then all types found in the
17695/// debug information are loaded.
17696///
17697/// @param linux_kernel_mode if set to true, then consider the special
17698/// linux kernel symbol tables when determining if a symbol is
17699/// exported or not.
17700///
17701/// @return a smart pointer to the resulting dwarf::reader.
17702void
17703reset_reader(elf_based_reader& rdr,
17704 const std::string& elf_path,
17705 const vector<char**>&debug_info_root_path,
17706 bool read_all_types,
17707 bool linux_kernel_mode)
17708{
17709 reader& r = dynamic_cast<reader&>(rdr);
17710 r.initialize(elf_path, debug_info_root_path,
17711 read_all_types, linux_kernel_mode);
17712}
17713
17714/// Read all @ref abigail::translation_unit possible from the debug info
17715/// accessible from an elf file, stuff them into a libabigail ABI
17716/// Corpus and return it.
17717///
17718/// @param elf_path the path to the elf file.
17719///
17720/// @param debug_info_root_paths a vector of pointers to root paths
17721/// under which to look for the debug info of the elf files that are
17722/// later handled by the Dwfl. This for cases where the debug info is
17723/// split into a different file from the binary we want to inspect.
17724/// On Red Hat compatible systems, this root path is usually
17725/// /usr/lib/debug by default. If this argument is set to NULL, then
17726/// "./debug" and /usr/lib/debug will be searched for sub-directories
17727/// containing the debug info file.
17728///
17729/// @param environment the environment used by the current context.
17730/// This environment contains resources needed by the DWARF reader and by
17731/// the types and declarations that are to be created later. Note
17732/// that ABI artifacts that are to be compared all need to be created
17733/// within the same environment. Also, the lifetime of the
17734/// environment must be greater than the lifetime of the resulting
17735/// corpus because the corpus uses resources that are allocated in the
17736/// environment.
17737///
17738/// @param load_all_types if set to false only the types that are
17739/// reachable from publicly exported declarations (of functions and
17740/// variables) are read. If set to true then all types found in the
17741/// debug information are loaded.
17742///
17743/// @param resulting_corp a pointer to the resulting abigail::corpus.
17744///
17745/// @return the resulting status.
17746corpus_sptr
17747read_corpus_from_elf(const std::string& elf_path,
17748 const vector<char**>& debug_info_root_paths,
17750 bool load_all_types,
17751 fe_iface::status& status)
17752{
17753 elf_based_reader_sptr rdr =
17754 dwarf::reader::create(elf_path, debug_info_root_paths,
17755 environment, load_all_types,
17756 /*linux_kernel_mode=*/false);
17757
17758 return rdr->read_corpus(status);
17759}
17760
17761/// Look into the symbol tables of a given elf file and see if we find
17762/// a given symbol.
17763///
17764/// @param env the environment we are operating from.
17765///
17766/// @param elf_path the path to the elf file to consider.
17767///
17768/// @param symbol_name the name of the symbol to look for.
17769///
17770/// @param demangle if true, try to demangle the symbol name found in
17771/// the symbol table.
17772///
17773/// @param syms the vector of symbols found with the name @p symbol_name.
17774///
17775/// @return true iff the symbol was found among the publicly exported
17776/// symbols of the ELF file.
17777bool
17778lookup_symbol_from_elf(const environment& env,
17779 const string& elf_path,
17780 const string& symbol_name,
17781 bool demangle,
17782 vector<elf_symbol_sptr>& syms)
17783
17784{
17785 if (elf_version(EV_CURRENT) == EV_NONE)
17786 return false;
17787
17788 int fd = open(elf_path.c_str(), O_RDONLY);
17789 if (fd < 0)
17790 return false;
17791
17792 struct stat s;
17793 if (fstat(fd, &s))
17794 return false;
17795
17796 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
17797 if (elf == 0)
17798 return false;
17799
17800 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
17801 demangle, syms);
17802 elf_end(elf);
17803 close(fd);
17804
17805 return value;
17806}
17807
17808/// Look into the symbol tables of an elf file to see if a public
17809/// function of a given name is found.
17810///
17811/// @param env the environment we are operating from.
17812///
17813/// @param elf_path the path to the elf file to consider.
17814///
17815/// @param symbol_name the name of the function to look for.
17816///
17817/// @param syms the vector of public function symbols found with the
17818/// name @p symname.
17819///
17820/// @return true iff a function with symbol name @p symbol_name is
17821/// found.
17822bool
17823lookup_public_function_symbol_from_elf(environment& env,
17824 const string& path,
17825 const string& symname,
17826 vector<elf_symbol_sptr>& syms)
17827{
17828 if (elf_version(EV_CURRENT) == EV_NONE)
17829 return false;
17830
17831 int fd = open(path.c_str(), O_RDONLY);
17832 if (fd < 0)
17833 return false;
17834
17835 struct stat s;
17836 if (fstat(fd, &s))
17837 return false;
17838
17839 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
17840 if (elf == 0)
17841 return false;
17842
17843 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
17844 elf_end(elf);
17845 close(fd);
17846
17847 return value;
17848}
17849
17850}// end namespace dwarf
17851
17852}// 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:1718
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...
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.
Elf * elf_handle() const
Getter of the handle used to access ELF 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.
const vector< char ** > & debug_info_root_paths() const
Getter of the vector of directory paths to look into for split debug information files.
symtab_reader::symtab_sptr & symtab() const
Getter of an abstract representation of the symbol table of the underlying ELF file.
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.
@ STATUS_DEBUG_INFO_NOT_FOUND
This status is for when the debug info could not be read.
@ STATUS_ALT_DEBUG_INFO_NOT_FOUND
This status is for when the alternate debug info could not be found.
@ STATUS_OK
This status is for when the call went OK.
@ STATUS_UNKNOWN
The status is in an unknown state.
const options_type & options() const
Getter of the the options of the current Front End Interface.
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.
This class is to hold the value of the bound of a subrange. The value can be either signed or unsigne...
Definition abg-ir.h:2560
static string vector_as_string(const vector< subrange_sptr > &)
Return a string representation of a vector of subranges.
Definition abg-ir.cc:19133
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition abg-ir.h:2540
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition abg-ir.h:2537
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition abg-ir.h:4145
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:4647
The abstraction of the version of an ELF symbol.
Definition abg-ir.h:1203
binding
The binding of a symbol.
Definition abg-ir.h:949
type
The type of a symbol.
Definition abg-ir.h:936
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:1937
visibility
The visibility of the symbol.
Definition abg-ir.h:958
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition abg-ir.h:2772
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition abg-ir.h:148
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition abg-ir.h:3139
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition abg-ir.h:3142
The source location of a token.
Definition abg-ir.h:307
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition abg-ir.h:2226
The internal representation of an integral type.
Definition abg-ir-priv.h:48
language
The language of the translation unit.
Definition abg-ir.h:708
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...
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...
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.
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,...
hash_t combine_hashes(hash_t val1, hash_t val2)
Combine two hash values to produce a third hash value.
Definition abg-hash.cc:172
hash_t hash(uint64_t v, uint64_t seed)
Hash an integer value and combine it with a hash previously computed.
Definition abg-hash.cc:196
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition abg-fwd.h:235
bool get_member_function_is_dtor(const function_decl &f)
Test whether a member function is a destructor.
Definition abg-ir.cc:6309
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:12071
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition abg-fwd.h:221
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:11935
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition abg-fwd.h:269
access_specifier
Access specifier for class members.
Definition abg-ir.h:888
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:13652
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:13814
vector< type_base_wptr > type_base_wptrs_type
A convenience typedef for a vector of type_base_wptr.
Definition abg-fwd.h:142
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:11413
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition abg-ir.cc:10465
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition abg-ir.cc:5254
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:11841
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition abg-ir.h:897
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:10759
bool parse_real_type(const string &type_name, real_type &type)
Parse a real type from a string.
Definition abg-ir.cc:16386
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition abg-ir.cc:8330
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:10056
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:11337
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:11961
comparison_result
The result of structural comparison of type ABI artifacts.
Definition abg-ir-priv.h:35
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition abg-ir.cc:10791
void set_member_function_virtuality(function_decl &fn, bool is_virtual, ssize_t voffset)
Set the virtual-ness of a member fcuntion.
Definition abg-ir.cc:6570
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition abg-fwd.h:244
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition abg-ir.cc:10516
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:7188
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:5969
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:13515
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition abg-ir.cc:6337
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:12402
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:11022
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition abg-fwd.h:193
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:6393
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:6770
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:10567
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:11484
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition abg-ir.cc:5399
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition abg-ir.cc:10669
abg_compat::optional< uint64_t > hash_t
The abstraction for an 8 bytes hash value.
Definition abg-ir.h:105
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:13682
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition abg-fwd.h:210
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition abg-fwd.h:167
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:12111
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:11240
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:11277
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition abg-ir.cc:1686
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:10741
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:148
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition abg-ir.cc:8398
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition abg-fwd.h:256
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:239
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition abg-fwd.h:264
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:120
shared_ptr< translation_unit > translation_unit_sptr
Convenience typedef for a shared pointer on a translation_unit type.
Definition abg-fwd.h:136
void hash_and_canonicalize_types(IteratorType begin, IteratorType end, deref_lambda deref, bool do_log=false, bool show_stats=false)
Hash and canonicalize a sequence of types.
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:12601
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition abg-ir.cc:1700
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:12148
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition abg-fwd.h:226
bool is_const_qualified_type(const qualified_type_def_sptr &t)
Test if a given qualified type is const.
Definition abg-ir.cc:7156
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition abg-ir.cc:6223
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition abg-ir.cc:1672
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition abg-ir.cc:10405
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:25235
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition abg-ir.cc:5319
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:8305
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:28582
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition abg-ir.cc:5370
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition abg-fwd.h:175
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition abg-ir.cc:6496
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:11105
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:11544
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition abg-ir.cc:11071
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:13470
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:11820
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition abg-ir.cc:5468
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:13419
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition abg-ir.cc:10078
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition abg-ir.cc:26335
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:11749
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition abg-fwd.h:161
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:6893
bool return_comparison_result(T &l, T &r, bool value)
Return the result of the comparison of two (sub) types.
Definition abg-ir.cc:1127
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition abg-fwd.h:284
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition abg-ir.cc:1709
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition abg-ir.cc:15054
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:7523
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:13777
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition abg-ir.cc:10353
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:11514
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:11464
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:12181
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:8587
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:24348
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:11574
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition abg-ir.cc:5272
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:6280
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...
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.
A functor to hash instances of interned_string.