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-2025 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
254static void
255cleanup_decl_name(string&);
256
257/// Convenience typedef for a shared pointer to an
258/// addr_elf_symbol_sptr_map_type.
259typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
260
261/// Convenience typedef for a map that associates an @ref
262/// interned_string to a @ref function_type_sptr.
263typedef unordered_map<interned_string,
266
267/// Convenience typedef for a stack containing the scopes up to the
268/// current point in the abigail Internal Representation (aka IR) tree
269/// that is being built.
270typedef stack<scope_decl*> scope_stack_type;
271
272/// Convenience typedef for a map which key is a dwarf offset. The
273/// value is also a dwarf offset.
274typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
275
276/// Convenience typedef for a map which key is a string and which
277/// value is a vector of smart pointer to a class_or_union_sptr.
278typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_map;
279
280/// Convenience typedef for a map which key is a string and which
281/// value is a vector of smart pointer to a class.
282typedef unordered_map<string, classes_type> string_classes_map;
283
284/// Convenience typedef for a map which key is a string and which
285/// value is a vector of smart pointer to a enum.
286typedef unordered_map<string, enums_type> string_enums_map;
287
288/// The abstraction of the place where a partial unit has been
289/// imported. This is what the DW_TAG_imported_unit DIE expresses.
290///
291/// This type thus contains:
292/// - the offset to which the partial unit is imported
293/// - the offset of the imported partial unit.
294/// - the offset of the imported partial unit.
295struct imported_unit_point
296{
297 Dwarf_Off offset_of_import;
298 // The boolean below is true iff the imported unit comes from the
299 // alternate debug info file.
300 die_source imported_unit_die_source;
301 Dwarf_Off imported_unit_die_off;
302 Dwarf_Off imported_unit_cu_off;
303 Dwarf_Off imported_unit_child_off;
304
305 /// Default constructor for @ref the type imported_unit_point.
306 imported_unit_point()
307 : offset_of_import(),
308 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
309 imported_unit_die_off(),
310 imported_unit_cu_off(),
311 imported_unit_child_off()
312 {}
313
314 /// Constructor of @ref the type imported_unit_point.
315 ///
316 /// @param import_off the offset of the point at which the unit has
317 /// been imported.
318 imported_unit_point(Dwarf_Off import_off)
319 : offset_of_import(import_off),
320 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
321 imported_unit_die_off(),
322 imported_unit_cu_off(),
323 imported_unit_child_off()
324 {}
325
326 /// Constructor of @ref the type imported_unit_point.
327 ///
328 /// @param import_off the offset of the point at which the unit has
329 /// been imported.
330 ///
331 /// @param from where the imported DIE comes from.
332 ///
333 /// @param imported_die the die of the unit that has been imported.
334 imported_unit_point(Dwarf_Off import_off,
335 const Dwarf_Die& imported_die,
336 die_source from)
337 : offset_of_import(import_off),
338 imported_unit_die_source(from),
339 imported_unit_die_off(dwarf_dieoffset
340 (const_cast<Dwarf_Die*>(&imported_die))),
341 imported_unit_cu_off(),
342 imported_unit_child_off()
343 {
344 Dwarf_Die imported_unit_child;
345
346 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
347 &imported_unit_child) == 0);
348
349 imported_unit_child_off =
350 dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
351
352 Dwarf_Die cu_die_memory;
353 Dwarf_Die *cu_die;
354
355 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
356 &cu_die_memory, 0, 0);
357 imported_unit_cu_off = dwarf_dieoffset(cu_die);
358 }
359}; // struct imported_unit_point
360
361/// Convenience typedef for a vector of @ref imported_unit_point.
362typedef vector<imported_unit_point> imported_unit_points_type;
363
364/// Convenience typedef for a vector of @ref imported_unit_point.
365typedef unordered_map<Dwarf_Off, imported_unit_points_type>
367
368/// "Less than" operator for instances of @ref imported_unit_point
369/// type.
370///
371/// @param the left hand side operand of the "Less than" operator.
372///
373/// @param the right hand side operand of the "Less than" operator.
374///
375/// @return true iff @p l is less than @p r.
376static bool
377operator<(const imported_unit_point& l, const imported_unit_point& r)
378{return l.offset_of_import < r.offset_of_import;}
379
380static bool
381get_parent_die(const reader& rdr,
382 const Dwarf_Die* die,
383 Dwarf_Die& parent_die,
384 size_t where_offset);
385
386static bool
387get_scope_die(const reader& rdr,
388 const Dwarf_Die* die,
389 size_t where_offset,
390 Dwarf_Die& scope_die);
391
392static bool
393get_die_language(const Dwarf_Die *die, translation_unit::language &lang) ;
394
395static bool
396die_is_in_c(const Dwarf_Die *die);
397
398static bool
399die_is_in_cplus_plus(const Dwarf_Die *die);
400
401static bool
402die_is_in_c_or_cplusplus(const Dwarf_Die *die);
403
404static bool
405die_is_anonymous(const Dwarf_Die* die);
406
407static bool
408die_is_anonymous_data_member(const Dwarf_Die* die);
409
410static bool
411die_is_type(const Dwarf_Die* die);
412
413static bool
414die_is_decl(const Dwarf_Die* die);
415
416static bool
417die_is_declaration_only(Dwarf_Die* die);
418
419static bool
420die_is_variable_decl(const Dwarf_Die *die);
421
422static bool
423die_is_function_decl(const Dwarf_Die *die);
424
425static bool
426die_has_size_attribute(const Dwarf_Die *die);
427
428static bool
429die_has_no_child(const Dwarf_Die *die);
430
431static bool
432die_is_namespace(const Dwarf_Die* die);
433
434static bool
435die_is_unspecified(Dwarf_Die* die);
436
437static bool
438die_is_void_type(Dwarf_Die* die);
439
440static bool
441die_is_pointer_type(const Dwarf_Die* die);
442
443static bool
444pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
445
446static bool
447die_is_reference_type(const Dwarf_Die* die);
448
449static bool
450die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
451
452static bool
453die_is_pointer_or_reference_type(const Dwarf_Die* die);
454
455static bool
456die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
457
458static bool
459die_is_class_type(const Dwarf_Die* die);
460
461static bool
462die_is_qualified_type(const Dwarf_Die* die);
463
464static bool
465die_is_function_type(const Dwarf_Die *die);
466
467static bool
468die_has_object_pointer(const Dwarf_Die* die,
469 Dwarf_Die& object_pointer);
470
471static bool
472die_has_children(const Dwarf_Die* die);
473
474static bool
475fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die);
476
477static bool
478member_fn_die_has_this_pointer(const reader& rdr,
479 const Dwarf_Die* die,
480 size_t where_offset,
481 Dwarf_Die& class_die,
482 Dwarf_Die& object_pointer_die);
483
484static bool
485die_this_pointer_from_object_pointer(Dwarf_Die* die,
486 Dwarf_Die& this_pointer);
487
488static bool
489die_this_pointer_is_const(Dwarf_Die* die);
490
491static bool
492die_object_pointer_is_for_const_method(Dwarf_Die* die);
493
494static bool
495is_type_die_to_be_canonicalized(const Dwarf_Die *die);
496
497static bool
498die_is_at_class_scope(const reader& rdr,
499 const Dwarf_Die* die,
500 size_t where_offset,
501 Dwarf_Die& class_scope_die);
502static bool
503eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
504 size_t expr_len,
505 int64_t& value,
506 bool& is_tls_address);
507
509dwarf_language_to_tu_language(size_t l);
510
511static bool
512die_unsigned_constant_attribute(const Dwarf_Die* die,
513 unsigned attr_name,
514 uint64_t& cst);
515
516static bool
517die_signed_constant_attribute(const Dwarf_Die*die,
518 unsigned attr_name,
519 int64_t& cst);
520
521static bool
522die_constant_attribute(const Dwarf_Die *die,
523 unsigned attr_name,
524 bool is_signed,
526
527static bool
528die_member_offset(const reader& rdr,
529 const Dwarf_Die* die,
530 int64_t& offset);
531
532static bool
533form_is_DW_FORM_strx(unsigned form);
534
535static bool
536form_is_DW_FORM_line_strp(unsigned form);
537
538static bool
539die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
540
541static string
542die_name(const Dwarf_Die* die);
543
544static void
545die_name_and_linkage_name(const Dwarf_Die* die,
546 string& name,
547 string& linkage_name);
548static location
549die_location(const reader& rdr, const Dwarf_Die* die);
550
551static bool
552die_location_address(Dwarf_Die* die,
553 Dwarf_Addr& address,
554 bool& is_tls_address);
555
556static bool
557die_die_attribute(const Dwarf_Die* die,
558 unsigned attr_name,
559 Dwarf_Die& result,
560 bool recursively = true);
561
562static bool
563die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
564
565static bool
566subrange_die_indirect_bound_value(const Dwarf_Die *die,
567 unsigned attr_name,
569 bool& is_signed);
570
571static bool
572subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
573 unsigned attr_name,
574 Dwarf_Die& referenced_subrange);
575static string
576get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
577
578static string
579build_internal_anonymous_die_name(const string &base_name,
580 size_t anonymous_type_index);
581
582static string
583die_qualified_type_name(const reader& rdr,
584 const Dwarf_Die* die,
585 size_t where,
586 unordered_set<uint64_t>& guard);
587
588static string
589die_qualified_decl_name(const reader& rdr,
590 const Dwarf_Die* die,
591 size_t where,
592 unordered_set<uint64_t>& guard);
593
594static string
595die_qualified_name(const reader& rdr,
596 const Dwarf_Die* die,
597 size_t where,
598 unordered_set<uint64_t>& guard);
599
600static string
601die_qualified_name(const reader& rdr,
602 const Dwarf_Die* die,
603 size_t where);
604
605static string
606die_type_name(const reader& rdr, const Dwarf_Die* die,
607 bool qualified_name, size_t where_offset,
608 unordered_set<uint64_t>& infinite_loop_guard);
609
610static string
611die_type_name(const reader& rdr, const Dwarf_Die* die,
612 bool qualified_name, size_t where_offset);
613
614static bool
615die_qualified_type_name_empty(const reader& rdr,
616 const Dwarf_Die* die, size_t where,
617 string &qualified_name,
618 unordered_set<uint64_t>& infinite_loop_guard);
619
620static void
621die_return_and_parm_names_from_fn_type_die(const reader& rdr,
622 const Dwarf_Die* die,
623 size_t where_offset,
624 bool pretty_print,
625 bool qualified_name,
626 bool &is_method_type,
627 string &return_type_name,
628 string &class_name,
629 vector<string>& parm_names,
630 bool& is_const,
631 bool& is_static,
632 unordered_set<uint64_t>& infinite_loop_guard);
633
634static string
635die_function_signature(const reader& rdr,
636 const Dwarf_Die *die,
637 bool qualified_name,
638 size_t where_offset,
639 unordered_set<uint64_t>& infinite_loop_guard);
640
641static bool
642die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
643
644static bool
645die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
646
647static bool
648die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die);
649
650static bool
651die_function_type_is_method_type(const reader& rdr,
652 const Dwarf_Die *die,
653 size_t where_offset,
654 Dwarf_Die& object_pointer_die,
655 Dwarf_Die& class_die,
656 bool& is_static);
657
658static string
659die_enum_flat_representation(const reader& rdr,
660 const Dwarf_Die* die,
661 const string& indent,
662 bool one_line,
663 bool qualified_names,
664 size_t where_offset);
665
666static string
667die_class_flat_representation(const reader& rdr,
668 const Dwarf_Die* die,
669 const string& indent,
670 bool one_line,
671 bool qualified_names,
672 size_t where_offset,
673 unordered_set<uint64_t>& infinite_loop_guard);
674
675static string
676die_class_or_enum_flat_representation(const reader& rdr,
677 const Dwarf_Die* die,
678 const string& indent,
679 bool one_line,
680 bool qualified_names,
681 size_t where_offset,
682 unordered_set<uint64_t>& infinite_loop_guard);
683
684static string
685die_class_or_enum_flat_representation(const reader& rdr,
686 const Dwarf_Die* die,
687 const string& indent,
688 bool one_line,
689 bool qualified_names,
690 size_t where_offset);
691
692static string
693die_pretty_print_type(const reader& rdr,
694 const Dwarf_Die* die,
695 size_t where_offset,
696 unordered_set<uint64_t>& guard);
697
698static string
699die_pretty_print_decl(const reader& rdr,
700 const Dwarf_Die* die,
701 bool qualified_name,
702 bool include_fns,
703 size_t where_offset,
704 unordered_set<uint64_t>& infinite_loop_guard);
705
706static string
707die_pretty_print(reader& rdr,
708 const Dwarf_Die* die,
709 size_t where_offset,
710 unordered_set<uint64_t>& infinite_loop_guard);
711
712static void
713maybe_canonicalize_type(const type_base_sptr& t,
714 reader& rdr);
715
716static uint64_t
717get_default_array_lower_bound(translation_unit::language l);
718
719static bool
720find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
721 Dwarf_Off,
722 imported_unit_points_type::const_iterator&);
723
725build_subrange_type(reader& rdr,
726 const Dwarf_Die* die,
727 size_t where_offset,
728 bool associate_type_to_die = true);
729
730static void
731build_subranges_from_array_type_die(const reader& rdr,
732 const Dwarf_Die* die,
734 size_t where_offset,
735 bool associate_type_to_die = true);
736
738compare_dies(const reader& rdr,
739 const Dwarf_Die *l, const Dwarf_Die *r,
740 bool update_canonical_dies_on_the_fly);
741
742static bool
743compare_dies_during_canonicalization(reader& rdr,
744 const Dwarf_Die *l, const Dwarf_Die *r,
745 bool update_canonical_dies_on_the_fly);
746
747static bool
748get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
749
750static bool
751get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member);
752
753/// Get the language used to generate a given DIE.
754///
755/// @param die the DIE to consider.
756///
757/// @param lang the resulting language.
758///
759/// @return true iff the language of the DIE was found.
760static bool
761get_die_language(const Dwarf_Die *die, translation_unit::language &lang)
762{
763 Dwarf_Die cu_die;
764 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
765
766 uint64_t l = 0;
767 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
768 return false;
769
770 lang = dwarf_language_to_tu_language(l);
771 return true;
772}
773
774/// Test if a given DIE originates from a program written in the C
775/// language.
776///
777/// @param die the DIE to consider.
778///
779/// @return true iff @p die originates from a program in the C
780/// language.
781static bool
782die_is_in_c(const Dwarf_Die *die)
783{
784 translation_unit::language l = translation_unit::LANG_UNKNOWN;
785 if (!get_die_language(die, l))
786 return false;
787 return is_c_language(l);
788}
789
790/// Test if a given DIE originates from a program written in the C++
791/// language.
792///
793/// @param die the DIE to consider.
794///
795/// @return true iff @p die originates from a program in the C++
796/// language.
797static bool
798die_is_in_cplus_plus(const Dwarf_Die *die)
799{
800 translation_unit::language l = translation_unit::LANG_UNKNOWN;
801 if (!get_die_language(die, l))
802 return false;
803 return is_cplus_plus_language(l);
804}
805
806/// Test if a given DIE originates from a program written either in
807/// C or C++.
808///
809/// @param die the DIE to consider.
810///
811/// @return true iff @p die originates from a program written either in
812/// C or C++.
813static bool
814die_is_in_c_or_cplusplus(const Dwarf_Die *die)
815{
816 translation_unit::language l = translation_unit::LANG_UNKNOWN;
817 if (!get_die_language(die, l))
818 return false;
819 return (is_cplus_plus_language(l) || is_c_language(l));
820}
821
822/// Compare a symbol name against another name, possibly demangling
823/// the symbol_name before performing the comparison.
824///
825/// @param symbol_name the symbol_name to take in account.
826///
827/// @param name the second name to take in account.
828///
829/// @param demangle if true, demangle @p symbol_name and compare the
830/// result of the demangling with @p name.
831///
832/// @return true iff symbol_name equals name.
833static bool
834compare_symbol_name(const string& symbol_name,
835 const string& name,
836 bool demangle)
837{
838 if (demangle)
839 {
840 string m = demangle_cplus_mangled_name(symbol_name);
841 return m == name;
842 }
843 return symbol_name == name;
844}
845
846/// Lookup a symbol using the SysV ELF hash table.
847///
848/// Note that this function hasn't been tested. So it hasn't been
849/// debugged yet. IOW, it is not known to work. Or rather, it's
850/// almost like it's surely doesn't work ;-)
851///
852/// Use it at your own risks. :-)
853///
854///@parm env the environment we are operating from.
855///
856/// @param elf_handle the elf_handle to use.
857///
858/// @param sym_name the symbol name to look for.
859///
860/// @param ht_index the index (in the section headers table) of the
861/// hash table section to use.
862///
863/// @param sym_tab_index the index (in the section headers table) of
864/// the symbol table to use.
865///
866/// @param demangle if true, demangle @p sym_name before comparing it
867/// to names from the symbol table.
868///
869/// @param syms_found a vector of symbols found with the name @p
870/// sym_name. table.
871static bool
872lookup_symbol_from_sysv_hash_tab(const environment& env,
873 Elf* elf_handle,
874 const string& sym_name,
875 size_t ht_index,
876 size_t sym_tab_index,
877 bool demangle,
878 vector<elf_symbol_sptr>& syms_found)
879{
880 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
881 ABG_ASSERT(sym_tab_section);
882
883 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
884 ABG_ASSERT(sym_tab_data);
885
886 GElf_Shdr sheader_mem;
887 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
888 &sheader_mem);
889 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
890 ABG_ASSERT(hash_section);
891
892 // Poke at the different parts of the hash table and get them ready
893 // to be used.
894 unsigned long hash = elf_hash(sym_name.c_str());
895 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
896 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
897 size_t nb_buckets = ht_data[0];
898 size_t nb_chains = ht_data[1];
899
900 if (nb_buckets == 0)
901 // An empty hash table. Not sure if that is possible, but it
902 // would mean an empty table of exported symbols.
903 return false;
904
905 //size_t nb_chains = ht_data[1];
906 Elf32_Word* ht_buckets = &ht_data[2];
907 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
908
909 // Now do the real work.
910 size_t bucket = hash % nb_buckets;
911 size_t symbol_index = ht_buckets[bucket];
912
913 GElf_Sym symbol;
914 const char* sym_name_str;
915 size_t sym_size;
916 elf_symbol::type sym_type;
917 elf_symbol::binding sym_binding;
918 elf_symbol::visibility sym_visibility;
919 bool found = false;
920
921 do
922 {
923 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
924 sym_name_str = elf_strptr(elf_handle,
925 sym_tab_section_header->sh_link,
926 symbol.st_name);
927 if (sym_name_str
928 && compare_symbol_name(sym_name_str, sym_name, demangle))
929 {
930 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
931 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
932 sym_visibility =
933 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
934 sym_size = symbol.st_size;
935 elf_symbol::version ver;
936 if (get_version_for_symbol(elf_handle, symbol_index,
937 /*get_def_version=*/true, ver))
938 ABG_ASSERT(!ver.str().empty());
939 elf_symbol_sptr symbol_found =
941 symbol_index,
942 sym_size,
943 sym_name_str,
944 sym_type,
945 sym_binding,
946 symbol.st_shndx != SHN_UNDEF,
947 symbol.st_shndx == SHN_COMMON,
948 ver, sym_visibility);
949 syms_found.push_back(symbol_found);
950 found = true;
951 }
952 symbol_index = ht_chains[symbol_index];
953 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
954
955 return found;
956}
957
958/// Get the size of the elf class, in bytes.
959///
960/// @param elf_handle the elf handle to use.
961///
962/// @return the size computed.
963static char
964get_elf_class_size_in_bytes(Elf* elf_handle)
965{
966 char result = 0;
967 GElf_Ehdr hdr;
968
969 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
970 int c = hdr.e_ident[EI_CLASS];
971
972 switch (c)
973 {
974 case ELFCLASS32:
975 result = 4;
976 break;
977 case ELFCLASS64:
978 result = 8;
979 break;
980 default:
982 }
983
984 return result;
985}
986
987/// Get a given word of a bloom filter, referred to by the index of
988/// the word.
989///
990/// The bloom word size depends on the current elf class (32 bits for
991/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
992/// abstracts that nicely.
993///
994/// @param elf_handle the elf handle to use.
995///
996/// @param bloom_filter the bloom filter to consider.
997///
998/// @param index the index of the bloom filter to return.
999///
1000/// @return a 64 bits work containing the bloom word found at index @p
1001/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
1002/// most significant bytes of the result are going to be zero.
1003static Elf64_Xword
1004bloom_word_at(Elf* elf_handle,
1005 Elf32_Word* bloom_filter,
1006 size_t index)
1007{
1008 Elf64_Xword result = 0;
1009 GElf_Ehdr h;
1010 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
1011 int c;
1012 c = h.e_ident[EI_CLASS];
1013
1014 switch(c)
1015 {
1016 case ELFCLASS32:
1017 result = bloom_filter[index];
1018 break ;
1019 case ELFCLASS64:
1020 {
1021 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
1022 result = f[index];
1023 }
1024 break;
1025 default:
1026 abort();
1027 }
1028
1029 return result;
1030}
1031
1032/// The abstraction of the gnu elf hash table.
1033///
1034/// The members of this struct are explained at
1035/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1036/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1037struct gnu_ht
1038{
1039 size_t nb_buckets;
1040 Elf32_Word* buckets;
1041 Elf32_Word* chain;
1042 size_t first_sym_index;
1043 size_t bf_nwords;
1044 size_t bf_size;
1045 Elf32_Word* bloom_filter;
1046 size_t shift;
1047 size_t sym_count;
1048 Elf_Scn* sym_tab_section;
1049 GElf_Shdr sym_tab_section_header;
1050
1051 gnu_ht()
1052 : nb_buckets(0),
1053 buckets(0),
1054 chain(0),
1055 first_sym_index(0),
1056 bf_nwords(0),
1057 bf_size(0),
1058 bloom_filter(0),
1059 shift(0),
1060 sym_count(0),
1061 sym_tab_section(0)
1062 {}
1063}; // end struct gnu_ht
1064
1065/// Setup the members of the gnu hash table.
1066///
1067/// @param elf_handle a handle on the elf file to use.
1068///
1069/// @param ht_index the index (into the elf section headers table) of
1070/// the hash table section to use.
1071///
1072/// @param sym_tab_index the index (into the elf section headers
1073/// table) of the symbol table the gnu hash table is about.
1074///
1075/// @param ht the resulting hash table.
1076///
1077/// @return true iff the hash table @ ht could be setup.
1078static bool
1079setup_gnu_ht(Elf* elf_handle,
1080 size_t ht_index,
1081 size_t sym_tab_index,
1082 gnu_ht& ht)
1083{
1084 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1085 ABG_ASSERT(ht.sym_tab_section);
1086 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
1087 ht.sym_count =
1088 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
1089 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1090 ABG_ASSERT(hash_section);
1091
1092 // Poke at the different parts of the hash table and get them ready
1093 // to be used.
1094 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1095 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1096
1097 ht.nb_buckets = ht_data[0];
1098 if (ht.nb_buckets == 0)
1099 // An empty hash table. Not sure if that is possible, but it
1100 // would mean an empty table of exported symbols.
1101 return false;
1102 ht.first_sym_index = ht_data[1];
1103 // The number of words used by the bloom filter. A size of a word
1104 // is ELFCLASS.
1105 ht.bf_nwords = ht_data[2];
1106 // The shift used by the bloom filter code.
1107 ht.shift = ht_data[3];
1108 // The data of the bloom filter proper.
1109 ht.bloom_filter = &ht_data[4];
1110 // The size of the bloom filter in 4 bytes word. This is going to
1111 // be used to index the 'bloom_filter' above, which is of type
1112 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1113 // words.
1114 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1115 // The buckets of the hash table.
1116 ht.buckets = ht.bloom_filter + ht.bf_size;
1117 // The chain of the hash table.
1118 ht.chain = ht.buckets + ht.nb_buckets;
1119
1120 return true;
1121}
1122
1123/// Look into the symbol tables of the underlying elf file and find
1124/// the symbol we are being asked.
1125///
1126/// This function uses the GNU hash table for the symbol lookup.
1127///
1128/// The reference of for the implementation of this function can be
1129/// found at:
1130/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1131/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1132///
1133/// @param elf_handle the elf handle to use.
1134///
1135/// @param sym_name the name of the symbol to look for.
1136///
1137/// @param ht_index the index of the hash table header to use.
1138///
1139/// @param sym_tab_index the index of the symbol table header to use
1140/// with this hash table.
1141///
1142/// @param demangle if true, demangle @p sym_name.
1143///
1144/// @param syms_found the vector of symbols found with the name @p
1145/// sym_name.
1146///
1147/// @return true if a symbol was actually found.
1148static bool
1149lookup_symbol_from_gnu_hash_tab(const environment& env,
1150 Elf* elf_handle,
1151 const string& sym_name,
1152 size_t ht_index,
1153 size_t sym_tab_index,
1154 bool demangle,
1155 vector<elf_symbol_sptr>& syms_found)
1156{
1157 gnu_ht ht;
1158 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
1159 return false;
1160
1161 // Now do the real work.
1162
1163 // Compute bloom hashes (GNU hash and second bloom specific hashes).
1164 size_t h1 = elf_gnu_hash(sym_name.c_str());
1165 size_t h2 = h1 >> ht.shift;
1166 // The size of one of the words used in the bloom
1167 // filter, in bits.
1168 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1169 int n = (h1 / c) % ht.bf_nwords;
1170 // The bitmask of the bloom filter has a size of either 32-bits on
1171 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1172 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1173 // type used here. When dealing with 32bits binaries, the upper
1174 // bits of the bitmask will be zero anyway.
1175 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1176
1177 // Test if the symbol is *NOT* present in this ELF file.
1178 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1179 return false;
1180
1181 size_t i = ht.buckets[h1 % ht.nb_buckets];
1182 if (i == STN_UNDEF)
1183 return false;
1184
1185 Elf32_Word stop_word, *stop_wordp;
1186 elf_symbol::version ver;
1187 GElf_Sym symbol;
1188 const char* sym_name_str;
1189 bool found = false;
1190
1191 elf_symbol::type sym_type;
1192 elf_symbol::binding sym_binding;
1193 elf_symbol::visibility sym_visibility;
1194
1195 // Let's walk the hash table and record the versions of all the
1196 // symbols which name equal sym_name.
1197 for (i = ht.buckets[h1 % ht.nb_buckets],
1198 stop_wordp = &ht.chain[i - ht.first_sym_index];
1199 i != STN_UNDEF
1200 && (stop_wordp
1201 < ht.chain + (ht.sym_count - ht.first_sym_index));
1202 ++i, ++stop_wordp)
1203 {
1204 stop_word = *stop_wordp;
1205 if ((stop_word & ~ 1)!= (h1 & ~1))
1206 // A given bucket can reference several hashes. Here we
1207 // stumbled across a hash value different from the one we are
1208 // looking for. Let's keep walking.
1209 continue;
1210
1211 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1212 i, &symbol));
1213 sym_name_str = elf_strptr(elf_handle,
1214 ht.sym_tab_section_header.sh_link,
1215 symbol.st_name);
1216 if (sym_name_str
1217 && compare_symbol_name(sym_name_str, sym_name, demangle))
1218 {
1219 // So we found a symbol (in the symbol table) that equals
1220 // sym_name. Now lets try to get its version and record it.
1221 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1222 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1223 sym_visibility =
1224 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1225
1226 if (get_version_for_symbol(elf_handle, i,
1227 /*get_def_version=*/true,
1228 ver))
1229 ABG_ASSERT(!ver.str().empty());
1230
1231 elf_symbol_sptr symbol_found =
1232 elf_symbol::create(env, i,
1233 symbol.st_size,
1234 sym_name_str,
1235 sym_type, sym_binding,
1236 symbol.st_shndx != SHN_UNDEF,
1237 symbol.st_shndx == SHN_COMMON,
1238 ver, sym_visibility);
1239 syms_found.push_back(symbol_found);
1240 found = true;
1241 }
1242
1243 if (stop_word & 1)
1244 // The last bit of the stop_word is 1. That means we need to
1245 // stop here. We reached the end of the chain of values
1246 // referenced by the hask bucket.
1247 break;
1248 }
1249 return found;
1250}
1251
1252/// Look into the symbol tables of the underlying elf file and find
1253/// the symbol we are being asked.
1254///
1255/// This function uses the elf hash table (be it the GNU hash table or
1256/// the sysv hash table) for the symbol lookup.
1257///
1258/// @param env the environment we are operating from.
1259///
1260/// @param elf_handle the elf handle to use.
1261///
1262/// @param ht_kind the kind of hash table to use. This is returned by
1263/// the function function find_hash_table_section_index.
1264///
1265/// @param ht_index the index (in the section headers table) of the
1266/// hash table section to use.
1267///
1268/// @param sym_tab_index the index (in section headers table) of the
1269/// symbol table index to use with this hash table.
1270///
1271/// @param symbol_name the name of the symbol to look for.
1272///
1273/// @param demangle if true, demangle @p sym_name.
1274///
1275/// @param syms_found the symbols that were actually found with the
1276/// name @p symbol_name.
1277///
1278/// @return true iff the function found the symbol from the elf hash
1279/// table.
1280static bool
1281lookup_symbol_from_elf_hash_tab(const environment& env,
1282 Elf* elf_handle,
1283 hash_table_kind ht_kind,
1284 size_t ht_index,
1285 size_t symtab_index,
1286 const string& symbol_name,
1287 bool demangle,
1288 vector<elf_symbol_sptr>& syms_found)
1289{
1290 if (elf_handle == 0 || symbol_name.empty())
1291 return false;
1292
1293 if (ht_kind == NO_HASH_TABLE_KIND)
1294 return false;
1295
1296 if (ht_kind == SYSV_HASH_TABLE_KIND)
1297 return lookup_symbol_from_sysv_hash_tab(env,
1298 elf_handle, symbol_name,
1299 ht_index,
1300 symtab_index,
1301 demangle,
1302 syms_found);
1303 else if (ht_kind == GNU_HASH_TABLE_KIND)
1304 return lookup_symbol_from_gnu_hash_tab(env,
1305 elf_handle, symbol_name,
1306 ht_index,
1307 symtab_index,
1308 demangle,
1309 syms_found);
1310 return false;
1311}
1312
1313/// Lookup a symbol from the symbol table directly.
1314///
1315///
1316/// @param env the environment we are operating from.
1317///
1318/// @param elf_handle the elf handle to use.
1319///
1320/// @param sym_name the name of the symbol to look up.
1321///
1322/// @param sym_tab_index the index (in the section headers table) of
1323/// the symbol table section.
1324///
1325/// @param demangle if true, demangle the names found in the symbol
1326/// table before comparing them with @p sym_name.
1327///
1328/// @param sym_name_found the actual name of the symbol found.
1329///
1330/// @param sym_type the type of the symbol found.
1331///
1332/// @param sym_binding the binding of the symbol found.
1333///
1334/// @param sym_versions the versions of the symbol found.
1335///
1336/// @return true iff the symbol was found.
1337static bool
1338lookup_symbol_from_symtab(const environment& env,
1339 Elf* elf_handle,
1340 const string& sym_name,
1341 size_t sym_tab_index,
1342 bool demangle,
1343 vector<elf_symbol_sptr>& syms_found)
1344{
1345 // TODO: read all of the symbol table, store it in memory in a data
1346 // structure that associates each symbol with its versions and in
1347 // which lookups of a given symbol is fast.
1348 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1349 ABG_ASSERT(sym_tab_section);
1350
1351 GElf_Shdr header_mem;
1352 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1353 &header_mem);
1354
1355 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1356 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1357 GElf_Sym* sym;
1358 char* name_str = 0;
1359 elf_symbol::version ver;
1360 bool found = false;
1361
1362 for (size_t i = 0; i < symcount; ++i)
1363 {
1364 GElf_Sym sym_mem;
1365 sym = gelf_getsym(symtab, i, &sym_mem);
1366 name_str = elf_strptr(elf_handle,
1367 sym_tab_header->sh_link,
1368 sym->st_name);
1369
1370 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1371 {
1372 elf_symbol::type sym_type =
1373 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1374 elf_symbol::binding sym_binding =
1375 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1376 elf_symbol::visibility sym_visibility =
1377 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1378 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1379 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1380
1381 if (get_version_for_symbol(elf_handle, i,
1382 /*get_def_version=*/sym_is_defined,
1383 ver))
1384 ABG_ASSERT(!ver.str().empty());
1385 elf_symbol_sptr symbol_found =
1386 elf_symbol::create(env, i, sym->st_size,
1387 name_str, sym_type,
1388 sym_binding, sym_is_defined,
1389 sym_is_common, ver, sym_visibility);
1390 syms_found.push_back(symbol_found);
1391 found = true;
1392 }
1393 }
1394
1395 if (found)
1396 return true;
1397
1398 return false;
1399}
1400
1401/// Look into the symbol tables of the underlying elf file and see
1402/// if we find a given symbol.
1403///
1404/// @param env the environment we are operating from.
1405///
1406/// @param symbol_name the name of the symbol to look for.
1407///
1408/// @param demangle if true, try to demangle the symbol name found in
1409/// the symbol table before comparing it to @p symbol_name.
1410///
1411/// @param syms_found the list of symbols found, with the name @p
1412/// symbol_name.
1413///
1414/// @param sym_type this is set to the type of the symbol found. This
1415/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1416/// STT_FUNC, STT_IFUNC, etc ...
1417///
1418/// Note that this parameter is set iff the function returns true.
1419///
1420/// @param sym_binding this is set to the binding of the symbol found.
1421/// This is a standard elf.h value of the symbol binding kind, that
1422/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1423///
1424/// @param symbol_versions the versions of the symbol @p symbol_name,
1425/// if it was found.
1426///
1427/// @return true iff a symbol with the name @p symbol_name was found.
1428static bool
1429lookup_symbol_from_elf(const environment& env,
1430 Elf* elf_handle,
1431 const string& symbol_name,
1432 bool demangle,
1433 vector<elf_symbol_sptr>& syms_found)
1434{
1435 size_t hash_table_index = 0, symbol_table_index = 0;
1436 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1437
1438 if (!demangle)
1439 ht_kind = find_hash_table_section_index(elf_handle,
1440 hash_table_index,
1441 symbol_table_index);
1442
1443 if (ht_kind == NO_HASH_TABLE_KIND)
1444 {
1445 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1446 return false;
1447
1448 return lookup_symbol_from_symtab(env,
1449 elf_handle,
1450 symbol_name,
1451 symbol_table_index,
1452 demangle,
1453 syms_found);
1454 }
1455
1456 return lookup_symbol_from_elf_hash_tab(env,
1457 elf_handle,
1458 ht_kind,
1459 hash_table_index,
1460 symbol_table_index,
1461 symbol_name,
1462 demangle,
1463 syms_found);
1464}
1465
1466/// Look into the symbol tables of the underlying elf file and see if
1467/// we find a given public (global or weak) symbol of function type.
1468///
1469/// @param env the environment we are operating from.
1470///
1471/// @param elf_handle the elf handle to use for the query.
1472///
1473/// @param symbol_name the function symbol to look for.
1474///
1475/// @param func_syms the vector of public functions symbols found, if
1476/// any.
1477///
1478/// @return true iff the symbol was found.
1479static bool
1480lookup_public_function_symbol_from_elf(environment& env,
1481 Elf* elf_handle,
1482 const string& symbol_name,
1483 vector<elf_symbol_sptr>& func_syms)
1484{
1485 vector<elf_symbol_sptr> syms_found;
1486 bool found = false;
1487
1488 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1489 /*demangle=*/false, syms_found))
1490 {
1491 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1492 i != syms_found.end();
1493 ++i)
1494 {
1495 elf_symbol::type type = (*i)->get_type();
1496 elf_symbol::binding binding = (*i)->get_binding();
1497
1498 if ((type == elf_symbol::FUNC_TYPE
1499 || type == elf_symbol::GNU_IFUNC_TYPE
1500 || type == elf_symbol::COMMON_TYPE)
1501 && (binding == elf_symbol::GLOBAL_BINDING
1502 || binding == elf_symbol::WEAK_BINDING))
1503 {
1504 func_syms.push_back(*i);
1505 found = true;
1506 }
1507 }
1508 }
1509
1510 return found;
1511}
1512
1513// ---------------------------------------
1514// <location expression evaluation types>
1515// ---------------------------------------
1516
1517/// An abstraction of a value representing the result of the
1518/// evaluation of a dwarf expression. This is abstraction represents
1519/// a partial view on the possible values because we are only
1520/// interested in extracting the latest and longuest constant
1521/// sub-expression of a given dwarf expression.
1522class expr_result
1523{
1524 bool is_const_;
1525 int64_t const_value_;
1526
1527public:
1528 expr_result()
1529 : is_const_(true),
1530 const_value_(0)
1531 {}
1532
1533 expr_result(bool is_const)
1534 : is_const_(is_const),
1535 const_value_(0)
1536 {}
1537
1538 explicit expr_result(int64_t v)
1539 :is_const_(true),
1540 const_value_(v)
1541 {}
1542
1543 /// @return true if the value is a constant. Otherwise, return
1544 /// false, meaning the value represents a quantity for which we need
1545 /// inferior (a running program) state to determine the value.
1546 bool
1547 is_const() const
1548 {return is_const_;}
1549
1550
1551 /// @param f a flag saying if the value is set to a constant or not.
1552 void
1553 is_const(bool f)
1554 {is_const_ = f;}
1555
1556 /// Get the current constant value iff this represents a
1557 /// constant.
1558 ///
1559 /// @param value the out parameter. Is set to the constant value of
1560 /// the @ref expr_result. This is set iff the function return true.
1561 ///
1562 ///@return true if this has a constant value, false otherwise.
1563 bool
1564 const_value(int64_t& value)
1565 {
1566 if (is_const())
1567 {
1568 value = const_value_;
1569 return true;
1570 }
1571 return false;
1572 }
1573
1574 /// Getter of the constant value of the current @ref expr_result.
1575 ///
1576 /// Note that the current @ref expr_result must be constant,
1577 /// otherwise the current process is aborted.
1578 ///
1579 /// @return the constant value of the current @ref expr_result.
1580 int64_t
1581 const_value() const
1582 {
1583 ABG_ASSERT(is_const());
1584 return const_value_;
1585 }
1586
1587 operator int64_t() const
1588 {return const_value();}
1589
1590 expr_result&
1591 operator=(const int64_t v)
1592 {
1593 const_value_ = v;
1594 return *this;
1595 }
1596
1597 bool
1598 operator==(const expr_result& o) const
1599 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1600
1601 bool
1602 operator>=(const expr_result& o) const
1603 {return const_value_ >= o.const_value_;}
1604
1605 bool
1606 operator<=(const expr_result& o) const
1607 {return const_value_ <= o.const_value_;}
1608
1609 bool
1610 operator>(const expr_result& o) const
1611 {return const_value_ > o.const_value_;}
1612
1613 bool
1614 operator<(const expr_result& o) const
1615 {return const_value_ < o.const_value_;}
1616
1617 expr_result
1618 operator+(const expr_result& v) const
1619 {
1620 expr_result r(*this);
1621 r.const_value_ += v.const_value_;
1622 r.is_const_ = r.is_const_ && v.is_const_;
1623 return r;
1624 }
1625
1626 expr_result&
1627 operator+=(int64_t v)
1628 {
1629 const_value_ += v;
1630 return *this;
1631 }
1632
1633 expr_result
1634 operator-(const expr_result& v) const
1635 {
1636 expr_result r(*this);
1637 r.const_value_ -= v.const_value_;
1638 r.is_const_ = r.is_const_ && v.is_const_;
1639 return r;
1640 }
1641
1642 expr_result
1643 operator%(const expr_result& v) const
1644 {
1645 expr_result r(*this);
1646 r.const_value_ %= v.const_value_;
1647 r.is_const_ = r.is_const_ && v.is_const();
1648 return r;
1649 }
1650
1651 expr_result
1652 operator*(const expr_result& v) const
1653 {
1654 expr_result r(*this);
1655 r.const_value_ *= v.const_value_;
1656 r.is_const_ = r.is_const_ && v.is_const();
1657 return r;
1658 }
1659
1660 expr_result
1661 operator|(const expr_result& v) const
1662 {
1663 expr_result r(*this);
1664 r.const_value_ |= v.const_value_;
1665 r.is_const_ = r.is_const_ && v.is_const_;
1666 return r;
1667 }
1668
1669 expr_result
1670 operator^(const expr_result& v) const
1671 {
1672 expr_result r(*this);
1673 r.const_value_ ^= v.const_value_;
1674 r.is_const_ = r.is_const_ && v.is_const_;
1675 return r;
1676 }
1677
1678 expr_result
1679 operator>>(const expr_result& v) const
1680 {
1681 expr_result r(*this);
1682 r.const_value_ = r.const_value_ >> v.const_value_;
1683 r.is_const_ = r.is_const_ && v.is_const_;
1684 return r;
1685 }
1686
1687 expr_result
1688 operator<<(const expr_result& v) const
1689 {
1690 expr_result r(*this);
1691 r.const_value_ = r.const_value_ << v.const_value_;
1692 r.is_const_ = r.is_const_ && v.is_const_;
1693 return r;
1694 }
1695
1696 expr_result
1697 operator~() const
1698 {
1699 expr_result r(*this);
1700 r.const_value_ = ~r.const_value_;
1701 return r;
1702 }
1703
1704 expr_result
1705 neg() const
1706 {
1707 expr_result r(*this);
1708 r.const_value_ = -r.const_value_;
1709 return r;
1710 }
1711
1712 expr_result
1713 abs() const
1714 {
1715 expr_result r = *this;
1716 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1717 return r;
1718 }
1719
1720 expr_result
1721 operator&(const expr_result& o)
1722 {
1723 expr_result r(*this);
1724 r.const_value_ &= o.const_value_;
1725 r.is_const_ = r.is_const_ && o.is_const_;
1726 return r;
1727 }
1728
1729 expr_result
1730 operator/(const expr_result& o)
1731 {
1732 expr_result r(*this);
1733 r.is_const_ = r.is_const_ && o.is_const_;
1734 return r.const_value() / o.const_value();
1735 }
1736};// class end expr_result;
1737
1738/// A class that implements a stack of @ref expr_result, to be used in
1739/// the engine evaluating DWARF expressions.
1740class expr_result_stack_type
1741{
1742 vector<expr_result> elems_;
1743
1744public:
1745
1746 expr_result_stack_type()
1747 {elems_.reserve(4);}
1748
1749 expr_result&
1750 operator[](unsigned i)
1751 {
1752 unsigned s = elems_.size();
1753 ABG_ASSERT(s > i);
1754 return elems_[s - 1 -i];
1755 }
1756
1757 const expr_result&
1758 operator[](unsigned i) const
1759 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1760
1761 unsigned
1762 size() const
1763 {return elems_.size();}
1764
1765 vector<expr_result>::reverse_iterator
1766 begin()
1767 {return elems_.rbegin();}
1768
1769 const vector<expr_result>::reverse_iterator
1770 begin() const
1771 {return const_cast<expr_result_stack_type*>(this)->begin();}
1772
1773 vector<expr_result>::reverse_iterator
1774 end()
1775 {return elems_.rend();}
1776
1777 const vector<expr_result>::reverse_iterator
1778 end() const
1779 {return const_cast<expr_result_stack_type*>(this)->end();}
1780
1781 expr_result&
1782 front()
1783 {return elems_.back();}
1784
1785 const expr_result&
1786 front() const
1787 {return const_cast<expr_result_stack_type*>(this)->front();}
1788
1789 void
1790 push_front(expr_result e)
1791 {elems_.push_back(e);}
1792
1793 expr_result
1794 pop_front()
1795 {
1796 expr_result r = front();
1797 elems_.pop_back();
1798 return r;
1799 }
1800
1801 void
1802 erase(vector<expr_result>::reverse_iterator i)
1803 {elems_.erase(--i.base());}
1804
1805 void
1806 clear()
1807 {elems_.clear();}
1808}; // end class expr_result_stack_type
1809
1810/// Abstraction of the evaluation context of a dwarf expression.
1811struct dwarf_expr_eval_context
1812{
1813 expr_result accum;
1814 expr_result_stack_type stack;
1815 // Is set to true if the result of the expression that got evaluated
1816 // is a TLS address.
1817 bool set_tls_addr;
1818
1819 dwarf_expr_eval_context()
1820 : accum(/*is_const=*/false),
1821 set_tls_addr(false)
1822 {
1823 stack.push_front(expr_result(true));
1824 }
1825
1826 void
1827 reset()
1828 {
1829 stack.clear();
1830 stack.push_front(expr_result(true));
1831 accum = expr_result(false);
1832 set_tls_addr = false;
1833 }
1834
1835 /// Set a flag to to tell that the result of the expression that got
1836 /// evaluated is a TLS address.
1837 ///
1838 /// @param f true iff the result of the expression that got
1839 /// evaluated is a TLS address, false otherwise.
1840 void
1841 set_tls_address(bool f)
1842 {set_tls_addr = f;}
1843
1844 /// Getter for the flag that tells if the result of the expression
1845 /// that got evaluated is a TLS address.
1846 ///
1847 /// @return true iff the result of the expression that got evaluated
1848 /// is a TLS address.
1849 bool
1850 set_tls_address() const
1851 {return set_tls_addr;}
1852
1853 expr_result
1854 pop()
1855 {
1856 expr_result r = stack.front();
1857 stack.pop_front();
1858 return r;
1859 }
1860
1861 void
1862 push(const expr_result& v)
1863 {stack.push_front(v);}
1864};//end class dwarf_expr_eval_context
1865
1866// ---------------------------------------
1867// </location expression evaluation types>
1868// ---------------------------------------
1869
1870class reader;
1871
1872typedef shared_ptr<reader> reader_sptr;
1873
1874/// The DWARF reader used to build the ABI corpus from debug info in
1875/// DWARF format.
1876///
1877/// This type is to be instanciated
1878/// abigail::dwarf::reader::create().
1879class reader : public elf_based_reader
1880{
1881public:
1882
1883 /// A set of containers that contains one container per kind of @ref
1884 /// die_source. This allows to associate DIEs to things, depending
1885 /// on the source of the DIE.
1886 template <typename ContainerType>
1887 class die_source_dependant_container_set
1888 {
1889 ContainerType primary_debug_info_container_;
1890 ContainerType alt_debug_info_container_;
1891 ContainerType type_unit_container_;
1892
1893 public:
1894
1895 /// Getter for the container associated to DIEs coming from a
1896 /// given @ref die_source.
1897 ///
1898 /// @param source the die_source for which we want the container.
1899 ///
1900 /// @return the container that associates DIEs coming from @p
1901 /// source to something.
1902 ContainerType&
1903 get_container(die_source source)
1904 {
1905 ContainerType *result = 0;
1906 switch (source)
1907 {
1908 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1909 result = &primary_debug_info_container_;
1910 break;
1911 case ALT_DEBUG_INFO_DIE_SOURCE:
1912 result = &alt_debug_info_container_;
1913 break;
1914 case TYPE_UNIT_DIE_SOURCE:
1915 result = &type_unit_container_;
1916 break;
1917 case NO_DEBUG_INFO_DIE_SOURCE:
1918 case NUMBER_OF_DIE_SOURCES:
1920 }
1921 return *result;
1922 }
1923
1924 /// Getter for the container associated to DIEs coming from a
1925 /// given @ref die_source.
1926 ///
1927 /// @param source the die_source for which we want the container.
1928 ///
1929 /// @return the container that associates DIEs coming from @p
1930 /// source to something.
1931 const ContainerType&
1932 get_container(die_source source) const
1933 {
1934 return const_cast<die_source_dependant_container_set*>(this)->
1935 get_container(source);
1936 }
1937
1938 /// Getter for the container associated to DIEs coming from the
1939 /// same source as a given DIE.
1940 ///
1941 /// @param rdr the DWARF reader to consider.
1942 ///
1943 /// @param die the DIE which should have the same source as the
1944 /// source of the container we want.
1945 ///
1946 /// @return the container that associates DIEs coming from the
1947 /// same source as @p die.
1948 ContainerType&
1949 get_container(const reader& rdr, const Dwarf_Die *die)
1950 {
1951 const die_source source = rdr.get_die_source(die);
1952 return get_container(source);
1953 }
1954
1955 /// Getter for the container associated to DIEs coming from the
1956 /// same source as a given DIE.
1957 ///
1958 /// @param rdr the DWARF reader to consider.
1959 ///
1960 /// @param die the DIE which should have the same source as the
1961 /// source of the container we want.
1962 ///
1963 /// @return the container that associates DIEs coming from the
1964 /// same source as @p die.
1965 const ContainerType&
1966 get_container(const reader& rdr, const Dwarf_Die *die) const
1967 {
1968 return const_cast<die_source_dependant_container_set*>(this)->
1969 get_container(rdr, die);
1970 }
1971
1972 /// Clear the container set.
1973 void
1974 clear()
1975 {
1976 primary_debug_info_container_.clear();
1977 alt_debug_info_container_.clear();
1978 type_unit_container_.clear();
1979 }
1980 }; // end die_dependant_container_set
1981
1982 /// Statistics to help for debugging purposes.
1983 struct stats
1984 {
1985 unsigned number_of_suppressed_functions = 0;
1986 unsigned number_of_suppressed_variables = 0;
1987 unsigned number_of_allowed_functions = 0;
1988 unsigned number_of_allowed_variables = 0;
1989
1990 /// Clear the statistic data members.
1991 void
1992 clear()
1993 {
1994 number_of_suppressed_functions = 0;
1995 number_of_suppressed_variables = 0;
1996 number_of_allowed_functions = 0;
1997 number_of_allowed_variables = 0;
1998 }
1999 };
2000
2001 unsigned short dwarf_version_;
2002 Dwarf_Die* cur_tu_die_;
2003 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
2004 // A set of maps (one per kind of die source) that associates a decl
2005 // string representation with the DIEs (offsets) representing that
2006 // decl.
2007 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2008 decl_die_repr_die_offsets_maps_;
2009 // A set of maps (one per kind of die source) that associates a type
2010 // string representation with the DIEs (offsets) representing that
2011 // type.
2012 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2013 type_die_repr_die_offsets_maps_;
2014 mutable die_source_dependant_container_set<die_istring_map_type>
2015 die_qualified_name_maps_;
2016 mutable die_source_dependant_container_set<die_istring_map_type>
2017 die_pretty_repr_maps_;
2018 mutable die_source_dependant_container_set<die_istring_map_type>
2019 die_pretty_type_repr_maps_;
2020 // A set of maps (one per kind of die source) that associates the
2021 // offset of a decl die to its corresponding decl artifact.
2022 mutable die_source_dependant_container_set<die_artefact_map_type>
2023 decl_die_artefact_maps_;
2024 // A set of maps (one per kind of die source) that associates the
2025 // offset of a type die to its corresponding type artifact.
2026 mutable die_source_dependant_container_set<die_artefact_map_type>
2027 type_die_artefact_maps_;
2028 /// A set of vectors (one per kind of die source) that associates
2029 /// the offset of a type DIE to the offset of its canonical DIE.
2030 mutable die_source_dependant_container_set<offset_offset_map_type>
2031 canonical_type_die_offsets_;
2032 /// A set of vectors (one per kind of die source) that associates
2033 /// the offset of a decl DIE to the offset of its canonical DIE.
2034 mutable die_source_dependant_container_set<offset_offset_map_type>
2035 canonical_decl_die_offsets_;
2036 /// A map that associates a function type representations to
2037 /// function types, inside a translation unit.
2038 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
2039 /// A map that associates a pair of DIE offsets to the result of the
2040 /// comparison of that pair.
2041 mutable std::unordered_map<std::pair<offset_type,offset_type>,
2043 dwarf_offset_pair_hash> die_comparison_results_;
2044 // The set of types pair that have been canonical-type-propagated.
2045 mutable offset_pair_set_type propagated_types_;
2046 die_class_or_union_map_type die_wip_classes_map_;
2047 die_class_or_union_map_type alternate_die_wip_classes_map_;
2048 die_class_or_union_map_type type_unit_die_wip_classes_map_;
2049 die_function_type_map_type die_wip_function_types_map_;
2050 die_function_type_map_type alternate_die_wip_function_types_map_;
2051 die_function_type_map_type type_unit_die_wip_function_types_map_;
2052 die_function_decl_map_type die_function_with_no_symbol_map_;
2053 vector<type_base_sptr> types_to_canonicalize_;
2054 string_classes_or_unions_map decl_only_classes_map_;
2055 string_enums_map decl_only_enums_map_;
2056 die_tu_map_type die_tu_map_;
2057 translation_unit_sptr cur_tu_;
2058 scope_decl_sptr nil_scope_;
2059 scope_stack_type scope_stack_;
2060 offset_offset_map_type primary_die_parent_map_;
2061 // A map that associates each tu die to a vector of unit import
2062 // points, in the main debug info
2063 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
2064 // A map that associates each tu die to a vector of unit import
2065 // points, in the alternate debug info
2066 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
2067 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
2068 // A DIE -> parent map for DIEs coming from the alternate debug info
2069 // file.
2070 offset_offset_map_type alternate_die_parent_map_;
2071 offset_offset_map_type type_section_die_parent_map_;
2072 list<var_decl_sptr> var_decls_to_add_;
2073#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2074 bool debug_die_canonicalization_is_on_;
2075 bool use_canonical_die_comparison_;
2076#endif
2077 mutable size_t compare_count_;
2078 mutable size_t canonical_propagated_count_;
2079 mutable size_t cancelled_propagation_count_;
2080 mutable optional<bool> leverage_dwarf_factorization_;
2081 mutable stats stats_;
2082
2083protected:
2084
2085 reader() = delete;
2086
2087 /// Constructor of reader.
2088 ///
2089 /// @param elf_path the path to the elf file the context is to be
2090 /// used for.
2091 ///
2092 /// @param debug_info_root_paths a vector of pointers to the path to
2093 /// the root directory under which the debug info is to be found for
2094 /// @p elf_path. Leave this empty if the debug info is not in a
2095 /// split file.
2096 ///
2097 /// @param environment the environment used by the current context.
2098 /// This environment contains resources needed by the DWARF reader and by
2099 /// the types and declarations that are to be created later. Note
2100 /// that ABI artifacts that are to be compared all need to be
2101 /// created within the same environment.
2102 ///
2103 /// Please also note that the life time of this environment object
2104 /// must be greater than the life time of the resulting @ref
2105 /// reader the context uses resources that are allocated in
2106 /// the environment.
2107 ///
2108 /// @param load_all_types if set to false only the types that are
2109 /// reachable from publicly exported declarations (of functions and
2110 /// variables) are read. If set to true then all types found in the
2111 /// debug information are loaded.
2112 ///
2113 /// @param linux_kernel_mode if set to true, then consider the special
2114 /// linux kernel symbol tables when determining if a symbol is
2115 /// exported or not.
2116 reader(const string& elf_path,
2117 const vector<string>& debug_info_root_paths,
2118 environment& environment,
2119 bool load_all_types,
2120 bool linux_kernel_mode)
2121 : elf_based_reader(elf_path,
2123 environment)
2124 {
2125 reset(load_all_types, linux_kernel_mode);
2126 }
2127
2128 /// Clear the statistics for reading the current corpus.
2129 void
2130 clear_stats()
2131 {
2132 stats_.clear();
2133 }
2134
2135public:
2136
2137 /// Initializer of reader.
2138 ///
2139 /// Resets the reader so that it can be re-used to read another binary.
2140 ///
2141 /// @param load_all_types if set to false only the types that are
2142 /// reachable from publicly exported declarations (of functions and
2143 /// variables) are read. If set to true then all types found in the
2144 /// debug information are loaded.
2145 ///
2146 /// @param linux_kernel_mode if set to true, then consider the
2147 /// special linux kernel symbol tables when determining if a symbol
2148 /// is exported or not.
2149 void
2150 reset(bool load_all_types, bool linux_kernel_mode)
2151 {
2152 dwarf_version_ = 0;
2153 cur_tu_die_ = 0;
2154 decl_die_repr_die_offsets_maps_.clear();
2155 type_die_repr_die_offsets_maps_.clear();
2156 die_qualified_name_maps_.clear();
2157 die_pretty_repr_maps_.clear();
2158 die_pretty_type_repr_maps_.clear();
2159 decl_die_artefact_maps_.clear();
2160 type_die_artefact_maps_.clear();
2161 canonical_type_die_offsets_.clear();
2162 canonical_decl_die_offsets_.clear();
2163 die_wip_classes_map_.clear();
2164 alternate_die_wip_classes_map_.clear();
2165 type_unit_die_wip_classes_map_.clear();
2166 die_wip_function_types_map_.clear();
2167 alternate_die_wip_function_types_map_.clear();
2168 type_unit_die_wip_function_types_map_.clear();
2169 die_function_with_no_symbol_map_.clear();
2170 types_to_canonicalize_.clear();
2171 decl_only_classes_map_.clear();
2172 die_tu_map_.clear();
2173 corpus().reset();
2174 corpus_group().reset();
2175 cur_tu_.reset();
2176 primary_die_parent_map_.clear();
2177 tu_die_imported_unit_points_map_.clear();
2178 alt_tu_die_imported_unit_points_map_.clear();
2179 type_units_tu_die_imported_unit_points_map_.clear();
2180 alternate_die_parent_map_.clear();
2181 type_section_die_parent_map_.clear();
2182 var_decls_to_add_.clear();
2183 clear_per_translation_unit_data();
2184 clear_per_corpus_data();
2185 options().load_in_linux_kernel_mode = linux_kernel_mode;
2186 options().load_all_types = load_all_types;
2187#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2188 debug_die_canonicalization_is_on_ =
2189 env().debug_die_canonicalization_is_on();
2190 use_canonical_die_comparison_ = true;
2191#endif
2192 compare_count_ = 0;
2193 canonical_propagated_count_ = 0;
2194 cancelled_propagation_count_ = 0;
2195 load_in_linux_kernel_mode(linux_kernel_mode);
2196 clear_stats();
2197 }
2198
2199 /// Initializer of reader.
2200 ///
2201 /// Resets the reader so that it can be re-used to read another binary.
2202 ///
2203 /// @param elf_path the path to the new ELF file.
2204 ///
2205 /// @param debug_info_root_paths the vector of debug-info path to
2206 /// look for split debug info.
2207 ///
2208 /// @param load_all_types if set to false only the types that are
2209 /// reachable from publicly exported declarations (of functions and
2210 /// variables) are read. If set to true then all types found in the
2211 /// debug information are loaded.
2212 ///
2213 /// @param linux_kernel_mode if set to true, then consider the
2214 /// special linux kernel symbol tables when determining if a symbol
2215 /// is exported or not.
2216 void
2217 initialize(const string& elf_path,
2218 const vector<string>& debug_info_root_paths,
2219 bool load_all_types,
2220 bool linux_kernel_mode)
2221 {
2223 reset(load_all_types, linux_kernel_mode);
2224 }
2225
2226 /// Create an instance of DWARF Reader.
2227 ///
2228 /// @param elf_path the path to the ELF file to read from.
2229 ///
2230 /// @param debug_info_root_paths a vector of paths where to look up
2231 /// split debug info files.
2232 ///
2233 /// @param environment the environment to be used by the reader.
2234 ///
2235 /// @param load_all_types if set to false only the types that are
2236 /// reachable from publicly exported declarations (of functions and
2237 /// variables) are read. If set to true then all types found in the
2238 /// debug information are loaded.
2239 ///
2240 /// @param linux_kernel_mode if set to true, then consider the
2241 /// special linux kernel symbol tables when determining if a symbol
2242 /// is exported or not.
2243 static dwarf::reader_sptr
2244 create(const std::string& elf_path,
2245 const vector<string>& debug_info_root_paths,
2246 environment& environment,
2247 bool load_all_types,
2248 bool linux_kernel_mode)
2249 {
2250 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2251 environment, load_all_types,
2252 linux_kernel_mode));
2253 return result;
2254 }
2255
2256 /// Destructor of the @ref reader type.
2257 ~reader()
2258 {
2259 }
2260
2261 /// Read and analyze the ELF and DWARF information associated with
2262 /// the underlying ELF file and build an ABI corpus out of it.
2263 ///
2264 /// @param status output parameter. This is set to the status of
2265 /// the analysis of the debug info.
2266 ///
2267 /// @return the resulting ABI corpus.
2268 corpus_sptr
2269 read_corpus(status& status)
2270 {
2272
2273 // Load the generic ELF parts of the corpus.
2275
2276 if (!(status & STATUS_OK))
2277 {
2278 // Something went badly wrong. There is nothing we can do
2279 // with this ELF file. Bail out.
2280 return corpus_sptr();
2281 }
2282
2283 // If we couldn't find debug info from the elf path, then say it.
2284 if (dwarf_debug_info() == nullptr)
2286
2287 {
2288 string alt_di_path;
2289 if (refers_to_alt_debug_info(alt_di_path)
2292 }
2293
2294 if (// If debug info was found but not the required alternate debug
2295 // info ...
2298 // ... then we cannot handle the binary.
2299 return corpus_sptr();
2300
2301 // Read the variable and function descriptions from the debug info
2302 // we have, through the dwfl handle.
2303 corpus_sptr corp = read_debug_info_into_corpus();
2304
2305 status |= STATUS_OK;
2306
2307 return corp;
2308 }
2309
2310 /// Read an analyze the DWARF information.
2311 ///
2312 /// Construct an ABI corpus from it.
2313 ///
2314 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2315 ///
2316 /// @return the resulting ABI corpus.
2317 corpus_sptr
2318 read_debug_info_into_corpus()
2319 {
2320 // First set some mundane properties of the corpus gathered from
2321 // ELF.
2322 corpus::origin origin = corpus()->get_origin();
2323 origin |= corpus::DWARF_ORIGIN;
2324 corpus()->set_origin(origin);
2325 if (corpus_group())
2326 {
2327 origin |= corpus_group()->get_origin();
2328 corpus_group()->set_origin(origin);
2329 }
2330
2331 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2332 && !env().user_set_analyze_exported_interfaces_only())
2333 // So we are looking at the Linux Kernel and the user has not set
2334 // any particular option regarding the amount of types to analyse.
2335 // In that case, we need to only analyze types that are reachable
2336 // from exported interfaces otherwise we get such a massive amount
2337 // of type DIEs to look at that things are just too slow down the
2338 // road.
2339 env().analyze_exported_interfaces_only(true);
2340
2341 corpus()->set_soname(dt_soname());
2342 corpus()->set_needed(dt_needed());
2343 corpus()->set_architecture_name(elf_architecture());
2344 // Set symbols information to the corpus.
2345 corpus()->set_symtab(symtab());
2346
2347 // Get out now if no debug info is found or if the symbol table is
2348 // empty.
2349 if (!dwarf_debug_info()
2350 || !corpus()->get_symtab()
2351 || !corpus()->get_symtab()->has_symbols())
2352 return corpus();
2353
2354 uint8_t address_size = 0;
2355 size_t header_size = 0;
2356
2357#ifdef WITH_DEBUG_SELF_COMPARISON
2358 if (env().self_comparison_debug_is_on())
2359 {
2360 corpus_group_sptr g = corpus_group();
2361 if (g)
2362 env().set_self_comparison_debug_input(g);
2363 else
2364 env().set_self_comparison_debug_input(corpus());
2365 }
2366#endif
2367
2368 env().priv_->do_log(do_log());
2369
2370 // Walk all the DIEs of the debug info to build a DIE -> parent map
2371 // useful for get_die_parent() to work.
2372 {
2373 tools_utils::timer t;
2374 if (do_log())
2375 {
2376 cerr << "building die -> parent maps ...";
2377 t.start();
2378 }
2379
2380 build_die_parent_maps();
2381
2382 if (do_log())
2383 {
2384 t.stop();
2385 cerr << " DONE@" << corpus()->get_path()
2386 << ":"
2387 << t
2388 << "\n";
2389 }
2390 }
2391
2392 env().canonicalization_is_done(false);
2393
2394 {
2395 tools_utils::timer t;
2396 if (do_log())
2397 {
2398 cerr << "DWARF Reader: building the "
2399 "libabigail internal representation ...\n";
2400 t.start();
2401 }
2402 // And now walk all the DIEs again to build the libabigail IR.
2403 Dwarf_Half dwarf_vers = 0;
2404 for (Dwarf_Off offset = 0, next_offset = 0;
2405 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2406 offset, &next_offset, &header_size,
2407 &dwarf_vers, NULL, &address_size, NULL,
2408 NULL, NULL) == 0);
2409 offset = next_offset)
2410 {
2411 Dwarf_Off die_offset = offset + header_size;
2412 Dwarf_Die unit;
2413 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2414 die_offset, &unit)
2415 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2416 continue;
2417
2418 dwarf_version(dwarf_vers);
2419
2420 address_size *= 8;
2421
2422 // Build a translation_unit IR node from cu; note that cu must
2423 // be a DW_TAG_compile_unit die.
2424 translation_unit_sptr ir_node =
2425 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2426 ABG_ASSERT(ir_node);
2427 }
2428 if (do_log())
2429 {
2430 t.stop();
2431 cerr << "DWARF Reader: building "
2432 << "the libabigail internal representation "
2433 << "DONE for corpus " << corpus()->get_path()
2434 << " in: "
2435 << t
2436 << "\n";
2437
2438 cerr << "DWARF Reader: Number of aggregate types compared: "
2439 << compare_count_ << "\n"
2440 << "Number of canonical types propagated: "
2441 << canonical_propagated_count_ << "\n"
2442 << "Number of cancelled propagated canonical types:"
2443 << cancelled_propagation_count_ << "\n"
2444 << "Number of suppressed functions: "
2445 << stats_.number_of_suppressed_functions << "\n"
2446 << "Number of allowed functions: "
2447 << stats_.number_of_allowed_functions << "\n"
2448 << "Total number of fns in the corpus: "
2449 << corpus()->get_functions().size() << "\n"
2450 << "Total number of variables in the corpus: "
2451 << corpus()->get_variables().size() << "\n";
2452 }
2453 }
2454
2455 {
2456 tools_utils::timer t;
2457 if (do_log())
2458 {
2459 cerr << "DWARF Reader: resolving declaration only classes ...";
2460 t.start();
2461 }
2462 resolve_declaration_only_classes();
2463 if (do_log())
2464 {
2465 t.stop();
2466 cerr << " DONE@" << corpus()->get_path()
2467 << " in :"
2468 << t
2469 <<"\n";
2470 }
2471 }
2472
2473 {
2474 tools_utils::timer t;
2475 if (do_log())
2476 {
2477 cerr << "resolving declaration only enums ...";
2478 t.start();
2479 }
2480 resolve_declaration_only_enums();
2481 if (do_log())
2482 {
2483 t.stop();
2484 cerr << " DONE@" << corpus()->get_path()
2485 << ":"
2486 << t
2487 <<"\n";
2488 }
2489 }
2490
2491 {
2492 tools_utils::timer t;
2493 if (do_log())
2494 {
2495 cerr << "DWARF Reader: fixing up functions with linkage name but "
2496 << "no advertised underlying symbols ....";
2497 t.start();
2498 }
2499 fixup_functions_with_no_symbols();
2500 if (do_log())
2501 {
2502 t.stop();
2503 cerr << " DONE@" << corpus()->get_path()
2504 <<" in :"
2505 << t
2506 <<"\n";
2507 }
2508 }
2509
2510 merge_member_functions_and_variables_in_classes_of_same_names();
2511
2512 /// Now, look at the types that needs to be canonicalized after the
2513 /// translation has been constructed (which is just now) and
2514 /// canonicalize them.
2515 ///
2516 /// These types need to be constructed at the end of the translation
2517 /// unit reading phase because some types are modified by some DIEs
2518 /// even after the principal DIE describing the type has been read;
2519 /// this happens for clones of virtual destructors (for instance) or
2520 /// even for some static data members. We need to do that for types
2521 /// are in the alternate debug info section and for types that in
2522 /// the main debug info section.
2523 {
2524 tools_utils::timer t;
2525 if (do_log())
2526 {
2527 cerr << "DWARF Reader: perform late type canonicalizing ...\n";
2528 t.start();
2529 }
2530
2531 perform_late_type_canonicalizing();
2532 if (do_log())
2533 {
2534 t.stop();
2535 cerr << "DWARF Reader: late type canonicalizing DONE for "
2536 << corpus()->get_path()
2537 << " in :"
2538 << t
2539 << "\n";
2540 }
2541 }
2542
2543 env().canonicalization_is_done(true);
2544
2545 {
2546 tools_utils::timer t;
2547 if (do_log())
2548 {
2549 cerr << "DWARF Reader: sort functions and variables ...";
2550 t.start();
2551 }
2552 corpus()->sort_functions();
2553 corpus()->sort_variables();
2554 if (do_log())
2555 {
2556 t.stop();
2557 cerr << " DONE@" << corpus()->get_path()
2558 << ":"
2559 << t
2560 <<" \n";
2561 }
2562 }
2563
2564 return corpus();
2565 }
2566
2567 /// Clear the data that is relevant only for the current translation
2568 /// unit being read. The rest of the data is relevant for the
2569 /// entire ABI corpus.
2570 void
2571 clear_per_translation_unit_data()
2572 {
2573 while (!scope_stack().empty())
2574 scope_stack().pop();
2575 var_decls_to_re_add_to_tree().clear();
2576 per_tu_repr_to_fn_type_maps().clear();
2577 }
2578
2579 /// Clear the data that is relevant for the current corpus being
2580 /// read.
2581 void
2582 clear_per_corpus_data()
2583 {
2584 die_qualified_name_maps_.clear();
2585 die_pretty_repr_maps_.clear();
2586 die_pretty_type_repr_maps_.clear();
2587 clear_types_to_canonicalize();
2588 }
2589
2590 /// Getter for the current environment.
2591 ///
2592 /// @return the current environment.
2593 environment&
2594 env()
2595 {return options().env;}
2596
2597 /// Getter for the current environment.
2598 ///
2599 /// @return the current environment.
2600 const environment&
2601 env() const
2602 {return const_cast<reader*>(this)->env();}
2603
2604 /// Getter for the flag that tells us if we are dropping functions
2605 /// and variables that have undefined symbols.
2606 ///
2607 /// @return true iff we are dropping functions and variables that have
2608 /// undefined symbols.
2609 bool
2610 drop_undefined_syms() const
2611 {return options().drop_undefined_syms;}
2612
2613 /// Setter for the flag that tells us if we are dropping functions
2614 /// and variables that have undefined symbols.
2615 ///
2616 /// @param f the new value of the flag.
2617 void
2618 drop_undefined_syms(bool f)
2619 {options().drop_undefined_syms = f;}
2620
2621 /// Getter of the DWARF version.
2622 unsigned short
2623 dwarf_version() const
2624 {return dwarf_version_;}
2625
2626 void
2627 dwarf_version(unsigned short v)
2628 {dwarf_version_ = v;}
2629
2630 /// Return the ELF descriptor used for DWARF access.
2631 ///
2632 /// This can be the same as reader::elf_handle() above, if the
2633 /// DWARF info is in the same ELF file as the one of the binary we
2634 /// are analizing. It is different if e.g, the debug info is split
2635 /// from the ELF file we are analizing.
2636 ///
2637 /// @return a pointer to the ELF descriptor used to access debug
2638 /// info.
2639 Elf*
2640 dwarf_elf_handle() const
2641 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2642
2643 /// Test if the debug information is in a separate ELF file wrt the
2644 /// main ELF file of the program (application or shared library) we
2645 /// are analizing.
2646 ///
2647 /// @return true if the debug information is in a separate ELF file
2648 /// compared to the main ELF file of the program (application or
2649 /// shared library) that we are looking at.
2650 bool
2651 dwarf_is_splitted() const
2652 {return dwarf_elf_handle() != elf_handle();}
2653
2654 /// Return the correct debug info, depending on the DIE source we
2655 /// are looking at.
2656 ///
2657 /// @param source the DIE source to consider.
2658 ///
2659 /// @return the right debug info, depending on @p source.
2660 const Dwarf*
2661 dwarf_per_die_source(die_source source) const
2662 {
2663 const Dwarf *result = 0;
2664 switch(source)
2665 {
2666 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2667 case TYPE_UNIT_DIE_SOURCE:
2668 result = dwarf_debug_info();
2669 break;
2670 case ALT_DEBUG_INFO_DIE_SOURCE:
2671 result = alternate_dwarf_debug_info();
2672 break;
2673 case NO_DEBUG_INFO_DIE_SOURCE:
2674 case NUMBER_OF_DIE_SOURCES:
2676 }
2677 return result;
2678 }
2679
2680 /// Return the path to the ELF path we are reading.
2681 ///
2682 /// @return the elf path.
2683 const string&
2684 elf_path() const
2685 {return corpus_path();}
2686
2687 const Dwarf_Die*
2688 cur_tu_die() const
2689 {return cur_tu_die_;}
2690
2691 void
2692 cur_tu_die(Dwarf_Die* cur_tu_die)
2693 {cur_tu_die_ = cur_tu_die;}
2694
2695 dwarf_expr_eval_context&
2696 dwarf_expr_eval_ctxt() const
2697 {return dwarf_expr_eval_context_;}
2698
2699 /// Getter of the maps set that associates a representation of a
2700 /// decl DIE to a vector of offsets of DIEs having that representation.
2701 ///
2702 /// @return the maps set that associates a representation of a decl
2703 /// DIE to a vector of offsets of DIEs having that representation.
2704 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2705 decl_die_repr_die_offsets_maps() const
2706 {return decl_die_repr_die_offsets_maps_;}
2707
2708 /// Getter of the maps set that associates a representation of a
2709 /// decl DIE to a vector of offsets of DIEs having that representation.
2710 ///
2711 /// @return the maps set that associates a representation of a decl
2712 /// DIE to a vector of offsets of DIEs having that representation.
2713 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2714 decl_die_repr_die_offsets_maps()
2715 {return decl_die_repr_die_offsets_maps_;}
2716
2717 /// Getter of the maps set that associate a representation of a type
2718 /// DIE to a vector of offsets of DIEs having that representation.
2719 ///
2720 /// @return the maps set that associate a representation of a type
2721 /// DIE to a vector of offsets of DIEs having that representation.
2722 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2723 type_die_repr_die_offsets_maps() const
2724 {return type_die_repr_die_offsets_maps_;}
2725
2726 /// Getter of the maps set that associate a representation of a type
2727 /// DIE to a vector of offsets of DIEs having that representation.
2728 ///
2729 /// @return the maps set that associate a representation of a type
2730 /// DIE to a vector of offsets of DIEs having that representation.
2731 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2732 type_die_repr_die_offsets_maps()
2733 {return type_die_repr_die_offsets_maps_;}
2734
2735
2736 /// Compute the offset of the canonical DIE of a given DIE.
2737 ///
2738 /// @param die the DIE to consider.
2739 ///
2740 /// @param canonical_die_offset out parameter. This is set to the
2741 /// resulting canonical DIE that was computed.
2742 ///
2743 /// @param die_as_type if yes, it means @p die has to be considered
2744 /// as a type.
2745 void
2746 compute_canonical_die_offset(const Dwarf_Die *die,
2747 Dwarf_Off &canonical_die_offset,
2748 bool die_as_type) const
2749 {
2750 offset_offset_map_type &canonical_dies =
2751 die_as_type
2752 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2753 get_container(*this, die)
2754 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2755 get_container(*this, die);
2756
2757 Dwarf_Die canonical_die;
2758 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2759
2760 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2761 }
2762
2763 /// Compute (find) the canonical DIE of a given DIE.
2764 ///
2765 /// @param die the DIE to consider.
2766 ///
2767 /// @param canonical_dies the vector in which the canonical dies ar
2768 /// stored. The index of each element is the offset of the DIE we
2769 /// want the canonical DIE for. And the value of the element at
2770 /// that index is the canonical DIE offset we are looking for.
2771 ///
2772 /// @param canonical_die_offset out parameter. This is set to the
2773 /// resulting canonical DIE that was computed.
2774 ///
2775 /// @param die_as_type if yes, it means @p die has to be considered
2776 /// as a type.
2777 void
2778 compute_canonical_die(const Dwarf_Die *die,
2779 offset_offset_map_type& canonical_dies,
2780 Dwarf_Die &canonical_die,
2781 bool die_as_type) const
2782 {
2783 const die_source source = get_die_source(die);
2784
2785 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2786
2787 compute_canonical_die(die_offset, source,
2788 canonical_dies,
2789 canonical_die, die_as_type);
2790 }
2791
2792 /// Compute (find) the canonical DIE of a given DIE.
2793 ///
2794 /// @param die_offset the offset of the DIE to consider.
2795 ///
2796 /// @param source the source of the DIE to consider.
2797 ///
2798 /// @param canonical_dies the vector in which the canonical dies ar
2799 /// stored. The index of each element is the offset of the DIE we
2800 /// want the canonical DIE for. And the value of the element at
2801 /// that index is the canonical DIE offset we are looking for.
2802 ///
2803 /// @param canonical_die_offset out parameter. This is set to the
2804 /// resulting canonical DIE that was computed.
2805 ///
2806 /// @param die_as_type if yes, it means @p die has to be considered
2807 /// as a type.
2808 void
2809 compute_canonical_die(Dwarf_Off die_offset,
2810 die_source source,
2811 offset_offset_map_type& canonical_dies,
2812 Dwarf_Die &canonical_die,
2813 bool die_as_type) const
2814 {
2815 // The map that associates the string representation of 'die'
2816 // with a vector of offsets of potentially equivalent DIEs.
2818 die_as_type
2819 ? (const_cast<reader*>(this)->
2820 type_die_repr_die_offsets_maps().get_container(source))
2821 : (const_cast<reader*>(this)->
2822 decl_die_repr_die_offsets_maps().get_container(source));
2823
2824 Dwarf_Die die;
2825 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2826 die_offset, &die));
2827
2828 // The variable repr is the the string representation of 'die'.
2829 //
2830 // Even if die_as_type is true -- which means that 'die' is said
2831 // to be considered as a type -- we always consider a
2832 // DW_TAG_subprogram DIE as a decl here, as far as its string
2833 // representation is concerned.
2834 interned_string name =
2835 (die_as_type)
2836 ? get_die_pretty_type_representation(&die, /*where=*/0)
2837 : get_die_pretty_representation(&die, /*where=*/0);
2838
2839 Dwarf_Off canonical_die_offset = 0;
2840 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2841 if (i == map.end())
2842 {
2843 dwarf_offsets_type offsets;
2844 offsets.push_back(die_offset);
2845 map[name] = offsets;
2846 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2847 get_die_from_offset(source, die_offset, &canonical_die);
2848 return;
2849 }
2850
2851 Dwarf_Off cur_die_offset;
2852 Dwarf_Die potential_canonical_die;
2853 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2854 o != i->second.end();
2855 ++o)
2856 {
2857 cur_die_offset = *o;
2858 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2859 if (compare_dies(*this, &die, &potential_canonical_die,
2860 /*update_canonical_dies_on_the_fly=*/false))
2861 {
2862 canonical_die_offset = cur_die_offset;
2863 set_canonical_die_offset(canonical_dies, die_offset,
2864 canonical_die_offset);
2865 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2866 return;
2867 }
2868 }
2869
2870 canonical_die_offset = die_offset;
2871 i->second.push_back(die_offset);
2872 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2873 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2874 }
2875
2876 /// Getter of the canonical DIE of a given DIE.
2877 ///
2878 /// @param die the DIE to consider.
2879 ///
2880 /// @param canonical_die output parameter. Is set to the resulting
2881 /// canonical die, if this function returns true.
2882 ///
2883 /// @param where the offset of the logical DIE we are supposed to be
2884 /// calling this function from. If set to zero this means this is
2885 /// to be ignored.
2886 ///
2887 /// @param die_as_type if set to yes, it means @p die is to be
2888 /// considered as a type DIE.
2889 ///
2890 /// @return true iff a canonical DIE was found for @p die.
2891 bool
2892 get_canonical_die(const Dwarf_Die *die,
2893 Dwarf_Die &canonical_die,
2894 size_t where,
2895 bool die_as_type)
2896 {
2897 const die_source source = get_die_source(die);
2898
2899 offset_offset_map_type &canonical_dies =
2900 die_as_type
2901 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2902 get_container(source)
2903 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2904 get_container(source);
2905
2906 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2907 if (Dwarf_Off canonical_die_offset =
2908 get_canonical_die_offset(canonical_dies, die_offset))
2909 {
2910 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2911 return true;
2912 }
2913
2914 // The map that associates the string representation of 'die'
2915 // with a vector of offsets of potentially equivalent DIEs.
2917 die_as_type
2918 ? (const_cast<reader*>(this)->
2919 type_die_repr_die_offsets_maps().get_container(*this, die))
2920 : (const_cast<reader*>(this)->
2921 decl_die_repr_die_offsets_maps().get_container(*this, die));
2922
2923 // The variable repr is the the string representation of 'die'.
2924 //
2925 // Even if die_as_type is true -- which means that 'die' is said
2926 // to be considered as a type -- we always consider a
2927 // DW_TAG_subprogram DIE as a decl here, as far as its string
2928 // representation is concerned.
2929 interned_string name =
2930 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2931 ? get_die_pretty_type_representation(die, where)
2932 : get_die_pretty_representation(die, where);
2933
2934 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2935 if (i == map.end())
2936 return false;
2937
2938 Dwarf_Off cur_die_offset;
2939 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2940 o != i->second.end();
2941 ++o)
2942 {
2943 cur_die_offset = *o;
2944 get_die_from_offset(source, cur_die_offset, &canonical_die);
2945 // compare die and canonical_die.
2946 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2947 die, &canonical_die,
2948 /*update_canonical_dies_on_the_fly=*/true))
2949 {
2950 set_canonical_die_offset(canonical_dies,
2951 die_offset,
2952 cur_die_offset);
2953 return true;
2954 }
2955 }
2956
2957 return false;
2958 }
2959
2960 /// Retrieve the canonical DIE of a given DIE.
2961 ///
2962 /// The canonical DIE is a DIE that is structurally equivalent to
2963 /// this one.
2964 ///
2965 /// Note that this function caches the canonical DIE that was
2966 /// computed. Subsequent invocations of this function on the same
2967 /// DIE return the same cached DIE.
2968 ///
2969 /// @param die the DIE to get a canonical type for.
2970 ///
2971 /// @param canonical_die the resulting canonical DIE.
2972 ///
2973 /// @param where the offset of the logical DIE we are supposed to be
2974 /// calling this function from. If set to zero this means this is
2975 /// to be ignored.
2976 ///
2977 /// @param die_as_type if true, consider DIE is a type.
2978 ///
2979 /// @return true if an *existing* canonical DIE was found.
2980 /// Otherwise, @p die is considered as being a canonical DIE for
2981 /// itself. @p canonical_die is thus set to the canonical die in
2982 /// either cases.
2983 bool
2984 get_or_compute_canonical_die(const Dwarf_Die* die,
2985 Dwarf_Die& canonical_die,
2986 size_t where,
2987 bool die_as_type) const
2988 {
2989 const die_source source = get_die_source(die);
2990
2991 offset_offset_map_type &canonical_dies =
2992 die_as_type
2993 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2994 get_container(source)
2995 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2996 get_container(source);
2997
2998 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2999
3000 if (Dwarf_Off canonical_die_offset =
3001 get_canonical_die_offset(canonical_dies,
3002 initial_die_offset))
3003 {
3004 get_die_from_offset(source, canonical_die_offset, &canonical_die);
3005 return true;
3006 }
3007
3008 if (!is_type_die_to_be_canonicalized(die))
3009 return false;
3010
3011 // The map that associates the string representation of 'die'
3012 // with a vector of offsets of potentially equivalent DIEs.
3014 die_as_type
3015 ? (const_cast<reader*>(this)->
3016 type_die_repr_die_offsets_maps().get_container(*this, die))
3017 : (const_cast<reader*>(this)->
3018 decl_die_repr_die_offsets_maps().get_container(*this, die));
3019
3020 // The variable repr is the the string representation of 'die'.
3021 //
3022 // Even if die_as_type is true -- which means that 'die' is said
3023 // to be considered as a type -- we always consider a
3024 // DW_TAG_subprogram DIE as a decl here, as far as its string
3025 // representation is concerned.
3026 interned_string name =
3027 (die_as_type)
3028 ? get_die_pretty_type_representation(die, where)
3029 : get_die_pretty_representation(die, where);
3030
3031 istring_dwarf_offsets_map_type::iterator i = map.find(name);
3032 if (i == map.end())
3033 {
3034 dwarf_offsets_type offsets;
3035 offsets.push_back(initial_die_offset);
3036 map[name] = offsets;
3037 get_die_from_offset(source, initial_die_offset, &canonical_die);
3038 set_canonical_die_offset(canonical_dies,
3039 initial_die_offset,
3040 initial_die_offset);
3041 return false;
3042 }
3043
3044 // walk i->second without any iterator (using a while loop rather
3045 // than a for loop) because compare_dies might add new content to
3046 // the end of the i->second vector during the walking.
3047 dwarf_offsets_type::size_type n = 0, s = i->second.size();
3048 while (n < s)
3049 {
3050 Dwarf_Off die_offset = i->second[n];
3051 get_die_from_offset(source, die_offset, &canonical_die);
3052 // compare die and canonical_die.
3053 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
3054 die, &canonical_die,
3055 /*update_canonical_dies_on_the_fly=*/true))
3056 {
3057 set_canonical_die_offset(canonical_dies,
3058 initial_die_offset,
3059 die_offset);
3060 return true;
3061 }
3062 ++n;
3063 }
3064
3065 // We didn't find a canonical DIE for 'die'. So let's consider
3066 // that it is its own canonical DIE.
3067 get_die_from_offset(source, initial_die_offset, &canonical_die);
3068 i->second.push_back(initial_die_offset);
3069 set_canonical_die_offset(canonical_dies,
3070 initial_die_offset,
3071 initial_die_offset);
3072
3073 return false;
3074 }
3075
3076 /// Get the source of the DIE.
3077 ///
3078 /// The function returns an enumerator value saying if the DIE comes
3079 /// from the .debug_info section of the primary debug info file, the
3080 /// .debug_info section of the alternate debug info file, or the
3081 /// .debug_types section.
3082 ///
3083 /// @param die the DIE to get the source of.
3084 ///
3085 /// @return the source of the DIE if it could be determined,
3086 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
3088 get_die_source(const Dwarf_Die *die) const
3089 {
3090 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
3091 ABG_ASSERT(die);
3092 ABG_ASSERT(get_die_source(*die, source));
3093 return source;
3094 }
3095
3096 /// Get the source of the DIE.
3097 ///
3098 /// The function returns an enumerator value saying if the DIE comes
3099 /// from the .debug_info section of the primary debug info file, the
3100 /// .debug_info section of the alternate debug info file, or the
3101 /// .debug_types section.
3102 ///
3103 /// @param die the DIE to get the source of.
3104 ///
3105 /// @param source out parameter. The function sets this parameter
3106 /// to the source of the DIE @p iff it returns true.
3107 ///
3108 /// @return true iff the source of the DIE could be determined and
3109 /// returned.
3110 bool
3111 get_die_source(const Dwarf_Die &die, die_source &source) const
3112 {
3113 Dwarf_Die cu_die;
3114 Dwarf_Die cu_kind;
3115 uint8_t address_size = 0, offset_size = 0;
3116 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
3117 &cu_die, &address_size,
3118 &offset_size))
3119 return false;
3120
3121 Dwarf_Half version = 0;
3122 Dwarf_Off abbrev_offset = 0;
3123 uint64_t type_signature = 0;
3124 Dwarf_Off type_offset = 0;
3125 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
3126 &version, &abbrev_offset,
3127 &address_size, &offset_size,
3128 &type_signature, &type_offset))
3129 return false;
3130
3131 int tag = dwarf_tag(&cu_kind);
3132
3133 if (tag == DW_TAG_compile_unit
3134 || tag == DW_TAG_partial_unit)
3135 {
3136 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
3137 if (dwarf_debug_info() == die_dwarf)
3138 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
3139 else if (alternate_dwarf_debug_info() == die_dwarf)
3140 source = ALT_DEBUG_INFO_DIE_SOURCE;
3141 else
3143 }
3144 else if (tag == DW_TAG_type_unit)
3145 source = TYPE_UNIT_DIE_SOURCE;
3146 else
3147 return false;
3148
3149 return true;
3150 }
3151
3152 /// Getter for the DIE designated by an offset.
3153 ///
3154 /// @param source the source of the DIE to get.
3155 ///
3156 /// @param offset the offset of the DIE to get.
3157 ///
3158 /// @param die the resulting DIE. The pointer has to point to an
3159 /// allocated memory region.
3160 void
3161 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
3162 {
3163 if (source == TYPE_UNIT_DIE_SOURCE)
3164 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3165 offset, die));
3166 else
3167 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3168 offset, die));
3169 }
3170
3171public:
3172
3173 /// Add an entry to the relevant die->decl map.
3174 ///
3175 /// @param die the DIE to add the the map.
3176 ///
3177 /// @param decl the decl to consider.
3178 ///
3179 /// @param where_offset where in the DIE stream we logically are.
3180 ///
3181 /// @param do_associate_by_repr if true then this function
3182 /// associates the representation string of @p die with the
3183 /// declaration @p decl, in a corpus-wide manner. That is, in the
3184 /// entire current corpus, there is going to be just one declaration
3185 /// associated with a DIE of the string representation of @p die.
3186 ///
3187 /// @param do_associate_by_repr_per_tu if true, then this function
3188 /// associates the representation string of @p die with the
3189 /// declaration @p decl in a translation unit wide manner. That is,
3190 /// in the entire current translation unit, there is going to be
3191 /// just one declaration associated with a DIE of the string
3192 /// representation of @p die.
3193 void
3194 associate_die_to_decl(Dwarf_Die* die,
3195 decl_base_sptr decl,
3196 size_t where_offset,
3197 bool do_associate_by_repr = false)
3198 {
3199 const die_source source = get_die_source(die);
3200
3202 decl_die_artefact_maps().get_container(source);
3203
3204 size_t die_offset;
3205 if (do_associate_by_repr)
3206 {
3207 Dwarf_Die equiv_die;
3208 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
3209 /*die_as_type=*/false))
3210 return;
3211 die_offset = dwarf_dieoffset(&equiv_die);
3212 }
3213 else
3214 die_offset = dwarf_dieoffset(die);
3215
3216 m[die_offset] = decl;
3217 }
3218
3219 /// Lookup the decl for a given DIE.
3220 ///
3221 /// The returned decl is either the decl of the DIE that as the
3222 /// exact offset @p die_offset
3223 /// die_offset, or
3224 /// give
3225 ///
3226 /// @param die_offset the offset of the DIE to consider.
3227 ///
3228 /// @param source where the DIE represented by @p die_offset comes
3229 /// from.
3230 ///
3231 /// Note that "alternate debug info sections" is a GNU extension as
3232 /// of DWARF4 and is described at
3233 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3234 ///
3235 /// @return the resulting decl, or null if no decl is associated to
3236 /// the DIE represented by @p die_offset.
3237 decl_base_sptr
3238 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3239 {
3240 decl_base_sptr result =
3241 is_decl(lookup_artifact_from_die_offset(die_offset, source,
3242 /*die_as_type=*/false));
3243
3244 return result;
3245 }
3246
3247 /// Get the qualified name of a given DIE.
3248 ///
3249 /// If the name of the DIE was already computed before just return
3250 /// that name from a cache. Otherwise, build the name, cache it and
3251 /// return it.
3252 ///
3253 /// @param die the DIE to consider.
3254 ///
3255 /// @param where_offset where in the DIE stream we logically are.
3256 ///
3257 /// @param guard the set of DIE offsets of the stack of DIEs
3258 /// involved in the construction of the qualified name of the type.
3259 /// This set is used to detect (and avoid) cycles in the stack of
3260 /// DIEs that is going to be walked to compute the qualified type
3261 /// name.
3262 ///
3263 /// @return the interned string representing the qualified name of
3264 /// @p die.
3265 interned_string
3266 get_die_qualified_name(Dwarf_Die *die, size_t where_offset,
3267 unordered_set<uint64_t>& guard) const
3268 {
3269 ABG_ASSERT(die);
3271 die_qualified_name_maps_.get_container(*this, die);
3272
3273 size_t die_offset = dwarf_dieoffset(die);
3274 die_istring_map_type::const_iterator i = map.find(die_offset);
3275
3276 if (i == map.end())
3277 {
3278 reader& rdr = *const_cast<reader*>(this);
3279 string qualified_name = die_qualified_name(rdr, die,
3280 where_offset,
3281 guard);
3282 interned_string istr = env().intern(qualified_name);
3283 map[die_offset] = istr;
3284 return istr;
3285 }
3286
3287 return i->second;
3288 }
3289
3290 /// Get the qualified name of a given DIE which is considered to be
3291 /// the DIE for a type.
3292 ///
3293 /// For instance, for a DW_TAG_subprogram DIE, this function
3294 /// computes the name of the function *type* that corresponds to the
3295 /// function.
3296 ///
3297 /// If the name of the DIE was already computed before just return
3298 /// that name from a cache. Otherwise, build the name, cache it and
3299 /// return it.
3300 ///
3301 /// @param die the DIE to consider.
3302 ///
3303 /// @param where_offset where in the DIE stream we logically are.
3304 ///
3305 /// @param guard the set of DIE offsets of the stack of DIEs
3306 /// involved in the construction of the qualified name of the type.
3307 /// This set is used to detect (and avoid) cycles in the stack of
3308 /// DIEs that is going to be walked to compute the qualified type
3309 /// name.
3310 ///
3311 /// @return the interned string representing the qualified name of
3312 /// @p die.
3313 interned_string
3314 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset,
3315 unordered_set<uint64_t>& guard) const
3316 {
3317 ABG_ASSERT(die);
3318
3319 // The name of the translation unit die is "".
3320 if (die == cur_tu_die())
3321 return env().intern("");
3322
3324 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3325 die);
3326
3327 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3328 die_istring_map_type::const_iterator i =
3329 map.find(die_offset);
3330
3331 if (i == map.end())
3332 {
3333 reader& rdr = *const_cast<reader*>(this);
3334 string qualified_name;
3335 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3336 if ((tag == DW_TAG_structure_type
3337 || tag == DW_TAG_class_type
3338 || tag == DW_TAG_union_type)
3339 && die_is_anonymous(die))
3340 qualified_name =
3341 die_class_or_enum_flat_representation(*this, die, /*indent=*/"",
3342 /*one_line=*/true,
3343 /*qualified_name=*/false,
3344 where_offset,
3345 guard);
3346 else
3347 qualified_name = die_qualified_type_name(rdr, die,
3348 where_offset,
3349 guard);
3350
3351 interned_string istr = env().intern(qualified_name);
3352 map[die_offset] = istr;
3353 return istr;
3354 }
3355
3356 return i->second;
3357 }
3358
3359 /// Get the pretty representation of a DIE that represents a type.
3360 ///
3361 /// For instance, for the DW_TAG_subprogram, this function computes
3362 /// the pretty representation of the type of the function, not the
3363 /// pretty representation of the function declaration.
3364 ///
3365 /// Once the pretty representation is computed, it's stored in a
3366 /// cache. Subsequent invocations of this function on the same DIE
3367 /// will yield the cached name.
3368 ///
3369 /// @param die the DIE to consider.
3370 ///
3371 /// @param where_offset where in the DIE stream we logically are.
3372 ///
3373 /// @param guard the set of DIE offsets of the stack of DIEs
3374 /// involved in the construction of the pretty representation of the
3375 /// type. This set is used to detect (and avoid) cycles in the
3376 /// stack of DIEs that is going to be walked to compute the
3377 /// pretty representation.
3378 ///
3379 /// @return the interned_string that represents the pretty
3380 /// representation.
3381 interned_string
3382 get_die_pretty_type_representation(const Dwarf_Die *die,
3383 size_t where_offset,
3384 unordered_set<uint64_t>& guard) const
3385 {
3386 ABG_ASSERT(die);
3388 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3389 die);
3390
3391 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3392 die_istring_map_type::const_iterator i = map.find(die_offset);
3393
3394 if (i == map.end())
3395 {
3396 reader& rdr = *const_cast<reader*>(this);
3397 string pretty_representation =
3398 die_pretty_print_type(rdr, die, where_offset, guard);
3399 interned_string istr = env().intern(pretty_representation);
3400 map[die_offset] = istr;
3401 return istr;
3402 }
3403
3404 return i->second;
3405 }
3406
3407
3408 /// Get the pretty representation of a DIE that represents a type.
3409 ///
3410 /// For instance, for the DW_TAG_subprogram, this function computes
3411 /// the pretty representation of the type of the function, not the
3412 /// pretty representation of the function declaration.
3413 ///
3414 /// Once the pretty representation is computed, it's stored in a
3415 /// cache. Subsequent invocations of this function on the same DIE
3416 /// will yield the cached name.
3417 ///
3418 /// @param die the DIE to consider.
3419 ///
3420 /// @param where_offset where in the DIE stream we logically are.
3421 ///
3422 /// @return the interned_string that represents the pretty
3423 /// representation.
3424 interned_string
3425 get_die_pretty_type_representation(const Dwarf_Die *die,
3426 size_t where_offset) const
3427 {
3428 unordered_set<uint64_t> guard;
3429 return get_die_pretty_type_representation(die, where_offset, guard);
3430 }
3431
3432 /// Get the pretty representation of a DIE.
3433 ///
3434 /// Once the pretty representation is computed, it's stored in a
3435 /// cache. Subsequent invocations of this function on the same DIE
3436 /// will yield the cached name.
3437 ///
3438 /// @param die the DIE to consider.
3439 ///
3440 /// @param where_offset where in the DIE stream we logically are.
3441 ///
3442 /// @param guard the set of DIE offsets of the stack of DIEs
3443 /// involved in the construction of the pretty representation of the
3444 /// type. This set is used to detect (and avoid) cycles in the
3445 /// stack of DIEs that is going to be walked to compute the
3446 /// pretty representation.
3447 ///
3448 /// @return the interned_string that represents the pretty
3449 /// representation.
3450 interned_string
3451 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset,
3452 unordered_set<uint64_t>& guard) const
3453 {
3454 ABG_ASSERT(die);
3455
3457 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3458 die);
3459
3460 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3461 die_istring_map_type::const_iterator i = map.find(die_offset);
3462
3463 if (i == map.end())
3464 {
3465 reader& rdr = *const_cast<reader*>(this);
3466 string pretty_representation =
3467 die_pretty_print(rdr, die, where_offset, guard);
3468 interned_string istr = env().intern(pretty_representation);
3469 map[die_offset] = istr;
3470 return istr;
3471 }
3472
3473 return i->second;
3474 }
3475
3476 /// Get the pretty representation of a DIE.
3477 ///
3478 /// Once the pretty representation is computed, it's stored in a
3479 /// cache. Subsequent invocations of this function on the same DIE
3480 /// will yield the cached name.
3481 ///
3482 /// @param die the DIE to consider.
3483 ///
3484 /// @param where_offset where in the DIE stream we logically are.
3485 ///
3486 /// @return the interned_string that represents the pretty
3487 /// representation.
3488 interned_string
3489 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3490 {
3491 unordered_set<uint64_t> guard;
3492 return get_die_pretty_representation(die, where_offset, guard);
3493 }
3494
3495 /// Lookup the artifact that was built to represent a type that has
3496 /// the same pretty representation as the type denoted by a given
3497 /// DIE.
3498 ///
3499 /// Note that the DIE must have previously been associated with the
3500 /// artifact using the functions associate_die_to_decl or
3501 /// associate_die_to_type.
3502 ///
3503 /// Also, note that the scope of the lookup is the current ABI
3504 /// corpus.
3505 ///
3506 /// @param die the DIE to consider.
3507 ///
3508 /// @param where_offset where in the DIE stream we logically are.
3509 ///
3510 /// @return the type artifact found.
3512 lookup_type_artifact_from_die(Dwarf_Die *die) const
3513 {
3514 type_or_decl_base_sptr artifact =
3515 lookup_artifact_from_die(die, /*type_as_die=*/true);
3516 if (function_decl_sptr fn = is_function_decl(artifact))
3517 return fn->get_type();
3518 return artifact;
3519 }
3520
3521 /// Lookup the artifact that was built to represent a type or a
3522 /// declaration that has the same pretty representation as the type
3523 /// denoted by a given DIE.
3524 ///
3525 /// Note that the DIE must have previously been associated with the
3526 /// artifact using the functions associate_die_to_decl or
3527 /// associate_die_to_type.
3528 ///
3529 /// Also, note that the scope of the lookup is the current ABI
3530 /// corpus.
3531 ///
3532 /// @param die the DIE to consider.
3533 ///
3534 /// @param where_offset where in the DIE stream we logically are.
3535 ///
3536 /// @param die_as_type if true, it means the DIE is to be considered
3537 /// as a type.
3538 ///
3539 /// @return the artifact found.
3541 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3542 {
3543 Dwarf_Die equiv_die;
3544 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3545 return type_or_decl_base_sptr();
3546
3547 const die_artefact_map_type& m =
3548 die_as_type
3549 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3550 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3551
3552 size_t die_offset = dwarf_dieoffset(&equiv_die);
3553 die_artefact_map_type::const_iterator i = m.find(die_offset);
3554
3555 if (i == m.end())
3556 return type_or_decl_base_sptr();
3557 return i->second;
3558 }
3559
3560 /// Lookup the artifact that was built to represent a type or a
3561 /// declaration that has the same pretty representation as the type
3562 /// denoted by the offset of a given DIE.
3563 ///
3564 /// Note that the DIE must have previously been associated with the
3565 /// artifact using either associate_die_to_decl or
3566 /// associate_die_to_type.
3567 ///
3568 /// Also, note that the scope of the lookup is the current ABI
3569 /// corpus.
3570 ///
3571 /// @param die the DIE to consider.
3572 ///
3573 /// @param where_offset where in the DIE stream we logically are.
3574 ///
3575 /// @param die_as_type if true, it means the DIE is to be considered
3576 /// as a type.
3577 ///
3578 /// @return the artifact found.
3580 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3581 die_source source,
3582 bool die_as_type = false) const
3583 {
3584 const die_artefact_map_type& m =
3585 die_as_type
3586 ? type_die_artefact_maps().get_container(source)
3587 : decl_die_artefact_maps().get_container(source);
3588
3589 die_artefact_map_type::const_iterator i = m.find(die_offset);
3590 if (i == m.end())
3591 return type_or_decl_base_sptr();
3592 return i->second;
3593 }
3594
3595 /// Check if we can assume the One Definition Rule[1] to be relevant
3596 /// for the current translation unit.
3597 ///
3598 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3599 ///
3600 /// At the moment this returns true if the current translation unit
3601 /// is in C++ language. In that case, it's relevant to assume that
3602 /// we use optimizations based on the ODR.
3603 bool
3604 odr_is_relevant() const
3605 {return odr_is_relevant(cur_transl_unit()->get_language());}
3606
3607 /// Check if we can assume the One Definition Rule[1] to be relevant
3608 /// for a given language.
3609 ///
3610 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3611 ///
3612 /// At the moment this returns true if the language considered
3613 /// is C++, Java or Ada.
3614 bool
3616 {
3617 return (is_cplus_plus_language(l)
3618 || is_java_language(l)
3619 || is_ada_language(l));
3620 }
3621
3622 /// Check if we can assume the One Definition Rule to be relevant
3623 /// for a given DIE.
3624 ///
3625 /// @param die the DIE to consider.
3626 ///
3627 /// @return true if the ODR is relevant for @p die.
3628 bool
3629 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3630 {
3631 Dwarf_Die die;
3632 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3633 die_offset, &die));
3634 return odr_is_relevant(&die);
3635 }
3636
3637 /// Check if we can assume the One Definition Rule to be relevant
3638 /// for a given DIE.
3639 ///
3640 /// @param die the DIE to consider.
3641 ///
3642 /// @return true if the ODR is relevant for @p die.
3643 bool
3644 odr_is_relevant(const Dwarf_Die *die) const
3645 {
3647 if (!get_die_language(die, lang))
3648 return odr_is_relevant();
3649
3650 return odr_is_relevant(lang);
3651 }
3652
3653 /// Getter for the maps set that associates a decl DIE offset to an
3654 /// artifact.
3655 ///
3656 /// @return the maps set that associates a decl DIE offset to an
3657 /// artifact.
3658 die_source_dependant_container_set<die_artefact_map_type>&
3659 decl_die_artefact_maps()
3660 {return decl_die_artefact_maps_;}
3661
3662 /// Getter for the maps set that associates a decl DIE offset to an
3663 /// artifact.
3664 ///
3665 /// @return the maps set that associates a decl DIE offset to an
3666 /// artifact.
3667 const die_source_dependant_container_set<die_artefact_map_type>&
3668 decl_die_artefact_maps() const
3669 {return decl_die_artefact_maps_;}
3670
3671 /// Getter for the maps set that associates a type DIE offset to an
3672 /// artifact.
3673 ///
3674 /// @return the maps set that associates a type DIE offset to an
3675 /// artifact.
3676 die_source_dependant_container_set<die_artefact_map_type>&
3677 type_die_artefact_maps()
3678 {return type_die_artefact_maps_;}
3679
3680 /// Getter for the maps set that associates a type DIE offset to an
3681 /// artifact.
3682 ///
3683 /// @return the maps set that associates a type DIE offset to an
3684 /// artifact.
3685 const die_source_dependant_container_set<die_artefact_map_type>&
3686 type_die_artefact_maps() const
3687 {return type_die_artefact_maps_;}
3688
3689 /// Getter of the maps that associates function type representations
3690 /// to function types, inside a translation unit.
3691 ///
3692 /// @return the maps that associates function type representations
3693 /// to function types, inside a translation unit.
3695 per_tu_repr_to_fn_type_maps()
3696 {return per_tu_repr_to_fn_type_maps_;}
3697
3698 /// Getter of the maps that associates function type representations
3699 /// to function types, inside a translation unit.
3700 ///
3701 /// @return the maps that associates function type representations
3702 /// to function types, inside a translation unit.
3704 per_tu_repr_to_fn_type_maps() const
3705 {return per_tu_repr_to_fn_type_maps_;}
3706
3707 /// Associate the representation of a function type DIE to a given
3708 /// function type, inside the current translation unit.
3709 ///
3710 /// @param die the DIE to associate to the function type, using its
3711 /// representation.
3712 ///
3713 /// @param fn_type the function type to associate to @p die.
3714 void
3715 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3716 const function_type_sptr &fn_type)
3717 {
3718 if (!die_is_function_type(die))
3719 return;
3720
3721 interned_string repr =
3722 get_die_pretty_type_representation(die, /*where=*/0);
3723 ABG_ASSERT(!repr.empty());
3724
3725 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3726 }
3727
3728 /// Lookup the function type associated to a given function type
3729 /// DIE, in the current translation unit.
3730 ///
3731 /// @param die the DIE of function type to consider.
3732 ///
3733 /// @return the @ref function_type_sptr associated to @p die, or nil
3734 /// of no function_type is associated to @p die.
3736 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3737 {
3738 if (!die_is_function_type(die))
3739 return function_type_sptr();
3740
3741 interned_string repr = die_name(die).empty() ?
3742 get_die_pretty_type_representation(die, /*where=*/0)
3743 : get_die_pretty_representation(die, /*where=*/0);
3744 ABG_ASSERT(!repr.empty());
3745
3746 istring_fn_type_map_type::const_iterator i =
3747 per_tu_repr_to_fn_type_maps().find(repr);
3748
3749 if (i == per_tu_repr_to_fn_type_maps().end())
3750 return function_type_sptr();
3751
3752 return i->second;
3753 }
3754
3755 /// Set the canonical DIE offset of a given DIE.
3756 ///
3757 /// @param canonical_dies the vector that holds canonical DIEs.
3758 ///
3759 /// @param die_offset the offset of the DIE to set the canonical DIE
3760 /// for.
3761 ///
3762 /// @param canonical_die_offset the canonical DIE offset to
3763 /// associate to @p die_offset.
3764 void
3765 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3766 Dwarf_Off die_offset,
3767 Dwarf_Off canonical_die_offset) const
3768 {
3769 canonical_dies[die_offset] = canonical_die_offset;}
3770
3771 /// Set the canonical DIE offset of a given DIE.
3772 ///
3773 ///
3774 /// @param die_offset the offset of the DIE to set the canonical DIE
3775 /// for.
3776 ///
3777 /// @param source the source of the DIE denoted by @p die_offset.
3778 ///
3779 /// @param canonical_die_offset the canonical DIE offset to
3780 /// associate to @p die_offset.
3781 ///
3782 /// @param die_as_type if true, it means that @p die_offset has to
3783 /// be considered as a type.
3784 void
3785 set_canonical_die_offset(Dwarf_Off die_offset,
3786 die_source source,
3787 Dwarf_Off canonical_die_offset,
3788 bool die_as_type) const
3789 {
3790 offset_offset_map_type &canonical_dies =
3791 die_as_type
3792 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3793 get_container(source)
3794 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3795 get_container(source);
3796
3797 set_canonical_die_offset(canonical_dies,
3798 die_offset,
3799 canonical_die_offset);
3800 }
3801
3802 /// Set the canonical DIE offset of a given DIE.
3803 ///
3804 ///
3805 /// @param die the DIE to set the canonical DIE for.
3806 ///
3807 /// @param canonical_die_offset the canonical DIE offset to
3808 /// associate to @p die_offset.
3809 ///
3810 /// @param die_as_type if true, it means that @p die has to be
3811 /// considered as a type.
3812 void
3813 set_canonical_die_offset(const Dwarf_Die *die,
3814 Dwarf_Off canonical_die_offset,
3815 bool die_as_type) const
3816 {
3817 const die_source source = get_die_source(die);
3818
3819 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3820
3821 set_canonical_die_offset(die_offset, source,
3822 canonical_die_offset,
3823 die_as_type);
3824 }
3825
3826 /// Get the canonical DIE offset of a given DIE.
3827 ///
3828 /// @param canonical_dies the vector that contains canonical DIES.
3829 ///
3830 /// @param die_offset the offset of the DIE to consider.
3831 ///
3832 /// @return the canonical of the DIE denoted by @p die_offset, or
3833 /// zero if no canonical DIE was found.
3834 Dwarf_Off
3835 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3836 Dwarf_Off die_offset) const
3837 {
3838 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3839 if (it == canonical_dies.end())
3840 return 0;
3841 return it->second;
3842 }
3843
3844 /// Get the canonical DIE offset of a given DIE.
3845 ///
3846 /// @param die_offset the offset of the DIE to consider.
3847 ///
3848 /// @param source the source of the DIE denoted by @p die_offset.
3849 ///
3850 /// @param die_as_type if true, it means that @p is to be considered
3851 /// as a type DIE.
3852 ///
3853 /// @return the canonical of the DIE denoted by @p die_offset, or
3854 /// zero if no canonical DIE was found.
3855 Dwarf_Off
3856 get_canonical_die_offset(Dwarf_Off die_offset,
3857 die_source source,
3858 bool die_as_type) const
3859 {
3860 offset_offset_map_type &canonical_dies =
3861 die_as_type
3862 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3863 get_container(source)
3864 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3865 get_container(source);
3866
3867 return get_canonical_die_offset(canonical_dies, die_offset);
3868 }
3869
3870 /// Erase the canonical type of a given DIE.
3871 ///
3872 /// @param die_offset the offset of the DIE to consider.
3873 ///
3874 /// @param source the source of the canonical type.
3875 ///
3876 /// @param die_as_type if true, it means that @p is to be considered
3877 /// as a type DIE.
3878 ///
3879 /// @return the canonical of the DIE denoted by @p die_offset, or
3880 /// zero if no canonical DIE was found and erased..
3881 bool
3882 erase_canonical_die_offset(Dwarf_Off die_offset,
3883 die_source source,
3884 bool die_as_type) const
3885 {
3886 offset_offset_map_type &canonical_dies =
3887 die_as_type
3888 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3889 get_container(source)
3890 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3891 get_container(source);
3892
3893 return canonical_dies.erase(die_offset);
3894 }
3895
3896
3897 /// Associate a DIE (representing a type) to the type that it
3898 /// represents.
3899 ///
3900 /// @param die the DIE to consider.
3901 ///
3902 /// @param type the type to associate the DIE to.
3903 ///
3904 /// @param where_offset where in the DIE stream we logically are.
3905 void
3906 associate_die_to_type(const Dwarf_Die *die,
3907 type_base_sptr type,
3908 size_t where)
3909 {
3910 if (!type)
3911 return;
3912
3913 Dwarf_Die equiv_die;
3914 if (!get_or_compute_canonical_die(die, equiv_die, where,
3915 /*die_as_type=*/true))
3916 return;
3917
3919 type_die_artefact_maps().get_container(*this, &equiv_die);
3920
3921 size_t die_offset = dwarf_dieoffset(&equiv_die);
3922 m[die_offset] = type;
3923 }
3924
3925 /// Lookup the type associated to a given DIE.
3926 ///
3927 /// Note that the DIE must have been associated to type by a
3928 /// previous invocation of the function
3929 /// reader::associate_die_to_type().
3930 ///
3931 /// @param die the DIE to consider.
3932 ///
3933 /// @return the type associated to the DIE or NULL if no type is
3934 /// associated to the DIE.
3935 type_base_sptr
3936 lookup_type_from_die(const Dwarf_Die* die) const
3937 {
3938 type_or_decl_base_sptr artifact =
3939 lookup_artifact_from_die(die, /*die_as_type=*/true);
3940 if (function_decl_sptr fn = is_function_decl(artifact))
3941 return fn->get_type();
3942 return is_type(artifact);
3943 }
3944
3945 /// Lookup the type associated to a DIE at a given offset, from a
3946 /// given source.
3947 ///
3948 /// Note that the DIE must have been associated to type by a
3949 /// previous invocation of the function
3950 /// reader::associate_die_to_type().
3951 ///
3952 /// @param die_offset the offset of the DIE to consider.
3953 ///
3954 /// @param source the source of the DIE to consider.
3955 ///
3956 /// @return the type associated to the DIE or NULL if no type is
3957 /// associated to the DIE.
3958 type_base_sptr
3959 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3960 {
3961 type_base_sptr result;
3962 const die_artefact_map_type& m =
3963 type_die_artefact_maps().get_container(source);
3964 die_artefact_map_type::const_iterator i = m.find(die_offset);
3965 if (i != m.end())
3966 {
3967 if (function_decl_sptr fn = is_function_decl(i->second))
3968 return fn->get_type();
3969 result = is_type(i->second);
3970 }
3971
3972 if (!result)
3973 {
3974 // Maybe we are looking for a class type being constructed?
3975 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3976 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3977
3978 if (i != m.end())
3979 result = i->second;
3980 }
3981
3982 if (!result)
3983 {
3984 // Maybe we are looking for a function type being constructed?
3986 die_wip_function_types_map(source);
3987 die_function_type_map_type::const_iterator i = m.find(die_offset);
3988
3989 if (i != m.end())
3990 result = i->second;
3991 }
3992
3993 return result;
3994 }
3995
3996 /// Getter of a map that associates a die that represents a
3997 /// class/struct with the declaration of the class, while the class
3998 /// is being constructed.
3999 ///
4000 /// @param source where the DIE is from.
4001 ///
4002 /// @return the map that associates a DIE to the class that is being
4003 /// built.
4005 die_wip_classes_map(die_source source) const
4006 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
4007
4008 /// Getter of a map that associates a die that represents a
4009 /// class/struct with the declaration of the class, while the class
4010 /// is being constructed.
4011 ///
4012 /// @param source where the DIE comes from.
4013 ///
4014 /// @return the map that associates a DIE to the class that is being
4015 /// built.
4017 die_wip_classes_map(die_source source)
4018 {
4019 switch (source)
4020 {
4021 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4022 break;
4023 case ALT_DEBUG_INFO_DIE_SOURCE:
4024 return alternate_die_wip_classes_map_;
4025 case TYPE_UNIT_DIE_SOURCE:
4026 return type_unit_die_wip_classes_map_;
4027 case NO_DEBUG_INFO_DIE_SOURCE:
4028 case NUMBER_OF_DIE_SOURCES:
4030 }
4031 return die_wip_classes_map_;
4032 }
4033
4034 /// Getter for a map that associates a die (that represents a
4035 /// function type) whith a function type, while the function type is
4036 /// being constructed (WIP == work in progress).
4037 ///
4038 /// @param source where the DIE comes from.n
4039 ///
4040 /// @return the map of wip function types.
4042 die_wip_function_types_map(die_source source) const
4043 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
4044
4045 /// Getter for a map that associates a die (that represents a
4046 /// function type) whith a function type, while the function type is
4047 /// being constructed (WIP == work in progress).
4048 ///
4049 /// @param source where DIEs of the map come from.
4050 ///
4051 /// @return the map of wip function types.
4053 die_wip_function_types_map(die_source source)
4054 {
4055 switch (source)
4056 {
4057 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4058 break;
4059 case ALT_DEBUG_INFO_DIE_SOURCE:
4060 return alternate_die_wip_function_types_map_;
4061 case TYPE_UNIT_DIE_SOURCE:
4062 return type_unit_die_wip_function_types_map_;
4063 case NO_DEBUG_INFO_DIE_SOURCE:
4064 case NUMBER_OF_DIE_SOURCES:
4066 }
4067 return die_wip_function_types_map_;
4068 }
4069
4070 /// Getter for a map that associates a die with a function decl
4071 /// which has a linkage name but no elf symbol yet.
4072 ///
4073 /// This is to fixup function decls with linkage names, but with no
4074 /// link to their underlying elf symbol. There are some DIEs like
4075 /// that in DWARF sometimes, especially when the compiler optimizes
4076 /// stuff aggressively.
4078 die_function_decl_with_no_symbol_map()
4079 {return die_function_with_no_symbol_map_;}
4080
4081 /// Return true iff a given offset is for the DIE of a class that is
4082 /// being built, but that is not fully built yet. WIP == "work in
4083 /// progress".
4084 ///
4085 /// @param offset the DIE offset to consider.
4086 ///
4087 /// @param source where the DIE of the map come from.
4088 ///
4089 /// @return true iff @p offset is the offset of the DIE of a class
4090 /// that is being currently built.
4091 bool
4092 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
4093 {
4094 die_class_or_union_map_type::const_iterator i =
4095 die_wip_classes_map(source).find(offset);
4096 return (i != die_wip_classes_map(source).end());
4097 }
4098
4099 /// Return true iff a given offset is for the DIE of a function type
4100 /// that is being built at the moment, but is not fully built yet.
4101 /// WIP == work in progress.
4102 ///
4103 /// @param offset DIE offset to consider.
4104 ///
4105 /// @param source where the DIE comes from.
4106 ///
4107 /// @return true iff @p offset is the offset of the DIE of a
4108 /// function type that is being currently built.
4109 bool
4110 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
4111 {
4112 die_function_type_map_type::const_iterator i =
4113 die_wip_function_types_map(source).find(offset);
4114 return (i != die_wip_function_types_map(source).end());
4115 }
4116
4117 /// Sometimes, a data member die can erroneously have an empty name as
4118 /// a result of a bug of the DWARF emitter.
4119 ///
4120 /// This is what happens in
4121 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
4122 ///
4123 /// In that case, this function constructs an artificial name for that
4124 /// data member. The pattern of the name is as follows:
4125 ///
4126 /// "unnamed-@-<location>".
4127 ///
4128 ///location is either the value of the data member location of the
4129 ///data member if it has one or concatenation of its source location
4130 ///if it has none. If no location can be calculated then the function
4131 ///returns the empty string.
4132 string
4133 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
4134 {
4135 string result;
4136 // Let's make sure we are looking at a data member with an empty
4137 // name ...
4138 if (!die
4139 || dwarf_tag(die) != DW_TAG_member
4140 || !die_name(die).empty())
4141 return result;
4142
4143 // ... and yet, it's not an anonymous data member (aka unnamed
4144 // field) as described in
4145 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
4146 if (die_is_anonymous_data_member(die))
4147 return result;
4148
4149 // If we come this far, it means we are looking at a buggy data
4150 // member with no name. Let's build a name for it so that it can be
4151 // addressed.
4152 int64_t offset_in_bits = 0;
4153 bool has_offset = die_member_offset(*this, die, offset_in_bits);
4154 location loc;
4155 if (!has_offset)
4156 {
4157 loc = die_location(*this, die);
4158 if (!loc)
4159 return result;
4160 }
4161
4162 std::ostringstream o;
4163 o << "unnamed-dm-@-";
4164 if (has_offset)
4165 o << "offset-" << offset_in_bits << "bits";
4166 else
4167 o << "loc-" << loc.expand();
4168
4169 return o.str();
4170 }
4171
4172 /// Getter for the map of declaration-only classes that are to be
4173 /// resolved to their definition classes by the end of the corpus
4174 /// loading.
4175 ///
4176 /// @return a map of string -> vector of classes where the key is
4177 /// the fully qualified name of the class and the value is the
4178 /// vector of declaration-only class.
4180 declaration_only_classes() const
4181 {return decl_only_classes_map_;}
4182
4183 /// Getter for the map of declaration-only classes that are to be
4184 /// resolved to their definition classes by the end of the corpus
4185 /// loading.
4186 ///
4187 /// @return a map of string -> vector of classes where the key is
4188 /// the fully qualified name of the class and the value is the
4189 /// vector of declaration-only class.
4191 declaration_only_classes()
4192 {return decl_only_classes_map_;}
4193
4194 /// If a given artifact is a class, union or enum that is
4195 /// declaration-only, then stash it on the side so that at the end
4196 /// of the construction of the IR for the ABI corpus, we can resolve
4197 /// that declaration to its definition.
4198 ///
4199 /// @parameter t the ABI artifact to consider.
4200 void
4201 maybe_schedule_decl_only_type_for_resolution(const type_or_decl_base_sptr& t)
4202 {
4203 if (class_or_union_sptr cou = is_class_or_union_type(t))
4204 maybe_schedule_declaration_only_class_for_resolution(cou);
4205 else if (enum_type_decl_sptr e = is_enum_type(t))
4206 maybe_schedule_declaration_only_enum_for_resolution(e);
4207 }
4208
4209 /// If a given class is a declaration-only class then stash it on
4210 /// the side so that at the end of the corpus reading we can resolve
4211 /// it to its definition.
4212 ///
4213 /// @param klass the class to consider.
4214 void
4215 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
4216 {
4217 if (cou->get_is_declaration_only()
4218 && cou->get_definition_of_declaration() == 0
4219 // Make sure the class is not anonymous. Anonymous classes
4220 // are usually later named by a typedef. At that time, after
4221 // being named by a typedef, this method is going to be called
4222 // with the class being named by the typedef.
4223 && !cou->get_qualified_name().empty())
4224 {
4225 string qn = cou->get_qualified_name();
4226 string_classes_or_unions_map::iterator record =
4227 declaration_only_classes().find(qn);
4228 if (record == declaration_only_classes().end())
4229 declaration_only_classes()[qn].push_back(cou);
4230 else
4231 record->second.push_back(cou);
4232 }
4233 }
4234
4235 /// Test if a given declaration-only class has been scheduled for
4236 /// resolution to a defined class.
4237 ///
4238 /// @param klass the class to consider for the test.
4239 ///
4240 /// @return true iff @p klass is a declaration-only class and if
4241 /// it's been scheduled for resolution to a defined class.
4242 bool
4243 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4244 {
4245 if (cou->get_is_declaration_only())
4246 return ((declaration_only_classes().find(cou->get_qualified_name())
4247 != declaration_only_classes().end())
4248 || (declaration_only_classes().find(cou->get_name())
4249 != declaration_only_classes().end()));
4250
4251 return false;
4252 }
4253
4254 /// Compare two ABI artifacts in a context which canonicalization
4255 /// has not be done yet.
4256 ///
4257 /// Please note that this should only be called on IR nodes that
4258 /// belong to the same binary.
4259 ///
4260 /// @param l the left-hand-side operand of the comparison
4261 ///
4262 /// @param r the right-hand-side operand of the comparison.
4263 ///
4264 /// @return true if @p l equals @p r.
4265 bool
4266 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4267 const type_or_decl_base_sptr &r)
4268 {
4269 if (!l || !r)
4270 return !!l == !!r;
4271
4272 const environment& e = l->get_environment();
4273 ABG_ASSERT(!e.canonicalization_is_done());
4274
4275 if (is_decl(l) && is_decl(r)
4276 && l->kind() == r->kind()
4277 && ((l->get_corpus() && r->get_corpus()
4278 && (l->get_corpus() == r->get_corpus()))
4279 ||(l->get_translation_unit()
4280 && r->get_translation_unit()
4281 && l->get_translation_unit() == r->get_translation_unit())))
4282 {
4283 // Fast path optimization. If the two types are declared at
4284 // the same location (in the same binary) then it very likely
4285 // means the two types are equal.
4286 //
4287 // We really need every bit of optimization here because
4288 // otherwise, comparing types before canonicalization can take
4289 // forever.*
4290 decl_base *ld = is_decl(l.get());
4291 decl_base *rd = is_decl(r.get());
4292 ABG_ASSERT(ld && rd);
4293 if (ld->get_qualified_name() != rd->get_qualified_name())
4294 return false;
4295
4296 location ll = ld->get_location(), rl = rd->get_location();
4297 if (ll && rl)
4298 {
4299 string l1 = ll.expand();
4300 string l2 = rl.expand();
4301 if (l1 == l2)
4302 return true;
4303 }
4304 }
4305
4306 e.priv_->allow_type_comparison_results_caching(true);
4307 bool s0 = e.decl_only_class_equals_definition();
4308 e.decl_only_class_equals_definition(true);
4309 bool equal = l == r;
4310 e.decl_only_class_equals_definition(s0);
4311 e.priv_->clear_type_comparison_results_cache();
4312 e.priv_->allow_type_comparison_results_caching(false);
4313 return equal;
4314 }
4315
4316 /// Walk the declaration-only classes that have been found during
4317 /// the building of the corpus and resolve them to their definitions.
4318 void
4319 resolve_declaration_only_classes()
4320 {
4321 vector<string> resolved_classes;
4322
4323 for (string_classes_or_unions_map::iterator i =
4324 declaration_only_classes().begin();
4325 i != declaration_only_classes().end();
4326 ++i)
4327 {
4328 bool to_resolve = false;
4329 for (classes_or_unions_type::iterator j = i->second.begin();
4330 j != i->second.end();
4331 ++j)
4332 if ((*j)->get_is_declaration_only()
4333 && ((*j)->get_definition_of_declaration() == 0))
4334 to_resolve = true;
4335
4336 if (!to_resolve)
4337 {
4338 resolved_classes.push_back(i->first);
4339 continue;
4340 }
4341
4342 // Now, for each decl-only class that have the current name
4343 // 'i->first', let's try to poke at the fully defined class
4344 // that is defined in the same translation unit as the
4345 // declaration.
4346 //
4347 // If we find one class (defined in the TU of the declaration)
4348 // that defines the declaration, then the declaration can be
4349 // resolved to that class.
4350 //
4351 // If no defining class is found in the TU of the declaration,
4352 // then there are possibly three cases to consider:
4353 //
4354 // 1/ There is exactly one class that defines the
4355 // declaration and that class is defined in another TU. In
4356 // this case, the declaration is resolved to that
4357 // definition.
4358 //
4359 // 2/ There are more than one class that define that
4360 // declaration and none of them is defined in the TU of the
4361 // declaration. If those classes are all different, then
4362 // the declaration is left unresolved.
4363 //
4364 // 3/ No class defines the declaration. In this case, the
4365 // declaration is left unresoved.
4366
4367 // So get the classes that might define the current
4368 // declarations which name is i->first.
4369 const type_base_wptrs_type *classes =
4370 lookup_class_types(i->first, *corpus());
4371 if (!classes)
4372 classes = lookup_union_types(i->first, *corpus());
4373
4374 if (!classes)
4375 continue;
4376
4377 // This is a map that associates the translation unit path to
4378 // the class (that potentially defines the declarations that
4379 // we consider) that are defined in that translation unit. It
4380 // should stay ordered by using the TU path as key to ensure
4381 // stability of the order of classe definitions in ABIXML
4382 // output.
4383 map<string, class_or_union_sptr> per_tu_class_map;
4384 for (type_base_wptrs_type::const_iterator c = classes->begin();
4385 c != classes->end();
4386 ++c)
4387 {
4388 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4389 ABG_ASSERT(klass);
4390
4392 if (klass->get_is_declaration_only())
4393 continue;
4394
4395 string tu_path = klass->get_translation_unit()->get_absolute_path();
4396 if (tu_path.empty())
4397 continue;
4398
4399 // Build a map that associates the translation unit path
4400 // to the class (that potentially defines the declarations
4401 // that we consider) that are defined in that translation unit.
4402 per_tu_class_map[tu_path] = klass;
4403 }
4404
4405 if (!per_tu_class_map.empty())
4406 {
4407 // Walk the declarations to resolve and resolve them
4408 // either to the definitions that are in the same TU as
4409 // the declaration, or to the definition found elsewhere,
4410 // if there is only one such definition.
4411 for (classes_or_unions_type::iterator j = i->second.begin();
4412 j != i->second.end();
4413 ++j)
4414 {
4415 if ((*j)->get_is_declaration_only()
4416 && ((*j)->get_definition_of_declaration() == 0))
4417 {
4418 string tu_path =
4419 (*j)->get_translation_unit()->get_absolute_path();
4420 map<string, class_or_union_sptr>::const_iterator e =
4421 per_tu_class_map.find(tu_path);
4422 if (e != per_tu_class_map.end())
4423 (*j)->set_definition_of_declaration(e->second);
4424 else if (per_tu_class_map.size() == 1)
4425 (*j)->set_definition_of_declaration
4426 (per_tu_class_map.begin()->second);
4427 else
4428 {
4429 // We are in case where there are more than
4430 // one definition for the declaration. Let's
4431 // see if they are all equal. If they are,
4432 // then the declaration resolves to the
4433 // definition. Otherwise, we are in the case
4434 // 3/ described above.
4435 map<string,
4436 class_or_union_sptr>::const_iterator it;
4437 class_or_union_sptr first_class =
4438 per_tu_class_map.begin()->second;
4439 bool all_class_definitions_are_equal = true;
4440 for (it = per_tu_class_map.begin();
4441 it != per_tu_class_map.end();
4442 ++it)
4443 {
4444 if (it == per_tu_class_map.begin())
4445 continue;
4446 else
4447 {
4448 if (!compare_before_canonicalisation(it->second,
4449 first_class))
4450 {
4451 all_class_definitions_are_equal = false;
4452 break;
4453 }
4454 }
4455 }
4456 if (all_class_definitions_are_equal)
4457 (*j)->set_definition_of_declaration(first_class);
4458 }
4459 }
4460 }
4461 resolved_classes.push_back(i->first);
4462 }
4463 }
4464
4465 size_t num_decl_only_classes = declaration_only_classes().size(),
4466 num_resolved = resolved_classes.size();
4467 if (show_stats())
4468 cerr << "resolved " << num_resolved
4469 << " class declarations out of "
4470 << num_decl_only_classes
4471 << "\n";
4472
4473 for (vector<string>::const_iterator i = resolved_classes.begin();
4474 i != resolved_classes.end();
4475 ++i)
4476 declaration_only_classes().erase(*i);
4477
4478 if (show_stats() && !declaration_only_classes().empty())
4479 {
4480 cerr << "Here are the "
4481 << num_decl_only_classes - num_resolved
4482 << " unresolved class declarations:\n";
4483 for (string_classes_or_unions_map::iterator i =
4484 declaration_only_classes().begin();
4485 i != declaration_only_classes().end();
4486 ++i)
4487 cerr << " " << i->first << "\n";
4488 }
4489 }
4490
4491 /// Getter for the map of declaration-only enums that are to be
4492 /// resolved to their definition enums by the end of the corpus
4493 /// loading.
4494 ///
4495 /// @return a map of string -> vector of enums where the key is
4496 /// the fully qualified name of the enum and the value is the
4497 /// vector of declaration-only enum.
4498 const string_enums_map&
4499 declaration_only_enums() const
4500 {return decl_only_enums_map_;}
4501
4502 /// Getter for the map of declaration-only enums that are to be
4503 /// resolved to their definition enums by the end of the corpus
4504 /// loading.
4505 ///
4506 /// @return a map of string -> vector of enums where the key is
4507 /// the fully qualified name of the enum and the value is the
4508 /// vector of declaration-only enum.
4510 declaration_only_enums()
4511 {return decl_only_enums_map_;}
4512
4513 /// If a given enum is a declaration-only enum then stash it on
4514 /// the side so that at the end of the corpus reading we can resolve
4515 /// it to its definition.
4516 ///
4517 /// @param enom the enum to consider.
4518 void
4519 maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4520 {
4521 if (enom->get_is_declaration_only()
4522 && enom->get_definition_of_declaration() == 0
4523 // Make sure the enum is not anonymous. Anonymous enums are
4524 // usually later named by a typedef. At that time, after
4525 // being named by a typedef, this method is going to be called
4526 // with the enum being named by the typedef.
4527 && !enom->get_qualified_name().empty())
4528 {
4529 string qn = enom->get_qualified_name();
4530 string_enums_map::iterator record =
4531 declaration_only_enums().find(qn);
4532 if (record == declaration_only_enums().end())
4533 declaration_only_enums()[qn].push_back(enom);
4534 else
4535 record->second.push_back(enom);
4536 }
4537 }
4538
4539 /// Test if a given declaration-only enum has been scheduled for
4540 /// resolution to a defined enum.
4541 ///
4542 /// @param enom the enum to consider for the test.
4543 ///
4544 /// @return true iff @p enom is a declaration-only enum and if
4545 /// it's been scheduled for resolution to a defined enum.
4546 bool
4547 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4548 {
4549 if (enom->get_is_declaration_only())
4550 return (declaration_only_enums().find(enom->get_qualified_name())
4551 != declaration_only_enums().end());
4552
4553 return false;
4554 }
4555
4556 /// Walk the declaration-only enums that have been found during
4557 /// the building of the corpus and resolve them to their definitions.
4558 ///
4559 /// TODO: Do away with this function by factorizing it with
4560 /// resolve_declaration_only_classes. All declaration-only decls
4561 /// could be handled the same way as declaration-only-ness is a
4562 /// property of abigail::ir::decl_base now.
4563 void
4564 resolve_declaration_only_enums()
4565 {
4566 vector<string> resolved_enums;
4567
4568 for (string_enums_map::iterator i =
4569 declaration_only_enums().begin();
4570 i != declaration_only_enums().end();
4571 ++i)
4572 {
4573 bool to_resolve = false;
4574 for (enums_type::iterator j = i->second.begin();
4575 j != i->second.end();
4576 ++j)
4577 if ((*j)->get_is_declaration_only()
4578 && ((*j)->get_definition_of_declaration() == 0))
4579 to_resolve = true;
4580
4581 if (!to_resolve)
4582 {
4583 resolved_enums.push_back(i->first);
4584 continue;
4585 }
4586
4587 // Now, for each decl-only enum that have the current name
4588 // 'i->first', let's try to poke at the fully defined enum
4589 // that is defined in the same translation unit as the
4590 // declaration.
4591 //
4592 // If we find one enum (defined in the TU of the declaration)
4593 // that defines the declaration, then the declaration can be
4594 // resolved to that enum.
4595 //
4596 // If no defining enum is found in the TU of the declaration,
4597 // then there are possibly three cases to consider:
4598 //
4599 // 1/ There is exactly one enum that defines the
4600 // declaration and that enum is defined in another TU. In
4601 // this case, the declaration is resolved to that
4602 // definition.
4603 //
4604 // 2/ There are more than one (different) enum that define
4605 // that declaration and none of them is defined in the TU of
4606 // the declaration. In this case, the declaration is left
4607 // unresolved.
4608 //
4609 // 3/ No enum defines the declaration. In this case, the
4610 // declaration is left unresolved.
4611
4612 // So get the enums that might define the current
4613 // declarations which name is i->first.
4614 const type_base_wptrs_type *enums =
4615 lookup_enum_types(i->first, *corpus());
4616 if (!enums)
4617 continue;
4618
4619 // This is a map that associates the translation unit path to
4620 // the enum (that potentially defines the declarations that
4621 // we consider) that are defined in that translation unit. It
4622 // should stay ordered by using the TU path as key to ensure
4623 // stability of the order of enum definitions in ABIXML
4624 // output.
4625 map<string, enum_type_decl_sptr> per_tu_enum_map;
4626 for (type_base_wptrs_type::const_iterator c = enums->begin();
4627 c != enums->end();
4628 ++c)
4629 {
4630 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4631 ABG_ASSERT(enom);
4632
4634 if (enom->get_is_declaration_only())
4635 continue;
4636
4637 string tu_path = enom->get_translation_unit()->get_absolute_path();
4638 if (tu_path.empty())
4639 continue;
4640
4641 // Build a map that associates the translation unit path
4642 // to the enum (that potentially defines the declarations
4643 // that we consider) that are defined in that translation unit.
4644 per_tu_enum_map[tu_path] = enom;
4645 }
4646
4647 if (!per_tu_enum_map.empty())
4648 {
4649 // Walk the declarations to resolve and resolve them
4650 // either to the definitions that are in the same TU as
4651 // the declaration, or to the definition found elsewhere,
4652 // if there is only one such definition.
4653 for (enums_type::iterator j = i->second.begin();
4654 j != i->second.end();
4655 ++j)
4656 {
4657 if ((*j)->get_is_declaration_only()
4658 && ((*j)->get_definition_of_declaration() == 0))
4659 {
4660 string tu_path =
4661 (*j)->get_translation_unit()->get_absolute_path();
4662 map<string, enum_type_decl_sptr>::const_iterator e =
4663 per_tu_enum_map.find(tu_path);
4664 if (e != per_tu_enum_map.end())
4665 (*j)->set_definition_of_declaration(e->second);
4666 else if (per_tu_enum_map.size() == 1)
4667 (*j)->set_definition_of_declaration
4668 (per_tu_enum_map.begin()->second);
4669 else
4670 {
4671 // We are in case where there are more than
4672 // one definition for the declaration. Let's
4673 // see if they are all equal. If they are,
4674 // then the declaration resolves to the
4675 // definition. Otherwise, we are in the case
4676 // 3/ described above.
4677 map<string,
4678 enum_type_decl_sptr>::const_iterator it;
4679 enum_type_decl_sptr first_enum =
4680 per_tu_enum_map.begin()->second;
4681 bool all_enum_definitions_are_equal = true;
4682 for (it = per_tu_enum_map.begin();
4683 it != per_tu_enum_map.end();
4684 ++it)
4685 {
4686 if (it == per_tu_enum_map.begin())
4687 continue;
4688 else
4689 {
4690 if (!compare_before_canonicalisation(it->second,
4691 first_enum))
4692 {
4693 all_enum_definitions_are_equal = false;
4694 break;
4695 }
4696 }
4697 }
4698 if (all_enum_definitions_are_equal)
4699 (*j)->set_definition_of_declaration(first_enum);
4700 }
4701 }
4702 }
4703 resolved_enums.push_back(i->first);
4704 }
4705 }
4706
4707 size_t num_decl_only_enums = declaration_only_enums().size(),
4708 num_resolved = resolved_enums.size();
4709 if (show_stats())
4710 cerr << "resolved " << num_resolved
4711 << " enum declarations out of "
4712 << num_decl_only_enums
4713 << "\n";
4714
4715 for (vector<string>::const_iterator i = resolved_enums.begin();
4716 i != resolved_enums.end();
4717 ++i)
4718 declaration_only_enums().erase(*i);
4719
4720 if (show_stats() && !declaration_only_enums().empty())
4721 {
4722 cerr << "Here are the "
4723 << num_decl_only_enums - num_resolved
4724 << " unresolved enum declarations:\n";
4725 for (string_enums_map::iterator i = declaration_only_enums().begin();
4726 i != declaration_only_enums().end();
4727 ++i)
4728 cerr << " " << i->first << "\n";
4729 }
4730 }
4731
4732 /// Test if a symbol belongs to a function of the current ABI
4733 /// corpus.
4734 ///
4735 /// This is a sub-routine of fixup_functions_with_no_symbols.
4736 ///
4737 /// @param fn the function symbol to consider.
4738 ///
4739 /// @returnt true if @p fn belongs to a function of the current ABI
4740 /// corpus.
4741 bool
4742 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4743 {
4744 corpus_sptr corp = corpus();
4745 if (!corp)
4746 return false;
4747
4748 interned_string id = corp->get_environment().intern(fn->get_id_string());
4749
4750 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4751 if (!fns)
4752 return false;
4753
4754 for (auto f : *fns)
4755 if (f->get_symbol())
4756 return true;
4757
4758 return false;
4759 }
4760
4761 /// Some functions described by DWARF may have their linkage name
4762 /// set, but no link to their actual underlying elf symbol. When
4763 /// these are virtual member functions, comparing the enclosing type
4764 /// against another one which has its underlying symbol properly set
4765 /// might lead to spurious type changes.
4766 ///
4767 /// If the corpus contains a symbol with the same name as the
4768 /// linkage name of the function, then set up the link between the
4769 /// function and its underlying symbol.
4770 ///
4771 /// Note that for the moment, only virtual member functions are
4772 /// fixed up like this. This is because they really are the only
4773 /// fuctions of functions that can affect types (in spurious ways).
4774 void
4775 fixup_functions_with_no_symbols()
4776 {
4777 corpus_sptr corp = corpus();
4778 if (!corp)
4779 return;
4780
4781 die_function_decl_map_type &fns_with_no_symbol =
4782 die_function_decl_with_no_symbol_map();
4783
4784 if (do_log())
4785 cerr << fns_with_no_symbol.size()
4786 << " functions to fixup, potentially\n";
4787
4788 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4789 i != fns_with_no_symbol.end();
4790 ++i)
4791 if (elf_symbol_sptr sym =
4792 corp->lookup_function_symbol(i->second->get_linkage_name()))
4793 {
4794 // So i->second is a virtual member function that was
4795 // previously scheduled to be set a function symbol.
4796 //
4797 // But if it appears that it now has a symbol already set,
4798 // then do not set a symbol to it again.
4799 //
4800 // Or if it appears that another virtual member function
4801 // from the current ABI Corpus, with the same linkage
4802 // (mangled) name has already been set a symbol, then do not
4803 // set a symbol to this function either. Otherwise, there
4804 // will be two virtual member functions with the same symbol
4805 // in the class and that leads to spurious hard-to-debug
4806 // change reports later down the road.
4807 if (i->second->get_symbol()
4808 || symbol_already_belongs_to_a_function(sym))
4809 continue;
4810
4811 ABG_ASSERT(is_member_function(i->second));
4813 i->second->set_symbol(sym);
4814
4815 if (do_log())
4816 cerr << "fixed up '"
4817 << i->second->get_pretty_representation()
4818 << "' with symbol '"
4819 << sym->get_id_string()
4820 << "'\n";
4821 }
4822
4823 fns_with_no_symbol.clear();
4824 }
4825
4826 /// Copy missing member functions from a source @ref class_decl to a
4827 /// destination one.
4828 ///
4829 /// If a function is present on the source @ref class_decl and not
4830 /// on the destination one, then it's copied from the source class
4831 /// to the destination one.
4832 void
4833 copy_missing_member_functions(class_decl_sptr& dest_class,
4834 const class_decl_sptr& src_class)
4835 {
4836 for (auto method : src_class->get_member_functions())
4837 if (!method->get_linkage_name().empty())
4838 if (!dest_class->find_member_function(method->get_linkage_name()))
4839 {
4840 method_decl_sptr copied_method =
4841 copy_member_function(dest_class, method);
4842 ABG_ASSERT(copied_method);
4843 schedule_type_for_late_canonicalization(copied_method->get_type());
4844 }
4845 }
4846
4847 /// Copy missing data members from a source @ref class_decl to a
4848 /// destination one.
4849 ///
4850 /// If a data membe is present on the source @ref class_decl and not
4851 /// on the destination one, then it's copied from the source class
4852 /// to the destination one.
4853 ///
4854 /// @param dest_class the destination class type to copy the data
4855 /// member to.
4856 ///
4857 /// @param src_class the source class type to copy the data member
4858 /// from.
4859 void
4860 copy_missing_member_variables(class_decl_sptr& dest_class,
4861 const class_decl_sptr& src_class)
4862 {
4863 for (auto var : src_class->get_data_members())
4864 if (!var->get_name().empty())
4865 if (!dest_class->find_data_member(var->get_name()))
4866 {
4867 var_decl_sptr copied_data_member =
4868 copy_member_variable(dest_class, var);
4869 ABG_ASSERT(copied_data_member);
4870 }
4871 }
4872
4873 /// Test if there is an interator in a given range that points to
4874 /// an anonymous class.
4875 ///
4876 /// @param begin the start of the iterator range to consider.
4877 ///
4878 /// @param end the end of the iterator range to consider. This
4879 /// points to after the range.
4880 template <typename iterator_type>
4881 bool
4882 contains_anonymous_class(const iterator_type& begin,
4883 const iterator_type& end)
4884 {
4885 for (auto i = begin; i < end; ++i)
4886 {
4887 type_base_sptr t(*i);
4889 if (c && c->get_is_anonymous())
4890 return true;
4891 }
4892 return false;
4893 }
4894
4895 /// Ensure that all classes of the same name have the same virtual
4896 /// member functions. So copy the virtual member functions from a
4897 /// class C that have them to another class C that doesn't.
4898 ///
4899 /// @param begin an iterator to the first member of the set of
4900 /// classes which to merge virtual member functions for.
4901 ///
4902 /// @param end an iterator to the last member (one past the end
4903 /// actually) of the set of classes which to merge virtual member
4904 /// functions for.
4905 template <typename iterator_type>
4906 void
4907 merge_member_functions_of_classes(const iterator_type& begin,
4908 const iterator_type& end)
4909 {
4910 if (contains_anonymous_class(begin, end))
4911 return;
4912
4913 for (auto i = begin; i < end; ++i)
4914 {
4915 type_base_sptr t(*i);
4916 class_decl_sptr reference_class = is_class_type(t);
4917 if (!reference_class)
4918 continue;
4919
4920 string n1 = reference_class->get_pretty_representation(true, true);
4921 string n2;
4922 for (auto j = begin; j < end; ++j)
4923 {
4924 if (j == i)
4925 continue;
4926
4927 type_base_sptr type(*j);
4928 class_decl_sptr klass = is_class_type(type);
4929 if (!klass)
4930 continue;
4931
4932 n2 = klass->get_pretty_representation(true, true);
4933 if (n1 != n2)
4934 continue;
4935
4936 copy_missing_member_functions(reference_class, klass);
4937 copy_missing_member_functions(klass, reference_class);
4938 }
4939 }
4940 }
4941
4942 /// Ensure that all classes of the same name have the same data
4943 /// members.
4944 ///
4945 /// So copy the data mebmers from a class C that have them to
4946 /// another class C that doesn't.
4947 ///
4948 /// @param begin an iterator to the first member of the set of
4949 /// classes which to merge data members for.
4950 ///
4951 /// @param end an iterator to the last member (one past the end
4952 /// actually) of the set of classes which to merge data members for.
4953 template <typename iterator_type>
4954 void
4955 merge_member_variables_of_classes(const iterator_type& begin,
4956 const iterator_type& end)
4957 {
4958 if (contains_anonymous_class(begin, end))
4959 return;
4960
4961 for (auto i = begin; i < end; ++i)
4962 {
4963 type_base_sptr t(*i);
4964 class_decl_sptr reference_class = is_class_type(t);
4965 if (!reference_class)
4966 continue;
4967
4968 string n1 = reference_class->get_pretty_representation(true, true);
4969 string n2;
4970 for (auto j = begin; j < end; ++j)
4971 {
4972 if (j == i)
4973 continue;
4974
4975 type_base_sptr type(*j);
4976 class_decl_sptr klass = is_class_type(type);
4977 if (!klass)
4978 continue;
4979
4980 n2 = klass->get_pretty_representation(true, true);
4981 if (n1 != n2)
4982 continue;
4983
4984 copy_missing_member_variables(reference_class, klass);
4985 copy_missing_member_variables(klass, reference_class);
4986 }
4987 }
4988 }
4989
4990 /// Ensure that all classes of the same name have the same virtual
4991 /// member functions. So copy the virtual member functions from a
4992 /// class C that have them to another class C that doesn't.
4993 void
4994 merge_member_functions_and_variables_in_classes_of_same_names()
4995 {
4996 corpus_sptr abi = corpus();
4997 if (!abi)
4998 return;
4999
5001 abi->get_types().class_types();
5002
5003 for (auto entry : class_types)
5004 {
5005 auto& classes = entry.second;
5006 type_base_sptr first(classes.front());
5007
5008 if (classes.size() > 1 && !is_anonymous_type(first))
5009 {
5010 bool a_class_has_member_fns = false;
5011 bool a_class_has_member_vars = false;
5012 for (auto& c : classes)
5013 {
5014 type_base_sptr t(c);
5015 if (class_decl_sptr klass = is_class_type(t))
5016 {
5017 if (!klass->get_member_functions().empty())
5018 a_class_has_member_fns = true;
5019
5020 if (!klass->get_static_data_members().empty())
5021 a_class_has_member_vars = true;
5022 }
5023 }
5024 if (a_class_has_member_fns)
5025 merge_member_functions_of_classes(classes.begin(),
5026 classes.end());
5027 if (a_class_has_member_vars)
5028 merge_member_variables_of_classes(classes.begin(),
5029 classes.end());
5030 }
5031 }
5032 }
5033
5034 /// @return vectors of types created during the analysis of the
5035 /// DWARF and in the need of being canonicalized.
5036 const vector<type_base_sptr>&
5037 types_to_canonicalize() const
5038 {return types_to_canonicalize_;}
5039
5040 /// @return vectors of types created during the analysis of the
5041 /// DWARF and in the need of being canonicalized.
5042 vector<type_base_sptr>&
5043 types_to_canonicalize()
5044 {return types_to_canonicalize_;}
5045
5046 /// Clear the containers holding types to canonicalize.
5047 void
5048 clear_types_to_canonicalize()
5049 {
5050 types_to_canonicalize_.clear();
5051 }
5052
5053 /// Types that were created but not tied to a particular DIE, must
5054 /// be scheduled for late canonicalization using this method.
5055 ///
5056 /// @param t the type to schedule for late canonicalization.
5057 void
5058 schedule_type_for_late_canonicalization(const type_base_sptr &t)
5059 {
5060 types_to_canonicalize_.push_back(t);
5061 }
5062
5063 /// Canonicalize types which DIE offsets are stored in vectors on
5064 /// the side. This is a sub-routine of
5065 /// reader::perform_late_type_canonicalizing().
5066 ///
5067 /// @param source where the DIE of the types to canonicalize are
5068 /// from.
5069 void
5070 canonicalize_types_scheduled()
5071 {
5072 tools_utils::timer cn_timer;
5073 if (do_log())
5074 {
5075 cerr << "DWARF Reader is going to canonicalize "
5076 << std::dec
5077 << types_to_canonicalize().size()
5078 << " types";
5079 corpus_sptr c = corpus();
5080 if (c)
5081 cerr << " from corpus " << corpus()->get_path() << "\n";
5082 cn_timer.start();
5083 }
5084
5086 (types_to_canonicalize().begin(),
5087 types_to_canonicalize().end(),
5088 [](const vector<type_base_sptr>::const_iterator& i)
5089 {return *i;}, do_log(), show_stats());
5090
5091 if (do_log())
5092 {
5093 cn_timer.stop();
5094 cerr << "DWARF Reader finished types "
5095 << "sorting, hashing & canonicalizing in: "
5096 << cn_timer << "\n";
5097 }
5098 }
5099
5100 /// Compute the number of canonicalized and missed types in the late
5101 /// canonicalization phase.
5102 ///
5103 /// @param source where the DIEs of the canonicalized types are
5104 /// from.
5105 ///
5106 /// @param canonicalized the number of types that got canonicalized
5107 /// is added to the value already present in this parameter.
5108 ///
5109 /// @param missed the number of types scheduled for late
5110 /// canonicalization and which couldn't be canonicalized (for a
5111 /// reason) is added to the value already present in this parameter.
5112 void
5113 add_late_canonicalized_types_stats(size_t& canonicalized,
5114 size_t& missed) const
5115 {
5116 for (auto t : types_to_canonicalize())
5117 {
5118 if (t->get_canonical_type())
5119 ++canonicalized;
5120 else
5121 ++missed;
5122 }
5123 }
5124
5125 // Look at the types that need to be canonicalized after the
5126 // translation unit has been constructed and canonicalize them.
5127 void
5128 perform_late_type_canonicalizing()
5129 {
5130 canonicalize_types_scheduled();
5131
5132 if (show_stats())
5133 {
5134 size_t num_canonicalized = 0, num_missed = 0, total = 0;
5135 add_late_canonicalized_types_stats(num_canonicalized,
5136 num_missed);
5137 total = num_canonicalized + num_missed;
5138 cerr << "binary: "
5139 << elf_path()
5140 << "\n";
5141 cerr << " # late canonicalized types: "
5142 << num_canonicalized;
5143 if (total)
5144 cerr << " (" << num_canonicalized * 100 / total << "%)";
5145 cerr << "\n"
5146 << " # missed canonicalization opportunities: "
5147 << num_missed;
5148 if (total)
5149 cerr << " (" << num_missed * 100 / total << "%)";
5150 cerr << "\n";
5151 }
5152
5153 }
5154
5155 const die_tu_map_type&
5156 die_tu_map() const
5157 {return die_tu_map_;}
5158
5160 die_tu_map()
5161 {return die_tu_map_;}
5162
5163 /// Getter for the map that associates a translation unit DIE to the
5164 /// vector of imported unit points that it contains.
5165 ///
5166 /// @param source where the DIEs are from.
5167 ///
5168 /// @return the map.
5170 tu_die_imported_unit_points_map(die_source source) const
5171 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
5172
5173 /// Getter for the map that associates a translation unit DIE to the
5174 /// vector of imported unit points that it contains.
5175 ///
5176 /// @param source where the DIEs are from.
5177 ///
5178 /// @return the map.
5180 tu_die_imported_unit_points_map(die_source source)
5181 {
5182 switch (source)
5183 {
5184 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5185 break;
5186 case ALT_DEBUG_INFO_DIE_SOURCE:
5187 return alt_tu_die_imported_unit_points_map_;
5188 case TYPE_UNIT_DIE_SOURCE:
5189 return type_units_tu_die_imported_unit_points_map_;
5190 case NO_DEBUG_INFO_DIE_SOURCE:
5191 case NUMBER_OF_DIE_SOURCES:
5192 // We cannot reach this point.
5194 }
5195 return tu_die_imported_unit_points_map_;
5196 }
5197
5198 /// Reset the current corpus being constructed.
5199 ///
5200 /// This actually deletes the current corpus being constructed.
5201 void
5202 reset_corpus()
5203 {corpus().reset();}
5204
5205 /// Get the map that associates each DIE to its parent DIE. This is
5206 /// for DIEs coming from the main debug info sections.
5207 ///
5208 /// @param source where the DIEs in the map come from.
5209 ///
5210 /// @return the DIE -> parent map.
5212 die_parent_map(die_source source) const
5213 {return const_cast<reader*>(this)->die_parent_map(source);}
5214
5215 /// Get the map that associates each DIE to its parent DIE. This is
5216 /// for DIEs coming from the main debug info sections.
5217 ///
5218 /// @param source where the DIEs in the map come from.
5219 ///
5220 /// @return the DIE -> parent map.
5222 die_parent_map(die_source source)
5223 {
5224 switch (source)
5225 {
5226 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5227 break;
5228 case ALT_DEBUG_INFO_DIE_SOURCE:
5229 return alternate_die_parent_map_;
5230 case TYPE_UNIT_DIE_SOURCE:
5231 return type_section_die_parent_map();
5232 case NO_DEBUG_INFO_DIE_SOURCE:
5233 case NUMBER_OF_DIE_SOURCES:
5235 }
5236 return primary_die_parent_map_;
5237 }
5238
5240 type_section_die_parent_map() const
5241 {return type_section_die_parent_map_;}
5242
5244 type_section_die_parent_map()
5245 {return type_section_die_parent_map_;}
5246
5247 /// Getter of the current translation unit.
5248 ///
5249 /// @return the current translation unit being constructed.
5251 cur_transl_unit() const
5252 {return cur_tu_;}
5253
5254 /// Getter of the current translation unit.
5255 ///
5256 /// @return the current translation unit being constructed.
5258 cur_transl_unit()
5259 {return cur_tu_;}
5260
5261 /// Setter of the current translation unit.
5262 ///
5263 /// @param tu the current translation unit being constructed.
5264 void
5265 cur_transl_unit(translation_unit_sptr tu)
5266 {
5267 if (tu)
5268 cur_tu_ = tu;
5269 }
5270
5271 /// Return the global scope of the current translation unit.
5272 ///
5273 /// @return the global scope of the current translation unit.
5274 const scope_decl_sptr&
5275 global_scope() const
5276 {return cur_transl_unit()->get_global_scope();}
5277
5278 /// Return a scope that is nil.
5279 ///
5280 /// @return a scope that is nil.
5281 const scope_decl_sptr&
5282 nil_scope() const
5283 {return nil_scope_;}
5284
5285 const scope_stack_type&
5286 scope_stack() const
5287 {return scope_stack_;}
5288
5290 scope_stack()
5291 {return scope_stack_;}
5292
5293 scope_decl*
5294 current_scope()
5295 {
5296 if (scope_stack().empty())
5297 {
5298 if (cur_transl_unit())
5299 scope_stack().push(cur_transl_unit()->get_global_scope().get());
5300 }
5301 return scope_stack().top();
5302 }
5303
5304 list<var_decl_sptr>&
5305 var_decls_to_re_add_to_tree()
5306 {return var_decls_to_add_;}
5307
5308 /// Test if a DIE represents a decl (function or variable) that has
5309 /// a symbol that is exported, whatever that means. This is
5310 /// supposed to work for Linux Kernel binaries as well.
5311 ///
5312 /// This is useful to limit the amount of DIEs taken into account to
5313 /// the strict limit of what an ABI actually means. Limiting the
5314 /// volume of DIEs analyzed this way is an important optimization to
5315 /// keep big binaries "manageable" by libabigail.
5316 ///
5317 /// @param DIE the die to consider.
5318 bool
5319 is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
5320 {
5321 if (!die || !die_is_decl(die))
5322 return false;
5323
5324 bool result = false, address_found = false, symbol_is_exported = false;;
5325 Dwarf_Addr decl_symbol_address = 0;
5326
5327 if (die_is_variable_decl(die))
5328 {
5329 if ((address_found = get_variable_address(die, decl_symbol_address)))
5330 symbol_is_exported =
5331 !!variable_symbol_is_exported(decl_symbol_address);
5332 }
5333 else if (die_is_function_decl(die))
5334 {
5335 if ((address_found = get_function_address(die, decl_symbol_address)))
5336 symbol_is_exported =
5337 !!function_symbol_is_exported(decl_symbol_address);
5338 }
5339
5340 if (address_found)
5341 result = symbol_is_exported;
5342
5343 return result;
5344 }
5345
5346 /// Test if a DIE is a variable or function DIE which name denotes
5347 /// an undefined ELF symbol.
5348 ///
5349 /// @return true iff @p die represents a function or variable that
5350 /// has an undefined symbol.
5351 bool
5352 is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
5353 {
5354 if (is_decl_die_with_exported_symbol(die))
5355 return false;
5356
5357 string name, linkage_name;
5358 die_name_and_linkage_name(die, name, linkage_name);
5359 if (linkage_name.empty())
5360 linkage_name = name;
5361
5362 bool result = false;
5363 if ((die_is_variable_decl(die)
5364 && symtab()->variable_symbol_is_undefined(linkage_name))
5365 ||
5366 (die_is_function_decl(die)
5367 && symtab()->function_symbol_is_undefined(linkage_name)))
5368 result = true;
5369
5370 return result;
5371 }
5372
5373 /// This is a sub-routine of maybe_adjust_fn_sym_address and
5374 /// maybe_adjust_var_sym_address.
5375 ///
5376 /// Given an address that we got by looking at some debug
5377 /// information (e.g, a symbol's address referred to by a DWARF
5378 /// TAG), If the ELF file we are interested in is a shared library
5379 /// or an executable, then adjust the address to be coherent with
5380 /// where the executable (or shared library) is loaded. That way,
5381 /// the address can be used to look for symbols in the executable or
5382 /// shared library.
5383 ///
5384 /// @return the adjusted address, or the same address as @p addr if
5385 /// it didn't need any adjustment.
5386 Dwarf_Addr
5387 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
5388 {
5389 if (addr == 0)
5390 return addr;
5391
5392 GElf_Ehdr eh_mem;
5393 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5394
5395 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
5396 {
5397 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
5398 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
5399 dwarf_elf_load_address));
5401 elf_load_address));
5402 if (dwarf_is_splitted()
5403 && (dwarf_elf_load_address != elf_load_address))
5404 // This means that in theory the DWARF and the executable are
5405 // not loaded at the same address. And addr is meaningful
5406 // only in the context of the DWARF.
5407 //
5408 // So let's transform addr into an offset relative to where
5409 // the DWARF is loaded, and let's add that relative offset
5410 // to the load address of the executable. That way, addr
5411 // becomes meaningful in the context of the executable and
5412 // can thus be used to compare against the address of
5413 // symbols of the executable, for instance.
5414 addr = addr - dwarf_elf_load_address + elf_load_address;
5415 }
5416
5417 return addr;
5418 }
5419
5420 /// For a relocatable (*.o) elf file, this function expects an
5421 /// absolute address, representing a function symbol. It then
5422 /// extracts the address of the .text section from the symbol
5423 /// absolute address to get the relative address of the function
5424 /// from the beginning of the .text section.
5425 ///
5426 /// For executable or shared library, this function expects an
5427 /// address of a function symbol that was retrieved by looking at a
5428 /// DWARF "file". The function thus adjusts the address to make it
5429 /// be meaningful in the context of the ELF file.
5430 ///
5431 /// In both cases, the address can then be compared against the
5432 /// st_value field of a function symbol from the ELF file.
5433 ///
5434 /// @param addr an adress for a function symbol that was retrieved
5435 /// from a DWARF file.
5436 ///
5437 /// @return the (possibly) adjusted address, or just @p addr if no
5438 /// adjustment took place.
5439 Dwarf_Addr
5440 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
5441 {
5442 if (addr == 0)
5443 return addr;
5444
5445 Elf* elf = elf_handle();
5446 GElf_Ehdr eh_mem;
5447 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5448
5449 if (elf_header->e_type == ET_REL)
5450 // We are looking at a relocatable file. In this case, we don't
5451 // do anything because:
5452 //
5453 // 1/ the addresses from DWARF are absolute (relative to the
5454 // beginning of the relocatable file)
5455 //
5456 // 2/ The ELF symbol addresses that we store in our lookup
5457 // tables are translated from section-related to absolute as
5458 // well. So we don't have anything to do at this point for
5459 // ET_REL files.
5460 ;
5461 else
5462 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5463
5464 return addr;
5465 }
5466
5467 /// For a relocatable (*.o) elf file, this function expects an
5468 /// absolute address, representing a global variable symbol. It
5469 /// then extracts the address of the {.data,.data1,.rodata,.bss}
5470 /// section from the symbol absolute address to get the relative
5471 /// address of the variable from the beginning of the data section.
5472 ///
5473 /// For executable or shared library, this function expects an
5474 /// address of a variable symbol that was retrieved by looking at a
5475 /// DWARF "file". The function thus adjusts the address to make it
5476 /// be meaningful in the context of the ELF file.
5477 ///
5478 /// In both cases, the address can then be compared against the
5479 /// st_value field of a function symbol from the ELF file.
5480 ///
5481 /// @param addr an address for a global variable symbol that was
5482 /// retrieved from a DWARF file.
5483 ///
5484 /// @return the (possibly) adjusted address, or just @p addr if no
5485 /// adjustment took place.
5486 Dwarf_Addr
5487 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5488 {
5489 Elf* elf = elf_handle();
5490 GElf_Ehdr eh_mem;
5491 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5492
5493 if (elf_header->e_type == ET_REL)
5494 // We are looking at a relocatable file. In this case, we don't
5495 // do anything because:
5496 //
5497 // 1/ the addresses from DWARF are absolute (relative to the
5498 // beginning of the relocatable file)
5499 //
5500 // 2/ The ELF symbol addresses that we store in our lookup
5501 // tables are translated from section-related to absolute as
5502 // well. So we don't have anything to do at this point for
5503 // ET_REL files.
5504 ;
5505 else
5506 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5507
5508 return addr;
5509 }
5510
5511 /// Get the first exported function address in the set of addresses
5512 /// referred to by the DW_AT_ranges attribute of a given DIE.
5513 ///
5514 /// @param die the DIE we are considering.
5515 ///
5516 /// @param address output parameter. This is set to the first
5517 /// address found in the sequence pointed to by the DW_AT_ranges
5518 /// attribute found on the DIE @p die, iff the function returns
5519 /// true. Otherwise, no value is set into this output parameter.
5520 ///
5521 /// @return true iff the DIE @p die does have a DW_AT_ranges
5522 /// attribute and an address of an exported function was found in
5523 /// its sequence value.
5524 bool
5525 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5526 Dwarf_Addr& address) const
5527 {
5528 Dwarf_Addr base;
5529 Dwarf_Addr end_addr;
5530 ptrdiff_t offset = 0;
5531
5532 do
5533 {
5534 Dwarf_Addr addr = 0, fn_addr = 0;
5535 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5536 {
5537 fn_addr = maybe_adjust_fn_sym_address(addr);
5538 if (function_symbol_is_exported(fn_addr))
5539 {
5540 address = fn_addr;
5541 return true;
5542 }
5543 }
5544 } while (offset > 0);
5545 return false;
5546 }
5547
5548 /// Get the address of the function.
5549 ///
5550 /// The address of the function is considered to be the value of the
5551 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5552 /// only) to not point to an absolute address anymore, but rather to
5553 /// the address of the function inside the .text segment.
5554 ///
5555 /// @param function_die the die of the function to consider.
5556 ///
5557 /// @param address the resulting address iff the function returns
5558 /// true.
5559 ///
5560 /// @return true if the function address was found.
5561 bool
5562 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5563 {
5564 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5565 DW_AT_low_pc, address))
5566 // So no DW_AT_low_pc was found. Let's see if the function DIE
5567 // has got a DW_AT_ranges attribute instead. If it does, the
5568 // first address of the set of addresses represented by the
5569 // value of that DW_AT_ranges represents the function (symbol)
5570 // address we are looking for.
5571 if (!get_first_exported_fn_address_from_DW_AT_ranges
5572 (const_cast<Dwarf_Die*>(function_die),
5573 address))
5574 return false;
5575
5576 address = maybe_adjust_fn_sym_address(address);
5577 return true;
5578 }
5579
5580 /// Get the address of the global variable.
5581 ///
5582 /// The address of the global variable is considered to be the value
5583 /// of the DW_AT_location attribute, possibly adjusted (in
5584 /// relocatable files only) to not point to an absolute address
5585 /// anymore, but rather to the address of the global variable inside
5586 /// the data segment.
5587 ///
5588 /// @param variable_die the die of the function to consider.
5589 ///
5590 /// @param address the resulting address iff this function returns
5591 /// true.
5592 ///
5593 /// @return true if the variable address was found.
5594 bool
5595 get_variable_address(const Dwarf_Die* variable_die,
5596 Dwarf_Addr& address) const
5597 {
5598 bool is_tls_address = false;
5599 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5600 address, is_tls_address))
5601 return false;
5602 if (!is_tls_address)
5603 address = maybe_adjust_var_sym_address(address);
5604 return true;
5605 }
5606
5607 /// Getter of the exported decls builder object.
5608 ///
5609 /// @return the exported decls builder.
5610 corpus::exported_decls_builder*
5611 exported_decls_builder()
5612 {return corpus()->get_exported_decls_builder().get();}
5613
5614 /// Getter of the "load_all_types" flag. This flag tells if all the
5615 /// types (including those not reachable by public declarations) are
5616 /// to be read and represented in the final ABI corpus.
5617 ///
5618 /// @return the load_all_types flag.
5619 bool
5620 load_all_types() const
5621 {return options().load_all_types;}
5622
5623 /// Setter of the "load_all_types" flag. This flag tells if all the
5624 /// types (including those not reachable by public declarations) are
5625 /// to be read and represented in the final ABI corpus.
5626 ///
5627 /// @param f the new load_all_types flag.
5628 void
5629 load_all_types(bool f)
5630 {options().load_all_types = f;}
5631
5632 bool
5633 load_in_linux_kernel_mode() const
5634 {return options().load_in_linux_kernel_mode;}
5635
5636 void
5637 load_in_linux_kernel_mode(bool f)
5638 {options().load_in_linux_kernel_mode = f;}
5639
5640 /// Getter of the 'load-undefined-interface' property.
5641 ///
5642 /// That property tells the reader if it should load the interfaces
5643 /// that are undefined in the binary. An undefined interface is a
5644 /// variable or function which has a symbol that is not defined in
5645 /// the binary.
5646 ///
5647 /// @return true iff the front-end has to load the undefined
5648 /// interfaces.
5649 bool
5650 load_undefined_interfaces() const
5652
5653 /// Test if it's allowed to assume that the DWARF debug info has
5654 /// been factorized (for instance, with the DWZ tool) so that if two
5655 /// type DIEs originating from the .gnu_debugaltlink section have
5656 /// different offsets, they represent different types.
5657 ///
5658 /// @return true iff we can assume that the DWARF debug info has
5659 /// been factorized.
5660 bool
5661 leverage_dwarf_factorization() const
5662 {
5663 if (!leverage_dwarf_factorization_.has_value())
5664 {
5665 if (options().leverage_dwarf_factorization
5666 && elf_helpers::find_section_by_name(elf_handle(),
5667 ".gnu_debugaltlink"))
5668 leverage_dwarf_factorization_ = true;
5669 else
5670 leverage_dwarf_factorization_ = false;
5671 }
5672 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5673
5674 return *leverage_dwarf_factorization_;
5675 }
5676
5677 /// Getter of the "show_stats" flag.
5678 ///
5679 /// This flag tells if we should emit statistics about various
5680 /// internal stuff.
5681 ///
5682 /// @return the value of the flag.
5683 bool
5684 show_stats() const
5685 {return options().show_stats;}
5686
5687 /// Setter of the "show_stats" flag.
5688 ///
5689 /// This flag tells if we should emit statistics about various
5690 /// internal stuff.
5691 ///
5692 /// @param f the value of the flag.
5693 void
5694 show_stats(bool f)
5695 {options().show_stats = f;}
5696
5697 /// Getter of the "do_log" flag.
5698 ///
5699 /// This flag tells if we should log about various internal
5700 /// details.
5701 ///
5702 /// return the "do_log" flag.
5703 bool
5704 do_log() const
5705 {return options().do_log;}
5706
5707 /// Setter of the "do_log" flag.
5708 ///
5709 /// This flag tells if we should log about various internal details.
5710 ///
5711 /// @param f the new value of the flag.
5712 void
5713 do_log(bool f)
5714 {options().do_log = f;}
5715
5716 /// Walk the DIEs under a given die and for each child, populate the
5717 /// die -> parent map to record the child -> parent relationship
5718 /// that
5719 /// exists between the child and the given die.
5720 ///
5721 /// The function also builds the vector of places where units are
5722 /// imported.
5723 ///
5724 /// This is done recursively as for each child DIE, this function
5725 /// walks its children as well.
5726 ///
5727 /// @param die the DIE whose children to walk recursively.
5728 ///
5729 /// @param source where the DIE @p die comes from.
5730 ///
5731 /// @param imported_units a vector containing all the offsets of the
5732 /// points where unit have been imported, under @p die.
5733 void
5734 build_die_parent_relations_under(Dwarf_Die* die,
5735 die_source source,
5736 imported_unit_points_type & imported_units)
5737 {
5738 if (!die)
5739 return;
5740
5741 offset_offset_map_type& parent_of = die_parent_map(source);
5742
5743 Dwarf_Die child;
5744 if (dwarf_child(die, &child) != 0)
5745 return;
5746
5747 do
5748 {
5749 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5750 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5751 {
5752 Dwarf_Die imported_unit;
5753 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5754 // If the imported_unit has a sub-tree, let's record
5755 // this point at which the sub-tree is imported into
5756 // the current debug info.
5757 //
5758 // Otherwise, if the imported_unit has no sub-tree,
5759 // there is no point in recording where a non-existent
5760 // sub-tree is being imported.
5761 //
5762 // Note that the imported_unit_points_type type below
5763 // expects the imported_unit to have a sub-tree.
5764 && die_has_children(&imported_unit))
5765 {
5766 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5767 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5768 imported_units.push_back
5769 (imported_unit_point(dwarf_dieoffset(&child),
5770 imported_unit,
5771 imported_unit_die_source));
5772 }
5773 }
5774 build_die_parent_relations_under(&child, source, imported_units);
5775 }
5776 while (dwarf_siblingof(&child, &child) == 0);
5777
5778 }
5779
5780 /// Determine if we do have to build a DIE -> parent map, depending
5781 /// on a given language.
5782 ///
5783 /// Some languages like C++, Ada etc, do have the concept of
5784 /// namespace and yet, the DIE data structure doesn't provide us
5785 /// with a way to get the parent namespace of a given DIE. So for
5786 /// those languages, we need to build a DIE -> parent map so that we
5787 /// can get the namespace DIE (or more generally the scope DIE) of a given
5788 /// DIE as we need it.
5789 ///
5790 /// But then some more basic languages like C or assembly don't have
5791 /// that need.
5792 ///
5793 /// This function, depending on the language, tells us if we need to
5794 /// build the DIE -> parent map or not.
5795 ///
5796 /// @param lang the language to consider.
5797 ///
5798 /// @return true iff we need to build the DIE -> parent map for this
5799 /// language.
5800 bool
5801 do_we_build_die_parent_maps(translation_unit::language lang)
5802 {
5803 if (is_c_language(lang))
5804 return false;
5805
5806 switch (lang)
5807 {
5808 case translation_unit::LANG_UNKNOWN:
5809#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5810 case translation_unit::LANG_Mips_Assembler:
5811#endif
5812 return false;
5813 default:
5814 break;
5815 }
5816 return true;
5817 }
5818
5819 /// Walk all the DIEs accessible in the debug info (and in the
5820 /// alternate debug info as well) and build maps representing the
5821 /// relationship DIE -> parent. That is, make it so that we can get
5822 /// the parent for a given DIE.
5823 ///
5824 /// Note that the goal of this map is to be able to get the parent
5825 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5826 /// when we get a DIE of a type, and we want to build an internal
5827 /// representation for it, we need to get its fully qualified name.
5828 /// For that, we need to know what is the parent DIE of that type
5829 /// DIE, so that we can know what the namespace of that type is.
5830 ///
5831 /// Note that as the C language doesn't have namespaces (all types
5832 /// are defined in the same global namespace), this function doesn't
5833 /// build the DIE -> parent map if the current translation unit
5834 /// comes from C. This saves time on big C ELF files with a lot of
5835 /// DIEs.
5836 void
5837 build_die_parent_maps()
5838 {
5839 bool we_do_have_to_build_die_parent_map = false;
5840 uint8_t address_size = 0;
5841 size_t header_size = 0;
5842 // Get the DIE of the current translation unit, look at it to get
5843 // its language. If that language is in C, then all types are in
5844 // the global namespace so we don't need to build the DIE ->
5845 // parent map. So we dont build it in that case.
5846 for (Dwarf_Off offset = 0, next_offset = 0;
5847 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5848 offset, &next_offset, &header_size,
5849 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5850 offset = next_offset)
5851 {
5852 Dwarf_Off die_offset = offset + header_size;
5853 Dwarf_Die cu;
5854 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5855 die_offset, &cu))
5856 continue;
5857
5858 uint64_t l = 0;
5859 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5860 translation_unit::language lang = dwarf_language_to_tu_language(l);
5861 if (do_we_build_die_parent_maps(lang))
5862 we_do_have_to_build_die_parent_map = true;
5863 }
5864
5865 if (!we_do_have_to_build_die_parent_map)
5866 return;
5867
5868 // Build the DIE -> parent relation for DIEs coming from the
5869 // .debug_info section in the alternate debug info file.
5870 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5871 for (Dwarf_Off offset = 0, next_offset = 0;
5872 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5873 offset, &next_offset, &header_size,
5874 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5875 offset = next_offset)
5876 {
5877 Dwarf_Off die_offset = offset + header_size;
5878 Dwarf_Die cu;
5879 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5880 die_offset, &cu))
5881 continue;
5882 cur_tu_die(&cu);
5883
5884 imported_unit_points_type& imported_units =
5885 tu_die_imported_unit_points_map(source)[die_offset] =
5887 build_die_parent_relations_under(&cu, source, imported_units);
5888 }
5889
5890 // Build the DIE -> parent relation for DIEs coming from the
5891 // .debug_info section of the main debug info file.
5892 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5893 address_size = 0;
5894 header_size = 0;
5895 for (Dwarf_Off offset = 0, next_offset = 0;
5896 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5897 offset, &next_offset, &header_size,
5898 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5899 offset = next_offset)
5900 {
5901 Dwarf_Off die_offset = offset + header_size;
5902 Dwarf_Die cu;
5903 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5904 die_offset, &cu))
5905 continue;
5906 cur_tu_die(&cu);
5907 imported_unit_points_type& imported_units =
5908 tu_die_imported_unit_points_map(source)[die_offset] =
5910 build_die_parent_relations_under(&cu, source, imported_units);
5911 }
5912
5913 // Build the DIE -> parent relation for DIEs coming from the
5914 // .debug_types section.
5915 source = TYPE_UNIT_DIE_SOURCE;
5916 address_size = 0;
5917 header_size = 0;
5918 uint64_t type_signature = 0;
5919 Dwarf_Off type_offset;
5920 for (Dwarf_Off offset = 0, next_offset = 0;
5921 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5922 offset, &next_offset, &header_size,
5923 NULL, NULL, &address_size, NULL,
5924 &type_signature, &type_offset) == 0);
5925 offset = next_offset)
5926 {
5927 Dwarf_Off die_offset = offset + header_size;
5928 Dwarf_Die cu;
5929
5930 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5931 die_offset, &cu))
5932 continue;
5933 cur_tu_die(&cu);
5934 imported_unit_points_type& imported_units =
5935 tu_die_imported_unit_points_map(source)[die_offset] =
5937 build_die_parent_relations_under(&cu, source, imported_units);
5938 }
5939 }
5940};// end class reader.
5941
5942/// The type of the aggregates being compared during a DIE comparison.
5943///
5944/// This encapsulates the stack of aggregates being compared at any
5945/// single point.
5946///
5947/// This is useful to detect "comparison cycles" and thus avoid the
5948/// resulting infinite loops.
5949///
5950/// This is also useful for implementing a very important optimization
5951/// that takes place during the canonicalization
5952struct offset_pairs_stack_type
5953{
5954 // The DWARF DWARF reader that is useful for so many things.
5955 const reader& rdr_;
5956 // The set of types that are being compared. This is to speed up
5957 // searches.
5959 // The stack of types that are being compared. The top of the
5960 // stack is the back of the vector.
5962 // A map that associates a redundant type pair to the vector of
5963 // types that depends on it.
5964 offset_pair_vect_map_type redundant_types_;
5965 // A map that associates a dependant type to the vector of redundant
5966 // types it depends on.
5967 offset_pair_vect_map_type dependant_types_;
5968
5969 offset_pairs_stack_type(const reader& rdr)
5970 : rdr_ (rdr)
5971 {}
5972
5973 /// Add a pair of types being compared to the stack of aggregates
5974 /// being compared.
5975 ///
5976 /// @param p the pair of offsets of the type DIEs to consider.
5977 void
5978 add(const offset_pair_type& p)
5979 {
5980 set_.insert(p);
5981 vect_.push_back(p);
5982 }
5983
5984 /// Erase a pair of types being compared from the stack of
5985 /// aggregates being compared.
5986 ///
5987 /// @param p the pair of offsets of the type DIEs to consider.
5988 ///
5989 /// @return true iff @p was found and erased from the stack.
5990 bool
5991 erase(const offset_pair_type& p)
5992 {
5993 if (set_.erase(p))
5994 {
5995 offset_pair_vector_type::iterator i;
5996
5997 for (i = vect_.begin();i < vect_.end(); ++i)
5998 if (*i == p)
5999 break;
6000
6001 if (i != vect_.end())
6002 vect_.erase(i);
6003
6004 return true;
6005 }
6006
6007 return false;
6008 }
6009
6010 /// Test if a pair of type DIEs is part of the stack of type DIEs
6011 /// being compared.
6012 ///
6013 /// @param p the pair of offsets of the type DIEs to consider.
6014 ///
6015 /// @return true iff @p was found in the stack of types being
6016 /// compared.
6017 bool
6018 contains(const offset_pair_type &p) const
6019 {
6020 if (set_.find(p) == set_.end())
6021 return false;
6022 return true;
6023 }
6024
6025 /// Get the set of comparison pair that depends on a given
6026 /// comparison pair.
6027 ///
6028 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
6029 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
6030 /// words, the pair T appears in the comparison stack BEFORE the
6031 /// pair P.
6032 ///
6033 /// So, this function returns the vector of comparison pairs that
6034 /// appear in the comparison stack AFTER a given comparison pair.
6035 ///
6036 /// @param p the comparison pair to consider.
6037 ///
6038 /// @param pairs out parameter. This is filled with the comparison
6039 /// pairs that depend on @p, iff the function returns true.
6040 ///
6041 /// @return true iff comparison pairs depending on @p have been
6042 /// found and collected in @pairs.
6043 bool
6044 get_pairs_that_depend_on(const offset_pair_type& p,
6045 offset_pair_vector_type& pairs) const
6046 {
6047 bool result = false;
6048 if (!contains(p))
6049 return result;
6050
6051 // First, get an iterator on the position of 'p'.
6052 offset_pair_vector_type::const_iterator i;
6053 for (i = vect_.begin(); i != vect_.end(); ++i)
6054 if (*i == p)
6055 break;
6056
6057 if (i == vect_.end())
6058 return result;
6059
6060 // Then, harvest all the comparison pairs that come after the
6061 // position of 'p'.
6062 for (++i; i != vect_.end(); ++i)
6063 {
6064 pairs.push_back(*i);
6065 result = true;
6066 }
6067
6068 return result;
6069 }
6070
6071 /// Record the fact that a set of comparison pairs depends on a
6072 /// given comparison pair.
6073 ///
6074 /// Set a map that associates each dependant comparison pair to the
6075 /// pair it depends on.
6076 ///
6077 /// @param p the comparison pair that the set depends on.
6078 ///
6079 /// @param dependant_types the set of types that depends on @p.
6080 void
6081 record_dependant_types(const offset_pair_type& p,
6082 const offset_pair_vector_type& dependant_types)
6083 {
6084 for (auto type_pair : dependant_types)
6085 dependant_types_[type_pair].push_back(p);
6086 }
6087
6088 /// Record a comparison pair as being redundant.
6089 ///
6090 ///
6091 /// @param p the comparison pair to record as redundant.
6092 void
6093 record_redundant_type_die_pair(const offset_pair_type& p)
6094 {
6095 offset_pair_vector_type dependant_types;
6096 get_pairs_that_depend_on(p, dependant_types);
6097
6098 // First, record the relationship "p -> [pairs that depend on p]".
6099 auto it = redundant_types_.find(p);
6100 if (it == redundant_types_.end())
6101 {
6102 auto entry = std::make_pair(p, dependant_types);
6103 redundant_types_.insert(entry);
6104 }
6105 else
6106 it->second.insert(it->second.end(),
6107 dependant_types.begin(),
6108 dependant_types.end());
6109
6110 // For each dependant type pair, record the association:
6111 // dependant_pair --> [vect of redundant types]
6112 record_dependant_types(p, dependant_types);
6113 }
6114
6115 /// Test if a given pair has been detected as redundant.
6116 ///
6117 /// @param p the pair of DIEs to consider.
6118 ///
6119 /// @return iff @p is redundant.
6120 bool
6121 is_redundant(const offset_pair_type& p)
6122 {
6123 auto i = redundant_types_.find(p);
6124 if (i != redundant_types_.end())
6125 return true;
6126 return false;
6127 }
6128
6129 /// Test if a given pair is dependant on at least a redundant type.
6130 ///
6131 /// @param p the pair to consider.
6132 ///
6133 /// @return true iff @p depends on a redundant type.
6134 bool
6135 depends_on_redundant_types(const offset_pair_type& p)
6136 {
6137 auto i = dependant_types_.find(p);
6138 if (i == dependant_types_.end())
6139 return false;
6140 return true;
6141 }
6142
6143 /// Remove a redundant pair from the system.
6144 ///
6145 /// This needs updating the system to also remove the dependant
6146 /// types that depend on the redundant pair (if they depend only on
6147 /// that redundant pair).
6148 ///
6149 /// @param p the pair to consider.
6150 ///
6151 /// @param erase_canonical_die_offset if true then erase the cached
6152 /// comparison results for the redundant pair and its dependant
6153 /// types.
6154 void
6155 erase_redundant_type_pair_entry(const offset_pair_type& p,
6156 bool erase_cached_results = false)
6157 {
6158 // First, update the dependant types that depend on the redundant
6159 // type pair
6160 auto redundant_type = redundant_types_.find(p);
6161 if (redundant_type != redundant_types_.end())
6162 {
6163 for (auto dependant_type : redundant_type->second)
6164 {
6165 // Each dependant_type depends on the redundant type 'p',
6166 // among others.
6167 auto dependant_types_it = dependant_types_.find(dependant_type);
6168 ABG_ASSERT(dependant_types_it != dependant_types_.end());
6169 // Erase the redundant type 'p' from the redundant types
6170 // that dependant_type depends on.
6171 {
6172 auto i = dependant_types_it->second.begin();
6173 for (; i!= dependant_types_it->second.end();++i)
6174 if (*i == p)
6175 break;
6176 if (i != dependant_types_it->second.end())
6177 dependant_types_it->second.erase(i);
6178 }
6179 // If the dependant type itself doesn't depend on ANY
6180 // redundant type anymore, then remove the depend type
6181 // from the map of the dependant types.
6182 if (dependant_types_it->second.empty())
6183 {
6184 if (erase_cached_results)
6185 rdr_.die_comparison_results_.erase(dependant_type);
6186 dependant_types_.erase(dependant_types_it);
6187 }
6188 }
6189 }
6190 if (erase_cached_results)
6191 rdr_.die_comparison_results_.erase(p);
6192 redundant_types_.erase(p);
6193 }
6194
6195 /// If a comparison pair has been detected as redundant, stop
6196 /// tracking it as well as its dependant pairs. That will
6197 /// essentially make it impossible to reset/cancel the canonical
6198 /// propagated types for those depdant pairs, but will also save
6199 /// ressources.
6200 ///
6201 /// @param p the comparison pair to consider.
6202 void
6203 confirm_canonical_propagated_type(const offset_pair_type& p)
6204 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
6205
6206 /// Walk the types that depend on a comparison pair and cancel their
6207 /// canonical-propagate-type, that means remove their canonical
6208 /// types and mark them as not being canonically-propagated. Also,
6209 /// erase their cached comparison results that was likely set to
6210 /// COMPARISON_RESULT_UNKNOWN.
6211 ///
6212 /// @param p the pair to consider.
6213 void
6214 cancel_canonical_propagated_type(const offset_pair_type& p)
6215 {
6216 offset_pair_set_type dependant_types;
6217 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
6218 for (auto dependant_type : dependant_types)
6219 {
6220 // If this dependant type was canonical-type-propagated then
6221 // erase that canonical type.
6222 if (rdr_.propagated_types_.find(dependant_type)
6223 != rdr_.propagated_types_.end())
6224 {
6225 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
6226 dependant_type.first.source_,
6227 /*die_as_type=*/true);
6228 rdr_.propagated_types_.erase(dependant_type);
6229 rdr_.cancelled_propagation_count_++;
6230 }
6231 // Update the cached result. We know the comparison result
6232 // must now be different.
6233 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
6234 if (comp_result_it != rdr_.die_comparison_results_.end())
6235 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6236 }
6237
6238 // Update the cached result of the root type to cancel too.
6239 auto comp_result_it = rdr_.die_comparison_results_.find(p);
6240 if (comp_result_it != rdr_.die_comparison_results_.end())
6241 {
6242 // At this point, the result of p is either
6243 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
6244 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
6245 // Make sure it's the cached result is now
6246 // COMPARISON_RESULT_DIFFERENT.
6247 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
6248 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6249 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
6250 }
6251
6252 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
6253 {
6254 rdr_.erase_canonical_die_offset(p.first.offset_,
6255 p.first.source_,
6256 /*die_as_type=*/true);
6257 rdr_.propagated_types_.erase(p);
6258 rdr_.cancelled_propagation_count_++;
6259 }
6260 }
6261
6262 /// Get the set of comparison pairs that depend on a given pair.
6263 ///
6264 /// @param p the pair to consider.
6265 ///
6266 /// @param result this is set to the pairs that depend on @p, iff
6267 /// the function returned true.
6268 ///
6269 /// @param transitive_closure if set to true, the transitive closure
6270 /// of the @result is set to it.
6271 ///
6272 /// @return true iff @result could be filled with the dependant
6273 /// types.
6274 bool
6275 get_dependant_types(const offset_pair_type& p,
6276 offset_pair_set_type& result,
6277 bool transitive_closure = false)
6278 {
6279 auto i = redundant_types_.find(p);
6280 if (i != redundant_types_.end())
6281 {
6282 for (auto dependant_type : i->second)
6283 if (result.find(dependant_type) == result.end())
6284 {
6285 result.insert(dependant_type);
6286 if (transitive_closure)
6287 get_dependant_types(p, result, /*transitive_closure=*/true);
6288 }
6289 return true;
6290 }
6291 return false;
6292 }
6293}; // end struct offset_pairs_stack_type
6294
6296build_ir_node_from_die(reader& rdr,
6297 Dwarf_Die* die,
6298 scope_decl* scope,
6299 bool called_from_public_decl,
6300 size_t where_offset,
6301 bool is_declaration_only = true,
6302 bool is_required_decl_spec = false);
6303
6305build_ir_node_from_die(reader& rdr,
6306 Dwarf_Die* die,
6307 bool called_from_public_decl,
6308 size_t where_offset);
6309
6310static decl_base_sptr
6311build_ir_node_for_void_type(reader& rdr);
6312
6314build_ir_node_for_void_pointer_type(reader& rdr);
6315
6316static class_decl_sptr
6317add_or_update_class_type(reader& rdr,
6318 Dwarf_Die* die,
6319 scope_decl* scope,
6320 bool is_struct,
6321 class_decl_sptr klass,
6322 bool called_from_public_decl,
6323 size_t where_offset,
6324 bool is_declaration_only);
6325
6326static union_decl_sptr
6327add_or_update_union_type(reader& rdr,
6328 Dwarf_Die* die,
6329 scope_decl* scope,
6330 union_decl_sptr union_type,
6331 bool called_from_public_decl,
6332 size_t where_offset,
6333 bool is_declaration_only);
6334
6335static decl_base_sptr
6336build_ir_node_for_void_type(reader& rdr);
6337
6338static decl_base_sptr
6339build_ir_node_for_variadic_parameter_type(reader &rdr);
6340
6341static function_decl_sptr
6342build_function_decl(reader& rdr,
6343 Dwarf_Die* die,
6344 size_t where_offset,
6346
6347static bool
6348function_is_suppressed(const reader& rdr,
6349 const scope_decl* scope,
6350 Dwarf_Die *function_die,
6351 bool is_declaration_only);
6352
6353static function_decl_sptr
6354build_or_get_fn_decl_if_not_suppressed(reader& rdr,
6355 scope_decl *scope,
6356 Dwarf_Die *die,
6357 size_t where_offset,
6358 bool is_declaration_only,
6360
6361static var_decl_sptr
6362build_var_decl(reader& rdr,
6363 Dwarf_Die *die,
6364 size_t where_offset,
6365 var_decl_sptr result = var_decl_sptr());
6366
6367static var_decl_sptr
6368build_or_get_var_decl_if_not_suppressed(reader& rdr,
6369 scope_decl *scope,
6370 Dwarf_Die *die,
6371 size_t where_offset,
6372 bool is_declaration_only,
6374 bool is_required_decl_spec = false);
6375static bool
6376variable_is_suppressed(const reader& rdr,
6377 const scope_decl* scope,
6378 Dwarf_Die *variable_die,
6379 bool is_declaration_only,
6380 bool is_required_decl_spec = false);
6381
6382static void
6383finish_member_function_reading(Dwarf_Die* die,
6384 const function_decl_sptr& f,
6385 const class_or_union_sptr klass,
6386 reader& rdr);
6387
6388/// Test if a given DIE is anonymous
6389///
6390/// @param die the DIE to consider.
6391///
6392/// @return true iff @p die is anonymous.
6393static bool
6394die_is_anonymous(const Dwarf_Die* die)
6395{
6396 Dwarf_Attribute attr;
6397 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
6398 return true;
6399 return false;
6400}
6401
6402/// Test if a DIE is an anonymous data member, aka, "unnamed field".
6403///
6404/// Unnamed fields are specified at
6405/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
6406///
6407/// @param die the DIE to consider.
6408///
6409/// @return true iff @p die is an anonymous data member.
6410static bool
6411die_is_anonymous_data_member(const Dwarf_Die* die)
6412{
6413 if (!die
6414 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
6415 || !die_name(die).empty())
6416 return false;
6417
6418 Dwarf_Die type_die;
6419 if (!die_die_attribute(die, DW_AT_type, type_die))
6420 return false;
6421
6422 if (dwarf_tag(&type_die) != DW_TAG_structure_type
6423 && dwarf_tag(&type_die) != DW_TAG_union_type)
6424 return false;
6425
6426 return true;
6427}
6428
6429/// Get the value of an attribute that is supposed to be a string, or
6430/// an empty string if the attribute could not be found.
6431///
6432/// @param die the DIE to get the attribute value from.
6433///
6434/// @param attr_name the attribute name. Must come from dwarf.h and
6435/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6436///
6437/// @return the string representing the value of the attribute, or an
6438/// empty string if no string attribute could be found.
6439static string
6440die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
6441{
6442 if (!die)
6443 return "";
6444
6445 Dwarf_Attribute attr;
6446 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6447 return "";
6448
6449 const char* str = dwarf_formstring(&attr);
6450 return str ? str : "";
6451}
6452
6453/// Get the value of an attribute that is supposed to be a string, or
6454/// an empty string if the attribute could not be found.
6455///
6456/// @param die the DIE to get the attribute value from.
6457///
6458/// @param attr_name the attribute name. Must come from dwarf.h and
6459/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6460///
6461/// @return the char* representing the value of the attribute, or an
6462/// empty string if no string attribute could be found.
6463static const char*
6464die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
6465{
6466 if (!die)
6467 return nullptr;
6468
6469 Dwarf_Attribute attr;
6470 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6471 return nullptr;
6472
6473 const char* str = dwarf_formstring(&attr);
6474 return str;
6475}
6476
6477/// Get the value of an attribute that is supposed to be an unsigned
6478/// constant.
6479///
6480/// @param die the DIE to read the information from.
6481///
6482/// @param attr_name the DW_AT_* name of the attribute. Must come
6483/// from dwarf.h and be an enumerator representing an attribute like,
6484/// e.g, DW_AT_decl_line.
6485///
6486///@param cst the output parameter that is set to the value of the
6487/// attribute @p attr_name. This parameter is set iff the function
6488/// return true.
6489///
6490/// @return true if there was an attribute of the name @p attr_name
6491/// and with a value that is a constant, false otherwise.
6492static bool
6493die_unsigned_constant_attribute(const Dwarf_Die* die,
6494 unsigned attr_name,
6495 uint64_t& cst)
6496{
6497 if (!die)
6498 return false;
6499
6500 Dwarf_Attribute attr;
6501 Dwarf_Word result = 0;
6502 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6503 || dwarf_formudata(&attr, &result))
6504 return false;
6505
6506 cst = result;
6507 return true;
6508}
6509
6510/// Read a signed constant value from a given attribute.
6511///
6512/// The signed constant expected must be of constant form.
6513///
6514/// @param die the DIE to get the attribute from.
6515///
6516/// @param attr_name the attribute name.
6517///
6518/// @param cst the resulting signed constant read.
6519///
6520/// @return true iff a signed constant attribute of the name @p
6521/// attr_name was found on the DIE @p die.
6522static bool
6523die_signed_constant_attribute(const Dwarf_Die *die,
6524 unsigned attr_name,
6525 int64_t& cst)
6526{
6527 if (!die)
6528 return false;
6529
6530 Dwarf_Attribute attr;
6531 Dwarf_Sword result = 0;
6532 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6533 || dwarf_formsdata(&attr, &result))
6534 return false;
6535
6536 cst = result;
6537 return true;
6538}
6539
6540/// Read the value of a constant attribute that is either signed or
6541/// unsigned into a array_type_def::subrange_type::bound_value value.
6542///
6543/// The bound_value instance will capture the actual signedness of the
6544/// read attribute.
6545///
6546/// @param die the DIE from which to read the value of the attribute.
6547///
6548/// @param attr_name the attribute name to consider.
6549///
6550/// @param is_signed true if the attribute value has to read as
6551/// signed.
6552///
6553/// @param value the resulting value read from attribute @p attr_name
6554/// on DIE @p die.
6555///
6556/// @return true iff DIE @p die has an attribute named @p attr_name
6557/// with a constant value.
6558static bool
6559die_constant_attribute(const Dwarf_Die *die,
6560 unsigned attr_name,
6561 bool is_signed,
6562 array_type_def::subrange_type::bound_value &value)
6563{
6564 if (!is_signed)
6565 {
6566 uint64_t l = 0;
6567 if (!die_unsigned_constant_attribute(die, attr_name, l))
6568 return false;
6569 value.set_unsigned(l);
6570 }
6571 else
6572 {
6573 int64_t l = 0;
6574 if (!die_signed_constant_attribute(die, attr_name, l))
6575 return false;
6576 value.set_signed(l);
6577 }
6578 return true;
6579}
6580
6581/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6582///
6583/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6584/// enum in dwarf.h so we have to use an unsigned int for the form,
6585/// grrr.
6586///
6587/// @param form the form to consider.
6588///
6589/// @return true iff @p form is DW_FORM_strx{1,4}.
6590static bool
6591form_is_DW_FORM_strx(unsigned form)
6592{
6593 if (form)
6594 {
6595#if defined HAVE_DW_FORM_strx1 \
6596 && defined HAVE_DW_FORM_strx2 \
6597 && defined HAVE_DW_FORM_strx3 \
6598 && defined HAVE_DW_FORM_strx4
6599 if (form == DW_FORM_strx1
6600 || form == DW_FORM_strx2
6601 || form == DW_FORM_strx3
6602 ||form == DW_FORM_strx4)
6603 return true;
6604#endif
6605 }
6606 return false;
6607}
6608
6609/// Test if a given DWARF form is DW_FORM_line_strp.
6610///
6611/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6612/// enum in dwarf.h so we have to use an unsigned int for the form,
6613/// grrr.
6614///
6615/// @param form the form to consider.
6616///
6617/// @return true iff @p form is DW_FORM_line_strp.
6618static bool
6619form_is_DW_FORM_line_strp(unsigned form)
6620{
6621 if (form)
6622 {
6623#if defined HAVE_DW_FORM_line_strp
6624 if (form == DW_FORM_line_strp)
6625 return true;
6626#endif
6627 }
6628 return false;
6629}
6630
6631/// Get the value of a DIE attribute; that value is meant to be a
6632/// flag.
6633///
6634/// @param die the DIE to get the attribute from.
6635///
6636/// @param attr_name the DW_AT_* name of the attribute. Must come
6637/// from dwarf.h and be an enumerator representing an attribute like,
6638/// e.g, DW_AT_external.
6639///
6640/// @param flag the output parameter to store the flag value into.
6641/// This is set iff the function returns true.
6642///
6643/// @param recursively if true, the function looks through the
6644/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6645/// all the way down to the initial DIE that is cloned and look on
6646/// that DIE to see if it has the @p attr_name attribute.
6647///
6648/// @return true if the DIE has a flag attribute named @p attr_name,
6649/// false otherwise.
6650static bool
6651die_flag_attribute(const Dwarf_Die* die,
6652 unsigned attr_name,
6653 bool& flag,
6654 bool recursively = true)
6655{
6656 Dwarf_Attribute attr;
6657 if (recursively
6658 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6659 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6660 return false;
6661
6662 bool f = false;
6663 if (dwarf_formflag(&attr, &f))
6664 return false;
6665
6666 flag = f;
6667 return true;
6668}
6669
6670/// Get the mangled name from a given DIE.
6671///
6672/// @param die the DIE to read the mangled name from.
6673///
6674/// @return the mangled name if it's present in the DIE, or just an
6675/// empty string if it's not.
6676static string
6677die_linkage_name(const Dwarf_Die* die)
6678{
6679 if (!die)
6680 return "";
6681
6682 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6683 if (linkage_name.empty())
6684 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6685 return linkage_name;
6686}
6687
6688/// Get the file path that is the value of the DW_AT_decl_file
6689/// attribute on a given DIE, if the DIE is a decl DIE having that
6690/// attribute.
6691///
6692/// @param die the DIE to consider.
6693///
6694/// @return a string containing the file path that is the logical
6695/// value of the DW_AT_decl_file attribute. If the DIE @p die
6696/// doesn't have a DW_AT_decl_file attribute, then the return value is
6697/// just an empty string.
6698static string
6699die_decl_file_attribute(const Dwarf_Die* die)
6700{
6701 if (!die)
6702 return "";
6703
6704 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6705
6706 return str ? str : "";
6707}
6708
6709/// Get the value of an attribute which value is supposed to be a
6710/// reference to a DIE.
6711///
6712/// @param die the DIE to read the value from.
6713///
6714/// @param attr_name the DW_AT_* attribute name to read.
6715///
6716/// @param result the DIE resulting from reading the attribute value.
6717/// This is set iff the function returns true.
6718///
6719/// @param recursively if true, the function looks through the
6720/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6721/// all the way down to the initial DIE that is cloned and look on
6722/// that DIE to see if it has the @p attr_name attribute.
6723///
6724/// @return true if the DIE @p die contains an attribute named @p
6725/// attr_name that is a DIE reference, false otherwise.
6726static bool
6727die_die_attribute(const Dwarf_Die* die,
6728 unsigned attr_name,
6729 Dwarf_Die& result,
6730 bool recursively)
6731{
6732 Dwarf_Attribute attr;
6733 if (recursively
6734 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6735 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6736 return false;
6737
6738 return dwarf_formref_die(&attr, &result);
6739}
6740
6741/// Get the DIE that is the "origin" of the current one.
6742///
6743/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6744/// attribute. Those DIEs represent a concrete instance of an
6745/// abstract entity. The concrete instance can be a concrete instance
6746/// of an inline function, or the concrete implementation of an
6747/// abstract interface. On both cases, we call the abstract instance
6748/// from which the concrete instance derives the "origin".
6749///
6750/// This function returns the ultimate origin DIE of a given DIE by
6751/// following the chain of its DW_AT_abstract_origin and
6752/// DW_AT_specification attributes.
6753///
6754/// @param die the DIE to consider.
6755///
6756/// @param origin_die this is an output parameter that is set by this
6757/// function to the resulting origin DIE iff the function returns
6758/// true.
6759///
6760/// @return true iff the function actually found an origin DIE and
6761/// set it to the @p origin_die parameter.
6762static bool
6763die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6764{
6765 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6766 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6767 {
6768 while (die_die_attribute(&origin_die,
6769 DW_AT_specification,
6770 origin_die, true)
6771 || die_die_attribute(&origin_die,
6772 DW_AT_abstract_origin,
6773 origin_die, true))
6774 {
6775 // Keep looking for the origin die ...
6776 ;
6777 }
6778 return true;
6779 }
6780 return false;
6781}
6782
6783/// Test if a subrange DIE indirectly references another subrange DIE
6784/// through a given attribute.
6785///
6786/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6787/// attribute be a reference to either a data member or a variable
6788/// which type is itself a DW_TAG_subrange_type. This latter subrange
6789/// DIE is said to be "indirectly referenced" by the former subrange
6790/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6791/// the value we want for the DW_AT_upper_bound of the former.
6792///
6793/// This function tests if the former subrange DIE does indirectly
6794/// reference another subrange DIE through a given attribute (not
6795/// necessarily DW_AT_upper_bound).
6796///
6797/// @param die the DIE to consider. Note that It must be a
6798/// DW_TAG_subrange_type.
6799///
6800/// @param attr_name the name of the attribute to look through for the
6801/// indirectly referenced subrange DIE.
6802///
6803/// @param referenced_subrange if the function returns true, then the
6804/// argument of this parameter is set to the indirectly referenced
6805/// DW_TAG_subrange_type DIE.
6806///
6807/// @return true iff @p DIE indirectly references a subrange DIE
6808/// through the attribute @p attr_name.
6809static bool
6810subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6811 unsigned attr_name,
6812 Dwarf_Die& referenced_subrange)
6813{
6814 bool result = false;
6815
6816 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6817 return result;
6818
6819 Dwarf_Die referenced_die;
6820 if (die_die_attribute(die, attr_name, referenced_die))
6821 {
6822 unsigned tag = dwarf_tag(&referenced_die);
6823 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6824 {
6825 Dwarf_Die type_die;
6826 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6827 {
6828 tag = dwarf_tag(&type_die);
6829 if (tag == DW_TAG_subrange_type)
6830 {
6831 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6832 result = true;
6833 }
6834 }
6835 }
6836 }
6837 return result;
6838}
6839
6840/// Return the bound value of subrange die by looking at an indirectly
6841/// referenced subrange DIE.
6842///
6843/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6844/// attribute be a reference to either a data member or a variable
6845/// which type is itself a DW_TAG_subrange_type. This latter subrange
6846/// DIE is said to be "indirectly referenced" by the former subrange
6847/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6848/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6849///
6850/// This function gets the DW_AT_{lower,upper}_bound value of a
6851/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6852/// the indirectly referenced subrange type, if it exists.
6853///
6854/// @param die the subrange DIE to consider.
6855///
6856/// @param attr_name the name of the attribute to consider, typically,
6857/// DW_AT_{lower,upper}_bound.
6858///
6859/// @param v the found value, iff this function returned true.
6860///
6861/// @param is_signed, this is set to true if @p v is signed. This
6862/// parameter is set at all only if the function returns true.
6863///
6864/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6865/// indirectly referenced subrange type.
6866static bool
6867subrange_die_indirect_bound_value(const Dwarf_Die *die,
6868 unsigned attr_name,
6869 array_type_def::subrange_type::bound_value& v,
6870 bool& is_signed)
6871{
6872 bool result = false;
6873
6874 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6875 return result;
6876
6877 Dwarf_Die subrange_die;
6878 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6879 subrange_die))
6880 {
6881 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6882 result = true;
6883 }
6884 return result;
6885}
6886
6887/// Read and return an addresss class attribute from a given DIE.
6888///
6889/// @param die the DIE to consider.
6890///
6891/// @param attr_name the name of the address class attribute to read
6892/// the value from.
6893///
6894/// @param the resulting address.
6895///
6896/// @return true iff the attribute could be read, was of the expected
6897/// address class and could thus be translated into the @p result.
6898static bool
6899die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6900{
6901 Dwarf_Attribute attr;
6902 if (!dwarf_attr_integrate(die, attr_name, &attr))
6903 return false;
6904 return dwarf_formaddr(&attr, &result) == 0;
6905}
6906
6907/// Returns the source location associated with a decl DIE.
6908///
6909/// @param rdr the @ref reader to use.
6910///
6911/// @param die the DIE the read the source location from.
6912///
6913/// @return the location associated with @p die.
6914static location
6915die_location(const reader& rdr, const Dwarf_Die* die)
6916{
6917 if (!die)
6918 return location();
6919
6920 string file = die_decl_file_attribute(die);
6921 uint64_t line = 0;
6922 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6923
6924 if (!file.empty() && line != 0)
6925 {
6926 translation_unit_sptr tu = rdr.cur_transl_unit();
6927 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6928 return l;
6929 }
6930 return location();
6931}
6932
6933/// Return a copy of the name of a DIE.
6934///
6935/// @param die the DIE to consider.
6936///
6937/// @return a copy of the name of the DIE.
6938static string
6939die_name(const Dwarf_Die* die)
6940{
6941 string name = die_string_attribute(die, DW_AT_name);
6942 return name;
6943}
6944
6945/// Return the location, the name and the mangled name of a given DIE.
6946///
6947/// @param rdr the DWARF reader to use.
6948///
6949/// @param die the DIE to read location and names from.
6950///
6951/// @param loc the location output parameter to set.
6952///
6953/// @param name the name output parameter to set.
6954///
6955/// @param linkage_name the linkage_name output parameter to set.
6956static void
6957die_loc_and_name(const reader& rdr,
6958 Dwarf_Die* die,
6959 location& loc,
6960 string& name,
6961 string& linkage_name)
6962{
6963 loc = die_location(rdr, die);
6964 name = die_name(die);
6965 linkage_name = die_linkage_name(die);
6966}
6967
6968/// Return the name and the mangled name of a given DIE.
6969///
6970/// @param die the DIE to read location and names from.
6971///
6972/// @param name the name output parameter to set.
6973///
6974/// @param linkage_name the linkage_name output parameter to set.
6975static void
6976die_name_and_linkage_name(const Dwarf_Die* die,
6977 string& name,
6978 string& linkage_name)
6979{
6980 name = die_name(die);
6981 linkage_name = die_linkage_name(die);
6982}
6983
6984/// Get the size of a (type) DIE as the value for the parameter
6985/// DW_AT_byte_size or DW_AT_bit_size.
6986///
6987/// @param die the DIE to read the information from.
6988///
6989/// @param size the resulting size in bits. This is set iff the
6990/// function return true.
6991///
6992/// @return true if the size attribute was found.
6993static bool
6994die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6995{
6996 if (!die)
6997 return false;
6998
6999 uint64_t byte_size = 0, bit_size = 0;
7000
7001 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
7002 {
7003 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
7004 return false;
7005 }
7006 else
7007 bit_size = byte_size * 8;
7008
7009 size = bit_size;
7010
7011 return true;
7012}
7013
7014/// Get the access specifier (from the DW_AT_accessibility attribute
7015/// value) of a given DIE.
7016///
7017/// @param die the DIE to consider.
7018///
7019/// @param access the resulting access. This is set iff the function
7020/// returns true.
7021///
7022/// @return bool if the DIE contains the DW_AT_accessibility die.
7023static bool
7024die_access_specifier(Dwarf_Die * die, access_specifier& access)
7025{
7026 if (!die)
7027 return false;
7028
7029 uint64_t a = 0;
7030 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
7031 return false;
7032
7033 access_specifier result = private_access;
7034
7035 switch (a)
7036 {
7037 case private_access:
7038 result = private_access;
7039 break;
7040
7041 case protected_access:
7042 result = protected_access;
7043 break;
7044
7045 case public_access:
7046 result = public_access;
7047 break;
7048
7049 default:
7050 break;
7051 }
7052
7053 access = result;
7054 return true;
7055}
7056
7057/// Test whether a given DIE represents a decl that is public. That
7058/// is, one with the DW_AT_external attribute set.
7059///
7060/// @param die the DIE to consider for testing.
7061///
7062/// @return true if a DW_AT_external attribute is present and its
7063/// value is set to the true; return false otherwise.
7064static bool
7065die_is_public_decl(const Dwarf_Die* die)
7066{
7067 if (!die)
7068 return false;
7069 bool is_public = false;
7070
7071 // If this is a DW_TAG_subprogram DIE, look for the
7072 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
7073 // then it's public. In all other cases, this should return false.
7074
7075 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7076 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
7077 die_flag_attribute(die, DW_AT_external, is_public);
7078 else if (tag == DW_TAG_namespace)
7079 {
7080 string name = die_name(die);
7081 is_public = !name.empty();
7082 }
7083
7084 return is_public;
7085}
7086
7087/// Test if a DIE is effectively public.
7088///
7089/// This is meant to return true when either the DIE is public or when
7090/// it's a variable DIE that is at (global) namespace level.
7091///
7092/// @return true iff either the DIE is public or is a variable DIE
7093/// that is at (global) namespace level.
7094static bool
7095die_is_effectively_public_decl(const reader& rdr,
7096 const Dwarf_Die* die)
7097{
7098 if (die_is_public_decl(die))
7099 return true;
7100
7101 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7102 if (tag == DW_TAG_variable || tag == DW_TAG_member)
7103 {
7104 // The DIE is a variable.
7105 Dwarf_Die parent_die;
7106 size_t where_offset = 0;
7107 if (!get_parent_die(rdr, die, parent_die, where_offset))
7108 return false;
7109
7110 tag = dwarf_tag(&parent_die);
7111 if (tag == DW_TAG_compile_unit
7112 || tag == DW_TAG_partial_unit
7113 || tag == DW_TAG_type_unit)
7114 // The DIE is at global scope.
7115 return true;
7116
7117 if (tag == DW_TAG_namespace)
7118 {
7119 string name = die_name(&parent_die);
7120 if (name.empty())
7121 // The DIE at unnamed namespace scope, so it's not public.
7122 return false;
7123 // The DIE is at namespace scope.
7124 return true;
7125 }
7126 }
7127 return false;
7128}
7129
7130/// Test whether a given DIE represents a declaration-only DIE.
7131///
7132/// That is, if the DIE has the DW_AT_declaration flag set.
7133///
7134/// @param die the DIE to consider.
7135//
7136/// @return true if a DW_AT_declaration is present, false otherwise.
7137static bool
7138die_is_declaration_only(Dwarf_Die* die)
7139{
7140 bool is_declaration = false;
7141 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
7142 if (is_declaration && (!die_has_size_attribute(die)
7143 || !die_has_children(die)))
7144 return true;
7145 return false;
7146}
7147
7148/// Test if a DIE is for a function decl.
7149///
7150/// @param die the DIE to consider.
7151///
7152/// @return true iff @p die represents a function decl.
7153static bool
7154die_is_function_decl(const Dwarf_Die *die)
7155{
7156 if (!die)
7157 return false;
7158
7159 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7160 if (tag == DW_TAG_subprogram)
7161 return true;
7162 return false;
7163}
7164
7165/// Test if a DIE is for a variable decl.
7166///
7167/// @param die the DIE to consider.
7168///
7169/// @return true iff @p die represents a variable decl.
7170static bool
7171die_is_variable_decl(const Dwarf_Die *die)
7172{
7173 if (!die)
7174 return false;
7175
7176 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7177 if (tag == DW_TAG_variable)
7178 return true;
7179 return false;
7180}
7181
7182/// Test if a DIE has size attribute.
7183///
7184/// @param die the DIE to consider.
7185///
7186/// @return true if the DIE has a size attribute.
7187static bool
7188die_has_size_attribute(const Dwarf_Die *die)
7189{
7190 uint64_t s;
7191 if (die_size_in_bits(die, s))
7192 return true;
7193 return false;
7194}
7195
7196/// Test that a DIE has no child DIE.
7197///
7198/// @param die the DIE to consider.
7199///
7200/// @return true iff @p die has no child DIE.
7201static bool
7202die_has_no_child(const Dwarf_Die *die)
7203{
7204 if (!die)
7205 return true;
7206
7207 Dwarf_Die child;
7208 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7209 return false;
7210 return true;
7211}
7212
7213/// Test whether a given DIE represents a declaration-only DIE.
7214///
7215/// That is, if the DIE has the DW_AT_declaration flag set.
7216///
7217/// @param die the DIE to consider.
7218//
7219/// @return true if a DW_AT_declaration is present, false otherwise.
7220static bool
7221die_is_declaration_only(const Dwarf_Die* die)
7222{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
7223
7224/// Tests whether a given DIE is artificial.
7225///
7226/// @param die the test to test for.
7227///
7228/// @return true if the DIE is artificial, false otherwise.
7229static bool
7230die_is_artificial(Dwarf_Die* die)
7231{
7232 bool is_artificial;
7233 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
7234}
7235
7236///@return true if a tag represents a type, false otherwise.
7237///
7238///@param tag the tag to consider.
7239static bool
7240is_type_tag(unsigned tag)
7241{
7242 bool result = false;
7243
7244 switch (tag)
7245 {
7246 case DW_TAG_array_type:
7247 case DW_TAG_class_type:
7248 case DW_TAG_enumeration_type:
7249 case DW_TAG_pointer_type:
7250 case DW_TAG_reference_type:
7251 case DW_TAG_string_type:
7252 case DW_TAG_structure_type:
7253 case DW_TAG_subroutine_type:
7254 case DW_TAG_typedef:
7255 case DW_TAG_union_type:
7256 case DW_TAG_ptr_to_member_type:
7257 case DW_TAG_set_type:
7258 case DW_TAG_subrange_type:
7259 case DW_TAG_base_type:
7260 case DW_TAG_const_type:
7261 case DW_TAG_file_type:
7262 case DW_TAG_packed_type:
7263 case DW_TAG_thrown_type:
7264 case DW_TAG_volatile_type:
7265 case DW_TAG_restrict_type:
7266 case DW_TAG_interface_type:
7267 case DW_TAG_unspecified_type:
7268 case DW_TAG_shared_type:
7269 case DW_TAG_rvalue_reference_type:
7270 case DW_TAG_coarray_type:
7271 case DW_TAG_atomic_type:
7272 case DW_TAG_immutable_type:
7273 result = true;
7274 break;
7275
7276 default:
7277 result = false;
7278 break;
7279 }
7280
7281 return result;
7282}
7283
7284/// Test if a given DIE is a type whose canonical type is to be
7285/// propagated during DIE canonicalization
7286///
7287/// This is a sub-routine of compare_dies.
7288///
7289/// @param tag the tag of the DIE to consider.
7290///
7291/// @return true iff the DIE of tag @p tag is can see its canonical
7292/// type be propagated during the type comparison that happens during
7293/// DIE canonicalization.
7294static bool
7295is_canon_type_to_be_propagated_tag(unsigned tag)
7296{
7297 bool result = false;
7298
7299 switch (tag)
7300 {
7301 case DW_TAG_class_type:
7302 case DW_TAG_structure_type:
7303 case DW_TAG_union_type:
7304 case DW_TAG_subroutine_type:
7305 case DW_TAG_subprogram:
7306 result = true;
7307 break;
7308
7309 default:
7310 result = false;
7311 break;
7312 }
7313
7314 return result;
7315}
7316
7317/// Test if a given kind of DIE ought to have its comparison result
7318/// cached by compare_dies, so that subsequent invocations of
7319/// compare_dies can be faster.
7320///
7321/// @param tag the tag of the DIE to consider.
7322///
7323/// @return true iff DIEs of the tag @p tag ought to have its
7324/// comparison results cached.
7325static bool
7326type_comparison_result_to_be_cached(unsigned tag)
7327{
7328 bool r = false;
7329 switch (tag)
7330 {
7331 case DW_TAG_class_type:
7332 case DW_TAG_structure_type:
7333 case DW_TAG_union_type:
7334 case DW_TAG_subroutine_type:
7335 case DW_TAG_subprogram:
7336 r = true;
7337 break;
7338
7339 default:
7340 r = false;
7341 break;
7342 }
7343 return r;
7344}
7345
7346/// Cache the result of comparing to type DIEs.
7347///
7348/// @param rdr the context to consider.
7349///
7350/// @param tag the tag of the DIEs to consider.
7351///
7352/// @param p the offsets of the pair of DIEs being compared.
7353///
7354/// @param result the comparison result to be cached.
7355static bool
7356maybe_cache_type_comparison_result(const reader& rdr,
7357 int tag,
7358 const offset_pair_type& p,
7359 comparison_result result)
7360{
7361 if (!type_comparison_result_to_be_cached(tag)
7362 || (result != COMPARISON_RESULT_EQUAL
7363 && result != COMPARISON_RESULT_DIFFERENT))
7364 return false;
7365
7366 rdr.die_comparison_results_[p] = result;
7367
7368 return true;
7369
7370}
7371
7372/// Get the cached result of the comparison of a pair of DIEs.
7373///
7374/// @param rdr the context to consider.
7375///
7376/// @param tag the tag of the pair of DIEs to consider.
7377///
7378/// @param p the offsets of the pair of DIEs to consider.
7379///
7380/// @param result out parameter set to the cached result of the
7381/// comparison of @p p if it has been found.
7382///
7383/// @return true iff a cached result for the comparisonof @p has been
7384/// found and set into @p result.
7385static bool
7386get_cached_type_comparison_result(const reader& rdr,
7387 const offset_pair_type& p,
7388 comparison_result& result)
7389{
7390 auto i = rdr.die_comparison_results_.find(p);
7391 if (i != rdr.die_comparison_results_.end())
7392 {
7393 result = i->second;
7394 return true;
7395 }
7396 return false;
7397}
7398
7399/// Get the cached result of the comparison of a pair of DIEs, if the
7400/// kind of DIEs ought to have its comparison results cached.
7401///
7402/// @param rdr the context to consider.
7403///
7404/// @param tag the tag of the pair of DIEs to consider.
7405///
7406/// @param p the offsets of the pair of DIEs to consider.
7407///
7408/// @param result out parameter set to the cached result of the
7409/// comparison of @p p if it has been found.
7410///
7411/// @return true iff a cached result for the comparisonof @p has been
7412/// found and set into @p result.
7413static bool
7414maybe_get_cached_type_comparison_result(const reader& rdr,
7415 int tag,
7416 const offset_pair_type& p,
7417 comparison_result& result)
7418{
7419 if (type_comparison_result_to_be_cached(tag))
7420 {
7421 // Types of this kind might have their comparison result cached
7422 // when they are not canonicalized. So let's see if we have a
7423 // cached comparison result.
7424 if (get_cached_type_comparison_result(rdr, p, result))
7425 return true;
7426 }
7427 return false;
7428}
7429
7430/// Test if a given DIE is to be canonicalized.
7431///
7432/// @param die the DIE to consider.
7433///
7434/// @return true iff @p die is to be canonicalized.
7435static bool
7436is_type_die_to_be_canonicalized(const Dwarf_Die *die)
7437{
7438 bool result = false;
7439 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7440
7441 if (!is_type_tag(tag))
7442 return false;
7443
7444 switch (tag)
7445 {
7446 case DW_TAG_class_type:
7447 case DW_TAG_structure_type:
7448 case DW_TAG_union_type:
7449 result = !die_is_declaration_only(die);
7450 break;
7451
7452 case DW_TAG_subroutine_type:
7453 case DW_TAG_subprogram:
7454 case DW_TAG_array_type:
7455 result = true;
7456
7457 default:
7458 break;
7459 }
7460
7461 return result;
7462}
7463
7464/// Test if a DIE tag represents a declaration.
7465///
7466/// @param tag the DWARF tag to consider.
7467///
7468/// @return true iff @p tag is for a declaration.
7469static bool
7470is_decl_tag(unsigned tag)
7471{
7472 switch (tag)
7473 {
7474 case DW_TAG_formal_parameter:
7475 case DW_TAG_imported_declaration:
7476 case DW_TAG_member:
7477 case DW_TAG_unspecified_parameters:
7478 case DW_TAG_subprogram:
7479 case DW_TAG_variable:
7480 case DW_TAG_namespace:
7481 case DW_TAG_GNU_template_template_param:
7482 case DW_TAG_GNU_template_parameter_pack:
7483 case DW_TAG_GNU_formal_parameter_pack:
7484 return true;
7485 }
7486 return false;
7487}
7488
7489/// Test if a DIE represents a type DIE.
7490///
7491/// @param die the DIE to consider.
7492///
7493/// @return true if @p die represents a type, false otherwise.
7494static bool
7495die_is_type(const Dwarf_Die* die)
7496{
7497 if (!die)
7498 return false;
7499 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7500}
7501
7502/// Test if a DIE represents a declaration.
7503///
7504/// @param die the DIE to consider.
7505///
7506/// @return true if @p die represents a decl, false otherwise.
7507static bool
7508die_is_decl(const Dwarf_Die* die)
7509{
7510 if (!die)
7511 return false;
7512 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7513}
7514
7515/// Test if a DIE represents a namespace.
7516///
7517/// @param die the DIE to consider.
7518///
7519/// @return true if @p die represents a namespace, false otherwise.
7520static bool
7521die_is_namespace(const Dwarf_Die* die)
7522{
7523 if (!die)
7524 return false;
7525 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7526}
7527
7528/// Test if a DIE has tag DW_TAG_unspecified_type.
7529///
7530/// @param die the DIE to consider.
7531///
7532/// @return true if @p die has tag DW_TAG_unspecified_type.
7533static bool
7534die_is_unspecified(Dwarf_Die* die)
7535{
7536 if (!die)
7537 return false;
7538 return (dwarf_tag(die) == DW_TAG_unspecified_type);
7539}
7540
7541/// Test if a DIE represents a void type.
7542///
7543/// @param die the DIE to consider.
7544///
7545/// @return true if @p die represents a void type, false otherwise.
7546static bool
7547die_is_void_type(Dwarf_Die* die)
7548{
7549 if (!die || dwarf_tag(die) != DW_TAG_base_type)
7550 return false;
7551
7552 string name = die_name(die);
7553 if (name == "void")
7554 return true;
7555
7556 return false;
7557}
7558
7559/// Test if a DIE represents a pointer type.
7560///
7561/// @param die the die to consider.
7562///
7563/// @return true iff @p die represents a pointer type.
7564static bool
7565die_is_pointer_type(const Dwarf_Die* die)
7566{
7567 if (!die)
7568 return false;
7569
7570 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7571 if (tag == DW_TAG_pointer_type)
7572 return true;
7573
7574 return false;
7575}
7576
7577/// Test if a DIE is for a pointer, reference or qualified type to
7578/// anonymous class or struct.
7579///
7580/// @param die the DIE to consider.
7581///
7582/// @return true iff @p is for a pointer, reference or qualified type
7583/// to anonymous class or struct.
7584static bool
7585pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7586{
7587 if (!die_is_pointer_array_or_reference_type(die)
7588 && !die_is_qualified_type(die))
7589 return false;
7590
7591 Dwarf_Die underlying_type_die;
7592 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7593 return false;
7594
7595 if (!die_is_class_type(&underlying_type_die))
7596 return false;
7597
7598 string name = die_name(&underlying_type_die);
7599
7600 return name.empty();
7601}
7602
7603/// Test if a DIE represents a reference type.
7604///
7605/// @param die the die to consider.
7606///
7607/// @return true iff @p die represents a reference type.
7608static bool
7609die_is_reference_type(const Dwarf_Die* die)
7610{
7611 if (!die)
7612 return false;
7613
7614 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7615 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7616 return true;
7617
7618 return false;
7619}
7620
7621/// Test if a DIE represents an array type.
7622///
7623/// @param die the die to consider.
7624///
7625/// @return true iff @p die represents an array type.
7626static bool
7627die_is_array_type(const Dwarf_Die* die)
7628{
7629 if (!die)
7630 return false;
7631
7632 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7633 if (tag == DW_TAG_array_type)
7634 return true;
7635
7636 return false;
7637}
7638
7639/// Test if a DIE represents a pointer, reference or array type.
7640///
7641/// @param die the die to consider.
7642///
7643/// @return true iff @p die represents a pointer or reference type.
7644static bool
7645die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7646{return (die_is_pointer_type(die)
7647 || die_is_reference_type(die)
7648 || die_is_array_type(die));}
7649
7650/// Test if a DIE represents a pointer or a reference type.
7651///
7652/// @param die the die to consider.
7653///
7654/// @return true iff @p die represents a pointer or reference type.
7655static bool
7656die_is_pointer_or_reference_type(const Dwarf_Die* die)
7657{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7658
7659/// Test if a DIE represents a pointer, a reference or a typedef type.
7660///
7661/// @param die the die to consider.
7662///
7663/// @return true iff @p die represents a pointer, a reference or a
7664/// typedef type.
7665static bool
7666die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7667{return (die_is_pointer_array_or_reference_type(die)
7668 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7669
7670/// Test if a DIE represents a class type.
7671///
7672/// @param die the die to consider.
7673///
7674/// @return true iff @p die represents a class type.
7675static bool
7676die_is_class_type(const Dwarf_Die* die)
7677{
7678 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7679
7680 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7681 return true;
7682
7683 return false;
7684}
7685
7686/// Test if a DIE is for a qualified type.
7687///
7688/// @param die the DIE to consider.
7689///
7690/// @return true iff @p die is for a qualified type.
7691static bool
7692die_is_qualified_type(const Dwarf_Die* die)
7693{
7694 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7695 if (tag == DW_TAG_const_type
7696 || tag == DW_TAG_volatile_type
7697 || tag == DW_TAG_restrict_type)
7698 return true;
7699
7700 return false;
7701}
7702
7703/// Test if a DIE is for a function type.
7704///
7705/// @param die the DIE to consider.
7706///
7707/// @return true iff @p die is for a function type.
7708static bool
7709die_is_function_type(const Dwarf_Die *die)
7710{
7711 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7712 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7713 return true;
7714
7715 return false;
7716}
7717
7718/// Test if a DIE for a function pointer or member function has an
7719/// DW_AT_object_pointer attribute.
7720///
7721/// @param die the DIE to consider.
7722///
7723/// @param object_pointer out parameter. It's set to the DIE for the
7724/// object pointer iff the function returns true.
7725///
7726/// @return true iff the DIE @p die has an object pointer. In that
7727/// case, the parameter @p object_pointer is set to the DIE of that
7728/// object pointer.
7729static bool
7730die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7731{
7732 if (!die)
7733 return false;
7734
7735 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7736 return true;
7737
7738 return false;
7739}
7740
7741/// Test if a DIE has children DIEs.
7742///
7743/// @param die the DIE to consider.
7744///
7745/// @return true iff @p DIE has at least one child node.
7746static bool
7747die_has_children(const Dwarf_Die* die)
7748{
7749 if (!die)
7750 return false;
7751
7752 Dwarf_Die child;
7753 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7754 return true;
7755
7756 return false;
7757}
7758
7759/// Get the DIE representing the first parameter of the function
7760/// denoted by a given DIE.
7761///
7762/// @param die the function DIE to consider. Note that if this
7763/// parameter is neither a DW_TAG_subprogram nor a
7764/// DW_TAG_subroutine_type, then the current process is aborted.
7765///
7766/// @param first_parm_die output parameter. This is set to the DIE of
7767/// the first parameter of the function denoted by @p die. This
7768/// output parameter is set iff the function returns true.
7769///
7770/// @return true iff the first parameter of the function denoted by @p
7771/// die is returned in output parameter @p first_parm_die.
7772static bool
7773fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die)
7774{
7775 if (!die)
7776 return false;
7777
7778 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7779 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7780
7781 Dwarf_Die child;
7782 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7783 {
7784 int child_tag = dwarf_tag(&child);
7785 if (child_tag == DW_TAG_formal_parameter)
7786 {
7787 memcpy(&first_parm_die, &child, sizeof(Dwarf_Die));
7788 return true;
7789 }
7790 }
7791 return false;
7792}
7793
7794/// Test if a member function denoted by a given DIE has a parameter
7795/// which is a "this pointer".
7796///
7797/// Please note that if the member function denotes a static member
7798/// function or if the DIE does not denote a member function to begin
7799/// with, then the function will return false because no "this
7800/// pointer" will be found.
7801///
7802/// @param rdr the current DWARF reader in use.
7803///
7804/// @param die the DIE of the member function this function should
7805/// inspect.
7806///
7807/// @param where_offset where in the DIE stream we logically are.
7808///
7809/// @param class_die output parameter. This is set iff a "this
7810/// pointer" was found as the first parameters of the member function
7811/// denoted by @p die, and thus the function returns true If set, this
7812/// then points to the DIE of the class containing the member function
7813/// denoted by @p die.
7814///
7815/// @param object_pointer_die output parameter. This is set to the
7816/// DIE of the function parameter that carries the "this pointe".
7817/// This is set iff this function return true.
7818///
7819/// @return true iff the first parameter of the member function
7820/// denoted by @p die points to a "this pointer".
7821static bool
7822member_fn_die_has_this_pointer(const reader& rdr,
7823 const Dwarf_Die* die,
7824 size_t where_offset,
7825 Dwarf_Die& class_die,
7826 Dwarf_Die& object_pointer_die)
7827{
7828 if (!die)
7829 return false;
7830
7831 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7832 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
7833 return false;
7834
7835 if (tag == DW_TAG_subprogram
7836 && !die_is_at_class_scope(rdr, die, where_offset, class_die))
7837 return false;
7838
7839 Dwarf_Die first_parm_die;
7840 Dwarf_Die parm_type_die;
7841 if (die_has_object_pointer(die, object_pointer_die))
7842 {
7843 // This can be either a member function with a
7844 // DW_AT_object_pointer attribute or a DW_TAG_subroutine_type
7845 // with a DW_AT_object_pointer. In the later case, we are
7846 // looking at a member function type.
7847 memcpy(&first_parm_die, &object_pointer_die, sizeof(Dwarf_Die));
7848 if (!die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7849 return false;
7850 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7851 die_peel_typedef(&parm_type_die, parm_type_die);
7852 }
7853 else if (fn_die_first_parameter_die(die, first_parm_die))
7854 {
7855 memcpy(&object_pointer_die, &first_parm_die, sizeof(Dwarf_Die));
7856 bool is_artificial = false;
7857 if (die_flag_attribute(&first_parm_die, DW_AT_artificial, is_artificial))
7858 {
7859 if (die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7860 {
7861 tag = dwarf_tag(&parm_type_die);
7862 if (tag == DW_TAG_pointer_type)
7863 {
7864 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7865 die_peel_typedef(&parm_type_die, parm_type_die);
7866 }
7867 else
7868 return false;
7869 }
7870 else
7871 return false;
7872 }
7873 else
7874 return false;
7875 }
7876 else
7877 return false;
7878
7879 tag = dwarf_tag(&parm_type_die);
7880 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7881 {
7882 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7883 return true;
7884 }
7885 return false;
7886}
7887
7888/// When given the object pointer DIE of a function type or member
7889/// function DIE, this function returns the "this" pointer that points
7890/// to the associated class.
7891///
7892/// @param die the DIE of the object pointer of the function or member
7893/// function to consider.
7894///
7895/// @param this_pointer_die out parameter. This is set to the DIE of
7896/// the "this" pointer iff the function returns true.
7897///
7898/// @return true iff the function found the "this" pointer from the
7899/// object pointer DIE @p die. In that case, the parameter @p
7900/// this_pointer_die is set to the DIE of that "this" pointer.
7901static bool
7902die_this_pointer_from_object_pointer(Dwarf_Die* die,
7903 Dwarf_Die& this_pointer_die)
7904{
7905 ABG_ASSERT(die);
7906 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7907
7908 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7909 return true;
7910
7911 return false;
7912}
7913
7914/// Test if a given "this" pointer that points to a particular class
7915/// type is for a const class or not. If it's for a const class, then
7916/// it means the function type or the member function associated to
7917/// that "this" pointer is const.
7918///
7919/// @param dye the DIE of the "this" pointer to consider.
7920///
7921/// @return true iff @p die points to a const class type.
7922static bool
7923die_this_pointer_is_const(Dwarf_Die* dye)
7924{
7925 ABG_ASSERT(dye);
7926
7927 Dwarf_Die die;
7928 memcpy(&die, dye, sizeof(Dwarf_Die));
7929 if (dwarf_tag(&die) == DW_TAG_const_type)
7930 ABG_ASSERT(die_peel_qualified(&die, die));
7931
7932 if (dwarf_tag(&die) == DW_TAG_pointer_type)
7933 {
7934 Dwarf_Die pointed_to_type_die;
7935 if (die_die_attribute(&die, DW_AT_type, pointed_to_type_die))
7936 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7937 return true;
7938 }
7939
7940 return false;
7941}
7942
7943/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7944/// attribute) points to a const implicit class and so is for a const
7945/// method or or a const member function type.
7946///
7947/// @param die the DIE of the object pointer to consider.
7948///
7949/// @return true iff the object pointer represented by @p die is for a
7950/// a const method or const member function type.
7951static bool
7952die_object_pointer_is_for_const_method(Dwarf_Die* die)
7953{
7954 ABG_ASSERT(die);
7955 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7956
7957 Dwarf_Die this_pointer_die;
7958 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7959 if (die_this_pointer_is_const(&this_pointer_die))
7960 return true;
7961
7962 return false;
7963}
7964
7965/// Test if a DIE represents an entity that is at class scope.
7966///
7967/// @param rdr the DWARF reader to use.
7968///
7969/// @param die the DIE to consider.
7970///
7971/// @param where_offset where we are logically at in the DIE stream.
7972///
7973/// @param class_scope_die out parameter. Set to the DIE of the
7974/// containing class iff @p die happens to be at class scope; that is,
7975/// iff the function returns true.
7976///
7977/// @return true iff @p die is at class scope. In that case, @p
7978/// class_scope_die is set to the DIE of the class that contains @p
7979/// die.
7980static bool
7981die_is_at_class_scope(const reader& rdr,
7982 const Dwarf_Die* die,
7983 size_t where_offset,
7984 Dwarf_Die& class_scope_die)
7985{
7986 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7987 return false;
7988
7989 int tag = dwarf_tag(&class_scope_die);
7990
7991 return (tag == DW_TAG_structure_type
7992 || tag == DW_TAG_class_type
7993 || tag == DW_TAG_union_type);
7994}
7995
7996/// Return the leaf object under a pointer, reference or qualified
7997/// type DIE.
7998///
7999/// @param die the DIE of the type to consider.
8000///
8001/// @param peeled_die out parameter. Set to the DIE of the leaf
8002/// object iff the function actually peeled anything.
8003///
8004/// @return true upon successful completion.
8005static bool
8006die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
8007{
8008 if (!die)
8009 return false;
8010
8011 int tag = dwarf_tag(die);
8012
8013 if (tag == DW_TAG_const_type
8014 || tag == DW_TAG_volatile_type
8015 || tag == DW_TAG_restrict_type
8016 || tag == DW_TAG_pointer_type
8017 || tag == DW_TAG_reference_type
8018 || tag == DW_TAG_rvalue_reference_type)
8019 {
8020 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8021 return false;
8022 }
8023 else
8024 return false;
8025
8026 memcpy(&peeled_die, die, sizeof(peeled_die));
8027
8028 while (tag == DW_TAG_const_type
8029 || tag == DW_TAG_volatile_type
8030 || tag == DW_TAG_restrict_type
8031 || tag == DW_TAG_pointer_type
8032 || tag == DW_TAG_reference_type
8033 || tag == DW_TAG_rvalue_reference_type)
8034 {
8035 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8036 break;
8037 tag = dwarf_tag(&peeled_die);
8038 }
8039
8040 return true;
8041}
8042
8043/// Return the leaf object under a qualified type DIE.
8044///
8045/// @param die the DIE of the type to consider.
8046///
8047/// @param peeled_die out parameter. Set to the DIE of the leaf
8048/// object iff the function actually peeled anything.
8049///
8050/// @return true upon successful completion.
8051static bool
8052die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
8053{
8054 if (!die)
8055 return false;
8056
8057 memcpy(&peeled_die, die, sizeof(peeled_die));
8058
8059 int tag = dwarf_tag(&peeled_die);
8060
8061 bool result = false;
8062 while (tag == DW_TAG_const_type
8063 || tag == DW_TAG_volatile_type
8064 || tag == DW_TAG_restrict_type)
8065 {
8066 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8067 break;
8068 tag = dwarf_tag(&peeled_die);
8069 result = true;
8070 }
8071
8072 return result;
8073}
8074
8075/// Return the leaf object under a typedef type DIE.
8076///
8077/// @param die the DIE of the type to consider.
8078///
8079/// @param peeled_die out parameter. Set to the DIE of the leaf
8080/// object iff the function actually peeled anything.
8081///
8082/// @return true upon successful completion.
8083static bool
8084die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
8085{
8086 if (!die)
8087 return false;
8088
8089 int tag = dwarf_tag(die);
8090
8091 memcpy(&peeled_die, die, sizeof(peeled_die));
8092
8093 if (tag == DW_TAG_typedef)
8094 {
8095 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8096 return false;
8097 }
8098 else
8099 return false;
8100
8101 while (tag == DW_TAG_typedef)
8102 {
8103 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8104 break;
8105 tag = dwarf_tag(&peeled_die);
8106 }
8107
8108 return true;
8109
8110}
8111
8112/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
8113///
8114/// @param die the DIE to consider.
8115///
8116/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
8117/// iff the function returned true.
8118///
8119/// @return true iff the function could peel @p die.
8120static bool
8121die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
8122{
8123 if (!die)
8124 return false;
8125
8126 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8127
8128 if (tag == DW_TAG_pointer_type
8129 || tag == DW_TAG_reference_type
8130 || tag == DW_TAG_rvalue_reference_type
8131 || tag == DW_TAG_typedef)
8132 {
8133 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8134 return false;
8135 }
8136 else
8137 return false;
8138
8139 while (tag == DW_TAG_pointer_type
8140 || tag == DW_TAG_reference_type
8141 || tag == DW_TAG_rvalue_reference_type
8142 || tag == DW_TAG_typedef)
8143 {
8144 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8145 break;
8146 tag = dwarf_tag(&peeled_die);
8147 }
8148 return true;
8149}
8150
8151/// Test if a DIE for a function type represents a method type.
8152///
8153/// @param rdr the DWARF reader.
8154///
8155/// @param die the DIE to consider.
8156///
8157/// @param where_offset where we logically are in the stream of DIEs.
8158///
8159/// @param object_pointer_die out parameter. This is set by the
8160/// function to the DIE that refers to the formal function parameter
8161/// which holds the implicit "this" pointer of the method. That die
8162/// is called the object pointer DIE. This is set iff the member
8163/// function is a non-static member function and if the function
8164/// returns true. In other words, this is only set if the is_static
8165/// out parameter is set to false and the function returns true.
8166///
8167/// @param class_die out parameter. This is set by the function to
8168/// the DIE that represents the class of the method type. This is set
8169/// iff the function returns true.
8170///
8171/// @param is_static out parameter. This is set to true by the
8172/// function if @p die is a static method or a the type of a static
8173/// method. This is set iff the function returns true.
8174///
8175/// @return true iff @p die is a DIE for a method type.
8176static bool
8177die_function_type_is_method_type(const reader& rdr,
8178 const Dwarf_Die *die,
8179 size_t where_offset,
8180 Dwarf_Die& object_pointer_die,
8181 Dwarf_Die& class_die,
8182 bool& is_static)
8183{
8184 if (!die)
8185 return false;
8186
8187 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8188 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
8189
8190 if (member_fn_die_has_this_pointer(rdr, die, where_offset, class_die, object_pointer_die))
8191 {
8192 is_static = false;
8193 return true;
8194 }
8195 else if (die_is_at_class_scope(rdr, die, where_offset, class_die))
8196 {
8197 is_static = true;
8198 return true;
8199 }
8200
8201 return false;
8202}
8203
8204enum virtuality
8205{
8206 VIRTUALITY_NOT_VIRTUAL,
8207 VIRTUALITY_VIRTUAL,
8208 VIRTUALITY_PURE_VIRTUAL
8209};
8210
8211/// Get the virtual-ness of a given DIE, that is, the value of the
8212/// DW_AT_virtuality attribute.
8213///
8214/// @param die the DIE to read from.
8215///
8216/// @param virt the resulting virtuality attribute. This is set iff
8217/// the function returns true.
8218///
8219/// @return true if the virtual-ness could be determined.
8220static bool
8221die_virtuality(const Dwarf_Die* die, virtuality& virt)
8222{
8223 if (!die)
8224 return false;
8225
8226 uint64_t v = 0;
8227 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
8228
8229 if (v == DW_VIRTUALITY_virtual)
8230 virt = VIRTUALITY_VIRTUAL;
8231 else if (v == DW_VIRTUALITY_pure_virtual)
8232 virt = VIRTUALITY_PURE_VIRTUAL;
8233 else
8234 virt = VIRTUALITY_NOT_VIRTUAL;
8235
8236 return true;
8237}
8238
8239/// Test whether the DIE represent either a virtual base or function.
8240///
8241/// @param die the DIE to consider.
8242///
8243/// @return bool if the DIE represents a virtual base or function,
8244/// false othersise.
8245static bool
8246die_is_virtual(const Dwarf_Die* die)
8247{
8248 virtuality v;
8249 if (!die_virtuality(die, v))
8250 return false;
8251
8252 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
8253}
8254
8255/// Test if the DIE represents an entity that was declared inlined.
8256///
8257/// @param die the DIE to test for.
8258///
8259/// @return true if the DIE represents an entity that was declared
8260/// inlined.
8261static bool
8262die_is_declared_inline(Dwarf_Die* die)
8263{
8264 uint64_t inline_value = 0;
8265 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
8266 return false;
8267 return (inline_value == DW_INL_declared_inlined
8268 || inline_value == DW_INL_declared_not_inlined);
8269}
8270
8271/// Compare two DWARF strings using the most accurate (and slowest)
8272/// method possible.
8273///
8274/// @param l the DIE that carries the first string to consider, as an
8275/// attribute value.
8276///
8277/// @param attr_name the name of the attribute which value is the
8278/// string to compare.
8279///
8280/// @return true iff the string carried by @p l equals the one carried
8281/// by @p r.
8282static bool
8283slowly_compare_strings(const Dwarf_Die *l,
8284 const Dwarf_Die *r,
8285 unsigned attr_name)
8286{
8287 const char *l_str = die_char_str_attribute(l, attr_name),
8288 *r_str = die_char_str_attribute(r, attr_name);
8289 if (!l_str && !r_str)
8290 return true;
8291 return l_str && r_str && !strcmp(l_str, r_str);
8292}
8293
8294/// This function is a fast routine (optimization) to compare the
8295/// values of two string attributes of two DIEs.
8296///
8297/// @param l the first DIE to consider.
8298///
8299/// @param r the second DIE to consider.
8300///
8301/// @param attr_name the name of the attribute to compare, on the two
8302/// DIEs above.
8303///
8304/// @param result out parameter. This is set to the result of the
8305/// comparison. If the value of attribute @p attr_name on DIE @p l
8306/// equals the value of attribute @p attr_name on DIE @p r, then the
8307/// the argument of this parameter is set to true. Otherwise, it's
8308/// set to false. Note that the argument of this parameter is set iff
8309/// the function returned true.
8310///
8311/// @return true iff the comparison could be performed. There are
8312/// cases in which the comparison cannot be performed. For instance,
8313/// if one of the DIEs does not have the attribute @p attr_name. In
8314/// any case, if this function returns true, then the parameter @p
8315/// result is set to the result of the comparison.
8316static bool
8317compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
8318 unsigned attr_name,
8319 bool &result)
8320{
8321 Dwarf_Attribute l_attr, r_attr;
8322 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
8323 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
8324 return false;
8325
8326 ABG_ASSERT(l_attr.form == DW_FORM_strp
8327 || l_attr.form == DW_FORM_string
8328 || l_attr.form == DW_FORM_GNU_strp_alt
8329 || form_is_DW_FORM_strx(l_attr.form)
8330 || form_is_DW_FORM_line_strp(l_attr.form));
8331
8332 ABG_ASSERT(r_attr.form == DW_FORM_strp
8333 || r_attr.form == DW_FORM_string
8334 || r_attr.form == DW_FORM_GNU_strp_alt
8335 || form_is_DW_FORM_strx(r_attr.form)
8336 || form_is_DW_FORM_line_strp(r_attr.form));
8337
8338 if ((l_attr.form == DW_FORM_strp
8339 && r_attr.form == DW_FORM_strp)
8340 || (l_attr.form == DW_FORM_GNU_strp_alt
8341 && r_attr.form == DW_FORM_GNU_strp_alt)
8342 || (form_is_DW_FORM_strx(l_attr.form)
8343 && form_is_DW_FORM_strx(r_attr.form))
8344 || (form_is_DW_FORM_line_strp(l_attr.form)
8345 && form_is_DW_FORM_line_strp(r_attr.form)))
8346 {
8347 // So these string attributes are actually pointers into a
8348 // string table. The string table is most likely de-duplicated
8349 // so comparing the *values* of the pointers should be enough.
8350 //
8351 // This is the fast path.
8352 if (l_attr.valp == r_attr.valp)
8353 {
8354#if WITH_DEBUG_TYPE_CANONICALIZATION
8355 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
8356#endif
8357 result = true;
8358 return true;
8359 }
8360 }
8361
8362 // If we reached this point it means we couldn't use the fast path
8363 // because the string atttributes are strings that are "inline" in
8364 // the debug info section. Let's just compare them the slow and
8365 // obvious way.
8366 result = slowly_compare_strings(l, r, attr_name);
8367 return true;
8368}
8369
8370/// Compare the file path of the compilation units (aka CUs)
8371/// associated to two DIEs.
8372///
8373/// If the DIEs are for pointers or typedefs, this function also
8374/// compares the file paths of the CUs of the leaf DIEs (underlying
8375/// DIEs of the pointer or the typedef).
8376///
8377/// @param l the first type DIE to consider.
8378///
8379/// @param r the second type DIE to consider.
8380///
8381/// @return true iff the file paths of the DIEs of the two types are
8382/// equal.
8383static bool
8384compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
8385{
8386 Dwarf_Die l_cu, r_cu;
8387 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
8388 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
8389 return false;
8390
8391 bool compared =
8392 compare_dies_string_attribute_value(&l_cu, &r_cu,
8393 DW_AT_name,
8394 result);
8395 if (compared && result)
8396 {
8397 Dwarf_Die peeled_l, peeled_r;
8398 if (die_is_pointer_reference_or_typedef_type(l)
8399 && die_is_pointer_reference_or_typedef_type(r)
8400 && die_peel_pointer_and_typedef(l, peeled_l)
8401 && die_peel_pointer_and_typedef(r, peeled_r))
8402 {
8403 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
8404 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
8405 return false;
8406 compared =
8407 compare_dies_string_attribute_value(&l_cu, &r_cu,
8408 DW_AT_name,
8409 result);
8410 }
8411 }
8412
8413 return compared;
8414}
8415
8416// -----------------------------------
8417// <location expression evaluation>
8418// -----------------------------------
8419
8420/// Get the value of a given DIE attribute, knowing that it must be a
8421/// location expression.
8422///
8423/// @param die the DIE to read the attribute from.
8424///
8425/// @param attr_name the name of the attribute to read the value for.
8426///
8427/// @param expr the pointer to allocate and fill with the resulting
8428/// array of operators + operands forming a dwarf expression. This is
8429/// set iff the function returns true.
8430///
8431/// @param expr_len the length of the resulting dwarf expression.
8432/// This is set iff the function returns true.
8433///
8434/// @return true if the attribute exists and has a non-empty dwarf expression
8435/// as value. In that case the expr and expr_len arguments are set to the
8436/// resulting dwarf expression.
8437static bool
8438die_location_expr(const Dwarf_Die* die,
8439 unsigned attr_name,
8440 Dwarf_Op** expr,
8441 size_t* expr_len)
8442{
8443 if (!die)
8444 return false;
8445
8446 Dwarf_Attribute attr;
8447 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
8448 return false;
8449
8450 size_t len = 0;
8451 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
8452
8453 // Ignore location expressions where reading them succeeded but
8454 // their length is 0.
8455 result &= len > 0;
8456
8457 if (result)
8458 *expr_len = len;
8459
8460 return result;
8461}
8462
8463/// If the current operation in the dwarf expression represents a push
8464/// of a constant value onto the dwarf expr virtual machine (aka
8465/// DEVM), perform the operation and update the DEVM.
8466///
8467/// If the result of the operation is a constant, update the DEVM
8468/// accumulator with its value. Otherwise, the DEVM accumulator is
8469/// left with its previous value.
8470///
8471/// @param ops the array of the dwarf expression operations to consider.
8472///
8473/// @param ops_len the lengths of @p ops array above.
8474///
8475/// @param index the index of the operation to interpret, in @p ops.
8476///
8477/// @param next_index the index of the operation to interpret at the
8478/// next step, after this function completed and returned. This is
8479/// set an output parameter that is set iff the function returns true.
8480///
8481/// @param ctxt the DEVM evaluation context.
8482///
8483/// @return true if the current operation actually pushes a constant
8484/// value onto the DEVM stack, false otherwise.
8485static bool
8486op_pushes_constant_value(Dwarf_Op* ops,
8487 size_t ops_len,
8488 size_t index,
8489 size_t& next_index,
8490 dwarf_expr_eval_context& ctxt)
8491{
8492 ABG_ASSERT(index < ops_len);
8493
8494 Dwarf_Op& op = ops[index];
8495 int64_t value = 0;
8496
8497 switch (op.atom)
8498 {
8499 case DW_OP_addr:
8500 value = ops[index].number;
8501 break;
8502
8503 case DW_OP_const1u:
8504 case DW_OP_const1s:
8505 case DW_OP_const2u:
8506 case DW_OP_const2s:
8507 case DW_OP_const4u:
8508 case DW_OP_const4s:
8509 case DW_OP_const8u:
8510 case DW_OP_const8s:
8511 case DW_OP_constu:
8512 case DW_OP_consts:
8513 value = ops[index].number;
8514 break;
8515
8516 case DW_OP_lit0:
8517 value = 0;
8518 break;
8519 case DW_OP_lit1:
8520 value = 1;
8521 break;
8522 case DW_OP_lit2:
8523 value = 2;
8524 break;
8525 case DW_OP_lit3:
8526 value = 3;
8527 break;
8528 case DW_OP_lit4:
8529 value = 4;
8530 break;
8531 case DW_OP_lit5:
8532 value = 5;
8533 break;
8534 case DW_OP_lit6:
8535 value = 6;
8536 break;
8537 case DW_OP_lit7:
8538 value = 7;
8539 break;
8540 case DW_OP_lit8:
8541 value = 8;
8542 break;
8543 case DW_OP_lit9:
8544 value = 9;
8545 break;
8546 case DW_OP_lit10:
8547 value = 10;
8548 break;
8549 case DW_OP_lit11:
8550 value = 11;
8551 break;
8552 case DW_OP_lit12:
8553 value = 12;
8554 break;
8555 case DW_OP_lit13:
8556 value = 13;
8557 break;
8558 case DW_OP_lit14:
8559 value = 14;
8560 break;
8561 case DW_OP_lit15:
8562 value = 15;
8563 break;
8564 case DW_OP_lit16:
8565 value = 16;
8566 break;
8567 case DW_OP_lit17:
8568 value = 17;
8569 break;
8570 case DW_OP_lit18:
8571 value = 18;
8572 break;
8573 case DW_OP_lit19:
8574 value = 19;
8575 break;
8576 case DW_OP_lit20:
8577 value = 20;
8578 break;
8579 case DW_OP_lit21:
8580 value = 21;
8581 break;
8582 case DW_OP_lit22:
8583 value = 22;
8584 break;
8585 case DW_OP_lit23:
8586 value = 23;
8587 break;
8588 case DW_OP_lit24:
8589 value = 24;
8590 break;
8591 case DW_OP_lit25:
8592 value = 25;
8593 break;
8594 case DW_OP_lit26:
8595 value = 26;
8596 break;
8597 case DW_OP_lit27:
8598 value = 27;
8599 break;
8600 case DW_OP_lit28:
8601 value = 28;
8602 break;
8603 case DW_OP_lit29:
8604 value = 29;
8605 break;
8606 case DW_OP_lit30:
8607 value = 30;
8608 break;
8609 case DW_OP_lit31:
8610 value = 31;
8611 break;
8612
8613 default:
8614 return false;
8615 }
8616
8617 expr_result r(value);
8618 ctxt.push(r);
8619 ctxt.accum = r;
8620 next_index = index + 1;
8621
8622 return true;
8623}
8624
8625/// If the current operation in the dwarf expression represents a push
8626/// of a non-constant value onto the dwarf expr virtual machine (aka
8627/// DEVM), perform the operation and update the DEVM. A non-constant
8628/// is namely a quantity for which we need inferior (a running program
8629/// image) state to know the exact value.
8630///
8631/// Upon successful completion, as the result of the operation is a
8632/// non-constant the DEVM accumulator value is left to its state as of
8633/// before the invocation of this function.
8634///
8635/// @param ops the array of the dwarf expression operations to consider.
8636///
8637/// @param ops_len the lengths of @p ops array above.
8638///
8639/// @param index the index of the operation to interpret, in @p ops.
8640///
8641/// @param next_index the index of the operation to interpret at the
8642/// next step, after this function completed and returned. This is
8643/// set an output parameter that is set iff the function returns true.
8644///
8645/// @param ctxt the DEVM evaluation context.
8646///
8647/// @return true if the current operation actually pushes a
8648/// non-constant value onto the DEVM stack, false otherwise.
8649static bool
8650op_pushes_non_constant_value(Dwarf_Op* ops,
8651 size_t ops_len,
8652 size_t index,
8653 size_t& next_index,
8654 dwarf_expr_eval_context& ctxt)
8655{
8656 ABG_ASSERT(index < ops_len);
8657 Dwarf_Op& op = ops[index];
8658
8659 switch (op.atom)
8660 {
8661 case DW_OP_reg0:
8662 case DW_OP_reg1:
8663 case DW_OP_reg2:
8664 case DW_OP_reg3:
8665 case DW_OP_reg4:
8666 case DW_OP_reg5:
8667 case DW_OP_reg6:
8668 case DW_OP_reg7:
8669 case DW_OP_reg8:
8670 case DW_OP_reg9:
8671 case DW_OP_reg10:
8672 case DW_OP_reg11:
8673 case DW_OP_reg12:
8674 case DW_OP_reg13:
8675 case DW_OP_reg14:
8676 case DW_OP_reg15:
8677 case DW_OP_reg16:
8678 case DW_OP_reg17:
8679 case DW_OP_reg18:
8680 case DW_OP_reg19:
8681 case DW_OP_reg20:
8682 case DW_OP_reg21:
8683 case DW_OP_reg22:
8684 case DW_OP_reg23:
8685 case DW_OP_reg24:
8686 case DW_OP_reg25:
8687 case DW_OP_reg26:
8688 case DW_OP_reg27:
8689 case DW_OP_reg28:
8690 case DW_OP_reg29:
8691 case DW_OP_reg30:
8692 case DW_OP_reg31:
8693 next_index = index + 1;
8694 break;
8695
8696 case DW_OP_breg0:
8697 case DW_OP_breg1:
8698 case DW_OP_breg2:
8699 case DW_OP_breg3:
8700 case DW_OP_breg4:
8701 case DW_OP_breg5:
8702 case DW_OP_breg6:
8703 case DW_OP_breg7:
8704 case DW_OP_breg8:
8705 case DW_OP_breg9:
8706 case DW_OP_breg10:
8707 case DW_OP_breg11:
8708 case DW_OP_breg12:
8709 case DW_OP_breg13:
8710 case DW_OP_breg14:
8711 case DW_OP_breg15:
8712 case DW_OP_breg16:
8713 case DW_OP_breg17:
8714 case DW_OP_breg18:
8715 case DW_OP_breg19:
8716 case DW_OP_breg20:
8717 case DW_OP_breg21:
8718 case DW_OP_breg22:
8719 case DW_OP_breg23:
8720 case DW_OP_breg24:
8721 case DW_OP_breg25:
8722 case DW_OP_breg26:
8723 case DW_OP_breg27:
8724 case DW_OP_breg28:
8725 case DW_OP_breg29:
8726 case DW_OP_breg30:
8727 case DW_OP_breg31:
8728 next_index = index + 1;
8729 break;
8730
8731 case DW_OP_regx:
8732 next_index = index + 2;
8733 break;
8734
8735 case DW_OP_fbreg:
8736 next_index = index + 1;
8737 break;
8738
8739 case DW_OP_bregx:
8740 next_index = index + 1;
8741 break;
8742
8743 case DW_OP_GNU_variable_value:
8744 next_index = index + 1;
8745 break;
8746
8747 default:
8748 return false;
8749 }
8750
8751 expr_result r(false);
8752 ctxt.push(r);
8753
8754 return true;
8755}
8756
8757/// If the current operation in the dwarf expression represents a
8758/// manipulation of the stack of the DWARF Expression Virtual Machine
8759/// (aka DEVM), this function performs the operation and updates the
8760/// state of the DEVM. If the result of the operation represents a
8761/// constant value, then the accumulator of the DEVM is set to that
8762/// result's value, Otherwise, the DEVM accumulator is left with its
8763/// previous value.
8764///
8765/// @param expr the array of the dwarf expression operations to consider.
8766///
8767/// @param expr_len the lengths of @p ops array above.
8768///
8769/// @param index the index of the operation to interpret, in @p ops.
8770///
8771/// @param next_index the index of the operation to interpret at the
8772/// next step, after this function completed and returned. This is
8773/// set an output parameter that is set iff the function returns true.
8774///
8775/// @param ctxt the DEVM evaluation context.
8776///
8777/// @return true if the current operation actually manipulates the
8778/// DEVM stack, false otherwise.
8779static bool
8780op_manipulates_stack(Dwarf_Op* expr,
8781 size_t expr_len,
8782 size_t index,
8783 size_t& next_index,
8784 dwarf_expr_eval_context& ctxt)
8785{
8786 Dwarf_Op& op = expr[index];
8787 expr_result v;
8788
8789 switch (op.atom)
8790 {
8791 case DW_OP_dup:
8792 v = ctxt.stack.front();
8793 ctxt.push(v);
8794 break;
8795
8796 case DW_OP_drop:
8797 v = ctxt.stack.front();
8798 ctxt.pop();
8799 break;
8800
8801 case DW_OP_over:
8802 ABG_ASSERT(ctxt.stack.size() > 1);
8803 v = ctxt.stack[1];
8804 ctxt.push(v);
8805 break;
8806
8807 case DW_OP_pick:
8808 ABG_ASSERT(index + 1 < expr_len);
8809 v = op.number;
8810 ctxt.push(v);
8811 break;
8812
8813 case DW_OP_swap:
8814 ABG_ASSERT(ctxt.stack.size() > 1);
8815 v = ctxt.stack[1];
8816 ctxt.stack.erase(ctxt.stack.begin() + 1);
8817 ctxt.push(v);
8818 break;
8819
8820 case DW_OP_rot:
8821 ABG_ASSERT(ctxt.stack.size() > 2);
8822 v = ctxt.stack[2];
8823 ctxt.stack.erase(ctxt.stack.begin() + 2);
8824 ctxt.push(v);
8825 break;
8826
8827 case DW_OP_deref:
8828 case DW_OP_deref_size:
8829 ABG_ASSERT(ctxt.stack.size() > 0);
8830 ctxt.pop();
8831 v.is_const(false);
8832 ctxt.push(v);
8833 break;
8834
8835 case DW_OP_xderef:
8836 case DW_OP_xderef_size:
8837 ABG_ASSERT(ctxt.stack.size() > 1);
8838 ctxt.pop();
8839 ctxt.pop();
8840 v.is_const(false);
8841 ctxt.push(v);
8842 break;
8843
8844 case DW_OP_push_object_address:
8845 v.is_const(false);
8846 ctxt.push(v);
8847 break;
8848
8849 case DW_OP_form_tls_address:
8850 case DW_OP_GNU_push_tls_address:
8851 ABG_ASSERT(ctxt.stack.size() > 0);
8852 v = ctxt.pop();
8853 if (op.atom == DW_OP_form_tls_address)
8854 v.is_const(false);
8855 ctxt.push(v);
8856 break;
8857
8858 case DW_OP_call_frame_cfa:
8859 v.is_const(false);
8860 ctxt.push(v);
8861 break;
8862
8863 default:
8864 return false;
8865 }
8866
8867 if (v.is_const())
8868 ctxt.accum = v;
8869
8870 if (op.atom == DW_OP_form_tls_address
8871 || op.atom == DW_OP_GNU_push_tls_address)
8872 ctxt.set_tls_address(true);
8873 else
8874 ctxt.set_tls_address(false);
8875
8876 next_index = index + 1;
8877
8878 return true;
8879}
8880
8881/// If the current operation in the dwarf expression represents a push
8882/// of an arithmetic or logic operation onto the dwarf expr virtual
8883/// machine (aka DEVM), perform the operation and update the DEVM.
8884///
8885/// If the result of the operation is a constant, update the DEVM
8886/// accumulator with its value. Otherwise, the DEVM accumulator is
8887/// left with its previous value.
8888///
8889/// @param expr the array of the dwarf expression operations to consider.
8890///
8891/// @param expr_len the lengths of @p expr array above.
8892///
8893/// @param index the index of the operation to interpret, in @p expr.
8894///
8895/// @param next_index the index of the operation to interpret at the
8896/// next step, after this function completed and returned. This is
8897/// set an output parameter that is set iff the function returns true.
8898///
8899/// @param ctxt the DEVM evaluation context.
8900///
8901/// @return true if the current operation actually represent an
8902/// arithmetic or logic operation.
8903static bool
8904op_is_arith_logic(Dwarf_Op* expr,
8905 size_t expr_len,
8906 size_t index,
8907 size_t& next_index,
8908 dwarf_expr_eval_context& ctxt)
8909{
8910 ABG_ASSERT(index < expr_len);
8911
8912 Dwarf_Op& op = expr[index];
8913 expr_result val1, val2;
8914 bool result = false;
8915
8916 switch (op.atom)
8917 {
8918 case DW_OP_abs:
8919 ABG_ASSERT(ctxt.stack.size() > 0);
8920 val1 = ctxt.pop();
8921 val1 = val1.abs();
8922 ctxt.push(val1);
8923 result = true;
8924 break;
8925
8926 case DW_OP_and:
8927 ABG_ASSERT(ctxt.stack.size() > 1);
8928 val1 = ctxt.pop();
8929 val2 = ctxt.pop();
8930 ctxt.push(val1 & val2);
8931 break;
8932
8933 case DW_OP_div:
8934 ABG_ASSERT(ctxt.stack.size() > 1);
8935 val1 = ctxt.pop();
8936 val2 = ctxt.pop();
8937 if (!val1.is_const())
8938 val1 = 1;
8939 ctxt.push(val2 / val1);
8940 result = true;
8941 break;
8942
8943 case DW_OP_minus:
8944 ABG_ASSERT(ctxt.stack.size() > 1);
8945 val1 = ctxt.pop();
8946 val2 = ctxt.pop();
8947 ctxt.push(val2 - val1);
8948 result = true;
8949 break;
8950
8951 case DW_OP_mod:
8952 ABG_ASSERT(ctxt.stack.size() > 1);
8953 val1 = ctxt.pop();
8954 val2 = ctxt.pop();
8955 ctxt.push(val2 % val1);
8956 result = true;
8957 break;
8958
8959 case DW_OP_mul:
8960 ABG_ASSERT(ctxt.stack.size() > 1);
8961 val1 = ctxt.pop();
8962 val2 = ctxt.pop();
8963 ctxt.push(val2 * val1);
8964 result = true;
8965 break;
8966
8967 case DW_OP_neg:
8968 ABG_ASSERT(ctxt.stack.size() > 0);
8969 val1 = ctxt.pop();
8970 ctxt.push(-val1);
8971 result = true;
8972 break;
8973
8974 case DW_OP_not:
8975 ABG_ASSERT(ctxt.stack.size() > 0);
8976 val1 = ctxt.pop();
8977 ctxt.push(~val1);
8978 result = true;
8979 break;
8980
8981 case DW_OP_or:
8982 ABG_ASSERT(ctxt.stack.size() > 1);
8983 val1 = ctxt.pop();
8984 val2 = ctxt.pop();
8985 ctxt.push(val1 | val2);
8986 result = true;
8987 break;
8988
8989 case DW_OP_plus:
8990 ABG_ASSERT(ctxt.stack.size() > 1);
8991 val1 = ctxt.pop();
8992 val2 = ctxt.pop();
8993 ctxt.push(val2 + val1);
8994 result = true;
8995 break;
8996
8997 case DW_OP_plus_uconst:
8998 ABG_ASSERT(ctxt.stack.size() > 0);
8999 val1 = ctxt.pop();
9000 val1 += op.number;
9001 ctxt.push(val1);
9002 result = true;
9003 break;
9004
9005 case DW_OP_shl:
9006 ABG_ASSERT(ctxt.stack.size() > 1);
9007 val1 = ctxt.pop();
9008 val2 = ctxt.pop();
9009 ctxt.push(val2 << val1);
9010 result = true;
9011 break;
9012
9013 case DW_OP_shr:
9014 case DW_OP_shra:
9015 ABG_ASSERT(ctxt.stack.size() > 1);
9016 val1 = ctxt.pop();
9017 val2 = ctxt.pop();
9018 ctxt.push(val2 >> val1);
9019 result = true;
9020 break;
9021
9022 case DW_OP_xor:
9023 ABG_ASSERT(ctxt.stack.size() > 1);
9024 val1 = ctxt.pop();
9025 val2 = ctxt.pop();
9026 ctxt.push(val2 ^ val1);
9027 result = true;
9028 break;
9029
9030 default:
9031 break;
9032 }
9033
9034 if (result == true)
9035 {
9036 if (ctxt.stack.front().is_const())
9037 ctxt.accum = ctxt.stack.front();
9038
9039 next_index = index + 1;
9040 }
9041 return result;;
9042}
9043
9044/// If the current operation in the dwarf expression represents a push
9045/// of a control flow operation onto the dwarf expr virtual machine
9046/// (aka DEVM), perform the operation and update the DEVM.
9047///
9048/// If the result of the operation is a constant, update the DEVM
9049/// accumulator with its value. Otherwise, the DEVM accumulator is
9050/// left with its previous value.
9051///
9052/// @param expr the array of the dwarf expression operations to consider.
9053///
9054/// @param expr_len the lengths of @p expr array above.
9055///
9056/// @param index the index of the operation to interpret, in @p expr.
9057///
9058/// @param next_index the index of the operation to interpret at the
9059/// next step, after this function completed and returned. This is
9060/// set an output parameter that is set iff the function returns true.
9061///
9062/// @param ctxt the DEVM evaluation context.
9063///
9064/// @return true if the current operation actually represents a
9065/// control flow operation, false otherwise.
9066static bool
9067op_is_control_flow(Dwarf_Op* expr,
9068 size_t expr_len,
9069 size_t index,
9070 size_t& next_index,
9071 dwarf_expr_eval_context& ctxt)
9072{
9073 ABG_ASSERT(index < expr_len);
9074
9075 Dwarf_Op& op = expr[index];
9076 expr_result val1, val2;
9077
9078 switch (op.atom)
9079 {
9080 case DW_OP_eq:
9081 case DW_OP_ge:
9082 case DW_OP_gt:
9083 case DW_OP_le:
9084 case DW_OP_lt:
9085 case DW_OP_ne:
9086 {
9087 bool value = true;
9088 val1 = ctxt.pop();
9089 val2 = ctxt.pop();
9090 if (op.atom == DW_OP_eq)
9091 value = val2 == val1;
9092 else if (op.atom == DW_OP_ge)
9093 value = val2 >= val1;
9094 else if (op.atom == DW_OP_gt)
9095 value = val2 > val1;
9096 else if (op.atom == DW_OP_le)
9097 value = val2 <= val1;
9098 else if (op.atom == DW_OP_lt)
9099 value = val2 < val1;
9100 else if (op.atom == DW_OP_ne)
9101 value = val2 != val1;
9102
9103 val1 = value ? 1 : 0;
9104 ctxt.push(val1);
9105 }
9106 break;
9107
9108 case DW_OP_skip:
9109 if (op.number > 0)
9110 index += op.number - 1;
9111 break;
9112
9113 case DW_OP_bra:
9114 val1 = ctxt.pop();
9115 if (val1.const_value() != 0)
9116 index += val1.const_value() - 1;
9117 break;
9118
9119 case DW_OP_call2:
9120 case DW_OP_call4:
9121 case DW_OP_call_ref:
9122 case DW_OP_nop:
9123 break;
9124
9125 default:
9126 return false;
9127 }
9128
9129 if (ctxt.stack.front().is_const())
9130 ctxt.accum = ctxt.stack.front();
9131
9132 next_index = index + 1;
9133 return true;
9134}
9135
9136/// This function quickly evaluates a DWARF expression that is a
9137/// constant.
9138///
9139/// This is a "fast path" function that quickly evaluates a DWARF
9140/// expression that is only made of a DW_OP_plus_uconst operator.
9141///
9142/// This is a sub-routine of die_member_offset.
9143///
9144/// @param expr the DWARF expression to evaluate.
9145///
9146/// @param expr_len the length of the expression @p expr.
9147///
9148/// @param value out parameter. This is set to the result of the
9149/// evaluation of @p expr, iff this function returns true.
9150///
9151/// @return true iff the evaluation of @p expr went OK.
9152static bool
9153eval_quickly(Dwarf_Op* expr,
9154 uint64_t expr_len,
9155 int64_t& value)
9156{
9157 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
9158 {
9159 value = expr[0].number;
9160 return true;
9161 }
9162 return false;
9163}
9164
9165/// Evaluate the value of the last sub-expression that is a constant,
9166/// inside a given DWARF expression.
9167///
9168/// @param expr the DWARF expression to consider.
9169///
9170/// @param expr_len the length of the expression to consider.
9171///
9172/// @param value the resulting value of the last constant
9173/// sub-expression of the DWARF expression. This is set iff the
9174/// function returns true.
9175///
9176/// @param is_tls_address out parameter. This is set to true iff
9177/// the resulting value of the evaluation is a TLS (thread local
9178/// storage) address.
9179///
9180/// @param eval_ctxt the evaluation context to (re)use. Note that
9181/// this function initializes this context before using it.
9182///
9183/// @return true if the function could find a constant sub-expression
9184/// to evaluate, false otherwise.
9185static bool
9186eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9187 size_t expr_len,
9188 int64_t& value,
9189 bool& is_tls_address,
9190 dwarf_expr_eval_context &eval_ctxt)
9191{
9192 // Reset the evaluation context before evaluating the constant sub
9193 // expression contained in the DWARF expression 'expr'.
9194 eval_ctxt.reset();
9195
9196 size_t index = 0, next_index = 0;
9197 do
9198 {
9199 if (op_is_arith_logic(expr, expr_len, index,
9200 next_index, eval_ctxt)
9201 || op_pushes_constant_value(expr, expr_len, index,
9202 next_index, eval_ctxt)
9203 || op_manipulates_stack(expr, expr_len, index,
9204 next_index, eval_ctxt)
9205 || op_pushes_non_constant_value(expr, expr_len, index,
9206 next_index, eval_ctxt)
9207 || op_is_control_flow(expr, expr_len, index,
9208 next_index, eval_ctxt))
9209 ;
9210 else
9211 next_index = index + 1;
9212
9213 ABG_ASSERT(next_index > index);
9214 index = next_index;
9215 } while (index < expr_len);
9216
9217 is_tls_address = eval_ctxt.set_tls_address();
9218 if (eval_ctxt.accum.is_const())
9219 {
9220 value = eval_ctxt.accum;
9221 return true;
9222 }
9223 return false;
9224}
9225
9226/// Evaluate the value of the last sub-expression that is a constant,
9227/// inside a given DWARF expression.
9228///
9229/// @param expr the DWARF expression to consider.
9230///
9231/// @param expr_len the length of the expression to consider.
9232///
9233/// @param value the resulting value of the last constant
9234/// sub-expression of the DWARF expression. This is set iff the
9235/// function returns true.
9236///
9237/// @return true if the function could find a constant sub-expression
9238/// to evaluate, false otherwise.
9239static bool
9240eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9241 size_t expr_len,
9242 int64_t& value,
9243 bool& is_tls_address)
9244{
9245 dwarf_expr_eval_context eval_ctxt;
9246 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
9247 is_tls_address, eval_ctxt);
9248}
9249
9250// -----------------------------------
9251// </location expression evaluation>
9252// -----------------------------------
9253
9254/// Convert a DW_AT_bit_offset attribute value into the same value as
9255/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
9256///
9257/// On big endian machines, the value of the DW_AT_bit_offset
9258/// attribute + 8 * the value of the DW_AT_data_member_location
9259/// attribute is the same as the value of the DW_AT_data_bit_offset
9260/// attribute.
9261///
9262/// On little endian machines however, the situation is different.
9263/// The DW_AT_bit_offset value for a bit field is the number of bits
9264/// to the left of the most significant bit of the bit field, within
9265/// the integer value at DW_AT_data_member_location.
9266///
9267/// The DW_AT_data_bit_offset offset value is the number of bits to
9268/// the right of the least significant bit of the bit field, again
9269/// relative to the containing integer value.
9270///
9271/// In other words, DW_AT_data_bit_offset is what everybody would
9272/// instinctively think of as being the "offset of the bit field". 8 *
9273/// DW_AT_data_member_location + DW_AT_bit_offset however is very
9274/// counter-intuitive on little endian machines.
9275///
9276/// This function thus reads the value of a DW_AT_bit_offset property
9277/// of a DIE and converts it into what the DW_AT_data_bit_offset would
9278/// have been if it was present, ignoring the contribution of
9279/// DW_AT_data_member_location.
9280///
9281/// Note that DW_AT_bit_offset has been made obsolete starting from
9282/// DWARF5 (for GCC; Clang still emits it).
9283///
9284/// If you like coffee and it's not too late, now might be a good time
9285/// to have a coffee break. Otherwise if it's late at night, you
9286/// might want to consider an herbal tea break. Then come back to
9287/// read this.
9288///
9289///
9290/// In what follows, the bit fields are all contained within the first
9291/// whole int of the struct, so DW_AT_data_member_location is 0.
9292///
9293/// Okay, to have a better idea of what DW_AT_bit_offset and
9294/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
9295/// have bit fields data members defined as:
9296///
9297/// struct S
9298/// {
9299/// int j:5;
9300/// int k:6;
9301/// int m:5;
9302/// int n:8;
9303/// };
9304///
9305/// The below wonderful (at least!) ASCII art sketch describes the
9306/// layout of the bitfields of 'struct S' on a little endian machine.
9307/// You need to read the sketch from the bottom-up.
9308///
9309/// So please scroll down to its bottom. Note how the 32 bits integer
9310/// word containing the bit fields is laid out with its least
9311/// significant bit starting on the right hand side, at index 0.
9312///
9313/// Then slowly scroll up starting from there, and take the time to
9314/// read each line and see how the bit fields are laid out and what
9315/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
9316/// the bit fields.
9317///
9318/// DW_AT_bit_offset(n)
9319/// < - - - - - - >
9320/// | | n |
9321/// ^ ^< - - - - >^
9322/// DW_AT_data_bit_offset(n)
9323/// < - - - - - - - - - - - - - - - >
9324/// | |
9325/// ^ ^
9326/// DW_AT_bit_offset(m)
9327/// <--------------------------------->
9328/// | | m |
9329/// ^ ^< - >^
9330/// DW_AT_data_bit_offset(m)
9331/// < - - - - - - - - - - >
9332/// | |
9333/// ^ ^
9334/// DW_AT_bit_offset(k)
9335/// <-------------------------------------------->
9336/// | | k |
9337/// ^ ^< - - >^
9338/// DW_AT_data_bit_offset(k)
9339/// < - - - - >
9340/// | |
9341/// ^ ^
9342/// DW_AT_bit_offset(j)
9343/// <-------------------------------------------------------->
9344/// | |
9345/// ^ ^
9346/// n m k j
9347/// < - - - - - - > < - - - > < - - - - > < - - - >
9348///
9349/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9350/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
9351/// 31 27 23 16 15 11 10 6 5 4 0
9352///
9353/// So, the different bit fields all fit in one 32 bits word, assuming
9354/// the bit fields are tightly packed.
9355///
9356/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
9357/// on this little endian machine and let's see how it relates to
9358/// DW_AT_data_bit_offset of j.
9359///
9360/// DW_AT_bit_offset(j) would be equal to the number of bits from the
9361/// left of the 32 bits word (i.e from bit number 31) to the most
9362/// significant bit of the j bit field (i.e, bit number 4). Thus:
9363///
9364/// DW_AT_bit_offset(j) =
9365/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
9366///
9367/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
9368/// 32 bits word (i.e, bit number 0) to the lest significant bit of
9369/// the 'j' bit field (ie, bit number 0). Thus:
9370///
9371/// DW_AT_data_bit_offset(j) = 0.
9372///
9373/// More generally, we can notice that:
9374///
9375/// sizeof_in_bits(int) =
9376/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
9377///
9378/// It follows that:
9379///
9380/// DW_AT_data_bit_offset(j) =
9381/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
9382///
9383/// Thus:
9384///
9385/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
9386///
9387/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
9388/// from the right hand side of the word. It is what we would
9389/// intuitively think it is. DW_AT_bit_offset however is super
9390/// counter-intuitive, pfff.
9391///
9392/// Anyway, this general equation holds true for all bit fields.
9393///
9394/// Similarly, it follows that:
9395///
9396/// DW_AT_bit_offset(k) =
9397/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
9398///
9399/// Thus:
9400/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
9401///
9402///
9403/// Likewise:
9404///
9405/// DW_AT_bit_offset(m) =
9406/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
9407///
9408///
9409/// Thus:
9410/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
9411///
9412/// And:
9413///
9414///
9415/// Lastly:
9416///
9417/// DW_AT_bit_offset(n) =
9418/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
9419///
9420/// Thus:
9421/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
9422///
9423/// Luckily, the body of the function is much smaller than this
9424/// comment. Enjoy!
9425///
9426/// @param die the DIE to consider.
9427///
9428/// @param is_big_endian this is true iff the machine we are looking at
9429/// is big endian.
9430///
9431/// @param offset this is the output parameter into which the value of
9432/// the DW_AT_bit_offset is put, converted as if it was the value of
9433/// the DW_AT_data_bit_offset parameter, less the contribution of
9434/// DW_AT_data_member_location. This parameter is set iff the
9435/// function returns true.
9436///
9437/// @return true if DW_AT_bit_offset was found on @p die.
9438static bool
9439read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
9440 bool is_big_endian,
9441 uint64_t &offset)
9442{
9443 uint64_t off = 0;
9444 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
9445 return false;
9446
9447 if (is_big_endian)
9448 {
9449 offset = off;
9450 return true;
9451 }
9452
9453 // Okay, we are looking at a little endian machine. We need to
9454 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
9455 // have been. To understand this, you really need to read the
9456 // preliminary comment of this function.
9457 uint64_t containing_anonymous_object_size = 0;
9458 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
9459 containing_anonymous_object_size));
9460 containing_anonymous_object_size *= 8;
9461
9462 uint64_t bitfield_size = 0;
9463 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
9464 bitfield_size));
9465
9466 // As noted in the the preliminary comment of this function if we
9467 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
9468 // its DW_AT_bit_offset value, the equation is:
9469 //
9470 // DW_AT_data_bit_offset(k) =
9471 // sizeof_in_bits(containing_anonymous_object_size)
9472 // - DW_AT_data_bit_offset(k)
9473 // - sizeof_in_bits(k)
9474 offset = containing_anonymous_object_size - off - bitfield_size;
9475
9476 return true;
9477}
9478
9479/// Get the value of the DW_AT_data_member_location of the given DIE
9480/// attribute as an constant.
9481///
9482/// @param die the DIE to read the attribute from.
9483///
9484/// @param offset the attribute as a constant value. This is set iff
9485/// the function returns true.
9486///
9487/// @return true if the attribute exists and has a constant value. In
9488/// that case the offset is set to the value.
9489static bool
9490die_constant_data_member_location(const Dwarf_Die *die,
9491 int64_t& offset)
9492{
9493 if (!die)
9494 return false;
9495
9496 Dwarf_Attribute attr;
9497 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
9498 DW_AT_data_member_location,
9499 &attr))
9500 return false;
9501
9502 Dwarf_Word val;
9503 if (dwarf_formudata(&attr, &val) != 0)
9504 return false;
9505
9506 offset = val;
9507 return true;
9508}
9509
9510/// Get the offset of a struct/class member as represented by the
9511/// value of the DW_AT_data_member_location attribute.
9512///
9513/// There is a huge gotcha in here. The value of the
9514/// DW_AT_data_member_location is not necessarily a constant that one
9515/// would just read and be done with it. Rather, it can be a DWARF
9516/// expression that one has to interpret. In general, the offset can
9517/// be given by the DW_AT_data_bit_offset or by the
9518/// DW_AT_data_member_location attribute and optionally the
9519/// DW_AT_bit_offset attribute. The bit offset attributes are
9520/// always simple constants, but the DW_AT_data_member_location
9521/// attribute is a DWARF location expression.
9522///
9523/// When it's the DW_AT_data_member_location that is present,
9524/// there are three cases to possibly take into account:
9525///
9526/// 1/ The offset in the vtable where the offset of a virtual base
9527/// can be found, aka vptr offset. Given the address of a
9528/// given object O, the vptr offset for B is given by the
9529/// (DWARF) expression:
9530///
9531/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
9532///
9533/// where VIRTUAL_OFFSET is a constant value; In this case,
9534/// this function returns the constant VIRTUAL_OFFSET, as this
9535/// is enough to detect changes in a given virtual base
9536/// relative to the other virtual bases.
9537///
9538/// 2/ The offset of a regular data member. Given the address of
9539/// a struct object named O, the memory location for a
9540/// particular data member is given by the (DWARF) expression:
9541///
9542/// address(O) + OFFSET
9543///
9544/// where OFFSET is a constant. In this case, this function
9545/// returns the OFFSET constant.
9546///
9547/// 3/ The offset of a virtual member function in the virtual
9548/// pointer. The DWARF expression is a constant that designates
9549/// the offset of the function in the vtable. In this case this
9550/// function returns that constant.
9551///
9552/// @param rdr the DWARF reader to consider.
9553///
9554/// @param die the DIE to read the information from.
9555///
9556/// @param offset the resulting constant offset, in bits. This
9557/// argument is set iff the function returns true.
9558static bool
9559die_member_offset(const reader& rdr,
9560 const Dwarf_Die* die,
9561 int64_t& offset)
9562{
9563 Dwarf_Op* expr = NULL;
9564 size_t expr_len = 0;
9565 uint64_t bit_offset = 0;
9566
9567 // First let's see if the DW_AT_data_bit_offset attribute is
9568 // present.
9569 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
9570 {
9571 offset = bit_offset;
9572 return true;
9573 }
9574
9575 // First try to read DW_AT_data_member_location as a plain constant.
9576 // We do this because the generic method using die_location_expr
9577 // might hit a bug in elfutils libdw dwarf_location_expression only
9578 // fixed in elfutils 0.184+. The bug only triggers if the attribute
9579 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
9580 // handle all constants here because that is more consistent (and
9581 // slightly faster in the general case where the attribute isn't a
9582 // full DWARF expression).
9583 if (!die_constant_data_member_location(die, offset))
9584 {
9585 // Otherwise, let's see if the DW_AT_data_member_location
9586 // attribute and, optionally, the DW_AT_bit_offset attributes
9587 // are present.
9588 if (!die_location_expr(die, DW_AT_data_member_location,
9589 &expr, &expr_len))
9590 return false;
9591
9592 // The DW_AT_data_member_location attribute is present. Let's
9593 // evaluate it and get its constant sub-expression and return
9594 // that one.
9595 if (!eval_quickly(expr, expr_len, offset))
9596 {
9597 bool is_tls_address = false;
9598 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
9599 offset, is_tls_address,
9600 rdr.dwarf_expr_eval_ctxt()))
9601 return false;
9602 }
9603 }
9604 offset *= 8;
9605
9606 // On little endian machines, we need to convert the
9607 // DW_AT_bit_offset attribute into a relative offset to 8 *
9608 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
9609 // would be if it were used instead.
9610 //
9611 // In other words, before adding it to 8 *
9612 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9613 // converted into a human-understandable form that represents the
9614 // offset of the bitfield data member it describes. For details
9615 // about the conversion, please read the extensive comments of
9616 // read_and_convert_DW_at_bit_offset.
9617 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9618 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9619 offset += bit_offset;
9620
9621 return true;
9622}
9623
9624/// Read the value of the DW_AT_location attribute from a DIE,
9625/// evaluate the resulting DWARF expression and, if it's a constant
9626/// expression, return it.
9627///
9628/// @param die the DIE to consider.
9629///
9630/// @param address the resulting constant address. This is set iff
9631/// the function returns true.
9632///
9633/// @return true iff the whole sequence of action described above
9634/// could be completed normally.
9635static bool
9636die_location_address(Dwarf_Die* die,
9637 Dwarf_Addr& address,
9638 bool& is_tls_address)
9639{
9640 Dwarf_Op* expr = NULL;
9641 size_t expr_len = 0;
9642
9643 is_tls_address = false;
9644
9645 if (!die)
9646 return false;
9647
9648 Dwarf_Attribute attr;
9649 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9650 return false;
9651
9652 if (dwarf_getlocation(&attr, &expr, &expr_len))
9653 return false;
9654 // Ignore location expressions where reading them succeeded but
9655 // their length is 0.
9656 if (expr_len == 0)
9657 return false;
9658
9659 Dwarf_Attribute result;
9660 if (!dwarf_getlocation_attr(&attr, expr, &result))
9661 // A location that has been interpreted as an address.
9662 return !dwarf_formaddr(&result, &address);
9663
9664 // Just get the address out of the number field.
9665 address = expr->number;
9666 return true;
9667}
9668
9669/// Return the index of a function in its virtual table. That is,
9670/// return the value of the DW_AT_vtable_elem_location attribute.
9671///
9672/// @param die the DIE of the function to consider.
9673///
9674/// @param vindex the resulting index. This is set iff the function
9675/// returns true.
9676///
9677/// @return true if the DIE has a DW_AT_vtable_elem_location
9678/// attribute.
9679static bool
9680die_virtual_function_index(Dwarf_Die* die,
9681 int64_t& vindex)
9682{
9683 if (!die)
9684 return false;
9685
9686 Dwarf_Op* expr = NULL;
9687 size_t expr_len = 0;
9688 if (die_is_virtual(die))
9689 vindex = 0;
9690 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9691 &expr, &expr_len))
9692 return false;
9693
9694 int64_t i = 0;
9695 bool is_tls_addr = false;
9696 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9697 return false;
9698
9699 vindex = i;
9700 return true;
9701}
9702
9703/// Test if a given DIE represents an anonymous type.
9704///
9705/// Anonymous types we are interested in are classes, unions and
9706/// enumerations.
9707///
9708/// @param die the DIE to consider.
9709///
9710/// @return true iff @p die represents an anonymous type.
9711bool
9713{
9714 int tag = dwarf_tag(die);
9715
9716 if (tag == DW_TAG_class_type
9717 || tag == DW_TAG_structure_type
9718 || tag == DW_TAG_union_type
9719 || tag == DW_TAG_enumeration_type)
9720 return die_is_anonymous(die);
9721
9722 return false;
9723}
9724
9725/// Return the base of the internal name to represent an anonymous
9726/// type.
9727///
9728/// Typically, anonymous enums would be named
9729/// __anonymous_enum__<number>, anonymous struct or classes would be
9730/// named __anonymous_struct__<number> and anonymous unions would be
9731/// named __anonymous_union__<number>. The first part of these
9732/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9733/// the base name. This function returns that base name, depending on
9734/// the kind of type DIE we are looking at.
9735///
9736/// @param die the type DIE to look at. This function expects a type
9737/// DIE with an empty DW_AT_name property value (anonymous).
9738///
9739/// @return a string representing the base of the internal anonymous
9740/// name.
9741static string
9742get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9743{
9744 ABG_ASSERT(die_is_type(die));
9745 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9746
9747 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9748 string type_name;
9749 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9751 else if (tag == DW_TAG_union_type)
9753 else if (tag == DW_TAG_enumeration_type)
9755
9756 return type_name;
9757}
9758
9759/// Build a full internal anonymous type name.
9760///
9761/// @param base_name this is the base name as returned by the function
9762/// @ref get_internal_anonymous_die_prefix_name.
9763///
9764/// @param anonymous_type_index this is the index of the anonymous
9765/// type in its scope. That is, if there are more than one anonymous
9766/// types of a given kind in a scope, this index is what tells them
9767/// appart, starting from 0.
9768///
9769/// @return the built string, which is a concatenation of @p base_name
9770/// and @p anonymous_type_index.
9771static string
9772build_internal_anonymous_die_name(const string &base_name,
9773 size_t anonymous_type_index)
9774{
9775 string name = base_name;
9776 if (anonymous_type_index && !base_name.empty())
9777 {
9778 std::ostringstream o;
9779 o << base_name << anonymous_type_index;
9780 name = o.str();
9781 }
9782 return name;
9783}
9784
9785// ------------------------------------
9786// <DIE pretty printer>
9787// ------------------------------------
9788
9789/// Compute the qualified name of a DIE that represents a type.
9790///
9791/// For instance, if the DIE tag is DW_TAG_subprogram then this
9792/// function computes the name of the function *type*.
9793///
9794/// @param rdr the DWARF reader.
9795///
9796/// @param die the DIE to consider.
9797///
9798/// @param where_offset where in the are logically are in the DIE
9799/// stream.
9800///
9801/// @param guard the set of DIE offsets of the stack of DIEs involved
9802/// in the construction of the qualified name of the type. This set
9803/// is used to detect (and avoid) cycles in the stack of DIEs that is
9804/// going to be walked to compute the qualified type name.
9805///
9806/// @return a copy of the qualified name of the type.
9807static string
9808die_qualified_type_name(const reader& rdr,
9809 const Dwarf_Die* die,
9810 size_t where_offset,
9811 unordered_set<uint64_t>& guard)
9812{
9813 if (!die)
9814 return "";
9815
9816 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9817 if (tag == DW_TAG_compile_unit
9818 || tag == DW_TAG_partial_unit
9819 || tag == DW_TAG_type_unit)
9820 return "";
9821
9822 string name = die_name(die);
9823
9824 Dwarf_Die scope_die;
9825 if (!get_scope_die(rdr, die, where_offset, scope_die))
9826 return "";
9827
9828 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9829 string separator = colon_colon ? "::" : ".";
9830
9831 string repr;
9832
9833 switch (tag)
9834 {
9835 case DW_TAG_unspecified_type:
9836 break;
9837
9838 case DW_TAG_base_type:
9839 {
9840 abigail::ir::real_type real_type;
9841 if (parse_real_type(name, real_type))
9842 repr = real_type;
9843 else
9844 repr = name;
9845 }
9846 break;
9847
9848 case DW_TAG_typedef:
9849 ABG_ASSERT(!name.empty());
9850 // fall through
9851
9852 case DW_TAG_enumeration_type:
9853 case DW_TAG_structure_type:
9854 case DW_TAG_class_type:
9855 case DW_TAG_union_type:
9856 {
9857 if (die_is_anonymous(die))
9858 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
9859 /*one_line=*/true,
9860 /*qualed_name=*/false,
9861 where_offset, guard);
9862 else
9863 {
9864 string parent_name = die_qualified_name(rdr, &scope_die,
9865 where_offset, guard);
9866 repr = parent_name.empty() ? name : parent_name + separator + name;
9867 }
9868 }
9869 break;
9870
9871 case DW_TAG_const_type:
9872 case DW_TAG_volatile_type:
9873 case DW_TAG_restrict_type:
9874 {
9875 Dwarf_Die underlying_type_die;
9876 bool has_underlying_type_die =
9877 die_die_attribute(die, DW_AT_type, underlying_type_die);
9878
9879 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9880 break;
9881
9882 if (tag == DW_TAG_const_type)
9883 {
9884 if (has_underlying_type_die
9885 && die_is_reference_type(&underlying_type_die))
9886 // A reference is always const. So, to lower false
9887 // positive reports in diff computations, we consider a
9888 // const reference just as a reference. But we need to
9889 // keep the qualified-ness of the type. So we introduce
9890 // a 'no-op' qualifier here. Please remember that this
9891 // has to be kept in sync with what is done in
9892 // get_name_of_qualified_type. So if you change this
9893 // here, you have to change that code there too.
9894 repr = "";
9895 else if (!has_underlying_type_die
9896 || die_is_void_type(&underlying_type_die))
9897 {
9898 repr = "void";
9899 break;
9900 }
9901 else
9902 repr = "const";
9903 }
9904 else if (tag == DW_TAG_volatile_type)
9905 repr = "volatile";
9906 else if (tag == DW_TAG_restrict_type)
9907 repr = "restrict";
9908 else
9910
9911 string underlying_type_repr;
9912 if (has_underlying_type_die)
9913 underlying_type_repr =
9914 die_qualified_type_name(rdr, &underlying_type_die,
9915 where_offset, guard);
9916 else
9917 underlying_type_repr = "void";
9918
9919 if (underlying_type_repr.empty())
9920 repr.clear();
9921 else
9922 {
9923 if (has_underlying_type_die)
9924 {
9925 Dwarf_Die peeled;
9926 die_peel_qualified(&underlying_type_die, peeled);
9927 if (die_is_pointer_or_reference_type(&peeled))
9928 repr = underlying_type_repr + " " + repr;
9929 else
9930 repr += " " + underlying_type_repr;
9931 }
9932 else
9933 repr += " " + underlying_type_repr;
9934 }
9935 }
9936 break;
9937
9938 case DW_TAG_pointer_type:
9939 case DW_TAG_reference_type:
9940 case DW_TAG_rvalue_reference_type:
9941 {
9942 Dwarf_Die pointed_to_type_die;
9943 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9944 {
9945 if (tag == DW_TAG_pointer_type)
9946 repr = "void*";
9947 break;
9948 }
9949
9950 if (die_is_unspecified(&pointed_to_type_die))
9951 break;
9952
9953 string pointed_type_repr =
9954 die_qualified_type_name(rdr, &pointed_to_type_die,
9955 where_offset, guard);
9956
9957 repr = pointed_type_repr;
9958 if (repr.empty())
9959 break;
9960
9961 if (tag == DW_TAG_pointer_type)
9962 repr += "*";
9963 else if (tag == DW_TAG_reference_type)
9964 repr += "&";
9965 else if (tag == DW_TAG_rvalue_reference_type)
9966 repr += "&&";
9967 else
9969 }
9970 break;
9971
9972 case DW_TAG_subrange_type:
9973 {
9974 // In Ada, this one can be generated on its own, that is, not
9975 // as a sub-type of an array. So we need to support it on its
9976 // own. Note that when it's emitted as the sub-type of an
9977 // array like in C and C++, this is handled differently, for
9978 // now. But we try to make this usable by other languages
9979 // that are not Ada, even if we modelled it after Ada.
9980
9981 // So we build a subrange type for the sole purpose of using
9982 // the ::as_string() method of that type. So we don't add
9983 // that type to the current type tree being built.
9985 build_subrange_type(const_cast<reader&>(rdr),
9986 die, where_offset,
9987 /*associate_die_to_type=*/false);
9988 repr += s->as_string();
9989 break;
9990 }
9991
9992 case DW_TAG_array_type:
9993 {
9994 Dwarf_Die element_type_die;
9995 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9996 break;
9997 string element_type_name =
9998 die_qualified_type_name(rdr, &element_type_die, where_offset, guard);
9999 if (element_type_name.empty())
10000 break;
10001
10003 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
10004 die, subranges, where_offset,
10005 /*associate_type_to_die=*/false);
10006
10007 repr = element_type_name;
10009 }
10010 break;
10011
10012 case DW_TAG_subroutine_type:
10013 case DW_TAG_subprogram:
10014 {
10015 string return_type_name;
10016 string class_name;
10017 vector<string> parm_names;
10018 bool is_const = false;
10019 bool is_static = false;
10020 bool is_method_type = false;
10021 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10022 /*pretty_print=*/true,
10023 /*qualified_name=*/true,
10025 return_type_name, class_name,
10026 parm_names, is_const,
10027 is_static, guard);
10028 if (return_type_name.empty())
10029 return_type_name = "void";
10030
10031 repr = return_type_name;
10032
10033 if (is_method_type)
10034 // This is a method, so print the class name.
10035 repr += " (" + class_name + "::*)";
10036
10037 // Now parameters.
10038 repr += " (";
10039 for (vector<string>::const_iterator i = parm_names.begin();
10040 i != parm_names.end();
10041 ++i)
10042 {
10043 if (i != parm_names.begin())
10044 repr += ", ";
10045 repr += *i;
10046 }
10047 repr += ")";
10048
10049 }
10050 break;
10051
10052 case DW_TAG_string_type:
10053 case DW_TAG_ptr_to_member_type:
10054 case DW_TAG_set_type:
10055 case DW_TAG_file_type:
10056 case DW_TAG_packed_type:
10057 case DW_TAG_thrown_type:
10058 case DW_TAG_interface_type:
10059 case DW_TAG_shared_type:
10060 break;
10061 }
10062
10063 return repr;
10064}
10065
10066/// Compute the name of a type represented by a DIE.
10067///
10068/// @param rdr the reader to use.
10069///
10070/// @param die the type DIE to consider.
10071///
10072/// @param qualified_name if true then compute a qualified name.
10073///
10074/// @param where_offset where in the are logically are in the DIE
10075/// stream.
10076///
10077/// @param guard the set of DIE offsets of the stack of DIEs involved
10078/// in the construction of the name of the type. This set is used to
10079/// detect (and avoid) cycles in the stack of DIEs that is going to be
10080/// walked to compute the type name.
10081///
10082/// @return a copy of the string representing the type represented by
10083/// @p die.
10084static string
10085die_type_name(const reader& rdr,
10086 const Dwarf_Die* die,
10087 bool qualified_name,
10088 size_t where_offset,
10089 unordered_set<uint64_t>& guard)
10090{
10091 if (!die)
10092 return "";
10093
10094 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
10095 if (tag == DW_TAG_compile_unit
10096 || tag == DW_TAG_partial_unit
10097 || tag == DW_TAG_type_unit)
10098 return "";
10099
10100 string name = die_name(die);
10101
10102 Dwarf_Die scope_die;
10103 if (!get_scope_die(rdr, die, where_offset, scope_die))
10104 return "";
10105
10106 bool colon_colon = die_is_type(die) || die_is_namespace(die);
10107 string separator = colon_colon ? "::" : ".";
10108
10109 string repr;
10110
10111 switch (tag)
10112 {
10113 case DW_TAG_unspecified_type:
10114 break;
10115
10116 case DW_TAG_base_type:
10117 {
10118 abigail::ir::real_type int_type;
10119 if (parse_real_type(name, int_type))
10120 repr = int_type;
10121 else
10122 repr = name;
10123 }
10124 break;
10125
10126 case DW_TAG_typedef:
10127 ABG_ASSERT(!name.empty());
10128 // fall through
10129
10130 case DW_TAG_enumeration_type:
10131 case DW_TAG_structure_type:
10132 case DW_TAG_class_type:
10133 case DW_TAG_union_type:
10134 {
10135 if (die_is_anonymous(die))
10136 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
10137 /*one_line=*/true,
10138 /*qualed_name=*/false,
10139 where_offset,
10140 guard);
10141 else
10142 {
10143 string parent_name;
10144 if (qualified_name)
10145 {
10146 if (!is_anonymous_type_die(&scope_die))
10147 parent_name = die_qualified_name(rdr, &scope_die,
10148 where_offset, guard);
10149 }
10150 repr = parent_name.empty() ? name : parent_name + separator + name;
10151 }
10152 }
10153 break;
10154
10155 case DW_TAG_const_type:
10156 case DW_TAG_volatile_type:
10157 case DW_TAG_restrict_type:
10158 {
10159 Dwarf_Die underlying_type_die;
10160 bool has_underlying_type_die =
10161 die_die_attribute(die, DW_AT_type, underlying_type_die);
10162
10163 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
10164 break;
10165
10166 if (tag == DW_TAG_const_type)
10167 {
10168 if (has_underlying_type_die
10169 && die_is_reference_type(&underlying_type_die))
10170 // A reference is always const. So, to lower false
10171 // positive reports in diff computations, we consider a
10172 // const reference just as a reference. But we need to
10173 // keep the qualified-ness of the type. So we introduce
10174 // a 'no-op' qualifier here. Please remember that this
10175 // has to be kept in sync with what is done in
10176 // get_name_of_qualified_type. So if you change this
10177 // here, you have to change that code there too.
10178 repr = "";
10179 else if (!has_underlying_type_die
10180 || die_is_void_type(&underlying_type_die))
10181 {
10182 repr = "void";
10183 break;
10184 }
10185 else
10186 repr = "const";
10187 }
10188 else if (tag == DW_TAG_volatile_type)
10189 repr = "volatile";
10190 else if (tag == DW_TAG_restrict_type)
10191 repr = "restrict";
10192 else
10194
10195 string underlying_type_repr;
10196 if (has_underlying_type_die)
10197 underlying_type_repr =
10198 die_type_name(rdr, &underlying_type_die,
10199 qualified_name, where_offset,
10200 guard);
10201 else
10202 underlying_type_repr = "void";
10203
10204 if (underlying_type_repr.empty())
10205 repr.clear();
10206 else
10207 {
10208 if (has_underlying_type_die)
10209 {
10210 Dwarf_Die peeled;
10211 die_peel_qualified(&underlying_type_die, peeled);
10212 if (die_is_pointer_or_reference_type(&peeled))
10213 repr = underlying_type_repr + " " + repr;
10214 else
10215 repr += " " + underlying_type_repr;
10216 }
10217 else
10218 repr += " " + underlying_type_repr;
10219 }
10220 }
10221 break;
10222
10223 case DW_TAG_pointer_type:
10224 case DW_TAG_reference_type:
10225 case DW_TAG_rvalue_reference_type:
10226 {
10227 Dwarf_Die pointed_to_type_die;
10228 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
10229 {
10230 if (tag == DW_TAG_pointer_type)
10231 repr = "void*";
10232 break;
10233 }
10234
10235 if (die_is_unspecified(&pointed_to_type_die))
10236 break;
10237
10238 string pointed_type_repr =
10239 die_type_name(rdr, &pointed_to_type_die,
10240 qualified_name, where_offset,
10241 guard);
10242
10243 repr = pointed_type_repr;
10244 if (repr.empty())
10245 break;
10246
10247 if (tag == DW_TAG_pointer_type)
10248 repr += "*";
10249 else if (tag == DW_TAG_reference_type)
10250 repr += "&";
10251 else if (tag == DW_TAG_rvalue_reference_type)
10252 repr += "&&";
10253 else
10255 }
10256 break;
10257
10258 case DW_TAG_subrange_type:
10259 {
10260 // In Ada, this one can be generated on its own, that is, not
10261 // as a sub-type of an array. So we need to support it on its
10262 // own. Note that when it's emitted as the sub-type of an
10263 // array like in C and C++, this is handled differently, for
10264 // now. But we try to make this usable by other languages
10265 // that are not Ada, even if we modelled it after Ada.
10266
10267 // So we build a subrange type for the sole purpose of using
10268 // the ::as_string() method of that type. So we don't add
10269 // that type to the current type tree being built.
10271 build_subrange_type(const_cast<reader&>(rdr),
10272 die, where_offset,
10273 /*associate_die_to_type=*/false);
10274 repr += s->as_string();
10275 break;
10276 }
10277
10278 case DW_TAG_array_type:
10279 {
10280 Dwarf_Die element_type_die;
10281 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10282 break;
10283 string element_type_name =
10284 die_type_name(rdr, &element_type_die,
10285 qualified_name, where_offset,
10286 guard);
10287 if (element_type_name.empty())
10288 break;
10289
10291 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
10292 die, subranges, where_offset,
10293 /*associate_type_to_die=*/false);
10294
10295 repr = element_type_name;
10297 }
10298 break;
10299
10300 case DW_TAG_subroutine_type:
10301 case DW_TAG_subprogram:
10302 {
10303 string return_type_name;
10304 string class_name;
10305 vector<string> parm_names;
10306 bool is_const = false;
10307 bool is_static = false;
10308 bool is_method_type = false;
10309 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10310 /*pretty_print=*/true,
10311 qualified_name,
10313 return_type_name,
10314 class_name,
10315 parm_names, is_const,
10316 is_static, guard);
10317 if (return_type_name.empty())
10318 return_type_name = "void";
10319
10320 repr = return_type_name;
10321
10322 if (is_method_type)
10323 {
10324 // This is a method, so print the class name.
10325 repr += " (" + class_name + "::*)";
10326 }
10327
10328 // Now parameters.
10329 repr += " (";
10330 for (vector<string>::const_iterator i = parm_names.begin();
10331 i != parm_names.end();
10332 ++i)
10333 {
10334 if (i != parm_names.begin())
10335 repr += ", ";
10336 repr += *i;
10337 }
10338 repr += ")";
10339
10340 }
10341 break;
10342
10343 case DW_TAG_string_type:
10344 case DW_TAG_ptr_to_member_type:
10345 case DW_TAG_set_type:
10346 case DW_TAG_file_type:
10347 case DW_TAG_packed_type:
10348 case DW_TAG_thrown_type:
10349 case DW_TAG_interface_type:
10350 case DW_TAG_shared_type:
10351 break;
10352 }
10353
10354 return repr;
10355}
10356
10357/// Compute the name of a type represented by a DIE.
10358///
10359/// @param rdr the reader to use.
10360///
10361/// @param die the type DIE to consider.
10362///
10363/// @param qualified_name if true then compute a qualified name.
10364///
10365/// @param where_offset where in the are logically are in the DIE
10366/// stream.
10367///
10368/// @return a copy of the string representing the type represented by
10369/// @p die.
10370static string
10371die_type_name(const reader& rdr,
10372 const Dwarf_Die* die,
10373 bool qualified_name,
10374 size_t where_offset)
10375{
10376 unordered_set<uint64_t> guard;
10377 return die_type_name(rdr, die, qualified_name, where_offset, guard);
10378}
10379
10380/// Compute the qualified name of a decl represented by a given DIE.
10381///
10382/// For instance, for a DIE of tag DW_TAG_subprogram this function
10383/// computes the signature of the function *declaration*.
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 decl. This set
10393/// is used to detect (and avoid) cycles in the stack of DIEs that is
10394/// going to be walked to compute the qualified decl name.
10395///
10396/// @return a copy of the computed name.
10397static string
10398die_qualified_decl_name(const reader& rdr,
10399 const Dwarf_Die* die,
10400 size_t where_offset,
10401 unordered_set<uint64_t>& guard)
10402{
10403 if (!die || !die_is_decl(die))
10404 return "";
10405
10406 string name = die_name(die);
10407
10408 Dwarf_Die scope_die;
10409 if (!get_scope_die(rdr, die, where_offset, scope_die))
10410 return "";
10411
10412 string scope_name = die_qualified_name(rdr, &scope_die, where_offset, guard);
10413 string separator = "::";
10414
10415 string repr;
10416
10417 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10418 switch (tag)
10419 {
10420 case DW_TAG_namespace:
10421 case DW_TAG_member:
10422 case DW_TAG_variable:
10423 repr = scope_name.empty() ? name : scope_name + separator + name;
10424 break;
10425 case DW_TAG_subprogram:
10426 repr = die_function_signature(rdr, die,
10427 /*qualified_name=*/true,
10428 where_offset, guard);
10429 break;
10430
10431 case DW_TAG_unspecified_parameters:
10432 repr = "...";
10433 break;
10434
10435 case DW_TAG_formal_parameter:
10436 case DW_TAG_imported_declaration:
10437 case DW_TAG_GNU_template_template_param:
10438 case DW_TAG_GNU_template_parameter_pack:
10439 case DW_TAG_GNU_formal_parameter_pack:
10440 break;
10441 }
10442 return repr;
10443}
10444
10445/// Compute the qualified name of the artifact represented by a given
10446/// DIE.
10447///
10448/// If the DIE represents a type, then the function computes the name
10449/// of the type. Otherwise, if the DIE represents a decl then the
10450/// function computes the name of the decl. Note that a DIE of tag
10451/// DW_TAG_subprogram is going to be considered as a "type" -- just
10452/// like if it was a DW_TAG_subroutine_type.
10453///
10454/// @param rdr the DWARF reader.
10455///
10456/// @param die the DIE to consider.
10457///
10458/// @param where_offset where we are logically at in the DIE stream.
10459///
10460/// @param guard the set of DIE offsets of the stack of DIEs involved
10461/// in the construction of the qualified name of the DIE. This set is
10462/// used to detect (and avoid) cycles in the stack of DIEs that is
10463/// going to be walked to compute the qualified DIE name.
10464///
10465/// @return a copy of the computed name.
10466static string
10467die_qualified_name(const reader& rdr, const Dwarf_Die* die,
10468 size_t where, unordered_set<uint64_t>& guard)
10469{
10470 if (die_is_type(die))
10471 return die_qualified_type_name(rdr, die, where, guard);
10472 else if (die_is_decl(die))
10473 return die_qualified_decl_name(rdr, die, where, guard);
10474 return "";
10475}
10476
10477/// Compute the qualified name of the artifact represented by a given
10478/// DIE.
10479///
10480/// If the DIE represents a type, then the function computes the name
10481/// of the type. Otherwise, if the DIE represents a decl then the
10482/// function computes the name of the decl. Note that a DIE of tag
10483/// DW_TAG_subprogram is going to be considered as a "type" -- just
10484/// like if it was a DW_TAG_subroutine_type.
10485///
10486/// @param rdr the DWARF reader.
10487///
10488/// @param die the DIE to consider.
10489///
10490/// @param where_offset where we are logically at in the DIE stream.
10491///
10492/// @return a copy of the computed name.
10493static string
10494die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
10495{
10496 unordered_set<uint64_t> guard;
10497 return die_qualified_name(rdr, die, where, guard);
10498}
10499
10500/// Test if the qualified name of a given type should be empty.
10501///
10502/// The reason why the name of a DIE with a given tag would be empty
10503/// is that libabigail's internal representation doesn't yet support
10504/// that tag; or if the DIE's qualified name is built from names of
10505/// sub-types DIEs whose tags are not yet supported.
10506///
10507/// @param rdr the DWARF reader.
10508///
10509/// @param die the DIE to consider.
10510///
10511/// @param where where we are logically at, in the DIE stream.
10512///
10513/// @param qualified_name the qualified name of the DIE. This is set
10514/// only iff the function returns false.
10515///
10516/// @param guard the set of DIE offsets of the stack of DIEs involved
10517/// in the construction of the qualified name of the type. This set
10518/// is used to detect (and avoid) cycles in the stack of DIEs that is
10519/// going to be walked to compute the qualified type name.
10520///
10521/// @return true if the qualified name of the DIE is empty.
10522static bool
10523die_qualified_type_name_empty(const reader& rdr,
10524 const Dwarf_Die* die,
10525 size_t where, string &qualified_name,
10526 unordered_set<uint64_t>& guard)
10527{
10528 if (!die)
10529 return true;
10530
10531 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10532
10533 string qname;
10534 if (tag == DW_TAG_typedef
10535 || tag == DW_TAG_pointer_type
10536 || tag == DW_TAG_reference_type
10537 || tag == DW_TAG_rvalue_reference_type
10538 || tag == DW_TAG_array_type
10539 || tag == DW_TAG_const_type
10540 || tag == DW_TAG_volatile_type
10541 || tag == DW_TAG_restrict_type)
10542 {
10543 Dwarf_Die underlying_type_die;
10544 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
10545 {
10546 string name =
10547 die_qualified_type_name(rdr, &underlying_type_die, where, guard);
10548 if (name.empty())
10549 return true;
10550 }
10551 }
10552 else
10553 {
10554 string name = die_qualified_type_name(rdr, die, where, guard);
10555 if (name.empty())
10556 return true;
10557 }
10558
10559 qname = die_qualified_type_name(rdr, die, where, guard);
10560 if (qname.empty())
10561 return true;
10562
10563 qualified_name = qname;
10564 return false;
10565}
10566
10567/// Given the DIE that represents a function type, compute the names
10568/// of the following properties the function's type:
10569///
10570/// - return type
10571/// - enclosing class (if the function is a member function)
10572/// - function parameter types
10573///
10574/// When the function we are looking at is a member function, it also
10575/// tells if it's const.
10576///
10577/// @param rdr the DWARF reader.
10578///
10579/// @param die the DIE of the function or function type we are looking
10580/// at.
10581///
10582/// @param where_offset where we are logically at in the DIE stream.
10583///
10584/// @param pretty_print if set to yes, the type names are going to be
10585/// pretty-printed names; otherwise, they are just qualified type
10586/// names.
10587///
10588/// @param qualified_name if true then the names returned are
10589/// qualified.
10590///
10591/// @param is_method_type output parameter. This is set by the
10592/// function to true iff the DIE @p die represents a method.
10593///
10594/// @param return_type_name out parameter. This contains the name of
10595/// the return type of the function.
10596///
10597/// @param class_name out parameter. If the function is a member
10598/// function, this contains the name of the enclosing class.
10599///
10600/// @param parm_names out parameter. This vector is set to the names
10601/// of the types of the parameters of the function.
10602///
10603/// @param is_const out parameter. If the function is a member
10604/// function, this is set to true iff the member function is const.
10605///
10606/// @param is_static out parameter. If the function is a static
10607/// member function, then this is set to true.
10608///
10609/// @param guard the set of DIE offsets of the stack of DIEs involved
10610/// in the construction of the qualified name of the function type.
10611/// This set is used to detect (and avoid) cycles in the stack of DIEs
10612/// that is going to be walked to compute the qualified function type
10613/// name.
10614static void
10615die_return_and_parm_names_from_fn_type_die(const reader& rdr,
10616 const Dwarf_Die* die,
10617 size_t where_offset,
10618 bool pretty_print,
10619 bool qualified_name,
10620 bool &is_method_type,
10621 string &return_type_name,
10622 string &class_name,
10623 vector<string>& parm_names,
10624 bool& is_const,
10625 bool& is_static,
10626 unordered_set<uint64_t>& guard)
10627{
10628 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10629 if (guard.find(off) != guard.end())
10630 return;
10631 guard.insert(off);
10632
10633 Dwarf_Die child;
10634 Dwarf_Die ret_type_die;
10635 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
10636 return_type_name = "void";
10637 else
10638 {
10639 return_type_name =
10640 pretty_print
10641 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset, guard)
10642 : die_type_name(rdr, &ret_type_die, qualified_name,
10643 where_offset, guard);
10644 }
10645
10646 if (return_type_name.empty())
10647 return_type_name = "void";
10648
10649 Dwarf_Die object_pointer_die, class_die;
10651 die_function_type_is_method_type(rdr, die, where_offset,
10652 object_pointer_die,
10653 class_die, is_static);
10654
10655 is_const = false;
10656 if (is_method_type)
10657 {
10658 if (!is_anonymous_type_die(&class_die))
10659 class_name = die_type_name(rdr, &class_die, qualified_name,
10660 where_offset, guard);
10661
10662 Dwarf_Die this_pointer_die;
10663 Dwarf_Die pointed_to_die;
10664 if (!is_static
10665 && die_die_attribute(&object_pointer_die, DW_AT_type,
10666 this_pointer_die))
10667 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
10668 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
10669 is_const = true;
10670
10671 string fn_name = die_name(die);
10672 string non_qualified_class_name = die_name(&class_die);
10673 bool is_ctor = fn_name == non_qualified_class_name;
10674 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
10675
10676 if (is_ctor || is_dtor)
10677 return_type_name.clear();
10678 }
10679
10680 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
10681 do
10682 {
10683 int child_tag = dwarf_tag(&child);
10684 bool first_parm = true;
10685 if (child_tag == DW_TAG_formal_parameter)
10686 {
10687 // Skip the first parameter of a method.
10688 if (first_parm)
10689 {
10690 first_parm = false;
10691 if (is_method_type)
10692 continue;
10693 }
10694 Dwarf_Die parm_type_die;
10695 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
10696 continue;
10697 string qname =
10698 pretty_print
10699 ? rdr.get_die_pretty_representation(&parm_type_die,
10700 where_offset, guard)
10701 : die_type_name(rdr, &parm_type_die,
10702 qualified_name, where_offset, guard);
10703
10704 if (qname.empty())
10705 continue;
10706 parm_names.push_back(qname);
10707 }
10708 else if (child_tag == DW_TAG_unspecified_parameters)
10709 {
10710 // This is a variadic function parameter.
10711 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
10712 // After a DW_TAG_unspecified_parameters tag, we shouldn't
10713 // keep reading for parameters. The
10714 // unspecified_parameters TAG should be the last parameter
10715 // that we record. For instance, if there are multiple
10716 // DW_TAG_unspecified_parameters DIEs then we should care
10717 // only for the first one.
10718 break;
10719 }
10720 }
10721 while (dwarf_siblingof(&child, &child) == 0);
10722
10723 if (class_name.empty())
10724 {
10725 Dwarf_Die parent_die;
10726 if (get_parent_die(rdr, die, parent_die, where_offset))
10727 {
10728 if (die_is_class_type(&parent_die)
10729 && !is_anonymous_type_die(&parent_die))
10730 class_name = die_type_name(rdr, &parent_die,
10731 qualified_name,
10732 where_offset,
10733 guard);
10734 }
10735 }
10736
10737 guard.erase(off);
10738}
10739
10740/// This computes the signature of the a function declaration
10741/// represented by a DIE.
10742///
10743/// @param rdr the DWARF reader.
10744///
10745/// @param fn_die the DIE of the function to consider.
10746///
10747/// @param qualified_name if set to true then a qualified name is
10748/// going to be computed.
10749///
10750/// @param where_offset where we are logically at in the stream of
10751/// DIEs.
10752///
10753/// @param guard the set of DIE offsets of the stack of DIEs involved
10754/// in the construction of the signature of the function type. This
10755/// set is used to detect (and avoid) cycles in the stack of DIEs that
10756/// is going to be walked to compute the signature.
10757///
10758/// @return a copy of the computed function signature string.
10759static string
10760die_function_signature(const reader& rdr,
10761 const Dwarf_Die *fn_die,
10762 bool qualified_name,
10763 size_t where_offset,
10764 unordered_set<uint64_t>& guard)
10765{
10766
10768 bool has_lang = false;
10769 if ((has_lang = get_die_language(fn_die, lang)))
10770 {
10771 // In a binary originating from the C language, it's OK to use
10772 // the linkage name of the function as a key for the map which
10773 // is meant to reduce the number of DIE comparisons involved
10774 // during DIE canonicalization computation.
10775 if (is_c_language(lang))
10776 {
10777 string fn_name = die_linkage_name(fn_die);
10778 if (fn_name.empty())
10779 fn_name = die_name(fn_die);
10780 return fn_name;
10781 }
10782 }
10783
10784 // TODO: When we can structurally compare DIEs originating from C++
10785 // as well, we can use the linkage name of functions in C++ too, to
10786 // reduce the number of comparisons involved during DIE
10787 // canonicalization.
10788
10789 string return_type_name;
10790 Dwarf_Die ret_type_die;
10791 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
10792 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
10793 where_offset,
10794 guard);
10795
10796 if (return_type_name.empty())
10797 return_type_name = "void";
10798
10799 Dwarf_Die scope_die;
10800 string scope_name;
10801 if (qualified_name && get_scope_die(rdr, fn_die, where_offset, scope_die))
10802 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset, guard);
10803 string fn_name = die_name(fn_die);
10804 if (!scope_name.empty())
10805 fn_name = scope_name + "::" + fn_name;
10806
10807 string class_name;
10808 vector<string> parm_names;
10809 bool is_const = false;
10810 bool is_static = false;
10811 bool is_method_type = false;
10812
10813 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
10814 /*pretty_print=*/false,
10815 qualified_name, is_method_type,
10816 return_type_name, class_name,
10817 parm_names, is_const, is_static,
10818 guard);
10819
10820 bool is_virtual = die_is_virtual(fn_die);
10821
10822 string repr = is_method_type? "method" : "function";
10823 if (is_virtual)
10824 repr += " virtual";
10825
10826 if (!return_type_name.empty())
10827 repr += " " + return_type_name;
10828
10829 repr += " " + fn_name;
10830
10831 // Now parameters.
10832 repr += "(";
10833 bool some_parm_emitted = false;
10834 for (vector<string>::const_iterator i = parm_names.begin();
10835 i != parm_names.end();
10836 ++i)
10837 {
10838 if (i != parm_names.begin())
10839 {
10840 if (some_parm_emitted)
10841 repr += ", ";
10842 }
10843 else
10844 if (!is_static && is_method_type)
10845 // We are printing a non-static method name, skip the implicit "this"
10846 // parameter type.
10847 continue;
10848 repr += *i;
10849 some_parm_emitted = true;
10850 }
10851 repr += ")";
10852
10853 if (is_const)
10854 {
10856 repr += " const";
10857 }
10858
10859 return repr;
10860}
10861
10862/// Compute the flat representation string of a struct, class or union
10863/// type represented by a DIE.
10864///
10865/// The flat representation string looks like:
10866/// "struct {int foo; char blah;}.
10867///
10868/// That is useful to designate a struct (class or union) that is
10869/// anonymous.
10870///
10871/// @param rdr the DWARF reader to consider.
10872///
10873/// @param die the DIE of the type to return the flat representation
10874/// for.
10875///
10876/// @param indent the indentation string to use for the
10877/// representation.
10878///
10879/// @param one_line if true then the flat representation is
10880/// constructed on one line. Otherwise, each data member is
10881/// represented on its own line.
10882///
10883/// @param qualified_names if true then the data member (and their
10884/// type) names using in the representation are qualified.
10885///
10886/// @param where_offset where in the are logically are in the DIE
10887/// stream.
10888///
10889/// @param guard the set of DIE offsets of the stack of DIEs involved
10890/// in the construction of the flat representation of the type. This
10891/// set is used to detect (and avoid) cycles in the stack of DIEs that
10892/// is going to be walked to compute the flat representation.
10893static string
10894die_class_flat_representation(const reader& rdr,
10895 const Dwarf_Die* die,
10896 const string& indent,
10897 bool one_line,
10898 bool qualified_names,
10899 size_t where_offset,
10900 unordered_set<uint64_t>& guard)
10901{
10902 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10903
10904 string repr = indent;
10905 string local_indent = " ";
10906 string real_indent;
10907
10908 if (tag == DW_TAG_union_type)
10909 repr += "union";
10910 else if (tag == DW_TAG_structure_type)
10911 repr += "struct";
10912 else if (tag == DW_TAG_class_type)
10913 repr += "class";
10914 else
10916
10917 repr += " ";
10918
10919 if (die_is_anonymous(die))
10920 {
10921 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10922 if (guard.find(off) != guard.end())
10923 {
10924 repr += "{}";
10925 return repr;
10926 }
10927 guard.insert(off);
10928 }
10929
10930 if (!die_is_anonymous(die))
10931 repr += die_qualified_name(rdr, die, where_offset, guard);
10932
10933 repr += "{";
10934
10935 if (!one_line)
10936 repr += "\n";
10937
10938 Dwarf_Die member_child_die;
10939 bool first_sibling = true;
10940 for (bool got_it = get_member_child_die(die, &member_child_die);
10941 got_it;
10942 got_it = get_next_member_sibling_die(&member_child_die,
10943 &member_child_die),
10944 first_sibling = false)
10945 {
10946 // A member of the class is either a declaration or an anonymous
10947 // type. Otherwise, let's skip it.
10948 if (!die_is_decl(&member_child_die)
10949 && !(die_is_type(&member_child_die)
10950 && die_is_anonymous(&member_child_die)))
10951 continue;
10952
10953 if (one_line)
10954 real_indent = first_sibling ? "" : " " ;
10955 else
10956 real_indent = (first_sibling ? "": "\n") + indent + local_indent;
10957
10958 repr += real_indent;
10959
10960 repr += die_pretty_print_decl(rdr, &member_child_die,
10961 qualified_names,
10962 /*include_fns=*/false,
10963 where_offset,
10964 guard);
10965 repr += ";";
10966 }
10967
10968 if (one_line)
10969 repr += "}";
10970 else
10971 repr += indent + "}";
10972
10973 if (die_is_anonymous(die))
10974 {
10975 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10976 guard.erase(off);
10977 }
10978 return repr;
10979}
10980
10981/// Compute the flat representation string of a enum type represented
10982/// by a DIE.
10983///
10984/// The flat representation string looks like:
10985/// "enum {int foo; char blah;}.
10986///
10987/// That is useful to designate an enum that is anonymous.
10988///
10989/// @param rdr the DWARF reader to consider.
10990///
10991/// @param die the DIE of the type to return the flat representation
10992/// for.
10993///
10994/// @param indent the indentation string to use for the
10995/// representation.
10996///
10997/// @param one_line if true then the flat representation is
10998/// constructed on one line. Otherwise, each data member is
10999/// represented on its own line.
11000///
11001/// @param qualified_names if true then the data member (and their
11002/// type) names using in the representation are qualified.
11003///
11004/// @param where_offset where in the are logically are in the DIE
11005/// stream.
11006static string
11007die_enum_flat_representation(const reader& rdr,
11008 const Dwarf_Die* die,
11009 const string& indent,
11010 bool one_line,
11011 bool qualified_names,
11012 size_t where_offset)
11013{
11014 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11015
11016 std::ostringstream o;
11017 string local_indent = " ";
11018 string real_indent;
11019
11020 if (tag == DW_TAG_enumeration_type)
11021 o << "enum";
11022 else
11024
11025 o << " ";
11026
11027 if (!die_is_anonymous(die))
11028 o << (qualified_names
11029 ? die_qualified_name(rdr, die, where_offset)
11030 : die_name(die));
11031
11032 o << "{";
11033
11034 if (!one_line)
11035 o << "\n";
11036
11038 Dwarf_Die child;
11039 bool first_enumerator= true;
11040 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
11041 {
11042 do
11043 {
11044 if (dwarf_tag(&child) != DW_TAG_enumerator)
11045 continue;
11046
11047 string name, m;
11048 location l;
11049 die_loc_and_name(rdr, &child, l, name, m);
11050 uint64_t val = 0;
11051 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
11052
11053 if (one_line)
11054 real_indent = first_enumerator ? "" : ", ";
11055 else
11056 real_indent = first_enumerator ? "" : ",\n" + indent + local_indent;
11057 o << name + " = " << val;
11058 first_enumerator = false;
11059 }
11060 while (dwarf_siblingof(&child, &child) == 0);
11061 }
11062
11063 o << (one_line
11064 ? string("}")
11065 : "\n" + indent);
11066
11067 o << "}";
11068
11069 return o.str();
11070}
11071
11072/// Compute the flat representation string of a class or enum type
11073/// represented by a DIE.
11074///
11075/// The flat representation string looks like:
11076/// "union {int foo; char blah;}.
11077///
11078/// That is useful to designate a class or enum type that is
11079/// anonymous.
11080///
11081/// @param rdr the DWARF reader to consider.
11082///
11083/// @param die the DIE of the type to return the flat representation
11084/// for.
11085///
11086/// @param indent the indentation string to use for the
11087/// representation.
11088///
11089/// @param one_line if true then the flat representation is
11090/// constructed on one line. Otherwise, each data member is
11091/// represented on its own line.
11092///
11093/// @param qualified_names if true then the data member (and their
11094/// type) names using in the representation are qualified.
11095///
11096/// @param where_offset where in the are logically are in the DIE
11097/// stream.
11098///
11099/// @param guard the set of DIE offsets of the stack of DIEs involved
11100/// in the construction of the flat representation of the type. This
11101/// set is used to detect (and avoid) cycles in the stack of DIEs that
11102/// is going to be walked to compute the flat representation.
11103static string
11104die_class_or_enum_flat_representation(const reader& rdr,
11105 const Dwarf_Die* die,
11106 const string& indent,
11107 bool one_line,
11108 bool qualified_names,
11109 size_t where_offset,
11110 unordered_set<uint64_t>& guard)
11111{
11112 if (!die)
11113 return string();
11114
11115 string result;
11116 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11117
11118 switch (tag)
11119 {
11120 case DW_TAG_class_type:
11121 case DW_TAG_structure_type:
11122 case DW_TAG_union_type:
11123 result = die_class_flat_representation(rdr, die, indent,
11124 one_line, qualified_names,
11125 where_offset,
11126 guard);
11127 break;
11128 case DW_TAG_enumeration_type:
11129 result = die_enum_flat_representation(rdr, die, indent,
11130 one_line, qualified_names,
11131 where_offset);
11132 break;
11133 default:
11135 }
11136
11137 return result;
11138}
11139
11140/// Compute the flat representation string of a class or enum type
11141/// represented by a DIE.
11142///
11143/// The flat representation string looks like:
11144/// "union {int foo; char blah;}.
11145///
11146/// That is useful to designate a class or enum type that is
11147/// anonymous.
11148///
11149/// @param rdr the DWARF reader to consider.
11150///
11151/// @param die the DIE of the type to return the flat representation
11152/// for.
11153///
11154/// @param indent the indentation string to use for the
11155/// representation.
11156///
11157/// @param one_line if true then the flat representation is
11158/// constructed on one line. Otherwise, each data member is
11159/// represented on its own line.
11160///
11161/// @param qualified_names if true then the data member (and their
11162/// type) names using in the representation are qualified.
11163///
11164/// @param where_offset where in the are logically are in the DIE
11165/// stream.
11166static string
11167die_class_or_enum_flat_representation(const reader& rdr,
11168 const Dwarf_Die* die,
11169 const string& indent,
11170 bool one_line,
11171 bool qualified_names,
11172 size_t where_offset)
11173{
11174 unordered_set<uint64_t> guard;
11175 return die_class_or_enum_flat_representation(rdr, die, indent,
11176 one_line, qualified_names,
11177 where_offset, guard);
11178}
11179
11180/// Return a pretty string representation of a type, for internal purposes.
11181///
11182/// By internal purpose, we mean things like key-ing types for lookup
11183/// purposes and so on.
11184///
11185/// Note that this function is also used to pretty print functions.
11186/// For functions, it prints the *type* of the function.
11187///
11188/// @param rdr the context to use.
11189///
11190/// @param the DIE of the type to pretty print.
11191///
11192/// @param where_offset where we logically are placed when calling
11193/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11194/// entries.
11195///
11196/// @param guard the set of DIE offsets of the stack of DIEs involved
11197/// in the construction of the pretty representation of the type.
11198/// This set is used to detect (and avoid) cycles in the stack of DIEs
11199/// that is going to be walked to compute the pretty representation.
11200///
11201/// @return the resulting pretty representation.
11202static string
11203die_pretty_print_type(const reader& rdr,
11204 const Dwarf_Die* die,
11205 size_t where_offset,
11206 unordered_set<uint64_t>& guard)
11207{
11208 if (!die
11209 || (!die_is_type(die)
11210 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
11211 return "";
11212
11213 string repr;
11214
11215 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11216 switch (tag)
11217 {
11218 case DW_TAG_string_type:
11219 // For now, we won't try to go get the actual representation of
11220 // the string because this would make things more complicated;
11221 // for that we'd need to interpret some location expressions to
11222 // get the length of the string. And for dynamically allocated
11223 // strings, the result of the location expression evaluation
11224 // might not even be a constant. So at the moment I consider
11225 // this to be a lot of hassle for no great return. Until proven
11226 // otherwise, of course.
11227 repr = "string type";
11228
11229 case DW_TAG_unspecified_type:
11230 case DW_TAG_ptr_to_member_type:
11231 break;
11232
11233 case DW_TAG_namespace:
11234 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset,
11235 guard);
11236 break;
11237
11238 case DW_TAG_base_type:
11239 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11240 break;
11241
11242 case DW_TAG_typedef:
11243 {
11244 string qualified_name;
11245 if (!die_qualified_type_name_empty(rdr, die,
11246 where_offset,
11247 qualified_name,
11248 guard))
11249 repr = "typedef " + qualified_name;
11250 }
11251 break;
11252
11253 case DW_TAG_const_type:
11254 case DW_TAG_volatile_type:
11255 case DW_TAG_restrict_type:
11256 case DW_TAG_pointer_type:
11257 case DW_TAG_reference_type:
11258 case DW_TAG_rvalue_reference_type:
11259 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11260 break;
11261
11262 case DW_TAG_enumeration_type:
11263 {
11264 string qualified_name =
11265 rdr.get_die_qualified_type_name(die, where_offset, guard);
11266 repr = "enum " + qualified_name;
11267 }
11268 break;
11269
11270 case DW_TAG_structure_type:
11271 case DW_TAG_class_type:
11272 {
11273 string qualified_name =
11274 rdr.get_die_qualified_type_name(die, where_offset, guard);
11275 repr = "class " + qualified_name;
11276 }
11277 break;
11278
11279 case DW_TAG_union_type:
11280 {
11281 string qualified_name =
11282 rdr.get_die_qualified_type_name(die, where_offset, guard);
11283 repr = "union " + qualified_name;
11284 }
11285 break;
11286
11287 case DW_TAG_array_type:
11288 {
11289 Dwarf_Die element_type_die;
11290 if (!die_die_attribute(die, DW_AT_type, element_type_die))
11291 break;
11292 string element_type_name =
11293 rdr.get_die_qualified_type_name(&element_type_die,
11294 where_offset, guard);
11295 if (element_type_name.empty())
11296 break;
11297
11299 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
11300 /*associate_type_to_die=*/false);
11301
11302 repr = element_type_name;
11304 }
11305 break;
11306
11307 case DW_TAG_subrange_type:
11308 {
11309 // So this can be generated by Ada, on its own; that is, not
11310 // as a subtype of an array. In that case we need to handle
11311 // it properly.
11312
11313 // For now, we consider that the pretty printed name of the
11314 // subrange type is its name. We might need something more
11315 // advance, should the needs of the users get more
11316 // complicated.
11317 repr += die_qualified_type_name(rdr, die, where_offset, guard);
11318 }
11319 break;
11320
11321 case DW_TAG_subroutine_type:
11322 case DW_TAG_subprogram:
11323 {
11324 string return_type_name;
11325 string class_name;
11326 vector<string> parm_names;
11327 bool is_const = false;
11328 bool is_static = false;
11329 bool is_method_type = false;
11330 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
11331 /*pretty_print=*/true,
11332 /*qualified_name=*/true,
11334 return_type_name, class_name,
11335 parm_names, is_const,
11336 is_static, guard);
11337 if (!is_method_type)
11338 repr = "function type";
11339 else
11340 repr = "method type";
11341 repr += " " + rdr.get_die_qualified_type_name(die, where_offset, guard);
11342 }
11343 break;
11344
11345 case DW_TAG_set_type:
11346 case DW_TAG_file_type:
11347 case DW_TAG_packed_type:
11348 case DW_TAG_thrown_type:
11349 case DW_TAG_interface_type:
11350 case DW_TAG_shared_type:
11352 }
11353
11354 return repr;
11355}
11356
11357/// Return a pretty string representation of a declaration, for
11358/// internal purposes.
11359///
11360/// By internal purpose, we mean things like key-ing declarations for
11361/// lookup purposes and so on.
11362///
11363/// Note that this function is also used to pretty print functions.
11364/// For functions, it prints the signature of the function.
11365///
11366/// @param rdr the context to use.
11367///
11368/// @param die the DIE of the declaration to pretty print.
11369///
11370/// @param qualified_name if true then use qualified names.
11371///
11372/// @param where_offset where we logically are placed when calling
11373/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11374/// entries.
11375///
11376/// @param guard the set of DIE offsets of the stack of DIEs involved
11377/// in the construction of the pretty representation of the decl.
11378/// This set is used to detect (and avoid) cycles in the stack of DIEs
11379/// that is going to be walked to compute the pretty representation.
11380///
11381/// @return the resulting pretty representation.
11382static string
11383die_pretty_print_decl(const reader& rdr,
11384 const Dwarf_Die* die,
11385 bool qualified_name,
11386 bool include_fns,
11387 size_t where_offset,
11388 unordered_set<uint64_t>& guard)
11389{
11390 if (!die || !die_is_decl(die))
11391 return "";
11392
11393 string repr;
11394
11395 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11396 switch (tag)
11397 {
11398 case DW_TAG_namespace:
11399 repr = "namespace " + die_qualified_name(rdr, die, where_offset, guard);
11400 break;
11401
11402 case DW_TAG_member:
11403 case DW_TAG_variable:
11404 {
11405 string type_repr = "void";
11406 Dwarf_Die type_die;
11407 if (die_die_attribute(die, DW_AT_type, type_die))
11408 type_repr = die_type_name(rdr, &type_die,
11409 qualified_name,
11410 where_offset,
11411 guard);
11412 repr = (qualified_name
11413 ? die_qualified_name(rdr, die, where_offset, guard)
11414 : die_name(die));
11415
11416 if (repr.empty())
11417 repr = type_repr;
11418 else
11419 repr = type_repr + " " + repr;
11420 }
11421 break;
11422
11423 case DW_TAG_subprogram:
11424 if (include_fns)
11425 repr = die_function_signature(rdr, die, qualified_name,
11426 where_offset, guard);
11427 break;
11428
11429 default:
11430 break;
11431 }
11432 return repr;
11433}
11434
11435/// Compute the pretty printed representation of an artifact
11436/// represented by a DIE.
11437///
11438/// If the DIE is a type, compute the its pretty representation as a
11439/// type; otherwise, if it's a declaration, compute its pretty
11440/// representation as a declaration. Note for For instance, that a
11441/// DW_TAG_subprogram DIE is going to be represented as a function
11442/// *type*.
11443///
11444/// @param rdr the DWARF reader.
11445///
11446/// @param die the DIE to consider.
11447///
11448/// @param where_offset we in the DIE stream we are logically at.
11449///
11450/// @param guard the set of DIE offsets of the stack of DIEs involved
11451/// in the construction of the pretty representation of the DIe. This
11452/// set is used to detect (and avoid) cycles in the stack of DIEs that
11453/// is going to be walked to compute the pretty representation.
11454///
11455/// @return a copy of the pretty printed artifact.
11456static string
11457die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset,
11458 unordered_set<uint64_t>& guard)
11459{
11460 if (die_is_type(die))
11461 return die_pretty_print_type(rdr, die, where_offset, guard);
11462 else if (die_is_decl(die))
11463 return die_pretty_print_decl(rdr, die,
11464 /*qualified_names=*/true,
11465 /*include_fns=*/true,
11466 where_offset, guard);
11467 return "";
11468}
11469
11470// -----------------------------------
11471// </die pretty printer>
11472// -----------------------------------
11473
11474
11475// ----------------------------------
11476// <die comparison engine>
11477// ---------------------------------
11478
11479/// Compares two decls DIEs
11480///
11481/// This works only for DIEs emitted by the C language.
11482///
11483/// This implementation doesn't yet support namespaces.
11484///
11485/// This is a subroutine of compare_dies.
11486///
11487/// @return true iff @p l equals @p r.
11488static bool
11489compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
11490{
11491 ABG_ASSERT(l && r);
11492
11493 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11494 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11495 if (l_tag != r_tag)
11496 return false;
11497
11498 bool result = false;
11499
11500 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
11501 {
11502 // Fast path for functions and global variables.
11503 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
11504 result)
11505 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
11506 result))
11507 {
11508 if (!result)
11509 return false;
11510 }
11511
11512 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11513 result))
11514 {
11515 if (!result)
11516 return false;
11517 }
11518 return true;
11519 }
11520
11521 // Fast path for types.
11522 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11523 result))
11524 return result;
11525 return true;
11526}
11527
11528/// Test if at least one of two ODR-relevant DIEs is decl-only.
11529///
11530/// @param rdr the DWARF reader to consider.
11531///
11532/// @param l the first type DIE to consider.
11533///
11534/// @param r the second type DIE to consider.
11535///
11536/// @return true iff either @p l or @p r is decl-only and both are
11537/// ODR-relevant.
11538static bool
11539at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
11540 const Dwarf_Die *l,
11541 const Dwarf_Die *r)
11542{
11543 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
11544 return false;
11545
11546 if ((die_is_declaration_only(l) && die_has_no_child(l))
11547 || (die_is_declaration_only(r) && die_has_no_child(r)))
11548 return true;
11549 return false;
11550}
11551
11552/// Compares two type DIEs
11553///
11554/// This is a subroutine of compare_dies.
11555///
11556/// Note that this function doesn't look at the name of the DIEs.
11557/// Naming is taken into account by the function compare_as_decl_dies.
11558///
11559/// If the two DIEs are from a translation unit that is subject to the
11560/// ONE Definition Rule, then the function considers that if one DIE
11561/// is a declaration, then it's equivalent to the second. In that
11562/// case, the sizes of the two DIEs are not compared. This is so that
11563/// a declaration of a type compares equal to the definition of the
11564/// type.
11565///
11566/// @param rdr the DWARF reader to consider.
11567///
11568/// @param l the left operand of the comparison operator.
11569///
11570/// @param r the right operand of the comparison operator.
11571///
11572/// @return true iff @p l equals @p r.
11573static bool
11574compare_as_type_dies(const reader& rdr,
11575 const Dwarf_Die *l,
11576 const Dwarf_Die *r)
11577{
11578 ABG_ASSERT(l && r);
11579 ABG_ASSERT(die_is_type(l));
11580 ABG_ASSERT(die_is_type(r));
11581
11582 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
11583 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
11584 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11585 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
11586 // For now, we cannot compare DW_TAG_string_type because of its
11587 // string_length attribute that is a location descriptor that is
11588 // not necessarily a constant. So it's super hard to evaluate it
11589 // in a libabigail context. So for now, we just say that all
11590 // DW_TAG_string_type DIEs are different, by default.
11591 return false;
11592
11593 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11594 // A declaration of a type compares equal to the definition of the
11595 // type.
11596 return true;
11597
11598 uint64_t l_size = 0, r_size = 0;
11599 die_size_in_bits(l, l_size);
11600 die_size_in_bits(r, r_size);
11601
11602 return l_size == r_size;
11603}
11604
11605/// Compare two DIEs as decls (looking as their names etc) and as
11606/// types (looking at their size etc).
11607///
11608/// @param rdr the DWARF reader to consider.
11609///
11610/// @param l the first DIE to consider.
11611///
11612/// @param r the second DIE to consider.
11613///
11614/// @return TRUE iff @p l equals @p r as far as naming and size is
11615/// concerned.
11616static bool
11617compare_as_decl_and_type_dies(const reader &rdr,
11618 const Dwarf_Die *l,
11619 const Dwarf_Die *r)
11620{
11621 if (!compare_as_decl_dies(l, r)
11622 || !compare_as_type_dies(rdr, l, r))
11623 return false;
11624
11625 return true;
11626}
11627
11628/// Test if two DIEs representing function declarations have the same
11629/// linkage name, and thus are considered equal if they are C or C++,
11630/// because the two DIEs represent functions in the same binary.
11631///
11632/// If the DIEs don't have a linkage name, the function compares their
11633/// name. But in that case, the caller of the function must know that
11634/// in C++ for instance, that doesn't imply that the two functions are
11635/// equal.
11636///
11637/// @param l the first function DIE to consider.
11638///
11639/// @param r the second function DIE to consider.
11640///
11641/// @return true iff the function represented by @p l have the same
11642/// linkage name as the function represented by @p r.
11643static bool
11644fn_die_equal_by_linkage_name(const Dwarf_Die *l,
11645 const Dwarf_Die *r)
11646{
11647 if (!!l != !!r)
11648 return false;
11649
11650 if (!l)
11651 return false;
11652
11653 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11654 ABG_ASSERT(tag == DW_TAG_subprogram);
11655 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11656 ABG_ASSERT(tag == DW_TAG_subprogram);
11657
11658 string lname = die_name(l), rname = die_name(r);
11659 string llinkage_name = die_linkage_name(l),
11660 rlinkage_name = die_linkage_name(r);
11661
11662 if (die_is_in_c_or_cplusplus(l)
11663 && die_is_in_c_or_cplusplus(r))
11664 {
11665 if (!llinkage_name.empty() && !rlinkage_name.empty())
11666 return llinkage_name == rlinkage_name;
11667 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
11668 return false;
11669 else
11670 return lname == rname;
11671 }
11672
11673 return (!llinkage_name.empty()
11674 && !rlinkage_name.empty()
11675 && llinkage_name == rlinkage_name);
11676}
11677
11678/// Compare two DIEs in the context of DIE canonicalization.
11679///
11680/// If DIE canonicalization is on, the function compares the DIEs
11681/// canonically and structurally. The two types of comparison should
11682/// be equal, of course.
11683///
11684/// @param rdr the DWARF reader.
11685///
11686/// @param l_offset the offset of the first canonical DIE to compare.
11687///
11688/// @param r_offset the offset of the second canonical DIE to compare.
11689///
11690/// @param l_die_source the source of the DIE denoted by the offset @p
11691/// l_offset.
11692///
11693/// @param r_die_source the source of the DIE denoted by the offset @p
11694/// r_offset.
11695///
11696/// @param l_has_canonical_die_offset output parameter. Is set to
11697/// true if @p l_offset has a canonical DIE.
11698///
11699/// @param r_has_canonical_die_offset output parameter. Is set to
11700/// true if @p r_offset has a canonical DIE.
11701///
11702/// @param l_canonical_die_offset output parameter. If @p
11703/// l_has_canonical_die_offset is set to true, then this parameter is
11704/// set to the offset of the canonical DIE of the DIE designated by @p
11705/// l_offset.
11706static bool
11707try_canonical_die_comparison(const reader& rdr,
11708 Dwarf_Off l_offset, Dwarf_Off r_offset,
11709 die_source l_die_source, die_source r_die_source,
11710 bool& l_has_canonical_die_offset,
11711 bool& r_has_canonical_die_offset,
11712 Dwarf_Off& l_canonical_die_offset,
11713 Dwarf_Off& r_canonical_die_offset,
11714 bool& result)
11715{
11716#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11717 if (rdr.debug_die_canonicalization_is_on_
11718 && !rdr.use_canonical_die_comparison_)
11719 return false;
11720#endif
11721
11722
11723 l_has_canonical_die_offset =
11724 (l_canonical_die_offset =
11725 rdr.get_canonical_die_offset(l_offset, l_die_source,
11726 /*die_as_type=*/true));
11727
11728 r_has_canonical_die_offset =
11729 (r_canonical_die_offset =
11730 rdr.get_canonical_die_offset(r_offset, r_die_source,
11731 /*die_as_type=*/true));
11732
11733 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
11734 {
11735 result = (l_canonical_die_offset == r_canonical_die_offset);
11736 return true;
11737 }
11738
11739 return false;
11740}
11741
11742#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11743/// This function is called whenever a DIE comparison fails.
11744///
11745/// This function is intended for debugging purposes. The idea is for
11746/// hackers to set a breakpoint on this function so that they can
11747/// discover why exactly the comparison failed. They then can execute
11748/// the program from compare_dies_during_canonicalization, for
11749/// instance.
11750///
11751/// @param @l the left-hand side of the DIE comparison.
11752///
11753/// @param @r the right-hand side of the DIE comparison.
11754static void
11755notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
11756{
11757}
11758
11759#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
11760 notify_die_comparison_failed(l, r)
11761#else
11762#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
11763#endif
11764
11765/// A macro used to return from DIE comparison routines.
11766///
11767/// If the return value is false, the macro invokes the
11768/// notify_die_comparison_failed signalling function before returning.
11769/// That way, hackers willing to learn more about why the comparison
11770/// routine returned "false" can just set a breakpoint on
11771/// notify_die_comparison_failed and execute the program from
11772/// compare_dies_during_canonicalization, for instance.
11773///
11774/// @param value the value to return from the DIE comparison routines.
11775#define ABG_RETURN(value) \
11776 do \
11777 { \
11778 if ((value) == COMPARISON_RESULT_DIFFERENT) \
11779 { \
11780 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11781 } \
11782 return return_comparison_result(l, r, dies_being_compared, \
11783 value, aggregates_being_compared, \
11784 update_canonical_dies_on_the_fly); \
11785 } \
11786 while(false)
11787
11788/// A macro used to return the "false" boolean from DIE comparison
11789/// routines.
11790///
11791/// As the return value is false, the macro invokes the
11792/// notify_die_comparison_failed signalling function before returning.
11793///
11794/// @param value the value to return from the DIE comparison routines.
11795#define ABG_RETURN_FALSE \
11796 do \
11797 { \
11798 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11799 return return_comparison_result(l, r, dies_being_compared, \
11800 COMPARISON_RESULT_DIFFERENT, \
11801 aggregates_being_compared, \
11802 update_canonical_dies_on_the_fly); \
11803 } while(false)
11804
11805/// A macro to set the 'result' variable to 'false'.
11806///
11807/// The macro invokes the notify_die_comparison_failed function so
11808/// that the hacker can set a debugging breakpoint on
11809/// notify_die_comparison_failed to know where a DIE comparison failed
11810/// during compare_dies_during_canonicalization for instance.
11811///
11812/// @param result the 'result' variable to set.
11813///
11814/// @param l the first DIE of the comparison operation.
11815///
11816/// @param r the second DIE of the comparison operation.
11817#define SET_RESULT_TO_FALSE(result, l , r) \
11818 do \
11819 { \
11820 result = COMPARISON_RESULT_DIFFERENT; \
11821 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11822 } while(false)
11823
11824/// A macro to set the 'result' variable to a given value.
11825///
11826/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
11827/// invokes the notify_die_comparison_failed function so that the
11828/// hacker can set a debugging breakpoint on
11829/// notify_die_comparison_failed to know where a DIE comparison failed
11830/// during compare_dies_during_canonicalization for instance.
11831///
11832/// @param result the 'result' variable to set.
11833///
11834/// @param l the first DIE of the comparison operation.
11835///
11836/// @param r the second DIE of the comparison operation.
11837#define SET_RESULT_TO(result, value, l , r) \
11838 do \
11839 { \
11840 result = (value); \
11841 if (result == COMPARISON_RESULT_DIFFERENT) \
11842 { \
11843 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11844 } \
11845 } while(false)
11846
11847#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
11848 do \
11849 { \
11850 if (aggregates_being_compared.contains(dies_being_compared)) \
11851 { \
11852 result = COMPARISON_RESULT_CYCLE_DETECTED; \
11853 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
11854 ABG_RETURN(result); \
11855 } \
11856 } \
11857 while(false)
11858
11859/// Get the next member sibling of a given class or union member DIE.
11860///
11861/// @param die the DIE to consider.
11862///
11863/// @param member out parameter. This is set to the next member
11864/// sibling, iff the function returns TRUE.
11865///
11866/// @return TRUE iff the function set @p member to the next member
11867/// sibling DIE.
11868static bool
11869get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
11870{
11871 if (!die)
11872 return false;
11873
11874 bool found_member = false;
11875 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
11876 member) == 0);
11877 found_member;
11878 found_member = (dwarf_siblingof(member, member) == 0))
11879 {
11880 int tag = dwarf_tag(member);
11881 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
11882 break;
11883 }
11884
11885 return found_member;
11886}
11887
11888/// Get the first child DIE of a class/struct/union DIE that is a
11889/// member DIE.
11890///
11891/// Note that a member DIE is represented by a DWARF tag that is
11892/// either DW_TAG_member, DW_TAG_inheritance.
11893///
11894/// @param die the DIE to consider.
11895///
11896/// @param child out parameter. This is set to the first child DIE of
11897/// @p iff this function returns TRUE.
11898///
11899/// @return TRUE iff @p child is set to the first child DIE of @p die
11900/// that is a member DIE.
11901static bool
11902get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
11903{
11904 if (!die)
11905 return false;
11906
11907 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11908 ABG_ASSERT(tag == DW_TAG_structure_type
11909 || tag == DW_TAG_union_type
11910 || tag == DW_TAG_class_type);
11911
11912 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die), child) == 0);
11913
11914 if (!found_child)
11915 return false;
11916
11917 tag = dwarf_tag(child);
11918
11919 if (!(tag == DW_TAG_member
11920 || tag == DW_TAG_inheritance
11921 || tag == DW_TAG_subprogram))
11922 found_child = get_next_member_sibling_die(child, child);
11923
11924 return found_child;
11925}
11926
11927/// This is a sub-routine of return_comparison_result.
11928///
11929/// Propagate the canonical type of a the right-hand-side DIE to the
11930/// lef-hand-side DIE. This is a optimization that is done when the
11931/// two DIEs compare equal.
11932///
11933/// If the right-hand-side DIE is not canonicalized, the function
11934/// performs its canonicalization.
11935///
11936/// This optimization is performed only if
11937/// is_canon_type_to_be_propagated_tag returns true.
11938///
11939/// @param rdr the current context to consider.
11940///
11941/// @param l the left-hand-side DIE of the comparison. It's going to
11942/// receive the canonical type of the other DIE.
11943///
11944/// @param r the right-hand-side DIE of the comparison. Its canonical
11945/// type is propagated to @p l.
11946static void
11947maybe_propagate_canonical_type(const reader& rdr,
11948 const Dwarf_Die* l,
11949 const Dwarf_Die* r)
11950{
11951 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
11952 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11953
11954 if (l_tag != r_tag)
11955 return;
11956
11957 if (is_canon_type_to_be_propagated_tag(l_tag))
11958 propagate_canonical_type(rdr, l, r);
11959}
11960
11961/// Propagate the canonical type of a the right-hand-side DIE to the
11962/// left-hand-side DIE. This is a optimization that is done when the
11963/// two DIEs compare equal.
11964///
11965/// If the right-hand-side DIE is not canonicalized, the function
11966/// performs its canonicalization.
11967///
11968/// @param rdr the current context to consider.
11969///
11970/// @param l the left-hand-side DIE of the comparison. It's going to
11971/// receive the canonical type of the other DIE.
11972///
11973/// @param r the right-hand-side DIE of the comparison. Its canonical
11974/// type is propagated to @p l.
11975static void
11976propagate_canonical_type(const reader& rdr,
11977 const Dwarf_Die* l,
11978 const Dwarf_Die* r)
11979{
11980 ABG_ASSERT(l && r);
11981
11982 // If 'l' has no canonical DIE and if 'r' has one, then propagage
11983 // the canonical DIE of 'r' to 'l'.
11984 //
11985 // In case 'r' has no canonical DIE, then compute it, and then
11986 // propagate that canonical DIE to 'r'.
11987 const die_source l_source = rdr.get_die_source(l);
11988 const die_source r_source = rdr.get_die_source(r);
11989
11990 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
11991 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
11992 bool l_has_canonical_die_offset = false;
11993 bool r_has_canonical_die_offset = false;
11994 Dwarf_Off l_canonical_die_offset = 0;
11995 Dwarf_Off r_canonical_die_offset = 0;
11996
11997 l_has_canonical_die_offset =
11998 (l_canonical_die_offset =
11999 rdr.get_canonical_die_offset(l_offset, l_source,
12000 /*die_as_type=*/true));
12001
12002 r_has_canonical_die_offset =
12003 (r_canonical_die_offset =
12004 rdr.get_canonical_die_offset(r_offset, r_source,
12005 /*die_as_type=*/true));
12006
12007
12008 if (!l_has_canonical_die_offset
12009 && r_has_canonical_die_offset
12010 // A DIE can be equivalent only to another DIE of the same
12011 // source.
12012 && l_source == r_source)
12013 {
12014 ABG_ASSERT(r_canonical_die_offset);
12015 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
12016 /*die_as_type=*/true);
12017 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
12018 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
12019 rdr.canonical_propagated_count_++;
12020 }
12021}
12022
12023/// This function does the book keeping of comparison pairs necessary
12024/// to handle
12025///
12026/// * the detection of cycles during the comparison of aggregate
12027/// types, in conjuction with the macro
12028/// RETURN_IF_COMPARISON_CYCLE_DETECTED
12029///
12030/// * the handling of the canonical type propagation optimisation
12031/// to speed-up type canonicalization.
12032///
12033///
12034/// Note that this function is essentially a sub-routine of
12035/// compare_dies.
12036///
12037/// @param l the left-hand-side DIE being compared.
12038///
12039/// @param r the right-hand-side DIE being compared.
12040///
12041/// @param cur_dies the pair of die offsets of l and r. This is
12042/// redundant as it can been computed from @p l and @p r. However,
12043/// getting it as an argument is an optimization to avoid computing it
12044/// over and over again, given how often this function is invoked from
12045/// compare_dies.
12046///
12047/// @param return the result of comparing @p l against @p r.
12048///
12049/// @param comparison_stack the stack of pair of type DIEs being
12050/// compared.
12051///
12052/// @param do_propagate_canonical_type if true then the function
12053/// performs canonical DIEs propagation, meaning that if @p l equals
12054/// @p r and if @p r has a canonical type, then the canonical type of
12055/// @p l is set to the canonical type of @p r.
12056static comparison_result
12057return_comparison_result(const Dwarf_Die* l,
12058 const Dwarf_Die* r,
12059 const offset_pair_type& cur_dies,
12060 comparison_result result,
12061 offset_pairs_stack_type& comparison_stack,
12062 bool do_propagate_canonical_type = true)
12063{
12064 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
12065
12066 if (result == COMPARISON_RESULT_EQUAL)
12067 {
12068 // The result comparing the two types is "true", basically. So
12069 // let's propagate the canonical type of r onto l, so that we
12070 // don't need to compute the canonical type of r.
12071 if (do_propagate_canonical_type)
12072 {
12073 // Propagate canonical type.
12074 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12075
12076 // TODO: do we need to confirm any tentative canonical
12077 // propagation?
12078 }
12079 }
12080 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12081 {
12082 // So upon detection of the comparison cycle, compare_dies
12083 // returned early with the comparison result
12084 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
12085 // carry on with the comparison of all the OTHER sub-types of
12086 // the redundant type. If they all compare equal, then it means
12087 // the redundant type pair compared equal. Otherwise, it
12088 // compared different.
12089 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
12090 // Let's fall through to let the end of this function set the
12091 // result to COMPARISON_RESULT_UNKNOWN;
12092 }
12093 else if (result == COMPARISON_RESULT_UNKNOWN)
12094 {
12095 // Here is an introductory comment describing what we are going
12096 // to do in this case where the result of the comparison of the
12097 // current pair of type is not "false", basically.
12098 //
12099 // This means that we don't yet know what the result of
12100 // comparing these two types is, because one of the sub-types of
12101 // the types being compared is "redundant", meaning it appears
12102 // more than once in the comparison stack, so if we were to
12103 // naively try to carry on with the comparison member-wise, we'd
12104 // end up with an endless loop, a.k.a "comparison cycle".
12105 //
12106 // If the current type pair is redundant then:
12107 //
12108 // * This is a redundant type that has just been fully
12109 // compared. In that case, all the types that depend on
12110 // this redundant type and that have been tentatively
12111 // canonical-type-propagated must see their canonical types
12112 // "confirmed". This means that this type is going to be
12113 // considered as not being redundant anymore, meaning all
12114 // the types that depend on it must be updated as not being
12115 // dependant on it anymore, and the type itsef must be
12116 // removed from the map of redundant types.
12117 //
12118 // After the type's canonical-type-propagation is confirmed,
12119 // the result of its comparison must also be changed into
12120 // COMPARISON_RESULT_EQUAL.
12121 //
12122 // After that, If the current type depends on a redundant type,
12123 // then propagate its canonical type AND track it as having its
12124 // type being canonical-type-propagated.
12125 //
12126 // If the current type is not redundant however, then it must be
12127 // dependant on a redundant type. If it's not dependant on a
12128 // redundant type, then it must be of those types which
12129 // comparisons are not tracked for cycle, probably because they
12130 // are not aggregates. Otherwise, ABORT to understand why. I
12131 // believe this should not happen. In any case, after that
12132 // safety check is passed, we just need to return at this point.
12133
12134 if (comparison_stack.is_redundant(cur_dies)
12135 && comparison_stack.vect_.back() == cur_dies)
12136 {
12137 // We are in the case described above of a redundant type
12138 // that has been fully compared.
12139 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12140 comparison_stack.confirm_canonical_propagated_type(cur_dies);
12141
12142 result = COMPARISON_RESULT_EQUAL;
12143 }
12144 else if (is_canon_type_to_be_propagated_tag(l_tag)
12145 && comparison_stack.vect_.back() == cur_dies)
12146 {
12147 // The current type is not redundant. So, as described in
12148 // the introductory comment above, it must be dependant on a
12149 // redundant type.
12150 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
12151 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12152 // Then pass through.
12153 }
12154 }
12155 else if (result == COMPARISON_RESULT_DIFFERENT)
12156 {
12157 // Here is an introductory comment describing what we are going
12158 // to do in this case where the result of the comparison of the
12159 // current pair of type is "false", basically.
12160 //
12161 // If the type pair {l,r} is redundant then cancel the
12162 // canonical-type-propagation of all the dependant pairs that
12163 // depends on this redundant {l, r}. This means walk the types
12164 // that depends on {l, r} and cancel their
12165 // canonical-propagate-type, that means remove their canonical
12166 // types and mark them as not being canonically-propagated.
12167 // Also, erase their cached comparison results that was likely
12168 // set to COMPARISON_RESULT_UNKNOWN.
12169 //
12170 // Also, update the cached result for this pair, that was likely
12171 // to be COMPARISON_RESULT_UNKNOWN.
12172 if (comparison_stack.is_redundant(cur_dies)
12173 && comparison_stack.vect_.back() == cur_dies)
12174 comparison_stack.cancel_canonical_propagated_type(cur_dies);
12175 }
12176 else
12177 {
12178 // We should never reach here.
12180 }
12181
12182 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12183 result = COMPARISON_RESULT_UNKNOWN;
12184 else if (is_canon_type_to_be_propagated_tag(l_tag)
12185 && !comparison_stack.vect_.empty()
12186 && comparison_stack.vect_.back() == cur_dies)
12187 //Finally pop the pair types being compared from comparison_stack
12188 //iff {l,r} is on the top of the stack. If it's not, then it means
12189 //we are looking at a type that was detected as a being redundant
12190 //and thus hasn't been pushed to the stack yet gain.
12191 comparison_stack.erase(cur_dies);
12192
12193 maybe_cache_type_comparison_result(comparison_stack.rdr_,
12194 l_tag, cur_dies, result);
12195
12196 return result;
12197}
12198
12199/// Compare two DIEs emitted by a C compiler.
12200///
12201/// @param rdr the DWARF reader used to load the DWARF information.
12202///
12203/// @param l the left-hand-side argument of this comparison operator.
12204///
12205/// @param r the righ-hand-side argument of this comparison operator.
12206///
12207/// @param aggregates_being_compared this holds the names of the set
12208/// of aggregates being compared. It's used by the comparison
12209/// function to avoid recursing infinitely when faced with types
12210/// referencing themselves through pointers or references. By
12211/// default, just pass an empty instance of @ref istring_set_type to
12212/// it.
12213///
12214/// @param update_canonical_dies_on_the_fly if true, when two
12215/// sub-types compare equal (during the comparison of @p l and @p r)
12216/// update their canonical type. That way, two types of the same name
12217/// are structurally compared to each other only once. So the
12218/// non-linear structural comparison of two types of the same name
12219/// only happen once.
12220///
12221/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12222static comparison_result
12223compare_dies(const reader& rdr,
12224 const Dwarf_Die *l, const Dwarf_Die *r,
12225 offset_pairs_stack_type& aggregates_being_compared,
12226 bool update_canonical_dies_on_the_fly)
12227{
12228 ABG_ASSERT(l);
12229 ABG_ASSERT(r);
12230
12231 const die_source l_die_source = rdr.get_die_source(l);
12232 const die_source r_die_source = rdr.get_die_source(r);
12233
12234 offset_type l_offset =
12235 {
12236 l_die_source,
12237 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12238 };
12239
12240 offset_type r_offset =
12241 {
12242 r_die_source,
12243 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12244 };
12245
12246 offset_pair_type dies_being_compared(l_offset, r_offset);
12247
12248 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
12249 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12250
12251 if (l_tag != r_tag)
12253
12254 if (l_offset == r_offset)
12255 return COMPARISON_RESULT_EQUAL;
12256
12257 if (rdr.leverage_dwarf_factorization()
12258 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
12259 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
12260 if (l_offset != r_offset)
12261 return COMPARISON_RESULT_DIFFERENT;
12262
12263 comparison_result result = COMPARISON_RESULT_EQUAL;
12264 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
12265 dies_being_compared,
12266 result))
12267 return result;
12268
12269 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
12270 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
12271
12272 // If 'l' and 'r' already have canonical DIEs, then just compare the
12273 // offsets of their canonical DIEs.
12274 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
12275 {
12276 bool canonical_compare_result = false;
12277 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
12278 l_die_source, r_die_source,
12279 l_has_canonical_die_offset,
12280 r_has_canonical_die_offset,
12281 l_canonical_die_offset,
12282 r_canonical_die_offset,
12283 canonical_compare_result))
12284 {
12285 comparison_result result;
12286 SET_RESULT_TO(result,
12287 (canonical_compare_result
12288 ? COMPARISON_RESULT_EQUAL
12289 : COMPARISON_RESULT_DIFFERENT),
12290 l, r);
12291 return result;
12292 }
12293 }
12294
12295
12296
12297 switch (l_tag)
12298 {
12299 case DW_TAG_base_type:
12300 case DW_TAG_string_type:
12301 case DW_TAG_unspecified_type:
12302 if (!compare_as_decl_and_type_dies(rdr, l, r))
12303 SET_RESULT_TO_FALSE(result, l, r);
12304 break;
12305
12306 case DW_TAG_typedef:
12307 case DW_TAG_pointer_type:
12308 case DW_TAG_reference_type:
12309 case DW_TAG_rvalue_reference_type:
12310 case DW_TAG_const_type:
12311 case DW_TAG_volatile_type:
12312 case DW_TAG_restrict_type:
12313 {
12314 if (!compare_as_type_dies(rdr, l, r))
12315 {
12316 SET_RESULT_TO_FALSE(result, l, r);
12317 break;
12318 }
12319
12320 bool from_the_same_tu = false;
12321 if (!pointer_or_qual_die_of_anonymous_class_type(l)
12322 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
12323 && from_the_same_tu)
12324 {
12325 // These two typedefs, pointer, reference, or qualified
12326 // types have the same name and are defined in the same TU.
12327 // They thus ought to be the same.
12328 //
12329 // Note that pointers, reference or qualified types to
12330 // anonymous types are not taking into account here because
12331 // those always need to be structurally compared.
12332 SET_RESULT_TO_FALSE(result, l, r);
12333 break;
12334 }
12335 }
12336
12337 {
12338 // No fancy optimization in this case. We need to
12339 // structurally compare the two DIEs.
12340 Dwarf_Die lu_type_die, ru_type_die;
12341 bool lu_is_void, ru_is_void;
12342
12343 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
12344 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
12345
12346 if (lu_is_void && ru_is_void)
12347 result = COMPARISON_RESULT_EQUAL;
12348 else if (lu_is_void != ru_is_void)
12349 SET_RESULT_TO_FALSE(result, l, r);
12350 else
12351 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
12352 aggregates_being_compared,
12353 update_canonical_dies_on_the_fly);
12354 }
12355 break;
12356
12357 case DW_TAG_enumeration_type:
12358 if (!compare_as_decl_and_type_dies(rdr, l, r))
12359 SET_RESULT_TO_FALSE(result, l, r);
12360 else
12361 {
12362 // Walk the enumerators.
12363 Dwarf_Die l_enumtor, r_enumtor;
12364 bool found_l_enumtor = true, found_r_enumtor = true;
12365
12366 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12367 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
12368 &l_enumtor) == 0,
12369 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
12370 &r_enumtor) == 0;
12371 found_l_enumtor && found_r_enumtor;
12372 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
12373 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
12374 {
12375 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
12376 if ( l_tag != r_tag)
12377 {
12378 SET_RESULT_TO_FALSE(result, l, r);
12379 break;
12380 }
12381
12382 if (l_tag != DW_TAG_enumerator)
12383 continue;
12384
12385 uint64_t l_val = 0, r_val = 0;
12386 die_unsigned_constant_attribute(&l_enumtor,
12387 DW_AT_const_value,
12388 l_val);
12389 die_unsigned_constant_attribute(&r_enumtor,
12390 DW_AT_const_value,
12391 r_val);
12392 if (l_val != r_val)
12393 {
12394 SET_RESULT_TO_FALSE(result, l, r);
12395 break;
12396 }
12397 }
12398 if (found_l_enumtor != found_r_enumtor )
12399 SET_RESULT_TO_FALSE(result, l, r);
12400 }
12401 break;
12402
12403 case DW_TAG_structure_type:
12404 case DW_TAG_union_type:
12405 case DW_TAG_class_type:
12406 {
12407 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12408
12409 rdr.compare_count_++;
12410
12411 if (!compare_as_decl_and_type_dies(rdr, l, r))
12412 SET_RESULT_TO_FALSE(result, l, r);
12413 else if (rdr.options().assume_odr_for_cplusplus
12414 && rdr.odr_is_relevant(l)
12415 && rdr.odr_is_relevant(r)
12416 && !die_is_anonymous(l)
12417 && !die_is_anonymous(r))
12418 result = COMPARISON_RESULT_EQUAL;
12419 else
12420 {
12421 aggregates_being_compared.add(dies_being_compared);
12422
12423 Dwarf_Die l_member, r_member;
12424 bool found_l_member = true, found_r_member = true;
12425
12426 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12427 for (found_l_member = get_member_child_die(l, &l_member),
12428 found_r_member = get_member_child_die(r, &r_member);
12429 found_l_member && found_r_member;
12430 found_l_member = get_next_member_sibling_die(&l_member,
12431 &l_member),
12432 found_r_member = get_next_member_sibling_die(&r_member,
12433 &r_member))
12434 {
12435 int l_tag = dwarf_tag(&l_member),
12436 r_tag = dwarf_tag(&r_member);
12437
12438 if (l_tag != r_tag)
12439 {
12440 SET_RESULT_TO_FALSE(result, l, r);
12441 break;
12442 }
12443
12444 ABG_ASSERT(l_tag == DW_TAG_member
12445 || l_tag == DW_TAG_variable
12446 || l_tag == DW_TAG_inheritance
12447 || l_tag == DW_TAG_subprogram);
12448
12449 comparison_result local_result =
12450 compare_dies(rdr, &l_member, &r_member,
12451 aggregates_being_compared,
12452 update_canonical_dies_on_the_fly);
12453
12454 if (local_result == COMPARISON_RESULT_UNKNOWN)
12455 // Note that if the result of comparing any
12456 // sub-type is COMPARISON_RESULT_EQUAL, just
12457 // because we have at least one sub-type's
12458 // comparison being COMPARISON_RESULT_UNKNOWN
12459 // means that the comparison of this type will
12460 // return COMPARISON_RESULT_UNKNOWN to show
12461 // callers that this type (and all the types that
12462 // depend on it) depends on a redundant type
12463 result = local_result;
12464
12465 if (local_result == COMPARISON_RESULT_DIFFERENT)
12466 {
12467 SET_RESULT_TO_FALSE(result, l, r);
12468 break;
12469 }
12470 }
12471 if (found_l_member != found_r_member)
12472 {
12473 SET_RESULT_TO_FALSE(result, l, r);
12474 break;
12475 }
12476 }
12477 }
12478 break;
12479
12480 case DW_TAG_array_type:
12481 {
12482 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12483
12484 aggregates_being_compared.add(dies_being_compared);
12485
12486 rdr.compare_count_++;
12487
12488 Dwarf_Die l_child, r_child;
12489 bool found_l_child, found_r_child;
12490 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12491 &l_child) == 0,
12492 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12493 &r_child) == 0;
12494 found_l_child && found_r_child;
12495 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
12496 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
12497 {
12498 int l_child_tag = dwarf_tag(&l_child),
12499 r_child_tag = dwarf_tag(&r_child);
12500 if (l_child_tag == DW_TAG_subrange_type
12501 || r_child_tag == DW_TAG_subrange_type)
12502 {
12503 result = compare_dies(rdr, &l_child, &r_child,
12504 aggregates_being_compared,
12505 update_canonical_dies_on_the_fly);
12506 if (!result)
12507 {
12508 SET_RESULT_TO_FALSE(result, l, r);
12509 break;
12510 }
12511 }
12512 }
12513 if (found_l_child != found_r_child)
12514 SET_RESULT_TO_FALSE(result, l, r);
12515 // Compare the types of the elements of the array.
12516 Dwarf_Die ltype_die, rtype_die;
12517 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
12518 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
12519 ABG_ASSERT(found_ltype && found_rtype);
12520
12521 result = compare_dies(rdr, &ltype_die, &rtype_die,
12522 aggregates_being_compared,
12523 update_canonical_dies_on_the_fly);
12524 if (!result)
12526 }
12527 break;
12528
12529 case DW_TAG_subrange_type:
12530 {
12531 uint64_t l_lower_bound = 0, r_lower_bound = 0,
12532 l_upper_bound = 0, r_upper_bound = 0;
12533 bool l_lower_bound_set = false, r_lower_bound_set = false,
12534 l_upper_bound_set = false, r_upper_bound_set = false;
12535
12536 l_lower_bound_set =
12537 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
12538 r_lower_bound_set =
12539 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
12540
12541 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
12542 l_upper_bound))
12543 {
12544 uint64_t l_count = 0;
12545 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
12546 {
12547 l_upper_bound = l_lower_bound + l_count;
12548 l_upper_bound_set = true;
12549 if (l_upper_bound)
12550 --l_upper_bound;
12551 }
12552 }
12553 else
12554 l_upper_bound_set = true;
12555
12556 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
12557 r_upper_bound))
12558 {
12559 uint64_t r_count = 0;
12560 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
12561 {
12562 r_upper_bound = r_lower_bound + r_count;
12563 r_upper_bound_set = true;
12564 if (r_upper_bound)
12565 --r_upper_bound;
12566 }
12567 }
12568 else
12569 r_upper_bound_set = true;
12570
12571 if ((l_lower_bound_set != r_lower_bound_set)
12572 || (l_upper_bound_set != r_upper_bound_set)
12573 || (l_lower_bound != r_lower_bound)
12574 || (l_upper_bound != r_upper_bound))
12575 SET_RESULT_TO_FALSE(result, l, r);
12576 }
12577 break;
12578
12579 case DW_TAG_subroutine_type:
12580 case DW_TAG_subprogram:
12581 {
12582 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12583
12584 aggregates_being_compared.add(dies_being_compared);
12585
12586 rdr.compare_count_++;
12587
12588 if (l_tag == DW_TAG_subprogram
12589 && !fn_die_equal_by_linkage_name(l, r))
12590 {
12591 SET_RESULT_TO_FALSE(result, l, r);
12592 break;
12593 }
12594 else if (l_tag == DW_TAG_subprogram
12595 && die_is_in_c(l) && die_is_in_c(r))
12596 {
12597 result = COMPARISON_RESULT_EQUAL;
12598 break;
12599 }
12600 else if (!die_is_in_c(l) && !die_is_in_c(r))
12601 {
12602 // In C, we cannot have two different functions with the
12603 // same linkage name in a given binary. But here we are
12604 // looking at DIEs that don't originate from C. So we
12605 // need to compare return types and parameter types.
12606 Dwarf_Die l_return_type, r_return_type;
12607 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
12608 l_return_type);
12609 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
12610 r_return_type);
12611 if (l_return_type_is_void != r_return_type_is_void
12612 || (!l_return_type_is_void
12613 && !compare_dies(rdr,
12614 &l_return_type, &r_return_type,
12615 aggregates_being_compared,
12616 update_canonical_dies_on_the_fly)))
12617 SET_RESULT_TO_FALSE(result, l, r);
12618 else
12619 {
12620 Dwarf_Die l_child, r_child;
12621 bool found_l_child, found_r_child;
12622 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12623 &l_child) == 0,
12624 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12625 &r_child) == 0;
12626 found_l_child && found_r_child;
12627 found_l_child = dwarf_siblingof(&l_child,
12628 &l_child) == 0,
12629 found_r_child = dwarf_siblingof(&r_child,
12630 &r_child)==0)
12631 {
12632 int l_child_tag = dwarf_tag(&l_child);
12633 int r_child_tag = dwarf_tag(&r_child);
12634 comparison_result local_result =
12635 COMPARISON_RESULT_EQUAL;
12636 if (l_child_tag != r_child_tag)
12637 local_result = COMPARISON_RESULT_DIFFERENT;
12638 if (l_child_tag == DW_TAG_formal_parameter)
12639 local_result =
12640 compare_dies(rdr, &l_child, &r_child,
12641 aggregates_being_compared,
12642 update_canonical_dies_on_the_fly);
12643 if (local_result == COMPARISON_RESULT_DIFFERENT)
12644 {
12645 result = local_result;
12646 SET_RESULT_TO_FALSE(result, l, r);
12647 break;
12648 }
12649 if (local_result == COMPARISON_RESULT_UNKNOWN)
12650 // Note that if the result of comparing any
12651 // sub-type is COMPARISON_RESULT_EQUAL, just
12652 // because we have at least one sub-type's
12653 // comparison being COMPARISON_RESULT_UNKNOWN
12654 // means that the comparison of this type will
12655 // return COMPARISON_RESULT_UNKNOWN to show
12656 // callers that this type (and all the types
12657 // that depend on it) depends on a redundant
12658 // type and so, can't be
12659 // canonical-type-propagated.
12660 result = local_result;
12661 }
12662 if (found_l_child != found_r_child)
12663 {
12664 SET_RESULT_TO_FALSE(result, l, r);
12665 break;
12666 }
12667 }
12668 }
12669 }
12670 break;
12671
12672 case DW_TAG_formal_parameter:
12673 {
12674 Dwarf_Die l_type, r_type;
12675 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
12676 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
12677 if (l_type_is_void != r_type_is_void)
12678 SET_RESULT_TO_FALSE(result, l, r);
12679 else if (!l_type_is_void)
12680 {
12681 comparison_result local_result =
12682 compare_dies(rdr, &l_type, &r_type,
12683 aggregates_being_compared,
12684 update_canonical_dies_on_the_fly);
12685 SET_RESULT_TO(result, local_result, l, r);
12686 }
12687 }
12688 break;
12689
12690 case DW_TAG_variable:
12691 case DW_TAG_member:
12692 if (compare_as_decl_dies(l, r))
12693 {
12694 // Compare the offsets of the data members
12695 if (l_tag == DW_TAG_member)
12696 {
12697 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12698 die_member_offset(rdr, l, l_offset_in_bits);
12699 die_member_offset(rdr, r, r_offset_in_bits);
12700 if (l_offset_in_bits != r_offset_in_bits)
12701 SET_RESULT_TO_FALSE(result, l, r);
12702 }
12703 if (result)
12704 {
12705 // Compare the types of the data members or variables.
12706 Dwarf_Die l_type, r_type;
12707 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12708 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12709 comparison_result local_result =
12710 compare_dies(rdr, &l_type, &r_type,
12711 aggregates_being_compared,
12712 update_canonical_dies_on_the_fly);
12713 SET_RESULT_TO(result, local_result, l, r);
12714 }
12715 }
12716 else
12717 SET_RESULT_TO_FALSE(result, l, r);
12718 break;
12719
12720 case DW_TAG_inheritance:
12721 {
12722 Dwarf_Die l_type, r_type;
12723 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12724 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12725 result = compare_dies(rdr, &l_type, &r_type,
12726 aggregates_being_compared,
12727 update_canonical_dies_on_the_fly);
12728 if (!result)
12729 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12730
12731 uint64_t l_a = 0, r_a = 0;
12732 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
12733 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
12734 if (l_a != r_a)
12735 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12736
12737 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
12738 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
12739 if (l_a != r_a)
12740 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12741
12742 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12743 die_member_offset(rdr, l, l_offset_in_bits);
12744 die_member_offset(rdr, r, r_offset_in_bits);
12745 if (l_offset_in_bits != r_offset_in_bits)
12746 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12747 }
12748 break;
12749
12750 case DW_TAG_ptr_to_member_type:
12751 {
12752 bool comp_result = false;
12753 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
12754 if (!comp_result)
12755 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12756
12757 Dwarf_Die l_type, r_type;
12758 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12759 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12760 result = compare_dies(rdr, &l_type, &r_type,
12761 aggregates_being_compared,
12762 update_canonical_dies_on_the_fly);
12763 if (!result)
12764 ABG_RETURN(result);
12765
12766 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
12767 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
12768 result = compare_dies(rdr, &l_type, &r_type,
12769 aggregates_being_compared,
12770 update_canonical_dies_on_the_fly);
12771 if (!result)
12772 ABG_RETURN(result);
12773 }
12774 break;
12775
12776 case DW_TAG_enumerator:
12777 case DW_TAG_packed_type:
12778 case DW_TAG_set_type:
12779 case DW_TAG_file_type:
12780 case DW_TAG_thrown_type:
12781 case DW_TAG_interface_type:
12782 case DW_TAG_shared_type:
12783 case DW_TAG_compile_unit:
12784 case DW_TAG_namespace:
12785 case DW_TAG_module:
12786 case DW_TAG_constant:
12787 case DW_TAG_partial_unit:
12788 case DW_TAG_imported_unit:
12789 case DW_TAG_dwarf_procedure:
12790 case DW_TAG_imported_declaration:
12791 case DW_TAG_entry_point:
12792 case DW_TAG_label:
12793 case DW_TAG_lexical_block:
12794 case DW_TAG_unspecified_parameters:
12795 case DW_TAG_variant:
12796 case DW_TAG_common_block:
12797 case DW_TAG_common_inclusion:
12798 case DW_TAG_inlined_subroutine:
12799 case DW_TAG_with_stmt:
12800 case DW_TAG_access_declaration:
12801 case DW_TAG_catch_block:
12802 case DW_TAG_friend:
12803 case DW_TAG_namelist:
12804 case DW_TAG_namelist_item:
12805 case DW_TAG_template_type_parameter:
12806 case DW_TAG_template_value_parameter:
12807 case DW_TAG_try_block:
12808 case DW_TAG_variant_part:
12809 case DW_TAG_imported_module:
12810 case DW_TAG_condition:
12811 case DW_TAG_type_unit:
12812 case DW_TAG_template_alias:
12813 case DW_TAG_lo_user:
12814 case DW_TAG_MIPS_loop:
12815 case DW_TAG_format_label:
12816 case DW_TAG_function_template:
12817 case DW_TAG_class_template:
12818 case DW_TAG_GNU_BINCL:
12819 case DW_TAG_GNU_EINCL:
12820 case DW_TAG_GNU_template_template_param:
12821 case DW_TAG_GNU_template_parameter_pack:
12822 case DW_TAG_GNU_formal_parameter_pack:
12823 case DW_TAG_GNU_call_site:
12824 case DW_TAG_GNU_call_site_parameter:
12825 case DW_TAG_hi_user:
12826#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12827 if (rdr.debug_die_canonicalization_is_on_)
12829#endif
12831 break;
12832 }
12833
12834 ABG_RETURN(result);
12835}
12836
12837/// Compare two DIEs emitted by a C compiler.
12838///
12839/// @param rdr the DWARF reader used to load the DWARF information.
12840///
12841/// @param l the left-hand-side argument of this comparison operator.
12842///
12843/// @param r the righ-hand-side argument of this comparison operator.
12844///
12845/// @param update_canonical_dies_on_the_fly if yes, then this function
12846/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
12847/// comparing l and r. This helps in making so that sub-type DIEs of
12848/// 'l' and 'r' are compared structurally only once. This is how we
12849/// turn this exponential comparison problem into a problem that is a
12850/// closer to a linear one.
12851///
12852/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12853static comparison_result
12854compare_dies(const reader& rdr,
12855 const Dwarf_Die *l,
12856 const Dwarf_Die *r,
12857 bool update_canonical_dies_on_the_fly)
12858{
12859 offset_pairs_stack_type aggregates_being_compared(rdr);
12860 return compare_dies(rdr, l, r, aggregates_being_compared,
12861 update_canonical_dies_on_the_fly);
12862}
12863
12864/// Compare two DIEs for the purpose of canonicalization.
12865///
12866/// This is a sub-routine of reader::get_canonical_die.
12867///
12868/// When DIE canonicalization debugging is on, this function performs
12869/// both structural and canonical comparison. It expects that both
12870/// comparison yield the same result.
12871///
12872/// @param rdr the DWARF reader.
12873///
12874/// @param l the left-hand-side comparison operand DIE.
12875///
12876/// @param r the right-hand-side comparison operand DIE.
12877///
12878/// @param update_canonical_dies_on_the_fly if true, then some
12879/// aggregate DIEs will see their canonical types propagated.
12880///
12881/// @return true iff @p l equals @p r.
12882static bool
12883compare_dies_during_canonicalization(reader& rdr,
12884 const Dwarf_Die *l,
12885 const Dwarf_Die *r,
12886 bool update_canonical_dies_on_the_fly)
12887{
12888#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12889 if (rdr.debug_die_canonicalization_is_on_)
12890 {
12891 bool canonical_equality = false, structural_equality = false;
12892 rdr.use_canonical_die_comparison_ = false;
12893 structural_equality = compare_dies(rdr, l, r,
12894 /*update_canonical_dies_on_the_fly=*/false);
12895 rdr.use_canonical_die_comparison_ = true;
12896 canonical_equality = compare_dies(rdr, l, r,
12897 update_canonical_dies_on_the_fly);
12898 if (canonical_equality != structural_equality)
12899 {
12900 std::cerr << "structural & canonical equality different for DIEs: "
12901 << std::hex
12902 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12903 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12904 << std::dec
12905 << ", repr: '"
12906 << rdr.get_die_pretty_type_representation(l, 0)
12907 << "'"
12908 << std::endl;
12910 }
12911 return structural_equality;
12912 }
12913#endif
12914 return compare_dies(rdr, l, r,
12915 update_canonical_dies_on_the_fly);
12916}
12917
12918// ----------------------------------
12919// </die comparison engine>
12920// ---------------------------------
12921
12922/// Get the point where a DW_AT_import DIE is used to import a given
12923/// (unit) DIE, between two DIEs.
12924///
12925/// @param rdr the dwarf reader to consider.
12926///
12927/// @param partial_unit_offset the imported unit for which we want to
12928/// know the insertion point. This is usually a partial unit (with
12929/// tag DW_TAG_partial_unit) but it does not necessarily have to be
12930/// so.
12931///
12932/// @param first_die_offset the offset of the DIE from which this
12933/// function starts looking for the import point of
12934/// @partial_unit_offset. Note that this offset is excluded from the
12935/// set of potential solutions.
12936///
12937/// @param first_die_cu_offset the offset of the (compilation) unit
12938/// that @p first_die_cu_offset belongs to.
12939///
12940/// @param source where the DIE of first_die_cu_offset unit comes
12941/// from.
12942///
12943/// @param last_die_offset the offset of the last DIE of the up to
12944/// which this function looks for the import point of @p
12945/// partial_unit_offset. Note that this offset is excluded from the
12946/// set of potential solutions.
12947///
12948/// @param imported_point_offset. The resulting
12949/// imported_point_offset. Note that if the imported DIE @p
12950/// partial_unit_offset is not found between @p first_die_offset and
12951/// @p last_die_offset, this parameter is left untouched by this
12952/// function.
12953///
12954/// @return true iff an imported unit is found between @p
12955/// first_die_offset and @p last_die_offset.
12956static bool
12957find_import_unit_point_between_dies(const reader& rdr,
12958 size_t partial_unit_offset,
12959 Dwarf_Off first_die_offset,
12960 Dwarf_Off first_die_cu_offset,
12961 die_source source,
12962 size_t last_die_offset,
12963 size_t& imported_point_offset)
12964{
12965 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
12966 rdr.tu_die_imported_unit_points_map(source);
12967
12968 tu_die_imported_unit_points_map_type::const_iterator iter =
12969 tu_die_imported_unit_points_map.find(first_die_cu_offset);
12970
12971 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
12972
12973 const imported_unit_points_type& imported_unit_points = iter->second;
12974 if (imported_unit_points.empty())
12975 return false;
12976
12977 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
12978 imported_unit_points_type::const_iterator e = imported_unit_points.end();
12979
12980 find_lower_bound_in_imported_unit_points(imported_unit_points,
12981 first_die_offset,
12982 b);
12983
12984 if (last_die_offset != static_cast<size_t>(-1))
12985 find_lower_bound_in_imported_unit_points(imported_unit_points,
12986 last_die_offset,
12987 e);
12988
12989 if (e != imported_unit_points.end())
12990 {
12991 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12992 if (i->imported_unit_die_off == partial_unit_offset)
12993 {
12994 imported_point_offset = i->offset_of_import ;
12995 return true;
12996 }
12997
12998 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12999 {
13000 if (find_import_unit_point_between_dies(rdr,
13001 partial_unit_offset,
13002 i->imported_unit_child_off,
13003 i->imported_unit_cu_off,
13004 i->imported_unit_die_source,
13005 /*(Dwarf_Off)*/-1,
13006 imported_point_offset))
13007 return true;
13008 }
13009 }
13010 else
13011 {
13012 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
13013 if (i->imported_unit_die_off == partial_unit_offset)
13014 {
13015 imported_point_offset = i->offset_of_import ;
13016 return true;
13017 }
13018
13019 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
13020 {
13021 if (find_import_unit_point_between_dies(rdr,
13022 partial_unit_offset,
13023 i->imported_unit_child_off,
13024 i->imported_unit_cu_off,
13025 i->imported_unit_die_source,
13026 /*(Dwarf_Off)*/-1,
13027 imported_point_offset))
13028 return true;
13029 }
13030 }
13031
13032 return false;
13033}
13034
13035/// In the current translation unit, get the last point where a
13036/// DW_AT_import DIE is used to import a given (unit) DIE, before a
13037/// given DIE is found. That given DIE is called the limit DIE.
13038///
13039/// Said otherwise, this function returns the last import point of a
13040/// unit, before a limit.
13041///
13042/// @param rdr the dwarf reader to consider.
13043///
13044/// @param partial_unit_offset the imported unit for which we want to
13045/// know the insertion point of. This is usually a partial unit (with
13046/// tag DW_TAG_partial_unit) but it does not necessarily have to be
13047/// so.
13048///
13049/// @param where_offset the offset of the limit DIE.
13050///
13051/// @param imported_point_offset. The resulting imported_point_offset.
13052/// Note that if the imported DIE @p partial_unit_offset is not found
13053/// before @p die_offset, this is set to the last @p
13054/// partial_unit_offset found under @p parent_die.
13055///
13056/// @return true iff an imported unit is found before @p die_offset.
13057/// Note that if an imported unit is found after @p die_offset then @p
13058/// imported_point_offset is set and the function return false.
13059static bool
13060find_import_unit_point_before_die(const reader& rdr,
13061 size_t partial_unit_offset,
13062 size_t where_offset,
13063 size_t& imported_point_offset)
13064{
13065 size_t import_point_offset = 0;
13066 Dwarf_Die first_die_of_tu;
13067
13068 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
13069 &first_die_of_tu) != 0)
13070 return false;
13071
13072 Dwarf_Die cu_die_memory;
13073 Dwarf_Die *cu_die;
13074
13075 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
13076 &cu_die_memory, 0, 0);
13077
13078 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
13079 dwarf_dieoffset(&first_die_of_tu),
13080 dwarf_dieoffset(cu_die),
13081 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
13082 where_offset,
13083 import_point_offset))
13084 {
13085 imported_point_offset = import_point_offset;
13086 return true;
13087 }
13088
13089 if (import_point_offset)
13090 {
13091 imported_point_offset = import_point_offset;
13092 return true;
13093 }
13094
13095 return false;
13096}
13097
13098/// Return the parent DIE for a given DIE.
13099///
13100/// Note that the function build_die_parent_map() must have been
13101/// called before this one can work. This function either succeeds or
13102/// aborts the current process.
13103///
13104/// @param rdr the DWARF reader to consider.
13105///
13106/// @param die the DIE for which we want the parent.
13107///
13108/// @param parent_die the output parameter set to the parent die of
13109/// @p die. Its memory must be allocated and handled by the caller.
13110///
13111/// @param where_offset the offset of the DIE where we are "logically"
13112/// positionned at, in the DIE tree. This is useful when @p die is
13113/// e.g, DW_TAG_partial_unit that can be included in several places in
13114/// the DIE tree.
13115///
13116/// @return true if the function could get a parent DIE, false
13117/// otherwise.
13118static bool
13119get_parent_die(const reader& rdr,
13120 const Dwarf_Die* die,
13121 Dwarf_Die& parent_die,
13122 size_t where_offset)
13123{
13124 ABG_ASSERT(rdr.dwarf_debug_info());
13125
13126 const die_source source = rdr.get_die_source(die);
13127
13128 const offset_offset_map_type& m = rdr.die_parent_map(source);
13129 offset_offset_map_type::const_iterator i =
13130 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
13131
13132 if (i == m.end())
13133 return false;
13134
13135 switch (source)
13136 {
13137 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
13138 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13139 i->second, &parent_die));
13140 break;
13141 case ALT_DEBUG_INFO_DIE_SOURCE:
13142 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
13143 i->second, &parent_die));
13144 break;
13145 case TYPE_UNIT_DIE_SOURCE:
13146 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13147 i->second, &parent_die));
13148 break;
13149 case NO_DEBUG_INFO_DIE_SOURCE:
13150 case NUMBER_OF_DIE_SOURCES:
13152 }
13153
13154 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
13155 {
13156 if (where_offset == 0)
13157 {
13158 parent_die = *rdr.cur_tu_die();
13159 return true;
13160 }
13161 size_t import_point_offset = 0;
13162 bool found =
13163 find_import_unit_point_before_die(rdr,
13164 dwarf_dieoffset(&parent_die),
13165 where_offset,
13166 import_point_offset);
13167 if (!found)
13168 // It looks like parent_die (which comes from the alternate
13169 // debug info file) hasn't been imported into this TU. So,
13170 // Let's assume its logical parent is the DIE of the current
13171 // TU.
13172 parent_die = *rdr.cur_tu_die();
13173 else
13174 {
13175 ABG_ASSERT(import_point_offset);
13176 Dwarf_Die import_point_die;
13177 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13178 import_point_offset,
13179 &import_point_die));
13180 return get_parent_die(rdr, &import_point_die,
13181 parent_die, where_offset);
13182 }
13183 }
13184
13185 return true;
13186}
13187
13188/// Get the DIE representing the scope of a given DIE.
13189///
13190/// Please note that when the DIE we are looking at has a
13191/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
13192/// DIE is the parent DIE of the DIE referred to by that attribute.
13193/// In other words, this function returns the scope of the origin DIE
13194/// of the current DIE.
13195///
13196/// So, the scope DIE can be different from the parent DIE of a given
13197/// DIE.
13198///
13199/// Also note that if the current translation unit is from C, then
13200/// this returns the global scope.
13201///
13202/// @param rdr the DWARF reader to use.
13203///
13204/// @param dye the DIE to consider.
13205///
13206/// @param where_offset where we are logically at in the DIE stream.
13207///
13208/// @param scope_die out parameter. This is set to the resulting
13209/// scope DIE iff the function returns true.
13210///
13211/// @return true iff the scope was found and returned in the @p
13212/// scope_die parameter.
13213static bool
13214get_scope_die(const reader& rdr,
13215 const Dwarf_Die* dye,
13216 size_t where_offset,
13217 Dwarf_Die& scope_die)
13218{
13219 Dwarf_Die origin_die_mem;
13220 Dwarf_Die *die = &origin_die_mem;
13221 if (!die_origin_die(dye, origin_die_mem))
13222 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13223
13224 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13225 get_die_language(die, die_lang);
13226 if (is_c_language(die_lang)
13227 || rdr.die_parent_map(rdr.get_die_source(die)).empty())
13228 {
13229 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
13230 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
13231 }
13232
13233 if (!get_parent_die(rdr, die, scope_die, where_offset))
13234 return false;
13235
13236 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
13237 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
13238 || dwarf_tag(&scope_die) == DW_TAG_array_type)
13239 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
13240
13241 return true;
13242}
13243
13244/// Return the abigail IR node representing the scope of a given DIE.
13245///
13246/// Note that it is the logical scope that is returned. That is, if
13247/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
13248/// attribute, it's the scope of the referred-to DIE (via these
13249/// attributes) that is returned. In other words, its the scope of
13250/// the origin DIE that is returned.
13251///
13252/// Also note that if the current translation unit is from C, then
13253/// this returns the global scope.
13254///
13255/// @param rdr the dwarf reader to use.
13256///
13257/// @param dye the DIE to get the scope for.
13258///
13259/// @param called_from_public_decl is true if this function has been
13260/// initially called within the context of a public decl.
13261///
13262/// @param where_offset the offset of the DIE where we are "logically"
13263/// positionned at, in the DIE tree. This is useful when @p die is
13264/// e.g, DW_TAG_partial_unit that can be included in several places in
13265/// the DIE tree.
13266///
13267/// @return the resulting scope, or nil if could not be computed.
13268static scope_decl_sptr
13269get_scope_for_die(reader& rdr,
13270 Dwarf_Die* dye,
13271 bool called_for_public_decl,
13272 size_t where_offset)
13273{
13274 Dwarf_Die origin_die_mem;
13275 Dwarf_Die *die = &origin_die_mem;
13276
13277 if (!die_origin_die(dye, origin_die_mem))
13278 // There was no origin DIE found, so let's make the "die" pointer
13279 // above point to the content of the input "dye".
13280 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13281
13282 const die_source source_of_die = rdr.get_die_source(die);
13283
13284 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13285 get_die_language(die, die_lang);
13286 if (is_c_language(die_lang)
13287 || rdr.die_parent_map(source_of_die).empty())
13288 {
13289 // In units for the C languages all decls belong to the global
13290 // namespace. This is generally the case if Libabigail
13291 // determined that no DIE -> parent map was needed.
13292 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
13293 return rdr.global_scope();
13294 }
13295
13296 Dwarf_Die parent_die;
13297
13298 if (!get_parent_die(rdr, die, parent_die, where_offset))
13299 return rdr.nil_scope();
13300
13301 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
13302 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
13303 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13304 {
13305 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
13306 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13307 {
13308 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
13309 || source_of_die == TYPE_UNIT_DIE_SOURCE);
13310 return rdr.cur_transl_unit()->get_global_scope();
13311 }
13312
13313 // For top level DIEs like DW_TAG_compile_unit, we just want to
13314 // return the global scope for the corresponding translation
13315 // unit. This must have been set by
13316 // build_translation_unit_and_add_to_ir if we already started to
13317 // build the translation unit of parent_die. Otherwise, just
13318 // return the global scope of the current translation unit.
13319 die_tu_map_type::const_iterator i =
13320 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
13321 if (i != rdr.die_tu_map().end())
13322 return i->second->get_global_scope();
13323 return rdr.cur_transl_unit()->get_global_scope();
13324 }
13325
13328 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
13329 || dwarf_tag(&parent_die) == DW_TAG_array_type
13330 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
13331 // this is an entity defined in a scope that is a function.
13332 // Normally, I would say that this should be dropped. But I have
13333 // seen a case where a typedef DIE needed by a function parameter
13334 // was defined right before the parameter, under the scope of the
13335 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
13336 // the function parm too. So for that case, let's say that the
13337 // scope is the scope of the function itself. Note that this is
13338 // an error of the DWARF emitter. We should never see this DIE in
13339 // this context.
13340 {
13341 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
13342 called_for_public_decl,
13343 where_offset);
13344 if (is_anonymous_type_die(die))
13345 // For anonymous type that have nothing to do in a function or
13346 // array type context, let's put it in the containing
13347 // namespace. That is, do not let it be in a containing class
13348 // or union where it has nothing to do.
13349 while (is_class_or_union_type(s))
13350 {
13351 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
13352 return rdr.nil_scope();
13353 s = get_scope_for_die(rdr, &parent_die,
13354 called_for_public_decl,
13355 where_offset);
13356 }
13357 return s;
13358 }
13359 else
13360 d = build_ir_node_from_die(rdr, &parent_die,
13361 called_for_public_decl,
13362 where_offset);
13363 s = dynamic_pointer_cast<scope_decl>(d);
13364 if (!s)
13365 // this is an entity defined in someting that is not a scope.
13366 // Let's drop it.
13367 return rdr.nil_scope();
13368
13369 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
13370 if (cl && cl->get_is_declaration_only())
13371 {
13372 scope_decl_sptr scop =
13373 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
13374 if (scop)
13375 s = scop;
13376 else
13377 s = cl;
13378 }
13379 return s;
13380}
13381
13382/// Convert a DWARF constant representing the value of the
13383/// DW_AT_language property into the translation_unit::language
13384/// enumerator.
13385///
13386/// @param l the DWARF constant to convert.
13387///
13388/// @return the resulting translation_unit::language enumerator.
13390dwarf_language_to_tu_language(size_t l)
13391{
13392 switch (l)
13393 {
13394 case DW_LANG_C89:
13395 return translation_unit::LANG_C89;
13396 case DW_LANG_C99:
13397 return translation_unit::LANG_C99;
13398#ifdef HAVE_DW_LANG_C11_enumerator
13399 case DW_LANG_C11:
13400 return translation_unit::LANG_C11;
13401#endif
13402#ifdef HAVE_DW_LANG_C17
13403 case DW_LANG_C17:
13404 return translation_unit::LANG_C17;
13405#endif
13406#ifdef HAVE_DW_LANG_C23
13407 case DW_LANG_C23:
13408 return translation_unit::LANG_C23;
13409#endif
13410 case DW_LANG_C:
13411 return translation_unit::LANG_C;
13412#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
13413 case DW_LANG_C_plus_plus_03:
13414 return translation_unit::LANG_C_plus_plus_03;
13415#endif
13416
13417#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
13418 case DW_LANG_C_plus_plus_11:
13419 return translation_unit::LANG_C_plus_plus_11;
13420#endif
13421
13422#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
13423 case DW_LANG_C_plus_plus_14:
13424 return translation_unit::LANG_C_plus_plus_14;
13425#endif
13426#ifdef HAVE_DW_LANG_C_plus_plus_17
13427 case DW_LANG_C_plus_plus_17:
13428 return translation_unit::LANG_C_plus_plus_17;
13429#endif
13430
13431#ifdef HAVE_DW_LANG_C_plus_plus_20
13432 case DW_LANG_C_plus_plus_20:
13433 return translation_unit::LANG_C_plus_plus_20;
13434#endif
13435#ifdef HAVE_DW_LANG_C_plus_plus_23
13436 case DW_LANG_C_plus_plus_23:
13437 return translation_unit::LANG_C_plus_plus_23;
13438#endif
13439 case DW_LANG_C_plus_plus:
13440 return translation_unit::LANG_C_plus_plus;
13441#ifdef HAVE_DW_LANG_D_enumerator
13442 case DW_LANG_D:
13443 return translation_unit::LANG_D;
13444#endif
13445#ifdef HAVE_DW_LANG_OCaml_enumerator
13446 case DW_LANG_OCaml:
13447 return translation_unit::LANG_OCaml;
13448#endif
13449#ifdef HAVE_DW_LANG_Go_enumerator
13450 case DW_LANG_Go:
13451 return translation_unit::LANG_Go;
13452#endif
13453#ifdef HAVE_DW_LANG_Rust_enumerator
13454 case DW_LANG_Rust:
13455 return translation_unit::LANG_Rust;
13456#endif
13457#ifdef HAVE_DW_LANG_Zig
13458 case DW_LANG_Zig:
13459 return translation_unit::LANG_Zig;
13460#endif
13461#ifdef HAVE_DW_LANG_Metal
13462 case DW_LANG_Metal:
13463 return translation_unit::LANG_Metal;
13464#endif
13465 case DW_LANG_Ada83:
13466 return translation_unit::LANG_Ada83;
13467 case DW_LANG_Ada95:
13468 return translation_unit::LANG_Ada95;
13469#ifdef HAVE_DW_LANG_Ada2005
13470 case DW_LANG_Ada2005:
13471 return translation_unit::LANG_Ada2005;
13472#endif
13473
13474#ifdef HAVE_DW_LANG_Ada2012
13475 case DW_LANG_Ada2012:
13476 return translation_unit::LANG_Ada2012;
13477#endif
13478 case DW_LANG_Cobol74:
13479 return translation_unit::LANG_Cobol74;
13480 case DW_LANG_Cobol85:
13481 return translation_unit::LANG_Cobol85;
13482 case DW_LANG_Fortran77:
13483 return translation_unit::LANG_Fortran77;
13484 case DW_LANG_Fortran90:
13485 return translation_unit::LANG_Fortran90;
13486 case DW_LANG_Fortran95:
13487 return translation_unit::LANG_Fortran95;
13488#ifdef HAVE_DW_LANG_Fortran18
13489 case DW_LANG_Fortran18:
13490 return translation_unit::LANG_Fortran18;
13491#endif
13492#ifdef HAVE_DW_LANG_Fortran23
13493 case DW_LANG_Fortran23:
13494 return translation_unit::LANG_Fortran23;
13495#endif
13496 case DW_LANG_Pascal83:
13497 return translation_unit::LANG_Pascal83;
13498 case DW_LANG_Modula2:
13499 return translation_unit::LANG_Modula2;
13500 case DW_LANG_Java:
13501 return translation_unit::LANG_Java;
13502#ifdef HAVE_DW_LANG_Kotlin
13503 case DW_LANG_Kotlin:
13504 return translation_unit::LANG_Kotlin;
13505#endif
13506 case DW_LANG_PLI:
13507 return translation_unit::LANG_PLI;
13508 case DW_LANG_ObjC:
13509 return translation_unit::LANG_ObjC;
13510 case DW_LANG_ObjC_plus_plus:
13511 return translation_unit::LANG_ObjC_plus_plus;
13512
13513#ifdef HAVE_DW_LANG_UPC_enumerator
13514 case DW_LANG_UPC:
13515 return translation_unit::LANG_UPC;
13516#endif
13517#ifdef HAVE_DW_LANG_Python_enumerator
13518 case DW_LANG_Python:
13519 return translation_unit::LANG_Python;
13520#endif
13521#ifdef HAVE_DW_LANG_Ruby
13522 case DW_LANG_Ruby:
13523 return translation_unit::LANG_Ruby;
13524#endif
13525#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
13526 case DW_LANG_Mips_Assembler:
13527 return translation_unit::LANG_Mips_Assembler;
13528#endif
13529#ifdef HAVE_DW_LANG_Assembly
13530 case DW_LANG_Assembly:
13531 return translation_unit::LANG_Assembly;
13532#endif
13533#ifdef HAVE_DW_LANG_Crystal
13534 case DW_LANG_Crystal:
13535 return translation_unit::LANG_Crystal;
13536#endif
13537#ifdef HAVE_DW_LANG_HIP
13538 case DW_LANG_HIP:
13539 return translation_unit::LANG_HIP;
13540#endif
13541#ifdef HAVE_DW_LANG_C_sharp
13542 case DW_LANG_C_sharp:
13543 return translation_unit::LANG_C_sharp;
13544#endif
13545#ifdef HAVE_DW_LANG_Mojo
13546 case DW_LANG_Mojo:
13547 return translation_unit::LANG_Mojo;
13548#endif
13549#ifdef HAVE_DW_LANG_GLSL
13550 case DW_LANG_GLSL:
13551 return translation_unit::LANG_GLSL;
13552#endif
13553#ifdef HAVE_DW_LANG_GLSL_ES
13554 case DW_LANG_GLSL_ES:
13555 return translation_unit::LANG_GLSL_ES;
13556#endif
13557#ifdef HAVE_DW_LANG_HLSL
13558 case DW_LANG_HLSL:
13559 return translation_unit::LANG_HLSL;
13560#endif
13561#ifdef HAVE_DW_LANG_OpenCL_CPP
13562 case DW_LANG_OpenCL_CPP:
13563 return translation_unit::LANG_OpenCL_CPP;
13564#endif
13565#ifdef HAVE_DW_LANG_CPP_for_OpenCL
13566 case DW_LANG_CPP_for_OpenCL:
13567 return translation_unit::LANG_CPP_for_OpenCL;
13568#endif
13569#ifdef HAVE_DW_LANG_SYCL
13570 case DW_LANG_SYCL:
13571 return translation_unit::LANG_SYCL;
13572#endif
13573#ifdef HAVE_DW_LANG_Odin
13574 case DW_LANG_Odin:
13575 return translation_unit::LANG_Odin;
13576#endif
13577#ifdef HAVE_DW_LANG_P4
13578 case DW_LANG_P4:
13579 return translation_unit::LANG_P4;
13580#endif
13581#ifdef HAVE_DW_LANG_Move
13582 case DW_LANG_Move:
13583 return translation_unit::LANG_Move;
13584#endif
13585#ifdef HAVE_DW_LANG_Hylo
13586 case DW_LANG_Hylo:
13587 return translation_unit::LANG_Hylo;
13588#endif
13589
13590 default:
13591 return translation_unit::LANG_UNKNOWN;
13592 }
13593}
13594
13595/// Get the default array lower bound value as defined by the DWARF
13596/// specification, version 4, depending on the language of the
13597/// translation unit.
13598///
13599/// @param l the language of the translation unit.
13600///
13601/// @return the default array lower bound value.
13602static uint64_t
13603get_default_array_lower_bound(translation_unit::language l)
13604{
13605 int value = 0;
13606 switch (l)
13607 {
13608 case translation_unit::LANG_UNKNOWN:
13609 case translation_unit::LANG_C89:
13610 case translation_unit::LANG_C99:
13611 case translation_unit::LANG_C11:
13612 case translation_unit::LANG_C17:
13613 case translation_unit::LANG_C23:
13614 case translation_unit::LANG_C:
13615 case translation_unit::LANG_C_plus_plus_03:
13616 case translation_unit::LANG_C_plus_plus_11:
13617 case translation_unit::LANG_C_plus_plus_14:
13618 case translation_unit::LANG_C_plus_plus_17:
13619 case translation_unit::LANG_C_plus_plus_20:
13620 case translation_unit::LANG_C_plus_plus_23:
13621 case translation_unit::LANG_C_plus_plus:
13622 case translation_unit::LANG_OCaml:
13623 case translation_unit::LANG_ObjC:
13624 case translation_unit::LANG_ObjC_plus_plus:
13625 case translation_unit::LANG_D:
13626 case translation_unit::LANG_Rust:
13627 case translation_unit::LANG_Go:
13628 case translation_unit::LANG_Zig:
13629 case translation_unit::LANG_Metal:
13630 case translation_unit::LANG_Java:
13631 case translation_unit::LANG_Kotlin:
13632 case translation_unit::LANG_Python:
13633 case translation_unit::LANG_Ruby:
13634 case translation_unit::LANG_UPC:
13635 case translation_unit::LANG_Mips_Assembler:
13636 case translation_unit::LANG_Assembly:
13637 case translation_unit::LANG_Crystal:
13638 case translation_unit::LANG_HIP:
13639 case translation_unit::LANG_C_sharp:
13640 case translation_unit::LANG_Mojo:
13641 case translation_unit::LANG_GLSL:
13642 case translation_unit::LANG_GLSL_ES:
13643 case translation_unit::LANG_HLSL:
13644 case translation_unit::LANG_Odin:
13645 case translation_unit::LANG_P4:
13646 case translation_unit::LANG_OpenCL_CPP:
13647 case translation_unit::LANG_CPP_for_OpenCL:
13648 case translation_unit::LANG_SYCL:
13649 case translation_unit::LANG_Move:
13650 case translation_unit::LANG_Hylo:
13651 value = 0;
13652 break;
13653 case translation_unit::LANG_Cobol74:
13654 case translation_unit::LANG_Cobol85:
13655 case translation_unit::LANG_Fortran77:
13656 case translation_unit::LANG_Fortran90:
13657 case translation_unit::LANG_Fortran95:
13658 case translation_unit::LANG_Fortran18:
13659 case translation_unit::LANG_Fortran23:
13660 case translation_unit::LANG_Ada83:
13661 case translation_unit::LANG_Ada95:
13662 case translation_unit::LANG_Ada2005:
13663 case translation_unit::LANG_Ada2012:
13664 case translation_unit::LANG_Pascal83:
13665 case translation_unit::LANG_Modula2:
13666 case translation_unit::LANG_PLI:
13667 value = 1;
13668 break;
13669 }
13670
13671 return value;
13672}
13673
13674/// For a given offset, find the lower bound of a sorted vector of
13675/// imported unit point offset.
13676///
13677/// The lower bound is the smallest point (the point with the smallest
13678/// offset) which is the greater than a given offset.
13679///
13680/// @param imported_unit_points_type the sorted vector of imported
13681/// unit points.
13682///
13683/// @param val the offset to consider when looking for the lower
13684/// bound.
13685///
13686/// @param r an iterator to the lower bound found. This parameter is
13687/// set iff the function returns true.
13688///
13689/// @return true iff the lower bound has been found.
13690static bool
13691find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
13692 Dwarf_Off val,
13693 imported_unit_points_type::const_iterator& r)
13694{
13695 imported_unit_point v(val);
13696 imported_unit_points_type::const_iterator result =
13697 std::lower_bound(p.begin(), p.end(), v);
13698
13699 bool is_ok = result != p.end();
13700
13701 if (is_ok)
13702 r = result;
13703
13704 return is_ok;
13705}
13706
13707/// Given a DW_TAG_compile_unit, build and return the corresponding
13708/// abigail::translation_unit ir node. Note that this function
13709/// recursively reads the children dies of the current DIE and
13710/// populates the resulting translation unit.
13711///
13712/// @param rdr the DWARF reader to use.
13713///
13714/// @param die the DW_TAG_compile_unit DIE to consider.
13715///
13716/// @param address_size the size of the addresses expressed in this
13717/// translation unit in general.
13718///
13719/// @return a pointer to the resulting translation_unit.
13721build_translation_unit_and_add_to_ir(reader& rdr,
13722 Dwarf_Die* die,
13723 char address_size)
13724{
13725 translation_unit_sptr result;
13726
13727 if (!die)
13728 return result;
13729 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
13730
13731 // Clear the part of the context that is dependent on the translation
13732 // unit we are reading.
13733 rdr.clear_per_translation_unit_data();
13734
13735 rdr.cur_tu_die(die);
13736
13737 string path = die_string_attribute(die, DW_AT_name);
13738 if (path == "<artificial>")
13739 {
13740 // This is a file artificially generated by the compiler, so its
13741 // name is '<artificial>'. As we want all different translation
13742 // units to have unique path names, let's suffix this path name
13743 // with its die offset.
13744 std::ostringstream o;
13745 o << path << "-" << std::hex << dwarf_dieoffset(die);
13746 path = o.str();
13747 }
13748 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
13749
13750 // See if the same translation unit exits already in the current
13751 // corpus. Sometimes, the same translation unit can be present
13752 // several times in the same debug info. The content of the
13753 // different instances of the translation unit are different. So to
13754 // represent that, we are going to re-use the same translation
13755 // unit. That is, it's going to be the union of all the translation
13756 // units of the same path.
13757 {
13758 const string& abs_path =
13759 compilation_dir.empty() ? path : compilation_dir + "/" + path;
13760 result = rdr.corpus()->find_translation_unit(abs_path);
13761 }
13762
13763 if (!result)
13764 {
13765 result.reset(new translation_unit(rdr.env(),
13766 path,
13767 address_size));
13768 result->set_compilation_dir_path(compilation_dir);
13769 rdr.corpus()->add(result);
13770 uint64_t l = 0;
13771 die_unsigned_constant_attribute(die, DW_AT_language, l);
13772 result->set_language(dwarf_language_to_tu_language(l));
13773 }
13774
13775 rdr.cur_transl_unit(result);
13776 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
13777
13778 Dwarf_Die child;
13779 if (dwarf_child(die, &child) != 0)
13780 return result;
13781
13782 result->set_is_constructed(false);
13783 int tag = dwarf_tag(&child);
13784 do
13785 if (rdr.load_undefined_interfaces()
13786 && (rdr.is_decl_die_with_undefined_symbol(&child)
13787 || tag == DW_TAG_class_type // Top-level classes might
13788 // have undefined interfaces
13789 // that need to be
13790 // represented, so let's
13791 // analyze them as well.
13792 || ((tag == DW_TAG_union_type || tag == DW_TAG_structure_type)
13793 && die_is_in_cplus_plus(&child))))
13794 {
13795 // Analyze undefined functions & variables for the purpose of
13796 // analyzing compatibility matters.
13797 build_ir_node_from_die(rdr, &child,
13798 // Pretend the DIE is publicly defined
13799 // so that types that are reachable
13800 // from it get analyzed as well.
13801 /*die_is_public=*/true,
13802 dwarf_dieoffset(&child));
13803 }
13804 else if (!rdr.env().analyze_exported_interfaces_only()
13805 || rdr.is_decl_die_with_exported_symbol(&child))
13806 {
13807 // Analyze all the DIEs we encounter unless we are asked to only
13808 // analyze exported interfaces and the types reachables from them.
13809 build_ir_node_from_die(rdr, &child,
13810 die_is_public_decl(&child),
13811 dwarf_dieoffset(&child));
13812 }
13813 while (dwarf_siblingof(&child, &child) == 0);
13814
13815 if (!rdr.var_decls_to_re_add_to_tree().empty())
13816 for (list<var_decl_sptr>::const_iterator v =
13817 rdr.var_decls_to_re_add_to_tree().begin();
13818 v != rdr.var_decls_to_re_add_to_tree().end();
13819 ++v)
13820 {
13821 if (is_member_decl(*v))
13822 continue;
13823
13824 ABG_ASSERT((*v)->get_scope());
13825 string demangled_name =
13826 demangle_cplus_mangled_name((*v)->get_linkage_name());
13827 if (!demangled_name.empty())
13828 {
13829 std::list<string> fqn_comps;
13830 fqn_to_components(demangled_name, fqn_comps);
13831 string mem_name = fqn_comps.back();
13832 fqn_comps.pop_back();
13833 class_decl_sptr class_type;
13834 string ty_name;
13835 if (!fqn_comps.empty())
13836 {
13837 ty_name = components_to_type_name(fqn_comps);
13838 class_type =
13839 lookup_class_type(ty_name, *rdr.cur_transl_unit());
13840 }
13841 if (class_type)
13842 {
13843 // So we are seeing a member variable for which there
13844 // is a global variable definition DIE not having a
13845 // reference attribute pointing back to the member
13846 // variable declaration DIE. Thus remove the global
13847 // variable definition from its current non-class
13848 // scope ...
13849 decl_base_sptr d;
13850 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
13851 // This is the data member with the same name in cl.
13852 // We just need to flag it as static.
13853 ;
13854 else
13855 {
13856 // In this case there is no data member with the
13857 // same name in cl already. Let's add it there then
13858 // ...
13860 d = add_decl_to_scope(*v, class_type);
13861 }
13862
13863 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
13864 // Let's flag the data member as static.
13865 set_member_is_static(d, true);
13866 }
13867 }
13868 }
13869 rdr.var_decls_to_re_add_to_tree().clear();
13870
13871 result->set_is_constructed(true);
13872
13873 return result;
13874}
13875
13876/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
13877/// DW_TAG_module (for fortran) DIE.
13878///
13879/// Note that this function connects the DW_TAG_namespace to the IR
13880/// being currently created, reads the children of the DIE and
13881/// connects them to the IR as well.
13882///
13883/// @param rdr the DWARF reader to use.
13884///
13885/// @param die the DIE to read from. Must be either DW_TAG_namespace
13886/// or DW_TAG_module.
13887///
13888/// @param where_offset the offset of the DIE where we are "logically"
13889/// positionned at, in the DIE tree. This is useful when @p die is
13890/// e.g, DW_TAG_partial_unit that can be included in several places in
13891/// the DIE tree.
13892///
13893/// @return the resulting @ref abigail::namespace_decl or NULL if it
13894/// couldn't be created.
13896build_namespace_decl_and_add_to_ir(reader& rdr,
13897 Dwarf_Die* die,
13898 size_t where_offset)
13899{
13900 namespace_decl_sptr result;
13901
13902 if (!die)
13903 return result;
13904
13905 unsigned tag = dwarf_tag(die);
13906 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
13907 return result;
13908
13909 scope_decl_sptr scope = get_scope_for_die(rdr, die,
13910 /*called_for_public_decl=*/false,
13911 where_offset);
13912
13913 string name, linkage_name;
13914 location loc;
13915 die_loc_and_name(rdr, die, loc, name, linkage_name);
13916
13917 result.reset(new namespace_decl(rdr.env(), name, loc));
13918 add_decl_to_scope(result, scope.get());
13919 rdr.associate_die_to_decl(die, result, where_offset);
13920
13921 Dwarf_Die child;
13922 if (dwarf_child(die, &child) != 0)
13923 return result;
13924
13925 rdr.scope_stack().push(result.get());
13926 do
13927 build_ir_node_from_die(rdr, &child,
13928 // If this namespace DIE is private
13929 // (anonymous) then all its content is
13930 // considered private. Otherwise, its
13931 // public decls are considered public.
13932 /*called_from_public_decl=*/
13933 die_is_public_decl(die) && die_is_public_decl(&child),
13934 where_offset);
13935 while (dwarf_siblingof(&child, &child) == 0);
13936 rdr.scope_stack().pop();
13937
13938 return result;
13939}
13940
13941/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
13942///
13943/// @param rdr the DWARF reader to use.
13944///
13945/// @param die the DW_TAG_base_type to consider.
13946///
13947/// @param where_offset where we are logically at in the DIE stream.
13948///
13949/// @return the resulting decl_base_sptr.
13950static type_decl_sptr
13951build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
13952{
13953 type_decl_sptr result;
13954
13955 if (!die)
13956 return result;
13957 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
13958
13959 uint64_t byte_size = 0, bit_size = 0;
13960 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
13961 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
13962 return result;
13963
13964 if (bit_size == 0 && byte_size != 0)
13965 // Update the bit size.
13966 bit_size = byte_size * 8;
13967
13968 string type_name, linkage_name;
13969 location loc;
13970 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
13971
13972 if (byte_size == 0)
13973 {
13974 // The size of the type is zero, that must mean that we are
13975 // looking at the definition of the void type.
13976 if (type_name == "void")
13977 result = is_type_decl(build_ir_node_for_void_type(rdr));
13978 else
13979 // A type of size zero that is not void? Hmmh, I am not sure
13980 // what that means. Return nil for now.
13981 return result;
13982 }
13983
13984 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13985 {
13986 string normalized_type_name = type_name;
13987 real_type real_type;
13988 if (parse_real_type(type_name, real_type))
13989 normalized_type_name = real_type.to_string();
13990 result = lookup_basic_type(normalized_type_name, *corp);
13991 }
13992
13993 if (!result)
13994 if (corpus_sptr corp = rdr.corpus())
13995 result = lookup_basic_type(type_name, *corp);
13996 if (!result)
13997 result.reset(new type_decl(rdr.env(), type_name, bit_size,
13998 /*alignment=*/0, loc, linkage_name));
13999 rdr.associate_die_to_type(die, result, where_offset);
14000 return result;
14001}
14002
14003/// Construct the type that is to be used as the underlying type of an
14004/// enum.
14005///
14006/// @param rdr the DWARF reader to use.
14007///
14008/// @param enum_name the name of the enum that this type is going to
14009/// be the underlying type of.
14010///
14011/// @param enum_size the size of the enum.
14012///
14013/// @param is_anonymous whether the underlying type is anonymous or
14014/// not. By default, this should be set to true as before c++11 (and
14015/// in C), it's almost the case.
14016static type_decl_sptr
14017build_enum_underlying_type(reader& rdr,
14018 string enum_name,
14019 uint64_t enum_size,
14020 bool is_anonymous = true)
14021{
14022 string underlying_type_name =
14023 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
14024 enum_size);
14025
14026 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
14027 enum_size, enum_size, location()));
14028 result->set_is_anonymous(is_anonymous);
14029 result->set_is_artificial(true);
14030 translation_unit_sptr tu = rdr.cur_transl_unit();
14031 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
14032 result = dynamic_pointer_cast<type_decl>(d);
14033 ABG_ASSERT(result);
14034 maybe_canonicalize_type(result, rdr);
14035 return result;
14036}
14037
14038/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
14039///
14040/// @param rdr the DWARF reader to use.
14041///
14042/// @param die the DIE to read from.
14043///
14044/// @param scope the scope of the final enum. Note that this function
14045/// does *NOT* add the built type to this scope. The scope is just so
14046/// that the function knows how to name anonymous enums.
14047///
14048/// @param is_declaration_only is true if the DIE denoted by @p die is
14049/// a declaration-only DIE.
14050///
14051/// @return the built enum_type_decl or NULL if it could not be built.
14053build_enum_type(reader& rdr,
14054 Dwarf_Die* die,
14055 scope_decl* scope,
14056 size_t where_offset,
14057 bool is_declaration_only)
14058{
14059 enum_type_decl_sptr result;
14060 if (!die)
14061 return result;
14062
14063 unsigned tag = dwarf_tag(die);
14064 if (tag != DW_TAG_enumeration_type)
14065 return result;
14066
14067 string name, linkage_name;
14068 location loc;
14069 die_loc_and_name(rdr, die, loc, name, linkage_name);
14070
14071 bool is_anonymous = false;
14072 // If the enum is anonymous, let's give it a name.
14073 if (name.empty())
14074 {
14075 name = get_internal_anonymous_die_prefix_name(die);
14076 ABG_ASSERT(!name.empty());
14077 // But we remember that the type is anonymous.
14078 is_anonymous = true;
14079
14080 scope_decl* sc = scope ? scope : rdr.global_scope().get();
14081 if (size_t s = sc->get_num_anonymous_member_enums())
14082 name = build_internal_anonymous_die_name(name, s);
14083 }
14084
14085 bool use_odr = rdr.odr_is_relevant(die);
14086 // If the type has location, then associate it to its
14087 // representation. This way, all occurences of types with the same
14088 // representation (name) and location can be later detected as being
14089 // for the same type.
14090
14091 if (!is_anonymous)
14092 {
14093 if (use_odr)
14094 {
14095 if (enum_type_decl_sptr pre_existing_enum =
14096 is_enum_type(rdr.lookup_artifact_from_die(die)))
14097 result = pre_existing_enum;
14098 }
14099 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14100 {
14101 if (loc)
14102 result = lookup_enum_type_per_location(loc.expand(), *corp);
14103 }
14104 else if (loc)
14105 {
14106 if (enum_type_decl_sptr pre_existing_enum =
14107 is_enum_type(rdr.lookup_artifact_from_die(die)))
14108 if (pre_existing_enum->get_location() == loc)
14109 result = pre_existing_enum;
14110 }
14111
14112 if (result)
14113 {
14114 rdr.associate_die_to_type(die, result, where_offset);
14115 return result;
14116 }
14117 }
14118 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
14119 // we can look them up?
14120
14121 uint64_t size = 0;
14122 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14123 size *= 8;
14124 bool is_artificial = die_is_artificial(die);
14125
14126 // for now we consider that underlying types of enums are all anonymous
14127 bool enum_underlying_type_is_anonymous= true;
14128
14130 Dwarf_Die child;
14131 if (dwarf_child(die, &child) == 0)
14132 {
14133 do
14134 {
14135 if (dwarf_tag(&child) != DW_TAG_enumerator)
14136 continue;
14137
14138 string n, m;
14139 location l;
14140 die_loc_and_name(rdr, &child, l, n, m);
14141 uint64_t val = 0;
14142 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
14143 enms.push_back(enum_type_decl::enumerator(n, val));
14144 }
14145 while (dwarf_siblingof(&child, &child) == 0);
14146 }
14147
14148 // DWARF up to version 4 (at least) doesn't seem to carry the
14149 // underlying type, so let's create an artificial one here, which
14150 // sole purpose is to be passed to the constructor of the
14151 // enum_type_decl type.
14152 type_decl_sptr t =
14153 build_enum_underlying_type(rdr, name, size,
14154 enum_underlying_type_is_anonymous);
14155 t->set_is_declaration_only(is_declaration_only);
14156
14157 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
14158 result->set_is_anonymous(is_anonymous);
14159 result->set_is_declaration_only(is_declaration_only);
14160 result->set_is_artificial(is_artificial);
14161 rdr.associate_die_to_type(die, result, where_offset);
14162
14163 return result;
14164}
14165
14166/// Once a function_decl has been built and added to a class as a
14167/// member function, this function updates the information of the
14168/// function_decl concerning the properties of its relationship with
14169/// the member class. That is, it updates properties like
14170/// virtualness, access, constness, cdtorness, etc ...
14171///
14172/// @param die the DIE of the function_decl that has been just built.
14173///
14174/// @param f the function_decl that has just been built from @p die.
14175///
14176/// @param klass the @ref class_or_union that @p f belongs to.
14177///
14178/// @param rdr the context used to read the ELF/DWARF information.
14179static void
14180finish_member_function_reading(Dwarf_Die* die,
14181 const function_decl_sptr& f,
14182 const class_or_union_sptr klass,
14183 reader& rdr)
14184{
14185 ABG_ASSERT(klass);
14186
14187 method_decl_sptr m = is_method_decl(f);
14188 ABG_ASSERT(m);
14189
14190 method_type_sptr method_t = is_method_type(m->get_type());
14191 ABG_ASSERT(method_t);
14192
14193 size_t is_inline = die_is_declared_inline(die);
14194 bool is_ctor = (f->get_name() == klass->get_name());
14195 bool is_dtor = (!f->get_name().empty()
14196 && static_cast<string>(f->get_name())[0] == '~');
14197 bool is_virtual = die_is_virtual(die);
14198 int64_t vindex = -1;
14199 if (is_virtual)
14200 die_virtual_function_index(die, vindex);
14201 access_specifier access = public_access;
14202 if (class_decl_sptr c = is_class_type(klass))
14203 if (!c->is_struct())
14204 access = private_access;
14205 die_access_specifier(die, access);
14206
14207 m->is_declared_inline(is_inline);
14208 set_member_access_specifier(m, access);
14209 if (is_virtual)
14210 set_member_function_virtuality(m, is_virtual, vindex);
14211 bool is_static = method_t->get_is_for_static_method();
14212 set_member_is_static(m, is_static);
14213 set_member_function_is_ctor(m, is_ctor);
14214 set_member_function_is_dtor(m, is_dtor);
14215 set_member_function_is_const(m, method_t->get_is_const());
14216
14218
14219 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol()
14221 {
14222 // This is a virtual member function which has a linkage name
14223 // but has no underlying symbol set.
14224 //
14225 // The underlying elf symbol to set to this function can show up
14226 // later in the DWARF input or it can be that, because of some
14227 // compiler optimization, the relation between this function and
14228 // its underlying elf symbol is simply not emitted in the DWARF.
14229 //
14230 // Let's thus schedule this function for a later fixup pass
14231 // (performed by
14232 // reader::fixup_functions_with_no_symbols()) that will
14233 // set its underlying symbol.
14234 //
14235 // Note that if the underying symbol is encountered later in the
14236 // DWARF input, then the part of build_function_decl() that
14237 // updates the function to set its underlying symbol will
14238 // de-schedule this function wrt fixup pass.
14239 Dwarf_Off die_offset = dwarf_dieoffset(die);
14240 die_function_decl_map_type &fns_with_no_symbol =
14241 rdr.die_function_decl_with_no_symbol_map();
14242 die_function_decl_map_type::const_iterator i =
14243 fns_with_no_symbol.find(die_offset);
14244 if (i == fns_with_no_symbol.end())
14245 fns_with_no_symbol[die_offset] = f;
14246 }
14247
14248}
14249
14250/// If a function DIE has attributes which have not yet been read and
14251/// added to the internal representation that represents that function
14252/// then read those extra attributes and update the internal
14253/// representation.
14254///
14255/// @param rdr the DWARF reader to use.
14256///
14257/// @param die the function DIE to consider.
14258///
14259/// @param where_offset where we logical are, currently, in the stream
14260/// of DIEs. If you don't know what this is, you can just set it to zero.
14261///
14262/// @param existing_fn the representation of the function to update.
14263///
14264/// @return the updated function representation.
14265static function_decl_sptr
14266maybe_finish_function_decl_reading(reader& rdr,
14267 Dwarf_Die* die,
14268 size_t where_offset,
14269 const function_decl_sptr& existing_fn)
14270{
14271 function_decl_sptr result = build_function_decl(rdr, die,
14272 where_offset,
14273 existing_fn);
14274
14275 return result;
14276}
14277
14278/// Lookup a class or a typedef with a given qualified name in the
14279/// corpus that a given scope belongs to.
14280///
14281/// @param scope the scope to consider.
14282///
14283/// @param type_name the qualified name of the type to look for.
14284///
14285/// @return the typedef or class type found.
14286static type_base_sptr
14287lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
14288{
14289 string qname = build_qualified_name(scope, type_name);
14290 corpus* corp = scope->get_corpus();
14291 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
14292 return result;
14293}
14294
14295/// Lookup a class of typedef type from the current corpus being
14296/// constructed.
14297///
14298/// The type being looked for has the same name as a given DIE.
14299///
14300/// @param rdr the DWARF reader to use.
14301///
14302/// @param die the DIE which has the same name as the type we are
14303/// looking for.
14304///
14305/// @param called_for_public_decl whether this function is being
14306/// called from a a publicly defined declaration.
14307///
14308/// @param where_offset where we are logically at in the DIE stream.
14309///
14310/// @return the type found.
14311static type_base_sptr
14312lookup_class_or_typedef_from_corpus(reader& rdr,
14313 Dwarf_Die* die,
14314 bool called_for_public_decl,
14315 size_t where_offset)
14316{
14317 if (!die)
14318 return class_decl_sptr();
14319
14320 string class_name = die_string_attribute(die, DW_AT_name);
14321 if (class_name.empty())
14322 return class_decl_sptr();
14323
14324 scope_decl_sptr scope = get_scope_for_die(rdr, die,
14325 called_for_public_decl,
14326 where_offset);
14327 if (scope)
14328 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
14329
14330 return type_base_sptr();
14331}
14332
14333/// Test if a DIE represents a function that is a member of a given
14334/// class type.
14335///
14336/// @param rdr the DWARF reader.
14337///
14338/// @param function_die the DIE of the function to consider.
14339///
14340/// @param class_type the class type to consider.
14341///
14342/// @param where_offset where we are logically at in the DIE stream.
14343///
14344/// @return the method declaration corresponding to the member
14345/// function of @p class_type, iff @p function_die is for a member
14346/// function of @p class_type.
14347static method_decl_sptr
14348is_function_for_die_a_member_of_class(reader& rdr,
14349 Dwarf_Die* function_die,
14350 const class_or_union_sptr& class_type)
14351{
14352 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
14353
14354 if (!artifact)
14355 return method_decl_sptr();
14356
14357 method_decl_sptr method = is_method_decl(artifact);
14358 method_type_sptr method_type;
14359
14360 if (method)
14361 method_type = method->get_type();
14362 else
14363 method_type = is_method_type(artifact);
14364 ABG_ASSERT(method_type);
14365
14366 class_or_union_sptr method_class = method_type->get_class_type();
14367 ABG_ASSERT(method_class);
14368
14369 string method_class_name = method_class->get_qualified_name(),
14370 class_type_name = class_type->get_qualified_name();
14371
14372 if (method_class_name == class_type_name)
14373 {
14374 //ABG_ASSERT(class_type.get() == method_class.get());
14375 return method;
14376 }
14377
14378 return method_decl_sptr();
14379}
14380
14381/// If a given function DIE represents an existing member function of
14382/// a given class, then update that member function with new
14383/// properties present in the DIE. Otherwise, if the DIE represents a
14384/// new member function that is not already present in the class then
14385/// add that new member function to the class.
14386///
14387/// @param rdr the DWARF reader.
14388///
14389/// @param function_die the DIE of the potential member function to
14390/// consider.
14391///
14392/// @param class_type the class type to consider.
14393///
14394/// @param called_from_public_decl is true iff this function was
14395/// called from a publicly defined and exported declaration.
14396///
14397/// @param where_offset where we are logically at in the DIE stream.
14398///
14399/// @return the method decl representing the member function.
14400static method_decl_sptr
14401add_or_update_member_function(reader& rdr,
14402 Dwarf_Die* function_die,
14403 const class_or_union_sptr& class_type,
14404 bool called_from_public_decl,
14405 size_t where_offset)
14406{
14407 method_decl_sptr method =
14408 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
14409
14410 if (!method)
14411 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
14412 class_type.get(),
14413 called_from_public_decl,
14414 where_offset));
14415 if (!method)
14416 return method_decl_sptr();
14417
14418 finish_member_function_reading(function_die,
14419 is_function_decl(method),
14420 class_type, rdr);
14421 return method;
14422}
14423
14424/// Build a an IR node for class type from a DW_TAG_structure_type or
14425/// DW_TAG_class_type DIE and add that node to the ABI corpus being
14426/// currently built.
14427///
14428/// If the represents class type that already exists, then update the
14429/// existing class type with the new properties found in the DIE.
14430///
14431/// It meanst that this function can also update an existing
14432/// class_decl node with data members, member functions and other
14433/// properties coming from the DIE.
14434///
14435/// @param rdr the DWARF reader to consider.
14436///
14437/// @param die the DIE to read information from. Must be either a
14438/// DW_TAG_structure_type or a DW_TAG_class_type.
14439///
14440/// @param scope a pointer to the scope_decl* under which this class
14441/// is to be added to.
14442///
14443/// @param is_struct whether the class was declared as a struct.
14444///
14445/// @param klass if non-null, this is a klass to append the members
14446/// to. Otherwise, this function just builds the class from scratch.
14447///
14448/// @param called_from_public_decl set to true if this class is being
14449/// called from a "Public declaration like vars or public symbols".
14450///
14451/// @param where_offset the offset of the DIE where we are "logically"
14452/// positionned at, in the DIE tree. This is useful when @p die is
14453/// e.g, DW_TAG_partial_unit that can be included in several places in
14454/// the DIE tree.
14455///
14456/// @param is_declaration_only is true if the DIE denoted by @p die is
14457/// a declaration-only DIE.
14458///
14459/// @return the resulting class_type.
14460static class_decl_sptr
14461add_or_update_class_type(reader& rdr,
14462 Dwarf_Die* die,
14463 scope_decl* scope,
14464 bool is_struct,
14465 class_decl_sptr klass,
14466 bool called_from_public_decl,
14467 size_t where_offset,
14468 bool is_declaration_only)
14469{
14470 class_decl_sptr result;
14471 if (!die)
14472 return result;
14473
14474 const die_source source = rdr.get_die_source(die);
14475
14476 unsigned tag = dwarf_tag(die);
14477
14478 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
14479 return result;
14480
14481 {
14482 die_class_or_union_map_type::const_iterator i =
14483 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14484 if (i != rdr.die_wip_classes_map(source).end())
14485 {
14486 class_decl_sptr class_type = is_class_type(i->second);
14487 ABG_ASSERT(class_type);
14488 return class_type;
14489 }
14490 }
14491
14492 string name, linkage_name;
14493 location loc;
14494 die_loc_and_name(rdr, die, loc, name, linkage_name);
14495 cleanup_decl_name(name);
14496
14497 bool is_anonymous = false;
14498 if (name.empty())
14499 {
14500 // So we are looking at an anonymous struct. Let's
14501 // give it a name.
14502 name = get_internal_anonymous_die_prefix_name(die);
14503 ABG_ASSERT(!name.empty());
14504 // But we remember that the type is anonymous.
14505 is_anonymous = true;
14506
14507 size_t s = 0;
14508 if (scope)
14509 s = scope->get_num_anonymous_member_classes();
14510 else
14511 s = rdr.global_scope()->get_num_anonymous_member_classes();
14512 name = build_internal_anonymous_die_name(name, s);
14513 }
14514
14515 if (!is_anonymous)
14516 {
14517 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14518 {
14519 if (loc)
14520 // TODO: if there is only one class defined in the corpus
14521 // for this location, then re-use it. But if there are
14522 // more than one, then do not re-use it, for now.
14523 result = lookup_class_type_per_location(loc.expand(), *corp);
14524 else
14525 // TODO: if there is just one class for that name defined,
14526 // then re-use it. Otherwise, don't.
14527 result = lookup_class_type(name, *corp);
14528 if (result
14529 // If we are seeing a declaration of a definition we
14530 // already had, or if we are seing a type with the same
14531 // declaration-only-ness that we had before, then keep
14532 // the one we already had.
14533 && (result->get_is_declaration_only() == is_declaration_only
14534 || (!result->get_is_declaration_only()
14535 && is_declaration_only)))
14536 {
14537 rdr.associate_die_to_type(die, result, where_offset);
14538 return result;
14539 }
14540 else
14541 // We might be seeing the definition of a declaration we
14542 // already had. In that case, keep the definition and
14543 // drop the declaration.
14544 result.reset();
14545 }
14546 }
14547
14548 // If we've already seen the same class as 'die', then let's re-use
14549 // that one, unless it's an anonymous class. We can't really safely
14550 // re-use anonymous classes as they have no name, by construction.
14551 // What we can do, rather, is to reuse the typedef that name them,
14552 // when they do have a naming typedef.
14553 if (!is_anonymous)
14554 if (class_decl_sptr pre_existing_class =
14555 is_class_type(rdr.lookup_type_artifact_from_die(die)))
14556 klass = pre_existing_class;
14557
14558 uint64_t size = 0;
14559 die_size_in_bits(die, size);
14560 bool is_artificial = die_is_artificial(die);
14561
14562 Dwarf_Die child;
14563 bool has_child = (dwarf_child(die, &child) == 0);
14564
14565 decl_base_sptr res;
14566 if (klass)
14567 {
14568 res = result = klass;
14569 if (has_child && klass->get_is_declaration_only()
14570 && klass->get_definition_of_declaration())
14571 res = result = is_class_type(klass->get_definition_of_declaration());
14572 if (loc)
14573 result->set_location(loc);
14574 }
14575 else
14576 {
14577 result.reset(new class_decl(rdr.env(), name, size,
14578 /*alignment=*/0, is_struct, loc,
14579 decl_base::VISIBILITY_DEFAULT,
14580 is_anonymous));
14581
14582 result->set_is_declaration_only(is_declaration_only);
14583
14584 res = add_decl_to_scope(result, scope);
14585 result = dynamic_pointer_cast<class_decl>(res);
14586 ABG_ASSERT(result);
14587 }
14588
14589 if (!klass || klass->get_is_declaration_only())
14590 if (size != result->get_size_in_bits())
14591 result->set_size_in_bits(size);
14592
14593 if (klass)
14594 // We are amending a class that was built before. So let's check
14595 // if we need to amend its "declaration-only-ness" status.
14596 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
14597 // The size of the class doesn't match its
14598 // 'declaration-only-ness". We might have a non-zero sized
14599 // class which is declaration-only, or a zero sized class that
14600 // is not declaration-only. Let's set the declaration-only-ness
14601 // according to what we are instructed to.
14602 //
14603 // Note however that there are binaries out there emitted by
14604 // compilers (Clang, in C++) emit declarations-only classes that
14605 // have non-zero size. So we must honor these too. That is why
14606 // we are not forcing the declaration-only-ness to false when a
14607 // class has non-zero size. An example of such binary is
14608 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
14609 result->set_is_declaration_only(is_declaration_only);
14610
14611 // If a non-decl-only class has children node and is advertized as
14612 // having a non-zero size let's trust that.
14613 if (!result->get_is_declaration_only() && has_child)
14614 if (result->get_size_in_bits() == 0 && size != 0)
14615 result->set_size_in_bits(size);
14616
14617 result->set_is_artificial(is_artificial);
14618
14619 rdr.associate_die_to_type(die, result, where_offset);
14620
14621 if (!has_child)
14622 // TODO: set the access specifier for the declaration-only class
14623 // here.
14624 return result;
14625
14626 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14627
14628 bool is_incomplete_type = false;
14629 if (is_declaration_only && size == 0 && has_child)
14630 // this is an incomplete DWARF type as defined by [5.7.1]
14631 //
14632 // An incomplete structure, union or class type is represented by
14633 // a structure, union or class entry that does not have a byte
14634 // size attribute and that has a DW_AT_declaration attribute.
14635 //
14636 // Let's consider that it's thus a decl-only class, likely
14637 // referred to by a pointer. If we later encounter a definition
14638 // for this decl-only class type, then this decl-only class will
14639 // be resolved to it by the code in
14640 // reader::resolve_declaration_only_classes.
14641 is_incomplete_type = true;
14642
14643 scope_decl_sptr scop =
14644 dynamic_pointer_cast<scope_decl>(res);
14645 ABG_ASSERT(scop);
14646 rdr.scope_stack().push(scop.get());
14647
14648 if (has_child && !is_incomplete_type)
14649 {
14650 do
14651 {
14652 tag = dwarf_tag(&child);
14653
14654 // Handle base classes.
14655 if (tag == DW_TAG_inheritance)
14656 {
14657 result->set_is_declaration_only(false);
14658
14659 Dwarf_Die type_die;
14660 if (!die_die_attribute(&child, DW_AT_type, type_die))
14661 continue;
14662
14663 string type_name = die_type_name(rdr, &type_die,
14664 /*qualified_name=*/true,
14665 where_offset);
14666 type_base_sptr base_type;
14667 if (!type_name.empty())
14668 {
14669 base_type = result->find_base_class(type_name);
14670 if (base_type)
14671 continue;
14672 }
14673
14674 base_type =
14675 lookup_class_or_typedef_from_corpus(rdr, &type_die,
14676 called_from_public_decl,
14677 where_offset);
14678 if (!base_type)
14679 base_type =
14680 is_type(build_ir_node_from_die(rdr, &type_die,
14681 called_from_public_decl,
14682 where_offset));
14683
14684 // Sometimes base_type can be a typedef. Let's make
14685 // sure that typedef is compatible with a class type.
14687 if (!b)
14688 continue;
14689
14690 access_specifier access =
14691 is_struct
14692 ? public_access
14693 : private_access;
14694
14695 die_access_specifier(&child, access);
14696
14697 bool is_virt= die_is_virtual(&child);
14698 int64_t offset = 0;
14699 bool is_offset_present =
14700 die_member_offset(rdr, &child, offset);
14701
14702 class_decl::base_spec_sptr base(new class_decl::base_spec
14703 (b, access,
14704 is_offset_present ? offset : -1,
14705 is_virt));
14706 if (b->get_is_declaration_only()
14707 // Only non-anonymous decl-only classes are
14708 // scheduled for resolution to their definition.
14709 // Anonymous classes that are decl-only are likely
14710 // only artificially created by
14711 // get_opaque_version_of_type, from anonymous fully
14712 // defined classes. Those are never defined.
14713 && !b->get_qualified_name().empty())
14714 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
14715 if (result->find_base_class(b->get_qualified_name()))
14716 continue;
14717 result->add_base_specifier(base);
14718 }
14719 // Handle data members.
14720 else if (tag == DW_TAG_member
14721 || tag == DW_TAG_variable)
14722 {
14723 Dwarf_Die type_die;
14724 if (!die_die_attribute(&child, DW_AT_type, type_die))
14725 continue;
14726
14727 string n, m;
14728 location loc;
14729 die_loc_and_name(rdr, &child, loc, n, m);
14730 /// For now, we skip the hidden vtable pointer.
14731 /// Currently, we're looking for a member starting with
14732 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
14733 /// use as a name for the hidden vtable pointer.
14734 if (n.substr(0, 5) == "_vptr"
14735 && n.size() > 5
14736 && !std::isalnum(n.at(5))
14737 && n.at(5) != '_')
14738 continue;
14739
14740 // If the variable is already a member of this class,
14741 // move on. If it's an anonymous data member, we need
14742 // to handle it differently. We'll do that later below.
14743 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14744 continue;
14745
14746 int64_t offset_in_bits = 0;
14747 bool is_laid_out = die_member_offset(rdr, &child,
14748 offset_in_bits);
14749 // For now, is_static == !is_laid_out. When we have
14750 // templates, we'll try to be more specific. For now,
14751 // this approximation should do OK.
14752 bool is_static = !is_laid_out;
14753
14754 if (is_static)
14755 // We are looking at the *declaration* of a static
14756 // data member. The definition comes later (or
14757 // somewhere else, rather)in the DWARF. It's the
14758 // definition that we are interested in because it has
14759 // attributes of the concrete representation of the
14760 // static data member like, the ELF symbol (storage
14761 // address) of the variable, etc. It's at that point
14762 // that the IR of the data member is going to be
14763 // created (by build_ir_node_from_die, in the
14764 // DW_TAG_variable case) and added to this class/struct
14765 // being created. So for now, just ignore it.
14766 continue;
14767
14768 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
14769 called_from_public_decl,
14770 where_offset));
14771 type_base_sptr t = is_type(ty);
14772 if (!t)
14773 continue;
14774
14775 if (n.empty() && !die_is_anonymous_data_member(&child))
14776 {
14777 // We must be in a case where the data member has an
14778 // empty name because the DWARF emitter has a bug.
14779 // Let's generate an artificial name for that data
14780 // member.
14781 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
14782 ABG_ASSERT(!n.empty());
14783 }
14784
14785 // The call to build_ir_node_from_die above could have
14786 // triggered the adding of a data member named 'n' into
14787 // result. So let's check again if the variable is
14788 // already a member of this class. Here again, if it's
14789 // an anonymous data member, we need to handle it
14790 // differently. We'll do that later below.
14791 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14792 continue;
14793
14794 if (!is_static)
14795 // We have a non-static data member. So this class
14796 // cannot be a declaration-only class anymore, even if
14797 // some DWARF emitters might consider it otherwise.
14798 result->set_is_declaration_only(false);
14799 access_specifier access =
14800 is_struct
14801 ? public_access
14802 : private_access;
14803
14804 die_access_specifier(&child, access);
14805
14806 var_decl_sptr dm(new var_decl(n, t, loc, m));
14807 if (n.empty()
14809 // dm is an anonymous data member that was already
14810 // present in the current class so let's not add it.
14811 continue;
14812 result->add_data_member(dm, access, is_laid_out,
14813 is_static, offset_in_bits);
14814 ABG_ASSERT(has_scope(dm));
14815 rdr.associate_die_to_decl(&child, dm, where_offset,
14816 /*associate_by_repr=*/false);
14817 }
14818 // Handle member functions;
14819 else if (tag == DW_TAG_subprogram)
14820 {
14821 decl_base_sptr r =
14822 add_or_update_member_function(rdr, &child, result,
14823 called_from_public_decl,
14824 where_offset);
14826 rdr.associate_die_to_decl(&child, f, where_offset,
14827 /*associate_by_repr=*/true);
14828 }
14829 // Handle member types
14830 else if (die_is_type(&child))
14831 {
14832 // if the type is not already a member of this class,
14833 // then add it to the class.
14834 if (!is_anonymous_type_die(&child)
14835 && !result->find_member_type(die_name(&child)))
14836 build_ir_node_from_die(rdr, &child, result.get(),
14837 called_from_public_decl,
14838 where_offset);
14839 else if (is_anonymous_type_die(&child))
14840 {
14841 // Lookup the anonymous type DIE direcly by building
14842 // its flat representation & using it as the name of
14843 // the anonymous struct/union.
14844 string anonymous_type_name =
14845 die_class_or_enum_flat_representation(rdr, &child,
14846 /*indent=*/"",
14847 /*one_line=*/true,
14848 /*qualed_name=*/false,
14849 where_offset);
14850 if (type_base_sptr member_t =
14851 result->find_member_type(anonymous_type_name))
14852 rdr.associate_die_to_decl(&child, is_decl(member_t),
14853 where_offset,
14854 /*Associate_by_repr=*/false);
14855 else
14856 {
14857 type_base_sptr t =
14858 is_type(build_ir_node_from_die(rdr, &child,
14859 /*scope=*/result.get(),
14860 called_from_public_decl,
14861 where_offset));
14862 if (t)
14863 {
14864 add_decl_to_scope(is_decl(t), result.get());
14865 maybe_set_member_type_access_specifier(result,
14866 &child);
14867 }
14868 }
14869 }
14870 }
14871 } while (dwarf_siblingof(&child, &child) == 0);
14872 }
14873
14874 rdr.scope_stack().pop();
14875
14876 {
14877 die_class_or_union_map_type::const_iterator i =
14878 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14879 if (i != rdr.die_wip_classes_map(source).end())
14880 {
14881 if (is_member_type(i->second))
14883 get_member_access_specifier(i->second));
14884 rdr.die_wip_classes_map(source).erase(i);
14885 }
14886 }
14887
14888 return result;
14889}
14890
14891/// Build an @ref union_decl from a DW_TAG_union_type DIE.
14892///
14893/// @param rdr the DWARF reader to use.
14894///
14895/// @param die the DIE to read from.
14896///
14897/// @param scope the scope the resulting @ref union_decl belongs to.
14898///
14899/// @param union_type if this parameter is non-nil, then this function
14900/// updates the @ref union_decl that it points to, rather than
14901/// creating a new @ref union_decl.
14902///
14903/// @param called_from_public_decl is true if this function has been
14904/// initially called within the context of a public decl.
14905///
14906/// @param where_offset the offset of the DIE where we are "logically"
14907/// positionned at, in the DIE tree. This is useful when @p die is
14908/// e.g, DW_TAG_partial_unit that can be included in several places in
14909/// the DIE tree.
14910///
14911/// @param is_declaration_only is true if the DIE denoted by @p die is
14912/// a declaration-only DIE.
14913///
14914/// @return the resulting @ref union_decl type.
14915static union_decl_sptr
14916add_or_update_union_type(reader& rdr,
14917 Dwarf_Die* die,
14918 scope_decl* scope,
14919 union_decl_sptr union_type,
14920 bool called_from_public_decl,
14921 size_t where_offset,
14922 bool is_declaration_only)
14923{
14924 union_decl_sptr result;
14925 if (!die)
14926 return result;
14927
14928 unsigned tag = dwarf_tag(die);
14929
14930 if (tag != DW_TAG_union_type)
14931 return result;
14932
14933 const die_source source = rdr.get_die_source(die);
14934 {
14935 die_class_or_union_map_type::const_iterator i =
14936 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14937 if (i != rdr.die_wip_classes_map(source).end())
14938 {
14939 union_decl_sptr u = is_union_type(i->second);
14940 ABG_ASSERT(u);
14941 return u;
14942 }
14943 }
14944
14945 string name, linkage_name;
14946 location loc;
14947 die_loc_and_name(rdr, die, loc, name, linkage_name);
14948 cleanup_decl_name(name);
14949
14950 bool is_anonymous = false;
14951 if (name.empty())
14952 {
14953 // So we are looking at an anonymous union. Let's give it a
14954 // name.
14955 name = get_internal_anonymous_die_prefix_name(die);
14956 ABG_ASSERT(!name.empty());
14957 // But we remember that the type is anonymous.
14958 is_anonymous = true;
14959
14960 size_t s = 0;
14961 if (scope)
14962 s = scope->get_num_anonymous_member_unions();
14963 else
14964 s = rdr.global_scope()->get_num_anonymous_member_classes();
14965 name = build_internal_anonymous_die_name(name, s);
14966 }
14967
14968 // If the type has location, then associate it to its
14969 // representation. This way, all occurences of types with the same
14970 // representation (name) and location can be later detected as being
14971 // for the same type.
14972
14973 if (!is_anonymous)
14974 {
14975 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14976 {
14977 if (loc)
14978 result = lookup_union_type_per_location(loc.expand(), *corp);
14979 else
14980 result = lookup_union_type(name, *corp);
14981
14982 if (result)
14983 {
14984 rdr.associate_die_to_type(die, result, where_offset);
14985 return result;
14986 }
14987 }
14988 }
14989
14990 // if we've already seen a union with the same union as 'die' then
14991 // let's re-use that one. We can't really safely re-use anonymous
14992 // unions as they have no name, by construction. What we can do,
14993 // rather, is to reuse the typedef that name them, when they do have
14994 // a naming typedef.
14995 if (!is_anonymous)
14996 if (union_decl_sptr pre_existing_union =
14997 is_union_type(rdr.lookup_artifact_from_die(die)))
14998 union_type = pre_existing_union;
14999
15000 uint64_t size = 0;
15001 die_size_in_bits(die, size);
15002 bool is_artificial = die_is_artificial(die);
15003
15004 if (union_type)
15005 {
15006 result = union_type;
15007 result->set_location(loc);
15008 }
15009 else
15010 {
15011 result.reset(new union_decl(rdr.env(), name, size, loc,
15012 decl_base::VISIBILITY_DEFAULT,
15013 is_anonymous));
15014 if (is_declaration_only)
15015 result->set_is_declaration_only(true);
15016 result = is_union_type(add_decl_to_scope(result, scope));
15017 ABG_ASSERT(result);
15018 }
15019
15020 if (size)
15021 {
15022 result->set_size_in_bits(size);
15023 result->set_is_declaration_only(false);
15024 }
15025
15026 result->set_is_artificial(is_artificial);
15027
15028 rdr.associate_die_to_type(die, result, where_offset);
15029
15030 Dwarf_Die child;
15031 bool has_child = (dwarf_child(die, &child) == 0);
15032 if (!has_child)
15033 return result;
15034
15035 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
15036
15037 scope_decl_sptr scop =
15038 dynamic_pointer_cast<scope_decl>(result);
15039 ABG_ASSERT(scop);
15040 rdr.scope_stack().push(scop.get());
15041
15042 if (has_child)
15043 {
15044 do
15045 {
15046 tag = dwarf_tag(&child);
15047 // Handle data members.
15048 if (tag == DW_TAG_member || tag == DW_TAG_variable)
15049 {
15050 Dwarf_Die type_die;
15051 if (!die_die_attribute(&child, DW_AT_type, type_die))
15052 continue;
15053
15054 string n, m;
15055 location loc;
15056 die_loc_and_name(rdr, &child, loc, n, m);
15057
15058 // Because we can be updating an existing union, let's
15059 // make sure we don't already have a member of the same
15060 // name. Anonymous member are handled a bit later below
15061 // so let's not consider them here.
15062 if (!n.empty() && lookup_var_decl_in_scope(n, result))
15063 continue;
15064
15065 ssize_t offset_in_bits = 0;
15066 decl_base_sptr ty =
15067 is_decl(build_ir_node_from_die(rdr, &type_die,
15068 called_from_public_decl,
15069 where_offset));
15070 type_base_sptr t = is_type(ty);
15071 if (!t)
15072 continue;
15073
15074 // We have a non-static data member. So this union
15075 // cannot be a declaration-only union anymore, even if
15076 // some DWARF emitters might consider it otherwise.
15077 result->set_is_declaration_only(false);
15078 access_specifier access = public_access;
15079
15080 die_access_specifier(&child, access);
15081
15082 var_decl_sptr dm(new var_decl(n, t, loc, m));
15083 // If dm is an anonymous data member, let's make sure
15084 // the current union doesn't already have it as a data
15085 // member.
15086 if (n.empty() && result->find_data_member(dm))
15087 continue;
15088
15089 if (!n.empty() && lookup_var_decl_in_scope(n, result))
15090 continue;
15091
15092 result->add_data_member(dm, access, /*is_laid_out=*/true,
15093 /*is_static=*/false,
15094 offset_in_bits);
15095 ABG_ASSERT(has_scope(dm));
15096 rdr.associate_die_to_decl(&child, dm, where_offset,
15097 /*associate_by_repr=*/false);
15098 }
15099 // Handle member functions;
15100 else if (tag == DW_TAG_subprogram)
15101 {
15102 decl_base_sptr r =
15103 is_decl(build_ir_node_from_die(rdr, &child,
15104 result.get(),
15105 called_from_public_decl,
15106 where_offset));
15107 if (!r)
15108 continue;
15109
15110 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
15111 ABG_ASSERT(f);
15112
15113 finish_member_function_reading(&child, f, result, rdr);
15114
15115 rdr.associate_die_to_decl(&child, f, where_offset,
15116 /*associate_by_repr=*/false);
15117 }
15118 // Handle member types
15119 else if (die_is_type(&child))
15120 {
15121 string type_name = die_type_name(rdr, &child,
15122 /*qualified_name=*/false,
15123 where_offset);
15124 if (type_base_sptr member_t = result->find_member_type(type_name))
15125 rdr.associate_die_to_decl(&child, is_decl(member_t),
15126 where_offset,
15127 /*associate_by_repr=*/false);
15128 else
15129 decl_base_sptr td =
15130 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
15131 called_from_public_decl,
15132 where_offset));
15133 }
15134 } while (dwarf_siblingof(&child, &child) == 0);
15135 }
15136
15137 rdr.scope_stack().pop();
15138
15139 {
15140 die_class_or_union_map_type::const_iterator i =
15141 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
15142 if (i != rdr.die_wip_classes_map(source).end())
15143 {
15144 if (is_member_type(i->second))
15146 get_member_access_specifier(i->second));
15147 rdr.die_wip_classes_map(source).erase(i);
15148 }
15149 }
15150
15151 return result;
15152}
15153
15154/// build a qualified type from a DW_TAG_const_type,
15155/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
15156///
15157/// @param rdr the DWARF reader to consider.
15158///
15159/// @param die the input DIE to read from.
15160///
15161/// @param called_from_public_decl true if this function was called
15162/// from a context where either a public function or a public variable
15163/// is being built.
15164///
15165/// @param where_offset the offset of the DIE where we are "logically"
15166/// positionned at, in the DIE tree. This is useful when @p die is
15167/// e.g, DW_TAG_partial_unit that can be included in several places in
15168/// the DIE tree.
15169///
15170/// @return the resulting qualified_type_def.
15171static type_base_sptr
15172build_qualified_type(reader& rdr,
15173 Dwarf_Die* die,
15174 bool called_from_public_decl,
15175 size_t where_offset)
15176{
15177 type_base_sptr result;
15178 if (!die)
15179 return result;
15180
15181 unsigned tag = dwarf_tag(die);
15182
15183 if (tag != DW_TAG_const_type
15184 && tag != DW_TAG_volatile_type
15185 && tag != DW_TAG_restrict_type)
15186 return result;
15187
15188 Dwarf_Die underlying_type_die;
15189 decl_base_sptr utype_decl;
15190 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15191 // So, if no DW_AT_type is present, then this means (if we are
15192 // looking at a debug info emitted by GCC) that we are looking
15193 // at a qualified void type.
15194 utype_decl = build_ir_node_for_void_type(rdr);
15195
15196 if (!utype_decl)
15197 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
15198 called_from_public_decl,
15199 where_offset));
15200 if (!utype_decl)
15201 return result;
15202
15203 // The call to build_ir_node_from_die() could have triggered the
15204 // creation of the type for this DIE. In that case, just return it.
15205 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15206 {
15207 result = t;
15208 rdr.associate_die_to_type(die, result, where_offset);
15209 return result;
15210 }
15211
15212 type_base_sptr utype = is_type(utype_decl);
15213 ABG_ASSERT(utype);
15214
15215 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
15216 if (tag == DW_TAG_const_type)
15217 qual |= qualified_type_def::CV_CONST;
15218 else if (tag == DW_TAG_volatile_type)
15219 qual |= qualified_type_def::CV_VOLATILE;
15220 else if (tag == DW_TAG_restrict_type)
15221 qual |= qualified_type_def::CV_RESTRICT;
15222 else
15224
15225 if (!result)
15226 result.reset(new qualified_type_def(utype, qual, location()));
15227
15228 rdr.associate_die_to_type(die, result, where_offset);
15229
15230 return result;
15231}
15232
15233/// Walk a tree of typedef of qualified arrays and schedule all type
15234/// nodes for canonicalization.
15235///
15236/// This is to be used after an array tree has been cloned. In that
15237/// case, the newly cloned type nodes have to be scheduled for
15238/// canonicalization.
15239///
15240/// This is a subroutine of maybe_strip_qualification.
15241///
15242/// @param t the type node to be scheduled for canonicalization.
15243///
15244/// @param rdr the DWARF reader to use.
15245static void
15246schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
15247 reader &rdr)
15248{
15249 if (typedef_decl_sptr type = is_typedef(t))
15250 {
15251 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15252 rdr);
15253 rdr.schedule_type_for_late_canonicalization(t);
15254 }
15255 else if (qualified_type_def_sptr type = is_qualified_type(t))
15256 {
15257 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15258 rdr);
15259 rdr.schedule_type_for_late_canonicalization(t);
15260 }
15261 else if (array_type_def_sptr type = is_array_type(t))
15262 {
15263 for (vector<array_type_def::subrange_sptr>::const_iterator i =
15264 type->get_subranges().begin();
15265 i != type->get_subranges().end();
15266 ++i)
15267 {
15268 if (!(*i)->get_scope())
15269 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
15270 rdr.schedule_type_for_late_canonicalization(*i);
15271
15272 }
15273 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
15274 rdr);
15275 rdr.schedule_type_for_late_canonicalization(type);
15276 }
15277}
15278
15279/// Strip qualification from a qualified type, when it makes sense.
15280///
15281/// DWARF constructs "const reference". This is redundant because a
15282/// reference is always const. The issue is these redundant types then
15283/// leak into the IR and make for bad diagnostics.
15284///
15285/// This function thus strips the const qualifier from the type in
15286/// that case. It might contain code to strip other cases like this
15287/// in the future.
15288///
15289/// @param t the type to strip const qualification from.
15290///
15291/// @param rdr the @ref reader to use.
15292///
15293/// @return the stripped type or just return @p t.
15294static decl_base_sptr
15295maybe_strip_qualification(const qualified_type_def_sptr t,
15296 reader &rdr)
15297{
15298 if (!t)
15299 return t;
15300
15301 decl_base_sptr result = t;
15302 type_base_sptr u = t->get_underlying_type();
15303
15306 if (result.get() != t.get())
15307 return result;
15308
15310 {
15311 array_type_def_sptr array;
15312 scope_decl * scope = 0;
15313 if ((array = is_array_type(u)))
15314 {
15315 scope = array->get_scope();
15316 ABG_ASSERT(scope);
15317 array = is_array_type(clone_array_tree(array));
15318 schedule_array_tree_for_late_canonicalization(array, rdr);
15319 add_decl_to_scope(array, scope);
15320 t->set_underlying_type(array);
15321 u = t->get_underlying_type();
15322 }
15323 else if (is_typedef_of_array(u))
15324 {
15325 scope = is_decl(u)->get_scope();
15326 ABG_ASSERT(scope);
15327 typedef_decl_sptr typdef =
15329 schedule_array_tree_for_late_canonicalization(typdef, rdr);
15330 ABG_ASSERT(typdef);
15331 add_decl_to_scope(typdef, scope);
15332 t->set_underlying_type(typdef);
15333 u = t->get_underlying_type();
15334 array = is_typedef_of_array(u);
15335 }
15336 else
15338
15339 ABG_ASSERT(array);
15340 // We should not be editing types that are already canonicalized.
15341 ABG_ASSERT(!array->get_canonical_type());
15342 type_base_sptr element_type = array->get_element_type();
15343
15344 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
15345 {
15346 // We should not be editing types that are already canonicalized.
15347 ABG_ASSERT(!qualified->get_canonical_type());
15348 qualified_type_def::CV quals = qualified->get_cv_quals();
15349 quals |= t->get_cv_quals();
15350 qualified->set_cv_quals(quals);
15352 result = is_decl(u);
15353 }
15354 else
15355 {
15356 qualified_type_def_sptr qual_type
15357 (new qualified_type_def(element_type,
15358 t->get_cv_quals(),
15359 t->get_location()));
15361 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
15362 array->set_element_type(qual_type);
15363 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
15364 result = is_decl(u);
15365 }
15366 }
15367
15368 return result;
15369}
15370
15371/// Build a pointer type from a DW_TAG_pointer_type DIE.
15372///
15373/// @param rdr the DWARF reader to consider.
15374///
15375/// @param die the DIE to read information from.
15376///
15377/// @param called_from_public_decl true if this function was called
15378/// from a context where either a public function or a public variable
15379/// is being built.
15380///
15381/// @param where_offset the offset of the DIE where we are "logically"
15382/// positionned at, in the DIE tree. This is useful when @p die is
15383/// e.g, DW_TAG_partial_unit that can be included in several places in
15384/// the DIE tree.
15385///
15386/// @return the resulting pointer to pointer_type_def.
15388build_pointer_type_def(reader& rdr,
15389 Dwarf_Die* die,
15390 bool called_from_public_decl,
15391 size_t where_offset)
15392{
15393 pointer_type_def_sptr result;
15394
15395 if (!die)
15396 return result;
15397
15398 unsigned tag = dwarf_tag(die);
15399 if (tag != DW_TAG_pointer_type)
15400 return result;
15401
15402 type_or_decl_base_sptr utype_decl;
15403 Dwarf_Die underlying_type_die;
15404 bool has_underlying_type_die = false;
15405 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15406 // If the DW_AT_type attribute is missing, that means we are
15407 // looking at a pointer to "void".
15408 utype_decl = build_ir_node_for_void_type(rdr);
15409 else
15410 has_underlying_type_die = true;
15411
15412 if (!utype_decl && has_underlying_type_die)
15413 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
15414 called_from_public_decl,
15415 where_offset);
15416 if (!utype_decl)
15417 return result;
15418
15419 // The call to build_ir_node_from_die() could have triggered the
15420 // creation of the type for this DIE. In that case, just return it.
15421 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15422 {
15423 result = is_pointer_type(t);
15424 ABG_ASSERT(result);
15425 return result;
15426 }
15427
15428 type_base_sptr utype = is_type(utype_decl);
15429 ABG_ASSERT(utype);
15430
15431 // if the DIE for the pointer type doesn't have a byte_size
15432 // attribute then we assume the size of the pointer is the address
15433 // size of the current translation unit.
15434 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15435 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15436 // The size as expressed by DW_AT_byte_size is in byte, so let's
15437 // convert it to bits.
15438 size *= 8;
15439
15440 // And the size of the pointer must be the same as the address size
15441 // of the current translation unit.
15442 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15443
15444 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
15445 ABG_ASSERT(result->get_pointed_to_type());
15446
15447 if (is_void_pointer_type(result))
15448 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
15449
15450 rdr.associate_die_to_type(die, result, where_offset);
15451 return result;
15452}
15453
15454/// Build a reference type from either a DW_TAG_reference_type or
15455/// DW_TAG_rvalue_reference_type DIE.
15456///
15457/// @param rdr the DWARF reader to consider.
15458///
15459/// @param die the DIE to read from.
15460///
15461/// @param called_from_public_decl true if this function was called
15462/// from a context where either a public function or a public variable
15463/// is being built.
15464///
15465/// @param where_offset the offset of the DIE where we are "logically"
15466/// positionned at, in the DIE tree. This is useful when @p die is
15467/// e.g, DW_TAG_partial_unit that can be included in several places in
15468/// the DIE tree.
15469///
15470/// @return a pointer to the resulting reference_type_def.
15472build_reference_type(reader& rdr,
15473 Dwarf_Die* die,
15474 bool called_from_public_decl,
15475 size_t where_offset)
15476{
15478
15479 if (!die)
15480 return result;
15481
15482 unsigned tag = dwarf_tag(die);
15483 if (tag != DW_TAG_reference_type
15484 && tag != DW_TAG_rvalue_reference_type)
15485 return result;
15486
15487 Dwarf_Die underlying_type_die;
15488 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15489 return result;
15490
15491 type_or_decl_base_sptr utype_decl =
15492 build_ir_node_from_die(rdr, &underlying_type_die,
15493 called_from_public_decl,
15494 where_offset);
15495 if (!utype_decl)
15496 return result;
15497
15498 // The call to build_ir_node_from_die() could have triggered the
15499 // creation of the type for this DIE. In that case, just return it.
15500 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15501 {
15502 result = is_reference_type(t);
15503 ABG_ASSERT(result);
15504 return result;
15505 }
15506
15507 type_base_sptr utype = is_type(utype_decl);
15508 ABG_ASSERT(utype);
15509
15510 // if the DIE for the reference type doesn't have a byte_size
15511 // attribute then we assume the size of the reference is the address
15512 // size of the current translation unit.
15513 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15514 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15515 size *= 8;
15516
15517 // And the size of the pointer must be the same as the address size
15518 // of the current translation unit.
15519 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15520
15521 bool is_lvalue = tag == DW_TAG_reference_type;
15522
15523 result.reset(new reference_type_def(utype, is_lvalue, size,
15524 /*alignment=*/0,
15525 location()));
15526 if (corpus_sptr corp = rdr.corpus())
15527 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
15528 result = t;
15529 rdr.associate_die_to_type(die, result, where_offset);
15530 return result;
15531}
15532
15533/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
15534/// DW_TAG_ptr_to_member_type.
15535///
15536/// @param the DWARF reader touse.
15537///
15538/// @param the DIE to consider. It must carry the tag
15539/// DW_TAG_ptr_to_member_type.
15540///
15541/// @param called_from_public_decl true if this function was called
15542/// from a context where either a public function or a public variable
15543/// is being built.
15544///
15545/// @param where_offset the offset of the DIE where we are "logically"
15546/// positionned at, in the DIE tree. This is useful when @p die is
15547/// e.g, DW_TAG_partial_unit that can be included in several places in
15548/// the DIE tree.
15549///
15550/// @return a pointer to the resulting @ref ptr_to_mbr_type.
15552build_ptr_to_mbr_type(reader& rdr,
15553 Dwarf_Die* die,
15554 bool called_from_public_decl,
15555 size_t where_offset)
15556{
15557 ptr_to_mbr_type_sptr result;
15558
15559 if (!die)
15560 return result;
15561
15562 unsigned tag = dwarf_tag(die);
15563 if (tag != DW_TAG_ptr_to_member_type)
15564 return result;
15565
15566 Dwarf_Die data_member_type_die, containing_type_die;
15567
15568 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
15569 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
15570 return result;
15571
15572 type_or_decl_base_sptr data_member_type =
15573 build_ir_node_from_die(rdr, &data_member_type_die,
15574 called_from_public_decl, where_offset);
15575 if (!data_member_type)
15576 return result;
15577
15578 type_or_decl_base_sptr containing_type =
15579 build_ir_node_from_die(rdr, &containing_type_die,
15580 called_from_public_decl, where_offset);
15581 if (!containing_type)
15582 return result;
15583
15585 (is_type(containing_type)))
15586 return result;
15587
15588 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15589 {
15590 result = is_ptr_to_mbr_type(t);
15591 ABG_ASSERT(result);
15592 return result;
15593 }
15594
15595 uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
15596
15597 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
15598 is_type(data_member_type),
15599 is_type(containing_type),
15600 size_in_bits,
15601 /*alignment=*/0,
15602 location()));
15603
15604 rdr.associate_die_to_type(die, result, where_offset);
15605 return result;
15606}
15607
15608/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
15609///
15610/// @param rdr the DWARF reader to consider.
15611///
15612/// @param die the DIE to read from.
15613///
15614/// @param is_method points to a class or union declaration iff we're
15615/// building the type for a method. This is the enclosing class or
15616/// union of the method.
15617///
15618/// @param where_offset the offset of the DIE where we are "logically"
15619/// positioned at, in the DIE tree. This is useful when @p die is
15620/// e.g, DW_TAG_partial_unit that can be included in several places in
15621/// the DIE tree.
15622///
15623/// @return a pointer to the resulting function_type_sptr.
15624static function_type_sptr
15625build_function_type(reader& rdr,
15626 Dwarf_Die* die,
15627 class_or_union_sptr is_method,
15628 size_t where_offset)
15629{
15630 function_type_sptr result;
15631
15632 if (!die)
15633 return result;
15634
15635 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
15636 || dwarf_tag(die) == DW_TAG_subprogram);
15637
15638 const die_source source = rdr.get_die_source(die);
15639
15640 {
15641 size_t off = dwarf_dieoffset(die);
15642 auto i = rdr.die_wip_function_types_map(source).find(off);
15643 if (i != rdr.die_wip_function_types_map(source).end())
15644 {
15645 function_type_sptr fn_type = is_function_type(i->second);
15646 ABG_ASSERT(fn_type);
15647 return fn_type;
15648 }
15649 }
15650
15651 decl_base_sptr type_decl;
15652
15653 translation_unit_sptr tu = rdr.cur_transl_unit();
15654 ABG_ASSERT(tu);
15655
15656 /// If, inside the current translation unit, we've already seen a
15657 /// function type with the same text representation, then reuse that
15658 /// one instead.
15659 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
15660 {
15661 result = is_function_type(t);
15662 ABG_ASSERT(result);
15663 rdr.associate_die_to_type(die, result, where_offset);
15664 return result;
15665 }
15666
15667 bool odr_is_relevant = rdr.odr_is_relevant(die);
15668 if (odr_is_relevant)
15669 {
15670 // So we can rely on the One Definition Rule to say that if
15671 // several different function types have the same name (or
15672 // rather, representation) across the entire binary, then they
15673 // ought to designate the same function type. So let's ensure
15674 // that if we've already seen a function type with the same
15675 // representation as the function type 'die', then it's the same
15676 // type as the one denoted by 'die'.
15677 if (function_type_sptr fn_type =
15678 is_function_type(rdr.lookup_type_artifact_from_die(die)))
15679 {
15680 rdr.associate_die_to_type(die, fn_type, where_offset);
15681 return fn_type;
15682 }
15683 }
15684
15685 // Let's look at the DIE to detect if it's the DIE for a method
15686 // (type). If it is, we can deduce the name of its enclosing class
15687 // and if it's a static or const.
15688 bool is_const = false;
15689 bool is_static = false;
15690 Dwarf_Die object_pointer_die;
15691 Dwarf_Die class_type_die;
15692 bool has_this_parm_die =
15693 die_function_type_is_method_type(rdr, die, where_offset,
15694 object_pointer_die,
15695 class_type_die,
15696 is_static);
15697 if (has_this_parm_die)
15698 {
15699 // The function (type) has a "this" parameter DIE. It means it's
15700 // a member function DIE.
15701 if (!is_static)
15702 if (die_object_pointer_is_for_const_method(&object_pointer_die))
15703 is_const = true;
15704
15705 if (!is_method)
15706 {
15707 // We were initially called as if the function represented
15708 // by DIE was *NOT* a member function. But now we know it's
15709 // a member function. Let's take that into account.
15710 class_or_union_sptr klass_type =
15711 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
15712 /*called_from_pub_decl=*/true,
15713 where_offset));
15714 if (!klass_type)
15715 {
15716 // We could not create the class type. For instance,
15717 // this can be due to the fact that the class is
15718 // suppressed. In those cases, we just bail out.
15719 return nullptr;
15720 }
15721 is_method = klass_type;
15722 }
15723 }
15724
15725 // Let's create the type early and record it as being for the DIE
15726 // 'die'. This way, when building the sub-type triggers the
15727 // creation of a type matching the same 'die', then we'll reuse this
15728 // one.
15729
15730 result.reset(is_method
15731 ? new method_type(is_method, is_const,
15732 tu->get_address_size(),
15733 /*alignment=*/0)
15734 : new function_type(rdr.env(), tu->get_address_size(),
15735 /*alignment=*/0));
15736 rdr.associate_die_to_type(die, result, where_offset);
15737 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
15738
15739 type_base_sptr return_type;
15740 Dwarf_Die ret_type_die;
15741 if (die_die_attribute(die, DW_AT_type, ret_type_die))
15742 return_type =
15743 is_type(build_ir_node_from_die(rdr, &ret_type_die,
15744 /*called_from_public_decl=*/true,
15745 where_offset));
15746 if (!return_type)
15747 return_type = is_type(build_ir_node_for_void_type(rdr));
15748 result->set_return_type(return_type);
15749
15750 Dwarf_Die child;
15751 function_decl::parameters function_parms;
15752
15753 if (dwarf_child(die, &child) == 0)
15754 do
15755 {
15756 int child_tag = dwarf_tag(&child);
15757 if (child_tag == DW_TAG_formal_parameter)
15758 {
15759 // This is a "normal" function parameter.
15760 string name, linkage_name;
15761 location loc;
15762 die_loc_and_name(rdr, &child, loc, name, linkage_name);
15764 // Sometimes, bogus compiler emit names that are
15765 // non-ascii garbage. Let's just ditch that for now.
15766 name.clear();
15767 bool is_artificial = die_is_artificial(&child);
15768 type_base_sptr parm_type;
15769 Dwarf_Die parm_type_die;
15770 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
15771 parm_type =
15772 is_type(build_ir_node_from_die(rdr, &parm_type_die,
15773 /*called_from_public_decl=*/true,
15774 where_offset));
15775 if (!parm_type)
15776 continue;
15777 if (is_method
15778 && is_const_qualified_type(parm_type)
15779 && function_parms.empty())
15780 // We are looking at the first (implicit) parameter of a
15781 // method. This is basically the "this pointer". For
15782 // concrete instances of abstract methods, GCC sometimes
15783 // represents that pointer as a const pointer, whereas
15784 // in the abstract interface representing that method
15785 // the this-pointer is represented as a non-qualified
15786 // pointer. Let's trim the const qualifier away. That
15787 // will minize the chance to have spurious
15788 // const-qualifier changes on implicit parameters when
15789 // comparing methods that otherwise have no meaningful
15790 // ABI changes.
15791 parm_type =
15793
15795 (new function_decl::parameter(parm_type, name, loc,
15796 /*variadic_marker=*/false,
15797 is_artificial));
15798 function_parms.push_back(p);
15799 }
15800 else if (child_tag == DW_TAG_unspecified_parameters)
15801 {
15802 // This is a variadic function parameter.
15803 bool is_artificial = die_is_artificial(&child);
15804
15805 type_base_sptr parm_type =
15806 is_type(build_ir_node_for_variadic_parameter_type(rdr));
15808 (new function_decl::parameter(parm_type,
15809 /*name=*/"",
15810 location(),
15811 /*variadic_marker=*/true,
15812 is_artificial));
15813 function_parms.push_back(p);
15814 // After a DW_TAG_unspecified_parameters tag, we shouldn't
15815 // keep reading for parameters. The
15816 // unspecified_parameters TAG should be the last parameter
15817 // that we record. For instance, if there are multiple
15818 // DW_TAG_unspecified_parameters DIEs then we should care
15819 // only for the first one.
15820 break;
15821 }
15822 }
15823 while (dwarf_siblingof(&child, &child) == 0);
15824
15825 result->set_parameters(function_parms);
15826
15827 tu->bind_function_type_life_time(result);
15828
15829 result->set_is_artificial(true);
15830
15831 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
15832
15833 {
15834 die_function_type_map_type::const_iterator i =
15835 rdr.die_wip_function_types_map(source).
15836 find(dwarf_dieoffset(die));
15837 if (i != rdr.die_wip_function_types_map(source).end())
15838 rdr.die_wip_function_types_map(source).erase(i);
15839 }
15840
15841 maybe_canonicalize_type(result, rdr);
15842 return result;
15843}
15844
15845/// Build a subrange type from a DW_TAG_subrange_type.
15846///
15847/// @param rdr the DWARF reader to consider.
15848///
15849/// @param die the DIE to read from.
15850///
15851/// @param where_offset the offset of the DIE where we are "logically"
15852/// positionned at in the DIE tree. This is useful when @p die is
15853/// e,g, DW_TAG_partial_unit that can be included in several places in
15854/// the DIE tree.
15855///
15856/// @param associate_die_to_type if this is true then the resulting
15857/// type is associated to the @p die, so that next time when the
15858/// system looks up the type associated to it, the current resulting
15859/// type is returned. If false, then no association is done and the
15860/// resulting type can be destroyed right after. This can be useful
15861/// when the sole purpose of building the @ref
15862/// array_type_def::subrange_type is to use some of its method like,
15863/// e.g, its name pretty printing methods.
15864///
15865/// @return the newly built instance of @ref
15866/// array_type_def::subrange_type, or nil if no type could be built.
15868build_subrange_type(reader& rdr,
15869 const Dwarf_Die* die,
15870 size_t where_offset,
15871 bool associate_type_to_die)
15872{
15874
15875 if (!die)
15876 return result;
15877
15878 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
15879 if (tag != DW_TAG_subrange_type)
15880 return result;
15881
15882 string name = die_name(die);
15883
15884 // load the underlying type.
15885 Dwarf_Die underlying_type_die;
15886 type_base_sptr underlying_type;
15887 /* Unless there is an underlying type which says differently. */
15888 bool is_signed = false;
15889 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
15890 underlying_type =
15891 is_type(build_ir_node_from_die(rdr,
15892 &underlying_type_die,
15893 /*called_from_public_decl=*/true,
15894 where_offset));
15895
15896 if (underlying_type)
15897 {
15898 uint64_t ate;
15899 if (die_unsigned_constant_attribute (&underlying_type_die,
15900 DW_AT_encoding,
15901 ate))
15902 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
15903 }
15904
15905 // The DW_TAG_subrange_type DIE may have some size related
15906 // attributes (DW_AT_byte_size or DW_AT_bit_size). If not, then the
15907 // size is deduced from the size of its underlying type.
15908 bool has_size_info = false;
15909 uint64_t size = 0;
15910 if ((has_size_info = die_unsigned_constant_attribute(die,
15911 DW_AT_byte_size, size)))
15912 size *= 8;
15913 else
15914 has_size_info = die_unsigned_constant_attribute(die,
15915 DW_AT_bit_size, size);
15916
15917 translation_unit::language language = rdr.cur_transl_unit()->get_language();
15918 array_type_def::subrange_type::bound_value lower_bound =
15919 get_default_array_lower_bound(language);
15920 array_type_def::subrange_type::bound_value upper_bound;
15921 uint64_t count = 0;
15922 bool is_non_finite = false;
15923 bool non_zero_count_present = false;
15924
15925 // The DWARF 4 specifications says, in [5.11 Subrange
15926 // Type Entries]:
15927 //
15928 // The subrange entry may have the attributes
15929 // DW_AT_lower_bound and DW_AT_upper_bound to
15930 // specify, respectively, the lower and upper bound
15931 // values of the subrange.
15932 //
15933 // So let's look for DW_AT_lower_bound first.
15934 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
15935
15936 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
15937 is_signed, upper_bound);
15938 if (!found_upper_bound)
15939 found_upper_bound = subrange_die_indirect_bound_value(die,
15940 DW_AT_upper_bound,
15941 upper_bound,
15942 is_signed);
15943 // Then, DW_AT_upper_bound.
15944 if (!found_upper_bound)
15945 {
15946 // The DWARF 4 spec says, in [5.11 Subrange Type
15947 // Entries]:
15948 //
15949 // The DW_AT_upper_bound attribute may be replaced
15950 // by a DW_AT_count attribute, whose value
15951 // describes the number of elements in the
15952 // subrange rather than the value of the last
15953 // element."
15954 //
15955 // So, as DW_AT_upper_bound is not present in this
15956 // case, let's see if there is a DW_AT_count.
15957 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
15958 {
15959 if (count)
15960 // DW_AT_count can be present and be set to zero. This is
15961 // for instance the case to model this gcc extension to
15962 // represent flexible arrays:
15963 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
15964 // For instance: int flex_array[0];
15965 non_zero_count_present = true;
15966
15967 // When the count is present and non-zero, we can deduce the
15968 // upper_bound from the lower_bound and the number of
15969 // elements of the array:
15970 int64_t u = lower_bound.get_signed_value() + count;
15971 if (u)
15972 upper_bound = u - 1;
15973 }
15974
15975 if (!non_zero_count_present)
15976 // No upper_bound nor count was present on the DIE, this means
15977 // the array is considered to have an infinite (or rather not
15978 // known) size.
15979 is_non_finite = true;
15980 }
15981
15982 if (UINT64_MAX == upper_bound.get_unsigned_value())
15983 // If the upper_bound size is the max of the integer value
15984 // then it most certainly means unknown size.
15985 is_non_finite = true;
15986
15987 result.reset
15988 (new array_type_def::subrange_type(rdr.env(),
15989 name,
15990 lower_bound,
15991 upper_bound,
15992 underlying_type,
15993 location()));
15994 result->is_non_finite(is_non_finite);
15995
15996 if (has_size_info)
15997 result->set_size_in_bits(size);
15998 else
15999 {
16000 // The DW_TAG_subrange_type doesn't appear to have any size
16001 // attribute. In that case, the size is deduced from the size
16002 // of the underlying type. If there is no underlying type
16003 // specified, then the size of the subrange type is the size
16004 if (!underlying_type)
16005 result->set_size_in_bits(rdr.cur_transl_unit()->get_address_size());
16006 }
16007
16008 // Let's ensure the resulting subrange looks metabolically healthy.
16009 ABG_ASSERT(result->is_non_finite()
16010 || (result->get_length() ==
16011 (uint64_t) (result->get_upper_bound()
16012 - result->get_lower_bound() + 1)));
16013
16014 if (associate_type_to_die)
16015 rdr.associate_die_to_type(die, result, where_offset);
16016
16017 return result;
16018}
16019
16020/// Build the sub-ranges of an array type.
16021///
16022/// This is a sub-routine of build_array_type().
16023///
16024/// @param rdr the context to read from.
16025///
16026/// @param die the DIE of tag DW_TAG_array_type which contains
16027/// children DIEs that represent the sub-ranges.
16028///
16029/// @param subranges out parameter. This is set to the sub-ranges
16030/// that are built from @p die.
16031///
16032/// @param where_offset the offset of the DIE where we are "logically"
16033/// positioned at, in the DIE tree. This is useful when @p die is
16034/// e.g, DW_TAG_partial_unit that can be included in several places in
16035/// the DIE tree.
16036static void
16037build_subranges_from_array_type_die(const reader& rdr,
16038 const Dwarf_Die* die,
16040 size_t where_offset,
16041 bool associate_type_to_die)
16042{
16043 Dwarf_Die child;
16044
16045 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
16046 {
16047 do
16048 {
16049 int child_tag = dwarf_tag(&child);
16050 if (child_tag == DW_TAG_subrange_type)
16051 {
16053 if (associate_type_to_die)
16054 {
16055 // We are being called to create the type, add it to
16056 // the current type graph and associate it to the
16057 // DIE it's been created from.
16059 build_ir_node_from_die(const_cast<reader&>(rdr), &child,
16060 /*called_from_public_decl=*/true,
16061 where_offset);
16062 s = is_subrange_type(t);
16063 }
16064 else
16065 // We are being called to create the type but *NOT*
16066 // add it to the current tyupe tree, *NOR* associate
16067 // it to the DIE it's been created from.
16068 s = build_subrange_type(const_cast<reader&>(rdr), &child,
16069 where_offset,
16070 /*associate_type_to_die=*/false);
16071 if (s)
16072 subranges.push_back(s);
16073 }
16074 }
16075 while (dwarf_siblingof(&child, &child) == 0);
16076 }
16077}
16078
16079/// Build an array type from a DW_TAG_array_type DIE.
16080///
16081/// @param rdr the DWARF reader to consider.
16082///
16083/// @param die the DIE to read from.
16084///
16085/// @param called_from_public_decl true if this function was called
16086/// from a context where either a public function or a public variable
16087/// is being built.
16088///
16089/// @param where_offset the offset of the DIE where we are "logically"
16090/// positioned at, in the DIE tree. This is useful when @p die is
16091/// e.g, DW_TAG_partial_unit that can be included in several places in
16092/// the DIE tree.
16093///
16094/// @return a pointer to the resulting array_type_def.
16096build_array_type(reader& rdr,
16097 Dwarf_Die* die,
16098 bool called_from_public_decl,
16099 size_t where_offset)
16100{
16101 array_type_def_sptr result;
16102
16103 if (!die)
16104 return result;
16105
16106 unsigned tag = dwarf_tag(die);
16107 if (tag != DW_TAG_array_type)
16108 return result;
16109
16110 decl_base_sptr type_decl;
16111 Dwarf_Die type_die;
16112
16113 if (die_die_attribute(die, DW_AT_type, type_die))
16114 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
16115 called_from_public_decl,
16116 where_offset));
16117 if (!type_decl)
16118 return result;
16119
16120 // The call to build_ir_node_from_die() could have triggered the
16121 // creation of the type for this DIE. In that case, just return it.
16122 if (type_base_sptr t = rdr.lookup_type_from_die(die))
16123 {
16124 result = is_array_type(t);
16125 ABG_ASSERT(result);
16126 return result;
16127 }
16128
16129 type_base_sptr type = is_type(type_decl);
16130 ABG_ASSERT(type);
16131
16133
16134 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
16135
16136 result.reset(new array_type_def(type, subranges, location()));
16137 rdr.associate_die_to_type(die, result, where_offset);
16138 return result;
16139}
16140
16141/// Create a typedef_decl from a DW_TAG_typedef DIE.
16142///
16143/// @param rdr the DWARF reader to consider.
16144///
16145/// @param die the DIE to read from.
16146///
16147/// @param called_from_public_decl true if this function was called
16148/// from a context where either a public function or a public variable
16149/// is being built.
16150///
16151/// @param where_offset the offset of the DIE where we are "logically"
16152/// positionned at, in the DIE tree. This is useful when @p die is
16153/// e.g, DW_TAG_partial_unit that can be included in several places in
16154/// the DIE tree.
16155///
16156/// @return the newly created typedef_decl.
16157static typedef_decl_sptr
16158build_typedef_type(reader& rdr,
16159 Dwarf_Die* die,
16160 bool called_from_public_decl,
16161 size_t where_offset)
16162{
16163 typedef_decl_sptr result;
16164
16165 if (!die)
16166 return result;
16167
16168 unsigned tag = dwarf_tag(die);
16169 if (tag != DW_TAG_typedef)
16170 return result;
16171
16172 string name, linkage_name;
16173 location loc;
16174 die_loc_and_name(rdr, die, loc, name, linkage_name);
16175
16176 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
16177 if (loc)
16178 result = lookup_typedef_type_per_location(loc.expand(), *corp);
16179
16180 if (!result)
16181 {
16182 type_base_sptr utype;
16183 Dwarf_Die underlying_type_die;
16184 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
16185 // A typedef DIE with no underlying type means a typedef to
16186 // void type.
16187 utype = rdr.env().get_void_type();
16188
16189 if (!utype)
16190 utype =
16191 is_type(build_ir_node_from_die(rdr,
16192 &underlying_type_die,
16193 called_from_public_decl,
16194 where_offset));
16195 if (!utype)
16196 return result;
16197
16198 ABG_ASSERT(utype);
16199 result.reset(new typedef_decl(name, utype, loc, linkage_name));
16200
16201 if ((is_class_or_union_type(utype) || is_enum_type(utype))
16202 && is_anonymous_type(utype))
16203 {
16204 // This is a naming typedef for an enum or a class. Let's
16205 // mark the underlying decl as such.
16206 decl_base_sptr decl = is_decl(utype);
16207 ABG_ASSERT(decl);
16208 decl->set_naming_typedef(result);
16209 rdr.maybe_schedule_decl_only_type_for_resolution(utype);
16210 }
16211 }
16212
16213 rdr.associate_die_to_type(die, result, where_offset);
16214
16215 return result;
16216}
16217
16218/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
16219/// denoted by the DIE is not suppressed by a suppression
16220/// specification associated to the current DWARF reader.
16221///
16222/// Note that if a member variable declaration with the same name as
16223/// the name of the DIE we are looking at exists, this function returns
16224/// that existing variable declaration.
16225///
16226/// @param rdr the DWARF reader to use.
16227///
16228/// @param die the DIE representing the variable we are looking at.
16229///
16230/// @param where_offset the offset of the DIE where we are "logically"
16231/// positionned at, in the DIE tree. This is useful when @p die is
16232/// e.g, DW_TAG_partial_unit that can be included in several places in
16233/// the DIE tree.
16234///
16235/// @param is_declaration_only if true, it means the variable DIE has
16236/// the is_declaration_only only attribute.
16237///
16238/// @param result if this is set to an existing var_decl, this means
16239/// that the function will append the new properties it sees on @p die
16240/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16241/// new var_decl is going to be allocated and returned.
16242///
16243/// @param is_required_decl_spec this is true iff the variable to
16244/// build is referred to as being the specification of another
16245/// variable.
16246///
16247/// @return a pointer to the newly created var_decl. If the var_decl
16248/// could not be built, this function returns NULL.
16249static var_decl_sptr
16250build_or_get_var_decl_if_not_suppressed(reader& rdr,
16251 scope_decl *scope,
16252 Dwarf_Die *die,
16253 size_t where_offset,
16254 bool is_declaration_only,
16255 var_decl_sptr result,
16256 bool is_required_decl_spec)
16257{
16258 var_decl_sptr var;
16259 if (variable_is_suppressed(rdr, scope, die,
16260 is_declaration_only,
16261 is_required_decl_spec))
16262 {
16263 ++rdr.stats_.number_of_suppressed_variables;
16264 return var;
16265 }
16266
16267 if (class_decl* class_type = is_class_type(scope))
16268 {
16269 string var_name = die_name(die);
16270 if (!var_name.empty())
16271 if ((var = class_type->find_data_member(var_name)))
16272 return var;
16273 }
16274
16275 // The variable was not suppressed.
16276 ++rdr.stats_.number_of_suppressed_variables;
16277
16278 var = build_var_decl(rdr, die, where_offset, result);
16279 return var;
16280}
16281
16282/// Build a @ref var_decl out of a DW_TAG_variable DIE.
16283///
16284/// @param rdr the DWARF reader to use.
16285///
16286/// @param die the DIE representing the variable 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 result if this is set to an existing var_decl, this means
16294/// that the function will append the new properties it sees on @p die
16295/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16296/// new var_decl is going to be allocated and returned.
16297///
16298/// @return a pointer to the newly created var_decl. If the var_decl
16299/// could not be built, this function returns NULL.
16300static var_decl_sptr
16301build_var_decl(reader& rdr,
16302 Dwarf_Die *die,
16303 size_t where_offset,
16304 var_decl_sptr result)
16305{
16306 if (!die)
16307 return result;
16308
16309 int tag = dwarf_tag(die);
16310 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
16311
16312 if (!die_is_public_decl(die))
16313 return result;
16314
16315 type_base_sptr type;
16316 Dwarf_Die type_die;
16317 if (die_die_attribute(die, DW_AT_type, type_die))
16318 {
16319 decl_base_sptr ty =
16320 is_decl(build_ir_node_from_die(rdr, &type_die,
16321 /*called_from_public_decl=*/true,
16322 where_offset));
16323 if (!ty)
16324 return result;
16325 type = is_type(ty);
16326 ABG_ASSERT(type);
16327 }
16328
16329 if (!type && !result)
16330 return result;
16331
16332 string name, linkage_name;
16333 location loc;
16334 die_loc_and_name(rdr, die, loc, name, linkage_name);
16335
16336 if (!result)
16337 result.reset(new var_decl(name, type, loc, linkage_name));
16338 else
16339 {
16340 // We were called to append properties that might have been
16341 // missing from the first version of the variable. And usually
16342 // that missing property is the mangled name or the type.
16343 if (!linkage_name.empty())
16344 result->set_linkage_name(linkage_name);
16345
16346 if (type)
16347 result->set_type(type);
16348 }
16349
16350 // Check if a variable symbol with this name is exported by the elf
16351 // binary. If it is, then set the symbol of the variable, if it's
16352 // not set already.
16353 if (!result->get_symbol())
16354 {
16355 elf_symbol_sptr var_sym;
16356 Dwarf_Addr var_addr;
16357
16358 if (rdr.get_variable_address(die, var_addr))
16359 {
16360 rdr.symtab()->
16361 update_main_symbol(var_addr,
16362 result->get_linkage_name().empty()
16363 ? result->get_name()
16364 : result->get_linkage_name());
16365 var_sym = rdr.variable_symbol_is_exported(var_addr);
16366 }
16367
16368 if (var_sym)
16369 {
16370 result->set_symbol(var_sym);
16371 // If the linkage name is not set or is wrong, set it to
16372 // the name of the underlying symbol.
16373 string linkage_name = result->get_linkage_name();
16374 if (linkage_name.empty()
16375 || !var_sym->get_alias_from_name(linkage_name))
16376 result->set_linkage_name(var_sym->get_name());
16377 result->set_is_in_public_symbol_table(true);
16378 }
16379
16380 if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
16381 {
16382 // We are looking at a global variable which symbol is
16383 // undefined. Let's set its symbol.
16384 string n = result->get_linkage_name();
16385 if (n.empty())
16386 n = result->get_name();
16387 var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
16388 if (var_sym)
16389 {
16390 result->set_symbol(var_sym);
16391 result->set_is_in_public_symbol_table(false);
16392 }
16393 }
16394 }
16395
16396 return result;
16397}
16398
16399/// Test if a given function denoted by its DIE and its scope is
16400/// suppressed by any of the suppression specifications associated to
16401/// a given context of ELF/DWARF reading.
16402///
16403/// Note that a non-member function which symbol is not exported is
16404/// also suppressed.
16405///
16406/// @param rdr the ELF/DWARF reading content of interest.
16407///
16408/// @param scope of the scope of the function.
16409///
16410/// @param function_die the DIE representing the function.
16411///
16412/// @param is_declaration_only is true if the DIE denoted by @p die is
16413/// a declaration-only DIE.
16414///
16415/// @return true iff @p function_die is suppressed by at least one
16416/// suppression specification attached to the @p rdr.
16417static bool
16418function_is_suppressed(const reader& rdr,
16419 const scope_decl* scope,
16420 Dwarf_Die *function_die,
16421 bool is_declaration_only)
16422{
16423 if (function_die == 0
16424 || dwarf_tag(function_die) != DW_TAG_subprogram)
16425 return false;
16426
16427 string fname = die_string_attribute(function_die, DW_AT_name);
16428 string flinkage_name = die_linkage_name(function_die);
16429 if (flinkage_name.empty() && die_is_in_c(function_die))
16430 flinkage_name = fname;
16431 string qualified_name = build_qualified_name(scope, fname);
16432
16433 // A non-member non-static function which symbol is not exported is
16434 // suppressed.
16435 //
16436 // Note that if the non-member non-static function has an undefined
16437 // symbol, by default, it's not suppressed. Unless we are asked to
16438 // drop undefined symbols too.
16439 if (!is_class_type(scope)
16440 && (!is_declaration_only || rdr.drop_undefined_syms()))
16441 {
16442 Dwarf_Addr fn_addr;
16443 if (!rdr.get_function_address(function_die, fn_addr))
16444 return true;
16445
16446 elf_symbol_sptr symbol =
16447 rdr.function_symbol_is_exported(fn_addr);
16448 if (!symbol)
16449 return true;
16450 if (symbol->is_suppressed())
16451 return true;
16452
16453 // Since there is only one symbol in DWARF associated with an elf_symbol,
16454 // we can assume this is the main symbol then. Otherwise the main hinting
16455 // did not work as expected.
16456 ABG_ASSERT(symbol->is_main_symbol());
16457 if (symbol->has_aliases())
16458 for (elf_symbol_sptr a = symbol->get_next_alias();
16459 !a->is_main_symbol(); a = a->get_next_alias())
16460 if (a->is_suppressed())
16461 return true;
16462 }
16463
16464 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
16465 /*require_drop_property=*/true);
16466}
16467
16468/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
16469/// function denoted by the DIE is not suppressed by a suppression
16470/// specification associated to the current DWARF reader.
16471///
16472/// Note that if a member function declaration with the same signature
16473/// (pretty representation) as one of the DIE we are looking at
16474/// exists, this function returns that existing function declaration.
16475/// Similarly, if there is already a constructed member function with
16476/// the same linkage name as the one on the DIE, this function returns
16477/// that member function.
16478///
16479/// Also note that the function_decl IR returned by this function must
16480/// be passed to finish_member_function_reading because several
16481/// properties from the DIE are actually read by that function, and
16482/// the corresponding properties on the function_decl IR are updated
16483/// accordingly. This is done to support "updating" a function_decl
16484/// IR with properties scathered across several DIEs.
16485///
16486/// @param rdr the DWARF reader to use.
16487///
16488/// @param scope the scope of the function we are looking at.
16489///
16490/// @param fn_die the DIE representing the function we are looking at.
16491///
16492/// @param where_offset the offset of the DIE where we are "logically"
16493/// positionned at, in the DIE tree. This is useful when @p die is
16494/// e.g, DW_TAG_partial_unit that can be included in several places in
16495/// the DIE tree.
16496///
16497/// @param is_declaration_only is true if the DIE denoted by @p fn_die
16498/// is a declaration-only DIE.
16499///
16500/// @param result if this is set to an existing function_decl, this
16501/// means that the function will append the new properties it sees on
16502/// @p fn_die to that exising function_decl. Otherwise, if this
16503/// parameter is NULL, a new function_decl is going to be allocated
16504/// and returned.
16505///
16506/// @return a pointer to the newly created var_decl. If the var_decl
16507/// could not be built, this function returns NULL.
16508static function_decl_sptr
16509build_or_get_fn_decl_if_not_suppressed(reader& rdr,
16510 scope_decl *scope,
16511 Dwarf_Die *fn_die,
16512 size_t where_offset,
16513 bool is_declaration_only,
16514 function_decl_sptr result)
16515{
16517 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
16518 {
16519 ++rdr.stats_.number_of_suppressed_functions;
16520 return fn;
16521 }
16522
16523 string name = die_name(fn_die);
16524 string linkage_name = die_linkage_name(fn_die);
16525 bool is_dtor = !name.empty() && name[0]== '~';
16526 bool is_virtual = false;
16527 if (is_dtor)
16528 {
16529 Dwarf_Attribute attr;
16530 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
16531 DW_AT_vtable_elem_location,
16532 &attr))
16533 is_virtual = true;
16534 }
16535
16536
16537 // If we've already built an IR for a function with the same
16538 // signature (from another DIE), reuse it, unless that function is a
16539 // virtual C++ destructor. Several virtual C++ destructors with the
16540 // same signature can be implemented by several different ELF
16541 // symbols. So re-using C++ destructors like that can lead to us
16542 // missing some destructors.
16543 if (!result && (!(is_dtor && is_virtual)))
16544 {
16545 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
16546 {
16547 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
16548 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
16549 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
16550 return fn;
16551 }
16552 }
16553
16554 // The function was not suppressed.
16555 ++rdr.stats_.number_of_allowed_functions;
16556
16557 // If a member function with the same linkage name as the one
16558 // carried by the DIE already exists, then return it.
16559 if (class_decl* klass = is_class_type(scope))
16560 {
16561 string linkage_name = die_linkage_name(fn_die);
16562 fn = klass->find_member_function_sptr(linkage_name);
16563 if (fn)
16564 // We found a member function that has the same signature.
16565 // Let's mark it for update.
16566 result = fn;
16567 }
16568
16569 if (!fn || !fn->get_symbol())
16570 // We haven't yet been able to construct a function IR, or, we
16571 // have one 'partial' function IR that doesn't have any associated
16572 // symbol yet. Note that in the later case, a function IR without
16573 // any associated symbol will be dropped on the floor by
16574 // potential_member_fn_should_be_dropped. So let's build or a new
16575 // function IR or complete the existing partial IR.
16576 fn = build_function_decl(rdr, fn_die, where_offset, result);
16577
16578 return fn;
16579}
16580
16581/// Test if a given variable denoted by its DIE and its scope is
16582/// suppressed by any of the suppression specifications associated to
16583/// a given context of ELF/DWARF reading.
16584///
16585/// @param rdr the ELF/DWARF reading content of interest.
16586///
16587/// @param scope of the scope of the variable.
16588///
16589/// @param variable_die the DIE representing the variable.
16590///
16591/// @param is_declaration_only true if the variable is supposed to be
16592/// decl-only.
16593///
16594/// @param is_required_decl_spec if true, means that the @p
16595/// variable_die being considered is for a variable decl that is a
16596/// specification for a concrete variable being built.
16597///
16598/// @return true iff @p variable_die is suppressed by at least one
16599/// suppression specification attached to the @p rdr.
16600static bool
16601variable_is_suppressed(const reader& rdr,
16602 const scope_decl* scope,
16603 Dwarf_Die *variable_die,
16604 bool is_declaration_only,
16605 bool is_required_decl_spec)
16606{
16607 if (variable_die == 0
16608 || (dwarf_tag(variable_die) != DW_TAG_variable
16609 && dwarf_tag(variable_die) != DW_TAG_member))
16610 return false;
16611
16612 string name = die_string_attribute(variable_die, DW_AT_name);
16613 string linkage_name = die_linkage_name(variable_die);
16614 if (linkage_name.empty() && die_is_in_c(variable_die))
16615 linkage_name = name;
16616 string qualified_name = build_qualified_name(scope, name);
16617
16618 // If a non member variable that is a declaration (has no defined
16619 // and exported symbol) and is not the specification of another
16620 // concrete variable, then it's suppressed. This is a size
16621 // optimization; it removes useless declaration-only variables from
16622 // the IR.
16623 if (!is_class_type(scope)
16624 && !is_required_decl_spec
16625 // If we are asked to load undefined interfaces, then we don't
16626 // suppress declaration-only variables as they might have
16627 // undefined elf-symbols.
16628 && (!is_declaration_only || !rdr.load_undefined_interfaces()))
16629 {
16630 Dwarf_Addr var_addr = 0;
16631 if (!rdr.get_variable_address(variable_die, var_addr))
16632 return true;
16633
16634 elf_symbol_sptr symbol =
16635 rdr.variable_symbol_is_exported(var_addr);
16636 if (!symbol)
16637 return true;
16638 if (symbol->is_suppressed())
16639 return true;
16640
16641 // Since there is only one symbol in DWARF associated with an elf_symbol,
16642 // we can assume this is the main symbol then. Otherwise the main hinting
16643 // did not work as expected.
16644 ABG_ASSERT(symbol->is_main_symbol());
16645 if (symbol->has_aliases())
16646 for (elf_symbol_sptr a = symbol->get_next_alias();
16647 !a->is_main_symbol(); a = a->get_next_alias())
16648 if (a->is_suppressed())
16649 return true;
16650 }
16651
16653 qualified_name,
16654 linkage_name,
16655 /*require_drop_property=*/true);
16656}
16657
16658/// Test if a type (designated by a given DIE) in a given scope is
16659/// suppressed by the suppression specifications that are associated
16660/// to a given DWARF reader.
16661///
16662/// @param rdr the DWARF reader to consider.
16663///
16664/// @param scope of the scope of the type DIE to consider.
16665///
16666/// @param type_die the DIE that designates the type to consider.
16667///
16668/// @param type_is_opaque out parameter. If this function returns
16669/// true (the type @p type_die is suppressed) and if the type was
16670/// suppressed because it's opaque then this parameter is set to
16671/// true.
16672///
16673/// @return true iff the type designated by the DIE @p type_die, in
16674/// the scope @p scope is suppressed by at the suppression
16675/// specifications associated to the current DWARF reader.
16676static bool
16677type_is_suppressed(const reader& rdr,
16678 const scope_decl* scope,
16679 Dwarf_Die *type_die,
16680 bool &type_is_opaque)
16681{
16682 if (type_die == 0
16683 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
16684 && dwarf_tag(type_die) != DW_TAG_class_type
16685 && dwarf_tag(type_die) != DW_TAG_structure_type
16686 && dwarf_tag(type_die) != DW_TAG_union_type))
16687 return false;
16688
16689 string type_name, linkage_name;
16690 location type_location;
16691 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16692 string qualified_name = build_qualified_name(scope, type_name);
16693
16694 return suppr::is_type_suppressed(rdr,
16695 qualified_name,
16696 type_location,
16697 type_is_opaque,
16698 /*require_drop_property=*/true);
16699}
16700
16701/// Test if a type (designated by a given DIE) in a given scope is
16702/// suppressed by the suppression specifications that are associated
16703/// to a given DWARF reader.
16704///
16705/// @param rdr the DWARF reader to consider.
16706///
16707/// @param scope of the scope of the type DIE to consider.
16708///
16709/// @param type_die the DIE that designates the type to consider.
16710///
16711/// @return true iff the type designated by the DIE @p type_die, in
16712/// the scope @p scope is suppressed by at the suppression
16713/// specifications associated to the current DWARF reader.
16714static bool
16715type_is_suppressed(const reader& rdr,
16716 const scope_decl* scope,
16717 Dwarf_Die *type_die)
16718{
16719 bool type_is_opaque = false;
16720 return type_is_suppressed(rdr, scope, type_die, type_is_opaque);
16721}
16722
16723/// Get the opaque version of a type that was suppressed because it's
16724/// a private type.
16725///
16726/// The opaque version version of the type is just a declared-only
16727/// version of the type (class, union or enum type) denoted by @p
16728/// type_die.
16729///
16730/// @param rdr the DWARF reader in use.
16731///
16732/// @param scope the scope of the type die we are looking at.
16733///
16734/// @param type_die the type DIE we are looking at.
16735///
16736/// @param where_offset the offset of the DIE where we are "logically"
16737/// positionned at, in the DIE tree. This is useful when @p die is
16738/// e.g, DW_TAG_partial_unit that can be included in several places in
16739/// the DIE tree.
16740///
16741/// @return the opaque version of the type denoted by @p type_die or
16742/// nil if no opaque version was found.
16744get_opaque_version_of_type(reader &rdr,
16745 scope_decl *scope,
16746 Dwarf_Die *type_die,
16747 size_t where_offset)
16748{
16750
16751 if (type_die == 0)
16752 return result;
16753
16754 unsigned tag = dwarf_tag(type_die);
16755 if (tag != DW_TAG_class_type
16756 && tag != DW_TAG_structure_type
16757 && tag != DW_TAG_union_type
16758 && tag != DW_TAG_enumeration_type)
16759 return result;
16760
16761 string type_name, linkage_name;
16762 location type_location;
16763 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16764
16765 string qualified_name = build_qualified_name(scope, type_name);
16766
16767 //
16768 // TODO: also handle declaration-only unions. To do that, we mostly
16769 // need to adapt add_or_update_union_type to make it schedule
16770 // declaration-only unions for resolution too.
16771 //
16772 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
16773 {
16774 string_classes_or_unions_map::const_iterator i =
16775 rdr.declaration_only_classes().find(qualified_name);
16776 if (i != rdr.declaration_only_classes().end())
16777 result = i->second.back();
16778
16779 if (!result)
16780 {
16781 // So we didn't find any pre-existing forward-declared-only
16782 // class for the class definition that we could return as an
16783 // opaque type. So let's build one.
16784 //
16785 // TODO: we need to be able to do this for unions too!
16786 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
16787 /*alignment=*/0, /*size=*/0,
16788 tag == DW_TAG_structure_type,
16789 type_location,
16790 decl_base::VISIBILITY_DEFAULT));
16791 klass->set_is_declaration_only(true);
16792 klass->set_is_artificial(die_is_artificial(type_die));
16793 add_decl_to_scope(klass, scope);
16794 rdr.associate_die_to_type(type_die, klass, where_offset);
16795 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
16796 result = klass;
16797 }
16798 }
16799
16800 if (tag == DW_TAG_enumeration_type)
16801 {
16802 string_enums_map::const_iterator i =
16803 rdr.declaration_only_enums().find(qualified_name);
16804 if (i != rdr.declaration_only_enums().end())
16805 result = i->second.back();
16806
16807 if (!result)
16808 {
16809 uint64_t size = 0;
16810 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
16811 size *= 8;
16812 type_decl_sptr underlying_type =
16813 build_enum_underlying_type(rdr, type_name, size,
16814 /*anonymous=*/true);
16815 enum_type_decl::enumerators enumeratorz;
16816 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
16817 type_location,
16818 underlying_type,
16819 enumeratorz,
16820 linkage_name));
16821 enum_type->set_is_artificial(die_is_artificial(type_die));
16822 add_decl_to_scope(enum_type, scope);
16823 result = enum_type;
16824 }
16825 }
16826
16827 return result;
16828}
16829
16830/// Create a function symbol with a given name.
16831///
16832/// @param sym_name the name of the symbol to create.
16833///
16834/// @param env the environment to create the symbol in.
16835///
16836/// @return the newly created symbol.
16838create_default_fn_sym(const string& sym_name, const environment& env)
16839{
16841 elf_symbol_sptr result =
16843 /*symbol index=*/ 0,
16844 /*symbol size=*/ 0,
16845 sym_name,
16846 /*symbol type=*/ elf_symbol::FUNC_TYPE,
16847 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
16848 /*symbol is defined=*/ true,
16849 /*symbol is common=*/ false,
16850 /*symbol version=*/ ver,
16851 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
16852 return result;
16853}
16854
16855/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
16856///
16857/// @param rdr the DWARF reader to use
16858///
16859/// @param die the DW_TAG_subprogram DIE to read from.
16860///
16861/// @param where_offset the offset of the DIE where we are "logically"
16862/// positionned at, in the DIE tree. This is useful when @p die is
16863/// e.g, DW_TAG_partial_unit that can be included in several places in
16864/// the DIE tree.
16865///
16866/// @param called_for_public_decl this is set to true if the function
16867/// was called for a public (function) decl.
16868static function_decl_sptr
16869build_function_decl(reader& rdr,
16870 Dwarf_Die* die,
16871 size_t where_offset,
16873{
16874 function_decl_sptr result = fn;
16875 if (!die)
16876 return result;
16877 int tag = dwarf_tag(die);
16878 ABG_ASSERT(tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine);
16879
16880 if (!die_is_public_decl(die))
16881 return result;
16882
16883 translation_unit_sptr tu = rdr.cur_transl_unit();
16884 ABG_ASSERT(tu);
16885
16886 string fname, flinkage_name;
16887 location floc;
16888 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
16889 cleanup_decl_name(fname);
16890
16891 size_t is_inline = die_is_declared_inline(die);
16892 class_or_union_sptr is_method =
16893 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
16894
16895 if (result)
16896 {
16897 // Add the properties that might have been missing from the
16898 // first declaration of the function. For now, it usually is
16899 // the mangled name that goes missing in the first declarations.
16900 //
16901 // Also note that if 'fn' has just been cloned, the current
16902 // linkage name (of the current DIE) might be different from the
16903 // linkage name of 'fn'. In that case, update the linkage name
16904 // of 'fn' too.
16905 if (!flinkage_name.empty()
16906 && result->get_linkage_name() != flinkage_name)
16907 result->set_linkage_name(flinkage_name);
16908 if (floc)
16909 if (!result->get_location())
16910 result->set_location(floc);
16911 result->is_declared_inline(is_inline);
16912 }
16913 else
16914 {
16915 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
16916 where_offset));
16917 if (!fn_type)
16918 return result;
16919
16920 maybe_canonicalize_type(fn_type, rdr);
16921
16922 result.reset(is_method
16923 ? new method_decl(fname, fn_type,
16924 is_inline, floc,
16925 flinkage_name)
16926 : new function_decl(fname, fn_type,
16927 is_inline, floc,
16928 flinkage_name));
16929 }
16930
16931 // Set the symbol of the function. If the linkage name is not set
16932 // or is wrong, set it to the name of the underlying symbol.
16933 if (!result->get_symbol())
16934 {
16935 elf_symbol_sptr fn_sym;
16936 Dwarf_Addr fn_addr;
16937 if (rdr.get_function_address(die, fn_addr))
16938 {
16939 rdr.symtab()->
16940 update_main_symbol(fn_addr,
16941 result->get_linkage_name().empty()
16942 ? result->get_name()
16943 : result->get_linkage_name());
16944 fn_sym = rdr.function_symbol_is_exported(fn_addr);
16945 }
16946
16947 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
16948 {
16949 result->set_symbol(fn_sym);
16950 string linkage_name = result->get_linkage_name();
16951 if (linkage_name.empty())
16952 result->set_linkage_name(fn_sym->get_name());
16953 result->set_is_in_public_symbol_table(true);
16954 }
16955
16956 if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
16957 {
16958 // We are looking at a function which symbol is undefined.
16959 // let's set its symbol.
16960 string n = result->get_linkage_name();
16961 if (n.empty())
16962 n = result->get_name();
16963 fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
16964 if (fn_sym)
16965 {
16966 result->set_symbol(fn_sym);
16967 result->set_is_in_public_symbol_table(false);
16968 }
16969 }
16970 }
16971
16972 rdr.associate_die_to_type(die, result->get_type(), where_offset);
16973
16974 size_t die_offset = dwarf_dieoffset(die);
16975
16976 if (fn
16977 && is_member_function(fn)
16979 && !result->get_linkage_name().empty())
16980 // This function is a virtual member function which has its
16981 // linkage name *and* and has its underlying symbol correctly set.
16982 // It thus doesn't need any fixup related to elf symbol. So
16983 // remove it from the set of virtual member functions with linkage
16984 // names and no elf symbol that need to be fixed up.
16985 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
16986 return result;
16987}
16988
16989/// Canonicalize a type if it's suitable for early canonicalizing, or,
16990/// if it's not, schedule it for late canonicalization, after the
16991/// debug info of the current translation unit has been fully read.
16992///
16993/// A (composite) type is deemed suitable for early canonicalizing iff
16994/// all of its sub-types are canonicalized themselve. Non composite
16995/// types are always deemed suitable for early canonicalization.
16996///
16997/// Note that this function knows how to deal with anonymous classes,
16998/// structs and enums, unlike the overload below:
16999///
17000/// @param t the type DIE to consider for canonicalization.
17001///
17002/// @param rdr the @ref reader to use.
17003static void
17004maybe_canonicalize_type(const type_base_sptr& t,
17005 reader& rdr)
17006{
17007 if (!t)
17008 return;
17009
17010 rdr.schedule_type_for_late_canonicalization(t);
17011}
17012
17013/// If a given decl is a member type declaration, set its access
17014/// specifier from the DIE that represents it.
17015///
17016/// @param member_type_declaration the member type declaration to
17017/// consider.
17018static void
17019maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
17020 Dwarf_Die* die)
17021{
17022 if (is_type(member_type_declaration)
17023 && is_member_decl(member_type_declaration))
17024 {
17025 class_or_union* scope =
17026 is_class_or_union_type(member_type_declaration->get_scope());
17027 ABG_ASSERT(scope);
17028
17029 access_specifier access = public_access;
17030 if (class_decl* cl = is_class_type(scope))
17031 if (!cl->is_struct())
17032 access = private_access;
17033
17034 die_access_specifier(die, access);
17035 set_member_access_specifier(member_type_declaration, access);
17036 }
17037}
17038
17039/// Normalize a decl name so that it can be compared to other decl
17040/// names without risking to have spurious changes.
17041///
17042/// The function removes white spaces from the and normalizes
17043/// numerical litterals.
17044///
17045/// @param str in/out parameter. The string to normalize, in place.
17046static void
17047cleanup_decl_name(string& str)
17048{
17051}
17052
17053/// This function tests if a given function which might be intented to
17054/// be added to a class scope (to become a member function) should be
17055/// dropped on the floor instead and not be added to the class.
17056///
17057/// This is a subroutine of build_ir_node_from_die.
17058///
17059/// @param fn the function to consider.
17060///
17061/// @param fn_die the DWARF die of @p fn.
17062///
17063/// @param scope the scope in which @p fn is to be added.
17064///
17065/// @return true iff @p fn should be dropped on the floor.
17066static bool
17067potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
17068 const Dwarf_Die *fn_die)
17069{
17070 if (!fn || fn->get_scope())
17071 return false;
17072
17073 if (// A function that is not virtual ...
17074 !die_is_virtual(fn_die)
17075 // .. and yet has no defined ELF symbol associated ...
17076 && !fn->get_symbol())
17077 // Should not be added to its class scope.
17078 //
17079 // Why would it? It's not part of the ABI anyway, as it doesn't
17080 // have any ELF symbol associated and is not a virtual member
17081 // function. It just constitutes bloat in the IR and might even
17082 // induce spurious change reports down the road.
17083 return true;
17084
17085 return false;
17086}
17087
17088/// Build an IR node from a given DIE and add the node to the current
17089/// IR being build and held in the DWARF reader. Doing that is called
17090/// "emitting an IR node for the DIE".
17091///
17092/// @param rdr the DWARF reader.
17093///
17094/// @param die the DIE to consider.
17095///
17096/// @param scope the scope under which the resulting IR node has to be
17097/// added.
17098///
17099/// @param called_from_public_decl set to yes if this function is
17100/// called from the functions used to build a public decl (functions
17101/// and variables). In that case, this function accepts building IR
17102/// nodes representing types. Otherwise, this function only creates
17103/// IR nodes representing public decls (functions and variables).
17104/// This is done to avoid emitting IR nodes for types that are not
17105/// referenced by public functions or variables.
17106///
17107/// @param where_offset the offset of the DIE where we are "logically"
17108/// positionned at, in the DIE tree. This is useful when @p die is
17109/// e.g, DW_TAG_partial_unit that can be included in several places in
17110/// the DIE tree.
17111///
17112/// @param is_required_decl_spec if true, it means the ir node to
17113/// build is for a decl that is a specification for another decl that
17114/// is concrete. If you don't know what this is, set it to false.
17115///
17116/// @param is_declaration_only is true if the DIE denoted by @p die is
17117/// a declaration-only DIE.
17118///
17119/// @return the resulting IR node.
17121build_ir_node_from_die(reader& rdr,
17122 Dwarf_Die* die,
17123 scope_decl* scope,
17124 bool called_from_public_decl,
17125 size_t where_offset,
17126 bool is_declaration_only,
17127 bool is_required_decl_spec)
17128{
17130
17131 if (!die || !scope)
17132 return result;
17133
17134 int tag = dwarf_tag(die);
17135
17136 if (!called_from_public_decl)
17137 {
17138 if (rdr.load_all_types() && die_is_type(die))
17139 /* We were instructed to load debug info for all types,
17140 included those that are not reachable from a public
17141 declaration. So load the debug info for this type. */;
17142 else if (tag != DW_TAG_subprogram
17143 && tag != DW_TAG_variable
17144 && tag != DW_TAG_member
17145 && tag != DW_TAG_namespace)
17146 return result;
17147 }
17148
17149 const die_source source_of_die = rdr.get_die_source(die);
17150
17151 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
17152 source_of_die)))
17153 {
17154 if (rdr.load_all_types())
17155 if (called_from_public_decl)
17156 if (type_base_sptr t = is_type(result))
17157 if (corpus *abi_corpus = rdr.corpus().get())
17158 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17159
17160 return result;
17161 }
17162
17163 // This is *the* bit of code that ensures we have the right notion
17164 // of "declared" at any point in a DIE chain formed from
17165 // DW_AT_abstract_origin and DW_AT_specification links. There should
17166 // be no other callers of die_is_declaration_only.
17167 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
17168
17169 switch (tag)
17170 {
17171 // Type DIEs we support.
17172 case DW_TAG_base_type:
17173 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
17174 {
17175 result =
17176 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
17177 maybe_canonicalize_type(t, rdr);
17178 }
17179 break;
17180
17181 case DW_TAG_typedef:
17182 {
17183 typedef_decl_sptr t = build_typedef_type(rdr, die,
17184 called_from_public_decl,
17185 where_offset);
17186
17187 result = add_decl_to_scope(t, scope);
17188 if (result)
17189 {
17190 maybe_set_member_type_access_specifier(is_decl(result), die);
17191 maybe_canonicalize_type(t, rdr);
17192 }
17193 }
17194 break;
17195
17196 case DW_TAG_pointer_type:
17197 {
17199 build_pointer_type_def(rdr, die,
17200 called_from_public_decl,
17201 where_offset);
17202 if (p)
17203 {
17204 result =
17205 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
17206 ABG_ASSERT(result->get_translation_unit());
17207 maybe_canonicalize_type(p, rdr);
17208 }
17209 }
17210 break;
17211
17212 case DW_TAG_reference_type:
17213 case DW_TAG_rvalue_reference_type:
17214 {
17216 build_reference_type(rdr, die,
17217 called_from_public_decl,
17218 where_offset);
17219 if (r)
17220 {
17221 result =
17222 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
17223 maybe_canonicalize_type(r, rdr);
17224 }
17225 }
17226 break;
17227
17228 case DW_TAG_ptr_to_member_type:
17229 {
17231 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
17232 where_offset);
17233 if (p)
17234 {
17235 result =
17237 rdr.cur_transl_unit()->get_global_scope());
17238 maybe_canonicalize_type(p, rdr);
17239 }
17240 }
17241 break;
17242
17243 case DW_TAG_const_type:
17244 case DW_TAG_volatile_type:
17245 case DW_TAG_restrict_type:
17246 {
17247 type_base_sptr q =
17248 build_qualified_type(rdr, die,
17249 called_from_public_decl,
17250 where_offset);
17251 if (q)
17252 {
17253 // Strip some potentially redundant type qualifiers from
17254 // the qualified type we just built.
17255 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
17256 rdr);
17257 if (!d)
17258 d = get_type_declaration(q);
17259 ABG_ASSERT(d);
17260 type_base_sptr ty = is_type(d);
17261 // Associate the die to type ty again because 'ty'might be
17262 // different from 'q', because 'ty' is 'q' possibly
17263 // stripped from some redundant type qualifier.
17264 rdr.associate_die_to_type(die, ty, where_offset);
17265 result =
17266 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
17267 maybe_canonicalize_type(is_type(result), rdr);
17268 }
17269 }
17270 break;
17271
17272 case DW_TAG_enumeration_type:
17273 {
17274 bool type_is_opaque = false;
17275 bool type_suppressed =
17276 type_is_suppressed(rdr, scope, die, type_is_opaque);
17277 if (type_suppressed && type_is_opaque)
17278 {
17279 // The type is suppressed because it's private. If other
17280 // non-suppressed and declaration-only instances of this
17281 // type exist in the current corpus, then it means those
17282 // non-suppressed instances are opaque versions of the
17283 // suppressed private type. Lets return one of these opaque
17284 // types then.
17285 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17286 maybe_canonicalize_type(is_type(result), rdr);
17287 }
17288 else if (!type_suppressed)
17289 {
17290 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
17291 where_offset,
17292 is_declaration_only);
17293 result = add_decl_to_scope(e, scope);
17294 if (result)
17295 {
17296 maybe_set_member_type_access_specifier(is_decl(result), die);
17297 maybe_canonicalize_type(is_type(result), rdr);
17298 }
17299 }
17300 }
17301 break;
17302
17303 case DW_TAG_class_type:
17304 case DW_TAG_structure_type:
17305 {
17306 bool type_is_opaque = false;
17307 bool type_suppressed=
17308 type_is_suppressed(rdr, scope, die, type_is_opaque);
17309
17310 if (type_suppressed && type_is_opaque)
17311 {
17312 // The type is suppressed because it's private. If other
17313 // non-suppressed and declaration-only instances of this
17314 // type exist in the current corpus, then it means those
17315 // non-suppressed instances are opaque versions of the
17316 // suppressed private type. Lets return one of these opaque
17317 // types then.
17318 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17319 maybe_canonicalize_type(is_type(result), rdr);
17320 }
17321 else if (!type_suppressed)
17322 {
17323 class_decl_sptr klass;
17324 Dwarf_Die spec_die;
17325 if (die_die_attribute(die, DW_AT_specification, spec_die))
17326 {
17327 scope_decl_sptr skope =
17328 get_scope_for_die(rdr, &spec_die,
17329 called_from_public_decl,
17330 where_offset);
17331 ABG_ASSERT(skope);
17332 decl_base_sptr cl =
17333 is_decl(build_ir_node_from_die(rdr, &spec_die,
17334 skope.get(),
17335 called_from_public_decl,
17336 where_offset,
17337 is_declaration_only,
17338 /*is_required_decl_spec=*/false));
17339 ABG_ASSERT(cl);
17340 klass = dynamic_pointer_cast<class_decl>(cl);
17341 ABG_ASSERT(klass);
17342
17343 klass =
17344 add_or_update_class_type(rdr, die,
17345 skope.get(),
17346 tag == DW_TAG_structure_type,
17347 klass,
17348 called_from_public_decl,
17349 where_offset,
17350 is_declaration_only);
17351 }
17352 else
17353 {
17354 if (class_decl* class_sc = is_class_type(scope))
17355 {
17356 string type_name = die_type_name(rdr, die,
17357 /*qualified_name=*/false,
17358 where_offset);
17359 if (class_decl_sptr c =
17360 is_class_type(class_sc->find_member_type(type_name)))
17361 klass = c;
17362 else
17363 klass =
17364 add_or_update_class_type(rdr, die, scope,
17365 tag == DW_TAG_structure_type,
17367 called_from_public_decl,
17368 where_offset,
17369 is_declaration_only);
17370 }
17371 else
17372 klass =
17373 add_or_update_class_type(rdr, die, scope,
17374 tag == DW_TAG_structure_type,
17376 called_from_public_decl,
17377 where_offset,
17378 is_declaration_only);
17379 }
17380 if (klass)
17381 {
17382 maybe_set_member_type_access_specifier(klass, die);
17383 maybe_canonicalize_type(klass, rdr);
17384 }
17385 result = klass;
17386 }
17387 }
17388 break;
17389 case DW_TAG_union_type:
17390 if (!type_is_suppressed(rdr, scope, die))
17391 {
17392 union_decl_sptr union_type;
17393 if (class_decl* class_sc = is_class_type(scope))
17394 {
17395 string type_name = die_type_name(rdr, die,
17396 /*qualified_name=*/false,
17397 where_offset);
17398 if (union_decl_sptr u =
17399 is_union_type(class_sc->find_member_type(type_name)))
17400 union_type = u;
17401 }
17402
17403 if (!union_type)
17404 union_type =
17405 add_or_update_union_type(rdr, die, scope,
17406 union_decl_sptr(),
17407 called_from_public_decl,
17408 where_offset,
17409 is_declaration_only);
17410
17411 if (union_type)
17412 {
17413 maybe_set_member_type_access_specifier(union_type, die);
17414 maybe_canonicalize_type(union_type, rdr);
17415 result = union_type;
17416 }
17417 }
17418 break;
17419 case DW_TAG_string_type:
17420 break;
17421 case DW_TAG_subroutine_type:
17422 {
17423 function_type_sptr f = build_function_type(rdr, die,
17425 where_offset);
17426 if (f)
17427 {
17428 result = f;
17429 result->set_is_artificial(false);
17430 maybe_canonicalize_type(f, rdr);
17431 }
17432 }
17433 break;
17434 case DW_TAG_array_type:
17435 {
17436 array_type_def_sptr a = build_array_type(rdr,
17437 die,
17438 called_from_public_decl,
17439 where_offset);
17440 if (a)
17441 {
17442 result =
17443 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
17444 maybe_canonicalize_type(a, rdr);
17445 }
17446 break;
17447 }
17448 case DW_TAG_subrange_type:
17449 {
17450 // If we got here, this means the subrange type is a "free
17451 // form" defined in the global namespace of the current
17452 // translation unit, like what is found in Ada.
17454 build_subrange_type(rdr, die, where_offset,
17455 /*associate_type_to_die=*/true);
17456 if (s)
17457 {
17458 result =
17459 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
17460 maybe_canonicalize_type(s, rdr);
17461 }
17462 }
17463 break;
17464 case DW_TAG_packed_type:
17465 break;
17466 case DW_TAG_set_type:
17467 break;
17468 case DW_TAG_file_type:
17469 break;
17470 case DW_TAG_thrown_type:
17471 break;
17472 case DW_TAG_interface_type:
17473 break;
17474 case DW_TAG_unspecified_type:
17475 break;
17476 case DW_TAG_shared_type:
17477 break;
17478
17479 case DW_TAG_compile_unit:
17480 // We shouldn't reach this point b/c this should be handled by
17481 // build_translation_unit.
17483
17484 case DW_TAG_namespace:
17485 case DW_TAG_module:
17486 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
17487 break;
17488
17489 case DW_TAG_variable:
17490 case DW_TAG_member:
17491 {
17492 if (tag == DW_TAG_member)
17493 ABG_ASSERT(!die_is_in_c(die));
17494
17495 scope_decl_sptr var_scope =
17496 get_scope_for_die(rdr, die,
17497 /*called_from_public_decl=*/
17498 die_is_effectively_public_decl(rdr, die),
17499 where_offset);
17500 var_decl_sptr v =
17501 build_or_get_var_decl_if_not_suppressed(rdr, var_scope.get(), die,
17502 where_offset,
17503 is_declaration_only,
17504 /*result=*/var_decl_sptr(),
17505 is_required_decl_spec);
17506 if (v && is_data_member(v))
17507 // We might have gotten a pre-existing data member variable
17508 // that was already built. This means this DIE is a
17509 // concrete implementation of a previous specification.
17510 // Read the specific attributes of this concrete
17511 // implementation and add them to the existing IR node we
17512 // have.
17513 v = build_var_decl(rdr, die, where_offset, v);
17514
17515 Dwarf_Addr addr = 0;
17516 bool has_data_location = false;
17517 has_data_location = rdr.get_variable_address(die, addr);
17518
17519 if ((v && has_data_location && is_class_type(var_scope))
17520 // This is most likely for a static data member's variable
17521 // that has data location ...
17522 || (v && rdr.is_decl_die_with_undefined_symbol(die))
17523 || (v && rdr.is_decl_die_with_exported_symbol(die))
17524 // ... or this is for an undefined or defined & exported
17525 // global variable.
17526 )
17527 {
17528 add_decl_to_scope(v, var_scope);
17529 if (is_data_member(v))
17530 // We are sure this is a static data member at this
17531 // point because a non-static data member would have
17532 // been encountered as a child of a class or union DIE
17533 // and thus handled by add_or_update_class_type or
17534 // add_or_update_union_type.
17535 set_member_is_static(v, true);
17536 else
17537 rdr.var_decls_to_re_add_to_tree().push_back(v);
17538 rdr.add_var_to_exported_or_undefined_decls(v);
17539 rdr.associate_die_to_decl(die, v, where_offset,
17540 /*associate_by_repr=*/false);
17541 result = v;
17542 }
17543 }
17544 break;
17545
17546 case DW_TAG_subprogram:
17547 case DW_TAG_inlined_subroutine:
17548 {
17549 if (die_is_artificial(die))
17550 break;
17551
17552 Dwarf_Die abstract_origin_die;
17553 bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
17554 abstract_origin_die,
17555 /*recursive=*/true);
17556
17557
17558 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
17559 where_offset);
17560 scope_decl* interface_scope = scope ? scope : s.get();
17561
17562 class_decl* class_scope = is_class_type(interface_scope);
17563 string linkage_name = die_linkage_name(die);
17564 string spec_linkage_name;
17565 function_decl_sptr existing_fn;
17566
17567 if (class_scope)
17568 {
17569 // The scope of the function DIE we are looking at is a
17570 // class. So we are looking at a member function.
17571 if (!linkage_name.empty())
17572 {
17573 if ((existing_fn =
17574 class_scope->find_member_function_sptr(linkage_name)))
17575 {
17576 // A function with the same linkage name has
17577 // already been created. Let's see if we are a
17578 // clone of it or not.
17579 spec_linkage_name = existing_fn->get_linkage_name();
17580 if (has_abstract_origin
17581 && !spec_linkage_name.empty()
17582 && linkage_name != spec_linkage_name)
17583 {
17584 // The current DIE has 'existing_fn' as
17585 // abstract orign, and has a linkage name that
17586 // is different from from the linkage name of
17587 // 'existing_fn'. That means, the current DIE
17588 // represents a clone of 'existing_fn'.
17589 existing_fn = existing_fn->clone();
17590 }
17591 }
17592 }
17593 }
17594 else if (has_abstract_origin)
17595 // Let's see if this function is the implementation of an
17596 // existing interface. In that case, let's read the
17597 // specification of the origin interface ...
17598 existing_fn = build_function_decl(rdr, &abstract_origin_die, where_offset,
17599 /*existing_fn=*/nullptr);
17600
17601 rdr.scope_stack().push(interface_scope);
17602
17603 // Either we create a brand new IR for the current function
17604 // DIE we are looking at, or we complete an existing IR node
17605 // with the new completementary information carried by this
17606 // DIE for that IR node.
17607 result =
17608 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
17609 die, where_offset,
17610 is_declaration_only,
17611 existing_fn);
17612
17613 if (result && !existing_fn)
17614 {
17615 // We built a brand new IR for the function DIE. Now
17616 // there should be enough information on that IR to know
17617 // if we should drop it on the floor or keep it ...
17618 if (potential_member_fn_should_be_dropped(is_function_decl(result), die)
17619 && !is_required_decl_spec)
17620 {
17621 // So apparently we should drop that function IR on
17622 // the floor. Let's do so.
17623 result.reset();
17624 break;
17625 }
17626 }
17627
17628 // OK so we came to the conclusion that we need to keep
17629 // the function. So let's add it to its scope.
17630 result = add_decl_to_scope(is_decl(result), interface_scope);
17631
17633 if (fn && is_member_function(fn))
17634 {
17635 class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
17636 sptr_utils::noop_deleter());
17637 ABG_ASSERT(klass);
17638 finish_member_function_reading(die, fn, klass, rdr);
17639 }
17640
17641 if (fn)
17642 {
17643 if (!is_member_function(fn)
17645 // Virtual member functions are added to the set of
17646 // functions exported by the current ABI corpus *after*
17647 // the canonicalization of their parent type. So let's
17648 // not do it here.
17649 rdr.add_fn_to_exported_or_undefined_decls(fn.get());
17650 rdr.associate_die_to_decl(die, fn, where_offset,
17651 /*associate_by_repr=*/false);
17652 maybe_canonicalize_type(fn->get_type(), rdr);
17653 }
17654
17655 rdr.scope_stack().pop();
17656 }
17657 break;
17658
17659 case DW_TAG_formal_parameter:
17660 // We should not read this case as it should have been dealt
17661 // with by build_function_decl above.
17663
17664 case DW_TAG_constant:
17665 break;
17666 case DW_TAG_enumerator:
17667 break;
17668
17669 case DW_TAG_partial_unit:
17670 case DW_TAG_imported_unit:
17671 // For now, the DIEs under these are read lazily when they are
17672 // referenced by a public decl DIE that is under a
17673 // DW_TAG_compile_unit, so we shouldn't get here.
17675
17676 // Other declaration we don't really intend to support yet.
17677 case DW_TAG_dwarf_procedure:
17678 case DW_TAG_imported_declaration:
17679 case DW_TAG_entry_point:
17680 case DW_TAG_label:
17681 case DW_TAG_lexical_block:
17682 case DW_TAG_unspecified_parameters:
17683 case DW_TAG_variant:
17684 case DW_TAG_common_block:
17685 case DW_TAG_common_inclusion:
17686 case DW_TAG_inheritance:
17687 case DW_TAG_with_stmt:
17688 case DW_TAG_access_declaration:
17689 case DW_TAG_catch_block:
17690 case DW_TAG_friend:
17691 case DW_TAG_namelist:
17692 case DW_TAG_namelist_item:
17693 case DW_TAG_template_type_parameter:
17694 case DW_TAG_template_value_parameter:
17695 case DW_TAG_try_block:
17696 case DW_TAG_variant_part:
17697 case DW_TAG_imported_module:
17698 case DW_TAG_condition:
17699 case DW_TAG_type_unit:
17700 case DW_TAG_template_alias:
17701 case DW_TAG_lo_user:
17702 case DW_TAG_MIPS_loop:
17703 case DW_TAG_format_label:
17704 case DW_TAG_function_template:
17705 case DW_TAG_class_template:
17706 case DW_TAG_GNU_BINCL:
17707 case DW_TAG_GNU_EINCL:
17708 case DW_TAG_GNU_template_template_param:
17709 case DW_TAG_GNU_template_parameter_pack:
17710 case DW_TAG_GNU_formal_parameter_pack:
17711 case DW_TAG_GNU_call_site:
17712 case DW_TAG_GNU_call_site_parameter:
17713 case DW_TAG_hi_user:
17714 default:
17715 break;
17716 }
17717
17718 if (result && tag != DW_TAG_subroutine_type)
17719 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
17720 /*associate_by_repr=*/false);
17721
17722 if (result)
17723 if (rdr.load_all_types())
17724 if (called_from_public_decl)
17725 if (type_base_sptr t = is_type(result))
17726 if (corpus *abi_corpus = scope->get_corpus())
17727 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17728
17729 rdr.maybe_schedule_decl_only_type_for_resolution(result);
17730
17731 return result;
17732}
17733
17734/// Build the IR node for a void type.
17735///
17736/// @param rdr the DWARF reader to use.
17737///
17738/// @return the void type node.
17739static decl_base_sptr
17740build_ir_node_for_void_type(reader& rdr)
17741{
17742 const environment& env = rdr.env();
17743
17744 type_base_sptr t = env.get_void_type();
17745 decl_base_sptr type_declaration = get_type_declaration(t);
17746 if (!has_scope(type_declaration))
17747 {
17748 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17749 rdr.schedule_type_for_late_canonicalization(t);
17750 }
17751 return type_declaration;
17752}
17753
17754/// Build the IR node for a "pointer to void type".
17755///
17756/// That IR node is shared across the ABI corpus.
17757///
17758/// Note that this function just gets that IR node from the
17759/// environment and, if it's not added to any scope yet, adds it to
17760/// the global scope associated to the current translation unit.
17761///
17762/// @param rdr the DWARF reader to consider.
17763///
17764/// @return the IR node.
17766build_ir_node_for_void_pointer_type(reader& rdr)
17767{
17768 const environment& env = rdr.env();
17769 type_base_sptr t = env.get_void_pointer_type();
17770 decl_base_sptr type_declaration = get_type_declaration(t);
17771 if (!has_scope(type_declaration))
17772 {
17773 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17774 rdr.schedule_type_for_late_canonicalization(t);
17775 }
17776 return type_declaration;
17777}
17778
17779/// Build the IR node for a variadic parameter type.
17780///
17781/// @param rdr the DWARF reader to use.
17782///
17783/// @return the variadic parameter type.
17784static decl_base_sptr
17785build_ir_node_for_variadic_parameter_type(reader &rdr)
17786{
17787
17788 const environment& env = rdr.env();
17789 type_base_sptr t = env.get_variadic_parameter_type();
17790 decl_base_sptr type_declaration = get_type_declaration(t);
17791 if (!has_scope(type_declaration))
17792 {
17793 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17794 rdr.schedule_type_for_late_canonicalization(t);
17795 }
17796 return type_declaration;
17797}
17798
17799/// Build an IR node from a given DIE and add the node to the current
17800/// IR being build and held in the DWARF reader. Doing that is called
17801/// "emitting an IR node for the DIE".
17802///
17803/// @param rdr the DWARF reader.
17804///
17805/// @param die the DIE to consider.
17806///
17807/// @param called_from_public_decl set to yes if this function is
17808/// called from the functions used to build a public decl (functions
17809/// and variables). In that case, this function accepts building IR
17810/// nodes representing types. Otherwise, this function only creates
17811/// IR nodes representing public decls (functions and variables).
17812/// This is done to avoid emitting IR nodes for types that are not
17813/// referenced by public functions or variables.
17814///
17815/// @param where_offset the offset of the DIE where we are "logically"
17816/// positionned at, in the DIE tree. This is useful when @p die is
17817/// e.g, DW_TAG_partial_unit that can be included in several places in
17818/// the DIE tree.
17819///
17820/// @return the resulting IR node.
17822build_ir_node_from_die(reader& rdr,
17823 Dwarf_Die* die,
17824 bool called_from_public_decl,
17825 size_t where_offset)
17826{
17827 if (!die)
17828 return decl_base_sptr();
17829
17830 // Normaly, a decl that is meant to be external has a DW_AT_external
17831 // set. But then some compilers fail to always emit that flag. For
17832 // instance, for static data members, some compilers won't emit the
17833 // DW_AT_external. In that case, we assume that if the variable is
17834 // at global or named namespace scope, then we can assume it's
17835 // external. If the variable doesn't have any ELF symbol associated
17836 // to it, it'll be dropped on the floor anyway. Those variable
17837 // decls are considered as being "effectively public".
17838 bool consider_as_called_from_public_decl =
17839 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
17840 scope_decl_sptr scope = get_scope_for_die(rdr, die,
17841 consider_as_called_from_public_decl,
17842 where_offset);
17843 if (!scope)
17844 scope = rdr.global_scope();
17845
17846 return build_ir_node_from_die(rdr, die, scope.get(),
17847 called_from_public_decl,
17848 where_offset, true);
17849}
17850
17851/// Create a dwarf::reader.
17852///
17853/// @param elf_path the path to the elf file the reader is to be used
17854/// for.
17855///
17856/// @param debug_info_root_paths a vector to the paths to the
17857/// directories under which the debug info is to be found for @p
17858/// elf_path. Pass an empty vector if the debug info is not in a
17859/// split file.
17860///
17861/// @param environment the environment used by the current context.
17862/// This environment contains resources needed by the DWARF reader and by
17863/// the types and declarations that are to be created later. Note
17864/// that ABI artifacts that are to be compared all need to be created
17865/// within the same environment.
17866///
17867/// Please also note that the life time of this environment object
17868/// must be greater than the life time of the resulting @ref
17869/// reader the context uses resources that are allocated in the
17870/// environment.
17871///
17872/// @param load_all_types if set to false only the types that are
17873/// reachable from publicly exported declarations (of functions and
17874/// variables) are read. If set to true then all types found in the
17875/// debug information are loaded.
17876///
17877/// @param linux_kernel_mode if set to true, then consider the special
17878/// linux kernel symbol tables when determining if a symbol is
17879/// exported or not.
17880///
17881/// @return a smart pointer to the resulting dwarf::reader.
17882elf_based_reader_sptr
17883create_reader(const std::string& elf_path,
17884 const vector<string>& debug_info_root_paths,
17886 bool load_all_types,
17887 bool linux_kernel_mode)
17888{
17889
17890 reader_sptr r = reader::create(elf_path,
17891 debug_info_root_paths,
17893 load_all_types,
17894 linux_kernel_mode);
17895 return static_pointer_cast<elf_based_reader>(r);
17896}
17897
17898/// Re-initialize a reader so that it can re-used to read
17899/// another binary.
17900///
17901/// @param rdr the context to re-initialize.
17902///
17903/// @param elf_path the path to the elf file the context is to be used
17904/// for.
17905///
17906/// @param debug_info_root_path a pointer to the path to the root
17907/// directory under which the debug info is to be found for @p
17908/// elf_path. Leave this to NULL if the debug info is not in a split
17909/// file.
17910///
17911/// @param environment the environment used by the current context.
17912/// This environment contains resources needed by the DWARF reader and by
17913/// the types and declarations that are to be created later. Note
17914/// that ABI artifacts that are to be compared all need to be created
17915/// within the same environment.
17916///
17917/// Please also note that the life time of this environment object
17918/// must be greater than the life time of the resulting @ref
17919/// reader the context uses resources that are allocated in the
17920/// environment.
17921///
17922/// @param load_all_types if set to false only the types that are
17923/// reachable from publicly exported declarations (of functions and
17924/// variables) are read. If set to true then all types found in the
17925/// debug information are loaded.
17926///
17927/// @param linux_kernel_mode if set to true, then consider the special
17928/// linux kernel symbol tables when determining if a symbol is
17929/// exported or not.
17930///
17931/// @return a smart pointer to the resulting dwarf::reader.
17932void
17934 const std::string& elf_path,
17935 const vector<string>&debug_info_root_path,
17936 bool read_all_types,
17937 bool linux_kernel_mode)
17938{
17939 reader& r = dynamic_cast<reader&>(rdr);
17940 r.initialize(elf_path, debug_info_root_path,
17941 read_all_types, linux_kernel_mode);
17942}
17943
17944/// Read all @ref abigail::translation_unit possible from the debug info
17945/// accessible from an elf file, stuff them into a libabigail ABI
17946/// Corpus and return it.
17947///
17948/// @param elf_path the path to the elf file.
17949///
17950/// @param debug_info_root_paths a vector of pointers to root paths
17951/// under which to look for the debug info of the elf files that are
17952/// later handled by the Dwfl. This for cases where the debug info is
17953/// split into a different file from the binary we want to inspect.
17954/// On Red Hat compatible systems, this root path is usually
17955/// /usr/lib/debug by default. If this argument is set to NULL, then
17956/// "./debug" and /usr/lib/debug will be searched for sub-directories
17957/// containing the debug info file.
17958///
17959/// @param environment the environment used by the current context.
17960/// This environment contains resources needed by the DWARF reader and by
17961/// the types and declarations that are to be created later. Note
17962/// that ABI artifacts that are to be compared all need to be created
17963/// within the same environment. Also, the lifetime of the
17964/// environment must be greater than the lifetime of the resulting
17965/// corpus because the corpus uses resources that are allocated in the
17966/// environment.
17967///
17968/// @param load_all_types if set to false only the types that are
17969/// reachable from publicly exported declarations (of functions and
17970/// variables) are read. If set to true then all types found in the
17971/// debug information are loaded.
17972///
17973/// @param resulting_corp a pointer to the resulting abigail::corpus.
17974///
17975/// @return the resulting status.
17976corpus_sptr
17977read_corpus_from_elf(const std::string& elf_path,
17978 const vector<string>& debug_info_root_paths,
17980 bool load_all_types,
17981 fe_iface::status& status)
17982{
17983 elf_based_reader_sptr rdr =
17984 dwarf::reader::create(elf_path, debug_info_root_paths,
17985 environment, load_all_types,
17986 /*linux_kernel_mode=*/false);
17987
17988 return rdr->read_corpus(status);
17989}
17990
17991/// Look into the symbol tables of a given elf file and see if we find
17992/// a given symbol.
17993///
17994/// @param env the environment we are operating from.
17995///
17996/// @param elf_path the path to the elf file to consider.
17997///
17998/// @param symbol_name the name of the symbol to look for.
17999///
18000/// @param demangle if true, try to demangle the symbol name found in
18001/// the symbol table.
18002///
18003/// @param syms the vector of symbols found with the name @p symbol_name.
18004///
18005/// @return true iff the symbol was found among the publicly exported
18006/// symbols of the ELF file.
18007bool
18008lookup_symbol_from_elf(const environment& env,
18009 const string& elf_path,
18010 const string& symbol_name,
18011 bool demangle,
18012 vector<elf_symbol_sptr>& syms)
18013
18014{
18015 if (elf_version(EV_CURRENT) == EV_NONE)
18016 return false;
18017
18018 int fd = open(elf_path.c_str(), O_RDONLY);
18019 if (fd < 0)
18020 return false;
18021
18022 struct stat s;
18023 if (fstat(fd, &s))
18024 return false;
18025
18026 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
18027 if (elf == 0)
18028 return false;
18029
18030 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
18031 demangle, syms);
18032 elf_end(elf);
18033 close(fd);
18034
18035 return value;
18036}
18037
18038/// Look into the symbol tables of an elf file to see if a public
18039/// function of a given name is found.
18040///
18041/// @param env the environment we are operating from.
18042///
18043/// @param elf_path the path to the elf file to consider.
18044///
18045/// @param symbol_name the name of the function to look for.
18046///
18047/// @param syms the vector of public function symbols found with the
18048/// name @p symname.
18049///
18050/// @return true iff a function with symbol name @p symbol_name is
18051/// found.
18052bool
18053lookup_public_function_symbol_from_elf(environment& env,
18054 const string& path,
18055 const string& symname,
18056 vector<elf_symbol_sptr>& syms)
18057{
18058 if (elf_version(EV_CURRENT) == EV_NONE)
18059 return false;
18060
18061 int fd = open(path.c_str(), O_RDONLY);
18062 if (fd < 0)
18063 return false;
18064
18065 struct stat s;
18066 if (fstat(fd, &s))
18067 return false;
18068
18069 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
18070 if (elf == 0)
18071 return false;
18072
18073 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
18074 elf_end(elf);
18075 close(fd);
18076
18077 return value;
18078}
18079
18080}// end namespace dwarf
18081
18082}// 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:1743
This contains the private implementation of the suppression engine of libabigail.
Utilities to ease the wrapping of C types into std::shared_ptr.
This contains the private implementation of the suppression engine of libabigail.
This contains the declarations for the symtab reader.
#define ABG_ASSERT_NOT_REACHED
A macro that expands to aborting the program when executed.
Simplified implementation of std::optional just enough to be used as a replacement for our purposes a...
elf_symbol_sptr function_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined function symbol.
const string & elf_architecture() const
Get the value of the 'ARCHITECTURE' property of the current ELF file.
const vector< string > & debug_info_root_paths() const
Getter of the vector of directory paths to look into for split debug information files.
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.
elf_symbol_sptr variable_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined variable symbol.
elf_symbol_sptr variable_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given variable symbol has been exported.
symtab_reader::symtab_sptr & symtab() const
Getter of an abstract representation of the symbol table of the underlying ELF file.
elf_symbol_sptr function_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given function symbol has been exported.
const vector< string > & dt_needed() const
Get the value of the DT_NEEDED property of the current ELF file.
The common interface of readers based on ELF.
elf_based_reader(const std::string &elf_path, const vector< string > &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< string > &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:2589
static string vector_as_string(const vector< subrange_sptr > &)
Return a string representation of a vector of subranges.
Definition abg-ir.cc:19515
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition abg-ir.h:2566
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition abg-ir.h:2569
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition abg-ir.h:4192
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:4792
The abstraction of the version of an ELF symbol.
Definition abg-ir.h:1232
binding
The binding of a symbol.
Definition abg-ir.h:978
type
The type of a symbol.
Definition abg-ir.h:965
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:2063
visibility
The visibility of the symbol.
Definition abg-ir.h:987
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition abg-ir.h:2801
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:3187
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition abg-ir.h:3190
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:2255
The internal representation of an integral type.
Definition abg-ir-priv.h:49
string to_string(bool internal=false) const
Return the string representation of the current instance of real_type.
Definition abg-ir.cc:16850
language
The language of the translation unit.
Definition abg-ir.h:708
enum type_or_decl_kind kind() const
Getter for the "kind" property of type_or_decl_base type.
Definition abg-ir.cc:4114
visiting_kind operator~(visiting_kind l)
The overloaded 'bit inversion' operator for visiting_kind.
visiting_kind operator&(visiting_kind l, visiting_kind r)
The overloaded and operator for visiting_kind.
visiting_kind operator|(visiting_kind l, visiting_kind r)
The overloaded or operator for visiting_kind.
ostream & operator<<(ostream &o, diff_category c)
Serialize an instance of diff_category to an output stream.
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...
corpus_sptr read_corpus_from_elf(const std::string &elf_path, const vector< string > &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,...
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.
void reset_reader(elf_based_reader &rdr, const std::string &elf_path, const vector< string > &debug_info_root_path, bool read_all_types, bool linux_kernel_mode)
Re-initialize a reader so that it can re-used to read another binary.
unordered_map< 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.
elf_based_reader_sptr create_reader(const std::string &elf_path, const vector< string > &debug_info_root_paths, environment &environment, bool load_all_types, bool linux_kernel_mode)
Create a dwarf::reader.
vector< imported_unit_point > imported_unit_points_type
Convenience typedef for a vector of imported_unit_point.
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:6454
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:12445
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:12309
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:917
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:14026
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:14188
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:11787
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition abg-ir.cc:10807
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition abg-ir.cc:5399
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:12215
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition abg-ir.h:926
bool parse_real_type(const string &type_name, real_type &type)
Parse a real type from a string.
Definition abg-ir.cc:16768
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition abg-ir.cc:8475
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:10207
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:11711
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:12335
comparison_result
The result of structural comparison of type ABI artifacts.
Definition abg-ir-priv.h:36
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition abg-ir.cc:11165
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:6715
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:10858
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:7333
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:6114
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:13889
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition abg-ir.cc:6482
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:12776
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:11396
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:6538
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:6915
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:10909
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:11858
string get_name(const type_or_decl_base *tod, bool qualified)
Build and return a copy of the name of an ABI artifact that is either a type or a decl.
Definition abg-ir.cc:8686
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition abg-ir.cc:5544
const class_decl * is_compatible_with_class_type(const type_base *t)
Test if a type is a class. This function looks through typedefs.
Definition abg-ir.cc:11118
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition abg-ir.cc:11011
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:14056
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:12485
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:11614
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:11651
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition abg-ir.cc:1808
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:11100
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:8543
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
method_decl_sptr copy_member_function(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:24842
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:12975
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition abg-ir.cc:1824
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:12522
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:7301
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition abg-ir.cc:6368
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition abg-ir.cc:1792
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition abg-ir.cc:10747
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:25793
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition abg-ir.cc:5464
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:8450
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:29165
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition abg-ir.cc:5515
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:6641
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:11479
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:11918
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition abg-ir.cc:11445
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:13844
var_decl_sptr copy_member_variable(class_or_union_sptr t, const var_decl *variable)
Copy a data member of a class_or_union into a new class_or_union.
Definition abg-ir.cc:24910
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:12194
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition abg-ir.cc:5613
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:13793
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition abg-ir.cc:10229
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition abg-ir.cc:26893
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:12123
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:7038
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:1833
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition abg-ir.cc:15428
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:7668
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:14151
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition abg-ir.cc:10695
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:11888
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:11838
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:12555
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:8732
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:11948
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition abg-ir.cc:5417
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:6425
bool is_function_suppressed(const fe_iface &fe, const string &fn_name, const string &fn_linkage_name, bool require_drop_property)
Test if a function is matched by at least one suppression specification associated with a given front...
bool is_type_suppressed(const fe_iface &fe, const string &type_name, const location &type_location, bool &type_is_opaque, bool require_drop_property)
Test if a type is matched by at least one suppression specification associated with a given front-end...
bool is_variable_suppressed(const fe_iface &fe, const string &var_name, const string &var_linkage_name, bool require_drop_property)
Test if a variable is matched by at least one suppression specification associated with a given front...
bool normalize_litterals(string &str)
Normalize the numerical litteral in a string.
bool remove_white_spaces(string &str)
Remove white spaces from a string.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool string_is_ascii_identifier(const string &str)
Test if a string is made of ascii characters which are identifiers acceptable in C or C++ programs.
Toplevel namespace for libabigail.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition abg-ir.cc:151
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition abg-ir.cc:185
bool load_undefined_interfaces
If this option is set to true, then the functions and variables that have an undefined symbol are goi...
A functor to hash instances of interned_string.