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-2026 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 const environment& env = types_to_canonicalize().front()->get_environment();
5095 cerr << "DWARF Reader finished types "
5096 << "sorting, hashing & canonicalizing in: "
5097 << cn_timer
5098 << ", for "
5099 << env.priv_->get_number_of_canonical_types()
5100 << " types\n";
5101 }
5102 }
5103
5104 /// Compute the number of canonicalized and missed types in the late
5105 /// canonicalization phase.
5106 ///
5107 /// @param source where the DIEs of the canonicalized types are
5108 /// from.
5109 ///
5110 /// @param canonicalized the number of types that got canonicalized
5111 /// is added to the value already present in this parameter.
5112 ///
5113 /// @param missed the number of types scheduled for late
5114 /// canonicalization and which couldn't be canonicalized (for a
5115 /// reason) is added to the value already present in this parameter.
5116 void
5117 add_late_canonicalized_types_stats(size_t& canonicalized,
5118 size_t& missed) const
5119 {
5120 for (auto t : types_to_canonicalize())
5121 {
5122 if (t->get_canonical_type())
5123 ++canonicalized;
5124 else
5125 ++missed;
5126 }
5127 }
5128
5129 // Look at the types that need to be canonicalized after the
5130 // translation unit has been constructed and canonicalize them.
5131 void
5132 perform_late_type_canonicalizing()
5133 {
5134 canonicalize_types_scheduled();
5135
5136 if (show_stats())
5137 {
5138 size_t num_canonicalized = 0, num_missed = 0, total = 0;
5139 add_late_canonicalized_types_stats(num_canonicalized,
5140 num_missed);
5141 total = num_canonicalized + num_missed;
5142 cerr << "binary: "
5143 << elf_path()
5144 << "\n";
5145 cerr << " # late canonicalized types: "
5146 << num_canonicalized;
5147 if (total)
5148 cerr << " (" << num_canonicalized * 100 / total << "%)";
5149 cerr << "\n"
5150 << " # missed canonicalization opportunities: "
5151 << num_missed;
5152 if (total)
5153 cerr << " (" << num_missed * 100 / total << "%)";
5154 cerr << "\n";
5155 }
5156
5157 }
5158
5159 const die_tu_map_type&
5160 die_tu_map() const
5161 {return die_tu_map_;}
5162
5164 die_tu_map()
5165 {return die_tu_map_;}
5166
5167 /// Getter for the map that associates a translation unit DIE to the
5168 /// vector of imported unit points that it contains.
5169 ///
5170 /// @param source where the DIEs are from.
5171 ///
5172 /// @return the map.
5174 tu_die_imported_unit_points_map(die_source source) const
5175 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
5176
5177 /// Getter for the map that associates a translation unit DIE to the
5178 /// vector of imported unit points that it contains.
5179 ///
5180 /// @param source where the DIEs are from.
5181 ///
5182 /// @return the map.
5184 tu_die_imported_unit_points_map(die_source source)
5185 {
5186 switch (source)
5187 {
5188 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5189 break;
5190 case ALT_DEBUG_INFO_DIE_SOURCE:
5191 return alt_tu_die_imported_unit_points_map_;
5192 case TYPE_UNIT_DIE_SOURCE:
5193 return type_units_tu_die_imported_unit_points_map_;
5194 case NO_DEBUG_INFO_DIE_SOURCE:
5195 case NUMBER_OF_DIE_SOURCES:
5196 // We cannot reach this point.
5198 }
5199 return tu_die_imported_unit_points_map_;
5200 }
5201
5202 /// Reset the current corpus being constructed.
5203 ///
5204 /// This actually deletes the current corpus being constructed.
5205 void
5206 reset_corpus()
5207 {corpus().reset();}
5208
5209 /// Get the map that associates each DIE to its parent DIE. This is
5210 /// for DIEs coming from the main debug info sections.
5211 ///
5212 /// @param source where the DIEs in the map come from.
5213 ///
5214 /// @return the DIE -> parent map.
5216 die_parent_map(die_source source) const
5217 {return const_cast<reader*>(this)->die_parent_map(source);}
5218
5219 /// Get the map that associates each DIE to its parent DIE. This is
5220 /// for DIEs coming from the main debug info sections.
5221 ///
5222 /// @param source where the DIEs in the map come from.
5223 ///
5224 /// @return the DIE -> parent map.
5226 die_parent_map(die_source source)
5227 {
5228 switch (source)
5229 {
5230 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5231 break;
5232 case ALT_DEBUG_INFO_DIE_SOURCE:
5233 return alternate_die_parent_map_;
5234 case TYPE_UNIT_DIE_SOURCE:
5235 return type_section_die_parent_map();
5236 case NO_DEBUG_INFO_DIE_SOURCE:
5237 case NUMBER_OF_DIE_SOURCES:
5239 }
5240 return primary_die_parent_map_;
5241 }
5242
5244 type_section_die_parent_map() const
5245 {return type_section_die_parent_map_;}
5246
5248 type_section_die_parent_map()
5249 {return type_section_die_parent_map_;}
5250
5251 /// Getter of the current translation unit.
5252 ///
5253 /// @return the current translation unit being constructed.
5255 cur_transl_unit() const
5256 {return cur_tu_;}
5257
5258 /// Getter of the current translation unit.
5259 ///
5260 /// @return the current translation unit being constructed.
5262 cur_transl_unit()
5263 {return cur_tu_;}
5264
5265 /// Setter of the current translation unit.
5266 ///
5267 /// @param tu the current translation unit being constructed.
5268 void
5269 cur_transl_unit(translation_unit_sptr tu)
5270 {
5271 if (tu)
5272 cur_tu_ = tu;
5273 }
5274
5275 /// Return the global scope of the current translation unit.
5276 ///
5277 /// @return the global scope of the current translation unit.
5278 const scope_decl_sptr&
5279 global_scope() const
5280 {return cur_transl_unit()->get_global_scope();}
5281
5282 /// Return a scope that is nil.
5283 ///
5284 /// @return a scope that is nil.
5285 const scope_decl_sptr&
5286 nil_scope() const
5287 {return nil_scope_;}
5288
5289 const scope_stack_type&
5290 scope_stack() const
5291 {return scope_stack_;}
5292
5294 scope_stack()
5295 {return scope_stack_;}
5296
5297 scope_decl*
5298 current_scope()
5299 {
5300 if (scope_stack().empty())
5301 {
5302 if (cur_transl_unit())
5303 scope_stack().push(cur_transl_unit()->get_global_scope().get());
5304 }
5305 return scope_stack().top();
5306 }
5307
5308 list<var_decl_sptr>&
5309 var_decls_to_re_add_to_tree()
5310 {return var_decls_to_add_;}
5311
5312 /// Test if a DIE represents a decl (function or variable) that has
5313 /// a symbol that is exported, whatever that means. This is
5314 /// supposed to work for Linux Kernel binaries as well.
5315 ///
5316 /// This is useful to limit the amount of DIEs taken into account to
5317 /// the strict limit of what an ABI actually means. Limiting the
5318 /// volume of DIEs analyzed this way is an important optimization to
5319 /// keep big binaries "manageable" by libabigail.
5320 ///
5321 /// @param DIE the die to consider.
5322 bool
5323 is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
5324 {
5325 if (!die || !die_is_decl(die))
5326 return false;
5327
5328 bool result = false, address_found = false, symbol_is_exported = false;;
5329 Dwarf_Addr decl_symbol_address = 0;
5330
5331 if (die_is_variable_decl(die))
5332 {
5333 if ((address_found = get_variable_address(die, decl_symbol_address)))
5334 symbol_is_exported =
5335 !!variable_symbol_is_exported(decl_symbol_address);
5336 }
5337 else if (die_is_function_decl(die))
5338 {
5339 if ((address_found = get_function_address(die, decl_symbol_address)))
5340 symbol_is_exported =
5341 !!function_symbol_is_exported(decl_symbol_address);
5342 }
5343
5344 if (address_found)
5345 result = symbol_is_exported;
5346
5347 return result;
5348 }
5349
5350 /// Test if a DIE is a variable or function DIE which name denotes
5351 /// an undefined ELF symbol.
5352 ///
5353 /// @return true iff @p die represents a function or variable that
5354 /// has an undefined symbol.
5355 bool
5356 is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
5357 {
5358 if (is_decl_die_with_exported_symbol(die))
5359 return false;
5360
5361 string name, linkage_name;
5362 die_name_and_linkage_name(die, name, linkage_name);
5363 if (linkage_name.empty())
5364 linkage_name = name;
5365
5366 bool result = false;
5367 if ((die_is_variable_decl(die)
5368 && symtab()->variable_symbol_is_undefined(linkage_name))
5369 ||
5370 (die_is_function_decl(die)
5371 && symtab()->function_symbol_is_undefined(linkage_name)))
5372 result = true;
5373
5374 return result;
5375 }
5376
5377 /// This is a sub-routine of maybe_adjust_fn_sym_address and
5378 /// maybe_adjust_var_sym_address.
5379 ///
5380 /// Given an address that we got by looking at some debug
5381 /// information (e.g, a symbol's address referred to by a DWARF
5382 /// TAG), If the ELF file we are interested in is a shared library
5383 /// or an executable, then adjust the address to be coherent with
5384 /// where the executable (or shared library) is loaded. That way,
5385 /// the address can be used to look for symbols in the executable or
5386 /// shared library.
5387 ///
5388 /// @return the adjusted address, or the same address as @p addr if
5389 /// it didn't need any adjustment.
5390 Dwarf_Addr
5391 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
5392 {
5393 if (addr == 0)
5394 return addr;
5395
5396 GElf_Ehdr eh_mem;
5397 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5398
5399 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
5400 {
5401 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
5402 if (get_binary_load_address(dwarf_elf_handle(),
5403 dwarf_elf_load_address)
5405 elf_load_address))
5406 if (dwarf_is_splitted()
5407 && (dwarf_elf_load_address != elf_load_address))
5408 // This means that in theory the DWARF and the executable are
5409 // not loaded at the same address. And addr is meaningful
5410 // only in the context of the DWARF.
5411 //
5412 // So let's transform addr into an offset relative to where
5413 // the DWARF is loaded, and let's add that relative offset
5414 // to the load address of the executable. That way, addr
5415 // becomes meaningful in the context of the executable and
5416 // can thus be used to compare against the address of
5417 // symbols of the executable, for instance.
5418 addr = addr - dwarf_elf_load_address + elf_load_address;
5419 }
5420
5421 return addr;
5422 }
5423
5424 /// For a relocatable (*.o) elf file, this function expects an
5425 /// absolute address, representing a function symbol. It then
5426 /// extracts the address of the .text section from the symbol
5427 /// absolute address to get the relative address of the function
5428 /// from the beginning of the .text section.
5429 ///
5430 /// For executable or shared library, this function expects an
5431 /// address of a function symbol that was retrieved by looking at a
5432 /// DWARF "file". The function thus adjusts the address to make it
5433 /// be meaningful in the context of the ELF file.
5434 ///
5435 /// In both cases, the address can then be compared against the
5436 /// st_value field of a function symbol from the ELF file.
5437 ///
5438 /// @param addr an adress for a function symbol that was retrieved
5439 /// from a DWARF file.
5440 ///
5441 /// @return the (possibly) adjusted address, or just @p addr if no
5442 /// adjustment took place.
5443 Dwarf_Addr
5444 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
5445 {
5446 if (addr == 0)
5447 return addr;
5448
5449 Elf* elf = elf_handle();
5450 GElf_Ehdr eh_mem;
5451 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5452
5453 if (elf_header->e_type == ET_REL)
5454 // We are looking at a relocatable file. In this case, we don't
5455 // do anything because:
5456 //
5457 // 1/ the addresses from DWARF are absolute (relative to the
5458 // beginning of the relocatable file)
5459 //
5460 // 2/ The ELF symbol addresses that we store in our lookup
5461 // tables are translated from section-related to absolute as
5462 // well. So we don't have anything to do at this point for
5463 // ET_REL files.
5464 ;
5465 else
5466 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5467
5468 return addr;
5469 }
5470
5471 /// For a relocatable (*.o) elf file, this function expects an
5472 /// absolute address, representing a global variable symbol. It
5473 /// then extracts the address of the {.data,.data1,.rodata,.bss}
5474 /// section from the symbol absolute address to get the relative
5475 /// address of the variable from the beginning of the data section.
5476 ///
5477 /// For executable or shared library, this function expects an
5478 /// address of a variable symbol that was retrieved by looking at a
5479 /// DWARF "file". The function thus adjusts the address to make it
5480 /// be meaningful in the context of the ELF file.
5481 ///
5482 /// In both cases, the address can then be compared against the
5483 /// st_value field of a function symbol from the ELF file.
5484 ///
5485 /// @param addr an address for a global variable symbol that was
5486 /// retrieved from a DWARF file.
5487 ///
5488 /// @return the (possibly) adjusted address, or just @p addr if no
5489 /// adjustment took place.
5490 Dwarf_Addr
5491 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5492 {
5493 Elf* elf = elf_handle();
5494 GElf_Ehdr eh_mem;
5495 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5496
5497 if (elf_header->e_type == ET_REL)
5498 // We are looking at a relocatable file. In this case, we don't
5499 // do anything because:
5500 //
5501 // 1/ the addresses from DWARF are absolute (relative to the
5502 // beginning of the relocatable file)
5503 //
5504 // 2/ The ELF symbol addresses that we store in our lookup
5505 // tables are translated from section-related to absolute as
5506 // well. So we don't have anything to do at this point for
5507 // ET_REL files.
5508 ;
5509 else
5510 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5511
5512 return addr;
5513 }
5514
5515 /// Get the first exported function address in the set of addresses
5516 /// referred to by the DW_AT_ranges attribute of a given DIE.
5517 ///
5518 /// @param die the DIE we are considering.
5519 ///
5520 /// @param address output parameter. This is set to the first
5521 /// address found in the sequence pointed to by the DW_AT_ranges
5522 /// attribute found on the DIE @p die, iff the function returns
5523 /// true. Otherwise, no value is set into this output parameter.
5524 ///
5525 /// @return true iff the DIE @p die does have a DW_AT_ranges
5526 /// attribute and an address of an exported function was found in
5527 /// its sequence value.
5528 bool
5529 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5530 Dwarf_Addr& address) const
5531 {
5532 Dwarf_Addr base;
5533 Dwarf_Addr end_addr;
5534 ptrdiff_t offset = 0;
5535
5536 do
5537 {
5538 Dwarf_Addr addr = 0, fn_addr = 0;
5539 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5540 {
5541 fn_addr = maybe_adjust_fn_sym_address(addr);
5542 if (function_symbol_is_exported(fn_addr))
5543 {
5544 address = fn_addr;
5545 return true;
5546 }
5547 }
5548 } while (offset > 0);
5549 return false;
5550 }
5551
5552 /// Get the address of the function.
5553 ///
5554 /// The address of the function is considered to be the value of the
5555 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5556 /// only) to not point to an absolute address anymore, but rather to
5557 /// the address of the function inside the .text segment.
5558 ///
5559 /// @param function_die the die of the function to consider.
5560 ///
5561 /// @param address the resulting address iff the function returns
5562 /// true.
5563 ///
5564 /// @return true if the function address was found.
5565 bool
5566 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5567 {
5568 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5569 DW_AT_low_pc, address))
5570 // So no DW_AT_low_pc was found. Let's see if the function DIE
5571 // has got a DW_AT_ranges attribute instead. If it does, the
5572 // first address of the set of addresses represented by the
5573 // value of that DW_AT_ranges represents the function (symbol)
5574 // address we are looking for.
5575 if (!get_first_exported_fn_address_from_DW_AT_ranges
5576 (const_cast<Dwarf_Die*>(function_die),
5577 address))
5578 return false;
5579
5580 address = maybe_adjust_fn_sym_address(address);
5581 return true;
5582 }
5583
5584 /// Get the address of the global variable.
5585 ///
5586 /// The address of the global variable is considered to be the value
5587 /// of the DW_AT_location attribute, possibly adjusted (in
5588 /// relocatable files only) to not point to an absolute address
5589 /// anymore, but rather to the address of the global variable inside
5590 /// the data segment.
5591 ///
5592 /// @param variable_die the die of the function to consider.
5593 ///
5594 /// @param address the resulting address iff this function returns
5595 /// true.
5596 ///
5597 /// @return true if the variable address was found.
5598 bool
5599 get_variable_address(const Dwarf_Die* variable_die,
5600 Dwarf_Addr& address) const
5601 {
5602 bool is_tls_address = false;
5603 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5604 address, is_tls_address))
5605 return false;
5606 if (!is_tls_address)
5607 address = maybe_adjust_var_sym_address(address);
5608 return true;
5609 }
5610
5611 /// Getter of the exported decls builder object.
5612 ///
5613 /// @return the exported decls builder.
5614 corpus::exported_decls_builder*
5615 exported_decls_builder()
5616 {return corpus()->get_exported_decls_builder().get();}
5617
5618 /// Getter of the "load_all_types" flag. This flag tells if all the
5619 /// types (including those not reachable by public declarations) are
5620 /// to be read and represented in the final ABI corpus.
5621 ///
5622 /// @return the load_all_types flag.
5623 bool
5624 load_all_types() const
5625 {return options().load_all_types;}
5626
5627 /// Setter of the "load_all_types" flag. This flag tells if all the
5628 /// types (including those not reachable by public declarations) are
5629 /// to be read and represented in the final ABI corpus.
5630 ///
5631 /// @param f the new load_all_types flag.
5632 void
5633 load_all_types(bool f)
5634 {options().load_all_types = f;}
5635
5636 bool
5637 load_in_linux_kernel_mode() const
5638 {return options().load_in_linux_kernel_mode;}
5639
5640 void
5641 load_in_linux_kernel_mode(bool f)
5642 {options().load_in_linux_kernel_mode = f;}
5643
5644 /// Getter of the 'load-undefined-interface' property.
5645 ///
5646 /// That property tells the reader if it should load the interfaces
5647 /// that are undefined in the binary. An undefined interface is a
5648 /// variable or function which has a symbol that is not defined in
5649 /// the binary.
5650 ///
5651 /// @return true iff the front-end has to load the undefined
5652 /// interfaces.
5653 bool
5654 load_undefined_interfaces() const
5656
5657 /// Test if it's allowed to assume that the DWARF debug info has
5658 /// been factorized (for instance, with the DWZ tool) so that if two
5659 /// type DIEs originating from the .gnu_debugaltlink section have
5660 /// different offsets, they represent different types.
5661 ///
5662 /// @return true iff we can assume that the DWARF debug info has
5663 /// been factorized.
5664 bool
5665 leverage_dwarf_factorization() const
5666 {
5667 if (!leverage_dwarf_factorization_.has_value())
5668 {
5669 if (options().leverage_dwarf_factorization
5670 && elf_helpers::find_section_by_name(elf_handle(),
5671 ".gnu_debugaltlink"))
5672 leverage_dwarf_factorization_ = true;
5673 else
5674 leverage_dwarf_factorization_ = false;
5675 }
5676 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5677
5678 return *leverage_dwarf_factorization_;
5679 }
5680
5681 /// Getter of the "show_stats" flag.
5682 ///
5683 /// This flag tells if we should emit statistics about various
5684 /// internal stuff.
5685 ///
5686 /// @return the value of the flag.
5687 bool
5688 show_stats() const
5689 {return options().show_stats;}
5690
5691 /// Setter of the "show_stats" flag.
5692 ///
5693 /// This flag tells if we should emit statistics about various
5694 /// internal stuff.
5695 ///
5696 /// @param f the value of the flag.
5697 void
5698 show_stats(bool f)
5699 {options().show_stats = f;}
5700
5701 /// Getter of the "do_log" flag.
5702 ///
5703 /// This flag tells if we should log about various internal
5704 /// details.
5705 ///
5706 /// return the "do_log" flag.
5707 bool
5708 do_log() const
5709 {return options().do_log;}
5710
5711 /// Setter of the "do_log" flag.
5712 ///
5713 /// This flag tells if we should log about various internal details.
5714 ///
5715 /// @param f the new value of the flag.
5716 void
5717 do_log(bool f)
5718 {options().do_log = f;}
5719
5720 /// Walk the DIEs under a given die and for each child, populate the
5721 /// die -> parent map to record the child -> parent relationship
5722 /// that
5723 /// exists between the child and the given die.
5724 ///
5725 /// The function also builds the vector of places where units are
5726 /// imported.
5727 ///
5728 /// This is done recursively as for each child DIE, this function
5729 /// walks its children as well.
5730 ///
5731 /// @param die the DIE whose children to walk recursively.
5732 ///
5733 /// @param source where the DIE @p die comes from.
5734 ///
5735 /// @param imported_units a vector containing all the offsets of the
5736 /// points where unit have been imported, under @p die.
5737 void
5738 build_die_parent_relations_under(Dwarf_Die* die,
5739 die_source source,
5740 imported_unit_points_type & imported_units)
5741 {
5742 if (!die)
5743 return;
5744
5745 offset_offset_map_type& parent_of = die_parent_map(source);
5746
5747 Dwarf_Die child;
5748 if (dwarf_child(die, &child) != 0)
5749 return;
5750
5751 do
5752 {
5753 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5754 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5755 {
5756 Dwarf_Die imported_unit;
5757 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5758 // If the imported_unit has a sub-tree, let's record
5759 // this point at which the sub-tree is imported into
5760 // the current debug info.
5761 //
5762 // Otherwise, if the imported_unit has no sub-tree,
5763 // there is no point in recording where a non-existent
5764 // sub-tree is being imported.
5765 //
5766 // Note that the imported_unit_points_type type below
5767 // expects the imported_unit to have a sub-tree.
5768 && die_has_children(&imported_unit))
5769 {
5770 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5771 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5772 imported_units.push_back
5773 (imported_unit_point(dwarf_dieoffset(&child),
5774 imported_unit,
5775 imported_unit_die_source));
5776 }
5777 }
5778 build_die_parent_relations_under(&child, source, imported_units);
5779 }
5780 while (dwarf_siblingof(&child, &child) == 0);
5781
5782 }
5783
5784 /// Determine if we do have to build a DIE -> parent map, depending
5785 /// on a given language.
5786 ///
5787 /// Some languages like C++, Ada etc, do have the concept of
5788 /// namespace and yet, the DIE data structure doesn't provide us
5789 /// with a way to get the parent namespace of a given DIE. So for
5790 /// those languages, we need to build a DIE -> parent map so that we
5791 /// can get the namespace DIE (or more generally the scope DIE) of a given
5792 /// DIE as we need it.
5793 ///
5794 /// But then some more basic languages like C or assembly don't have
5795 /// that need.
5796 ///
5797 /// This function, depending on the language, tells us if we need to
5798 /// build the DIE -> parent map or not.
5799 ///
5800 /// @param lang the language to consider.
5801 ///
5802 /// @return true iff we need to build the DIE -> parent map for this
5803 /// language.
5804 bool
5805 do_we_build_die_parent_maps(translation_unit::language lang)
5806 {
5807 if (is_c_language(lang))
5808 return false;
5809
5810 switch (lang)
5811 {
5812 case translation_unit::LANG_UNKNOWN:
5813#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5814 case translation_unit::LANG_Mips_Assembler:
5815#endif
5816 return false;
5817 default:
5818 break;
5819 }
5820 return true;
5821 }
5822
5823 /// Walk all the DIEs accessible in the debug info (and in the
5824 /// alternate debug info as well) and build maps representing the
5825 /// relationship DIE -> parent. That is, make it so that we can get
5826 /// the parent for a given DIE.
5827 ///
5828 /// Note that the goal of this map is to be able to get the parent
5829 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5830 /// when we get a DIE of a type, and we want to build an internal
5831 /// representation for it, we need to get its fully qualified name.
5832 /// For that, we need to know what is the parent DIE of that type
5833 /// DIE, so that we can know what the namespace of that type is.
5834 ///
5835 /// Note that as the C language doesn't have namespaces (all types
5836 /// are defined in the same global namespace), this function doesn't
5837 /// build the DIE -> parent map if the current translation unit
5838 /// comes from C. This saves time on big C ELF files with a lot of
5839 /// DIEs.
5840 void
5841 build_die_parent_maps()
5842 {
5843 bool we_do_have_to_build_die_parent_map = false;
5844 uint8_t address_size = 0;
5845 size_t header_size = 0;
5846 // Get the DIE of the current translation unit, look at it to get
5847 // its language. If that language is in C, then all types are in
5848 // the global namespace so we don't need to build the DIE ->
5849 // parent map. So we dont build it in that case.
5850 for (Dwarf_Off offset = 0, next_offset = 0;
5851 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5852 offset, &next_offset, &header_size,
5853 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5854 offset = next_offset)
5855 {
5856 Dwarf_Off die_offset = offset + header_size;
5857 Dwarf_Die cu;
5858 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5859 die_offset, &cu))
5860 continue;
5861
5862 uint64_t l = 0;
5863 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5864 translation_unit::language lang = dwarf_language_to_tu_language(l);
5865 if (do_we_build_die_parent_maps(lang))
5866 we_do_have_to_build_die_parent_map = true;
5867 }
5868
5869 if (!we_do_have_to_build_die_parent_map)
5870 return;
5871
5872 // Build the DIE -> parent relation for DIEs coming from the
5873 // .debug_info section in the alternate debug info file.
5874 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5875 for (Dwarf_Off offset = 0, next_offset = 0;
5876 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5877 offset, &next_offset, &header_size,
5878 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5879 offset = next_offset)
5880 {
5881 Dwarf_Off die_offset = offset + header_size;
5882 Dwarf_Die cu;
5883 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5884 die_offset, &cu))
5885 continue;
5886 cur_tu_die(&cu);
5887
5888 imported_unit_points_type& imported_units =
5889 tu_die_imported_unit_points_map(source)[die_offset] =
5891 build_die_parent_relations_under(&cu, source, imported_units);
5892 }
5893
5894 // Build the DIE -> parent relation for DIEs coming from the
5895 // .debug_info section of the main debug info file.
5896 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5897 address_size = 0;
5898 header_size = 0;
5899 for (Dwarf_Off offset = 0, next_offset = 0;
5900 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5901 offset, &next_offset, &header_size,
5902 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5903 offset = next_offset)
5904 {
5905 Dwarf_Off die_offset = offset + header_size;
5906 Dwarf_Die cu;
5907 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5908 die_offset, &cu))
5909 continue;
5910 cur_tu_die(&cu);
5911 imported_unit_points_type& imported_units =
5912 tu_die_imported_unit_points_map(source)[die_offset] =
5914 build_die_parent_relations_under(&cu, source, imported_units);
5915 }
5916
5917 // Build the DIE -> parent relation for DIEs coming from the
5918 // .debug_types section.
5919 source = TYPE_UNIT_DIE_SOURCE;
5920 address_size = 0;
5921 header_size = 0;
5922 uint64_t type_signature = 0;
5923 Dwarf_Off type_offset;
5924 for (Dwarf_Off offset = 0, next_offset = 0;
5925 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5926 offset, &next_offset, &header_size,
5927 NULL, NULL, &address_size, NULL,
5928 &type_signature, &type_offset) == 0);
5929 offset = next_offset)
5930 {
5931 Dwarf_Off die_offset = offset + header_size;
5932 Dwarf_Die cu;
5933
5934 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5935 die_offset, &cu))
5936 continue;
5937 cur_tu_die(&cu);
5938 imported_unit_points_type& imported_units =
5939 tu_die_imported_unit_points_map(source)[die_offset] =
5941 build_die_parent_relations_under(&cu, source, imported_units);
5942 }
5943 }
5944};// end class reader.
5945
5946/// The type of the aggregates being compared during a DIE comparison.
5947///
5948/// This encapsulates the stack of aggregates being compared at any
5949/// single point.
5950///
5951/// This is useful to detect "comparison cycles" and thus avoid the
5952/// resulting infinite loops.
5953///
5954/// This is also useful for implementing a very important optimization
5955/// that takes place during the canonicalization
5956struct offset_pairs_stack_type
5957{
5958 // The DWARF DWARF reader that is useful for so many things.
5959 const reader& rdr_;
5960 // The set of types that are being compared. This is to speed up
5961 // searches.
5963 // The stack of types that are being compared. The top of the
5964 // stack is the back of the vector.
5966 // A map that associates a redundant type pair to the vector of
5967 // types that depends on it.
5968 offset_pair_vect_map_type redundant_types_;
5969 // A map that associates a dependant type to the vector of redundant
5970 // types it depends on.
5971 offset_pair_vect_map_type dependant_types_;
5972
5973 offset_pairs_stack_type(const reader& rdr)
5974 : rdr_ (rdr)
5975 {}
5976
5977 /// Add a pair of types being compared to the stack of aggregates
5978 /// being compared.
5979 ///
5980 /// @param p the pair of offsets of the type DIEs to consider.
5981 void
5982 add(const offset_pair_type& p)
5983 {
5984 set_.insert(p);
5985 vect_.push_back(p);
5986 }
5987
5988 /// Erase a pair of types being compared from the stack of
5989 /// aggregates being compared.
5990 ///
5991 /// @param p the pair of offsets of the type DIEs to consider.
5992 ///
5993 /// @return true iff @p was found and erased from the stack.
5994 bool
5995 erase(const offset_pair_type& p)
5996 {
5997 if (set_.erase(p))
5998 {
5999 offset_pair_vector_type::iterator i;
6000
6001 for (i = vect_.begin();i < vect_.end(); ++i)
6002 if (*i == p)
6003 break;
6004
6005 if (i != vect_.end())
6006 vect_.erase(i);
6007
6008 return true;
6009 }
6010
6011 return false;
6012 }
6013
6014 /// Test if a pair of type DIEs is part of the stack of type DIEs
6015 /// being compared.
6016 ///
6017 /// @param p the pair of offsets of the type DIEs to consider.
6018 ///
6019 /// @return true iff @p was found in the stack of types being
6020 /// compared.
6021 bool
6022 contains(const offset_pair_type &p) const
6023 {
6024 if (set_.find(p) == set_.end())
6025 return false;
6026 return true;
6027 }
6028
6029 /// Get the set of comparison pair that depends on a given
6030 /// comparison pair.
6031 ///
6032 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
6033 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
6034 /// words, the pair T appears in the comparison stack BEFORE the
6035 /// pair P.
6036 ///
6037 /// So, this function returns the vector of comparison pairs that
6038 /// appear in the comparison stack AFTER a given comparison pair.
6039 ///
6040 /// @param p the comparison pair to consider.
6041 ///
6042 /// @param pairs out parameter. This is filled with the comparison
6043 /// pairs that depend on @p, iff the function returns true.
6044 ///
6045 /// @return true iff comparison pairs depending on @p have been
6046 /// found and collected in @pairs.
6047 bool
6048 get_pairs_that_depend_on(const offset_pair_type& p,
6049 offset_pair_vector_type& pairs) const
6050 {
6051 bool result = false;
6052 if (!contains(p))
6053 return result;
6054
6055 // First, get an iterator on the position of 'p'.
6056 offset_pair_vector_type::const_iterator i;
6057 for (i = vect_.begin(); i != vect_.end(); ++i)
6058 if (*i == p)
6059 break;
6060
6061 if (i == vect_.end())
6062 return result;
6063
6064 // Then, harvest all the comparison pairs that come after the
6065 // position of 'p'.
6066 for (++i; i != vect_.end(); ++i)
6067 {
6068 pairs.push_back(*i);
6069 result = true;
6070 }
6071
6072 return result;
6073 }
6074
6075 /// Record the fact that a set of comparison pairs depends on a
6076 /// given comparison pair.
6077 ///
6078 /// Set a map that associates each dependant comparison pair to the
6079 /// pair it depends on.
6080 ///
6081 /// @param p the comparison pair that the set depends on.
6082 ///
6083 /// @param dependant_types the set of types that depends on @p.
6084 void
6085 record_dependant_types(const offset_pair_type& p,
6086 const offset_pair_vector_type& dependant_types)
6087 {
6088 for (auto type_pair : dependant_types)
6089 dependant_types_[type_pair].push_back(p);
6090 }
6091
6092 /// Record a comparison pair as being redundant.
6093 ///
6094 ///
6095 /// @param p the comparison pair to record as redundant.
6096 void
6097 record_redundant_type_die_pair(const offset_pair_type& p)
6098 {
6099 offset_pair_vector_type dependant_types;
6100 get_pairs_that_depend_on(p, dependant_types);
6101
6102 // First, record the relationship "p -> [pairs that depend on p]".
6103 auto it = redundant_types_.find(p);
6104 if (it == redundant_types_.end())
6105 {
6106 auto entry = std::make_pair(p, dependant_types);
6107 redundant_types_.insert(entry);
6108 }
6109 else
6110 it->second.insert(it->second.end(),
6111 dependant_types.begin(),
6112 dependant_types.end());
6113
6114 // For each dependant type pair, record the association:
6115 // dependant_pair --> [vect of redundant types]
6116 record_dependant_types(p, dependant_types);
6117 }
6118
6119 /// Test if a given pair has been detected as redundant.
6120 ///
6121 /// @param p the pair of DIEs to consider.
6122 ///
6123 /// @return iff @p is redundant.
6124 bool
6125 is_redundant(const offset_pair_type& p)
6126 {
6127 auto i = redundant_types_.find(p);
6128 if (i != redundant_types_.end())
6129 return true;
6130 return false;
6131 }
6132
6133 /// Test if a given pair is dependant on at least a redundant type.
6134 ///
6135 /// @param p the pair to consider.
6136 ///
6137 /// @return true iff @p depends on a redundant type.
6138 bool
6139 depends_on_redundant_types(const offset_pair_type& p)
6140 {
6141 auto i = dependant_types_.find(p);
6142 if (i == dependant_types_.end())
6143 return false;
6144 return true;
6145 }
6146
6147 /// Remove a redundant pair from the system.
6148 ///
6149 /// This needs updating the system to also remove the dependant
6150 /// types that depend on the redundant pair (if they depend only on
6151 /// that redundant pair).
6152 ///
6153 /// @param p the pair to consider.
6154 ///
6155 /// @param erase_canonical_die_offset if true then erase the cached
6156 /// comparison results for the redundant pair and its dependant
6157 /// types.
6158 void
6159 erase_redundant_type_pair_entry(const offset_pair_type& p,
6160 bool erase_cached_results = false)
6161 {
6162 // First, update the dependant types that depend on the redundant
6163 // type pair
6164 auto redundant_type = redundant_types_.find(p);
6165 if (redundant_type != redundant_types_.end())
6166 {
6167 for (auto dependant_type : redundant_type->second)
6168 {
6169 // Each dependant_type depends on the redundant type 'p',
6170 // among others.
6171 auto dependant_types_it = dependant_types_.find(dependant_type);
6172 ABG_ASSERT(dependant_types_it != dependant_types_.end());
6173 // Erase the redundant type 'p' from the redundant types
6174 // that dependant_type depends on.
6175 {
6176 auto i = dependant_types_it->second.begin();
6177 for (; i!= dependant_types_it->second.end();++i)
6178 if (*i == p)
6179 break;
6180 if (i != dependant_types_it->second.end())
6181 dependant_types_it->second.erase(i);
6182 }
6183 // If the dependant type itself doesn't depend on ANY
6184 // redundant type anymore, then remove the depend type
6185 // from the map of the dependant types.
6186 if (dependant_types_it->second.empty())
6187 {
6188 if (erase_cached_results)
6189 rdr_.die_comparison_results_.erase(dependant_type);
6190 dependant_types_.erase(dependant_types_it);
6191 }
6192 }
6193 }
6194 if (erase_cached_results)
6195 rdr_.die_comparison_results_.erase(p);
6196 redundant_types_.erase(p);
6197 }
6198
6199 /// If a comparison pair has been detected as redundant, stop
6200 /// tracking it as well as its dependant pairs. That will
6201 /// essentially make it impossible to reset/cancel the canonical
6202 /// propagated types for those depdant pairs, but will also save
6203 /// ressources.
6204 ///
6205 /// @param p the comparison pair to consider.
6206 void
6207 confirm_canonical_propagated_type(const offset_pair_type& p)
6208 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
6209
6210 /// Walk the types that depend on a comparison pair and cancel their
6211 /// canonical-propagate-type, that means remove their canonical
6212 /// types and mark them as not being canonically-propagated. Also,
6213 /// erase their cached comparison results that was likely set to
6214 /// COMPARISON_RESULT_UNKNOWN.
6215 ///
6216 /// @param p the pair to consider.
6217 void
6218 cancel_canonical_propagated_type(const offset_pair_type& p)
6219 {
6220 offset_pair_set_type dependant_types;
6221 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
6222 for (auto dependant_type : dependant_types)
6223 {
6224 // If this dependant type was canonical-type-propagated then
6225 // erase that canonical type.
6226 if (rdr_.propagated_types_.find(dependant_type)
6227 != rdr_.propagated_types_.end())
6228 {
6229 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
6230 dependant_type.first.source_,
6231 /*die_as_type=*/true);
6232 rdr_.propagated_types_.erase(dependant_type);
6233 rdr_.cancelled_propagation_count_++;
6234 }
6235 // Update the cached result. We know the comparison result
6236 // must now be different.
6237 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
6238 if (comp_result_it != rdr_.die_comparison_results_.end())
6239 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6240 }
6241
6242 // Update the cached result of the root type to cancel too.
6243 auto comp_result_it = rdr_.die_comparison_results_.find(p);
6244 if (comp_result_it != rdr_.die_comparison_results_.end())
6245 {
6246 // At this point, the result of p is either
6247 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
6248 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
6249 // Make sure it's the cached result is now
6250 // COMPARISON_RESULT_DIFFERENT.
6251 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
6252 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6253 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
6254 }
6255
6256 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
6257 {
6258 rdr_.erase_canonical_die_offset(p.first.offset_,
6259 p.first.source_,
6260 /*die_as_type=*/true);
6261 rdr_.propagated_types_.erase(p);
6262 rdr_.cancelled_propagation_count_++;
6263 }
6264 }
6265
6266 /// Get the set of comparison pairs that depend on a given pair.
6267 ///
6268 /// @param p the pair to consider.
6269 ///
6270 /// @param result this is set to the pairs that depend on @p, iff
6271 /// the function returned true.
6272 ///
6273 /// @param transitive_closure if set to true, the transitive closure
6274 /// of the @result is set to it.
6275 ///
6276 /// @return true iff @result could be filled with the dependant
6277 /// types.
6278 bool
6279 get_dependant_types(const offset_pair_type& p,
6280 offset_pair_set_type& result,
6281 bool transitive_closure = false)
6282 {
6283 auto i = redundant_types_.find(p);
6284 if (i != redundant_types_.end())
6285 {
6286 for (auto dependant_type : i->second)
6287 if (result.find(dependant_type) == result.end())
6288 {
6289 result.insert(dependant_type);
6290 if (transitive_closure)
6291 get_dependant_types(p, result, /*transitive_closure=*/true);
6292 }
6293 return true;
6294 }
6295 return false;
6296 }
6297}; // end struct offset_pairs_stack_type
6298
6300build_ir_node_from_die(reader& rdr,
6301 Dwarf_Die* die,
6302 scope_decl* scope,
6303 bool called_from_public_decl,
6304 size_t where_offset,
6305 bool is_declaration_only = true,
6306 bool is_required_decl_spec = false);
6307
6309build_ir_node_from_die(reader& rdr,
6310 Dwarf_Die* die,
6311 bool called_from_public_decl,
6312 size_t where_offset);
6313
6314static decl_base_sptr
6315build_ir_node_for_void_type(reader& rdr);
6316
6318build_ir_node_for_void_pointer_type(reader& rdr);
6319
6320static class_decl_sptr
6321add_or_update_class_type(reader& rdr,
6322 Dwarf_Die* die,
6323 scope_decl* scope,
6324 bool is_struct,
6325 class_decl_sptr klass,
6326 bool called_from_public_decl,
6327 size_t where_offset,
6328 bool is_declaration_only);
6329
6330static union_decl_sptr
6331add_or_update_union_type(reader& rdr,
6332 Dwarf_Die* die,
6333 scope_decl* scope,
6334 union_decl_sptr union_type,
6335 bool called_from_public_decl,
6336 size_t where_offset,
6337 bool is_declaration_only);
6338
6339static decl_base_sptr
6340build_ir_node_for_void_type(reader& rdr);
6341
6342static decl_base_sptr
6343build_ir_node_for_variadic_parameter_type(reader &rdr);
6344
6345static function_decl_sptr
6346build_function_decl(reader& rdr,
6347 Dwarf_Die* die,
6348 size_t where_offset,
6350
6351static bool
6352function_is_suppressed(const reader& rdr,
6353 const scope_decl* scope,
6354 Dwarf_Die *function_die,
6355 bool is_declaration_only);
6356
6357static function_decl_sptr
6358build_or_get_fn_decl_if_not_suppressed(reader& rdr,
6359 scope_decl *scope,
6360 Dwarf_Die *die,
6361 size_t where_offset,
6362 bool is_declaration_only,
6364
6365static var_decl_sptr
6366build_var_decl(reader& rdr,
6367 Dwarf_Die *die,
6368 size_t where_offset,
6369 var_decl_sptr result = var_decl_sptr());
6370
6371static var_decl_sptr
6372build_or_get_var_decl_if_not_suppressed(reader& rdr,
6373 scope_decl *scope,
6374 Dwarf_Die *die,
6375 size_t where_offset,
6376 bool is_declaration_only,
6378 bool is_required_decl_spec = false);
6379static bool
6380variable_is_suppressed(const reader& rdr,
6381 const scope_decl* scope,
6382 Dwarf_Die *variable_die,
6383 bool is_declaration_only,
6384 bool is_required_decl_spec = false);
6385
6386static void
6387finish_member_function_reading(Dwarf_Die* die,
6388 const function_decl_sptr& f,
6389 const class_or_union_sptr klass,
6390 reader& rdr);
6391
6392/// Test if a given DIE is anonymous
6393///
6394/// @param die the DIE to consider.
6395///
6396/// @return true iff @p die is anonymous.
6397static bool
6398die_is_anonymous(const Dwarf_Die* die)
6399{
6400 Dwarf_Attribute attr;
6401 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
6402 return true;
6403 return false;
6404}
6405
6406/// Test if a DIE is an anonymous data member, aka, "unnamed field".
6407///
6408/// Unnamed fields are specified at
6409/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
6410///
6411/// @param die the DIE to consider.
6412///
6413/// @return true iff @p die is an anonymous data member.
6414static bool
6415die_is_anonymous_data_member(const Dwarf_Die* die)
6416{
6417 if (!die
6418 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
6419 || !die_name(die).empty())
6420 return false;
6421
6422 Dwarf_Die type_die;
6423 if (!die_die_attribute(die, DW_AT_type, type_die))
6424 return false;
6425
6426 if (dwarf_tag(&type_die) != DW_TAG_structure_type
6427 && dwarf_tag(&type_die) != DW_TAG_union_type)
6428 return false;
6429
6430 return true;
6431}
6432
6433/// Get the value of an attribute that is supposed to be a string, or
6434/// an empty string if the attribute could not be found.
6435///
6436/// @param die the DIE to get the attribute value from.
6437///
6438/// @param attr_name the attribute name. Must come from dwarf.h and
6439/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6440///
6441/// @return the string representing the value of the attribute, or an
6442/// empty string if no string attribute could be found.
6443static string
6444die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
6445{
6446 if (!die)
6447 return "";
6448
6449 Dwarf_Attribute attr;
6450 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6451 return "";
6452
6453 const char* str = dwarf_formstring(&attr);
6454 return str ? str : "";
6455}
6456
6457/// Get the value of an attribute that is supposed to be a string, or
6458/// an empty string if the attribute could not be found.
6459///
6460/// @param die the DIE to get the attribute value from.
6461///
6462/// @param attr_name the attribute name. Must come from dwarf.h and
6463/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6464///
6465/// @return the char* representing the value of the attribute, or an
6466/// empty string if no string attribute could be found.
6467static const char*
6468die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
6469{
6470 if (!die)
6471 return nullptr;
6472
6473 Dwarf_Attribute attr;
6474 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6475 return nullptr;
6476
6477 const char* str = dwarf_formstring(&attr);
6478 return str;
6479}
6480
6481/// Get the value of an attribute that is supposed to be an unsigned
6482/// constant.
6483///
6484/// @param die the DIE to read the information from.
6485///
6486/// @param attr_name the DW_AT_* name of the attribute. Must come
6487/// from dwarf.h and be an enumerator representing an attribute like,
6488/// e.g, DW_AT_decl_line.
6489///
6490///@param cst the output parameter that is set to the value of the
6491/// attribute @p attr_name. This parameter is set iff the function
6492/// return true.
6493///
6494/// @return true if there was an attribute of the name @p attr_name
6495/// and with a value that is a constant, false otherwise.
6496static bool
6497die_unsigned_constant_attribute(const Dwarf_Die* die,
6498 unsigned attr_name,
6499 uint64_t& cst)
6500{
6501 if (!die)
6502 return false;
6503
6504 Dwarf_Attribute attr;
6505 Dwarf_Word result = 0;
6506 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6507 || dwarf_formudata(&attr, &result))
6508 return false;
6509
6510 cst = result;
6511 return true;
6512}
6513
6514/// Read a signed constant value from a given attribute.
6515///
6516/// The signed constant expected must be of constant form.
6517///
6518/// @param die the DIE to get the attribute from.
6519///
6520/// @param attr_name the attribute name.
6521///
6522/// @param cst the resulting signed constant read.
6523///
6524/// @return true iff a signed constant attribute of the name @p
6525/// attr_name was found on the DIE @p die.
6526static bool
6527die_signed_constant_attribute(const Dwarf_Die *die,
6528 unsigned attr_name,
6529 int64_t& cst)
6530{
6531 if (!die)
6532 return false;
6533
6534 Dwarf_Attribute attr;
6535 Dwarf_Sword result = 0;
6536 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6537 || dwarf_formsdata(&attr, &result))
6538 return false;
6539
6540 cst = result;
6541 return true;
6542}
6543
6544/// Read the value of a constant attribute that is either signed or
6545/// unsigned into a array_type_def::subrange_type::bound_value value.
6546///
6547/// The bound_value instance will capture the actual signedness of the
6548/// read attribute.
6549///
6550/// @param die the DIE from which to read the value of the attribute.
6551///
6552/// @param attr_name the attribute name to consider.
6553///
6554/// @param is_signed true if the attribute value has to read as
6555/// signed.
6556///
6557/// @param value the resulting value read from attribute @p attr_name
6558/// on DIE @p die.
6559///
6560/// @return true iff DIE @p die has an attribute named @p attr_name
6561/// with a constant value.
6562static bool
6563die_constant_attribute(const Dwarf_Die *die,
6564 unsigned attr_name,
6565 bool is_signed,
6566 array_type_def::subrange_type::bound_value &value)
6567{
6568 if (!is_signed)
6569 {
6570 uint64_t l = 0;
6571 if (!die_unsigned_constant_attribute(die, attr_name, l))
6572 return false;
6573 value.set_unsigned(l);
6574 }
6575 else
6576 {
6577 int64_t l = 0;
6578 if (!die_signed_constant_attribute(die, attr_name, l))
6579 return false;
6580 value.set_signed(l);
6581 }
6582 return true;
6583}
6584
6585/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6586///
6587/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6588/// enum in dwarf.h so we have to use an unsigned int for the form,
6589/// grrr.
6590///
6591/// @param form the form to consider.
6592///
6593/// @return true iff @p form is DW_FORM_strx{1,4}.
6594static bool
6595form_is_DW_FORM_strx(unsigned form)
6596{
6597 if (form)
6598 {
6599#if defined HAVE_DW_FORM_strx1 \
6600 && defined HAVE_DW_FORM_strx2 \
6601 && defined HAVE_DW_FORM_strx3 \
6602 && defined HAVE_DW_FORM_strx4
6603 if (form == DW_FORM_strx1
6604 || form == DW_FORM_strx2
6605 || form == DW_FORM_strx3
6606 ||form == DW_FORM_strx4)
6607 return true;
6608#endif
6609 }
6610 return false;
6611}
6612
6613/// Test if a given DWARF form is DW_FORM_line_strp.
6614///
6615/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6616/// enum in dwarf.h so we have to use an unsigned int for the form,
6617/// grrr.
6618///
6619/// @param form the form to consider.
6620///
6621/// @return true iff @p form is DW_FORM_line_strp.
6622static bool
6623form_is_DW_FORM_line_strp(unsigned form)
6624{
6625 if (form)
6626 {
6627#if defined HAVE_DW_FORM_line_strp
6628 if (form == DW_FORM_line_strp)
6629 return true;
6630#endif
6631 }
6632 return false;
6633}
6634
6635/// Get the value of a DIE attribute; that value is meant to be a
6636/// flag.
6637///
6638/// @param die the DIE to get the attribute from.
6639///
6640/// @param attr_name the DW_AT_* name of the attribute. Must come
6641/// from dwarf.h and be an enumerator representing an attribute like,
6642/// e.g, DW_AT_external.
6643///
6644/// @param flag the output parameter to store the flag value into.
6645/// This is set iff the function returns true.
6646///
6647/// @param recursively if true, the function looks through the
6648/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6649/// all the way down to the initial DIE that is cloned and look on
6650/// that DIE to see if it has the @p attr_name attribute.
6651///
6652/// @return true if the DIE has a flag attribute named @p attr_name,
6653/// false otherwise.
6654static bool
6655die_flag_attribute(const Dwarf_Die* die,
6656 unsigned attr_name,
6657 bool& flag,
6658 bool recursively = true)
6659{
6660 Dwarf_Attribute attr;
6661 if (recursively
6662 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6663 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6664 return false;
6665
6666 bool f = false;
6667 if (dwarf_formflag(&attr, &f))
6668 return false;
6669
6670 flag = f;
6671 return true;
6672}
6673
6674/// Get the mangled name from a given DIE.
6675///
6676/// @param die the DIE to read the mangled name from.
6677///
6678/// @return the mangled name if it's present in the DIE, or just an
6679/// empty string if it's not.
6680static string
6681die_linkage_name(const Dwarf_Die* die)
6682{
6683 if (!die)
6684 return "";
6685
6686 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6687 if (linkage_name.empty())
6688 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6689 return linkage_name;
6690}
6691
6692/// Get the file path that is the value of the DW_AT_decl_file
6693/// attribute on a given DIE, if the DIE is a decl DIE having that
6694/// attribute.
6695///
6696/// @param die the DIE to consider.
6697///
6698/// @return a string containing the file path that is the logical
6699/// value of the DW_AT_decl_file attribute. If the DIE @p die
6700/// doesn't have a DW_AT_decl_file attribute, then the return value is
6701/// just an empty string.
6702static string
6703die_decl_file_attribute(const Dwarf_Die* die)
6704{
6705 if (!die)
6706 return "";
6707
6708 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6709
6710 return str ? str : "";
6711}
6712
6713/// Get the value of an attribute which value is supposed to be a
6714/// reference to a DIE.
6715///
6716/// @param die the DIE to read the value from.
6717///
6718/// @param attr_name the DW_AT_* attribute name to read.
6719///
6720/// @param result the DIE resulting from reading the attribute value.
6721/// This is set iff the function returns true.
6722///
6723/// @param recursively if true, the function looks through the
6724/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6725/// all the way down to the initial DIE that is cloned and look on
6726/// that DIE to see if it has the @p attr_name attribute.
6727///
6728/// @return true if the DIE @p die contains an attribute named @p
6729/// attr_name that is a DIE reference, false otherwise.
6730static bool
6731die_die_attribute(const Dwarf_Die* die,
6732 unsigned attr_name,
6733 Dwarf_Die& result,
6734 bool recursively)
6735{
6736 Dwarf_Attribute attr;
6737 if (recursively
6738 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6739 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6740 return false;
6741
6742 return dwarf_formref_die(&attr, &result);
6743}
6744
6745/// Get the DIE that is the "origin" of the current one.
6746///
6747/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6748/// attribute. Those DIEs represent a concrete instance of an
6749/// abstract entity. The concrete instance can be a concrete instance
6750/// of an inline function, or the concrete implementation of an
6751/// abstract interface. On both cases, we call the abstract instance
6752/// from which the concrete instance derives the "origin".
6753///
6754/// This function returns the ultimate origin DIE of a given DIE by
6755/// following the chain of its DW_AT_abstract_origin and
6756/// DW_AT_specification attributes.
6757///
6758/// @param die the DIE to consider.
6759///
6760/// @param origin_die this is an output parameter that is set by this
6761/// function to the resulting origin DIE iff the function returns
6762/// true.
6763///
6764/// @return true iff the function actually found an origin DIE and
6765/// set it to the @p origin_die parameter.
6766static bool
6767die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6768{
6769 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6770 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6771 {
6772 while (die_die_attribute(&origin_die,
6773 DW_AT_specification,
6774 origin_die, true)
6775 || die_die_attribute(&origin_die,
6776 DW_AT_abstract_origin,
6777 origin_die, true))
6778 {
6779 // Keep looking for the origin die ...
6780 ;
6781 }
6782 return true;
6783 }
6784 return false;
6785}
6786
6787/// Test if a subrange DIE indirectly references another subrange DIE
6788/// through a given attribute.
6789///
6790/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6791/// attribute be a reference to either a data member or a variable
6792/// which type is itself a DW_TAG_subrange_type. This latter subrange
6793/// DIE is said to be "indirectly referenced" by the former subrange
6794/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6795/// the value we want for the DW_AT_upper_bound of the former.
6796///
6797/// This function tests if the former subrange DIE does indirectly
6798/// reference another subrange DIE through a given attribute (not
6799/// necessarily DW_AT_upper_bound).
6800///
6801/// @param die the DIE to consider. Note that It must be a
6802/// DW_TAG_subrange_type.
6803///
6804/// @param attr_name the name of the attribute to look through for the
6805/// indirectly referenced subrange DIE.
6806///
6807/// @param referenced_subrange if the function returns true, then the
6808/// argument of this parameter is set to the indirectly referenced
6809/// DW_TAG_subrange_type DIE.
6810///
6811/// @return true iff @p DIE indirectly references a subrange DIE
6812/// through the attribute @p attr_name.
6813static bool
6814subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6815 unsigned attr_name,
6816 Dwarf_Die& referenced_subrange)
6817{
6818 bool result = false;
6819
6820 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6821 return result;
6822
6823 Dwarf_Die referenced_die;
6824 if (die_die_attribute(die, attr_name, referenced_die))
6825 {
6826 unsigned tag = dwarf_tag(&referenced_die);
6827 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6828 {
6829 Dwarf_Die type_die;
6830 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6831 {
6832 tag = dwarf_tag(&type_die);
6833 if (tag == DW_TAG_subrange_type)
6834 {
6835 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6836 result = true;
6837 }
6838 }
6839 }
6840 }
6841 return result;
6842}
6843
6844/// Return the bound value of subrange die by looking at an indirectly
6845/// referenced subrange DIE.
6846///
6847/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6848/// attribute be a reference to either a data member or a variable
6849/// which type is itself a DW_TAG_subrange_type. This latter subrange
6850/// DIE is said to be "indirectly referenced" by the former subrange
6851/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6852/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6853///
6854/// This function gets the DW_AT_{lower,upper}_bound value of a
6855/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6856/// the indirectly referenced subrange type, if it exists.
6857///
6858/// @param die the subrange DIE to consider.
6859///
6860/// @param attr_name the name of the attribute to consider, typically,
6861/// DW_AT_{lower,upper}_bound.
6862///
6863/// @param v the found value, iff this function returned true.
6864///
6865/// @param is_signed, this is set to true if @p v is signed. This
6866/// parameter is set at all only if the function returns true.
6867///
6868/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6869/// indirectly referenced subrange type.
6870static bool
6871subrange_die_indirect_bound_value(const Dwarf_Die *die,
6872 unsigned attr_name,
6873 array_type_def::subrange_type::bound_value& v,
6874 bool& is_signed)
6875{
6876 bool result = false;
6877
6878 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6879 return result;
6880
6881 Dwarf_Die subrange_die;
6882 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6883 subrange_die))
6884 {
6885 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6886 result = true;
6887 }
6888 return result;
6889}
6890
6891/// Read and return an addresss class attribute from a given DIE.
6892///
6893/// @param die the DIE to consider.
6894///
6895/// @param attr_name the name of the address class attribute to read
6896/// the value from.
6897///
6898/// @param the resulting address.
6899///
6900/// @return true iff the attribute could be read, was of the expected
6901/// address class and could thus be translated into the @p result.
6902static bool
6903die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6904{
6905 Dwarf_Attribute attr;
6906 if (!dwarf_attr_integrate(die, attr_name, &attr))
6907 return false;
6908 return dwarf_formaddr(&attr, &result) == 0;
6909}
6910
6911/// Returns the source location associated with a decl DIE.
6912///
6913/// @param rdr the @ref reader to use.
6914///
6915/// @param die the DIE the read the source location from.
6916///
6917/// @return the location associated with @p die.
6918static location
6919die_location(const reader& rdr, const Dwarf_Die* die)
6920{
6921 if (!die)
6922 return location();
6923
6924 string file = die_decl_file_attribute(die);
6925 uint64_t line = 0;
6926 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6927
6928 if (!file.empty() && line != 0)
6929 {
6930 translation_unit_sptr tu = rdr.cur_transl_unit();
6931 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6932 return l;
6933 }
6934 return location();
6935}
6936
6937/// Return a copy of the name of a DIE.
6938///
6939/// @param die the DIE to consider.
6940///
6941/// @return a copy of the name of the DIE.
6942static string
6943die_name(const Dwarf_Die* die)
6944{
6945 string name = die_string_attribute(die, DW_AT_name);
6946 return name;
6947}
6948
6949/// Return the location, the name and the mangled name of a given DIE.
6950///
6951/// @param rdr the DWARF reader to use.
6952///
6953/// @param die the DIE to read location and names from.
6954///
6955/// @param loc the location output parameter to set.
6956///
6957/// @param name the name output parameter to set.
6958///
6959/// @param linkage_name the linkage_name output parameter to set.
6960static void
6961die_loc_and_name(const reader& rdr,
6962 Dwarf_Die* die,
6963 location& loc,
6964 string& name,
6965 string& linkage_name)
6966{
6967 loc = die_location(rdr, die);
6968 name = die_name(die);
6969 linkage_name = die_linkage_name(die);
6970}
6971
6972/// Return the name and the mangled name of a given DIE.
6973///
6974/// @param die the DIE to read location and names from.
6975///
6976/// @param name the name output parameter to set.
6977///
6978/// @param linkage_name the linkage_name output parameter to set.
6979static void
6980die_name_and_linkage_name(const Dwarf_Die* die,
6981 string& name,
6982 string& linkage_name)
6983{
6984 name = die_name(die);
6985 linkage_name = die_linkage_name(die);
6986}
6987
6988/// Get the size of a (type) DIE as the value for the parameter
6989/// DW_AT_byte_size or DW_AT_bit_size.
6990///
6991/// @param die the DIE to read the information from.
6992///
6993/// @param size the resulting size in bits. This is set iff the
6994/// function return true.
6995///
6996/// @return true if the size attribute was found.
6997static bool
6998die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6999{
7000 if (!die)
7001 return false;
7002
7003 uint64_t byte_size = 0, bit_size = 0;
7004
7005 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
7006 {
7007 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
7008 return false;
7009 }
7010 else
7011 bit_size = byte_size * 8;
7012
7013 size = bit_size;
7014
7015 return true;
7016}
7017
7018/// Get the access specifier (from the DW_AT_accessibility attribute
7019/// value) of a given DIE.
7020///
7021/// @param die the DIE to consider.
7022///
7023/// @param access the resulting access. This is set iff the function
7024/// returns true.
7025///
7026/// @return bool if the DIE contains the DW_AT_accessibility die.
7027static bool
7028die_access_specifier(Dwarf_Die * die, access_specifier& access)
7029{
7030 if (!die)
7031 return false;
7032
7033 uint64_t a = 0;
7034 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
7035 return false;
7036
7037 access_specifier result = private_access;
7038
7039 switch (a)
7040 {
7041 case private_access:
7042 result = private_access;
7043 break;
7044
7045 case protected_access:
7046 result = protected_access;
7047 break;
7048
7049 case public_access:
7050 result = public_access;
7051 break;
7052
7053 default:
7054 break;
7055 }
7056
7057 access = result;
7058 return true;
7059}
7060
7061/// Test whether a given DIE represents a decl that is public. That
7062/// is, one with the DW_AT_external attribute set.
7063///
7064/// @param die the DIE to consider for testing.
7065///
7066/// @return true if a DW_AT_external attribute is present and its
7067/// value is set to the true; return false otherwise.
7068static bool
7069die_is_public_decl(const Dwarf_Die* die)
7070{
7071 if (!die)
7072 return false;
7073 bool is_public = false;
7074
7075 // If this is a DW_TAG_subprogram DIE, look for the
7076 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
7077 // then it's public. In all other cases, this should return false.
7078
7079 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7080 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
7081 die_flag_attribute(die, DW_AT_external, is_public);
7082 else if (tag == DW_TAG_namespace)
7083 {
7084 string name = die_name(die);
7085 is_public = !name.empty();
7086 }
7087
7088 return is_public;
7089}
7090
7091/// Test if a DIE is effectively public.
7092///
7093/// This is meant to return true when either the DIE is public or when
7094/// it's a variable DIE that is at (global) namespace level.
7095///
7096/// @return true iff either the DIE is public or is a variable DIE
7097/// that is at (global) namespace level.
7098static bool
7099die_is_effectively_public_decl(const reader& rdr,
7100 const Dwarf_Die* die)
7101{
7102 if (die_is_public_decl(die))
7103 return true;
7104
7105 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7106 if (tag == DW_TAG_variable || tag == DW_TAG_member)
7107 {
7108 // The DIE is a variable.
7109 Dwarf_Die parent_die;
7110 size_t where_offset = 0;
7111 if (!get_parent_die(rdr, die, parent_die, where_offset))
7112 return false;
7113
7114 tag = dwarf_tag(&parent_die);
7115 if (tag == DW_TAG_compile_unit
7116 || tag == DW_TAG_partial_unit
7117 || tag == DW_TAG_type_unit)
7118 // The DIE is at global scope.
7119 return true;
7120
7121 if (tag == DW_TAG_namespace)
7122 {
7123 string name = die_name(&parent_die);
7124 if (name.empty())
7125 // The DIE at unnamed namespace scope, so it's not public.
7126 return false;
7127 // The DIE is at namespace scope.
7128 return true;
7129 }
7130 }
7131 return false;
7132}
7133
7134/// Test whether a given DIE represents a declaration-only DIE.
7135///
7136/// That is, if the DIE has the DW_AT_declaration flag set.
7137///
7138/// @param die the DIE to consider.
7139//
7140/// @return true if a DW_AT_declaration is present, false otherwise.
7141static bool
7142die_is_declaration_only(Dwarf_Die* die)
7143{
7144 bool is_declaration = false;
7145 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
7146 if (is_declaration && (!die_has_size_attribute(die)
7147 || !die_has_children(die)))
7148 return true;
7149 return false;
7150}
7151
7152/// Test if a DIE is for a function decl.
7153///
7154/// @param die the DIE to consider.
7155///
7156/// @return true iff @p die represents a function decl.
7157static bool
7158die_is_function_decl(const Dwarf_Die *die)
7159{
7160 if (!die)
7161 return false;
7162
7163 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7164 if (tag == DW_TAG_subprogram)
7165 return true;
7166 return false;
7167}
7168
7169/// Test if a DIE is for a variable decl.
7170///
7171/// @param die the DIE to consider.
7172///
7173/// @return true iff @p die represents a variable decl.
7174static bool
7175die_is_variable_decl(const Dwarf_Die *die)
7176{
7177 if (!die)
7178 return false;
7179
7180 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7181 if (tag == DW_TAG_variable)
7182 return true;
7183 return false;
7184}
7185
7186/// Test if a DIE has size attribute.
7187///
7188/// @param die the DIE to consider.
7189///
7190/// @return true if the DIE has a size attribute.
7191static bool
7192die_has_size_attribute(const Dwarf_Die *die)
7193{
7194 uint64_t s;
7195 if (die_size_in_bits(die, s))
7196 return true;
7197 return false;
7198}
7199
7200/// Test that a DIE has no child DIE.
7201///
7202/// @param die the DIE to consider.
7203///
7204/// @return true iff @p die has no child DIE.
7205static bool
7206die_has_no_child(const Dwarf_Die *die)
7207{
7208 if (!die)
7209 return true;
7210
7211 Dwarf_Die child;
7212 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7213 return false;
7214 return true;
7215}
7216
7217/// Test whether a given DIE represents a declaration-only DIE.
7218///
7219/// That is, if the DIE has the DW_AT_declaration flag set.
7220///
7221/// @param die the DIE to consider.
7222//
7223/// @return true if a DW_AT_declaration is present, false otherwise.
7224static bool
7225die_is_declaration_only(const Dwarf_Die* die)
7226{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
7227
7228/// Tests whether a given DIE is artificial.
7229///
7230/// @param die the test to test for.
7231///
7232/// @return true if the DIE is artificial, false otherwise.
7233static bool
7234die_is_artificial(Dwarf_Die* die)
7235{
7236 bool is_artificial;
7237 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
7238}
7239
7240///@return true if a tag represents a type, false otherwise.
7241///
7242///@param tag the tag to consider.
7243static bool
7244is_type_tag(unsigned tag)
7245{
7246 bool result = false;
7247
7248 switch (tag)
7249 {
7250 case DW_TAG_array_type:
7251 case DW_TAG_class_type:
7252 case DW_TAG_enumeration_type:
7253 case DW_TAG_pointer_type:
7254 case DW_TAG_reference_type:
7255 case DW_TAG_string_type:
7256 case DW_TAG_structure_type:
7257 case DW_TAG_subroutine_type:
7258 case DW_TAG_typedef:
7259 case DW_TAG_union_type:
7260 case DW_TAG_ptr_to_member_type:
7261 case DW_TAG_set_type:
7262 case DW_TAG_subrange_type:
7263 case DW_TAG_base_type:
7264 case DW_TAG_const_type:
7265 case DW_TAG_file_type:
7266 case DW_TAG_packed_type:
7267 case DW_TAG_thrown_type:
7268 case DW_TAG_volatile_type:
7269 case DW_TAG_restrict_type:
7270 case DW_TAG_interface_type:
7271 case DW_TAG_unspecified_type:
7272 case DW_TAG_shared_type:
7273 case DW_TAG_rvalue_reference_type:
7274 case DW_TAG_coarray_type:
7275 case DW_TAG_atomic_type:
7276 case DW_TAG_immutable_type:
7277 result = true;
7278 break;
7279
7280 default:
7281 result = false;
7282 break;
7283 }
7284
7285 return result;
7286}
7287
7288/// Test if a given DIE is a type whose canonical type is to be
7289/// propagated during DIE canonicalization
7290///
7291/// This is a sub-routine of compare_dies.
7292///
7293/// @param tag the tag of the DIE to consider.
7294///
7295/// @return true iff the DIE of tag @p tag is can see its canonical
7296/// type be propagated during the type comparison that happens during
7297/// DIE canonicalization.
7298static bool
7299is_canon_type_to_be_propagated_tag(unsigned tag)
7300{
7301 bool result = false;
7302
7303 switch (tag)
7304 {
7305 case DW_TAG_class_type:
7306 case DW_TAG_structure_type:
7307 case DW_TAG_union_type:
7308 case DW_TAG_subroutine_type:
7309 case DW_TAG_subprogram:
7310 result = true;
7311 break;
7312
7313 default:
7314 result = false;
7315 break;
7316 }
7317
7318 return result;
7319}
7320
7321/// Test if a given kind of DIE ought to have its comparison result
7322/// cached by compare_dies, so that subsequent invocations of
7323/// compare_dies can be faster.
7324///
7325/// @param tag the tag of the DIE to consider.
7326///
7327/// @return true iff DIEs of the tag @p tag ought to have its
7328/// comparison results cached.
7329static bool
7330type_comparison_result_to_be_cached(unsigned tag)
7331{
7332 bool r = false;
7333 switch (tag)
7334 {
7335 case DW_TAG_class_type:
7336 case DW_TAG_structure_type:
7337 case DW_TAG_union_type:
7338 case DW_TAG_subroutine_type:
7339 case DW_TAG_subprogram:
7340 r = true;
7341 break;
7342
7343 default:
7344 r = false;
7345 break;
7346 }
7347 return r;
7348}
7349
7350/// Cache the result of comparing to type DIEs.
7351///
7352/// @param rdr the context to consider.
7353///
7354/// @param tag the tag of the DIEs to consider.
7355///
7356/// @param p the offsets of the pair of DIEs being compared.
7357///
7358/// @param result the comparison result to be cached.
7359static bool
7360maybe_cache_type_comparison_result(const reader& rdr,
7361 int tag,
7362 const offset_pair_type& p,
7363 comparison_result result)
7364{
7365 if (!type_comparison_result_to_be_cached(tag)
7366 || (result != COMPARISON_RESULT_EQUAL
7367 && result != COMPARISON_RESULT_DIFFERENT))
7368 return false;
7369
7370 rdr.die_comparison_results_[p] = result;
7371
7372 return true;
7373
7374}
7375
7376/// Get the cached result of the comparison of a pair of DIEs.
7377///
7378/// @param rdr the context to consider.
7379///
7380/// @param tag the tag of the pair of DIEs to consider.
7381///
7382/// @param p the offsets of the pair of DIEs to consider.
7383///
7384/// @param result out parameter set to the cached result of the
7385/// comparison of @p p if it has been found.
7386///
7387/// @return true iff a cached result for the comparisonof @p has been
7388/// found and set into @p result.
7389static bool
7390get_cached_type_comparison_result(const reader& rdr,
7391 const offset_pair_type& p,
7392 comparison_result& result)
7393{
7394 auto i = rdr.die_comparison_results_.find(p);
7395 if (i != rdr.die_comparison_results_.end())
7396 {
7397 result = i->second;
7398 return true;
7399 }
7400 return false;
7401}
7402
7403/// Get the cached result of the comparison of a pair of DIEs, if the
7404/// kind of DIEs ought to have its comparison results cached.
7405///
7406/// @param rdr the context to consider.
7407///
7408/// @param tag the tag of the pair of DIEs to consider.
7409///
7410/// @param p the offsets of the pair of DIEs to consider.
7411///
7412/// @param result out parameter set to the cached result of the
7413/// comparison of @p p if it has been found.
7414///
7415/// @return true iff a cached result for the comparisonof @p has been
7416/// found and set into @p result.
7417static bool
7418maybe_get_cached_type_comparison_result(const reader& rdr,
7419 int tag,
7420 const offset_pair_type& p,
7421 comparison_result& result)
7422{
7423 if (type_comparison_result_to_be_cached(tag))
7424 {
7425 // Types of this kind might have their comparison result cached
7426 // when they are not canonicalized. So let's see if we have a
7427 // cached comparison result.
7428 if (get_cached_type_comparison_result(rdr, p, result))
7429 return true;
7430 }
7431 return false;
7432}
7433
7434/// Test if a given DIE is to be canonicalized.
7435///
7436/// @param die the DIE to consider.
7437///
7438/// @return true iff @p die is to be canonicalized.
7439static bool
7440is_type_die_to_be_canonicalized(const Dwarf_Die *die)
7441{
7442 bool result = false;
7443 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7444
7445 if (!is_type_tag(tag))
7446 return false;
7447
7448 switch (tag)
7449 {
7450 case DW_TAG_class_type:
7451 case DW_TAG_structure_type:
7452 case DW_TAG_union_type:
7453 result = !die_is_declaration_only(die);
7454 break;
7455
7456 case DW_TAG_subroutine_type:
7457 case DW_TAG_subprogram:
7458 case DW_TAG_array_type:
7459 result = true;
7460
7461 default:
7462 break;
7463 }
7464
7465 return result;
7466}
7467
7468/// Test if a DIE tag represents a declaration.
7469///
7470/// @param tag the DWARF tag to consider.
7471///
7472/// @return true iff @p tag is for a declaration.
7473static bool
7474is_decl_tag(unsigned tag)
7475{
7476 switch (tag)
7477 {
7478 case DW_TAG_formal_parameter:
7479 case DW_TAG_imported_declaration:
7480 case DW_TAG_member:
7481 case DW_TAG_unspecified_parameters:
7482 case DW_TAG_subprogram:
7483 case DW_TAG_variable:
7484 case DW_TAG_namespace:
7485 case DW_TAG_GNU_template_template_param:
7486 case DW_TAG_GNU_template_parameter_pack:
7487 case DW_TAG_GNU_formal_parameter_pack:
7488 return true;
7489 }
7490 return false;
7491}
7492
7493/// Test if a DIE represents a type DIE.
7494///
7495/// @param die the DIE to consider.
7496///
7497/// @return true if @p die represents a type, false otherwise.
7498static bool
7499die_is_type(const Dwarf_Die* die)
7500{
7501 if (!die)
7502 return false;
7503 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7504}
7505
7506/// Test if a DIE represents a declaration.
7507///
7508/// @param die the DIE to consider.
7509///
7510/// @return true if @p die represents a decl, false otherwise.
7511static bool
7512die_is_decl(const Dwarf_Die* die)
7513{
7514 if (!die)
7515 return false;
7516 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7517}
7518
7519/// Test if a DIE represents a namespace.
7520///
7521/// @param die the DIE to consider.
7522///
7523/// @return true if @p die represents a namespace, false otherwise.
7524static bool
7525die_is_namespace(const Dwarf_Die* die)
7526{
7527 if (!die)
7528 return false;
7529 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7530}
7531
7532/// Test if a DIE has tag DW_TAG_unspecified_type.
7533///
7534/// @param die the DIE to consider.
7535///
7536/// @return true if @p die has tag DW_TAG_unspecified_type.
7537static bool
7538die_is_unspecified(Dwarf_Die* die)
7539{
7540 if (!die)
7541 return false;
7542 return (dwarf_tag(die) == DW_TAG_unspecified_type);
7543}
7544
7545/// Test if a DIE represents a void type.
7546///
7547/// @param die the DIE to consider.
7548///
7549/// @return true if @p die represents a void type, false otherwise.
7550static bool
7551die_is_void_type(Dwarf_Die* die)
7552{
7553 if (!die || dwarf_tag(die) != DW_TAG_base_type)
7554 return false;
7555
7556 string name = die_name(die);
7557 if (name == "void")
7558 return true;
7559
7560 return false;
7561}
7562
7563/// Test if a DIE represents a pointer type.
7564///
7565/// @param die the die to consider.
7566///
7567/// @return true iff @p die represents a pointer type.
7568static bool
7569die_is_pointer_type(const Dwarf_Die* die)
7570{
7571 if (!die)
7572 return false;
7573
7574 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7575 if (tag == DW_TAG_pointer_type)
7576 return true;
7577
7578 return false;
7579}
7580
7581/// Test if a DIE is for a pointer, reference or qualified type to
7582/// anonymous class or struct.
7583///
7584/// @param die the DIE to consider.
7585///
7586/// @return true iff @p is for a pointer, reference or qualified type
7587/// to anonymous class or struct.
7588static bool
7589pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7590{
7591 if (!die_is_pointer_array_or_reference_type(die)
7592 && !die_is_qualified_type(die))
7593 return false;
7594
7595 Dwarf_Die underlying_type_die;
7596 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7597 return false;
7598
7599 if (!die_is_class_type(&underlying_type_die))
7600 return false;
7601
7602 string name = die_name(&underlying_type_die);
7603
7604 return name.empty();
7605}
7606
7607/// Test if a DIE represents a reference type.
7608///
7609/// @param die the die to consider.
7610///
7611/// @return true iff @p die represents a reference type.
7612static bool
7613die_is_reference_type(const Dwarf_Die* die)
7614{
7615 if (!die)
7616 return false;
7617
7618 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7619 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7620 return true;
7621
7622 return false;
7623}
7624
7625/// Test if a DIE represents an array type.
7626///
7627/// @param die the die to consider.
7628///
7629/// @return true iff @p die represents an array type.
7630static bool
7631die_is_array_type(const Dwarf_Die* die)
7632{
7633 if (!die)
7634 return false;
7635
7636 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7637 if (tag == DW_TAG_array_type)
7638 return true;
7639
7640 return false;
7641}
7642
7643/// Test if a DIE represents a pointer, reference or array type.
7644///
7645/// @param die the die to consider.
7646///
7647/// @return true iff @p die represents a pointer or reference type.
7648static bool
7649die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7650{return (die_is_pointer_type(die)
7651 || die_is_reference_type(die)
7652 || die_is_array_type(die));}
7653
7654/// Test if a DIE represents a pointer or a reference type.
7655///
7656/// @param die the die to consider.
7657///
7658/// @return true iff @p die represents a pointer or reference type.
7659static bool
7660die_is_pointer_or_reference_type(const Dwarf_Die* die)
7661{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7662
7663/// Test if a DIE represents a pointer, a reference or a typedef type.
7664///
7665/// @param die the die to consider.
7666///
7667/// @return true iff @p die represents a pointer, a reference or a
7668/// typedef type.
7669static bool
7670die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7671{return (die_is_pointer_array_or_reference_type(die)
7672 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7673
7674/// Test if a DIE represents a class type.
7675///
7676/// @param die the die to consider.
7677///
7678/// @return true iff @p die represents a class type.
7679static bool
7680die_is_class_type(const Dwarf_Die* die)
7681{
7682 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7683
7684 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7685 return true;
7686
7687 return false;
7688}
7689
7690/// Test if a DIE is for a qualified type.
7691///
7692/// @param die the DIE to consider.
7693///
7694/// @return true iff @p die is for a qualified type.
7695static bool
7696die_is_qualified_type(const Dwarf_Die* die)
7697{
7698 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7699 if (tag == DW_TAG_const_type
7700 || tag == DW_TAG_volatile_type
7701 || tag == DW_TAG_restrict_type)
7702 return true;
7703
7704 return false;
7705}
7706
7707/// Test if a DIE is for a function type.
7708///
7709/// @param die the DIE to consider.
7710///
7711/// @return true iff @p die is for a function type.
7712static bool
7713die_is_function_type(const Dwarf_Die *die)
7714{
7715 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7716 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7717 return true;
7718
7719 return false;
7720}
7721
7722/// Test if a DIE for a function pointer or member function has an
7723/// DW_AT_object_pointer attribute.
7724///
7725/// @param die the DIE to consider.
7726///
7727/// @param object_pointer out parameter. It's set to the DIE for the
7728/// object pointer iff the function returns true.
7729///
7730/// @return true iff the DIE @p die has an object pointer. In that
7731/// case, the parameter @p object_pointer is set to the DIE of that
7732/// object pointer.
7733static bool
7734die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7735{
7736 if (!die)
7737 return false;
7738
7739 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7740 return true;
7741
7742 return false;
7743}
7744
7745/// Test if a DIE has children DIEs.
7746///
7747/// @param die the DIE to consider.
7748///
7749/// @return true iff @p DIE has at least one child node.
7750static bool
7751die_has_children(const Dwarf_Die* die)
7752{
7753 if (!die)
7754 return false;
7755
7756 Dwarf_Die child;
7757 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7758 return true;
7759
7760 return false;
7761}
7762
7763/// Get the DIE representing the first parameter of the function
7764/// denoted by a given DIE.
7765///
7766/// @param die the function DIE to consider. Note that if this
7767/// parameter is neither a DW_TAG_subprogram nor a
7768/// DW_TAG_subroutine_type, then the current process is aborted.
7769///
7770/// @param first_parm_die output parameter. This is set to the DIE of
7771/// the first parameter of the function denoted by @p die. This
7772/// output parameter is set iff the function returns true.
7773///
7774/// @return true iff the first parameter of the function denoted by @p
7775/// die is returned in output parameter @p first_parm_die.
7776static bool
7777fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die)
7778{
7779 if (!die)
7780 return false;
7781
7782 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7783 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7784
7785 Dwarf_Die child;
7786 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7787 {
7788 int child_tag = dwarf_tag(&child);
7789 if (child_tag == DW_TAG_formal_parameter)
7790 {
7791 memcpy(&first_parm_die, &child, sizeof(Dwarf_Die));
7792 return true;
7793 }
7794 }
7795 return false;
7796}
7797
7798/// Test if a member function denoted by a given DIE has a parameter
7799/// which is a "this pointer".
7800///
7801/// Please note that if the member function denotes a static member
7802/// function or if the DIE does not denote a member function to begin
7803/// with, then the function will return false because no "this
7804/// pointer" will be found.
7805///
7806/// @param rdr the current DWARF reader in use.
7807///
7808/// @param die the DIE of the member function this function should
7809/// inspect.
7810///
7811/// @param where_offset where in the DIE stream we logically are.
7812///
7813/// @param class_die output parameter. This is set iff a "this
7814/// pointer" was found as the first parameters of the member function
7815/// denoted by @p die, and thus the function returns true If set, this
7816/// then points to the DIE of the class containing the member function
7817/// denoted by @p die.
7818///
7819/// @param object_pointer_die output parameter. This is set to the
7820/// DIE of the function parameter that carries the "this pointe".
7821/// This is set iff this function return true.
7822///
7823/// @return true iff the first parameter of the member function
7824/// denoted by @p die points to a "this pointer".
7825static bool
7826member_fn_die_has_this_pointer(const reader& rdr,
7827 const Dwarf_Die* die,
7828 size_t where_offset,
7829 Dwarf_Die& class_die,
7830 Dwarf_Die& object_pointer_die)
7831{
7832 if (!die)
7833 return false;
7834
7835 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7836 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
7837 return false;
7838
7839 if (tag == DW_TAG_subprogram
7840 && !die_is_at_class_scope(rdr, die, where_offset, class_die))
7841 return false;
7842
7843 Dwarf_Die first_parm_die;
7844 Dwarf_Die parm_type_die;
7845 if (die_has_object_pointer(die, object_pointer_die))
7846 {
7847 // This can be either a member function with a
7848 // DW_AT_object_pointer attribute or a DW_TAG_subroutine_type
7849 // with a DW_AT_object_pointer. In the later case, we are
7850 // looking at a member function type.
7851 memcpy(&first_parm_die, &object_pointer_die, sizeof(Dwarf_Die));
7852 if (!die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7853 return false;
7854 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7855 die_peel_typedef(&parm_type_die, parm_type_die);
7856 }
7857 else if (fn_die_first_parameter_die(die, first_parm_die))
7858 {
7859 memcpy(&object_pointer_die, &first_parm_die, sizeof(Dwarf_Die));
7860 bool is_artificial = false;
7861 if (die_flag_attribute(&first_parm_die, DW_AT_artificial, is_artificial))
7862 {
7863 if (die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7864 {
7865 tag = dwarf_tag(&parm_type_die);
7866 if (tag == DW_TAG_pointer_type)
7867 {
7868 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7869 die_peel_typedef(&parm_type_die, parm_type_die);
7870 }
7871 else
7872 return false;
7873 }
7874 else
7875 return false;
7876 }
7877 else
7878 return false;
7879 }
7880 else
7881 return false;
7882
7883 tag = dwarf_tag(&parm_type_die);
7884 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7885 {
7886 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7887 return true;
7888 }
7889 return false;
7890}
7891
7892/// When given the object pointer DIE of a function type or member
7893/// function DIE, this function returns the "this" pointer that points
7894/// to the associated class.
7895///
7896/// @param die the DIE of the object pointer of the function or member
7897/// function to consider.
7898///
7899/// @param this_pointer_die out parameter. This is set to the DIE of
7900/// the "this" pointer iff the function returns true.
7901///
7902/// @return true iff the function found the "this" pointer from the
7903/// object pointer DIE @p die. In that case, the parameter @p
7904/// this_pointer_die is set to the DIE of that "this" pointer.
7905static bool
7906die_this_pointer_from_object_pointer(Dwarf_Die* die,
7907 Dwarf_Die& this_pointer_die)
7908{
7909 ABG_ASSERT(die);
7910 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7911
7912 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7913 return true;
7914
7915 return false;
7916}
7917
7918/// Test if a given "this" pointer that points to a particular class
7919/// type is for a const class or not. If it's for a const class, then
7920/// it means the function type or the member function associated to
7921/// that "this" pointer is const.
7922///
7923/// @param dye the DIE of the "this" pointer to consider.
7924///
7925/// @return true iff @p die points to a const class type.
7926static bool
7927die_this_pointer_is_const(Dwarf_Die* dye)
7928{
7929 ABG_ASSERT(dye);
7930
7931 Dwarf_Die die;
7932 memcpy(&die, dye, sizeof(Dwarf_Die));
7933 if (dwarf_tag(&die) == DW_TAG_const_type)
7934 ABG_ASSERT(die_peel_qualified(&die, die));
7935
7936 if (dwarf_tag(&die) == DW_TAG_pointer_type)
7937 {
7938 Dwarf_Die pointed_to_type_die;
7939 if (die_die_attribute(&die, DW_AT_type, pointed_to_type_die))
7940 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7941 return true;
7942 }
7943
7944 return false;
7945}
7946
7947/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7948/// attribute) points to a const implicit class and so is for a const
7949/// method or or a const member function type.
7950///
7951/// @param die the DIE of the object pointer to consider.
7952///
7953/// @return true iff the object pointer represented by @p die is for a
7954/// a const method or const member function type.
7955static bool
7956die_object_pointer_is_for_const_method(Dwarf_Die* die)
7957{
7958 ABG_ASSERT(die);
7959 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7960
7961 Dwarf_Die this_pointer_die;
7962 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7963 if (die_this_pointer_is_const(&this_pointer_die))
7964 return true;
7965
7966 return false;
7967}
7968
7969/// Test if a DIE represents an entity that is at class scope.
7970///
7971/// @param rdr the DWARF reader to use.
7972///
7973/// @param die the DIE to consider.
7974///
7975/// @param where_offset where we are logically at in the DIE stream.
7976///
7977/// @param class_scope_die out parameter. Set to the DIE of the
7978/// containing class iff @p die happens to be at class scope; that is,
7979/// iff the function returns true.
7980///
7981/// @return true iff @p die is at class scope. In that case, @p
7982/// class_scope_die is set to the DIE of the class that contains @p
7983/// die.
7984static bool
7985die_is_at_class_scope(const reader& rdr,
7986 const Dwarf_Die* die,
7987 size_t where_offset,
7988 Dwarf_Die& class_scope_die)
7989{
7990 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7991 return false;
7992
7993 int tag = dwarf_tag(&class_scope_die);
7994
7995 return (tag == DW_TAG_structure_type
7996 || tag == DW_TAG_class_type
7997 || tag == DW_TAG_union_type);
7998}
7999
8000/// Return the leaf object under a pointer, reference or qualified
8001/// type DIE.
8002///
8003/// @param die the DIE of the type to consider.
8004///
8005/// @param peeled_die out parameter. Set to the DIE of the leaf
8006/// object iff the function actually peeled anything.
8007///
8008/// @return true upon successful completion.
8009static bool
8010die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
8011{
8012 if (!die)
8013 return false;
8014
8015 int tag = dwarf_tag(die);
8016
8017 if (tag == DW_TAG_const_type
8018 || tag == DW_TAG_volatile_type
8019 || tag == DW_TAG_restrict_type
8020 || tag == DW_TAG_pointer_type
8021 || tag == DW_TAG_reference_type
8022 || tag == DW_TAG_rvalue_reference_type)
8023 {
8024 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8025 return false;
8026 }
8027 else
8028 return false;
8029
8030 memcpy(&peeled_die, die, sizeof(peeled_die));
8031
8032 while (tag == DW_TAG_const_type
8033 || tag == DW_TAG_volatile_type
8034 || tag == DW_TAG_restrict_type
8035 || tag == DW_TAG_pointer_type
8036 || tag == DW_TAG_reference_type
8037 || tag == DW_TAG_rvalue_reference_type)
8038 {
8039 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8040 break;
8041 tag = dwarf_tag(&peeled_die);
8042 }
8043
8044 return true;
8045}
8046
8047/// Return the leaf object under a qualified type DIE.
8048///
8049/// @param die the DIE of the type to consider.
8050///
8051/// @param peeled_die out parameter. Set to the DIE of the leaf
8052/// object iff the function actually peeled anything.
8053///
8054/// @return true upon successful completion.
8055static bool
8056die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
8057{
8058 if (!die)
8059 return false;
8060
8061 memcpy(&peeled_die, die, sizeof(peeled_die));
8062
8063 int tag = dwarf_tag(&peeled_die);
8064
8065 bool result = false;
8066 while (tag == DW_TAG_const_type
8067 || tag == DW_TAG_volatile_type
8068 || tag == DW_TAG_restrict_type)
8069 {
8070 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8071 break;
8072 tag = dwarf_tag(&peeled_die);
8073 result = true;
8074 }
8075
8076 return result;
8077}
8078
8079/// Return the leaf object under a typedef type DIE.
8080///
8081/// @param die the DIE of the type to consider.
8082///
8083/// @param peeled_die out parameter. Set to the DIE of the leaf
8084/// object iff the function actually peeled anything.
8085///
8086/// @return true upon successful completion.
8087static bool
8088die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
8089{
8090 if (!die)
8091 return false;
8092
8093 int tag = dwarf_tag(die);
8094
8095 memcpy(&peeled_die, die, sizeof(peeled_die));
8096
8097 if (tag == DW_TAG_typedef)
8098 {
8099 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8100 return false;
8101 }
8102 else
8103 return false;
8104
8105 while (tag == DW_TAG_typedef)
8106 {
8107 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8108 break;
8109 tag = dwarf_tag(&peeled_die);
8110 }
8111
8112 return true;
8113
8114}
8115
8116/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
8117///
8118/// @param die the DIE to consider.
8119///
8120/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
8121/// iff the function returned true.
8122///
8123/// @return true iff the function could peel @p die.
8124static bool
8125die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
8126{
8127 if (!die)
8128 return false;
8129
8130 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8131
8132 if (tag == DW_TAG_pointer_type
8133 || tag == DW_TAG_reference_type
8134 || tag == DW_TAG_rvalue_reference_type
8135 || tag == DW_TAG_typedef)
8136 {
8137 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8138 return false;
8139 }
8140 else
8141 return false;
8142
8143 while (tag == DW_TAG_pointer_type
8144 || tag == DW_TAG_reference_type
8145 || tag == DW_TAG_rvalue_reference_type
8146 || tag == DW_TAG_typedef)
8147 {
8148 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8149 break;
8150 tag = dwarf_tag(&peeled_die);
8151 }
8152 return true;
8153}
8154
8155/// Test if a DIE for a function type represents a method type.
8156///
8157/// @param rdr the DWARF reader.
8158///
8159/// @param die the DIE to consider.
8160///
8161/// @param where_offset where we logically are in the stream of DIEs.
8162///
8163/// @param object_pointer_die out parameter. This is set by the
8164/// function to the DIE that refers to the formal function parameter
8165/// which holds the implicit "this" pointer of the method. That die
8166/// is called the object pointer DIE. This is set iff the member
8167/// function is a non-static member function and if the function
8168/// returns true. In other words, this is only set if the is_static
8169/// out parameter is set to false and the function returns true.
8170///
8171/// @param class_die out parameter. This is set by the function to
8172/// the DIE that represents the class of the method type. This is set
8173/// iff the function returns true.
8174///
8175/// @param is_static out parameter. This is set to true by the
8176/// function if @p die is a static method or a the type of a static
8177/// method. This is set iff the function returns true.
8178///
8179/// @return true iff @p die is a DIE for a method type.
8180static bool
8181die_function_type_is_method_type(const reader& rdr,
8182 const Dwarf_Die *die,
8183 size_t where_offset,
8184 Dwarf_Die& object_pointer_die,
8185 Dwarf_Die& class_die,
8186 bool& is_static)
8187{
8188 if (!die)
8189 return false;
8190
8191 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8192 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
8193
8194 if (member_fn_die_has_this_pointer(rdr, die, where_offset, class_die, object_pointer_die))
8195 {
8196 is_static = false;
8197 return true;
8198 }
8199 else if (die_is_at_class_scope(rdr, die, where_offset, class_die))
8200 {
8201 is_static = true;
8202 return true;
8203 }
8204
8205 return false;
8206}
8207
8208enum virtuality
8209{
8210 VIRTUALITY_NOT_VIRTUAL,
8211 VIRTUALITY_VIRTUAL,
8212 VIRTUALITY_PURE_VIRTUAL
8213};
8214
8215/// Get the virtual-ness of a given DIE, that is, the value of the
8216/// DW_AT_virtuality attribute.
8217///
8218/// @param die the DIE to read from.
8219///
8220/// @param virt the resulting virtuality attribute. This is set iff
8221/// the function returns true.
8222///
8223/// @return true if the virtual-ness could be determined.
8224static bool
8225die_virtuality(const Dwarf_Die* die, virtuality& virt)
8226{
8227 if (!die)
8228 return false;
8229
8230 uint64_t v = 0;
8231 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
8232
8233 if (v == DW_VIRTUALITY_virtual)
8234 virt = VIRTUALITY_VIRTUAL;
8235 else if (v == DW_VIRTUALITY_pure_virtual)
8236 virt = VIRTUALITY_PURE_VIRTUAL;
8237 else
8238 virt = VIRTUALITY_NOT_VIRTUAL;
8239
8240 return true;
8241}
8242
8243/// Test whether the DIE represent either a virtual base or function.
8244///
8245/// @param die the DIE to consider.
8246///
8247/// @return bool if the DIE represents a virtual base or function,
8248/// false othersise.
8249static bool
8250die_is_virtual(const Dwarf_Die* die)
8251{
8252 virtuality v;
8253 if (!die_virtuality(die, v))
8254 return false;
8255
8256 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
8257}
8258
8259/// Test if the DIE represents an entity that was declared inlined.
8260///
8261/// @param die the DIE to test for.
8262///
8263/// @return true if the DIE represents an entity that was declared
8264/// inlined.
8265static bool
8266die_is_declared_inline(Dwarf_Die* die)
8267{
8268 uint64_t inline_value = 0;
8269 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
8270 return false;
8271 return (inline_value == DW_INL_declared_inlined
8272 || inline_value == DW_INL_declared_not_inlined);
8273}
8274
8275/// Compare two DWARF strings using the most accurate (and slowest)
8276/// method possible.
8277///
8278/// @param l the DIE that carries the first string to consider, as an
8279/// attribute value.
8280///
8281/// @param attr_name the name of the attribute which value is the
8282/// string to compare.
8283///
8284/// @return true iff the string carried by @p l equals the one carried
8285/// by @p r.
8286static bool
8287slowly_compare_strings(const Dwarf_Die *l,
8288 const Dwarf_Die *r,
8289 unsigned attr_name)
8290{
8291 const char *l_str = die_char_str_attribute(l, attr_name),
8292 *r_str = die_char_str_attribute(r, attr_name);
8293 if (!l_str && !r_str)
8294 return true;
8295 return l_str && r_str && !strcmp(l_str, r_str);
8296}
8297
8298/// This function is a fast routine (optimization) to compare the
8299/// values of two string attributes of two DIEs.
8300///
8301/// @param l the first DIE to consider.
8302///
8303/// @param r the second DIE to consider.
8304///
8305/// @param attr_name the name of the attribute to compare, on the two
8306/// DIEs above.
8307///
8308/// @param result out parameter. This is set to the result of the
8309/// comparison. If the value of attribute @p attr_name on DIE @p l
8310/// equals the value of attribute @p attr_name on DIE @p r, then the
8311/// the argument of this parameter is set to true. Otherwise, it's
8312/// set to false. Note that the argument of this parameter is set iff
8313/// the function returned true.
8314///
8315/// @return true iff the comparison could be performed. There are
8316/// cases in which the comparison cannot be performed. For instance,
8317/// if one of the DIEs does not have the attribute @p attr_name. In
8318/// any case, if this function returns true, then the parameter @p
8319/// result is set to the result of the comparison.
8320static bool
8321compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
8322 unsigned attr_name,
8323 bool &result)
8324{
8325 Dwarf_Attribute l_attr, r_attr;
8326 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
8327 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
8328 return false;
8329
8330 ABG_ASSERT(l_attr.form == DW_FORM_strp
8331 || l_attr.form == DW_FORM_string
8332 || l_attr.form == DW_FORM_GNU_strp_alt
8333 || form_is_DW_FORM_strx(l_attr.form)
8334 || form_is_DW_FORM_line_strp(l_attr.form));
8335
8336 ABG_ASSERT(r_attr.form == DW_FORM_strp
8337 || r_attr.form == DW_FORM_string
8338 || r_attr.form == DW_FORM_GNU_strp_alt
8339 || form_is_DW_FORM_strx(r_attr.form)
8340 || form_is_DW_FORM_line_strp(r_attr.form));
8341
8342 if ((l_attr.form == DW_FORM_strp
8343 && r_attr.form == DW_FORM_strp)
8344 || (l_attr.form == DW_FORM_GNU_strp_alt
8345 && r_attr.form == DW_FORM_GNU_strp_alt)
8346 || (form_is_DW_FORM_strx(l_attr.form)
8347 && form_is_DW_FORM_strx(r_attr.form))
8348 || (form_is_DW_FORM_line_strp(l_attr.form)
8349 && form_is_DW_FORM_line_strp(r_attr.form)))
8350 {
8351 // So these string attributes are actually pointers into a
8352 // string table. The string table is most likely de-duplicated
8353 // so comparing the *values* of the pointers should be enough.
8354 //
8355 // This is the fast path.
8356 if (l_attr.valp == r_attr.valp)
8357 {
8358#if WITH_DEBUG_TYPE_CANONICALIZATION
8359 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
8360#endif
8361 result = true;
8362 return true;
8363 }
8364 }
8365
8366 // If we reached this point it means we couldn't use the fast path
8367 // because the string atttributes are strings that are "inline" in
8368 // the debug info section. Let's just compare them the slow and
8369 // obvious way.
8370 result = slowly_compare_strings(l, r, attr_name);
8371 return true;
8372}
8373
8374/// Compare the file path of the compilation units (aka CUs)
8375/// associated to two DIEs.
8376///
8377/// If the DIEs are for pointers or typedefs, this function also
8378/// compares the file paths of the CUs of the leaf DIEs (underlying
8379/// DIEs of the pointer or the typedef).
8380///
8381/// @param l the first type DIE to consider.
8382///
8383/// @param r the second type DIE to consider.
8384///
8385/// @return true iff the file paths of the DIEs of the two types are
8386/// equal.
8387static bool
8388compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
8389{
8390 Dwarf_Die l_cu, r_cu;
8391 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
8392 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
8393 return false;
8394
8395 bool compared =
8396 compare_dies_string_attribute_value(&l_cu, &r_cu,
8397 DW_AT_name,
8398 result);
8399 if (compared && result)
8400 {
8401 Dwarf_Die peeled_l, peeled_r;
8402 if (die_is_pointer_reference_or_typedef_type(l)
8403 && die_is_pointer_reference_or_typedef_type(r)
8404 && die_peel_pointer_and_typedef(l, peeled_l)
8405 && die_peel_pointer_and_typedef(r, peeled_r))
8406 {
8407 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
8408 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
8409 return false;
8410 compared =
8411 compare_dies_string_attribute_value(&l_cu, &r_cu,
8412 DW_AT_name,
8413 result);
8414 }
8415 }
8416
8417 return compared;
8418}
8419
8420// -----------------------------------
8421// <location expression evaluation>
8422// -----------------------------------
8423
8424/// Get the value of a given DIE attribute, knowing that it must be a
8425/// location expression.
8426///
8427/// @param die the DIE to read the attribute from.
8428///
8429/// @param attr_name the name of the attribute to read the value for.
8430///
8431/// @param expr the pointer to allocate and fill with the resulting
8432/// array of operators + operands forming a dwarf expression. This is
8433/// set iff the function returns true.
8434///
8435/// @param expr_len the length of the resulting dwarf expression.
8436/// This is set iff the function returns true.
8437///
8438/// @return true if the attribute exists and has a non-empty dwarf expression
8439/// as value. In that case the expr and expr_len arguments are set to the
8440/// resulting dwarf expression.
8441static bool
8442die_location_expr(const Dwarf_Die* die,
8443 unsigned attr_name,
8444 Dwarf_Op** expr,
8445 size_t* expr_len)
8446{
8447 if (!die)
8448 return false;
8449
8450 Dwarf_Attribute attr;
8451 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
8452 return false;
8453
8454 size_t len = 0;
8455 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
8456
8457 // Ignore location expressions where reading them succeeded but
8458 // their length is 0.
8459 result &= len > 0;
8460
8461 if (result)
8462 *expr_len = len;
8463
8464 return result;
8465}
8466
8467/// If the current operation in the dwarf expression represents a push
8468/// of a constant value onto the dwarf expr virtual machine (aka
8469/// DEVM), perform the operation and update the DEVM.
8470///
8471/// If the result of the operation is a constant, update the DEVM
8472/// accumulator with its value. Otherwise, the DEVM accumulator is
8473/// left with its previous value.
8474///
8475/// @param ops the array of the dwarf expression operations to consider.
8476///
8477/// @param ops_len the lengths of @p ops array above.
8478///
8479/// @param index the index of the operation to interpret, in @p ops.
8480///
8481/// @param next_index the index of the operation to interpret at the
8482/// next step, after this function completed and returned. This is
8483/// set an output parameter that is set iff the function returns true.
8484///
8485/// @param ctxt the DEVM evaluation context.
8486///
8487/// @return true if the current operation actually pushes a constant
8488/// value onto the DEVM stack, false otherwise.
8489static bool
8490op_pushes_constant_value(Dwarf_Op* ops,
8491 size_t ops_len,
8492 size_t index,
8493 size_t& next_index,
8494 dwarf_expr_eval_context& ctxt)
8495{
8496 ABG_ASSERT(index < ops_len);
8497
8498 Dwarf_Op& op = ops[index];
8499 int64_t value = 0;
8500
8501 switch (op.atom)
8502 {
8503 case DW_OP_addr:
8504 value = ops[index].number;
8505 break;
8506
8507 case DW_OP_const1u:
8508 case DW_OP_const1s:
8509 case DW_OP_const2u:
8510 case DW_OP_const2s:
8511 case DW_OP_const4u:
8512 case DW_OP_const4s:
8513 case DW_OP_const8u:
8514 case DW_OP_const8s:
8515 case DW_OP_constu:
8516 case DW_OP_consts:
8517 value = ops[index].number;
8518 break;
8519
8520 case DW_OP_lit0:
8521 value = 0;
8522 break;
8523 case DW_OP_lit1:
8524 value = 1;
8525 break;
8526 case DW_OP_lit2:
8527 value = 2;
8528 break;
8529 case DW_OP_lit3:
8530 value = 3;
8531 break;
8532 case DW_OP_lit4:
8533 value = 4;
8534 break;
8535 case DW_OP_lit5:
8536 value = 5;
8537 break;
8538 case DW_OP_lit6:
8539 value = 6;
8540 break;
8541 case DW_OP_lit7:
8542 value = 7;
8543 break;
8544 case DW_OP_lit8:
8545 value = 8;
8546 break;
8547 case DW_OP_lit9:
8548 value = 9;
8549 break;
8550 case DW_OP_lit10:
8551 value = 10;
8552 break;
8553 case DW_OP_lit11:
8554 value = 11;
8555 break;
8556 case DW_OP_lit12:
8557 value = 12;
8558 break;
8559 case DW_OP_lit13:
8560 value = 13;
8561 break;
8562 case DW_OP_lit14:
8563 value = 14;
8564 break;
8565 case DW_OP_lit15:
8566 value = 15;
8567 break;
8568 case DW_OP_lit16:
8569 value = 16;
8570 break;
8571 case DW_OP_lit17:
8572 value = 17;
8573 break;
8574 case DW_OP_lit18:
8575 value = 18;
8576 break;
8577 case DW_OP_lit19:
8578 value = 19;
8579 break;
8580 case DW_OP_lit20:
8581 value = 20;
8582 break;
8583 case DW_OP_lit21:
8584 value = 21;
8585 break;
8586 case DW_OP_lit22:
8587 value = 22;
8588 break;
8589 case DW_OP_lit23:
8590 value = 23;
8591 break;
8592 case DW_OP_lit24:
8593 value = 24;
8594 break;
8595 case DW_OP_lit25:
8596 value = 25;
8597 break;
8598 case DW_OP_lit26:
8599 value = 26;
8600 break;
8601 case DW_OP_lit27:
8602 value = 27;
8603 break;
8604 case DW_OP_lit28:
8605 value = 28;
8606 break;
8607 case DW_OP_lit29:
8608 value = 29;
8609 break;
8610 case DW_OP_lit30:
8611 value = 30;
8612 break;
8613 case DW_OP_lit31:
8614 value = 31;
8615 break;
8616
8617 default:
8618 return false;
8619 }
8620
8621 expr_result r(value);
8622 ctxt.push(r);
8623 ctxt.accum = r;
8624 next_index = index + 1;
8625
8626 return true;
8627}
8628
8629/// If the current operation in the dwarf expression represents a push
8630/// of a non-constant value onto the dwarf expr virtual machine (aka
8631/// DEVM), perform the operation and update the DEVM. A non-constant
8632/// is namely a quantity for which we need inferior (a running program
8633/// image) state to know the exact value.
8634///
8635/// Upon successful completion, as the result of the operation is a
8636/// non-constant the DEVM accumulator value is left to its state as of
8637/// before the invocation of this function.
8638///
8639/// @param ops the array of the dwarf expression operations to consider.
8640///
8641/// @param ops_len the lengths of @p ops array above.
8642///
8643/// @param index the index of the operation to interpret, in @p ops.
8644///
8645/// @param next_index the index of the operation to interpret at the
8646/// next step, after this function completed and returned. This is
8647/// set an output parameter that is set iff the function returns true.
8648///
8649/// @param ctxt the DEVM evaluation context.
8650///
8651/// @return true if the current operation actually pushes a
8652/// non-constant value onto the DEVM stack, false otherwise.
8653static bool
8654op_pushes_non_constant_value(Dwarf_Op* ops,
8655 size_t ops_len,
8656 size_t index,
8657 size_t& next_index,
8658 dwarf_expr_eval_context& ctxt)
8659{
8660 ABG_ASSERT(index < ops_len);
8661 Dwarf_Op& op = ops[index];
8662
8663 switch (op.atom)
8664 {
8665 case DW_OP_reg0:
8666 case DW_OP_reg1:
8667 case DW_OP_reg2:
8668 case DW_OP_reg3:
8669 case DW_OP_reg4:
8670 case DW_OP_reg5:
8671 case DW_OP_reg6:
8672 case DW_OP_reg7:
8673 case DW_OP_reg8:
8674 case DW_OP_reg9:
8675 case DW_OP_reg10:
8676 case DW_OP_reg11:
8677 case DW_OP_reg12:
8678 case DW_OP_reg13:
8679 case DW_OP_reg14:
8680 case DW_OP_reg15:
8681 case DW_OP_reg16:
8682 case DW_OP_reg17:
8683 case DW_OP_reg18:
8684 case DW_OP_reg19:
8685 case DW_OP_reg20:
8686 case DW_OP_reg21:
8687 case DW_OP_reg22:
8688 case DW_OP_reg23:
8689 case DW_OP_reg24:
8690 case DW_OP_reg25:
8691 case DW_OP_reg26:
8692 case DW_OP_reg27:
8693 case DW_OP_reg28:
8694 case DW_OP_reg29:
8695 case DW_OP_reg30:
8696 case DW_OP_reg31:
8697 next_index = index + 1;
8698 break;
8699
8700 case DW_OP_breg0:
8701 case DW_OP_breg1:
8702 case DW_OP_breg2:
8703 case DW_OP_breg3:
8704 case DW_OP_breg4:
8705 case DW_OP_breg5:
8706 case DW_OP_breg6:
8707 case DW_OP_breg7:
8708 case DW_OP_breg8:
8709 case DW_OP_breg9:
8710 case DW_OP_breg10:
8711 case DW_OP_breg11:
8712 case DW_OP_breg12:
8713 case DW_OP_breg13:
8714 case DW_OP_breg14:
8715 case DW_OP_breg15:
8716 case DW_OP_breg16:
8717 case DW_OP_breg17:
8718 case DW_OP_breg18:
8719 case DW_OP_breg19:
8720 case DW_OP_breg20:
8721 case DW_OP_breg21:
8722 case DW_OP_breg22:
8723 case DW_OP_breg23:
8724 case DW_OP_breg24:
8725 case DW_OP_breg25:
8726 case DW_OP_breg26:
8727 case DW_OP_breg27:
8728 case DW_OP_breg28:
8729 case DW_OP_breg29:
8730 case DW_OP_breg30:
8731 case DW_OP_breg31:
8732 next_index = index + 1;
8733 break;
8734
8735 case DW_OP_regx:
8736 next_index = index + 2;
8737 break;
8738
8739 case DW_OP_fbreg:
8740 next_index = index + 1;
8741 break;
8742
8743 case DW_OP_bregx:
8744 next_index = index + 1;
8745 break;
8746
8747 case DW_OP_GNU_variable_value:
8748 next_index = index + 1;
8749 break;
8750
8751 default:
8752 return false;
8753 }
8754
8755 expr_result r(false);
8756 ctxt.push(r);
8757
8758 return true;
8759}
8760
8761/// If the current operation in the dwarf expression represents a
8762/// manipulation of the stack of the DWARF Expression Virtual Machine
8763/// (aka DEVM), this function performs the operation and updates the
8764/// state of the DEVM. If the result of the operation represents a
8765/// constant value, then the accumulator of the DEVM is set to that
8766/// result's value, Otherwise, the DEVM accumulator is left with its
8767/// previous value.
8768///
8769/// @param expr the array of the dwarf expression operations to consider.
8770///
8771/// @param expr_len the lengths of @p ops array above.
8772///
8773/// @param index the index of the operation to interpret, in @p ops.
8774///
8775/// @param next_index the index of the operation to interpret at the
8776/// next step, after this function completed and returned. This is
8777/// set an output parameter that is set iff the function returns true.
8778///
8779/// @param ctxt the DEVM evaluation context.
8780///
8781/// @return true if the current operation actually manipulates the
8782/// DEVM stack, false otherwise.
8783static bool
8784op_manipulates_stack(Dwarf_Op* expr,
8785 size_t expr_len,
8786 size_t index,
8787 size_t& next_index,
8788 dwarf_expr_eval_context& ctxt)
8789{
8790 Dwarf_Op& op = expr[index];
8791 expr_result v;
8792
8793 switch (op.atom)
8794 {
8795 case DW_OP_dup:
8796 v = ctxt.stack.front();
8797 ctxt.push(v);
8798 break;
8799
8800 case DW_OP_drop:
8801 v = ctxt.stack.front();
8802 ctxt.pop();
8803 break;
8804
8805 case DW_OP_over:
8806 ABG_ASSERT(ctxt.stack.size() > 1);
8807 v = ctxt.stack[1];
8808 ctxt.push(v);
8809 break;
8810
8811 case DW_OP_pick:
8812 ABG_ASSERT(index + 1 < expr_len);
8813 v = op.number;
8814 ctxt.push(v);
8815 break;
8816
8817 case DW_OP_swap:
8818 ABG_ASSERT(ctxt.stack.size() > 1);
8819 v = ctxt.stack[1];
8820 ctxt.stack.erase(ctxt.stack.begin() + 1);
8821 ctxt.push(v);
8822 break;
8823
8824 case DW_OP_rot:
8825 ABG_ASSERT(ctxt.stack.size() > 2);
8826 v = ctxt.stack[2];
8827 ctxt.stack.erase(ctxt.stack.begin() + 2);
8828 ctxt.push(v);
8829 break;
8830
8831 case DW_OP_deref:
8832 case DW_OP_deref_size:
8833 ABG_ASSERT(ctxt.stack.size() > 0);
8834 ctxt.pop();
8835 v.is_const(false);
8836 ctxt.push(v);
8837 break;
8838
8839 case DW_OP_xderef:
8840 case DW_OP_xderef_size:
8841 ABG_ASSERT(ctxt.stack.size() > 1);
8842 ctxt.pop();
8843 ctxt.pop();
8844 v.is_const(false);
8845 ctxt.push(v);
8846 break;
8847
8848 case DW_OP_push_object_address:
8849 v.is_const(false);
8850 ctxt.push(v);
8851 break;
8852
8853 case DW_OP_form_tls_address:
8854 case DW_OP_GNU_push_tls_address:
8855 ABG_ASSERT(ctxt.stack.size() > 0);
8856 v = ctxt.pop();
8857 if (op.atom == DW_OP_form_tls_address)
8858 v.is_const(false);
8859 ctxt.push(v);
8860 break;
8861
8862 case DW_OP_call_frame_cfa:
8863 v.is_const(false);
8864 ctxt.push(v);
8865 break;
8866
8867 default:
8868 return false;
8869 }
8870
8871 if (v.is_const())
8872 ctxt.accum = v;
8873
8874 if (op.atom == DW_OP_form_tls_address
8875 || op.atom == DW_OP_GNU_push_tls_address)
8876 ctxt.set_tls_address(true);
8877 else
8878 ctxt.set_tls_address(false);
8879
8880 next_index = index + 1;
8881
8882 return true;
8883}
8884
8885/// If the current operation in the dwarf expression represents a push
8886/// of an arithmetic or logic operation onto the dwarf expr virtual
8887/// machine (aka DEVM), perform the operation and update the DEVM.
8888///
8889/// If the result of the operation is a constant, update the DEVM
8890/// accumulator with its value. Otherwise, the DEVM accumulator is
8891/// left with its previous value.
8892///
8893/// @param expr the array of the dwarf expression operations to consider.
8894///
8895/// @param expr_len the lengths of @p expr array above.
8896///
8897/// @param index the index of the operation to interpret, in @p expr.
8898///
8899/// @param next_index the index of the operation to interpret at the
8900/// next step, after this function completed and returned. This is
8901/// set an output parameter that is set iff the function returns true.
8902///
8903/// @param ctxt the DEVM evaluation context.
8904///
8905/// @return true if the current operation actually represent an
8906/// arithmetic or logic operation.
8907static bool
8908op_is_arith_logic(Dwarf_Op* expr,
8909 size_t expr_len,
8910 size_t index,
8911 size_t& next_index,
8912 dwarf_expr_eval_context& ctxt)
8913{
8914 ABG_ASSERT(index < expr_len);
8915
8916 Dwarf_Op& op = expr[index];
8917 expr_result val1, val2;
8918 bool result = false;
8919
8920 switch (op.atom)
8921 {
8922 case DW_OP_abs:
8923 ABG_ASSERT(ctxt.stack.size() > 0);
8924 val1 = ctxt.pop();
8925 val1 = val1.abs();
8926 ctxt.push(val1);
8927 result = true;
8928 break;
8929
8930 case DW_OP_and:
8931 ABG_ASSERT(ctxt.stack.size() > 1);
8932 val1 = ctxt.pop();
8933 val2 = ctxt.pop();
8934 ctxt.push(val1 & val2);
8935 break;
8936
8937 case DW_OP_div:
8938 ABG_ASSERT(ctxt.stack.size() > 1);
8939 val1 = ctxt.pop();
8940 val2 = ctxt.pop();
8941 if (!val1.is_const())
8942 val1 = 1;
8943 ctxt.push(val2 / val1);
8944 result = true;
8945 break;
8946
8947 case DW_OP_minus:
8948 ABG_ASSERT(ctxt.stack.size() > 1);
8949 val1 = ctxt.pop();
8950 val2 = ctxt.pop();
8951 ctxt.push(val2 - val1);
8952 result = true;
8953 break;
8954
8955 case DW_OP_mod:
8956 ABG_ASSERT(ctxt.stack.size() > 1);
8957 val1 = ctxt.pop();
8958 val2 = ctxt.pop();
8959 ctxt.push(val2 % val1);
8960 result = true;
8961 break;
8962
8963 case DW_OP_mul:
8964 ABG_ASSERT(ctxt.stack.size() > 1);
8965 val1 = ctxt.pop();
8966 val2 = ctxt.pop();
8967 ctxt.push(val2 * val1);
8968 result = true;
8969 break;
8970
8971 case DW_OP_neg:
8972 ABG_ASSERT(ctxt.stack.size() > 0);
8973 val1 = ctxt.pop();
8974 ctxt.push(-val1);
8975 result = true;
8976 break;
8977
8978 case DW_OP_not:
8979 ABG_ASSERT(ctxt.stack.size() > 0);
8980 val1 = ctxt.pop();
8981 ctxt.push(~val1);
8982 result = true;
8983 break;
8984
8985 case DW_OP_or:
8986 ABG_ASSERT(ctxt.stack.size() > 1);
8987 val1 = ctxt.pop();
8988 val2 = ctxt.pop();
8989 ctxt.push(val1 | val2);
8990 result = true;
8991 break;
8992
8993 case DW_OP_plus:
8994 ABG_ASSERT(ctxt.stack.size() > 1);
8995 val1 = ctxt.pop();
8996 val2 = ctxt.pop();
8997 ctxt.push(val2 + val1);
8998 result = true;
8999 break;
9000
9001 case DW_OP_plus_uconst:
9002 ABG_ASSERT(ctxt.stack.size() > 0);
9003 val1 = ctxt.pop();
9004 val1 += op.number;
9005 ctxt.push(val1);
9006 result = true;
9007 break;
9008
9009 case DW_OP_shl:
9010 ABG_ASSERT(ctxt.stack.size() > 1);
9011 val1 = ctxt.pop();
9012 val2 = ctxt.pop();
9013 ctxt.push(val2 << val1);
9014 result = true;
9015 break;
9016
9017 case DW_OP_shr:
9018 case DW_OP_shra:
9019 ABG_ASSERT(ctxt.stack.size() > 1);
9020 val1 = ctxt.pop();
9021 val2 = ctxt.pop();
9022 ctxt.push(val2 >> val1);
9023 result = true;
9024 break;
9025
9026 case DW_OP_xor:
9027 ABG_ASSERT(ctxt.stack.size() > 1);
9028 val1 = ctxt.pop();
9029 val2 = ctxt.pop();
9030 ctxt.push(val2 ^ val1);
9031 result = true;
9032 break;
9033
9034 default:
9035 break;
9036 }
9037
9038 if (result == true)
9039 {
9040 if (ctxt.stack.front().is_const())
9041 ctxt.accum = ctxt.stack.front();
9042
9043 next_index = index + 1;
9044 }
9045 return result;;
9046}
9047
9048/// If the current operation in the dwarf expression represents a push
9049/// of a control flow operation onto the dwarf expr virtual machine
9050/// (aka DEVM), perform the operation and update the DEVM.
9051///
9052/// If the result of the operation is a constant, update the DEVM
9053/// accumulator with its value. Otherwise, the DEVM accumulator is
9054/// left with its previous value.
9055///
9056/// @param expr the array of the dwarf expression operations to consider.
9057///
9058/// @param expr_len the lengths of @p expr array above.
9059///
9060/// @param index the index of the operation to interpret, in @p expr.
9061///
9062/// @param next_index the index of the operation to interpret at the
9063/// next step, after this function completed and returned. This is
9064/// set an output parameter that is set iff the function returns true.
9065///
9066/// @param ctxt the DEVM evaluation context.
9067///
9068/// @return true if the current operation actually represents a
9069/// control flow operation, false otherwise.
9070static bool
9071op_is_control_flow(Dwarf_Op* expr,
9072 size_t expr_len,
9073 size_t index,
9074 size_t& next_index,
9075 dwarf_expr_eval_context& ctxt)
9076{
9077 ABG_ASSERT(index < expr_len);
9078
9079 Dwarf_Op& op = expr[index];
9080 expr_result val1, val2;
9081
9082 switch (op.atom)
9083 {
9084 case DW_OP_eq:
9085 case DW_OP_ge:
9086 case DW_OP_gt:
9087 case DW_OP_le:
9088 case DW_OP_lt:
9089 case DW_OP_ne:
9090 {
9091 bool value = true;
9092 val1 = ctxt.pop();
9093 val2 = ctxt.pop();
9094 if (op.atom == DW_OP_eq)
9095 value = val2 == val1;
9096 else if (op.atom == DW_OP_ge)
9097 value = val2 >= val1;
9098 else if (op.atom == DW_OP_gt)
9099 value = val2 > val1;
9100 else if (op.atom == DW_OP_le)
9101 value = val2 <= val1;
9102 else if (op.atom == DW_OP_lt)
9103 value = val2 < val1;
9104 else if (op.atom == DW_OP_ne)
9105 value = val2 != val1;
9106
9107 val1 = value ? 1 : 0;
9108 ctxt.push(val1);
9109 }
9110 break;
9111
9112 case DW_OP_skip:
9113 if (op.number > 0)
9114 index += op.number - 1;
9115 break;
9116
9117 case DW_OP_bra:
9118 val1 = ctxt.pop();
9119 if (val1.const_value() != 0)
9120 index += val1.const_value() - 1;
9121 break;
9122
9123 case DW_OP_call2:
9124 case DW_OP_call4:
9125 case DW_OP_call_ref:
9126 case DW_OP_nop:
9127 break;
9128
9129 default:
9130 return false;
9131 }
9132
9133 if (ctxt.stack.front().is_const())
9134 ctxt.accum = ctxt.stack.front();
9135
9136 next_index = index + 1;
9137 return true;
9138}
9139
9140/// This function quickly evaluates a DWARF expression that is a
9141/// constant.
9142///
9143/// This is a "fast path" function that quickly evaluates a DWARF
9144/// expression that is only made of a DW_OP_plus_uconst operator.
9145///
9146/// This is a sub-routine of die_member_offset.
9147///
9148/// @param expr the DWARF expression to evaluate.
9149///
9150/// @param expr_len the length of the expression @p expr.
9151///
9152/// @param value out parameter. This is set to the result of the
9153/// evaluation of @p expr, iff this function returns true.
9154///
9155/// @return true iff the evaluation of @p expr went OK.
9156static bool
9157eval_quickly(Dwarf_Op* expr,
9158 uint64_t expr_len,
9159 int64_t& value)
9160{
9161 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
9162 {
9163 value = expr[0].number;
9164 return true;
9165 }
9166 return false;
9167}
9168
9169/// Evaluate the value of the last sub-expression that is a constant,
9170/// inside a given DWARF expression.
9171///
9172/// @param expr the DWARF expression to consider.
9173///
9174/// @param expr_len the length of the expression to consider.
9175///
9176/// @param value the resulting value of the last constant
9177/// sub-expression of the DWARF expression. This is set iff the
9178/// function returns true.
9179///
9180/// @param is_tls_address out parameter. This is set to true iff
9181/// the resulting value of the evaluation is a TLS (thread local
9182/// storage) address.
9183///
9184/// @param eval_ctxt the evaluation context to (re)use. Note that
9185/// this function initializes this context before using it.
9186///
9187/// @return true if the function could find a constant sub-expression
9188/// to evaluate, false otherwise.
9189static bool
9190eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9191 size_t expr_len,
9192 int64_t& value,
9193 bool& is_tls_address,
9194 dwarf_expr_eval_context &eval_ctxt)
9195{
9196 // Reset the evaluation context before evaluating the constant sub
9197 // expression contained in the DWARF expression 'expr'.
9198 eval_ctxt.reset();
9199
9200 size_t index = 0, next_index = 0;
9201 do
9202 {
9203 if (op_is_arith_logic(expr, expr_len, index,
9204 next_index, eval_ctxt)
9205 || op_pushes_constant_value(expr, expr_len, index,
9206 next_index, eval_ctxt)
9207 || op_manipulates_stack(expr, expr_len, index,
9208 next_index, eval_ctxt)
9209 || op_pushes_non_constant_value(expr, expr_len, index,
9210 next_index, eval_ctxt)
9211 || op_is_control_flow(expr, expr_len, index,
9212 next_index, eval_ctxt))
9213 ;
9214 else
9215 next_index = index + 1;
9216
9217 ABG_ASSERT(next_index > index);
9218 index = next_index;
9219 } while (index < expr_len);
9220
9221 is_tls_address = eval_ctxt.set_tls_address();
9222 if (eval_ctxt.accum.is_const())
9223 {
9224 value = eval_ctxt.accum;
9225 return true;
9226 }
9227 return false;
9228}
9229
9230/// Evaluate the value of the last sub-expression that is a constant,
9231/// inside a given DWARF expression.
9232///
9233/// @param expr the DWARF expression to consider.
9234///
9235/// @param expr_len the length of the expression to consider.
9236///
9237/// @param value the resulting value of the last constant
9238/// sub-expression of the DWARF expression. This is set iff the
9239/// function returns true.
9240///
9241/// @return true if the function could find a constant sub-expression
9242/// to evaluate, false otherwise.
9243static bool
9244eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9245 size_t expr_len,
9246 int64_t& value,
9247 bool& is_tls_address)
9248{
9249 dwarf_expr_eval_context eval_ctxt;
9250 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
9251 is_tls_address, eval_ctxt);
9252}
9253
9254// -----------------------------------
9255// </location expression evaluation>
9256// -----------------------------------
9257
9258/// Convert a DW_AT_bit_offset attribute value into the same value as
9259/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
9260///
9261/// On big endian machines, the value of the DW_AT_bit_offset
9262/// attribute + 8 * the value of the DW_AT_data_member_location
9263/// attribute is the same as the value of the DW_AT_data_bit_offset
9264/// attribute.
9265///
9266/// On little endian machines however, the situation is different.
9267/// The DW_AT_bit_offset value for a bit field is the number of bits
9268/// to the left of the most significant bit of the bit field, within
9269/// the integer value at DW_AT_data_member_location.
9270///
9271/// The DW_AT_data_bit_offset offset value is the number of bits to
9272/// the right of the least significant bit of the bit field, again
9273/// relative to the containing integer value.
9274///
9275/// In other words, DW_AT_data_bit_offset is what everybody would
9276/// instinctively think of as being the "offset of the bit field". 8 *
9277/// DW_AT_data_member_location + DW_AT_bit_offset however is very
9278/// counter-intuitive on little endian machines.
9279///
9280/// This function thus reads the value of a DW_AT_bit_offset property
9281/// of a DIE and converts it into what the DW_AT_data_bit_offset would
9282/// have been if it was present, ignoring the contribution of
9283/// DW_AT_data_member_location.
9284///
9285/// Note that DW_AT_bit_offset has been made obsolete starting from
9286/// DWARF5 (for GCC; Clang still emits it).
9287///
9288/// If you like coffee and it's not too late, now might be a good time
9289/// to have a coffee break. Otherwise if it's late at night, you
9290/// might want to consider an herbal tea break. Then come back to
9291/// read this.
9292///
9293///
9294/// In what follows, the bit fields are all contained within the first
9295/// whole int of the struct, so DW_AT_data_member_location is 0.
9296///
9297/// Okay, to have a better idea of what DW_AT_bit_offset and
9298/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
9299/// have bit fields data members defined as:
9300///
9301/// struct S
9302/// {
9303/// int j:5;
9304/// int k:6;
9305/// int m:5;
9306/// int n:8;
9307/// };
9308///
9309/// The below wonderful (at least!) ASCII art sketch describes the
9310/// layout of the bitfields of 'struct S' on a little endian machine.
9311/// You need to read the sketch from the bottom-up.
9312///
9313/// So please scroll down to its bottom. Note how the 32 bits integer
9314/// word containing the bit fields is laid out with its least
9315/// significant bit starting on the right hand side, at index 0.
9316///
9317/// Then slowly scroll up starting from there, and take the time to
9318/// read each line and see how the bit fields are laid out and what
9319/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
9320/// the bit fields.
9321///
9322/// DW_AT_bit_offset(n)
9323/// < - - - - - - >
9324/// | | n |
9325/// ^ ^< - - - - >^
9326/// DW_AT_data_bit_offset(n)
9327/// < - - - - - - - - - - - - - - - >
9328/// | |
9329/// ^ ^
9330/// DW_AT_bit_offset(m)
9331/// <--------------------------------->
9332/// | | m |
9333/// ^ ^< - >^
9334/// DW_AT_data_bit_offset(m)
9335/// < - - - - - - - - - - >
9336/// | |
9337/// ^ ^
9338/// DW_AT_bit_offset(k)
9339/// <-------------------------------------------->
9340/// | | k |
9341/// ^ ^< - - >^
9342/// DW_AT_data_bit_offset(k)
9343/// < - - - - >
9344/// | |
9345/// ^ ^
9346/// DW_AT_bit_offset(j)
9347/// <-------------------------------------------------------->
9348/// | |
9349/// ^ ^
9350/// n m k j
9351/// < - - - - - - > < - - - > < - - - - > < - - - >
9352///
9353/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9354/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
9355/// 31 27 23 16 15 11 10 6 5 4 0
9356///
9357/// So, the different bit fields all fit in one 32 bits word, assuming
9358/// the bit fields are tightly packed.
9359///
9360/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
9361/// on this little endian machine and let's see how it relates to
9362/// DW_AT_data_bit_offset of j.
9363///
9364/// DW_AT_bit_offset(j) would be equal to the number of bits from the
9365/// left of the 32 bits word (i.e from bit number 31) to the most
9366/// significant bit of the j bit field (i.e, bit number 4). Thus:
9367///
9368/// DW_AT_bit_offset(j) =
9369/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
9370///
9371/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
9372/// 32 bits word (i.e, bit number 0) to the lest significant bit of
9373/// the 'j' bit field (ie, bit number 0). Thus:
9374///
9375/// DW_AT_data_bit_offset(j) = 0.
9376///
9377/// More generally, we can notice that:
9378///
9379/// sizeof_in_bits(int) =
9380/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
9381///
9382/// It follows that:
9383///
9384/// DW_AT_data_bit_offset(j) =
9385/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
9386///
9387/// Thus:
9388///
9389/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
9390///
9391/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
9392/// from the right hand side of the word. It is what we would
9393/// intuitively think it is. DW_AT_bit_offset however is super
9394/// counter-intuitive, pfff.
9395///
9396/// Anyway, this general equation holds true for all bit fields.
9397///
9398/// Similarly, it follows that:
9399///
9400/// DW_AT_bit_offset(k) =
9401/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
9402///
9403/// Thus:
9404/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
9405///
9406///
9407/// Likewise:
9408///
9409/// DW_AT_bit_offset(m) =
9410/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
9411///
9412///
9413/// Thus:
9414/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
9415///
9416/// And:
9417///
9418///
9419/// Lastly:
9420///
9421/// DW_AT_bit_offset(n) =
9422/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
9423///
9424/// Thus:
9425/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
9426///
9427/// Luckily, the body of the function is much smaller than this
9428/// comment. Enjoy!
9429///
9430/// @param die the DIE to consider.
9431///
9432/// @param is_big_endian this is true iff the machine we are looking at
9433/// is big endian.
9434///
9435/// @param offset this is the output parameter into which the value of
9436/// the DW_AT_bit_offset is put, converted as if it was the value of
9437/// the DW_AT_data_bit_offset parameter, less the contribution of
9438/// DW_AT_data_member_location. This parameter is set iff the
9439/// function returns true.
9440///
9441/// @return true if DW_AT_bit_offset was found on @p die.
9442static bool
9443read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
9444 bool is_big_endian,
9445 uint64_t &offset)
9446{
9447 uint64_t off = 0;
9448 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
9449 return false;
9450
9451 if (is_big_endian)
9452 {
9453 offset = off;
9454 return true;
9455 }
9456
9457 // Okay, we are looking at a little endian machine. We need to
9458 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
9459 // have been. To understand this, you really need to read the
9460 // preliminary comment of this function.
9461 uint64_t containing_anonymous_object_size = 0;
9462 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
9463 containing_anonymous_object_size));
9464 containing_anonymous_object_size *= 8;
9465
9466 uint64_t bitfield_size = 0;
9467 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
9468 bitfield_size));
9469
9470 // As noted in the the preliminary comment of this function if we
9471 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
9472 // its DW_AT_bit_offset value, the equation is:
9473 //
9474 // DW_AT_data_bit_offset(k) =
9475 // sizeof_in_bits(containing_anonymous_object_size)
9476 // - DW_AT_data_bit_offset(k)
9477 // - sizeof_in_bits(k)
9478 offset = containing_anonymous_object_size - off - bitfield_size;
9479
9480 return true;
9481}
9482
9483/// Get the value of the DW_AT_data_member_location of the given DIE
9484/// attribute as an constant.
9485///
9486/// @param die the DIE to read the attribute from.
9487///
9488/// @param offset the attribute as a constant value. This is set iff
9489/// the function returns true.
9490///
9491/// @return true if the attribute exists and has a constant value. In
9492/// that case the offset is set to the value.
9493static bool
9494die_constant_data_member_location(const Dwarf_Die *die,
9495 int64_t& offset)
9496{
9497 if (!die)
9498 return false;
9499
9500 Dwarf_Attribute attr;
9501 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
9502 DW_AT_data_member_location,
9503 &attr))
9504 return false;
9505
9506 Dwarf_Word val;
9507 if (dwarf_formudata(&attr, &val) != 0)
9508 return false;
9509
9510 offset = val;
9511 return true;
9512}
9513
9514/// Get the offset of a struct/class member as represented by the
9515/// value of the DW_AT_data_member_location attribute.
9516///
9517/// There is a huge gotcha in here. The value of the
9518/// DW_AT_data_member_location is not necessarily a constant that one
9519/// would just read and be done with it. Rather, it can be a DWARF
9520/// expression that one has to interpret. In general, the offset can
9521/// be given by the DW_AT_data_bit_offset or by the
9522/// DW_AT_data_member_location attribute and optionally the
9523/// DW_AT_bit_offset attribute. The bit offset attributes are
9524/// always simple constants, but the DW_AT_data_member_location
9525/// attribute is a DWARF location expression.
9526///
9527/// When it's the DW_AT_data_member_location that is present,
9528/// there are three cases to possibly take into account:
9529///
9530/// 1/ The offset in the vtable where the offset of a virtual base
9531/// can be found, aka vptr offset. Given the address of a
9532/// given object O, the vptr offset for B is given by the
9533/// (DWARF) expression:
9534///
9535/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
9536///
9537/// where VIRTUAL_OFFSET is a constant value; In this case,
9538/// this function returns the constant VIRTUAL_OFFSET, as this
9539/// is enough to detect changes in a given virtual base
9540/// relative to the other virtual bases.
9541///
9542/// 2/ The offset of a regular data member. Given the address of
9543/// a struct object named O, the memory location for a
9544/// particular data member is given by the (DWARF) expression:
9545///
9546/// address(O) + OFFSET
9547///
9548/// where OFFSET is a constant. In this case, this function
9549/// returns the OFFSET constant.
9550///
9551/// 3/ The offset of a virtual member function in the virtual
9552/// pointer. The DWARF expression is a constant that designates
9553/// the offset of the function in the vtable. In this case this
9554/// function returns that constant.
9555///
9556/// @param rdr the DWARF reader to consider.
9557///
9558/// @param die the DIE to read the information from.
9559///
9560/// @param offset the resulting constant offset, in bits. This
9561/// argument is set iff the function returns true.
9562static bool
9563die_member_offset(const reader& rdr,
9564 const Dwarf_Die* die,
9565 int64_t& offset)
9566{
9567 Dwarf_Op* expr = NULL;
9568 size_t expr_len = 0;
9569 uint64_t bit_offset = 0;
9570
9571 // First let's see if the DW_AT_data_bit_offset attribute is
9572 // present.
9573 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
9574 {
9575 offset = bit_offset;
9576 return true;
9577 }
9578
9579 // First try to read DW_AT_data_member_location as a plain constant.
9580 // We do this because the generic method using die_location_expr
9581 // might hit a bug in elfutils libdw dwarf_location_expression only
9582 // fixed in elfutils 0.184+. The bug only triggers if the attribute
9583 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
9584 // handle all constants here because that is more consistent (and
9585 // slightly faster in the general case where the attribute isn't a
9586 // full DWARF expression).
9587 if (!die_constant_data_member_location(die, offset))
9588 {
9589 // Otherwise, let's see if the DW_AT_data_member_location
9590 // attribute and, optionally, the DW_AT_bit_offset attributes
9591 // are present.
9592 if (!die_location_expr(die, DW_AT_data_member_location,
9593 &expr, &expr_len))
9594 return false;
9595
9596 // The DW_AT_data_member_location attribute is present. Let's
9597 // evaluate it and get its constant sub-expression and return
9598 // that one.
9599 if (!eval_quickly(expr, expr_len, offset))
9600 {
9601 bool is_tls_address = false;
9602 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
9603 offset, is_tls_address,
9604 rdr.dwarf_expr_eval_ctxt()))
9605 return false;
9606 }
9607 }
9608 offset *= 8;
9609
9610 // On little endian machines, we need to convert the
9611 // DW_AT_bit_offset attribute into a relative offset to 8 *
9612 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
9613 // would be if it were used instead.
9614 //
9615 // In other words, before adding it to 8 *
9616 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9617 // converted into a human-understandable form that represents the
9618 // offset of the bitfield data member it describes. For details
9619 // about the conversion, please read the extensive comments of
9620 // read_and_convert_DW_at_bit_offset.
9621 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9622 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9623 offset += bit_offset;
9624
9625 return true;
9626}
9627
9628/// Read the value of the DW_AT_location attribute from a DIE,
9629/// evaluate the resulting DWARF expression and, if it's a constant
9630/// expression, return it.
9631///
9632/// @param die the DIE to consider.
9633///
9634/// @param address the resulting constant address. This is set iff
9635/// the function returns true.
9636///
9637/// @return true iff the whole sequence of action described above
9638/// could be completed normally.
9639static bool
9640die_location_address(Dwarf_Die* die,
9641 Dwarf_Addr& address,
9642 bool& is_tls_address)
9643{
9644 Dwarf_Op* expr = NULL;
9645 size_t expr_len = 0;
9646
9647 is_tls_address = false;
9648
9649 if (!die)
9650 return false;
9651
9652 Dwarf_Attribute attr;
9653 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9654 return false;
9655
9656 if (dwarf_getlocation(&attr, &expr, &expr_len))
9657 return false;
9658 // Ignore location expressions where reading them succeeded but
9659 // their length is 0.
9660 if (expr_len == 0)
9661 return false;
9662
9663 Dwarf_Attribute result;
9664 if (!dwarf_getlocation_attr(&attr, expr, &result))
9665 // A location that has been interpreted as an address.
9666 return !dwarf_formaddr(&result, &address);
9667
9668 // Just get the address out of the number field.
9669 address = expr->number;
9670 return true;
9671}
9672
9673/// Return the index of a function in its virtual table. That is,
9674/// return the value of the DW_AT_vtable_elem_location attribute.
9675///
9676/// @param die the DIE of the function to consider.
9677///
9678/// @param vindex the resulting index. This is set iff the function
9679/// returns true.
9680///
9681/// @return true if the DIE has a DW_AT_vtable_elem_location
9682/// attribute.
9683static bool
9684die_virtual_function_index(Dwarf_Die* die,
9685 int64_t& vindex)
9686{
9687 if (!die)
9688 return false;
9689
9690 Dwarf_Op* expr = NULL;
9691 size_t expr_len = 0;
9692 if (die_is_virtual(die))
9693 vindex = 0;
9694 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9695 &expr, &expr_len))
9696 return false;
9697
9698 int64_t i = 0;
9699 bool is_tls_addr = false;
9700 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9701 return false;
9702
9703 vindex = i;
9704 return true;
9705}
9706
9707/// Test if a given DIE represents an anonymous type.
9708///
9709/// Anonymous types we are interested in are classes, unions and
9710/// enumerations.
9711///
9712/// @param die the DIE to consider.
9713///
9714/// @return true iff @p die represents an anonymous type.
9715bool
9717{
9718 int tag = dwarf_tag(die);
9719
9720 if (tag == DW_TAG_class_type
9721 || tag == DW_TAG_structure_type
9722 || tag == DW_TAG_union_type
9723 || tag == DW_TAG_enumeration_type)
9724 return die_is_anonymous(die);
9725
9726 return false;
9727}
9728
9729/// Return the base of the internal name to represent an anonymous
9730/// type.
9731///
9732/// Typically, anonymous enums would be named
9733/// __anonymous_enum__<number>, anonymous struct or classes would be
9734/// named __anonymous_struct__<number> and anonymous unions would be
9735/// named __anonymous_union__<number>. The first part of these
9736/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9737/// the base name. This function returns that base name, depending on
9738/// the kind of type DIE we are looking at.
9739///
9740/// @param die the type DIE to look at. This function expects a type
9741/// DIE with an empty DW_AT_name property value (anonymous).
9742///
9743/// @return a string representing the base of the internal anonymous
9744/// name.
9745static string
9746get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9747{
9748 ABG_ASSERT(die_is_type(die));
9749 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9750
9751 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9752 string type_name;
9753 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9755 else if (tag == DW_TAG_union_type)
9757 else if (tag == DW_TAG_enumeration_type)
9759
9760 return type_name;
9761}
9762
9763/// Build a full internal anonymous type name.
9764///
9765/// @param base_name this is the base name as returned by the function
9766/// @ref get_internal_anonymous_die_prefix_name.
9767///
9768/// @param anonymous_type_index this is the index of the anonymous
9769/// type in its scope. That is, if there are more than one anonymous
9770/// types of a given kind in a scope, this index is what tells them
9771/// appart, starting from 0.
9772///
9773/// @return the built string, which is a concatenation of @p base_name
9774/// and @p anonymous_type_index.
9775static string
9776build_internal_anonymous_die_name(const string &base_name,
9777 size_t anonymous_type_index)
9778{
9779 string name = base_name;
9780 if (anonymous_type_index && !base_name.empty())
9781 {
9782 std::ostringstream o;
9783 o << base_name << anonymous_type_index;
9784 name = o.str();
9785 }
9786 return name;
9787}
9788
9789// ------------------------------------
9790// <DIE pretty printer>
9791// ------------------------------------
9792
9793/// Compute the qualified name of a DIE that represents a type.
9794///
9795/// For instance, if the DIE tag is DW_TAG_subprogram then this
9796/// function computes the name of the function *type*.
9797///
9798/// @param rdr the DWARF reader.
9799///
9800/// @param die the DIE to consider.
9801///
9802/// @param where_offset where in the are logically are in the DIE
9803/// stream.
9804///
9805/// @param guard the set of DIE offsets of the stack of DIEs involved
9806/// in the construction of the qualified name of the type. This set
9807/// is used to detect (and avoid) cycles in the stack of DIEs that is
9808/// going to be walked to compute the qualified type name.
9809///
9810/// @return a copy of the qualified name of the type.
9811static string
9812die_qualified_type_name(const reader& rdr,
9813 const Dwarf_Die* die,
9814 size_t where_offset,
9815 unordered_set<uint64_t>& guard)
9816{
9817 if (!die)
9818 return "";
9819
9820 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9821 if (tag == DW_TAG_compile_unit
9822 || tag == DW_TAG_partial_unit
9823 || tag == DW_TAG_type_unit)
9824 return "";
9825
9826 string name = die_name(die);
9827
9828 Dwarf_Die scope_die;
9829 if (!get_scope_die(rdr, die, where_offset, scope_die))
9830 return "";
9831
9832 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9833 string separator = colon_colon ? "::" : ".";
9834
9835 string repr;
9836
9837 switch (tag)
9838 {
9839 case DW_TAG_unspecified_type:
9840 break;
9841
9842 case DW_TAG_base_type:
9843 {
9844 abigail::ir::real_type real_type;
9845 if (parse_real_type(name, real_type))
9846 repr = real_type;
9847 else
9848 repr = name;
9849 }
9850 break;
9851
9852 case DW_TAG_typedef:
9853 ABG_ASSERT(!name.empty());
9854 // fall through
9855
9856 case DW_TAG_enumeration_type:
9857 case DW_TAG_structure_type:
9858 case DW_TAG_class_type:
9859 case DW_TAG_union_type:
9860 {
9861 if (die_is_anonymous(die))
9862 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
9863 /*one_line=*/true,
9864 /*qualed_name=*/false,
9865 where_offset, guard);
9866 else
9867 {
9868 string parent_name = die_qualified_name(rdr, &scope_die,
9869 where_offset, guard);
9870 repr = parent_name.empty() ? name : parent_name + separator + name;
9871 }
9872 }
9873 break;
9874
9875 case DW_TAG_const_type:
9876 case DW_TAG_volatile_type:
9877 case DW_TAG_restrict_type:
9878 {
9879 Dwarf_Die underlying_type_die;
9880 bool has_underlying_type_die =
9881 die_die_attribute(die, DW_AT_type, underlying_type_die);
9882
9883 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9884 break;
9885
9886 if (tag == DW_TAG_const_type)
9887 {
9888 if (has_underlying_type_die
9889 && die_is_reference_type(&underlying_type_die))
9890 // A reference is always const. So, to lower false
9891 // positive reports in diff computations, we consider a
9892 // const reference just as a reference. But we need to
9893 // keep the qualified-ness of the type. So we introduce
9894 // a 'no-op' qualifier here. Please remember that this
9895 // has to be kept in sync with what is done in
9896 // get_name_of_qualified_type. So if you change this
9897 // here, you have to change that code there too.
9898 repr = "";
9899 else if (!has_underlying_type_die
9900 || die_is_void_type(&underlying_type_die))
9901 {
9902 repr = "void";
9903 break;
9904 }
9905 else
9906 repr = "const";
9907 }
9908 else if (tag == DW_TAG_volatile_type)
9909 repr = "volatile";
9910 else if (tag == DW_TAG_restrict_type)
9911 repr = "restrict";
9912 else
9914
9915 string underlying_type_repr;
9916 if (has_underlying_type_die)
9917 underlying_type_repr =
9918 die_qualified_type_name(rdr, &underlying_type_die,
9919 where_offset, guard);
9920 else
9921 underlying_type_repr = "void";
9922
9923 if (underlying_type_repr.empty())
9924 repr.clear();
9925 else
9926 {
9927 if (has_underlying_type_die)
9928 {
9929 Dwarf_Die peeled;
9930 die_peel_qualified(&underlying_type_die, peeled);
9931 if (die_is_pointer_or_reference_type(&peeled))
9932 repr = underlying_type_repr + " " + repr;
9933 else
9934 repr += " " + underlying_type_repr;
9935 }
9936 else
9937 repr += " " + underlying_type_repr;
9938 }
9939 }
9940 break;
9941
9942 case DW_TAG_pointer_type:
9943 case DW_TAG_reference_type:
9944 case DW_TAG_rvalue_reference_type:
9945 {
9946 Dwarf_Die pointed_to_type_die;
9947 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9948 {
9949 if (tag == DW_TAG_pointer_type)
9950 repr = "void*";
9951 break;
9952 }
9953
9954 if (die_is_unspecified(&pointed_to_type_die))
9955 break;
9956
9957 string pointed_type_repr =
9958 die_qualified_type_name(rdr, &pointed_to_type_die,
9959 where_offset, guard);
9960
9961 repr = pointed_type_repr;
9962 if (repr.empty())
9963 break;
9964
9965 if (tag == DW_TAG_pointer_type)
9966 repr += "*";
9967 else if (tag == DW_TAG_reference_type)
9968 repr += "&";
9969 else if (tag == DW_TAG_rvalue_reference_type)
9970 repr += "&&";
9971 else
9973 }
9974 break;
9975
9976 case DW_TAG_subrange_type:
9977 {
9978 // In Ada, this one can be generated on its own, that is, not
9979 // as a sub-type of an array. So we need to support it on its
9980 // own. Note that when it's emitted as the sub-type of an
9981 // array like in C and C++, this is handled differently, for
9982 // now. But we try to make this usable by other languages
9983 // that are not Ada, even if we modelled it after Ada.
9984
9985 // So we build a subrange type for the sole purpose of using
9986 // the ::as_string() method of that type. So we don't add
9987 // that type to the current type tree being built.
9989 build_subrange_type(const_cast<reader&>(rdr),
9990 die, where_offset,
9991 /*associate_die_to_type=*/false);
9992 repr += s->as_string();
9993 break;
9994 }
9995
9996 case DW_TAG_array_type:
9997 {
9998 Dwarf_Die element_type_die;
9999 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10000 break;
10001 string element_type_name =
10002 die_qualified_type_name(rdr, &element_type_die, where_offset, guard);
10003 if (element_type_name.empty())
10004 break;
10005
10007 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
10008 die, subranges, where_offset,
10009 /*associate_type_to_die=*/false);
10010
10011 repr = element_type_name;
10013 }
10014 break;
10015
10016 case DW_TAG_subroutine_type:
10017 case DW_TAG_subprogram:
10018 {
10019 string return_type_name;
10020 string class_name;
10021 vector<string> parm_names;
10022 bool is_const = false;
10023 bool is_static = false;
10024 bool is_method_type = false;
10025 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10026 /*pretty_print=*/true,
10027 /*qualified_name=*/true,
10029 return_type_name, class_name,
10030 parm_names, is_const,
10031 is_static, guard);
10032 if (return_type_name.empty())
10033 return_type_name = "void";
10034
10035 repr = return_type_name;
10036
10037 if (is_method_type)
10038 // This is a method, so print the class name.
10039 repr += " (" + class_name + "::*)";
10040
10041 // Now parameters.
10042 repr += " (";
10043 for (vector<string>::const_iterator i = parm_names.begin();
10044 i != parm_names.end();
10045 ++i)
10046 {
10047 if (i != parm_names.begin())
10048 repr += ", ";
10049 repr += *i;
10050 }
10051 repr += ")";
10052
10053 }
10054 break;
10055
10056 case DW_TAG_string_type:
10057 case DW_TAG_ptr_to_member_type:
10058 case DW_TAG_set_type:
10059 case DW_TAG_file_type:
10060 case DW_TAG_packed_type:
10061 case DW_TAG_thrown_type:
10062 case DW_TAG_interface_type:
10063 case DW_TAG_shared_type:
10064 break;
10065 }
10066
10067 return repr;
10068}
10069
10070/// Compute the name of a type represented by a DIE.
10071///
10072/// @param rdr the reader to use.
10073///
10074/// @param die the type DIE to consider.
10075///
10076/// @param qualified_name if true then compute a qualified name.
10077///
10078/// @param where_offset where in the are logically are in the DIE
10079/// stream.
10080///
10081/// @param guard the set of DIE offsets of the stack of DIEs involved
10082/// in the construction of the name of the type. This set is used to
10083/// detect (and avoid) cycles in the stack of DIEs that is going to be
10084/// walked to compute the type name.
10085///
10086/// @return a copy of the string representing the type represented by
10087/// @p die.
10088static string
10089die_type_name(const reader& rdr,
10090 const Dwarf_Die* die,
10091 bool qualified_name,
10092 size_t where_offset,
10093 unordered_set<uint64_t>& guard)
10094{
10095 if (!die)
10096 return "";
10097
10098 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
10099 if (tag == DW_TAG_compile_unit
10100 || tag == DW_TAG_partial_unit
10101 || tag == DW_TAG_type_unit)
10102 return "";
10103
10104 string name = die_name(die);
10105
10106 Dwarf_Die scope_die;
10107 if (!get_scope_die(rdr, die, where_offset, scope_die))
10108 return "";
10109
10110 bool colon_colon = die_is_type(die) || die_is_namespace(die);
10111 string separator = colon_colon ? "::" : ".";
10112
10113 string repr;
10114
10115 switch (tag)
10116 {
10117 case DW_TAG_unspecified_type:
10118 break;
10119
10120 case DW_TAG_base_type:
10121 {
10122 abigail::ir::real_type int_type;
10123 if (parse_real_type(name, int_type))
10124 repr = int_type;
10125 else
10126 repr = name;
10127 }
10128 break;
10129
10130 case DW_TAG_typedef:
10131 ABG_ASSERT(!name.empty());
10132 // fall through
10133
10134 case DW_TAG_enumeration_type:
10135 case DW_TAG_structure_type:
10136 case DW_TAG_class_type:
10137 case DW_TAG_union_type:
10138 {
10139 if (die_is_anonymous(die))
10140 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
10141 /*one_line=*/true,
10142 /*qualed_name=*/false,
10143 where_offset,
10144 guard);
10145 else
10146 {
10147 string parent_name;
10148 if (qualified_name)
10149 {
10150 if (!is_anonymous_type_die(&scope_die))
10151 parent_name = die_qualified_name(rdr, &scope_die,
10152 where_offset, guard);
10153 }
10154 repr = parent_name.empty() ? name : parent_name + separator + name;
10155 }
10156 }
10157 break;
10158
10159 case DW_TAG_const_type:
10160 case DW_TAG_volatile_type:
10161 case DW_TAG_restrict_type:
10162 {
10163 Dwarf_Die underlying_type_die;
10164 bool has_underlying_type_die =
10165 die_die_attribute(die, DW_AT_type, underlying_type_die);
10166
10167 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
10168 break;
10169
10170 if (tag == DW_TAG_const_type)
10171 {
10172 if (has_underlying_type_die
10173 && die_is_reference_type(&underlying_type_die))
10174 // A reference is always const. So, to lower false
10175 // positive reports in diff computations, we consider a
10176 // const reference just as a reference. But we need to
10177 // keep the qualified-ness of the type. So we introduce
10178 // a 'no-op' qualifier here. Please remember that this
10179 // has to be kept in sync with what is done in
10180 // get_name_of_qualified_type. So if you change this
10181 // here, you have to change that code there too.
10182 repr = "";
10183 else if (!has_underlying_type_die
10184 || die_is_void_type(&underlying_type_die))
10185 {
10186 repr = "void";
10187 break;
10188 }
10189 else
10190 repr = "const";
10191 }
10192 else if (tag == DW_TAG_volatile_type)
10193 repr = "volatile";
10194 else if (tag == DW_TAG_restrict_type)
10195 repr = "restrict";
10196 else
10198
10199 string underlying_type_repr;
10200 if (has_underlying_type_die)
10201 underlying_type_repr =
10202 die_type_name(rdr, &underlying_type_die,
10203 qualified_name, where_offset,
10204 guard);
10205 else
10206 underlying_type_repr = "void";
10207
10208 if (underlying_type_repr.empty())
10209 repr.clear();
10210 else
10211 {
10212 if (has_underlying_type_die)
10213 {
10214 Dwarf_Die peeled;
10215 die_peel_qualified(&underlying_type_die, peeled);
10216 if (die_is_pointer_or_reference_type(&peeled))
10217 repr = underlying_type_repr + " " + repr;
10218 else
10219 repr += " " + underlying_type_repr;
10220 }
10221 else
10222 repr += " " + underlying_type_repr;
10223 }
10224 }
10225 break;
10226
10227 case DW_TAG_pointer_type:
10228 case DW_TAG_reference_type:
10229 case DW_TAG_rvalue_reference_type:
10230 {
10231 Dwarf_Die pointed_to_type_die;
10232 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
10233 {
10234 if (tag == DW_TAG_pointer_type)
10235 repr = "void*";
10236 break;
10237 }
10238
10239 if (die_is_unspecified(&pointed_to_type_die))
10240 break;
10241
10242 string pointed_type_repr =
10243 die_type_name(rdr, &pointed_to_type_die,
10244 qualified_name, where_offset,
10245 guard);
10246
10247 repr = pointed_type_repr;
10248 if (repr.empty())
10249 break;
10250
10251 if (tag == DW_TAG_pointer_type)
10252 repr += "*";
10253 else if (tag == DW_TAG_reference_type)
10254 repr += "&";
10255 else if (tag == DW_TAG_rvalue_reference_type)
10256 repr += "&&";
10257 else
10259 }
10260 break;
10261
10262 case DW_TAG_subrange_type:
10263 {
10264 // In Ada, this one can be generated on its own, that is, not
10265 // as a sub-type of an array. So we need to support it on its
10266 // own. Note that when it's emitted as the sub-type of an
10267 // array like in C and C++, this is handled differently, for
10268 // now. But we try to make this usable by other languages
10269 // that are not Ada, even if we modelled it after Ada.
10270
10271 // So we build a subrange type for the sole purpose of using
10272 // the ::as_string() method of that type. So we don't add
10273 // that type to the current type tree being built.
10275 build_subrange_type(const_cast<reader&>(rdr),
10276 die, where_offset,
10277 /*associate_die_to_type=*/false);
10278 repr += s->as_string();
10279 break;
10280 }
10281
10282 case DW_TAG_array_type:
10283 {
10284 Dwarf_Die element_type_die;
10285 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10286 break;
10287 string element_type_name =
10288 die_type_name(rdr, &element_type_die,
10289 qualified_name, where_offset,
10290 guard);
10291 if (element_type_name.empty())
10292 break;
10293
10295 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
10296 die, subranges, where_offset,
10297 /*associate_type_to_die=*/false);
10298
10299 repr = element_type_name;
10301 }
10302 break;
10303
10304 case DW_TAG_subroutine_type:
10305 case DW_TAG_subprogram:
10306 {
10307 string return_type_name;
10308 string class_name;
10309 vector<string> parm_names;
10310 bool is_const = false;
10311 bool is_static = false;
10312 bool is_method_type = false;
10313 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10314 /*pretty_print=*/true,
10315 qualified_name,
10317 return_type_name,
10318 class_name,
10319 parm_names, is_const,
10320 is_static, guard);
10321 if (return_type_name.empty())
10322 return_type_name = "void";
10323
10324 repr = return_type_name;
10325
10326 if (is_method_type)
10327 {
10328 // This is a method, so print the class name.
10329 repr += " (" + class_name + "::*)";
10330 }
10331
10332 // Now parameters.
10333 repr += " (";
10334 for (vector<string>::const_iterator i = parm_names.begin();
10335 i != parm_names.end();
10336 ++i)
10337 {
10338 if (i != parm_names.begin())
10339 repr += ", ";
10340 repr += *i;
10341 }
10342 repr += ")";
10343
10344 }
10345 break;
10346
10347 case DW_TAG_string_type:
10348 case DW_TAG_ptr_to_member_type:
10349 case DW_TAG_set_type:
10350 case DW_TAG_file_type:
10351 case DW_TAG_packed_type:
10352 case DW_TAG_thrown_type:
10353 case DW_TAG_interface_type:
10354 case DW_TAG_shared_type:
10355 break;
10356 }
10357
10358 return repr;
10359}
10360
10361/// Compute the name of a type represented by a DIE.
10362///
10363/// @param rdr the reader to use.
10364///
10365/// @param die the type DIE to consider.
10366///
10367/// @param qualified_name if true then compute a qualified name.
10368///
10369/// @param where_offset where in the are logically are in the DIE
10370/// stream.
10371///
10372/// @return a copy of the string representing the type represented by
10373/// @p die.
10374static string
10375die_type_name(const reader& rdr,
10376 const Dwarf_Die* die,
10377 bool qualified_name,
10378 size_t where_offset)
10379{
10380 unordered_set<uint64_t> guard;
10381 return die_type_name(rdr, die, qualified_name, where_offset, guard);
10382}
10383
10384/// Compute the qualified name of a decl represented by a given DIE.
10385///
10386/// For instance, for a DIE of tag DW_TAG_subprogram this function
10387/// computes the signature of the function *declaration*.
10388///
10389/// @param rdr the DWARF reader.
10390///
10391/// @param die the DIE to consider.
10392///
10393/// @param where_offset where we are logically at in the DIE stream.
10394///
10395/// @param guard the set of DIE offsets of the stack of DIEs involved
10396/// in the construction of the qualified name of the decl. This set
10397/// is used to detect (and avoid) cycles in the stack of DIEs that is
10398/// going to be walked to compute the qualified decl name.
10399///
10400/// @return a copy of the computed name.
10401static string
10402die_qualified_decl_name(const reader& rdr,
10403 const Dwarf_Die* die,
10404 size_t where_offset,
10405 unordered_set<uint64_t>& guard)
10406{
10407 if (!die || !die_is_decl(die))
10408 return "";
10409
10410 string name = die_name(die);
10411
10412 Dwarf_Die scope_die;
10413 if (!get_scope_die(rdr, die, where_offset, scope_die))
10414 return "";
10415
10416 string scope_name = die_qualified_name(rdr, &scope_die, where_offset, guard);
10417 string separator = "::";
10418
10419 string repr;
10420
10421 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10422 switch (tag)
10423 {
10424 case DW_TAG_namespace:
10425 case DW_TAG_member:
10426 case DW_TAG_variable:
10427 repr = scope_name.empty() ? name : scope_name + separator + name;
10428 break;
10429 case DW_TAG_subprogram:
10430 repr = die_function_signature(rdr, die,
10431 /*qualified_name=*/true,
10432 where_offset, guard);
10433 break;
10434
10435 case DW_TAG_unspecified_parameters:
10436 repr = "...";
10437 break;
10438
10439 case DW_TAG_formal_parameter:
10440 case DW_TAG_imported_declaration:
10441 case DW_TAG_GNU_template_template_param:
10442 case DW_TAG_GNU_template_parameter_pack:
10443 case DW_TAG_GNU_formal_parameter_pack:
10444 break;
10445 }
10446 return repr;
10447}
10448
10449/// Compute the qualified name of the artifact represented by a given
10450/// DIE.
10451///
10452/// If the DIE represents a type, then the function computes the name
10453/// of the type. Otherwise, if the DIE represents a decl then the
10454/// function computes the name of the decl. Note that a DIE of tag
10455/// DW_TAG_subprogram is going to be considered as a "type" -- just
10456/// like if it was a DW_TAG_subroutine_type.
10457///
10458/// @param rdr the DWARF reader.
10459///
10460/// @param die the DIE to consider.
10461///
10462/// @param where_offset where we are logically at in the DIE stream.
10463///
10464/// @param guard the set of DIE offsets of the stack of DIEs involved
10465/// in the construction of the qualified name of the DIE. This set is
10466/// used to detect (and avoid) cycles in the stack of DIEs that is
10467/// going to be walked to compute the qualified DIE name.
10468///
10469/// @return a copy of the computed name.
10470static string
10471die_qualified_name(const reader& rdr, const Dwarf_Die* die,
10472 size_t where, unordered_set<uint64_t>& guard)
10473{
10474 if (die_is_type(die))
10475 return die_qualified_type_name(rdr, die, where, guard);
10476 else if (die_is_decl(die))
10477 return die_qualified_decl_name(rdr, die, where, guard);
10478 return "";
10479}
10480
10481/// Compute the qualified name of the artifact represented by a given
10482/// DIE.
10483///
10484/// If the DIE represents a type, then the function computes the name
10485/// of the type. Otherwise, if the DIE represents a decl then the
10486/// function computes the name of the decl. Note that a DIE of tag
10487/// DW_TAG_subprogram is going to be considered as a "type" -- just
10488/// like if it was a DW_TAG_subroutine_type.
10489///
10490/// @param rdr the DWARF reader.
10491///
10492/// @param die the DIE to consider.
10493///
10494/// @param where_offset where we are logically at in the DIE stream.
10495///
10496/// @return a copy of the computed name.
10497static string
10498die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
10499{
10500 unordered_set<uint64_t> guard;
10501 return die_qualified_name(rdr, die, where, guard);
10502}
10503
10504/// Test if the qualified name of a given type should be empty.
10505///
10506/// The reason why the name of a DIE with a given tag would be empty
10507/// is that libabigail's internal representation doesn't yet support
10508/// that tag; or if the DIE's qualified name is built from names of
10509/// sub-types DIEs whose tags are not yet supported.
10510///
10511/// @param rdr the DWARF reader.
10512///
10513/// @param die the DIE to consider.
10514///
10515/// @param where where we are logically at, in the DIE stream.
10516///
10517/// @param qualified_name the qualified name of the DIE. This is set
10518/// only iff the function returns false.
10519///
10520/// @param guard the set of DIE offsets of the stack of DIEs involved
10521/// in the construction of the qualified name of the type. This set
10522/// is used to detect (and avoid) cycles in the stack of DIEs that is
10523/// going to be walked to compute the qualified type name.
10524///
10525/// @return true if the qualified name of the DIE is empty.
10526static bool
10527die_qualified_type_name_empty(const reader& rdr,
10528 const Dwarf_Die* die,
10529 size_t where, string &qualified_name,
10530 unordered_set<uint64_t>& guard)
10531{
10532 if (!die)
10533 return true;
10534
10535 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10536
10537 string qname;
10538 if (tag == DW_TAG_typedef
10539 || tag == DW_TAG_pointer_type
10540 || tag == DW_TAG_reference_type
10541 || tag == DW_TAG_rvalue_reference_type
10542 || tag == DW_TAG_array_type
10543 || tag == DW_TAG_const_type
10544 || tag == DW_TAG_volatile_type
10545 || tag == DW_TAG_restrict_type)
10546 {
10547 Dwarf_Die underlying_type_die;
10548 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
10549 {
10550 string name =
10551 die_qualified_type_name(rdr, &underlying_type_die, where, guard);
10552 if (name.empty())
10553 return true;
10554 }
10555 }
10556 else
10557 {
10558 string name = die_qualified_type_name(rdr, die, where, guard);
10559 if (name.empty())
10560 return true;
10561 }
10562
10563 qname = die_qualified_type_name(rdr, die, where, guard);
10564 if (qname.empty())
10565 return true;
10566
10567 qualified_name = qname;
10568 return false;
10569}
10570
10571/// Given the DIE that represents a function type, compute the names
10572/// of the following properties the function's type:
10573///
10574/// - return type
10575/// - enclosing class (if the function is a member function)
10576/// - function parameter types
10577///
10578/// When the function we are looking at is a member function, it also
10579/// tells if it's const.
10580///
10581/// @param rdr the DWARF reader.
10582///
10583/// @param die the DIE of the function or function type we are looking
10584/// at.
10585///
10586/// @param where_offset where we are logically at in the DIE stream.
10587///
10588/// @param pretty_print if set to yes, the type names are going to be
10589/// pretty-printed names; otherwise, they are just qualified type
10590/// names.
10591///
10592/// @param qualified_name if true then the names returned are
10593/// qualified.
10594///
10595/// @param is_method_type output parameter. This is set by the
10596/// function to true iff the DIE @p die represents a method.
10597///
10598/// @param return_type_name out parameter. This contains the name of
10599/// the return type of the function.
10600///
10601/// @param class_name out parameter. If the function is a member
10602/// function, this contains the name of the enclosing class.
10603///
10604/// @param parm_names out parameter. This vector is set to the names
10605/// of the types of the parameters of the function.
10606///
10607/// @param is_const out parameter. If the function is a member
10608/// function, this is set to true iff the member function is const.
10609///
10610/// @param is_static out parameter. If the function is a static
10611/// member function, then this is set to true.
10612///
10613/// @param guard the set of DIE offsets of the stack of DIEs involved
10614/// in the construction of the qualified name of the function type.
10615/// This set is used to detect (and avoid) cycles in the stack of DIEs
10616/// that is going to be walked to compute the qualified function type
10617/// name.
10618static void
10619die_return_and_parm_names_from_fn_type_die(const reader& rdr,
10620 const Dwarf_Die* die,
10621 size_t where_offset,
10622 bool pretty_print,
10623 bool qualified_name,
10624 bool &is_method_type,
10625 string &return_type_name,
10626 string &class_name,
10627 vector<string>& parm_names,
10628 bool& is_const,
10629 bool& is_static,
10630 unordered_set<uint64_t>& guard)
10631{
10632 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10633 if (guard.find(off) != guard.end())
10634 return;
10635 guard.insert(off);
10636
10637 Dwarf_Die child;
10638 Dwarf_Die ret_type_die;
10639 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
10640 return_type_name = "void";
10641 else
10642 {
10643 return_type_name =
10644 pretty_print
10645 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset, guard)
10646 : die_type_name(rdr, &ret_type_die, qualified_name,
10647 where_offset, guard);
10648 }
10649
10650 if (return_type_name.empty())
10651 return_type_name = "void";
10652
10653 Dwarf_Die object_pointer_die, class_die;
10655 die_function_type_is_method_type(rdr, die, where_offset,
10656 object_pointer_die,
10657 class_die, is_static);
10658
10659 is_const = false;
10660 if (is_method_type)
10661 {
10662 if (!is_anonymous_type_die(&class_die))
10663 class_name = die_type_name(rdr, &class_die, qualified_name,
10664 where_offset, guard);
10665
10666 Dwarf_Die this_pointer_die;
10667 Dwarf_Die pointed_to_die;
10668 if (!is_static
10669 && die_die_attribute(&object_pointer_die, DW_AT_type,
10670 this_pointer_die))
10671 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
10672 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
10673 is_const = true;
10674
10675 string fn_name = die_name(die);
10676 string non_qualified_class_name = die_name(&class_die);
10677 bool is_ctor = fn_name == non_qualified_class_name;
10678 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
10679
10680 if (is_ctor || is_dtor)
10681 return_type_name.clear();
10682 }
10683
10684 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
10685 do
10686 {
10687 int child_tag = dwarf_tag(&child);
10688 bool first_parm = true;
10689 if (child_tag == DW_TAG_formal_parameter)
10690 {
10691 // Skip the first parameter of a method.
10692 if (first_parm)
10693 {
10694 first_parm = false;
10695 if (is_method_type)
10696 continue;
10697 }
10698 Dwarf_Die parm_type_die;
10699 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
10700 continue;
10701 string qname =
10702 pretty_print
10703 ? rdr.get_die_pretty_representation(&parm_type_die,
10704 where_offset, guard)
10705 : die_type_name(rdr, &parm_type_die,
10706 qualified_name, where_offset, guard);
10707
10708 if (qname.empty())
10709 continue;
10710 parm_names.push_back(qname);
10711 }
10712 else if (child_tag == DW_TAG_unspecified_parameters)
10713 {
10714 // This is a variadic function parameter.
10715 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
10716 // After a DW_TAG_unspecified_parameters tag, we shouldn't
10717 // keep reading for parameters. The
10718 // unspecified_parameters TAG should be the last parameter
10719 // that we record. For instance, if there are multiple
10720 // DW_TAG_unspecified_parameters DIEs then we should care
10721 // only for the first one.
10722 break;
10723 }
10724 }
10725 while (dwarf_siblingof(&child, &child) == 0);
10726
10727 if (class_name.empty())
10728 {
10729 Dwarf_Die parent_die;
10730 if (get_parent_die(rdr, die, parent_die, where_offset))
10731 {
10732 if (die_is_class_type(&parent_die)
10733 && !is_anonymous_type_die(&parent_die))
10734 class_name = die_type_name(rdr, &parent_die,
10735 qualified_name,
10736 where_offset,
10737 guard);
10738 }
10739 }
10740
10741 guard.erase(off);
10742}
10743
10744/// This computes the signature of the a function declaration
10745/// represented by a DIE.
10746///
10747/// @param rdr the DWARF reader.
10748///
10749/// @param fn_die the DIE of the function to consider.
10750///
10751/// @param qualified_name if set to true then a qualified name is
10752/// going to be computed.
10753///
10754/// @param where_offset where we are logically at in the stream of
10755/// DIEs.
10756///
10757/// @param guard the set of DIE offsets of the stack of DIEs involved
10758/// in the construction of the signature of the function type. This
10759/// set is used to detect (and avoid) cycles in the stack of DIEs that
10760/// is going to be walked to compute the signature.
10761///
10762/// @return a copy of the computed function signature string.
10763static string
10764die_function_signature(const reader& rdr,
10765 const Dwarf_Die *fn_die,
10766 bool qualified_name,
10767 size_t where_offset,
10768 unordered_set<uint64_t>& guard)
10769{
10770
10772 bool has_lang = false;
10773 if ((has_lang = get_die_language(fn_die, lang)))
10774 {
10775 // In a binary originating from the C language, it's OK to use
10776 // the linkage name of the function as a key for the map which
10777 // is meant to reduce the number of DIE comparisons involved
10778 // during DIE canonicalization computation.
10779 if (is_c_language(lang))
10780 {
10781 string fn_name = die_linkage_name(fn_die);
10782 if (fn_name.empty())
10783 fn_name = die_name(fn_die);
10784 return fn_name;
10785 }
10786 }
10787
10788 // TODO: When we can structurally compare DIEs originating from C++
10789 // as well, we can use the linkage name of functions in C++ too, to
10790 // reduce the number of comparisons involved during DIE
10791 // canonicalization.
10792
10793 string return_type_name;
10794 Dwarf_Die ret_type_die;
10795 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
10796 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
10797 where_offset,
10798 guard);
10799
10800 if (return_type_name.empty())
10801 return_type_name = "void";
10802
10803 Dwarf_Die scope_die;
10804 string scope_name;
10805 if (qualified_name && get_scope_die(rdr, fn_die, where_offset, scope_die))
10806 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset, guard);
10807 string fn_name = die_name(fn_die);
10808 if (!scope_name.empty())
10809 fn_name = scope_name + "::" + fn_name;
10810
10811 string class_name;
10812 vector<string> parm_names;
10813 bool is_const = false;
10814 bool is_static = false;
10815 bool is_method_type = false;
10816
10817 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
10818 /*pretty_print=*/false,
10819 qualified_name, is_method_type,
10820 return_type_name, class_name,
10821 parm_names, is_const, is_static,
10822 guard);
10823
10824 bool is_virtual = die_is_virtual(fn_die);
10825
10826 string repr = is_method_type? "method" : "function";
10827 if (is_virtual)
10828 repr += " virtual";
10829
10830 if (!return_type_name.empty())
10831 repr += " " + return_type_name;
10832
10833 repr += " " + fn_name;
10834
10835 // Now parameters.
10836 repr += "(";
10837 bool some_parm_emitted = false;
10838 for (vector<string>::const_iterator i = parm_names.begin();
10839 i != parm_names.end();
10840 ++i)
10841 {
10842 if (i != parm_names.begin())
10843 {
10844 if (some_parm_emitted)
10845 repr += ", ";
10846 }
10847 else
10848 if (!is_static && is_method_type)
10849 // We are printing a non-static method name, skip the implicit "this"
10850 // parameter type.
10851 continue;
10852 repr += *i;
10853 some_parm_emitted = true;
10854 }
10855 repr += ")";
10856
10857 if (is_const)
10858 {
10860 repr += " const";
10861 }
10862
10863 return repr;
10864}
10865
10866/// Compute the flat representation string of a struct, class or union
10867/// type represented by a DIE.
10868///
10869/// The flat representation string looks like:
10870/// "struct {int foo; char blah;}.
10871///
10872/// That is useful to designate a struct (class or union) that is
10873/// anonymous.
10874///
10875/// @param rdr the DWARF reader to consider.
10876///
10877/// @param die the DIE of the type to return the flat representation
10878/// for.
10879///
10880/// @param indent the indentation string to use for the
10881/// representation.
10882///
10883/// @param one_line if true then the flat representation is
10884/// constructed on one line. Otherwise, each data member is
10885/// represented on its own line.
10886///
10887/// @param qualified_names if true then the data member (and their
10888/// type) names using in the representation are qualified.
10889///
10890/// @param where_offset where in the are logically are in the DIE
10891/// stream.
10892///
10893/// @param guard the set of DIE offsets of the stack of DIEs involved
10894/// in the construction of the flat representation of the type. This
10895/// set is used to detect (and avoid) cycles in the stack of DIEs that
10896/// is going to be walked to compute the flat representation.
10897static string
10898die_class_flat_representation(const reader& rdr,
10899 const Dwarf_Die* die,
10900 const string& indent,
10901 bool one_line,
10902 bool qualified_names,
10903 size_t where_offset,
10904 unordered_set<uint64_t>& guard)
10905{
10906 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10907
10908 string repr = indent;
10909 string local_indent = " ";
10910 string real_indent;
10911
10912 if (tag == DW_TAG_union_type)
10913 repr += "union";
10914 else if (tag == DW_TAG_structure_type)
10915 repr += "struct";
10916 else if (tag == DW_TAG_class_type)
10917 repr += "class";
10918 else
10920
10921 repr += " ";
10922
10923 if (die_is_anonymous(die))
10924 {
10925 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10926 if (guard.find(off) != guard.end())
10927 {
10928 repr += "{}";
10929 return repr;
10930 }
10931 guard.insert(off);
10932 }
10933
10934 if (!die_is_anonymous(die))
10935 repr += die_qualified_name(rdr, die, where_offset, guard);
10936
10937 repr += "{";
10938
10939 if (!one_line)
10940 repr += "\n";
10941
10942 Dwarf_Die member_child_die;
10943 bool first_sibling = true;
10944 for (bool got_it = get_member_child_die(die, &member_child_die);
10945 got_it;
10946 got_it = get_next_member_sibling_die(&member_child_die,
10947 &member_child_die),
10948 first_sibling = false)
10949 {
10950 // A member of the class is either a declaration or an anonymous
10951 // type. Otherwise, let's skip it.
10952 if (!die_is_decl(&member_child_die)
10953 && !(die_is_type(&member_child_die)
10954 && die_is_anonymous(&member_child_die)))
10955 continue;
10956
10957 if (one_line)
10958 real_indent = first_sibling ? "" : " " ;
10959 else
10960 real_indent = (first_sibling ? "": "\n") + indent + local_indent;
10961
10962 repr += real_indent;
10963
10964 repr += die_pretty_print_decl(rdr, &member_child_die,
10965 qualified_names,
10966 /*include_fns=*/false,
10967 where_offset,
10968 guard);
10969 repr += ";";
10970 }
10971
10972 if (one_line)
10973 repr += "}";
10974 else
10975 repr += indent + "}";
10976
10977 if (die_is_anonymous(die))
10978 {
10979 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10980 guard.erase(off);
10981 }
10982 return repr;
10983}
10984
10985/// Compute the flat representation string of a enum type represented
10986/// by a DIE.
10987///
10988/// The flat representation string looks like:
10989/// "enum {int foo; char blah;}.
10990///
10991/// That is useful to designate an enum that is anonymous.
10992///
10993/// @param rdr the DWARF reader to consider.
10994///
10995/// @param die the DIE of the type to return the flat representation
10996/// for.
10997///
10998/// @param indent the indentation string to use for the
10999/// representation.
11000///
11001/// @param one_line if true then the flat representation is
11002/// constructed on one line. Otherwise, each data member is
11003/// represented on its own line.
11004///
11005/// @param qualified_names if true then the data member (and their
11006/// type) names using in the representation are qualified.
11007///
11008/// @param where_offset where in the are logically are in the DIE
11009/// stream.
11010static string
11011die_enum_flat_representation(const reader& rdr,
11012 const Dwarf_Die* die,
11013 const string& indent,
11014 bool one_line,
11015 bool qualified_names,
11016 size_t where_offset)
11017{
11018 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11019
11020 std::ostringstream o;
11021 string local_indent = " ";
11022 string real_indent;
11023
11024 if (tag == DW_TAG_enumeration_type)
11025 o << "enum";
11026 else
11028
11029 o << " ";
11030
11031 if (!die_is_anonymous(die))
11032 o << (qualified_names
11033 ? die_qualified_name(rdr, die, where_offset)
11034 : die_name(die));
11035
11036 o << "{";
11037
11038 if (!one_line)
11039 o << "\n";
11040
11042 Dwarf_Die child;
11043 bool first_enumerator= true;
11044 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
11045 {
11046 do
11047 {
11048 if (dwarf_tag(&child) != DW_TAG_enumerator)
11049 continue;
11050
11051 string name, m;
11052 location l;
11053 die_loc_and_name(rdr, &child, l, name, m);
11054 uint64_t val = 0;
11055 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
11056
11057 if (one_line)
11058 real_indent = first_enumerator ? "" : ", ";
11059 else
11060 real_indent = first_enumerator ? "" : ",\n" + indent + local_indent;
11061 o << name + " = " << val;
11062 first_enumerator = false;
11063 }
11064 while (dwarf_siblingof(&child, &child) == 0);
11065 }
11066
11067 o << (one_line
11068 ? string("}")
11069 : "\n" + indent);
11070
11071 o << "}";
11072
11073 return o.str();
11074}
11075
11076/// Compute the flat representation string of a class or enum type
11077/// represented by a DIE.
11078///
11079/// The flat representation string looks like:
11080/// "union {int foo; char blah;}.
11081///
11082/// That is useful to designate a class or enum type that is
11083/// anonymous.
11084///
11085/// @param rdr the DWARF reader to consider.
11086///
11087/// @param die the DIE of the type to return the flat representation
11088/// for.
11089///
11090/// @param indent the indentation string to use for the
11091/// representation.
11092///
11093/// @param one_line if true then the flat representation is
11094/// constructed on one line. Otherwise, each data member is
11095/// represented on its own line.
11096///
11097/// @param qualified_names if true then the data member (and their
11098/// type) names using in the representation are qualified.
11099///
11100/// @param where_offset where in the are logically are in the DIE
11101/// stream.
11102///
11103/// @param guard the set of DIE offsets of the stack of DIEs involved
11104/// in the construction of the flat representation of the type. This
11105/// set is used to detect (and avoid) cycles in the stack of DIEs that
11106/// is going to be walked to compute the flat representation.
11107static string
11108die_class_or_enum_flat_representation(const reader& rdr,
11109 const Dwarf_Die* die,
11110 const string& indent,
11111 bool one_line,
11112 bool qualified_names,
11113 size_t where_offset,
11114 unordered_set<uint64_t>& guard)
11115{
11116 if (!die)
11117 return string();
11118
11119 string result;
11120 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11121
11122 switch (tag)
11123 {
11124 case DW_TAG_class_type:
11125 case DW_TAG_structure_type:
11126 case DW_TAG_union_type:
11127 result = die_class_flat_representation(rdr, die, indent,
11128 one_line, qualified_names,
11129 where_offset,
11130 guard);
11131 break;
11132 case DW_TAG_enumeration_type:
11133 result = die_enum_flat_representation(rdr, die, indent,
11134 one_line, qualified_names,
11135 where_offset);
11136 break;
11137 default:
11139 }
11140
11141 return result;
11142}
11143
11144/// Compute the flat representation string of a class or enum type
11145/// represented by a DIE.
11146///
11147/// The flat representation string looks like:
11148/// "union {int foo; char blah;}.
11149///
11150/// That is useful to designate a class or enum type that is
11151/// anonymous.
11152///
11153/// @param rdr the DWARF reader to consider.
11154///
11155/// @param die the DIE of the type to return the flat representation
11156/// for.
11157///
11158/// @param indent the indentation string to use for the
11159/// representation.
11160///
11161/// @param one_line if true then the flat representation is
11162/// constructed on one line. Otherwise, each data member is
11163/// represented on its own line.
11164///
11165/// @param qualified_names if true then the data member (and their
11166/// type) names using in the representation are qualified.
11167///
11168/// @param where_offset where in the are logically are in the DIE
11169/// stream.
11170static string
11171die_class_or_enum_flat_representation(const reader& rdr,
11172 const Dwarf_Die* die,
11173 const string& indent,
11174 bool one_line,
11175 bool qualified_names,
11176 size_t where_offset)
11177{
11178 unordered_set<uint64_t> guard;
11179 return die_class_or_enum_flat_representation(rdr, die, indent,
11180 one_line, qualified_names,
11181 where_offset, guard);
11182}
11183
11184/// Return a pretty string representation of a type, for internal purposes.
11185///
11186/// By internal purpose, we mean things like key-ing types for lookup
11187/// purposes and so on.
11188///
11189/// Note that this function is also used to pretty print functions.
11190/// For functions, it prints the *type* of the function.
11191///
11192/// @param rdr the context to use.
11193///
11194/// @param the DIE of the type to pretty print.
11195///
11196/// @param where_offset where we logically are placed when calling
11197/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11198/// entries.
11199///
11200/// @param guard the set of DIE offsets of the stack of DIEs involved
11201/// in the construction of the pretty representation of the type.
11202/// This set is used to detect (and avoid) cycles in the stack of DIEs
11203/// that is going to be walked to compute the pretty representation.
11204///
11205/// @return the resulting pretty representation.
11206static string
11207die_pretty_print_type(const reader& rdr,
11208 const Dwarf_Die* die,
11209 size_t where_offset,
11210 unordered_set<uint64_t>& guard)
11211{
11212 if (!die
11213 || (!die_is_type(die)
11214 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
11215 return "";
11216
11217 string repr;
11218
11219 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11220 switch (tag)
11221 {
11222 case DW_TAG_string_type:
11223 // For now, we won't try to go get the actual representation of
11224 // the string because this would make things more complicated;
11225 // for that we'd need to interpret some location expressions to
11226 // get the length of the string. And for dynamically allocated
11227 // strings, the result of the location expression evaluation
11228 // might not even be a constant. So at the moment I consider
11229 // this to be a lot of hassle for no great return. Until proven
11230 // otherwise, of course.
11231 repr = "string type";
11232
11233 case DW_TAG_unspecified_type:
11234 case DW_TAG_ptr_to_member_type:
11235 break;
11236
11237 case DW_TAG_namespace:
11238 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset,
11239 guard);
11240 break;
11241
11242 case DW_TAG_base_type:
11243 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11244 break;
11245
11246 case DW_TAG_typedef:
11247 {
11248 string qualified_name;
11249 if (!die_qualified_type_name_empty(rdr, die,
11250 where_offset,
11251 qualified_name,
11252 guard))
11253 repr = "typedef " + qualified_name;
11254 }
11255 break;
11256
11257 case DW_TAG_const_type:
11258 case DW_TAG_volatile_type:
11259 case DW_TAG_restrict_type:
11260 case DW_TAG_pointer_type:
11261 case DW_TAG_reference_type:
11262 case DW_TAG_rvalue_reference_type:
11263 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11264 break;
11265
11266 case DW_TAG_enumeration_type:
11267 {
11268 string qualified_name =
11269 rdr.get_die_qualified_type_name(die, where_offset, guard);
11270 repr = "enum " + qualified_name;
11271 }
11272 break;
11273
11274 case DW_TAG_structure_type:
11275 case DW_TAG_class_type:
11276 {
11277 string qualified_name =
11278 rdr.get_die_qualified_type_name(die, where_offset, guard);
11279 repr = "class " + qualified_name;
11280 }
11281 break;
11282
11283 case DW_TAG_union_type:
11284 {
11285 string qualified_name =
11286 rdr.get_die_qualified_type_name(die, where_offset, guard);
11287 repr = "union " + qualified_name;
11288 }
11289 break;
11290
11291 case DW_TAG_array_type:
11292 {
11293 Dwarf_Die element_type_die;
11294 if (!die_die_attribute(die, DW_AT_type, element_type_die))
11295 break;
11296 string element_type_name =
11297 rdr.get_die_qualified_type_name(&element_type_die,
11298 where_offset, guard);
11299 if (element_type_name.empty())
11300 break;
11301
11303 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
11304 /*associate_type_to_die=*/false);
11305
11306 repr = element_type_name;
11308 }
11309 break;
11310
11311 case DW_TAG_subrange_type:
11312 {
11313 // So this can be generated by Ada, on its own; that is, not
11314 // as a subtype of an array. In that case we need to handle
11315 // it properly.
11316
11317 // For now, we consider that the pretty printed name of the
11318 // subrange type is its name. We might need something more
11319 // advance, should the needs of the users get more
11320 // complicated.
11321 repr += die_qualified_type_name(rdr, die, where_offset, guard);
11322 }
11323 break;
11324
11325 case DW_TAG_subroutine_type:
11326 case DW_TAG_subprogram:
11327 {
11328 string return_type_name;
11329 string class_name;
11330 vector<string> parm_names;
11331 bool is_const = false;
11332 bool is_static = false;
11333 bool is_method_type = false;
11334 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
11335 /*pretty_print=*/true,
11336 /*qualified_name=*/true,
11338 return_type_name, class_name,
11339 parm_names, is_const,
11340 is_static, guard);
11341 if (!is_method_type)
11342 repr = "function type";
11343 else
11344 repr = "method type";
11345 repr += " " + rdr.get_die_qualified_type_name(die, where_offset, guard);
11346 }
11347 break;
11348
11349 case DW_TAG_set_type:
11350 case DW_TAG_file_type:
11351 case DW_TAG_packed_type:
11352 case DW_TAG_thrown_type:
11353 case DW_TAG_interface_type:
11354 case DW_TAG_shared_type:
11356 }
11357
11358 return repr;
11359}
11360
11361/// Return a pretty string representation of a declaration, for
11362/// internal purposes.
11363///
11364/// By internal purpose, we mean things like key-ing declarations for
11365/// lookup purposes and so on.
11366///
11367/// Note that this function is also used to pretty print functions.
11368/// For functions, it prints the signature of the function.
11369///
11370/// @param rdr the context to use.
11371///
11372/// @param die the DIE of the declaration to pretty print.
11373///
11374/// @param qualified_name if true then use qualified names.
11375///
11376/// @param where_offset where we logically are placed when calling
11377/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11378/// entries.
11379///
11380/// @param guard the set of DIE offsets of the stack of DIEs involved
11381/// in the construction of the pretty representation of the decl.
11382/// This set is used to detect (and avoid) cycles in the stack of DIEs
11383/// that is going to be walked to compute the pretty representation.
11384///
11385/// @return the resulting pretty representation.
11386static string
11387die_pretty_print_decl(const reader& rdr,
11388 const Dwarf_Die* die,
11389 bool qualified_name,
11390 bool include_fns,
11391 size_t where_offset,
11392 unordered_set<uint64_t>& guard)
11393{
11394 if (!die || !die_is_decl(die))
11395 return "";
11396
11397 string repr;
11398
11399 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11400 switch (tag)
11401 {
11402 case DW_TAG_namespace:
11403 repr = "namespace " + die_qualified_name(rdr, die, where_offset, guard);
11404 break;
11405
11406 case DW_TAG_member:
11407 case DW_TAG_variable:
11408 {
11409 string type_repr = "void";
11410 Dwarf_Die type_die;
11411 if (die_die_attribute(die, DW_AT_type, type_die))
11412 type_repr = die_type_name(rdr, &type_die,
11413 qualified_name,
11414 where_offset,
11415 guard);
11416 repr = (qualified_name
11417 ? die_qualified_name(rdr, die, where_offset, guard)
11418 : die_name(die));
11419
11420 if (repr.empty())
11421 repr = type_repr;
11422 else
11423 repr = type_repr + " " + repr;
11424 }
11425 break;
11426
11427 case DW_TAG_subprogram:
11428 if (include_fns)
11429 repr = die_function_signature(rdr, die, qualified_name,
11430 where_offset, guard);
11431 break;
11432
11433 default:
11434 break;
11435 }
11436 return repr;
11437}
11438
11439/// Compute the pretty printed representation of an artifact
11440/// represented by a DIE.
11441///
11442/// If the DIE is a type, compute the its pretty representation as a
11443/// type; otherwise, if it's a declaration, compute its pretty
11444/// representation as a declaration. Note for For instance, that a
11445/// DW_TAG_subprogram DIE is going to be represented as a function
11446/// *type*.
11447///
11448/// @param rdr the DWARF reader.
11449///
11450/// @param die the DIE to consider.
11451///
11452/// @param where_offset we in the DIE stream we are logically at.
11453///
11454/// @param guard the set of DIE offsets of the stack of DIEs involved
11455/// in the construction of the pretty representation of the DIe. This
11456/// set is used to detect (and avoid) cycles in the stack of DIEs that
11457/// is going to be walked to compute the pretty representation.
11458///
11459/// @return a copy of the pretty printed artifact.
11460static string
11461die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset,
11462 unordered_set<uint64_t>& guard)
11463{
11464 if (die_is_type(die))
11465 return die_pretty_print_type(rdr, die, where_offset, guard);
11466 else if (die_is_decl(die))
11467 return die_pretty_print_decl(rdr, die,
11468 /*qualified_names=*/true,
11469 /*include_fns=*/true,
11470 where_offset, guard);
11471 return "";
11472}
11473
11474// -----------------------------------
11475// </die pretty printer>
11476// -----------------------------------
11477
11478
11479// ----------------------------------
11480// <die comparison engine>
11481// ---------------------------------
11482
11483/// Compares two decls DIEs
11484///
11485/// This works only for DIEs emitted by the C language.
11486///
11487/// This implementation doesn't yet support namespaces.
11488///
11489/// This is a subroutine of compare_dies.
11490///
11491/// @return true iff @p l equals @p r.
11492static bool
11493compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
11494{
11495 ABG_ASSERT(l && r);
11496
11497 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11498 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11499 if (l_tag != r_tag)
11500 return false;
11501
11502 bool result = false;
11503
11504 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
11505 {
11506 // Fast path for functions and global variables.
11507 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
11508 result)
11509 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
11510 result))
11511 {
11512 if (!result)
11513 return false;
11514 }
11515
11516 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11517 result))
11518 {
11519 if (!result)
11520 return false;
11521 }
11522 return true;
11523 }
11524
11525 // Fast path for types.
11526 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11527 result))
11528 return result;
11529 return true;
11530}
11531
11532/// Test if at least one of two ODR-relevant DIEs is decl-only.
11533///
11534/// @param rdr the DWARF reader to consider.
11535///
11536/// @param l the first type DIE to consider.
11537///
11538/// @param r the second type DIE to consider.
11539///
11540/// @return true iff either @p l or @p r is decl-only and both are
11541/// ODR-relevant.
11542static bool
11543at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
11544 const Dwarf_Die *l,
11545 const Dwarf_Die *r)
11546{
11547 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
11548 return false;
11549
11550 if ((die_is_declaration_only(l) && die_has_no_child(l))
11551 || (die_is_declaration_only(r) && die_has_no_child(r)))
11552 return true;
11553 return false;
11554}
11555
11556/// Compares two type DIEs
11557///
11558/// This is a subroutine of compare_dies.
11559///
11560/// Note that this function doesn't look at the name of the DIEs.
11561/// Naming is taken into account by the function compare_as_decl_dies.
11562///
11563/// If the two DIEs are from a translation unit that is subject to the
11564/// ONE Definition Rule, then the function considers that if one DIE
11565/// is a declaration, then it's equivalent to the second. In that
11566/// case, the sizes of the two DIEs are not compared. This is so that
11567/// a declaration of a type compares equal to the definition of the
11568/// type.
11569///
11570/// @param rdr the DWARF reader to consider.
11571///
11572/// @param l the left operand of the comparison operator.
11573///
11574/// @param r the right operand of the comparison operator.
11575///
11576/// @return true iff @p l equals @p r.
11577static bool
11578compare_as_type_dies(const reader& rdr,
11579 const Dwarf_Die *l,
11580 const Dwarf_Die *r)
11581{
11582 ABG_ASSERT(l && r);
11583 ABG_ASSERT(die_is_type(l));
11584 ABG_ASSERT(die_is_type(r));
11585
11586 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
11587 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
11588 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11589 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
11590 // For now, we cannot compare DW_TAG_string_type because of its
11591 // string_length attribute that is a location descriptor that is
11592 // not necessarily a constant. So it's super hard to evaluate it
11593 // in a libabigail context. So for now, we just say that all
11594 // DW_TAG_string_type DIEs are different, by default.
11595 return false;
11596
11597 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11598 // A declaration of a type compares equal to the definition of the
11599 // type.
11600 return true;
11601
11602 uint64_t l_size = 0, r_size = 0;
11603 die_size_in_bits(l, l_size);
11604 die_size_in_bits(r, r_size);
11605
11606 return l_size == r_size;
11607}
11608
11609/// Compare two DIEs as decls (looking as their names etc) and as
11610/// types (looking at their size etc).
11611///
11612/// @param rdr the DWARF reader to consider.
11613///
11614/// @param l the first DIE to consider.
11615///
11616/// @param r the second DIE to consider.
11617///
11618/// @return TRUE iff @p l equals @p r as far as naming and size is
11619/// concerned.
11620static bool
11621compare_as_decl_and_type_dies(const reader &rdr,
11622 const Dwarf_Die *l,
11623 const Dwarf_Die *r)
11624{
11625 if (!compare_as_decl_dies(l, r)
11626 || !compare_as_type_dies(rdr, l, r))
11627 return false;
11628
11629 return true;
11630}
11631
11632/// Test if two DIEs representing function declarations have the same
11633/// linkage name, and thus are considered equal if they are C or C++,
11634/// because the two DIEs represent functions in the same binary.
11635///
11636/// If the DIEs don't have a linkage name, the function compares their
11637/// name. But in that case, the caller of the function must know that
11638/// in C++ for instance, that doesn't imply that the two functions are
11639/// equal.
11640///
11641/// @param l the first function DIE to consider.
11642///
11643/// @param r the second function DIE to consider.
11644///
11645/// @return true iff the function represented by @p l have the same
11646/// linkage name as the function represented by @p r.
11647static bool
11648fn_die_equal_by_linkage_name(const Dwarf_Die *l,
11649 const Dwarf_Die *r)
11650{
11651 if (!!l != !!r)
11652 return false;
11653
11654 if (!l)
11655 return false;
11656
11657 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11658 ABG_ASSERT(tag == DW_TAG_subprogram);
11659 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11660 ABG_ASSERT(tag == DW_TAG_subprogram);
11661
11662 string lname = die_name(l), rname = die_name(r);
11663 string llinkage_name = die_linkage_name(l),
11664 rlinkage_name = die_linkage_name(r);
11665
11666 if (die_is_in_c_or_cplusplus(l)
11667 && die_is_in_c_or_cplusplus(r))
11668 {
11669 if (!llinkage_name.empty() && !rlinkage_name.empty())
11670 return llinkage_name == rlinkage_name;
11671 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
11672 return false;
11673 else
11674 return lname == rname;
11675 }
11676
11677 return (!llinkage_name.empty()
11678 && !rlinkage_name.empty()
11679 && llinkage_name == rlinkage_name);
11680}
11681
11682/// Compare two DIEs in the context of DIE canonicalization.
11683///
11684/// If DIE canonicalization is on, the function compares the DIEs
11685/// canonically and structurally. The two types of comparison should
11686/// be equal, of course.
11687///
11688/// @param rdr the DWARF reader.
11689///
11690/// @param l_offset the offset of the first canonical DIE to compare.
11691///
11692/// @param r_offset the offset of the second canonical DIE to compare.
11693///
11694/// @param l_die_source the source of the DIE denoted by the offset @p
11695/// l_offset.
11696///
11697/// @param r_die_source the source of the DIE denoted by the offset @p
11698/// r_offset.
11699///
11700/// @param l_has_canonical_die_offset output parameter. Is set to
11701/// true if @p l_offset has a canonical DIE.
11702///
11703/// @param r_has_canonical_die_offset output parameter. Is set to
11704/// true if @p r_offset has a canonical DIE.
11705///
11706/// @param l_canonical_die_offset output parameter. If @p
11707/// l_has_canonical_die_offset is set to true, then this parameter is
11708/// set to the offset of the canonical DIE of the DIE designated by @p
11709/// l_offset.
11710static bool
11711try_canonical_die_comparison(const reader& rdr,
11712 Dwarf_Off l_offset, Dwarf_Off r_offset,
11713 die_source l_die_source, die_source r_die_source,
11714 bool& l_has_canonical_die_offset,
11715 bool& r_has_canonical_die_offset,
11716 Dwarf_Off& l_canonical_die_offset,
11717 Dwarf_Off& r_canonical_die_offset,
11718 bool& result)
11719{
11720#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11721 if (rdr.debug_die_canonicalization_is_on_
11722 && !rdr.use_canonical_die_comparison_)
11723 return false;
11724#endif
11725
11726
11727 l_has_canonical_die_offset =
11728 (l_canonical_die_offset =
11729 rdr.get_canonical_die_offset(l_offset, l_die_source,
11730 /*die_as_type=*/true));
11731
11732 r_has_canonical_die_offset =
11733 (r_canonical_die_offset =
11734 rdr.get_canonical_die_offset(r_offset, r_die_source,
11735 /*die_as_type=*/true));
11736
11737 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
11738 {
11739 result = (l_canonical_die_offset == r_canonical_die_offset);
11740 return true;
11741 }
11742
11743 return false;
11744}
11745
11746#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11747/// This function is called whenever a DIE comparison fails.
11748///
11749/// This function is intended for debugging purposes. The idea is for
11750/// hackers to set a breakpoint on this function so that they can
11751/// discover why exactly the comparison failed. They then can execute
11752/// the program from compare_dies_during_canonicalization, for
11753/// instance.
11754///
11755/// @param @l the left-hand side of the DIE comparison.
11756///
11757/// @param @r the right-hand side of the DIE comparison.
11758static void
11759notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
11760{
11761}
11762
11763#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
11764 notify_die_comparison_failed(l, r)
11765#else
11766#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
11767#endif
11768
11769/// A macro used to return from DIE comparison routines.
11770///
11771/// If the return value is false, the macro invokes the
11772/// notify_die_comparison_failed signalling function before returning.
11773/// That way, hackers willing to learn more about why the comparison
11774/// routine returned "false" can just set a breakpoint on
11775/// notify_die_comparison_failed and execute the program from
11776/// compare_dies_during_canonicalization, for instance.
11777///
11778/// @param value the value to return from the DIE comparison routines.
11779#define ABG_RETURN(value) \
11780 do \
11781 { \
11782 if ((value) == COMPARISON_RESULT_DIFFERENT) \
11783 { \
11784 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11785 } \
11786 return return_comparison_result(l, r, dies_being_compared, \
11787 value, aggregates_being_compared, \
11788 update_canonical_dies_on_the_fly); \
11789 } \
11790 while(false)
11791
11792/// A macro used to return the "false" boolean from DIE comparison
11793/// routines.
11794///
11795/// As the return value is false, the macro invokes the
11796/// notify_die_comparison_failed signalling function before returning.
11797///
11798/// @param value the value to return from the DIE comparison routines.
11799#define ABG_RETURN_FALSE \
11800 do \
11801 { \
11802 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11803 return return_comparison_result(l, r, dies_being_compared, \
11804 COMPARISON_RESULT_DIFFERENT, \
11805 aggregates_being_compared, \
11806 update_canonical_dies_on_the_fly); \
11807 } while(false)
11808
11809/// A macro to set the 'result' variable to 'false'.
11810///
11811/// The macro invokes the notify_die_comparison_failed function so
11812/// that the hacker can set a debugging breakpoint on
11813/// notify_die_comparison_failed to know where a DIE comparison failed
11814/// during compare_dies_during_canonicalization for instance.
11815///
11816/// @param result the 'result' variable to set.
11817///
11818/// @param l the first DIE of the comparison operation.
11819///
11820/// @param r the second DIE of the comparison operation.
11821#define SET_RESULT_TO_FALSE(result, l , r) \
11822 do \
11823 { \
11824 result = COMPARISON_RESULT_DIFFERENT; \
11825 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11826 } while(false)
11827
11828/// A macro to set the 'result' variable to a given value.
11829///
11830/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
11831/// invokes the notify_die_comparison_failed function so that the
11832/// hacker can set a debugging breakpoint on
11833/// notify_die_comparison_failed to know where a DIE comparison failed
11834/// during compare_dies_during_canonicalization for instance.
11835///
11836/// @param result the 'result' variable to set.
11837///
11838/// @param l the first DIE of the comparison operation.
11839///
11840/// @param r the second DIE of the comparison operation.
11841#define SET_RESULT_TO(result, value, l , r) \
11842 do \
11843 { \
11844 result = (value); \
11845 if (result == COMPARISON_RESULT_DIFFERENT) \
11846 { \
11847 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11848 } \
11849 } while(false)
11850
11851#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
11852 do \
11853 { \
11854 if (aggregates_being_compared.contains(dies_being_compared)) \
11855 { \
11856 result = COMPARISON_RESULT_CYCLE_DETECTED; \
11857 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
11858 ABG_RETURN(result); \
11859 } \
11860 } \
11861 while(false)
11862
11863/// Get the next member sibling of a given class or union member DIE.
11864///
11865/// @param die the DIE to consider.
11866///
11867/// @param member out parameter. This is set to the next member
11868/// sibling, iff the function returns TRUE.
11869///
11870/// @return TRUE iff the function set @p member to the next member
11871/// sibling DIE.
11872static bool
11873get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
11874{
11875 if (!die)
11876 return false;
11877
11878 bool found_member = false;
11879 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
11880 member) == 0);
11881 found_member;
11882 found_member = (dwarf_siblingof(member, member) == 0))
11883 {
11884 int tag = dwarf_tag(member);
11885 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
11886 break;
11887 }
11888
11889 return found_member;
11890}
11891
11892/// Get the first child DIE of a class/struct/union DIE that is a
11893/// member DIE.
11894///
11895/// Note that a member DIE is represented by a DWARF tag that is
11896/// either DW_TAG_member, DW_TAG_inheritance.
11897///
11898/// @param die the DIE to consider.
11899///
11900/// @param child out parameter. This is set to the first child DIE of
11901/// @p iff this function returns TRUE.
11902///
11903/// @return TRUE iff @p child is set to the first child DIE of @p die
11904/// that is a member DIE.
11905static bool
11906get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
11907{
11908 if (!die)
11909 return false;
11910
11911 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11912 ABG_ASSERT(tag == DW_TAG_structure_type
11913 || tag == DW_TAG_union_type
11914 || tag == DW_TAG_class_type);
11915
11916 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die), child) == 0);
11917
11918 if (!found_child)
11919 return false;
11920
11921 tag = dwarf_tag(child);
11922
11923 if (!(tag == DW_TAG_member
11924 || tag == DW_TAG_inheritance
11925 || tag == DW_TAG_subprogram))
11926 found_child = get_next_member_sibling_die(child, child);
11927
11928 return found_child;
11929}
11930
11931/// This is a sub-routine of return_comparison_result.
11932///
11933/// Propagate the canonical type of a the right-hand-side DIE to the
11934/// lef-hand-side DIE. This is a optimization that is done when the
11935/// two DIEs compare equal.
11936///
11937/// If the right-hand-side DIE is not canonicalized, the function
11938/// performs its canonicalization.
11939///
11940/// This optimization is performed only if
11941/// is_canon_type_to_be_propagated_tag returns true.
11942///
11943/// @param rdr the current context to consider.
11944///
11945/// @param l the left-hand-side DIE of the comparison. It's going to
11946/// receive the canonical type of the other DIE.
11947///
11948/// @param r the right-hand-side DIE of the comparison. Its canonical
11949/// type is propagated to @p l.
11950static void
11951maybe_propagate_canonical_type(const reader& rdr,
11952 const Dwarf_Die* l,
11953 const Dwarf_Die* r)
11954{
11955 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
11956 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11957
11958 if (l_tag != r_tag)
11959 return;
11960
11961 if (is_canon_type_to_be_propagated_tag(l_tag))
11962 propagate_canonical_type(rdr, l, r);
11963}
11964
11965/// Propagate the canonical type of a the right-hand-side DIE to the
11966/// left-hand-side DIE. This is a optimization that is done when the
11967/// two DIEs compare equal.
11968///
11969/// If the right-hand-side DIE is not canonicalized, the function
11970/// performs its canonicalization.
11971///
11972/// @param rdr the current context to consider.
11973///
11974/// @param l the left-hand-side DIE of the comparison. It's going to
11975/// receive the canonical type of the other DIE.
11976///
11977/// @param r the right-hand-side DIE of the comparison. Its canonical
11978/// type is propagated to @p l.
11979static void
11980propagate_canonical_type(const reader& rdr,
11981 const Dwarf_Die* l,
11982 const Dwarf_Die* r)
11983{
11984 ABG_ASSERT(l && r);
11985
11986 // If 'l' has no canonical DIE and if 'r' has one, then propagage
11987 // the canonical DIE of 'r' to 'l'.
11988 //
11989 // In case 'r' has no canonical DIE, then compute it, and then
11990 // propagate that canonical DIE to 'r'.
11991 const die_source l_source = rdr.get_die_source(l);
11992 const die_source r_source = rdr.get_die_source(r);
11993
11994 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
11995 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
11996 bool l_has_canonical_die_offset = false;
11997 bool r_has_canonical_die_offset = false;
11998 Dwarf_Off l_canonical_die_offset = 0;
11999 Dwarf_Off r_canonical_die_offset = 0;
12000
12001 l_has_canonical_die_offset =
12002 (l_canonical_die_offset =
12003 rdr.get_canonical_die_offset(l_offset, l_source,
12004 /*die_as_type=*/true));
12005
12006 r_has_canonical_die_offset =
12007 (r_canonical_die_offset =
12008 rdr.get_canonical_die_offset(r_offset, r_source,
12009 /*die_as_type=*/true));
12010
12011
12012 if (!l_has_canonical_die_offset
12013 && r_has_canonical_die_offset
12014 // A DIE can be equivalent only to another DIE of the same
12015 // source.
12016 && l_source == r_source)
12017 {
12018 ABG_ASSERT(r_canonical_die_offset);
12019 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
12020 /*die_as_type=*/true);
12021 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
12022 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
12023 rdr.canonical_propagated_count_++;
12024 }
12025}
12026
12027/// This function does the book keeping of comparison pairs necessary
12028/// to handle
12029///
12030/// * the detection of cycles during the comparison of aggregate
12031/// types, in conjuction with the macro
12032/// RETURN_IF_COMPARISON_CYCLE_DETECTED
12033///
12034/// * the handling of the canonical type propagation optimisation
12035/// to speed-up type canonicalization.
12036///
12037///
12038/// Note that this function is essentially a sub-routine of
12039/// compare_dies.
12040///
12041/// @param l the left-hand-side DIE being compared.
12042///
12043/// @param r the right-hand-side DIE being compared.
12044///
12045/// @param cur_dies the pair of die offsets of l and r. This is
12046/// redundant as it can been computed from @p l and @p r. However,
12047/// getting it as an argument is an optimization to avoid computing it
12048/// over and over again, given how often this function is invoked from
12049/// compare_dies.
12050///
12051/// @param return the result of comparing @p l against @p r.
12052///
12053/// @param comparison_stack the stack of pair of type DIEs being
12054/// compared.
12055///
12056/// @param do_propagate_canonical_type if true then the function
12057/// performs canonical DIEs propagation, meaning that if @p l equals
12058/// @p r and if @p r has a canonical type, then the canonical type of
12059/// @p l is set to the canonical type of @p r.
12060static comparison_result
12061return_comparison_result(const Dwarf_Die* l,
12062 const Dwarf_Die* r,
12063 const offset_pair_type& cur_dies,
12064 comparison_result result,
12065 offset_pairs_stack_type& comparison_stack,
12066 bool do_propagate_canonical_type = true)
12067{
12068 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
12069
12070 if (result == COMPARISON_RESULT_EQUAL)
12071 {
12072 // The result comparing the two types is "true", basically. So
12073 // let's propagate the canonical type of r onto l, so that we
12074 // don't need to compute the canonical type of r.
12075 if (do_propagate_canonical_type)
12076 {
12077 // Propagate canonical type.
12078 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12079
12080 // TODO: do we need to confirm any tentative canonical
12081 // propagation?
12082 }
12083 }
12084 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12085 {
12086 // So upon detection of the comparison cycle, compare_dies
12087 // returned early with the comparison result
12088 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
12089 // carry on with the comparison of all the OTHER sub-types of
12090 // the redundant type. If they all compare equal, then it means
12091 // the redundant type pair compared equal. Otherwise, it
12092 // compared different.
12093 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
12094 // Let's fall through to let the end of this function set the
12095 // result to COMPARISON_RESULT_UNKNOWN;
12096 }
12097 else if (result == COMPARISON_RESULT_UNKNOWN)
12098 {
12099 // Here is an introductory comment describing what we are going
12100 // to do in this case where the result of the comparison of the
12101 // current pair of type is not "false", basically.
12102 //
12103 // This means that we don't yet know what the result of
12104 // comparing these two types is, because one of the sub-types of
12105 // the types being compared is "redundant", meaning it appears
12106 // more than once in the comparison stack, so if we were to
12107 // naively try to carry on with the comparison member-wise, we'd
12108 // end up with an endless loop, a.k.a "comparison cycle".
12109 //
12110 // If the current type pair is redundant then:
12111 //
12112 // * This is a redundant type that has just been fully
12113 // compared. In that case, all the types that depend on
12114 // this redundant type and that have been tentatively
12115 // canonical-type-propagated must see their canonical types
12116 // "confirmed". This means that this type is going to be
12117 // considered as not being redundant anymore, meaning all
12118 // the types that depend on it must be updated as not being
12119 // dependant on it anymore, and the type itsef must be
12120 // removed from the map of redundant types.
12121 //
12122 // After the type's canonical-type-propagation is confirmed,
12123 // the result of its comparison must also be changed into
12124 // COMPARISON_RESULT_EQUAL.
12125 //
12126 // After that, If the current type depends on a redundant type,
12127 // then propagate its canonical type AND track it as having its
12128 // type being canonical-type-propagated.
12129 //
12130 // If the current type is not redundant however, then it must be
12131 // dependant on a redundant type. If it's not dependant on a
12132 // redundant type, then it must be of those types which
12133 // comparisons are not tracked for cycle, probably because they
12134 // are not aggregates. Otherwise, ABORT to understand why. I
12135 // believe this should not happen. In any case, after that
12136 // safety check is passed, we just need to return at this point.
12137
12138 if (comparison_stack.is_redundant(cur_dies)
12139 && comparison_stack.vect_.back() == cur_dies)
12140 {
12141 // We are in the case described above of a redundant type
12142 // that has been fully compared.
12143 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12144 comparison_stack.confirm_canonical_propagated_type(cur_dies);
12145
12146 result = COMPARISON_RESULT_EQUAL;
12147 }
12148 else if (is_canon_type_to_be_propagated_tag(l_tag)
12149 && comparison_stack.vect_.back() == cur_dies)
12150 {
12151 // The current type is not redundant. So, as described in
12152 // the introductory comment above, it must be dependant on a
12153 // redundant type.
12154 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
12155 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12156 // Then pass through.
12157 }
12158 }
12159 else if (result == COMPARISON_RESULT_DIFFERENT)
12160 {
12161 // Here is an introductory comment describing what we are going
12162 // to do in this case where the result of the comparison of the
12163 // current pair of type is "false", basically.
12164 //
12165 // If the type pair {l,r} is redundant then cancel the
12166 // canonical-type-propagation of all the dependant pairs that
12167 // depends on this redundant {l, r}. This means walk the types
12168 // that depends on {l, r} and cancel their
12169 // canonical-propagate-type, that means remove their canonical
12170 // types and mark them as not being canonically-propagated.
12171 // Also, erase their cached comparison results that was likely
12172 // set to COMPARISON_RESULT_UNKNOWN.
12173 //
12174 // Also, update the cached result for this pair, that was likely
12175 // to be COMPARISON_RESULT_UNKNOWN.
12176 if (comparison_stack.is_redundant(cur_dies)
12177 && comparison_stack.vect_.back() == cur_dies)
12178 comparison_stack.cancel_canonical_propagated_type(cur_dies);
12179 }
12180 else
12181 {
12182 // We should never reach here.
12184 }
12185
12186 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12187 result = COMPARISON_RESULT_UNKNOWN;
12188 else if (is_canon_type_to_be_propagated_tag(l_tag)
12189 && !comparison_stack.vect_.empty()
12190 && comparison_stack.vect_.back() == cur_dies)
12191 //Finally pop the pair types being compared from comparison_stack
12192 //iff {l,r} is on the top of the stack. If it's not, then it means
12193 //we are looking at a type that was detected as a being redundant
12194 //and thus hasn't been pushed to the stack yet gain.
12195 comparison_stack.erase(cur_dies);
12196
12197 maybe_cache_type_comparison_result(comparison_stack.rdr_,
12198 l_tag, cur_dies, result);
12199
12200 return result;
12201}
12202
12203/// Compare two DIEs emitted by a C compiler.
12204///
12205/// @param rdr the DWARF reader used to load the DWARF information.
12206///
12207/// @param l the left-hand-side argument of this comparison operator.
12208///
12209/// @param r the righ-hand-side argument of this comparison operator.
12210///
12211/// @param aggregates_being_compared this holds the names of the set
12212/// of aggregates being compared. It's used by the comparison
12213/// function to avoid recursing infinitely when faced with types
12214/// referencing themselves through pointers or references. By
12215/// default, just pass an empty instance of @ref istring_set_type to
12216/// it.
12217///
12218/// @param update_canonical_dies_on_the_fly if true, when two
12219/// sub-types compare equal (during the comparison of @p l and @p r)
12220/// update their canonical type. That way, two types of the same name
12221/// are structurally compared to each other only once. So the
12222/// non-linear structural comparison of two types of the same name
12223/// only happen once.
12224///
12225/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12226static comparison_result
12227compare_dies(const reader& rdr,
12228 const Dwarf_Die *l, const Dwarf_Die *r,
12229 offset_pairs_stack_type& aggregates_being_compared,
12230 bool update_canonical_dies_on_the_fly)
12231{
12232 ABG_ASSERT(l);
12233 ABG_ASSERT(r);
12234
12235 const die_source l_die_source = rdr.get_die_source(l);
12236 const die_source r_die_source = rdr.get_die_source(r);
12237
12238 offset_type l_offset =
12239 {
12240 l_die_source,
12241 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12242 };
12243
12244 offset_type r_offset =
12245 {
12246 r_die_source,
12247 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12248 };
12249
12250 offset_pair_type dies_being_compared(l_offset, r_offset);
12251
12252 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
12253 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12254
12255 if (l_tag != r_tag)
12257
12258 if (l_offset == r_offset)
12259 return COMPARISON_RESULT_EQUAL;
12260
12261 if (rdr.leverage_dwarf_factorization()
12262 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
12263 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
12264 if (l_offset != r_offset)
12265 return COMPARISON_RESULT_DIFFERENT;
12266
12267 comparison_result result = COMPARISON_RESULT_EQUAL;
12268 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
12269 dies_being_compared,
12270 result))
12271 return result;
12272
12273 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
12274 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
12275
12276 // If 'l' and 'r' already have canonical DIEs, then just compare the
12277 // offsets of their canonical DIEs.
12278 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
12279 {
12280 bool canonical_compare_result = false;
12281 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
12282 l_die_source, r_die_source,
12283 l_has_canonical_die_offset,
12284 r_has_canonical_die_offset,
12285 l_canonical_die_offset,
12286 r_canonical_die_offset,
12287 canonical_compare_result))
12288 {
12289 comparison_result result;
12290 SET_RESULT_TO(result,
12291 (canonical_compare_result
12292 ? COMPARISON_RESULT_EQUAL
12293 : COMPARISON_RESULT_DIFFERENT),
12294 l, r);
12295 return result;
12296 }
12297 }
12298
12299
12300
12301 switch (l_tag)
12302 {
12303 case DW_TAG_base_type:
12304 case DW_TAG_string_type:
12305 case DW_TAG_unspecified_type:
12306 if (!compare_as_decl_and_type_dies(rdr, l, r))
12307 SET_RESULT_TO_FALSE(result, l, r);
12308 break;
12309
12310 case DW_TAG_typedef:
12311 case DW_TAG_pointer_type:
12312 case DW_TAG_reference_type:
12313 case DW_TAG_rvalue_reference_type:
12314 case DW_TAG_const_type:
12315 case DW_TAG_volatile_type:
12316 case DW_TAG_restrict_type:
12317 {
12318 if (!compare_as_type_dies(rdr, l, r))
12319 {
12320 SET_RESULT_TO_FALSE(result, l, r);
12321 break;
12322 }
12323
12324 bool from_the_same_tu = false;
12325 if (!pointer_or_qual_die_of_anonymous_class_type(l)
12326 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
12327 && from_the_same_tu)
12328 {
12329 // These two typedefs, pointer, reference, or qualified
12330 // types have the same name and are defined in the same TU.
12331 // They thus ought to be the same.
12332 //
12333 // Note that pointers, reference or qualified types to
12334 // anonymous types are not taking into account here because
12335 // those always need to be structurally compared.
12336 SET_RESULT_TO_FALSE(result, l, r);
12337 break;
12338 }
12339 }
12340
12341 {
12342 // No fancy optimization in this case. We need to
12343 // structurally compare the two DIEs.
12344 Dwarf_Die lu_type_die, ru_type_die;
12345 bool lu_is_void, ru_is_void;
12346
12347 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
12348 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
12349
12350 if (lu_is_void && ru_is_void)
12351 result = COMPARISON_RESULT_EQUAL;
12352 else if (lu_is_void != ru_is_void)
12353 SET_RESULT_TO_FALSE(result, l, r);
12354 else
12355 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
12356 aggregates_being_compared,
12357 update_canonical_dies_on_the_fly);
12358 }
12359 break;
12360
12361 case DW_TAG_enumeration_type:
12362 if (!compare_as_decl_and_type_dies(rdr, l, r))
12363 SET_RESULT_TO_FALSE(result, l, r);
12364 else
12365 {
12366 // Walk the enumerators.
12367 Dwarf_Die l_enumtor, r_enumtor;
12368 bool found_l_enumtor = true, found_r_enumtor = true;
12369
12370 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12371 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
12372 &l_enumtor) == 0,
12373 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
12374 &r_enumtor) == 0;
12375 found_l_enumtor && found_r_enumtor;
12376 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
12377 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
12378 {
12379 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
12380 if ( l_tag != r_tag)
12381 {
12382 SET_RESULT_TO_FALSE(result, l, r);
12383 break;
12384 }
12385
12386 if (l_tag != DW_TAG_enumerator)
12387 continue;
12388
12389 uint64_t l_val = 0, r_val = 0;
12390 die_unsigned_constant_attribute(&l_enumtor,
12391 DW_AT_const_value,
12392 l_val);
12393 die_unsigned_constant_attribute(&r_enumtor,
12394 DW_AT_const_value,
12395 r_val);
12396 if (l_val != r_val)
12397 {
12398 SET_RESULT_TO_FALSE(result, l, r);
12399 break;
12400 }
12401 }
12402 if (found_l_enumtor != found_r_enumtor )
12403 SET_RESULT_TO_FALSE(result, l, r);
12404 }
12405 break;
12406
12407 case DW_TAG_structure_type:
12408 case DW_TAG_union_type:
12409 case DW_TAG_class_type:
12410 {
12411 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12412
12413 rdr.compare_count_++;
12414
12415 if (!compare_as_decl_and_type_dies(rdr, l, r))
12416 SET_RESULT_TO_FALSE(result, l, r);
12417 else if (rdr.options().assume_odr_for_cplusplus
12418 && rdr.odr_is_relevant(l)
12419 && rdr.odr_is_relevant(r)
12420 && !die_is_anonymous(l)
12421 && !die_is_anonymous(r))
12422 result = COMPARISON_RESULT_EQUAL;
12423 else
12424 {
12425 aggregates_being_compared.add(dies_being_compared);
12426
12427 Dwarf_Die l_member, r_member;
12428 bool found_l_member = true, found_r_member = true;
12429
12430 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12431 for (found_l_member = get_member_child_die(l, &l_member),
12432 found_r_member = get_member_child_die(r, &r_member);
12433 found_l_member && found_r_member;
12434 found_l_member = get_next_member_sibling_die(&l_member,
12435 &l_member),
12436 found_r_member = get_next_member_sibling_die(&r_member,
12437 &r_member))
12438 {
12439 int l_tag = dwarf_tag(&l_member),
12440 r_tag = dwarf_tag(&r_member);
12441
12442 if (l_tag != r_tag)
12443 {
12444 SET_RESULT_TO_FALSE(result, l, r);
12445 break;
12446 }
12447
12448 ABG_ASSERT(l_tag == DW_TAG_member
12449 || l_tag == DW_TAG_variable
12450 || l_tag == DW_TAG_inheritance
12451 || l_tag == DW_TAG_subprogram);
12452
12453 comparison_result local_result =
12454 compare_dies(rdr, &l_member, &r_member,
12455 aggregates_being_compared,
12456 update_canonical_dies_on_the_fly);
12457
12458 if (local_result == COMPARISON_RESULT_UNKNOWN)
12459 // Note that if the result of comparing any
12460 // sub-type is COMPARISON_RESULT_EQUAL, just
12461 // because we have at least one sub-type's
12462 // comparison being COMPARISON_RESULT_UNKNOWN
12463 // means that the comparison of this type will
12464 // return COMPARISON_RESULT_UNKNOWN to show
12465 // callers that this type (and all the types that
12466 // depend on it) depends on a redundant type
12467 result = local_result;
12468
12469 if (local_result == COMPARISON_RESULT_DIFFERENT)
12470 {
12471 SET_RESULT_TO_FALSE(result, l, r);
12472 break;
12473 }
12474 }
12475 if (found_l_member != found_r_member)
12476 {
12477 SET_RESULT_TO_FALSE(result, l, r);
12478 break;
12479 }
12480 }
12481 }
12482 break;
12483
12484 case DW_TAG_array_type:
12485 {
12486 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12487
12488 aggregates_being_compared.add(dies_being_compared);
12489
12490 rdr.compare_count_++;
12491
12492 Dwarf_Die l_child, r_child;
12493 bool found_l_child, found_r_child;
12494 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12495 &l_child) == 0,
12496 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12497 &r_child) == 0;
12498 found_l_child && found_r_child;
12499 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
12500 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
12501 {
12502 int l_child_tag = dwarf_tag(&l_child),
12503 r_child_tag = dwarf_tag(&r_child);
12504 if (l_child_tag == DW_TAG_subrange_type
12505 || r_child_tag == DW_TAG_subrange_type)
12506 {
12507 result = compare_dies(rdr, &l_child, &r_child,
12508 aggregates_being_compared,
12509 update_canonical_dies_on_the_fly);
12510 if (!result)
12511 {
12512 SET_RESULT_TO_FALSE(result, l, r);
12513 break;
12514 }
12515 }
12516 }
12517 if (found_l_child != found_r_child)
12518 SET_RESULT_TO_FALSE(result, l, r);
12519 // Compare the types of the elements of the array.
12520 Dwarf_Die ltype_die, rtype_die;
12521 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
12522 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
12523 ABG_ASSERT(found_ltype && found_rtype);
12524
12525 result = compare_dies(rdr, &ltype_die, &rtype_die,
12526 aggregates_being_compared,
12527 update_canonical_dies_on_the_fly);
12528 if (!result)
12530 }
12531 break;
12532
12533 case DW_TAG_subrange_type:
12534 {
12535 uint64_t l_lower_bound = 0, r_lower_bound = 0,
12536 l_upper_bound = 0, r_upper_bound = 0;
12537 bool l_lower_bound_set = false, r_lower_bound_set = false,
12538 l_upper_bound_set = false, r_upper_bound_set = false;
12539
12540 l_lower_bound_set =
12541 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
12542 r_lower_bound_set =
12543 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
12544
12545 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
12546 l_upper_bound))
12547 {
12548 uint64_t l_count = 0;
12549 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
12550 {
12551 l_upper_bound = l_lower_bound + l_count;
12552 l_upper_bound_set = true;
12553 if (l_upper_bound)
12554 --l_upper_bound;
12555 }
12556 }
12557 else
12558 l_upper_bound_set = true;
12559
12560 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
12561 r_upper_bound))
12562 {
12563 uint64_t r_count = 0;
12564 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
12565 {
12566 r_upper_bound = r_lower_bound + r_count;
12567 r_upper_bound_set = true;
12568 if (r_upper_bound)
12569 --r_upper_bound;
12570 }
12571 }
12572 else
12573 r_upper_bound_set = true;
12574
12575 if ((l_lower_bound_set != r_lower_bound_set)
12576 || (l_upper_bound_set != r_upper_bound_set)
12577 || (l_lower_bound != r_lower_bound)
12578 || (l_upper_bound != r_upper_bound))
12579 SET_RESULT_TO_FALSE(result, l, r);
12580 }
12581 break;
12582
12583 case DW_TAG_subroutine_type:
12584 case DW_TAG_subprogram:
12585 {
12586 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12587
12588 aggregates_being_compared.add(dies_being_compared);
12589
12590 rdr.compare_count_++;
12591
12592 if (l_tag == DW_TAG_subprogram
12593 && !fn_die_equal_by_linkage_name(l, r))
12594 {
12595 SET_RESULT_TO_FALSE(result, l, r);
12596 break;
12597 }
12598 else if (l_tag == DW_TAG_subprogram
12599 && die_is_in_c(l) && die_is_in_c(r))
12600 {
12601 result = COMPARISON_RESULT_EQUAL;
12602 break;
12603 }
12604 else if (!die_is_in_c(l) && !die_is_in_c(r))
12605 {
12606 // In C, we cannot have two different functions with the
12607 // same linkage name in a given binary. But here we are
12608 // looking at DIEs that don't originate from C. So we
12609 // need to compare return types and parameter types.
12610 Dwarf_Die l_return_type, r_return_type;
12611 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
12612 l_return_type);
12613 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
12614 r_return_type);
12615 if (l_return_type_is_void != r_return_type_is_void
12616 || (!l_return_type_is_void
12617 && !compare_dies(rdr,
12618 &l_return_type, &r_return_type,
12619 aggregates_being_compared,
12620 update_canonical_dies_on_the_fly)))
12621 SET_RESULT_TO_FALSE(result, l, r);
12622 else
12623 {
12624 Dwarf_Die l_child, r_child;
12625 bool found_l_child, found_r_child;
12626 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12627 &l_child) == 0,
12628 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12629 &r_child) == 0;
12630 found_l_child && found_r_child;
12631 found_l_child = dwarf_siblingof(&l_child,
12632 &l_child) == 0,
12633 found_r_child = dwarf_siblingof(&r_child,
12634 &r_child)==0)
12635 {
12636 int l_child_tag = dwarf_tag(&l_child);
12637 int r_child_tag = dwarf_tag(&r_child);
12638 comparison_result local_result =
12639 COMPARISON_RESULT_EQUAL;
12640 if (l_child_tag != r_child_tag)
12641 local_result = COMPARISON_RESULT_DIFFERENT;
12642 if (l_child_tag == DW_TAG_formal_parameter)
12643 local_result =
12644 compare_dies(rdr, &l_child, &r_child,
12645 aggregates_being_compared,
12646 update_canonical_dies_on_the_fly);
12647 if (local_result == COMPARISON_RESULT_DIFFERENT)
12648 {
12649 result = local_result;
12650 SET_RESULT_TO_FALSE(result, l, r);
12651 break;
12652 }
12653 if (local_result == COMPARISON_RESULT_UNKNOWN)
12654 // Note that if the result of comparing any
12655 // sub-type is COMPARISON_RESULT_EQUAL, just
12656 // because we have at least one sub-type's
12657 // comparison being COMPARISON_RESULT_UNKNOWN
12658 // means that the comparison of this type will
12659 // return COMPARISON_RESULT_UNKNOWN to show
12660 // callers that this type (and all the types
12661 // that depend on it) depends on a redundant
12662 // type and so, can't be
12663 // canonical-type-propagated.
12664 result = local_result;
12665 }
12666 if (found_l_child != found_r_child)
12667 {
12668 SET_RESULT_TO_FALSE(result, l, r);
12669 break;
12670 }
12671 }
12672 }
12673 }
12674 break;
12675
12676 case DW_TAG_formal_parameter:
12677 {
12678 Dwarf_Die l_type, r_type;
12679 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
12680 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
12681 if (l_type_is_void != r_type_is_void)
12682 SET_RESULT_TO_FALSE(result, l, r);
12683 else if (!l_type_is_void)
12684 {
12685 comparison_result local_result =
12686 compare_dies(rdr, &l_type, &r_type,
12687 aggregates_being_compared,
12688 update_canonical_dies_on_the_fly);
12689 SET_RESULT_TO(result, local_result, l, r);
12690 }
12691 }
12692 break;
12693
12694 case DW_TAG_variable:
12695 case DW_TAG_member:
12696 if (compare_as_decl_dies(l, r))
12697 {
12698 // Compare the offsets of the data members
12699 if (l_tag == DW_TAG_member)
12700 {
12701 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12702 die_member_offset(rdr, l, l_offset_in_bits);
12703 die_member_offset(rdr, r, r_offset_in_bits);
12704 if (l_offset_in_bits != r_offset_in_bits)
12705 SET_RESULT_TO_FALSE(result, l, r);
12706 }
12707 if (result)
12708 {
12709 // Compare the types of the data members or variables.
12710 Dwarf_Die l_type, r_type;
12711 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12712 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12713 comparison_result local_result =
12714 compare_dies(rdr, &l_type, &r_type,
12715 aggregates_being_compared,
12716 update_canonical_dies_on_the_fly);
12717 SET_RESULT_TO(result, local_result, l, r);
12718 }
12719 }
12720 else
12721 SET_RESULT_TO_FALSE(result, l, r);
12722 break;
12723
12724 case DW_TAG_inheritance:
12725 {
12726 Dwarf_Die l_type, r_type;
12727 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12728 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12729 result = compare_dies(rdr, &l_type, &r_type,
12730 aggregates_being_compared,
12731 update_canonical_dies_on_the_fly);
12732 if (!result)
12733 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12734
12735 uint64_t l_a = 0, r_a = 0;
12736 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
12737 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
12738 if (l_a != r_a)
12739 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12740
12741 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
12742 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
12743 if (l_a != r_a)
12744 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12745
12746 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12747 die_member_offset(rdr, l, l_offset_in_bits);
12748 die_member_offset(rdr, r, r_offset_in_bits);
12749 if (l_offset_in_bits != r_offset_in_bits)
12750 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12751 }
12752 break;
12753
12754 case DW_TAG_ptr_to_member_type:
12755 {
12756 bool comp_result = false;
12757 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
12758 if (!comp_result)
12759 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12760
12761 Dwarf_Die l_type, r_type;
12762 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12763 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12764 result = compare_dies(rdr, &l_type, &r_type,
12765 aggregates_being_compared,
12766 update_canonical_dies_on_the_fly);
12767 if (!result)
12768 ABG_RETURN(result);
12769
12770 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
12771 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
12772 result = compare_dies(rdr, &l_type, &r_type,
12773 aggregates_being_compared,
12774 update_canonical_dies_on_the_fly);
12775 if (!result)
12776 ABG_RETURN(result);
12777 }
12778 break;
12779
12780 case DW_TAG_enumerator:
12781 case DW_TAG_packed_type:
12782 case DW_TAG_set_type:
12783 case DW_TAG_file_type:
12784 case DW_TAG_thrown_type:
12785 case DW_TAG_interface_type:
12786 case DW_TAG_shared_type:
12787 case DW_TAG_compile_unit:
12788 case DW_TAG_namespace:
12789 case DW_TAG_module:
12790 case DW_TAG_constant:
12791 case DW_TAG_partial_unit:
12792 case DW_TAG_imported_unit:
12793 case DW_TAG_dwarf_procedure:
12794 case DW_TAG_imported_declaration:
12795 case DW_TAG_entry_point:
12796 case DW_TAG_label:
12797 case DW_TAG_lexical_block:
12798 case DW_TAG_unspecified_parameters:
12799 case DW_TAG_variant:
12800 case DW_TAG_common_block:
12801 case DW_TAG_common_inclusion:
12802 case DW_TAG_inlined_subroutine:
12803 case DW_TAG_with_stmt:
12804 case DW_TAG_access_declaration:
12805 case DW_TAG_catch_block:
12806 case DW_TAG_friend:
12807 case DW_TAG_namelist:
12808 case DW_TAG_namelist_item:
12809 case DW_TAG_template_type_parameter:
12810 case DW_TAG_template_value_parameter:
12811 case DW_TAG_try_block:
12812 case DW_TAG_variant_part:
12813 case DW_TAG_imported_module:
12814 case DW_TAG_condition:
12815 case DW_TAG_type_unit:
12816 case DW_TAG_template_alias:
12817 case DW_TAG_lo_user:
12818 case DW_TAG_MIPS_loop:
12819 case DW_TAG_format_label:
12820 case DW_TAG_function_template:
12821 case DW_TAG_class_template:
12822 case DW_TAG_GNU_BINCL:
12823 case DW_TAG_GNU_EINCL:
12824 case DW_TAG_GNU_template_template_param:
12825 case DW_TAG_GNU_template_parameter_pack:
12826 case DW_TAG_GNU_formal_parameter_pack:
12827 case DW_TAG_GNU_call_site:
12828 case DW_TAG_GNU_call_site_parameter:
12829 case DW_TAG_hi_user:
12830#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12831 if (rdr.debug_die_canonicalization_is_on_)
12833#endif
12835 break;
12836 }
12837
12838 ABG_RETURN(result);
12839}
12840
12841/// Compare two DIEs emitted by a C compiler.
12842///
12843/// @param rdr the DWARF reader used to load the DWARF information.
12844///
12845/// @param l the left-hand-side argument of this comparison operator.
12846///
12847/// @param r the righ-hand-side argument of this comparison operator.
12848///
12849/// @param update_canonical_dies_on_the_fly if yes, then this function
12850/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
12851/// comparing l and r. This helps in making so that sub-type DIEs of
12852/// 'l' and 'r' are compared structurally only once. This is how we
12853/// turn this exponential comparison problem into a problem that is a
12854/// closer to a linear one.
12855///
12856/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12857static comparison_result
12858compare_dies(const reader& rdr,
12859 const Dwarf_Die *l,
12860 const Dwarf_Die *r,
12861 bool update_canonical_dies_on_the_fly)
12862{
12863 offset_pairs_stack_type aggregates_being_compared(rdr);
12864 return compare_dies(rdr, l, r, aggregates_being_compared,
12865 update_canonical_dies_on_the_fly);
12866}
12867
12868/// Compare two DIEs for the purpose of canonicalization.
12869///
12870/// This is a sub-routine of reader::get_canonical_die.
12871///
12872/// When DIE canonicalization debugging is on, this function performs
12873/// both structural and canonical comparison. It expects that both
12874/// comparison yield the same result.
12875///
12876/// @param rdr the DWARF reader.
12877///
12878/// @param l the left-hand-side comparison operand DIE.
12879///
12880/// @param r the right-hand-side comparison operand DIE.
12881///
12882/// @param update_canonical_dies_on_the_fly if true, then some
12883/// aggregate DIEs will see their canonical types propagated.
12884///
12885/// @return true iff @p l equals @p r.
12886static bool
12887compare_dies_during_canonicalization(reader& rdr,
12888 const Dwarf_Die *l,
12889 const Dwarf_Die *r,
12890 bool update_canonical_dies_on_the_fly)
12891{
12892#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12893 if (rdr.debug_die_canonicalization_is_on_)
12894 {
12895 bool canonical_equality = false, structural_equality = false;
12896 rdr.use_canonical_die_comparison_ = false;
12897 structural_equality = compare_dies(rdr, l, r,
12898 /*update_canonical_dies_on_the_fly=*/false);
12899 rdr.use_canonical_die_comparison_ = true;
12900 canonical_equality = compare_dies(rdr, l, r,
12901 update_canonical_dies_on_the_fly);
12902 if (canonical_equality != structural_equality)
12903 {
12904 std::cerr << "structural & canonical equality different for DIEs: "
12905 << std::hex
12906 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12907 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12908 << std::dec
12909 << ", repr: '"
12910 << rdr.get_die_pretty_type_representation(l, 0)
12911 << "'"
12912 << std::endl;
12914 }
12915 return structural_equality;
12916 }
12917#endif
12918 return compare_dies(rdr, l, r,
12919 update_canonical_dies_on_the_fly);
12920}
12921
12922// ----------------------------------
12923// </die comparison engine>
12924// ---------------------------------
12925
12926/// Get the point where a DW_AT_import DIE is used to import a given
12927/// (unit) DIE, between two DIEs.
12928///
12929/// @param rdr the dwarf reader to consider.
12930///
12931/// @param partial_unit_offset the imported unit for which we want to
12932/// know the insertion point. This is usually a partial unit (with
12933/// tag DW_TAG_partial_unit) but it does not necessarily have to be
12934/// so.
12935///
12936/// @param first_die_offset the offset of the DIE from which this
12937/// function starts looking for the import point of
12938/// @partial_unit_offset. Note that this offset is excluded from the
12939/// set of potential solutions.
12940///
12941/// @param first_die_cu_offset the offset of the (compilation) unit
12942/// that @p first_die_cu_offset belongs to.
12943///
12944/// @param source where the DIE of first_die_cu_offset unit comes
12945/// from.
12946///
12947/// @param last_die_offset the offset of the last DIE of the up to
12948/// which this function looks for the import point of @p
12949/// partial_unit_offset. Note that this offset is excluded from the
12950/// set of potential solutions.
12951///
12952/// @param imported_point_offset. The resulting
12953/// imported_point_offset. Note that if the imported DIE @p
12954/// partial_unit_offset is not found between @p first_die_offset and
12955/// @p last_die_offset, this parameter is left untouched by this
12956/// function.
12957///
12958/// @return true iff an imported unit is found between @p
12959/// first_die_offset and @p last_die_offset.
12960static bool
12961find_import_unit_point_between_dies(const reader& rdr,
12962 size_t partial_unit_offset,
12963 Dwarf_Off first_die_offset,
12964 Dwarf_Off first_die_cu_offset,
12965 die_source source,
12966 size_t last_die_offset,
12967 size_t& imported_point_offset)
12968{
12969 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
12970 rdr.tu_die_imported_unit_points_map(source);
12971
12972 tu_die_imported_unit_points_map_type::const_iterator iter =
12973 tu_die_imported_unit_points_map.find(first_die_cu_offset);
12974
12975 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
12976
12977 const imported_unit_points_type& imported_unit_points = iter->second;
12978 if (imported_unit_points.empty())
12979 return false;
12980
12981 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
12982 imported_unit_points_type::const_iterator e = imported_unit_points.end();
12983
12984 find_lower_bound_in_imported_unit_points(imported_unit_points,
12985 first_die_offset,
12986 b);
12987
12988 if (last_die_offset != static_cast<size_t>(-1))
12989 find_lower_bound_in_imported_unit_points(imported_unit_points,
12990 last_die_offset,
12991 e);
12992
12993 if (e != imported_unit_points.end())
12994 {
12995 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12996 if (i->imported_unit_die_off == partial_unit_offset)
12997 {
12998 imported_point_offset = i->offset_of_import ;
12999 return true;
13000 }
13001
13002 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
13003 {
13004 if (find_import_unit_point_between_dies(rdr,
13005 partial_unit_offset,
13006 i->imported_unit_child_off,
13007 i->imported_unit_cu_off,
13008 i->imported_unit_die_source,
13009 /*(Dwarf_Off)*/-1,
13010 imported_point_offset))
13011 return true;
13012 }
13013 }
13014 else
13015 {
13016 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
13017 if (i->imported_unit_die_off == partial_unit_offset)
13018 {
13019 imported_point_offset = i->offset_of_import ;
13020 return true;
13021 }
13022
13023 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
13024 {
13025 if (find_import_unit_point_between_dies(rdr,
13026 partial_unit_offset,
13027 i->imported_unit_child_off,
13028 i->imported_unit_cu_off,
13029 i->imported_unit_die_source,
13030 /*(Dwarf_Off)*/-1,
13031 imported_point_offset))
13032 return true;
13033 }
13034 }
13035
13036 return false;
13037}
13038
13039/// In the current translation unit, get the last point where a
13040/// DW_AT_import DIE is used to import a given (unit) DIE, before a
13041/// given DIE is found. That given DIE is called the limit DIE.
13042///
13043/// Said otherwise, this function returns the last import point of a
13044/// unit, before a limit.
13045///
13046/// @param rdr the dwarf reader to consider.
13047///
13048/// @param partial_unit_offset the imported unit for which we want to
13049/// know the insertion point of. This is usually a partial unit (with
13050/// tag DW_TAG_partial_unit) but it does not necessarily have to be
13051/// so.
13052///
13053/// @param where_offset the offset of the limit DIE.
13054///
13055/// @param imported_point_offset. The resulting imported_point_offset.
13056/// Note that if the imported DIE @p partial_unit_offset is not found
13057/// before @p die_offset, this is set to the last @p
13058/// partial_unit_offset found under @p parent_die.
13059///
13060/// @return true iff an imported unit is found before @p die_offset.
13061/// Note that if an imported unit is found after @p die_offset then @p
13062/// imported_point_offset is set and the function return false.
13063static bool
13064find_import_unit_point_before_die(const reader& rdr,
13065 size_t partial_unit_offset,
13066 size_t where_offset,
13067 size_t& imported_point_offset)
13068{
13069 size_t import_point_offset = 0;
13070 Dwarf_Die first_die_of_tu;
13071
13072 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
13073 &first_die_of_tu) != 0)
13074 return false;
13075
13076 Dwarf_Die cu_die_memory;
13077 Dwarf_Die *cu_die;
13078
13079 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
13080 &cu_die_memory, 0, 0);
13081
13082 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
13083 dwarf_dieoffset(&first_die_of_tu),
13084 dwarf_dieoffset(cu_die),
13085 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
13086 where_offset,
13087 import_point_offset))
13088 {
13089 imported_point_offset = import_point_offset;
13090 return true;
13091 }
13092
13093 if (import_point_offset)
13094 {
13095 imported_point_offset = import_point_offset;
13096 return true;
13097 }
13098
13099 return false;
13100}
13101
13102/// Return the parent DIE for a given DIE.
13103///
13104/// Note that the function build_die_parent_map() must have been
13105/// called before this one can work. This function either succeeds or
13106/// aborts the current process.
13107///
13108/// @param rdr the DWARF reader to consider.
13109///
13110/// @param die the DIE for which we want the parent.
13111///
13112/// @param parent_die the output parameter set to the parent die of
13113/// @p die. Its memory must be allocated and handled by the caller.
13114///
13115/// @param where_offset the offset of the DIE where we are "logically"
13116/// positionned at, in the DIE tree. This is useful when @p die is
13117/// e.g, DW_TAG_partial_unit that can be included in several places in
13118/// the DIE tree.
13119///
13120/// @return true if the function could get a parent DIE, false
13121/// otherwise.
13122static bool
13123get_parent_die(const reader& rdr,
13124 const Dwarf_Die* die,
13125 Dwarf_Die& parent_die,
13126 size_t where_offset)
13127{
13128 ABG_ASSERT(rdr.dwarf_debug_info());
13129
13130 const die_source source = rdr.get_die_source(die);
13131
13132 const offset_offset_map_type& m = rdr.die_parent_map(source);
13133 offset_offset_map_type::const_iterator i =
13134 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
13135
13136 if (i == m.end())
13137 return false;
13138
13139 switch (source)
13140 {
13141 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
13142 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13143 i->second, &parent_die));
13144 break;
13145 case ALT_DEBUG_INFO_DIE_SOURCE:
13146 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
13147 i->second, &parent_die));
13148 break;
13149 case TYPE_UNIT_DIE_SOURCE:
13150 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13151 i->second, &parent_die));
13152 break;
13153 case NO_DEBUG_INFO_DIE_SOURCE:
13154 case NUMBER_OF_DIE_SOURCES:
13156 }
13157
13158 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
13159 {
13160 if (where_offset == 0)
13161 {
13162 parent_die = *rdr.cur_tu_die();
13163 return true;
13164 }
13165 size_t import_point_offset = 0;
13166 bool found =
13167 find_import_unit_point_before_die(rdr,
13168 dwarf_dieoffset(&parent_die),
13169 where_offset,
13170 import_point_offset);
13171 if (!found)
13172 // It looks like parent_die (which comes from the alternate
13173 // debug info file) hasn't been imported into this TU. So,
13174 // Let's assume its logical parent is the DIE of the current
13175 // TU.
13176 parent_die = *rdr.cur_tu_die();
13177 else
13178 {
13179 ABG_ASSERT(import_point_offset);
13180 Dwarf_Die import_point_die;
13181 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13182 import_point_offset,
13183 &import_point_die));
13184 return get_parent_die(rdr, &import_point_die,
13185 parent_die, where_offset);
13186 }
13187 }
13188
13189 return true;
13190}
13191
13192/// Get the DIE representing the scope of a given DIE.
13193///
13194/// Please note that when the DIE we are looking at has a
13195/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
13196/// DIE is the parent DIE of the DIE referred to by that attribute.
13197/// In other words, this function returns the scope of the origin DIE
13198/// of the current DIE.
13199///
13200/// So, the scope DIE can be different from the parent DIE of a given
13201/// DIE.
13202///
13203/// Also note that if the current translation unit is from C, then
13204/// this returns the global scope.
13205///
13206/// @param rdr the DWARF reader to use.
13207///
13208/// @param dye the DIE to consider.
13209///
13210/// @param where_offset where we are logically at in the DIE stream.
13211///
13212/// @param scope_die out parameter. This is set to the resulting
13213/// scope DIE iff the function returns true.
13214///
13215/// @return true iff the scope was found and returned in the @p
13216/// scope_die parameter.
13217static bool
13218get_scope_die(const reader& rdr,
13219 const Dwarf_Die* dye,
13220 size_t where_offset,
13221 Dwarf_Die& scope_die)
13222{
13223 Dwarf_Die origin_die_mem;
13224 Dwarf_Die *die = &origin_die_mem;
13225 if (!die_origin_die(dye, origin_die_mem))
13226 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13227
13228 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13229 get_die_language(die, die_lang);
13230 if (is_c_language(die_lang)
13231 || rdr.die_parent_map(rdr.get_die_source(die)).empty())
13232 {
13233 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
13234 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
13235 }
13236
13237 if (!get_parent_die(rdr, die, scope_die, where_offset))
13238 return false;
13239
13240 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
13241 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
13242 || dwarf_tag(&scope_die) == DW_TAG_array_type)
13243 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
13244
13245 return true;
13246}
13247
13248/// Return the abigail IR node representing the scope of a given DIE.
13249///
13250/// Note that it is the logical scope that is returned. That is, if
13251/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
13252/// attribute, it's the scope of the referred-to DIE (via these
13253/// attributes) that is returned. In other words, its the scope of
13254/// the origin DIE that is returned.
13255///
13256/// Also note that if the current translation unit is from C, then
13257/// this returns the global scope.
13258///
13259/// @param rdr the dwarf reader to use.
13260///
13261/// @param dye the DIE to get the scope for.
13262///
13263/// @param called_from_public_decl is true if this function has been
13264/// initially called within the context of a public decl.
13265///
13266/// @param where_offset the offset of the DIE where we are "logically"
13267/// positionned at, in the DIE tree. This is useful when @p die is
13268/// e.g, DW_TAG_partial_unit that can be included in several places in
13269/// the DIE tree.
13270///
13271/// @return the resulting scope, or nil if could not be computed.
13272static scope_decl_sptr
13273get_scope_for_die(reader& rdr,
13274 Dwarf_Die* dye,
13275 bool called_for_public_decl,
13276 size_t where_offset)
13277{
13278 Dwarf_Die origin_die_mem;
13279 Dwarf_Die *die = &origin_die_mem;
13280
13281 if (!die_origin_die(dye, origin_die_mem))
13282 // There was no origin DIE found, so let's make the "die" pointer
13283 // above point to the content of the input "dye".
13284 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13285
13286 const die_source source_of_die = rdr.get_die_source(die);
13287
13288 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13289 get_die_language(die, die_lang);
13290 if (is_c_language(die_lang)
13291 || rdr.die_parent_map(source_of_die).empty())
13292 {
13293 // In units for the C languages all decls belong to the global
13294 // namespace. This is generally the case if Libabigail
13295 // determined that no DIE -> parent map was needed.
13296 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
13297 return rdr.global_scope();
13298 }
13299
13300 Dwarf_Die parent_die;
13301
13302 if (!get_parent_die(rdr, die, parent_die, where_offset))
13303 return rdr.nil_scope();
13304
13305 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
13306 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
13307 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13308 {
13309 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
13310 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13311 {
13312 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
13313 || source_of_die == TYPE_UNIT_DIE_SOURCE);
13314 return rdr.cur_transl_unit()->get_global_scope();
13315 }
13316
13317 // For top level DIEs like DW_TAG_compile_unit, we just want to
13318 // return the global scope for the corresponding translation
13319 // unit. This must have been set by
13320 // build_translation_unit_and_add_to_ir if we already started to
13321 // build the translation unit of parent_die. Otherwise, just
13322 // return the global scope of the current translation unit.
13323 die_tu_map_type::const_iterator i =
13324 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
13325 if (i != rdr.die_tu_map().end())
13326 return i->second->get_global_scope();
13327 return rdr.cur_transl_unit()->get_global_scope();
13328 }
13329
13332 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
13333 || dwarf_tag(&parent_die) == DW_TAG_array_type
13334 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
13335 // this is an entity defined in a scope that is a function.
13336 // Normally, I would say that this should be dropped. But I have
13337 // seen a case where a typedef DIE needed by a function parameter
13338 // was defined right before the parameter, under the scope of the
13339 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
13340 // the function parm too. So for that case, let's say that the
13341 // scope is the scope of the function itself. Note that this is
13342 // an error of the DWARF emitter. We should never see this DIE in
13343 // this context.
13344 {
13345 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
13346 called_for_public_decl,
13347 where_offset);
13348 if (is_anonymous_type_die(die))
13349 // For anonymous type that have nothing to do in a function or
13350 // array type context, let's put it in the containing
13351 // namespace. That is, do not let it be in a containing class
13352 // or union where it has nothing to do.
13353 while (is_class_or_union_type(s))
13354 {
13355 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
13356 return rdr.nil_scope();
13357 s = get_scope_for_die(rdr, &parent_die,
13358 called_for_public_decl,
13359 where_offset);
13360 }
13361 return s;
13362 }
13363 else
13364 d = build_ir_node_from_die(rdr, &parent_die,
13365 called_for_public_decl,
13366 where_offset);
13367 s = dynamic_pointer_cast<scope_decl>(d);
13368 if (!s)
13369 // this is an entity defined in someting that is not a scope.
13370 // Let's drop it.
13371 return rdr.nil_scope();
13372
13373 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
13374 if (cl && cl->get_is_declaration_only())
13375 {
13376 scope_decl_sptr scop =
13377 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
13378 if (scop)
13379 s = scop;
13380 else
13381 s = cl;
13382 }
13383 return s;
13384}
13385
13386/// Convert a DWARF constant representing the value of the
13387/// DW_AT_language property into the translation_unit::language
13388/// enumerator.
13389///
13390/// @param l the DWARF constant to convert.
13391///
13392/// @return the resulting translation_unit::language enumerator.
13394dwarf_language_to_tu_language(size_t l)
13395{
13396 switch (l)
13397 {
13398 case DW_LANG_C89:
13399 return translation_unit::LANG_C89;
13400 case DW_LANG_C99:
13401 return translation_unit::LANG_C99;
13402#ifdef HAVE_DW_LANG_C11_enumerator
13403 case DW_LANG_C11:
13404 return translation_unit::LANG_C11;
13405#endif
13406#ifdef HAVE_DW_LANG_C17
13407 case DW_LANG_C17:
13408 return translation_unit::LANG_C17;
13409#endif
13410#ifdef HAVE_DW_LANG_C23
13411 case DW_LANG_C23:
13412 return translation_unit::LANG_C23;
13413#endif
13414 case DW_LANG_C:
13415 return translation_unit::LANG_C;
13416#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
13417 case DW_LANG_C_plus_plus_03:
13418 return translation_unit::LANG_C_plus_plus_03;
13419#endif
13420
13421#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
13422 case DW_LANG_C_plus_plus_11:
13423 return translation_unit::LANG_C_plus_plus_11;
13424#endif
13425
13426#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
13427 case DW_LANG_C_plus_plus_14:
13428 return translation_unit::LANG_C_plus_plus_14;
13429#endif
13430#ifdef HAVE_DW_LANG_C_plus_plus_17
13431 case DW_LANG_C_plus_plus_17:
13432 return translation_unit::LANG_C_plus_plus_17;
13433#endif
13434
13435#ifdef HAVE_DW_LANG_C_plus_plus_20
13436 case DW_LANG_C_plus_plus_20:
13437 return translation_unit::LANG_C_plus_plus_20;
13438#endif
13439#ifdef HAVE_DW_LANG_C_plus_plus_23
13440 case DW_LANG_C_plus_plus_23:
13441 return translation_unit::LANG_C_plus_plus_23;
13442#endif
13443 case DW_LANG_C_plus_plus:
13444 return translation_unit::LANG_C_plus_plus;
13445#ifdef HAVE_DW_LANG_D_enumerator
13446 case DW_LANG_D:
13447 return translation_unit::LANG_D;
13448#endif
13449#ifdef HAVE_DW_LANG_OCaml_enumerator
13450 case DW_LANG_OCaml:
13451 return translation_unit::LANG_OCaml;
13452#endif
13453#ifdef HAVE_DW_LANG_Go_enumerator
13454 case DW_LANG_Go:
13455 return translation_unit::LANG_Go;
13456#endif
13457#ifdef HAVE_DW_LANG_Rust_enumerator
13458 case DW_LANG_Rust:
13459 return translation_unit::LANG_Rust;
13460#endif
13461#ifdef HAVE_DW_LANG_Zig
13462 case DW_LANG_Zig:
13463 return translation_unit::LANG_Zig;
13464#endif
13465#ifdef HAVE_DW_LANG_Metal
13466 case DW_LANG_Metal:
13467 return translation_unit::LANG_Metal;
13468#endif
13469 case DW_LANG_Ada83:
13470 return translation_unit::LANG_Ada83;
13471 case DW_LANG_Ada95:
13472 return translation_unit::LANG_Ada95;
13473#ifdef HAVE_DW_LANG_Ada2005
13474 case DW_LANG_Ada2005:
13475 return translation_unit::LANG_Ada2005;
13476#endif
13477
13478#ifdef HAVE_DW_LANG_Ada2012
13479 case DW_LANG_Ada2012:
13480 return translation_unit::LANG_Ada2012;
13481#endif
13482 case DW_LANG_Cobol74:
13483 return translation_unit::LANG_Cobol74;
13484 case DW_LANG_Cobol85:
13485 return translation_unit::LANG_Cobol85;
13486 case DW_LANG_Fortran77:
13487 return translation_unit::LANG_Fortran77;
13488 case DW_LANG_Fortran90:
13489 return translation_unit::LANG_Fortran90;
13490 case DW_LANG_Fortran95:
13491 return translation_unit::LANG_Fortran95;
13492#ifdef HAVE_DW_LANG_Fortran18
13493 case DW_LANG_Fortran18:
13494 return translation_unit::LANG_Fortran18;
13495#endif
13496#ifdef HAVE_DW_LANG_Fortran23
13497 case DW_LANG_Fortran23:
13498 return translation_unit::LANG_Fortran23;
13499#endif
13500 case DW_LANG_Pascal83:
13501 return translation_unit::LANG_Pascal83;
13502 case DW_LANG_Modula2:
13503 return translation_unit::LANG_Modula2;
13504 case DW_LANG_Java:
13505 return translation_unit::LANG_Java;
13506#ifdef HAVE_DW_LANG_Kotlin
13507 case DW_LANG_Kotlin:
13508 return translation_unit::LANG_Kotlin;
13509#endif
13510 case DW_LANG_PLI:
13511 return translation_unit::LANG_PLI;
13512 case DW_LANG_ObjC:
13513 return translation_unit::LANG_ObjC;
13514 case DW_LANG_ObjC_plus_plus:
13515 return translation_unit::LANG_ObjC_plus_plus;
13516
13517#ifdef HAVE_DW_LANG_UPC_enumerator
13518 case DW_LANG_UPC:
13519 return translation_unit::LANG_UPC;
13520#endif
13521#ifdef HAVE_DW_LANG_Python_enumerator
13522 case DW_LANG_Python:
13523 return translation_unit::LANG_Python;
13524#endif
13525#ifdef HAVE_DW_LANG_Ruby
13526 case DW_LANG_Ruby:
13527 return translation_unit::LANG_Ruby;
13528#endif
13529#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
13530 case DW_LANG_Mips_Assembler:
13531 return translation_unit::LANG_Mips_Assembler;
13532#endif
13533#ifdef HAVE_DW_LANG_Assembly
13534 case DW_LANG_Assembly:
13535 return translation_unit::LANG_Assembly;
13536#endif
13537#ifdef HAVE_DW_LANG_Crystal
13538 case DW_LANG_Crystal:
13539 return translation_unit::LANG_Crystal;
13540#endif
13541#ifdef HAVE_DW_LANG_HIP
13542 case DW_LANG_HIP:
13543 return translation_unit::LANG_HIP;
13544#endif
13545#ifdef HAVE_DW_LANG_C_sharp
13546 case DW_LANG_C_sharp:
13547 return translation_unit::LANG_C_sharp;
13548#endif
13549#ifdef HAVE_DW_LANG_Mojo
13550 case DW_LANG_Mojo:
13551 return translation_unit::LANG_Mojo;
13552#endif
13553#ifdef HAVE_DW_LANG_GLSL
13554 case DW_LANG_GLSL:
13555 return translation_unit::LANG_GLSL;
13556#endif
13557#ifdef HAVE_DW_LANG_GLSL_ES
13558 case DW_LANG_GLSL_ES:
13559 return translation_unit::LANG_GLSL_ES;
13560#endif
13561#ifdef HAVE_DW_LANG_HLSL
13562 case DW_LANG_HLSL:
13563 return translation_unit::LANG_HLSL;
13564#endif
13565#ifdef HAVE_DW_LANG_OpenCL_CPP
13566 case DW_LANG_OpenCL_CPP:
13567 return translation_unit::LANG_OpenCL_CPP;
13568#endif
13569#ifdef HAVE_DW_LANG_CPP_for_OpenCL
13570 case DW_LANG_CPP_for_OpenCL:
13571 return translation_unit::LANG_CPP_for_OpenCL;
13572#endif
13573#ifdef HAVE_DW_LANG_SYCL
13574 case DW_LANG_SYCL:
13575 return translation_unit::LANG_SYCL;
13576#endif
13577#ifdef HAVE_DW_LANG_Odin
13578 case DW_LANG_Odin:
13579 return translation_unit::LANG_Odin;
13580#endif
13581#ifdef HAVE_DW_LANG_P4
13582 case DW_LANG_P4:
13583 return translation_unit::LANG_P4;
13584#endif
13585#ifdef HAVE_DW_LANG_Move
13586 case DW_LANG_Move:
13587 return translation_unit::LANG_Move;
13588#endif
13589#ifdef HAVE_DW_LANG_Hylo
13590 case DW_LANG_Hylo:
13591 return translation_unit::LANG_Hylo;
13592#endif
13593
13594 default:
13595 return translation_unit::LANG_UNKNOWN;
13596 }
13597}
13598
13599/// Get the default array lower bound value as defined by the DWARF
13600/// specification, version 4, depending on the language of the
13601/// translation unit.
13602///
13603/// @param l the language of the translation unit.
13604///
13605/// @return the default array lower bound value.
13606static uint64_t
13607get_default_array_lower_bound(translation_unit::language l)
13608{
13609 int value = 0;
13610 switch (l)
13611 {
13612 case translation_unit::LANG_UNKNOWN:
13613 case translation_unit::LANG_C89:
13614 case translation_unit::LANG_C99:
13615 case translation_unit::LANG_C11:
13616 case translation_unit::LANG_C17:
13617 case translation_unit::LANG_C23:
13618 case translation_unit::LANG_C:
13619 case translation_unit::LANG_C_plus_plus_03:
13620 case translation_unit::LANG_C_plus_plus_11:
13621 case translation_unit::LANG_C_plus_plus_14:
13622 case translation_unit::LANG_C_plus_plus_17:
13623 case translation_unit::LANG_C_plus_plus_20:
13624 case translation_unit::LANG_C_plus_plus_23:
13625 case translation_unit::LANG_C_plus_plus:
13626 case translation_unit::LANG_OCaml:
13627 case translation_unit::LANG_ObjC:
13628 case translation_unit::LANG_ObjC_plus_plus:
13629 case translation_unit::LANG_D:
13630 case translation_unit::LANG_Rust:
13631 case translation_unit::LANG_Go:
13632 case translation_unit::LANG_Zig:
13633 case translation_unit::LANG_Metal:
13634 case translation_unit::LANG_Java:
13635 case translation_unit::LANG_Kotlin:
13636 case translation_unit::LANG_Python:
13637 case translation_unit::LANG_Ruby:
13638 case translation_unit::LANG_UPC:
13639 case translation_unit::LANG_Mips_Assembler:
13640 case translation_unit::LANG_Assembly:
13641 case translation_unit::LANG_Crystal:
13642 case translation_unit::LANG_HIP:
13643 case translation_unit::LANG_C_sharp:
13644 case translation_unit::LANG_Mojo:
13645 case translation_unit::LANG_GLSL:
13646 case translation_unit::LANG_GLSL_ES:
13647 case translation_unit::LANG_HLSL:
13648 case translation_unit::LANG_Odin:
13649 case translation_unit::LANG_P4:
13650 case translation_unit::LANG_OpenCL_CPP:
13651 case translation_unit::LANG_CPP_for_OpenCL:
13652 case translation_unit::LANG_SYCL:
13653 case translation_unit::LANG_Move:
13654 case translation_unit::LANG_Hylo:
13655 value = 0;
13656 break;
13657 case translation_unit::LANG_Cobol74:
13658 case translation_unit::LANG_Cobol85:
13659 case translation_unit::LANG_Fortran77:
13660 case translation_unit::LANG_Fortran90:
13661 case translation_unit::LANG_Fortran95:
13662 case translation_unit::LANG_Fortran18:
13663 case translation_unit::LANG_Fortran23:
13664 case translation_unit::LANG_Ada83:
13665 case translation_unit::LANG_Ada95:
13666 case translation_unit::LANG_Ada2005:
13667 case translation_unit::LANG_Ada2012:
13668 case translation_unit::LANG_Pascal83:
13669 case translation_unit::LANG_Modula2:
13670 case translation_unit::LANG_PLI:
13671 value = 1;
13672 break;
13673 }
13674
13675 return value;
13676}
13677
13678/// For a given offset, find the lower bound of a sorted vector of
13679/// imported unit point offset.
13680///
13681/// The lower bound is the smallest point (the point with the smallest
13682/// offset) which is the greater than a given offset.
13683///
13684/// @param imported_unit_points_type the sorted vector of imported
13685/// unit points.
13686///
13687/// @param val the offset to consider when looking for the lower
13688/// bound.
13689///
13690/// @param r an iterator to the lower bound found. This parameter is
13691/// set iff the function returns true.
13692///
13693/// @return true iff the lower bound has been found.
13694static bool
13695find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
13696 Dwarf_Off val,
13697 imported_unit_points_type::const_iterator& r)
13698{
13699 imported_unit_point v(val);
13700 imported_unit_points_type::const_iterator result =
13701 std::lower_bound(p.begin(), p.end(), v);
13702
13703 bool is_ok = result != p.end();
13704
13705 if (is_ok)
13706 r = result;
13707
13708 return is_ok;
13709}
13710
13711/// Given a DW_TAG_compile_unit, build and return the corresponding
13712/// abigail::translation_unit ir node. Note that this function
13713/// recursively reads the children dies of the current DIE and
13714/// populates the resulting translation unit.
13715///
13716/// @param rdr the DWARF reader to use.
13717///
13718/// @param die the DW_TAG_compile_unit DIE to consider.
13719///
13720/// @param address_size the size of the addresses expressed in this
13721/// translation unit in general.
13722///
13723/// @return a pointer to the resulting translation_unit.
13725build_translation_unit_and_add_to_ir(reader& rdr,
13726 Dwarf_Die* die,
13727 char address_size)
13728{
13729 translation_unit_sptr result;
13730
13731 if (!die)
13732 return result;
13733 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
13734
13735 // Clear the part of the context that is dependent on the translation
13736 // unit we are reading.
13737 rdr.clear_per_translation_unit_data();
13738
13739 rdr.cur_tu_die(die);
13740
13741 string path = die_string_attribute(die, DW_AT_name);
13742 if (path == "<artificial>")
13743 {
13744 // This is a file artificially generated by the compiler, so its
13745 // name is '<artificial>'. As we want all different translation
13746 // units to have unique path names, let's suffix this path name
13747 // with its die offset.
13748 std::ostringstream o;
13749 o << path << "-" << std::hex << dwarf_dieoffset(die);
13750 path = o.str();
13751 }
13752 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
13753
13754 // See if the same translation unit exits already in the current
13755 // corpus. Sometimes, the same translation unit can be present
13756 // several times in the same debug info. The content of the
13757 // different instances of the translation unit are different. So to
13758 // represent that, we are going to re-use the same translation
13759 // unit. That is, it's going to be the union of all the translation
13760 // units of the same path.
13761 {
13762 const string& abs_path =
13763 compilation_dir.empty() ? path : compilation_dir + "/" + path;
13764 result = rdr.corpus()->find_translation_unit(abs_path);
13765 }
13766
13767 if (!result)
13768 {
13769 result.reset(new translation_unit(rdr.env(),
13770 path,
13771 address_size));
13772 result->set_compilation_dir_path(compilation_dir);
13773 rdr.corpus()->add(result);
13774 uint64_t l = 0;
13775 die_unsigned_constant_attribute(die, DW_AT_language, l);
13776 result->set_language(dwarf_language_to_tu_language(l));
13777 }
13778
13779 rdr.cur_transl_unit(result);
13780 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
13781
13782 Dwarf_Die child;
13783 if (dwarf_child(die, &child) != 0)
13784 return result;
13785
13786 result->set_is_constructed(false);
13787 int tag = dwarf_tag(&child);
13788 do
13789 if (rdr.load_undefined_interfaces()
13790 && (rdr.is_decl_die_with_undefined_symbol(&child)
13791 || tag == DW_TAG_class_type // Top-level classes might
13792 // have undefined interfaces
13793 // that need to be
13794 // represented, so let's
13795 // analyze them as well.
13796 || ((tag == DW_TAG_union_type || tag == DW_TAG_structure_type)
13797 && die_is_in_cplus_plus(&child))))
13798 {
13799 // Analyze undefined functions & variables for the purpose of
13800 // analyzing compatibility matters.
13801 build_ir_node_from_die(rdr, &child,
13802 // Pretend the DIE is publicly defined
13803 // so that types that are reachable
13804 // from it get analyzed as well.
13805 /*die_is_public=*/true,
13806 dwarf_dieoffset(&child));
13807 }
13808 else if (!rdr.env().analyze_exported_interfaces_only()
13809 || rdr.is_decl_die_with_exported_symbol(&child))
13810 {
13811 // Analyze all the DIEs we encounter unless we are asked to only
13812 // analyze exported interfaces and the types reachables from them.
13813 build_ir_node_from_die(rdr, &child,
13814 die_is_public_decl(&child),
13815 dwarf_dieoffset(&child));
13816 }
13817 while (dwarf_siblingof(&child, &child) == 0);
13818
13819 if (!rdr.var_decls_to_re_add_to_tree().empty())
13820 for (list<var_decl_sptr>::const_iterator v =
13821 rdr.var_decls_to_re_add_to_tree().begin();
13822 v != rdr.var_decls_to_re_add_to_tree().end();
13823 ++v)
13824 {
13825 if (is_member_decl(*v))
13826 continue;
13827
13828 ABG_ASSERT((*v)->get_scope());
13829 string demangled_name =
13830 demangle_cplus_mangled_name((*v)->get_linkage_name());
13831 if (!demangled_name.empty())
13832 {
13833 std::list<string> fqn_comps;
13834 fqn_to_components(demangled_name, fqn_comps);
13835 string mem_name = fqn_comps.back();
13836 fqn_comps.pop_back();
13837 class_decl_sptr class_type;
13838 string ty_name;
13839 if (!fqn_comps.empty())
13840 {
13841 ty_name = components_to_type_name(fqn_comps);
13842 class_type =
13843 lookup_class_type(ty_name, *rdr.cur_transl_unit());
13844 }
13845 if (class_type)
13846 {
13847 // So we are seeing a member variable for which there
13848 // is a global variable definition DIE not having a
13849 // reference attribute pointing back to the member
13850 // variable declaration DIE. Thus remove the global
13851 // variable definition from its current non-class
13852 // scope ...
13853 decl_base_sptr d;
13854 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
13855 // This is the data member with the same name in cl.
13856 // We just need to flag it as static.
13857 ;
13858 else
13859 {
13860 // In this case there is no data member with the
13861 // same name in cl already. Let's add it there then
13862 // ...
13864 d = add_decl_to_scope(*v, class_type);
13865 }
13866
13867 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
13868 // Let's flag the data member as static.
13869 set_member_is_static(d, true);
13870 }
13871 }
13872 }
13873 rdr.var_decls_to_re_add_to_tree().clear();
13874
13875 result->set_is_constructed(true);
13876
13877 return result;
13878}
13879
13880/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
13881/// DW_TAG_module (for fortran) DIE.
13882///
13883/// Note that this function connects the DW_TAG_namespace to the IR
13884/// being currently created, reads the children of the DIE and
13885/// connects them to the IR as well.
13886///
13887/// @param rdr the DWARF reader to use.
13888///
13889/// @param die the DIE to read from. Must be either DW_TAG_namespace
13890/// or DW_TAG_module.
13891///
13892/// @param where_offset the offset of the DIE where we are "logically"
13893/// positionned at, in the DIE tree. This is useful when @p die is
13894/// e.g, DW_TAG_partial_unit that can be included in several places in
13895/// the DIE tree.
13896///
13897/// @return the resulting @ref abigail::namespace_decl or NULL if it
13898/// couldn't be created.
13900build_namespace_decl_and_add_to_ir(reader& rdr,
13901 Dwarf_Die* die,
13902 size_t where_offset)
13903{
13904 namespace_decl_sptr result;
13905
13906 if (!die)
13907 return result;
13908
13909 unsigned tag = dwarf_tag(die);
13910 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
13911 return result;
13912
13913 scope_decl_sptr scope = get_scope_for_die(rdr, die,
13914 /*called_for_public_decl=*/false,
13915 where_offset);
13916
13917 string name, linkage_name;
13918 location loc;
13919 die_loc_and_name(rdr, die, loc, name, linkage_name);
13920
13921 result.reset(new namespace_decl(rdr.env(), name, loc));
13922 add_decl_to_scope(result, scope.get());
13923 rdr.associate_die_to_decl(die, result, where_offset);
13924
13925 Dwarf_Die child;
13926 if (dwarf_child(die, &child) != 0)
13927 return result;
13928
13929 rdr.scope_stack().push(result.get());
13930 do
13931 build_ir_node_from_die(rdr, &child,
13932 // If this namespace DIE is private
13933 // (anonymous) then all its content is
13934 // considered private. Otherwise, its
13935 // public decls are considered public.
13936 /*called_from_public_decl=*/
13937 die_is_public_decl(die) && die_is_public_decl(&child),
13938 where_offset);
13939 while (dwarf_siblingof(&child, &child) == 0);
13940 rdr.scope_stack().pop();
13941
13942 return result;
13943}
13944
13945/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
13946///
13947/// @param rdr the DWARF reader to use.
13948///
13949/// @param die the DW_TAG_base_type to consider.
13950///
13951/// @param where_offset where we are logically at in the DIE stream.
13952///
13953/// @return the resulting decl_base_sptr.
13954static type_decl_sptr
13955build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
13956{
13957 type_decl_sptr result;
13958
13959 if (!die)
13960 return result;
13961 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
13962
13963 uint64_t byte_size = 0, bit_size = 0;
13964 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
13965 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
13966 return result;
13967
13968 if (bit_size == 0 && byte_size != 0)
13969 // Update the bit size.
13970 bit_size = byte_size * 8;
13971
13972 string type_name, linkage_name;
13973 location loc;
13974 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
13975
13976 if (byte_size == 0)
13977 {
13978 // The size of the type is zero, that must mean that we are
13979 // looking at the definition of the void type.
13980 if (type_name == "void")
13981 result = is_type_decl(build_ir_node_for_void_type(rdr));
13982 else
13983 // A type of size zero that is not void? Hmmh, I am not sure
13984 // what that means. Return nil for now.
13985 return result;
13986 }
13987
13988 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13989 {
13990 string normalized_type_name = type_name;
13991 real_type real_type;
13992 if (parse_real_type(type_name, real_type))
13993 normalized_type_name = real_type.to_string();
13994 result = lookup_basic_type(normalized_type_name, *corp);
13995 }
13996
13997 if (!result)
13998 if (corpus_sptr corp = rdr.corpus())
13999 result = lookup_basic_type(type_name, *corp);
14000 if (!result)
14001 result.reset(new type_decl(rdr.env(), type_name, bit_size,
14002 /*alignment=*/0, loc, linkage_name));
14003 rdr.associate_die_to_type(die, result, where_offset);
14004 return result;
14005}
14006
14007/// Construct the type that is to be used as the underlying type of an
14008/// enum.
14009///
14010/// @param rdr the DWARF reader to use.
14011///
14012/// @param enum_name the name of the enum that this type is going to
14013/// be the underlying type of.
14014///
14015/// @param enum_size the size of the enum.
14016///
14017/// @param is_anonymous whether the underlying type is anonymous or
14018/// not. By default, this should be set to true as before c++11 (and
14019/// in C), it's almost the case.
14020static type_decl_sptr
14021build_enum_underlying_type(reader& rdr,
14022 string enum_name,
14023 uint64_t enum_size,
14024 bool is_anonymous = true)
14025{
14026 string underlying_type_name =
14027 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
14028 enum_size);
14029
14030 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
14031 enum_size, enum_size, location()));
14032 result->set_is_anonymous(is_anonymous);
14033 result->set_is_artificial(true);
14034 translation_unit_sptr tu = rdr.cur_transl_unit();
14035 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
14036 result = dynamic_pointer_cast<type_decl>(d);
14037 ABG_ASSERT(result);
14038 maybe_canonicalize_type(result, rdr);
14039 return result;
14040}
14041
14042/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
14043///
14044/// @param rdr the DWARF reader to use.
14045///
14046/// @param die the DIE to read from.
14047///
14048/// @param scope the scope of the final enum. Note that this function
14049/// does *NOT* add the built type to this scope. The scope is just so
14050/// that the function knows how to name anonymous enums.
14051///
14052/// @param is_declaration_only is true if the DIE denoted by @p die is
14053/// a declaration-only DIE.
14054///
14055/// @return the built enum_type_decl or NULL if it could not be built.
14057build_enum_type(reader& rdr,
14058 Dwarf_Die* die,
14059 scope_decl* scope,
14060 size_t where_offset,
14061 bool is_declaration_only)
14062{
14063 enum_type_decl_sptr result;
14064 if (!die)
14065 return result;
14066
14067 unsigned tag = dwarf_tag(die);
14068 if (tag != DW_TAG_enumeration_type)
14069 return result;
14070
14071 string name, linkage_name;
14072 location loc;
14073 die_loc_and_name(rdr, die, loc, name, linkage_name);
14074
14075 bool is_anonymous = false;
14076 // If the enum is anonymous, let's give it a name.
14077 if (name.empty())
14078 {
14079 name = get_internal_anonymous_die_prefix_name(die);
14080 ABG_ASSERT(!name.empty());
14081 // But we remember that the type is anonymous.
14082 is_anonymous = true;
14083
14084 scope_decl* sc = scope ? scope : rdr.global_scope().get();
14085 if (size_t s = sc->get_num_anonymous_member_enums())
14086 name = build_internal_anonymous_die_name(name, s);
14087 }
14088
14089 bool use_odr = rdr.odr_is_relevant(die);
14090 // If the type has location, then associate it to its
14091 // representation. This way, all occurences of types with the same
14092 // representation (name) and location can be later detected as being
14093 // for the same type.
14094
14095 if (!is_anonymous)
14096 {
14097 if (use_odr)
14098 {
14099 if (enum_type_decl_sptr pre_existing_enum =
14100 is_enum_type(rdr.lookup_artifact_from_die(die)))
14101 result = pre_existing_enum;
14102 }
14103 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14104 {
14105 if (loc)
14106 result = lookup_enum_type_per_location(loc.expand(), *corp);
14107 }
14108 else if (loc)
14109 {
14110 if (enum_type_decl_sptr pre_existing_enum =
14111 is_enum_type(rdr.lookup_artifact_from_die(die)))
14112 if (pre_existing_enum->get_location() == loc)
14113 result = pre_existing_enum;
14114 }
14115
14116 if (result)
14117 {
14118 rdr.associate_die_to_type(die, result, where_offset);
14119 return result;
14120 }
14121 }
14122 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
14123 // we can look them up?
14124
14125 uint64_t size = 0;
14126 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14127 size *= 8;
14128 bool is_artificial = die_is_artificial(die);
14129
14130 // for now we consider that underlying types of enums are all anonymous
14131 bool enum_underlying_type_is_anonymous= true;
14132
14134 Dwarf_Die child;
14135 if (dwarf_child(die, &child) == 0)
14136 {
14137 do
14138 {
14139 if (dwarf_tag(&child) != DW_TAG_enumerator)
14140 continue;
14141
14142 string n, m;
14143 location l;
14144 die_loc_and_name(rdr, &child, l, n, m);
14145 uint64_t val = 0;
14146 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
14147 enms.push_back(enum_type_decl::enumerator(n, val));
14148 }
14149 while (dwarf_siblingof(&child, &child) == 0);
14150 }
14151
14152 // DWARF up to version 4 (at least) doesn't seem to carry the
14153 // underlying type, so let's create an artificial one here, which
14154 // sole purpose is to be passed to the constructor of the
14155 // enum_type_decl type.
14156 type_decl_sptr t =
14157 build_enum_underlying_type(rdr, name, size,
14158 enum_underlying_type_is_anonymous);
14159 t->set_is_declaration_only(is_declaration_only);
14160
14161 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
14162 result->set_is_anonymous(is_anonymous);
14163 result->set_is_declaration_only(is_declaration_only);
14164 result->set_is_artificial(is_artificial);
14165 rdr.associate_die_to_type(die, result, where_offset);
14166
14167 return result;
14168}
14169
14170/// Once a function_decl has been built and added to a class as a
14171/// member function, this function updates the information of the
14172/// function_decl concerning the properties of its relationship with
14173/// the member class. That is, it updates properties like
14174/// virtualness, access, constness, cdtorness, etc ...
14175///
14176/// @param die the DIE of the function_decl that has been just built.
14177///
14178/// @param f the function_decl that has just been built from @p die.
14179///
14180/// @param klass the @ref class_or_union that @p f belongs to.
14181///
14182/// @param rdr the context used to read the ELF/DWARF information.
14183static void
14184finish_member_function_reading(Dwarf_Die* die,
14185 const function_decl_sptr& f,
14186 const class_or_union_sptr klass,
14187 reader& rdr)
14188{
14189 ABG_ASSERT(klass);
14190
14191 method_decl_sptr m = is_method_decl(f);
14192 ABG_ASSERT(m);
14193
14194 method_type_sptr method_t = is_method_type(m->get_type());
14195 ABG_ASSERT(method_t);
14196
14197 size_t is_inline = die_is_declared_inline(die);
14198 bool is_ctor = (f->get_name() == klass->get_name());
14199 bool is_dtor = (!f->get_name().empty()
14200 && static_cast<string>(f->get_name())[0] == '~');
14201 bool is_virtual = die_is_virtual(die);
14202 int64_t vindex = -1;
14203 if (is_virtual)
14204 die_virtual_function_index(die, vindex);
14205 access_specifier access = public_access;
14206 if (class_decl_sptr c = is_class_type(klass))
14207 if (!c->is_struct())
14208 access = private_access;
14209 die_access_specifier(die, access);
14210
14211 m->is_declared_inline(is_inline);
14212 set_member_access_specifier(m, access);
14213 if (is_virtual)
14214 set_member_function_virtuality(m, is_virtual, vindex);
14215 bool is_static = method_t->get_is_for_static_method();
14216 set_member_is_static(m, is_static);
14217 set_member_function_is_ctor(m, is_ctor);
14218 set_member_function_is_dtor(m, is_dtor);
14219 set_member_function_is_const(m, method_t->get_is_const());
14220
14222
14223 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol()
14225 {
14226 // This is a virtual member function which has a linkage name
14227 // but has no underlying symbol set.
14228 //
14229 // The underlying elf symbol to set to this function can show up
14230 // later in the DWARF input or it can be that, because of some
14231 // compiler optimization, the relation between this function and
14232 // its underlying elf symbol is simply not emitted in the DWARF.
14233 //
14234 // Let's thus schedule this function for a later fixup pass
14235 // (performed by
14236 // reader::fixup_functions_with_no_symbols()) that will
14237 // set its underlying symbol.
14238 //
14239 // Note that if the underying symbol is encountered later in the
14240 // DWARF input, then the part of build_function_decl() that
14241 // updates the function to set its underlying symbol will
14242 // de-schedule this function wrt fixup pass.
14243 Dwarf_Off die_offset = dwarf_dieoffset(die);
14244 die_function_decl_map_type &fns_with_no_symbol =
14245 rdr.die_function_decl_with_no_symbol_map();
14246 die_function_decl_map_type::const_iterator i =
14247 fns_with_no_symbol.find(die_offset);
14248 if (i == fns_with_no_symbol.end())
14249 fns_with_no_symbol[die_offset] = f;
14250 }
14251
14252}
14253
14254/// If a function DIE has attributes which have not yet been read and
14255/// added to the internal representation that represents that function
14256/// then read those extra attributes and update the internal
14257/// representation.
14258///
14259/// @param rdr the DWARF reader to use.
14260///
14261/// @param die the function DIE to consider.
14262///
14263/// @param where_offset where we logical are, currently, in the stream
14264/// of DIEs. If you don't know what this is, you can just set it to zero.
14265///
14266/// @param existing_fn the representation of the function to update.
14267///
14268/// @return the updated function representation.
14269static function_decl_sptr
14270maybe_finish_function_decl_reading(reader& rdr,
14271 Dwarf_Die* die,
14272 size_t where_offset,
14273 const function_decl_sptr& existing_fn)
14274{
14275 function_decl_sptr result = build_function_decl(rdr, die,
14276 where_offset,
14277 existing_fn);
14278
14279 return result;
14280}
14281
14282/// Lookup a class or a typedef with a given qualified name in the
14283/// corpus that a given scope belongs to.
14284///
14285/// @param scope the scope to consider.
14286///
14287/// @param type_name the qualified name of the type to look for.
14288///
14289/// @return the typedef or class type found.
14290static type_base_sptr
14291lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
14292{
14293 string qname = build_qualified_name(scope, type_name);
14294 corpus* corp = scope->get_corpus();
14295 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
14296 return result;
14297}
14298
14299/// Lookup a class of typedef type from the current corpus being
14300/// constructed.
14301///
14302/// The type being looked for has the same name as a given DIE.
14303///
14304/// @param rdr the DWARF reader to use.
14305///
14306/// @param die the DIE which has the same name as the type we are
14307/// looking for.
14308///
14309/// @param called_for_public_decl whether this function is being
14310/// called from a a publicly defined declaration.
14311///
14312/// @param where_offset where we are logically at in the DIE stream.
14313///
14314/// @return the type found.
14315static type_base_sptr
14316lookup_class_or_typedef_from_corpus(reader& rdr,
14317 Dwarf_Die* die,
14318 bool called_for_public_decl,
14319 size_t where_offset)
14320{
14321 if (!die)
14322 return class_decl_sptr();
14323
14324 string class_name = die_string_attribute(die, DW_AT_name);
14325 if (class_name.empty())
14326 return class_decl_sptr();
14327
14328 scope_decl_sptr scope = get_scope_for_die(rdr, die,
14329 called_for_public_decl,
14330 where_offset);
14331 if (scope)
14332 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
14333
14334 return type_base_sptr();
14335}
14336
14337/// Test if a DIE represents a function that is a member of a given
14338/// class type.
14339///
14340/// @param rdr the DWARF reader.
14341///
14342/// @param function_die the DIE of the function to consider.
14343///
14344/// @param class_type the class type to consider.
14345///
14346/// @param where_offset where we are logically at in the DIE stream.
14347///
14348/// @return the method declaration corresponding to the member
14349/// function of @p class_type, iff @p function_die is for a member
14350/// function of @p class_type.
14351static method_decl_sptr
14352is_function_for_die_a_member_of_class(reader& rdr,
14353 Dwarf_Die* function_die,
14354 const class_or_union_sptr& class_type)
14355{
14356 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
14357
14358 if (!artifact)
14359 return method_decl_sptr();
14360
14361 method_decl_sptr method = is_method_decl(artifact);
14362 method_type_sptr method_type;
14363
14364 if (method)
14365 method_type = method->get_type();
14366 else
14367 method_type = is_method_type(artifact);
14368 ABG_ASSERT(method_type);
14369
14370 class_or_union_sptr method_class = method_type->get_class_type();
14371 ABG_ASSERT(method_class);
14372
14373 string method_class_name = method_class->get_qualified_name(),
14374 class_type_name = class_type->get_qualified_name();
14375
14376 if (method_class_name == class_type_name)
14377 {
14378 //ABG_ASSERT(class_type.get() == method_class.get());
14379 return method;
14380 }
14381
14382 return method_decl_sptr();
14383}
14384
14385/// If a given function DIE represents an existing member function of
14386/// a given class, then update that member function with new
14387/// properties present in the DIE. Otherwise, if the DIE represents a
14388/// new member function that is not already present in the class then
14389/// add that new member function to the class.
14390///
14391/// @param rdr the DWARF reader.
14392///
14393/// @param function_die the DIE of the potential member function to
14394/// consider.
14395///
14396/// @param class_type the class type to consider.
14397///
14398/// @param called_from_public_decl is true iff this function was
14399/// called from a publicly defined and exported declaration.
14400///
14401/// @param where_offset where we are logically at in the DIE stream.
14402///
14403/// @return the method decl representing the member function.
14404static method_decl_sptr
14405add_or_update_member_function(reader& rdr,
14406 Dwarf_Die* function_die,
14407 const class_or_union_sptr& class_type,
14408 bool called_from_public_decl,
14409 size_t where_offset)
14410{
14411 method_decl_sptr method =
14412 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
14413
14414 if (!method)
14415 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
14416 class_type.get(),
14417 called_from_public_decl,
14418 where_offset));
14419 if (!method)
14420 return method_decl_sptr();
14421
14422 finish_member_function_reading(function_die,
14423 is_function_decl(method),
14424 class_type, rdr);
14425 return method;
14426}
14427
14428/// Build a an IR node for class type from a DW_TAG_structure_type or
14429/// DW_TAG_class_type DIE and add that node to the ABI corpus being
14430/// currently built.
14431///
14432/// If the represents class type that already exists, then update the
14433/// existing class type with the new properties found in the DIE.
14434///
14435/// It meanst that this function can also update an existing
14436/// class_decl node with data members, member functions and other
14437/// properties coming from the DIE.
14438///
14439/// @param rdr the DWARF reader to consider.
14440///
14441/// @param die the DIE to read information from. Must be either a
14442/// DW_TAG_structure_type or a DW_TAG_class_type.
14443///
14444/// @param scope a pointer to the scope_decl* under which this class
14445/// is to be added to.
14446///
14447/// @param is_struct whether the class was declared as a struct.
14448///
14449/// @param klass if non-null, this is a klass to append the members
14450/// to. Otherwise, this function just builds the class from scratch.
14451///
14452/// @param called_from_public_decl set to true if this class is being
14453/// called from a "Public declaration like vars or public symbols".
14454///
14455/// @param where_offset the offset of the DIE where we are "logically"
14456/// positionned at, in the DIE tree. This is useful when @p die is
14457/// e.g, DW_TAG_partial_unit that can be included in several places in
14458/// the DIE tree.
14459///
14460/// @param is_declaration_only is true if the DIE denoted by @p die is
14461/// a declaration-only DIE.
14462///
14463/// @return the resulting class_type.
14464static class_decl_sptr
14465add_or_update_class_type(reader& rdr,
14466 Dwarf_Die* die,
14467 scope_decl* scope,
14468 bool is_struct,
14469 class_decl_sptr klass,
14470 bool called_from_public_decl,
14471 size_t where_offset,
14472 bool is_declaration_only)
14473{
14474 class_decl_sptr result;
14475 if (!die)
14476 return result;
14477
14478 const die_source source = rdr.get_die_source(die);
14479
14480 unsigned tag = dwarf_tag(die);
14481
14482 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
14483 return result;
14484
14485 {
14486 die_class_or_union_map_type::const_iterator i =
14487 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14488 if (i != rdr.die_wip_classes_map(source).end())
14489 {
14490 class_decl_sptr class_type = is_class_type(i->second);
14491 ABG_ASSERT(class_type);
14492 return class_type;
14493 }
14494 }
14495
14496 string name, linkage_name;
14497 location loc;
14498 die_loc_and_name(rdr, die, loc, name, linkage_name);
14499 cleanup_decl_name(name);
14500
14501 bool is_anonymous = false;
14502 if (name.empty())
14503 {
14504 // So we are looking at an anonymous struct. Let's
14505 // give it a name.
14506 name = get_internal_anonymous_die_prefix_name(die);
14507 ABG_ASSERT(!name.empty());
14508 // But we remember that the type is anonymous.
14509 is_anonymous = true;
14510
14511 size_t s = 0;
14512 if (scope)
14513 s = scope->get_num_anonymous_member_classes();
14514 else
14515 s = rdr.global_scope()->get_num_anonymous_member_classes();
14516 name = build_internal_anonymous_die_name(name, s);
14517 }
14518
14519 if (!is_anonymous)
14520 {
14521 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14522 {
14523 if (loc)
14524 // TODO: if there is only one class defined in the corpus
14525 // for this location, then re-use it. But if there are
14526 // more than one, then do not re-use it, for now.
14527 result = lookup_class_type_per_location(loc.expand(), *corp);
14528 else
14529 // TODO: if there is just one class for that name defined,
14530 // then re-use it. Otherwise, don't.
14531 result = lookup_class_type(name, *corp);
14532 if (result
14533 // If we are seeing a declaration of a definition we
14534 // already had, or if we are seing a type with the same
14535 // declaration-only-ness that we had before, then keep
14536 // the one we already had.
14537 && (result->get_is_declaration_only() == is_declaration_only
14538 || (!result->get_is_declaration_only()
14539 && is_declaration_only)))
14540 {
14541 rdr.associate_die_to_type(die, result, where_offset);
14542 return result;
14543 }
14544 else
14545 // We might be seeing the definition of a declaration we
14546 // already had. In that case, keep the definition and
14547 // drop the declaration.
14548 result.reset();
14549 }
14550 }
14551
14552 // If we've already seen the same class as 'die', then let's re-use
14553 // that one, unless it's an anonymous class. We can't really safely
14554 // re-use anonymous classes as they have no name, by construction.
14555 // What we can do, rather, is to reuse the typedef that name them,
14556 // when they do have a naming typedef.
14557 if (!is_anonymous)
14558 if (class_decl_sptr pre_existing_class =
14559 is_class_type(rdr.lookup_type_artifact_from_die(die)))
14560 klass = pre_existing_class;
14561
14562 uint64_t size = 0;
14563 die_size_in_bits(die, size);
14564 bool is_artificial = die_is_artificial(die);
14565
14566 Dwarf_Die child;
14567 bool has_child = (dwarf_child(die, &child) == 0);
14568
14569 decl_base_sptr res;
14570 if (klass)
14571 {
14572 res = result = klass;
14573 if (has_child && klass->get_is_declaration_only()
14574 && klass->get_definition_of_declaration())
14575 res = result = is_class_type(klass->get_definition_of_declaration());
14576 if (loc)
14577 result->set_location(loc);
14578 }
14579 else
14580 {
14581 result.reset(new class_decl(rdr.env(), name, size,
14582 /*alignment=*/0, is_struct, loc,
14583 decl_base::VISIBILITY_DEFAULT,
14584 is_anonymous));
14585
14586 result->set_is_declaration_only(is_declaration_only);
14587
14588 res = add_decl_to_scope(result, scope);
14589 result = dynamic_pointer_cast<class_decl>(res);
14590 ABG_ASSERT(result);
14591 }
14592
14593 if (!klass || klass->get_is_declaration_only())
14594 if (size != result->get_size_in_bits())
14595 result->set_size_in_bits(size);
14596
14597 if (klass)
14598 // We are amending a class that was built before. So let's check
14599 // if we need to amend its "declaration-only-ness" status.
14600 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
14601 // The size of the class doesn't match its
14602 // 'declaration-only-ness". We might have a non-zero sized
14603 // class which is declaration-only, or a zero sized class that
14604 // is not declaration-only. Let's set the declaration-only-ness
14605 // according to what we are instructed to.
14606 //
14607 // Note however that there are binaries out there emitted by
14608 // compilers (Clang, in C++) emit declarations-only classes that
14609 // have non-zero size. So we must honor these too. That is why
14610 // we are not forcing the declaration-only-ness to false when a
14611 // class has non-zero size. An example of such binary is
14612 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
14613 result->set_is_declaration_only(is_declaration_only);
14614
14615 // If a non-decl-only class has children node and is advertized as
14616 // having a non-zero size let's trust that.
14617 if (!result->get_is_declaration_only() && has_child)
14618 if (result->get_size_in_bits() == 0 && size != 0)
14619 result->set_size_in_bits(size);
14620
14621 result->set_is_artificial(is_artificial);
14622
14623 rdr.associate_die_to_type(die, result, where_offset);
14624
14625 if (!has_child)
14626 // TODO: set the access specifier for the declaration-only class
14627 // here.
14628 return result;
14629
14630 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14631
14632 bool is_incomplete_type = false;
14633 if (is_declaration_only && size == 0 && has_child)
14634 // this is an incomplete DWARF type as defined by [5.7.1]
14635 //
14636 // An incomplete structure, union or class type is represented by
14637 // a structure, union or class entry that does not have a byte
14638 // size attribute and that has a DW_AT_declaration attribute.
14639 //
14640 // Let's consider that it's thus a decl-only class, likely
14641 // referred to by a pointer. If we later encounter a definition
14642 // for this decl-only class type, then this decl-only class will
14643 // be resolved to it by the code in
14644 // reader::resolve_declaration_only_classes.
14645 is_incomplete_type = true;
14646
14647 scope_decl_sptr scop =
14648 dynamic_pointer_cast<scope_decl>(res);
14649 ABG_ASSERT(scop);
14650 rdr.scope_stack().push(scop.get());
14651
14652 if (has_child && !is_incomplete_type)
14653 {
14654 do
14655 {
14656 tag = dwarf_tag(&child);
14657
14658 // Handle base classes.
14659 if (tag == DW_TAG_inheritance)
14660 {
14661 result->set_is_declaration_only(false);
14662
14663 Dwarf_Die type_die;
14664 if (!die_die_attribute(&child, DW_AT_type, type_die))
14665 continue;
14666
14667 string type_name = die_type_name(rdr, &type_die,
14668 /*qualified_name=*/true,
14669 where_offset);
14670 type_base_sptr base_type;
14671 if (!type_name.empty())
14672 {
14673 base_type = result->find_base_class(type_name);
14674 if (base_type)
14675 continue;
14676 }
14677
14678 base_type =
14679 lookup_class_or_typedef_from_corpus(rdr, &type_die,
14680 called_from_public_decl,
14681 where_offset);
14682 if (!base_type)
14683 base_type =
14684 is_type(build_ir_node_from_die(rdr, &type_die,
14685 called_from_public_decl,
14686 where_offset));
14687
14688 // Sometimes base_type can be a typedef. Let's make
14689 // sure that typedef is compatible with a class type.
14691 if (!b)
14692 continue;
14693
14694 access_specifier access =
14695 is_struct
14696 ? public_access
14697 : private_access;
14698
14699 die_access_specifier(&child, access);
14700
14701 bool is_virt= die_is_virtual(&child);
14702 int64_t offset = 0;
14703 bool is_offset_present =
14704 die_member_offset(rdr, &child, offset);
14705
14706 class_decl::base_spec_sptr base(new class_decl::base_spec
14707 (b, access,
14708 is_offset_present ? offset : -1,
14709 is_virt));
14710 if (b->get_is_declaration_only()
14711 // Only non-anonymous decl-only classes are
14712 // scheduled for resolution to their definition.
14713 // Anonymous classes that are decl-only are likely
14714 // only artificially created by
14715 // get_opaque_version_of_type, from anonymous fully
14716 // defined classes. Those are never defined.
14717 && !b->get_qualified_name().empty())
14718 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
14719 if (result->find_base_class(b->get_qualified_name()))
14720 continue;
14721 result->add_base_specifier(base);
14722 }
14723 // Handle data members.
14724 else if (tag == DW_TAG_member
14725 || tag == DW_TAG_variable)
14726 {
14727 Dwarf_Die type_die;
14728 if (!die_die_attribute(&child, DW_AT_type, type_die))
14729 continue;
14730
14731 string n, m;
14732 location loc;
14733 die_loc_and_name(rdr, &child, loc, n, m);
14734 /// For now, we skip the hidden vtable pointer.
14735 /// Currently, we're looking for a member starting with
14736 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
14737 /// use as a name for the hidden vtable pointer.
14738 if (n.substr(0, 5) == "_vptr"
14739 && n.size() > 5
14740 && !std::isalnum(n.at(5))
14741 && n.at(5) != '_')
14742 continue;
14743
14744 // If the variable is already a member of this class,
14745 // move on. If it's an anonymous data member, we need
14746 // to handle it differently. We'll do that later below.
14747 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14748 continue;
14749
14750 int64_t offset_in_bits = 0;
14751 bool is_laid_out = die_member_offset(rdr, &child,
14752 offset_in_bits);
14753 // For now, is_static == !is_laid_out. When we have
14754 // templates, we'll try to be more specific. For now,
14755 // this approximation should do OK.
14756 bool is_static = !is_laid_out;
14757
14758 if (is_static)
14759 // We are looking at the *declaration* of a static
14760 // data member. The definition comes later (or
14761 // somewhere else, rather)in the DWARF. It's the
14762 // definition that we are interested in because it has
14763 // attributes of the concrete representation of the
14764 // static data member like, the ELF symbol (storage
14765 // address) of the variable, etc. It's at that point
14766 // that the IR of the data member is going to be
14767 // created (by build_ir_node_from_die, in the
14768 // DW_TAG_variable case) and added to this class/struct
14769 // being created. So for now, just ignore it.
14770 continue;
14771
14772 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
14773 called_from_public_decl,
14774 where_offset));
14775 type_base_sptr t = is_type(ty);
14776 if (!t)
14777 continue;
14778
14779 if (n.empty() && !die_is_anonymous_data_member(&child))
14780 {
14781 // We must be in a case where the data member has an
14782 // empty name because the DWARF emitter has a bug.
14783 // Let's generate an artificial name for that data
14784 // member.
14785 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
14786 ABG_ASSERT(!n.empty());
14787 }
14788
14789 // The call to build_ir_node_from_die above could have
14790 // triggered the adding of a data member named 'n' into
14791 // result. So let's check again if the variable is
14792 // already a member of this class. Here again, if it's
14793 // an anonymous data member, we need to handle it
14794 // differently. We'll do that later below.
14795 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14796 continue;
14797
14798 if (!is_static)
14799 // We have a non-static data member. So this class
14800 // cannot be a declaration-only class anymore, even if
14801 // some DWARF emitters might consider it otherwise.
14802 result->set_is_declaration_only(false);
14803 access_specifier access =
14804 is_struct
14805 ? public_access
14806 : private_access;
14807
14808 die_access_specifier(&child, access);
14809
14810 var_decl_sptr dm(new var_decl(n, t, loc, m));
14811 if (n.empty()
14813 // dm is an anonymous data member that was already
14814 // present in the current class so let's not add it.
14815 continue;
14816 result->add_data_member(dm, access, is_laid_out,
14817 is_static, offset_in_bits);
14818 ABG_ASSERT(has_scope(dm));
14819 rdr.associate_die_to_decl(&child, dm, where_offset,
14820 /*associate_by_repr=*/false);
14821 }
14822 // Handle member functions;
14823 else if (tag == DW_TAG_subprogram)
14824 {
14825 decl_base_sptr r =
14826 add_or_update_member_function(rdr, &child, result,
14827 called_from_public_decl,
14828 where_offset);
14830 rdr.associate_die_to_decl(&child, f, where_offset,
14831 /*associate_by_repr=*/true);
14832 }
14833 // Handle member types
14834 else if (die_is_type(&child))
14835 {
14836 // if the type is not already a member of this class,
14837 // then add it to the class.
14838 if (!is_anonymous_type_die(&child)
14839 && !result->find_member_type(die_name(&child)))
14840 build_ir_node_from_die(rdr, &child, result.get(),
14841 called_from_public_decl,
14842 where_offset);
14843 else if (is_anonymous_type_die(&child))
14844 {
14845 // Lookup the anonymous type DIE direcly by building
14846 // its flat representation & using it as the name of
14847 // the anonymous struct/union.
14848 string anonymous_type_name =
14849 die_class_or_enum_flat_representation(rdr, &child,
14850 /*indent=*/"",
14851 /*one_line=*/true,
14852 /*qualed_name=*/false,
14853 where_offset);
14854 if (type_base_sptr member_t =
14855 result->find_member_type(anonymous_type_name))
14856 rdr.associate_die_to_decl(&child, is_decl(member_t),
14857 where_offset,
14858 /*Associate_by_repr=*/false);
14859 else
14860 {
14861 type_base_sptr t =
14862 is_type(build_ir_node_from_die(rdr, &child,
14863 /*scope=*/result.get(),
14864 called_from_public_decl,
14865 where_offset));
14866 if (t)
14867 {
14868 add_decl_to_scope(is_decl(t), result.get());
14869 maybe_set_member_type_access_specifier(result,
14870 &child);
14871 }
14872 }
14873 }
14874 }
14875 } while (dwarf_siblingof(&child, &child) == 0);
14876 }
14877
14878 rdr.scope_stack().pop();
14879
14880 {
14881 die_class_or_union_map_type::const_iterator i =
14882 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14883 if (i != rdr.die_wip_classes_map(source).end())
14884 {
14885 if (is_member_type(i->second))
14887 get_member_access_specifier(i->second));
14888 rdr.die_wip_classes_map(source).erase(i);
14889 }
14890 }
14891
14892 return result;
14893}
14894
14895/// Build an @ref union_decl from a DW_TAG_union_type DIE.
14896///
14897/// @param rdr the DWARF reader to use.
14898///
14899/// @param die the DIE to read from.
14900///
14901/// @param scope the scope the resulting @ref union_decl belongs to.
14902///
14903/// @param union_type if this parameter is non-nil, then this function
14904/// updates the @ref union_decl that it points to, rather than
14905/// creating a new @ref union_decl.
14906///
14907/// @param called_from_public_decl is true if this function has been
14908/// initially called within the context of a public decl.
14909///
14910/// @param where_offset the offset of the DIE where we are "logically"
14911/// positionned at, in the DIE tree. This is useful when @p die is
14912/// e.g, DW_TAG_partial_unit that can be included in several places in
14913/// the DIE tree.
14914///
14915/// @param is_declaration_only is true if the DIE denoted by @p die is
14916/// a declaration-only DIE.
14917///
14918/// @return the resulting @ref union_decl type.
14919static union_decl_sptr
14920add_or_update_union_type(reader& rdr,
14921 Dwarf_Die* die,
14922 scope_decl* scope,
14923 union_decl_sptr union_type,
14924 bool called_from_public_decl,
14925 size_t where_offset,
14926 bool is_declaration_only)
14927{
14928 union_decl_sptr result;
14929 if (!die)
14930 return result;
14931
14932 unsigned tag = dwarf_tag(die);
14933
14934 if (tag != DW_TAG_union_type)
14935 return result;
14936
14937 const die_source source = rdr.get_die_source(die);
14938 {
14939 die_class_or_union_map_type::const_iterator i =
14940 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14941 if (i != rdr.die_wip_classes_map(source).end())
14942 {
14943 union_decl_sptr u = is_union_type(i->second);
14944 ABG_ASSERT(u);
14945 return u;
14946 }
14947 }
14948
14949 string name, linkage_name;
14950 location loc;
14951 die_loc_and_name(rdr, die, loc, name, linkage_name);
14952 cleanup_decl_name(name);
14953
14954 bool is_anonymous = false;
14955 if (name.empty())
14956 {
14957 // So we are looking at an anonymous union. Let's give it a
14958 // name.
14959 name = get_internal_anonymous_die_prefix_name(die);
14960 ABG_ASSERT(!name.empty());
14961 // But we remember that the type is anonymous.
14962 is_anonymous = true;
14963
14964 size_t s = 0;
14965 if (scope)
14966 s = scope->get_num_anonymous_member_unions();
14967 else
14968 s = rdr.global_scope()->get_num_anonymous_member_classes();
14969 name = build_internal_anonymous_die_name(name, s);
14970 }
14971
14972 // If the type has location, then associate it to its
14973 // representation. This way, all occurences of types with the same
14974 // representation (name) and location can be later detected as being
14975 // for the same type.
14976
14977 if (!is_anonymous)
14978 {
14979 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14980 {
14981 if (loc)
14982 result = lookup_union_type_per_location(loc.expand(), *corp);
14983 else
14984 result = lookup_union_type(name, *corp);
14985
14986 if (result)
14987 {
14988 rdr.associate_die_to_type(die, result, where_offset);
14989 return result;
14990 }
14991 }
14992 }
14993
14994 // if we've already seen a union with the same union as 'die' then
14995 // let's re-use that one. We can't really safely re-use anonymous
14996 // unions as they have no name, by construction. What we can do,
14997 // rather, is to reuse the typedef that name them, when they do have
14998 // a naming typedef.
14999 if (!is_anonymous)
15000 if (union_decl_sptr pre_existing_union =
15001 is_union_type(rdr.lookup_artifact_from_die(die)))
15002 union_type = pre_existing_union;
15003
15004 uint64_t size = 0;
15005 die_size_in_bits(die, size);
15006 bool is_artificial = die_is_artificial(die);
15007
15008 if (union_type)
15009 {
15010 result = union_type;
15011 result->set_location(loc);
15012 }
15013 else
15014 {
15015 result.reset(new union_decl(rdr.env(), name, size, loc,
15016 decl_base::VISIBILITY_DEFAULT,
15017 is_anonymous));
15018 if (is_declaration_only)
15019 result->set_is_declaration_only(true);
15020 result = is_union_type(add_decl_to_scope(result, scope));
15021 ABG_ASSERT(result);
15022 }
15023
15024 if (size)
15025 {
15026 result->set_size_in_bits(size);
15027 result->set_is_declaration_only(false);
15028 }
15029
15030 result->set_is_artificial(is_artificial);
15031
15032 rdr.associate_die_to_type(die, result, where_offset);
15033
15034 Dwarf_Die child;
15035 bool has_child = (dwarf_child(die, &child) == 0);
15036 if (!has_child)
15037 return result;
15038
15039 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
15040
15041 scope_decl_sptr scop =
15042 dynamic_pointer_cast<scope_decl>(result);
15043 ABG_ASSERT(scop);
15044 rdr.scope_stack().push(scop.get());
15045
15046 if (has_child)
15047 {
15048 do
15049 {
15050 tag = dwarf_tag(&child);
15051 // Handle data members.
15052 if (tag == DW_TAG_member || tag == DW_TAG_variable)
15053 {
15054 Dwarf_Die type_die;
15055 if (!die_die_attribute(&child, DW_AT_type, type_die))
15056 continue;
15057
15058 string n, m;
15059 location loc;
15060 die_loc_and_name(rdr, &child, loc, n, m);
15061
15062 // Because we can be updating an existing union, let's
15063 // make sure we don't already have a member of the same
15064 // name. Anonymous member are handled a bit later below
15065 // so let's not consider them here.
15066 if (!n.empty() && lookup_var_decl_in_scope(n, result))
15067 continue;
15068
15069 ssize_t offset_in_bits = 0;
15070 decl_base_sptr ty =
15071 is_decl(build_ir_node_from_die(rdr, &type_die,
15072 called_from_public_decl,
15073 where_offset));
15074 type_base_sptr t = is_type(ty);
15075 if (!t)
15076 continue;
15077
15078 // We have a non-static data member. So this union
15079 // cannot be a declaration-only union anymore, even if
15080 // some DWARF emitters might consider it otherwise.
15081 result->set_is_declaration_only(false);
15082 access_specifier access = public_access;
15083
15084 die_access_specifier(&child, access);
15085
15086 var_decl_sptr dm(new var_decl(n, t, loc, m));
15087 // If dm is an anonymous data member, let's make sure
15088 // the current union doesn't already have it as a data
15089 // member.
15090 if (n.empty() && result->find_data_member(dm))
15091 continue;
15092
15093 if (!n.empty() && lookup_var_decl_in_scope(n, result))
15094 continue;
15095
15096 result->add_data_member(dm, access, /*is_laid_out=*/true,
15097 /*is_static=*/false,
15098 offset_in_bits);
15099 ABG_ASSERT(has_scope(dm));
15100 rdr.associate_die_to_decl(&child, dm, where_offset,
15101 /*associate_by_repr=*/false);
15102 }
15103 // Handle member functions;
15104 else if (tag == DW_TAG_subprogram)
15105 {
15106 decl_base_sptr r =
15107 is_decl(build_ir_node_from_die(rdr, &child,
15108 result.get(),
15109 called_from_public_decl,
15110 where_offset));
15111 if (!r)
15112 continue;
15113
15114 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
15115 ABG_ASSERT(f);
15116
15117 finish_member_function_reading(&child, f, result, rdr);
15118
15119 rdr.associate_die_to_decl(&child, f, where_offset,
15120 /*associate_by_repr=*/false);
15121 }
15122 // Handle member types
15123 else if (die_is_type(&child))
15124 {
15125 string type_name = die_type_name(rdr, &child,
15126 /*qualified_name=*/false,
15127 where_offset);
15128 if (type_base_sptr member_t = result->find_member_type(type_name))
15129 rdr.associate_die_to_decl(&child, is_decl(member_t),
15130 where_offset,
15131 /*associate_by_repr=*/false);
15132 else
15133 decl_base_sptr td =
15134 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
15135 called_from_public_decl,
15136 where_offset));
15137 }
15138 } while (dwarf_siblingof(&child, &child) == 0);
15139 }
15140
15141 rdr.scope_stack().pop();
15142
15143 {
15144 die_class_or_union_map_type::const_iterator i =
15145 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
15146 if (i != rdr.die_wip_classes_map(source).end())
15147 {
15148 if (is_member_type(i->second))
15150 get_member_access_specifier(i->second));
15151 rdr.die_wip_classes_map(source).erase(i);
15152 }
15153 }
15154
15155 return result;
15156}
15157
15158/// build a qualified type from a DW_TAG_const_type,
15159/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
15160///
15161/// @param rdr the DWARF reader to consider.
15162///
15163/// @param die the input DIE to read from.
15164///
15165/// @param called_from_public_decl true if this function was called
15166/// from a context where either a public function or a public variable
15167/// is being built.
15168///
15169/// @param where_offset the offset of the DIE where we are "logically"
15170/// positionned at, in the DIE tree. This is useful when @p die is
15171/// e.g, DW_TAG_partial_unit that can be included in several places in
15172/// the DIE tree.
15173///
15174/// @return the resulting qualified_type_def.
15175static type_base_sptr
15176build_qualified_type(reader& rdr,
15177 Dwarf_Die* die,
15178 bool called_from_public_decl,
15179 size_t where_offset)
15180{
15181 type_base_sptr result;
15182 if (!die)
15183 return result;
15184
15185 unsigned tag = dwarf_tag(die);
15186
15187 if (tag != DW_TAG_const_type
15188 && tag != DW_TAG_volatile_type
15189 && tag != DW_TAG_restrict_type)
15190 return result;
15191
15192 Dwarf_Die underlying_type_die;
15193 decl_base_sptr utype_decl;
15194 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15195 // So, if no DW_AT_type is present, then this means (if we are
15196 // looking at a debug info emitted by GCC) that we are looking
15197 // at a qualified void type.
15198 utype_decl = build_ir_node_for_void_type(rdr);
15199
15200 if (!utype_decl)
15201 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
15202 called_from_public_decl,
15203 where_offset));
15204 if (!utype_decl)
15205 return result;
15206
15207 // The call to build_ir_node_from_die() could have triggered the
15208 // creation of the type for this DIE. In that case, just return it.
15209 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15210 {
15211 result = t;
15212 rdr.associate_die_to_type(die, result, where_offset);
15213 return result;
15214 }
15215
15216 type_base_sptr utype = is_type(utype_decl);
15217 ABG_ASSERT(utype);
15218
15219 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
15220 if (tag == DW_TAG_const_type)
15221 qual |= qualified_type_def::CV_CONST;
15222 else if (tag == DW_TAG_volatile_type)
15223 qual |= qualified_type_def::CV_VOLATILE;
15224 else if (tag == DW_TAG_restrict_type)
15225 qual |= qualified_type_def::CV_RESTRICT;
15226 else
15228
15229 if (!result)
15230 result.reset(new qualified_type_def(utype, qual, location()));
15231
15232 rdr.associate_die_to_type(die, result, where_offset);
15233
15234 return result;
15235}
15236
15237/// Walk a tree of typedef of qualified arrays and schedule all type
15238/// nodes for canonicalization.
15239///
15240/// This is to be used after an array tree has been cloned. In that
15241/// case, the newly cloned type nodes have to be scheduled for
15242/// canonicalization.
15243///
15244/// This is a subroutine of maybe_strip_qualification.
15245///
15246/// @param t the type node to be scheduled for canonicalization.
15247///
15248/// @param rdr the DWARF reader to use.
15249static void
15250schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
15251 reader &rdr)
15252{
15253 if (typedef_decl_sptr type = is_typedef(t))
15254 {
15255 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15256 rdr);
15257 rdr.schedule_type_for_late_canonicalization(t);
15258 }
15259 else if (qualified_type_def_sptr type = is_qualified_type(t))
15260 {
15261 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15262 rdr);
15263 rdr.schedule_type_for_late_canonicalization(t);
15264 }
15265 else if (array_type_def_sptr type = is_array_type(t))
15266 {
15267 for (vector<array_type_def::subrange_sptr>::const_iterator i =
15268 type->get_subranges().begin();
15269 i != type->get_subranges().end();
15270 ++i)
15271 {
15272 if (!(*i)->get_scope())
15273 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
15274 rdr.schedule_type_for_late_canonicalization(*i);
15275
15276 }
15277 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
15278 rdr);
15279 rdr.schedule_type_for_late_canonicalization(type);
15280 }
15281}
15282
15283/// Strip qualification from a qualified type, when it makes sense.
15284///
15285/// DWARF constructs "const reference". This is redundant because a
15286/// reference is always const. The issue is these redundant types then
15287/// leak into the IR and make for bad diagnostics.
15288///
15289/// This function thus strips the const qualifier from the type in
15290/// that case. It might contain code to strip other cases like this
15291/// in the future.
15292///
15293/// @param t the type to strip const qualification from.
15294///
15295/// @param rdr the @ref reader to use.
15296///
15297/// @return the stripped type or just return @p t.
15298static decl_base_sptr
15299maybe_strip_qualification(const qualified_type_def_sptr t,
15300 reader &rdr)
15301{
15302 if (!t)
15303 return t;
15304
15305 decl_base_sptr result = t;
15306 type_base_sptr u = t->get_underlying_type();
15307
15310 if (result.get() != t.get())
15311 return result;
15312
15314 {
15315 array_type_def_sptr array;
15316 scope_decl * scope = 0;
15317 if ((array = is_array_type(u)))
15318 {
15319 scope = array->get_scope();
15320 ABG_ASSERT(scope);
15321 array = is_array_type(clone_array_tree(array));
15322 schedule_array_tree_for_late_canonicalization(array, rdr);
15323 add_decl_to_scope(array, scope);
15324 t->set_underlying_type(array);
15325 u = t->get_underlying_type();
15326 }
15327 else if (is_typedef_of_array(u))
15328 {
15329 scope = is_decl(u)->get_scope();
15330 ABG_ASSERT(scope);
15331 typedef_decl_sptr typdef =
15333 schedule_array_tree_for_late_canonicalization(typdef, rdr);
15334 ABG_ASSERT(typdef);
15335 add_decl_to_scope(typdef, scope);
15336 t->set_underlying_type(typdef);
15337 u = t->get_underlying_type();
15338 array = is_typedef_of_array(u);
15339 }
15340 else
15342
15343 ABG_ASSERT(array);
15344 // We should not be editing types that are already canonicalized.
15345 ABG_ASSERT(!array->get_canonical_type());
15346 type_base_sptr element_type = array->get_element_type();
15347
15348 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
15349 {
15350 // We should not be editing types that are already canonicalized.
15351 ABG_ASSERT(!qualified->get_canonical_type());
15352 qualified_type_def::CV quals = qualified->get_cv_quals();
15353 quals |= t->get_cv_quals();
15354 qualified->set_cv_quals(quals);
15356 result = is_decl(u);
15357 }
15358 else
15359 {
15360 qualified_type_def_sptr qual_type
15361 (new qualified_type_def(element_type,
15362 t->get_cv_quals(),
15363 t->get_location()));
15365 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
15366 array->set_element_type(qual_type);
15367 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
15368 result = is_decl(u);
15369 }
15370 }
15371
15372 return result;
15373}
15374
15375/// Build a pointer type from a DW_TAG_pointer_type DIE.
15376///
15377/// @param rdr the DWARF reader to consider.
15378///
15379/// @param die the DIE to read information from.
15380///
15381/// @param called_from_public_decl true if this function was called
15382/// from a context where either a public function or a public variable
15383/// is being built.
15384///
15385/// @param where_offset the offset of the DIE where we are "logically"
15386/// positionned at, in the DIE tree. This is useful when @p die is
15387/// e.g, DW_TAG_partial_unit that can be included in several places in
15388/// the DIE tree.
15389///
15390/// @return the resulting pointer to pointer_type_def.
15392build_pointer_type_def(reader& rdr,
15393 Dwarf_Die* die,
15394 bool called_from_public_decl,
15395 size_t where_offset)
15396{
15397 pointer_type_def_sptr result;
15398
15399 if (!die)
15400 return result;
15401
15402 unsigned tag = dwarf_tag(die);
15403 if (tag != DW_TAG_pointer_type)
15404 return result;
15405
15406 type_or_decl_base_sptr utype_decl;
15407 Dwarf_Die underlying_type_die;
15408 bool has_underlying_type_die = false;
15409 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15410 // If the DW_AT_type attribute is missing, that means we are
15411 // looking at a pointer to "void".
15412 utype_decl = build_ir_node_for_void_type(rdr);
15413 else
15414 has_underlying_type_die = true;
15415
15416 if (!utype_decl && has_underlying_type_die)
15417 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
15418 called_from_public_decl,
15419 where_offset);
15420 if (!utype_decl)
15421 return result;
15422
15423 // The call to build_ir_node_from_die() could have triggered the
15424 // creation of the type for this DIE. In that case, just return it.
15425 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15426 {
15427 result = is_pointer_type(t);
15428 ABG_ASSERT(result);
15429 return result;
15430 }
15431
15432 type_base_sptr utype = is_type(utype_decl);
15433 ABG_ASSERT(utype);
15434
15435 // if the DIE for the pointer type doesn't have a byte_size
15436 // attribute then we assume the size of the pointer is the address
15437 // size of the current translation unit.
15438 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15439 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15440 // The size as expressed by DW_AT_byte_size is in byte, so let's
15441 // convert it to bits.
15442 size *= 8;
15443
15444 // And the size of the pointer must be the same as the address size
15445 // of the current translation unit.
15446 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15447
15448 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
15449 ABG_ASSERT(result->get_pointed_to_type());
15450
15451 if (is_void_pointer_type(result))
15452 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
15453
15454 rdr.associate_die_to_type(die, result, where_offset);
15455 return result;
15456}
15457
15458/// Build a reference type from either a DW_TAG_reference_type or
15459/// DW_TAG_rvalue_reference_type DIE.
15460///
15461/// @param rdr the DWARF reader to consider.
15462///
15463/// @param die the DIE to read from.
15464///
15465/// @param called_from_public_decl true if this function was called
15466/// from a context where either a public function or a public variable
15467/// is being built.
15468///
15469/// @param where_offset the offset of the DIE where we are "logically"
15470/// positionned at, in the DIE tree. This is useful when @p die is
15471/// e.g, DW_TAG_partial_unit that can be included in several places in
15472/// the DIE tree.
15473///
15474/// @return a pointer to the resulting reference_type_def.
15476build_reference_type(reader& rdr,
15477 Dwarf_Die* die,
15478 bool called_from_public_decl,
15479 size_t where_offset)
15480{
15482
15483 if (!die)
15484 return result;
15485
15486 unsigned tag = dwarf_tag(die);
15487 if (tag != DW_TAG_reference_type
15488 && tag != DW_TAG_rvalue_reference_type)
15489 return result;
15490
15491 Dwarf_Die underlying_type_die;
15492 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15493 return result;
15494
15495 type_or_decl_base_sptr utype_decl =
15496 build_ir_node_from_die(rdr, &underlying_type_die,
15497 called_from_public_decl,
15498 where_offset);
15499 if (!utype_decl)
15500 return result;
15501
15502 // The call to build_ir_node_from_die() could have triggered the
15503 // creation of the type for this DIE. In that case, just return it.
15504 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15505 {
15506 result = is_reference_type(t);
15507 ABG_ASSERT(result);
15508 return result;
15509 }
15510
15511 type_base_sptr utype = is_type(utype_decl);
15512 ABG_ASSERT(utype);
15513
15514 // if the DIE for the reference type doesn't have a byte_size
15515 // attribute then we assume the size of the reference is the address
15516 // size of the current translation unit.
15517 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15518 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15519 size *= 8;
15520
15521 // And the size of the pointer must be the same as the address size
15522 // of the current translation unit.
15523 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15524
15525 bool is_lvalue = tag == DW_TAG_reference_type;
15526
15527 result.reset(new reference_type_def(utype, is_lvalue, size,
15528 /*alignment=*/0,
15529 location()));
15530 if (corpus_sptr corp = rdr.corpus())
15531 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
15532 result = t;
15533 rdr.associate_die_to_type(die, result, where_offset);
15534 return result;
15535}
15536
15537/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
15538/// DW_TAG_ptr_to_member_type.
15539///
15540/// @param the DWARF reader touse.
15541///
15542/// @param the DIE to consider. It must carry the tag
15543/// DW_TAG_ptr_to_member_type.
15544///
15545/// @param called_from_public_decl true if this function was called
15546/// from a context where either a public function or a public variable
15547/// is being built.
15548///
15549/// @param where_offset the offset of the DIE where we are "logically"
15550/// positionned at, in the DIE tree. This is useful when @p die is
15551/// e.g, DW_TAG_partial_unit that can be included in several places in
15552/// the DIE tree.
15553///
15554/// @return a pointer to the resulting @ref ptr_to_mbr_type.
15556build_ptr_to_mbr_type(reader& rdr,
15557 Dwarf_Die* die,
15558 bool called_from_public_decl,
15559 size_t where_offset)
15560{
15561 ptr_to_mbr_type_sptr result;
15562
15563 if (!die)
15564 return result;
15565
15566 unsigned tag = dwarf_tag(die);
15567 if (tag != DW_TAG_ptr_to_member_type)
15568 return result;
15569
15570 Dwarf_Die data_member_type_die, containing_type_die;
15571
15572 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
15573 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
15574 return result;
15575
15576 type_or_decl_base_sptr data_member_type =
15577 build_ir_node_from_die(rdr, &data_member_type_die,
15578 called_from_public_decl, where_offset);
15579 if (!data_member_type)
15580 return result;
15581
15582 type_or_decl_base_sptr containing_type =
15583 build_ir_node_from_die(rdr, &containing_type_die,
15584 called_from_public_decl, where_offset);
15585 if (!containing_type)
15586 return result;
15587
15589 (is_type(containing_type)))
15590 return result;
15591
15592 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15593 {
15594 result = is_ptr_to_mbr_type(t);
15595 ABG_ASSERT(result);
15596 return result;
15597 }
15598
15599 uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
15600
15601 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
15602 is_type(data_member_type),
15603 is_type(containing_type),
15604 size_in_bits,
15605 /*alignment=*/0,
15606 location()));
15607
15608 rdr.associate_die_to_type(die, result, where_offset);
15609 return result;
15610}
15611
15612/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
15613///
15614/// @param rdr the DWARF reader to consider.
15615///
15616/// @param die the DIE to read from.
15617///
15618/// @param is_method points to a class or union declaration iff we're
15619/// building the type for a method. This is the enclosing class or
15620/// union of the method.
15621///
15622/// @param where_offset the offset of the DIE where we are "logically"
15623/// positioned at, in the DIE tree. This is useful when @p die is
15624/// e.g, DW_TAG_partial_unit that can be included in several places in
15625/// the DIE tree.
15626///
15627/// @return a pointer to the resulting function_type_sptr.
15628static function_type_sptr
15629build_function_type(reader& rdr,
15630 Dwarf_Die* die,
15631 class_or_union_sptr is_method,
15632 size_t where_offset)
15633{
15634 function_type_sptr result;
15635
15636 if (!die)
15637 return result;
15638
15639 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
15640 || dwarf_tag(die) == DW_TAG_subprogram);
15641
15642 const die_source source = rdr.get_die_source(die);
15643
15644 {
15645 size_t off = dwarf_dieoffset(die);
15646 auto i = rdr.die_wip_function_types_map(source).find(off);
15647 if (i != rdr.die_wip_function_types_map(source).end())
15648 {
15649 function_type_sptr fn_type = is_function_type(i->second);
15650 ABG_ASSERT(fn_type);
15651 return fn_type;
15652 }
15653 }
15654
15655 decl_base_sptr type_decl;
15656
15657 translation_unit_sptr tu = rdr.cur_transl_unit();
15658 ABG_ASSERT(tu);
15659
15660 /// If, inside the current translation unit, we've already seen a
15661 /// function type with the same text representation, then reuse that
15662 /// one instead.
15663 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
15664 {
15665 result = is_function_type(t);
15666 ABG_ASSERT(result);
15667 rdr.associate_die_to_type(die, result, where_offset);
15668 return result;
15669 }
15670
15671 bool odr_is_relevant = rdr.odr_is_relevant(die);
15672 if (odr_is_relevant)
15673 {
15674 // So we can rely on the One Definition Rule to say that if
15675 // several different function types have the same name (or
15676 // rather, representation) across the entire binary, then they
15677 // ought to designate the same function type. So let's ensure
15678 // that if we've already seen a function type with the same
15679 // representation as the function type 'die', then it's the same
15680 // type as the one denoted by 'die'.
15681 if (function_type_sptr fn_type =
15682 is_function_type(rdr.lookup_type_artifact_from_die(die)))
15683 {
15684 rdr.associate_die_to_type(die, fn_type, where_offset);
15685 return fn_type;
15686 }
15687 }
15688
15689 // Let's look at the DIE to detect if it's the DIE for a method
15690 // (type). If it is, we can deduce the name of its enclosing class
15691 // and if it's a static or const.
15692 bool is_const = false;
15693 bool is_static = false;
15694 Dwarf_Die object_pointer_die;
15695 Dwarf_Die class_type_die;
15696 bool has_this_parm_die =
15697 die_function_type_is_method_type(rdr, die, where_offset,
15698 object_pointer_die,
15699 class_type_die,
15700 is_static);
15701 if (has_this_parm_die)
15702 {
15703 // The function (type) has a "this" parameter DIE. It means it's
15704 // a member function DIE.
15705 if (!is_static)
15706 if (die_object_pointer_is_for_const_method(&object_pointer_die))
15707 is_const = true;
15708
15709 if (!is_method)
15710 {
15711 // We were initially called as if the function represented
15712 // by DIE was *NOT* a member function. But now we know it's
15713 // a member function. Let's take that into account.
15714 class_or_union_sptr klass_type =
15715 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
15716 /*called_from_pub_decl=*/true,
15717 where_offset));
15718 if (!klass_type)
15719 {
15720 // We could not create the class type. For instance,
15721 // this can be due to the fact that the class is
15722 // suppressed. In those cases, we just bail out.
15723 return nullptr;
15724 }
15725 is_method = klass_type;
15726 }
15727 }
15728
15729 // Let's create the type early and record it as being for the DIE
15730 // 'die'. This way, when building the sub-type triggers the
15731 // creation of a type matching the same 'die', then we'll reuse this
15732 // one.
15733
15734 result.reset(is_method
15735 ? new method_type(is_method, is_const,
15736 tu->get_address_size(),
15737 /*alignment=*/0)
15738 : new function_type(rdr.env(), tu->get_address_size(),
15739 /*alignment=*/0));
15740 rdr.associate_die_to_type(die, result, where_offset);
15741 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
15742
15743 type_base_sptr return_type;
15744 Dwarf_Die ret_type_die;
15745 if (die_die_attribute(die, DW_AT_type, ret_type_die))
15746 return_type =
15747 is_type(build_ir_node_from_die(rdr, &ret_type_die,
15748 /*called_from_public_decl=*/true,
15749 where_offset));
15750 if (!return_type)
15751 return_type = is_type(build_ir_node_for_void_type(rdr));
15752 result->set_return_type(return_type);
15753
15754 Dwarf_Die child;
15755 function_decl::parameters function_parms;
15756
15757 if (dwarf_child(die, &child) == 0)
15758 do
15759 {
15760 int child_tag = dwarf_tag(&child);
15761 if (child_tag == DW_TAG_formal_parameter)
15762 {
15763 // This is a "normal" function parameter.
15764 string name, linkage_name;
15765 location loc;
15766 die_loc_and_name(rdr, &child, loc, name, linkage_name);
15768 // Sometimes, bogus compiler emit names that are
15769 // non-ascii garbage. Let's just ditch that for now.
15770 name.clear();
15771 bool is_artificial = die_is_artificial(&child);
15772 type_base_sptr parm_type;
15773 Dwarf_Die parm_type_die;
15774 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
15775 parm_type =
15776 is_type(build_ir_node_from_die(rdr, &parm_type_die,
15777 /*called_from_public_decl=*/true,
15778 where_offset));
15779 if (!parm_type)
15780 continue;
15781 if (is_method
15782 && is_const_qualified_type(parm_type)
15783 && function_parms.empty())
15784 // We are looking at the first (implicit) parameter of a
15785 // method. This is basically the "this pointer". For
15786 // concrete instances of abstract methods, GCC sometimes
15787 // represents that pointer as a const pointer, whereas
15788 // in the abstract interface representing that method
15789 // the this-pointer is represented as a non-qualified
15790 // pointer. Let's trim the const qualifier away. That
15791 // will minize the chance to have spurious
15792 // const-qualifier changes on implicit parameters when
15793 // comparing methods that otherwise have no meaningful
15794 // ABI changes.
15795 parm_type =
15797
15799 (new function_decl::parameter(parm_type, name, loc,
15800 /*variadic_marker=*/false,
15801 is_artificial));
15802 function_parms.push_back(p);
15803 }
15804 else if (child_tag == DW_TAG_unspecified_parameters)
15805 {
15806 // This is a variadic function parameter.
15807 bool is_artificial = die_is_artificial(&child);
15808
15809 type_base_sptr parm_type =
15810 is_type(build_ir_node_for_variadic_parameter_type(rdr));
15812 (new function_decl::parameter(parm_type,
15813 /*name=*/"",
15814 location(),
15815 /*variadic_marker=*/true,
15816 is_artificial));
15817 function_parms.push_back(p);
15818 // After a DW_TAG_unspecified_parameters tag, we shouldn't
15819 // keep reading for parameters. The
15820 // unspecified_parameters TAG should be the last parameter
15821 // that we record. For instance, if there are multiple
15822 // DW_TAG_unspecified_parameters DIEs then we should care
15823 // only for the first one.
15824 break;
15825 }
15826 }
15827 while (dwarf_siblingof(&child, &child) == 0);
15828
15829 result->set_parameters(function_parms);
15830
15831 tu->bind_function_type_life_time(result);
15832
15833 result->set_is_artificial(true);
15834
15835 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
15836
15837 {
15838 die_function_type_map_type::const_iterator i =
15839 rdr.die_wip_function_types_map(source).
15840 find(dwarf_dieoffset(die));
15841 if (i != rdr.die_wip_function_types_map(source).end())
15842 rdr.die_wip_function_types_map(source).erase(i);
15843 }
15844
15845 maybe_canonicalize_type(result, rdr);
15846 return result;
15847}
15848
15849/// Build a subrange type from a DW_TAG_subrange_type.
15850///
15851/// @param rdr the DWARF reader to consider.
15852///
15853/// @param die the DIE to read from.
15854///
15855/// @param where_offset the offset of the DIE where we are "logically"
15856/// positionned at in the DIE tree. This is useful when @p die is
15857/// e,g, DW_TAG_partial_unit that can be included in several places in
15858/// the DIE tree.
15859///
15860/// @param associate_die_to_type if this is true then the resulting
15861/// type is associated to the @p die, so that next time when the
15862/// system looks up the type associated to it, the current resulting
15863/// type is returned. If false, then no association is done and the
15864/// resulting type can be destroyed right after. This can be useful
15865/// when the sole purpose of building the @ref
15866/// array_type_def::subrange_type is to use some of its method like,
15867/// e.g, its name pretty printing methods.
15868///
15869/// @return the newly built instance of @ref
15870/// array_type_def::subrange_type, or nil if no type could be built.
15872build_subrange_type(reader& rdr,
15873 const Dwarf_Die* die,
15874 size_t where_offset,
15875 bool associate_type_to_die)
15876{
15878
15879 if (!die)
15880 return result;
15881
15882 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
15883 if (tag != DW_TAG_subrange_type)
15884 return result;
15885
15886 string name = die_name(die);
15887
15888 // load the underlying type.
15889 Dwarf_Die underlying_type_die;
15890 type_base_sptr underlying_type;
15891 /* Unless there is an underlying type which says differently. */
15892 bool is_signed = false;
15893 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
15894 underlying_type =
15895 is_type(build_ir_node_from_die(rdr,
15896 &underlying_type_die,
15897 /*called_from_public_decl=*/true,
15898 where_offset));
15899
15900 if (underlying_type)
15901 {
15902 uint64_t ate;
15903 if (die_unsigned_constant_attribute (&underlying_type_die,
15904 DW_AT_encoding,
15905 ate))
15906 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
15907 }
15908
15909 // The DW_TAG_subrange_type DIE may have some size related
15910 // attributes (DW_AT_byte_size or DW_AT_bit_size). If not, then the
15911 // size is deduced from the size of its underlying type.
15912 bool has_size_info = false;
15913 uint64_t size = 0;
15914 if ((has_size_info = die_unsigned_constant_attribute(die,
15915 DW_AT_byte_size, size)))
15916 size *= 8;
15917 else
15918 has_size_info = die_unsigned_constant_attribute(die,
15919 DW_AT_bit_size, size);
15920
15921 translation_unit::language language = rdr.cur_transl_unit()->get_language();
15922 array_type_def::subrange_type::bound_value lower_bound =
15923 get_default_array_lower_bound(language);
15924 array_type_def::subrange_type::bound_value upper_bound;
15925 uint64_t count = 0;
15926 bool is_non_finite = false;
15927 bool non_zero_count_present = false;
15928
15929 // The DWARF 4 specifications says, in [5.11 Subrange
15930 // Type Entries]:
15931 //
15932 // The subrange entry may have the attributes
15933 // DW_AT_lower_bound and DW_AT_upper_bound to
15934 // specify, respectively, the lower and upper bound
15935 // values of the subrange.
15936 //
15937 // So let's look for DW_AT_lower_bound first.
15938 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
15939
15940 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
15941 is_signed, upper_bound);
15942 if (!found_upper_bound)
15943 found_upper_bound = subrange_die_indirect_bound_value(die,
15944 DW_AT_upper_bound,
15945 upper_bound,
15946 is_signed);
15947 // Then, DW_AT_upper_bound.
15948 if (!found_upper_bound)
15949 {
15950 // The DWARF 4 spec says, in [5.11 Subrange Type
15951 // Entries]:
15952 //
15953 // The DW_AT_upper_bound attribute may be replaced
15954 // by a DW_AT_count attribute, whose value
15955 // describes the number of elements in the
15956 // subrange rather than the value of the last
15957 // element."
15958 //
15959 // So, as DW_AT_upper_bound is not present in this
15960 // case, let's see if there is a DW_AT_count.
15961 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
15962 {
15963 if (count)
15964 // DW_AT_count can be present and be set to zero. This is
15965 // for instance the case to model this gcc extension to
15966 // represent flexible arrays:
15967 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
15968 // For instance: int flex_array[0];
15969 non_zero_count_present = true;
15970
15971 // When the count is present and non-zero, we can deduce the
15972 // upper_bound from the lower_bound and the number of
15973 // elements of the array:
15974 int64_t u = lower_bound.get_signed_value() + count;
15975 if (u)
15976 upper_bound = u - 1;
15977 }
15978
15979 if (!non_zero_count_present)
15980 // No upper_bound nor count was present on the DIE, this means
15981 // the array is considered to have an infinite (or rather not
15982 // known) size.
15983 is_non_finite = true;
15984 }
15985
15986 if (UINT64_MAX == upper_bound.get_unsigned_value())
15987 // If the upper_bound size is the max of the integer value
15988 // then it most certainly means unknown size.
15989 is_non_finite = true;
15990
15991 result.reset
15992 (new array_type_def::subrange_type(rdr.env(),
15993 name,
15994 lower_bound,
15995 upper_bound,
15996 underlying_type,
15997 location()));
15998 result->is_non_finite(is_non_finite);
15999
16000 if (has_size_info)
16001 result->set_size_in_bits(size);
16002 else
16003 {
16004 // The DW_TAG_subrange_type doesn't appear to have any size
16005 // attribute. In that case, the size is deduced from the size
16006 // of the underlying type. If there is no underlying type
16007 // specified, then the size of the subrange type is the size
16008 if (!underlying_type)
16009 result->set_size_in_bits(rdr.cur_transl_unit()->get_address_size());
16010 }
16011
16012 // Let's ensure the resulting subrange looks metabolically healthy.
16013 ABG_ASSERT(result->is_non_finite()
16014 || (result->get_length() ==
16015 (uint64_t) (result->get_upper_bound()
16016 - result->get_lower_bound() + 1)));
16017
16018 if (associate_type_to_die)
16019 rdr.associate_die_to_type(die, result, where_offset);
16020
16021 return result;
16022}
16023
16024/// Build the sub-ranges of an array type.
16025///
16026/// This is a sub-routine of build_array_type().
16027///
16028/// @param rdr the context to read from.
16029///
16030/// @param die the DIE of tag DW_TAG_array_type which contains
16031/// children DIEs that represent the sub-ranges.
16032///
16033/// @param subranges out parameter. This is set to the sub-ranges
16034/// that are built from @p die.
16035///
16036/// @param where_offset the offset of the DIE where we are "logically"
16037/// positioned at, in the DIE tree. This is useful when @p die is
16038/// e.g, DW_TAG_partial_unit that can be included in several places in
16039/// the DIE tree.
16040static void
16041build_subranges_from_array_type_die(const reader& rdr,
16042 const Dwarf_Die* die,
16044 size_t where_offset,
16045 bool associate_type_to_die)
16046{
16047 Dwarf_Die child;
16048
16049 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
16050 {
16051 do
16052 {
16053 int child_tag = dwarf_tag(&child);
16054 if (child_tag == DW_TAG_subrange_type)
16055 {
16057 if (associate_type_to_die)
16058 {
16059 // We are being called to create the type, add it to
16060 // the current type graph and associate it to the
16061 // DIE it's been created from.
16063 build_ir_node_from_die(const_cast<reader&>(rdr), &child,
16064 /*called_from_public_decl=*/true,
16065 where_offset);
16066 s = is_subrange_type(t);
16067 }
16068 else
16069 // We are being called to create the type but *NOT*
16070 // add it to the current tyupe tree, *NOR* associate
16071 // it to the DIE it's been created from.
16072 s = build_subrange_type(const_cast<reader&>(rdr), &child,
16073 where_offset,
16074 /*associate_type_to_die=*/false);
16075 if (s)
16076 subranges.push_back(s);
16077 }
16078 }
16079 while (dwarf_siblingof(&child, &child) == 0);
16080 }
16081}
16082
16083/// Build an array type from a DW_TAG_array_type DIE.
16084///
16085/// @param rdr the DWARF reader to consider.
16086///
16087/// @param die the DIE to read from.
16088///
16089/// @param called_from_public_decl true if this function was called
16090/// from a context where either a public function or a public variable
16091/// is being built.
16092///
16093/// @param where_offset the offset of the DIE where we are "logically"
16094/// positioned at, in the DIE tree. This is useful when @p die is
16095/// e.g, DW_TAG_partial_unit that can be included in several places in
16096/// the DIE tree.
16097///
16098/// @return a pointer to the resulting array_type_def.
16100build_array_type(reader& rdr,
16101 Dwarf_Die* die,
16102 bool called_from_public_decl,
16103 size_t where_offset)
16104{
16105 array_type_def_sptr result;
16106
16107 if (!die)
16108 return result;
16109
16110 unsigned tag = dwarf_tag(die);
16111 if (tag != DW_TAG_array_type)
16112 return result;
16113
16114 decl_base_sptr type_decl;
16115 Dwarf_Die type_die;
16116
16117 if (die_die_attribute(die, DW_AT_type, type_die))
16118 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
16119 called_from_public_decl,
16120 where_offset));
16121 if (!type_decl)
16122 return result;
16123
16124 // The call to build_ir_node_from_die() could have triggered the
16125 // creation of the type for this DIE. In that case, just return it.
16126 if (type_base_sptr t = rdr.lookup_type_from_die(die))
16127 {
16128 result = is_array_type(t);
16129 ABG_ASSERT(result);
16130 return result;
16131 }
16132
16133 type_base_sptr type = is_type(type_decl);
16134 ABG_ASSERT(type);
16135
16137
16138 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
16139
16140 result.reset(new array_type_def(type, subranges, location()));
16141 rdr.associate_die_to_type(die, result, where_offset);
16142 return result;
16143}
16144
16145/// Create a typedef_decl from a DW_TAG_typedef DIE.
16146///
16147/// @param rdr the DWARF reader to consider.
16148///
16149/// @param die the DIE to read from.
16150///
16151/// @param called_from_public_decl true if this function was called
16152/// from a context where either a public function or a public variable
16153/// is being built.
16154///
16155/// @param where_offset the offset of the DIE where we are "logically"
16156/// positionned at, in the DIE tree. This is useful when @p die is
16157/// e.g, DW_TAG_partial_unit that can be included in several places in
16158/// the DIE tree.
16159///
16160/// @return the newly created typedef_decl.
16161static typedef_decl_sptr
16162build_typedef_type(reader& rdr,
16163 Dwarf_Die* die,
16164 bool called_from_public_decl,
16165 size_t where_offset)
16166{
16167 typedef_decl_sptr result;
16168
16169 if (!die)
16170 return result;
16171
16172 unsigned tag = dwarf_tag(die);
16173 if (tag != DW_TAG_typedef)
16174 return result;
16175
16176 string name, linkage_name;
16177 location loc;
16178 die_loc_and_name(rdr, die, loc, name, linkage_name);
16179
16180 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
16181 if (loc)
16182 result = lookup_typedef_type_per_location(loc.expand(), *corp);
16183
16184 if (!result)
16185 {
16186 type_base_sptr utype;
16187 Dwarf_Die underlying_type_die;
16188 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
16189 // A typedef DIE with no underlying type means a typedef to
16190 // void type.
16191 utype = rdr.env().get_void_type();
16192
16193 if (!utype)
16194 utype =
16195 is_type(build_ir_node_from_die(rdr,
16196 &underlying_type_die,
16197 called_from_public_decl,
16198 where_offset));
16199 if (!utype)
16200 return result;
16201
16202 ABG_ASSERT(utype);
16203 result.reset(new typedef_decl(name, utype, loc, linkage_name));
16204
16205 if ((is_class_or_union_type(utype) || is_enum_type(utype))
16206 && is_anonymous_type(utype))
16207 {
16208 // This is a naming typedef for an enum or a class. Let's
16209 // mark the underlying decl as such.
16210 decl_base_sptr decl = is_decl(utype);
16211 ABG_ASSERT(decl);
16212 decl->set_naming_typedef(result);
16213 rdr.maybe_schedule_decl_only_type_for_resolution(utype);
16214 }
16215 }
16216
16217 rdr.associate_die_to_type(die, result, where_offset);
16218
16219 return result;
16220}
16221
16222/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
16223/// denoted by the DIE is not suppressed by a suppression
16224/// specification associated to the current DWARF reader.
16225///
16226/// Note that if a member variable declaration with the same name as
16227/// the name of the DIE we are looking at exists, this function returns
16228/// that existing variable declaration.
16229///
16230/// @param rdr the DWARF reader to use.
16231///
16232/// @param die the DIE representing the variable we are looking at.
16233///
16234/// @param where_offset the offset of the DIE where we are "logically"
16235/// positionned at, in the DIE tree. This is useful when @p die is
16236/// e.g, DW_TAG_partial_unit that can be included in several places in
16237/// the DIE tree.
16238///
16239/// @param is_declaration_only if true, it means the variable DIE has
16240/// the is_declaration_only only attribute.
16241///
16242/// @param result if this is set to an existing var_decl, this means
16243/// that the function will append the new properties it sees on @p die
16244/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16245/// new var_decl is going to be allocated and returned.
16246///
16247/// @param is_required_decl_spec this is true iff the variable to
16248/// build is referred to as being the specification of another
16249/// variable.
16250///
16251/// @return a pointer to the newly created var_decl. If the var_decl
16252/// could not be built, this function returns NULL.
16253static var_decl_sptr
16254build_or_get_var_decl_if_not_suppressed(reader& rdr,
16255 scope_decl *scope,
16256 Dwarf_Die *die,
16257 size_t where_offset,
16258 bool is_declaration_only,
16259 var_decl_sptr result,
16260 bool is_required_decl_spec)
16261{
16262 var_decl_sptr var;
16263 if (variable_is_suppressed(rdr, scope, die,
16264 is_declaration_only,
16265 is_required_decl_spec))
16266 {
16267 ++rdr.stats_.number_of_suppressed_variables;
16268 return var;
16269 }
16270
16271 if (class_decl* class_type = is_class_type(scope))
16272 {
16273 string var_name = die_name(die);
16274 if (!var_name.empty())
16275 if ((var = class_type->find_data_member(var_name)))
16276 return var;
16277 }
16278
16279 // The variable was not suppressed.
16280 ++rdr.stats_.number_of_suppressed_variables;
16281
16282 var = build_var_decl(rdr, die, where_offset, result);
16283 return var;
16284}
16285
16286/// Build a @ref var_decl out of a DW_TAG_variable DIE.
16287///
16288/// @param rdr the DWARF reader to use.
16289///
16290/// @param die the DIE representing the variable we are looking at.
16291///
16292/// @param where_offset the offset of the DIE where we are "logically"
16293/// positionned at, in the DIE tree. This is useful when @p die is
16294/// e.g, DW_TAG_partial_unit that can be included in several places in
16295/// the DIE tree.
16296///
16297/// @param result if this is set to an existing var_decl, this means
16298/// that the function will append the new properties it sees on @p die
16299/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16300/// new var_decl is going to be allocated and returned.
16301///
16302/// @return a pointer to the newly created var_decl. If the var_decl
16303/// could not be built, this function returns NULL.
16304static var_decl_sptr
16305build_var_decl(reader& rdr,
16306 Dwarf_Die *die,
16307 size_t where_offset,
16308 var_decl_sptr result)
16309{
16310 if (!die)
16311 return result;
16312
16313 int tag = dwarf_tag(die);
16314 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
16315
16316 if (!die_is_public_decl(die))
16317 return result;
16318
16319 type_base_sptr type;
16320 Dwarf_Die type_die;
16321 if (die_die_attribute(die, DW_AT_type, type_die))
16322 {
16323 decl_base_sptr ty =
16324 is_decl(build_ir_node_from_die(rdr, &type_die,
16325 /*called_from_public_decl=*/true,
16326 where_offset));
16327 if (!ty)
16328 return result;
16329 type = is_type(ty);
16330 ABG_ASSERT(type);
16331 }
16332
16333 if (!type && !result)
16334 return result;
16335
16336 string name, linkage_name;
16337 location loc;
16338 die_loc_and_name(rdr, die, loc, name, linkage_name);
16339
16340 if (!result)
16341 result.reset(new var_decl(name, type, loc, linkage_name));
16342 else
16343 {
16344 // We were called to append properties that might have been
16345 // missing from the first version of the variable. And usually
16346 // that missing property is the mangled name or the type.
16347 if (!linkage_name.empty())
16348 result->set_linkage_name(linkage_name);
16349
16350 if (type)
16351 result->set_type(type);
16352 }
16353
16354 // Check if a variable symbol with this name is exported by the elf
16355 // binary. If it is, then set the symbol of the variable, if it's
16356 // not set already.
16357 if (!result->get_symbol())
16358 {
16359 elf_symbol_sptr var_sym;
16360 Dwarf_Addr var_addr;
16361
16362 if (rdr.get_variable_address(die, var_addr))
16363 {
16364 rdr.symtab()->
16365 update_main_symbol(var_addr,
16366 result->get_linkage_name().empty()
16367 ? result->get_name()
16368 : result->get_linkage_name());
16369 var_sym = rdr.variable_symbol_is_exported(var_addr);
16370 }
16371
16372 if (var_sym)
16373 {
16374 result->set_symbol(var_sym);
16375 // If the linkage name is not set or is wrong, set it to
16376 // the name of the underlying symbol.
16377 string linkage_name = result->get_linkage_name();
16378 if (linkage_name.empty()
16379 || !var_sym->get_alias_from_name(linkage_name))
16380 result->set_linkage_name(var_sym->get_name());
16381 result->set_is_in_public_symbol_table(true);
16382 }
16383
16384 if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
16385 {
16386 // We are looking at a global variable which symbol is
16387 // undefined. Let's set its symbol.
16388 string n = result->get_linkage_name();
16389 if (n.empty())
16390 n = result->get_name();
16391 var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
16392 if (var_sym)
16393 {
16394 result->set_symbol(var_sym);
16395 result->set_is_in_public_symbol_table(false);
16396 }
16397 }
16398 }
16399
16400 return result;
16401}
16402
16403/// Test if a given function denoted by its DIE and its scope is
16404/// suppressed by any of the suppression specifications associated to
16405/// a given context of ELF/DWARF reading.
16406///
16407/// Note that a non-member function which symbol is not exported is
16408/// also suppressed.
16409///
16410/// @param rdr the ELF/DWARF reading content of interest.
16411///
16412/// @param scope of the scope of the function.
16413///
16414/// @param function_die the DIE representing the function.
16415///
16416/// @param is_declaration_only is true if the DIE denoted by @p die is
16417/// a declaration-only DIE.
16418///
16419/// @return true iff @p function_die is suppressed by at least one
16420/// suppression specification attached to the @p rdr.
16421static bool
16422function_is_suppressed(const reader& rdr,
16423 const scope_decl* scope,
16424 Dwarf_Die *function_die,
16425 bool is_declaration_only)
16426{
16427 if (function_die == 0
16428 || dwarf_tag(function_die) != DW_TAG_subprogram)
16429 return false;
16430
16431 string fname = die_string_attribute(function_die, DW_AT_name);
16432 string flinkage_name = die_linkage_name(function_die);
16433 if (flinkage_name.empty() && die_is_in_c(function_die))
16434 flinkage_name = fname;
16435 string qualified_name = build_qualified_name(scope, fname);
16436
16437 // A non-member non-static function which symbol is not exported is
16438 // suppressed.
16439 //
16440 // Note that if the non-member non-static function has an undefined
16441 // symbol, by default, it's not suppressed. Unless we are asked to
16442 // drop undefined symbols too.
16443 if (!is_class_type(scope)
16444 && (!is_declaration_only || rdr.drop_undefined_syms()))
16445 {
16446 Dwarf_Addr fn_addr;
16447 if (!rdr.get_function_address(function_die, fn_addr))
16448 return true;
16449
16450 elf_symbol_sptr symbol =
16451 rdr.function_symbol_is_exported(fn_addr);
16452 if (!symbol)
16453 return true;
16454 if (symbol->is_suppressed())
16455 return true;
16456
16457 // Since there is only one symbol in DWARF associated with an elf_symbol,
16458 // we can assume this is the main symbol then. Otherwise the main hinting
16459 // did not work as expected.
16460 ABG_ASSERT(symbol->is_main_symbol());
16461 if (symbol->has_aliases())
16462 for (elf_symbol_sptr a = symbol->get_next_alias();
16463 !a->is_main_symbol(); a = a->get_next_alias())
16464 if (a->is_suppressed())
16465 return true;
16466 }
16467
16468 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
16469 /*require_drop_property=*/true);
16470}
16471
16472/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
16473/// function denoted by the DIE is not suppressed by a suppression
16474/// specification associated to the current DWARF reader.
16475///
16476/// Note that if a member function declaration with the same signature
16477/// (pretty representation) as one of the DIE we are looking at
16478/// exists, this function returns that existing function declaration.
16479/// Similarly, if there is already a constructed member function with
16480/// the same linkage name as the one on the DIE, this function returns
16481/// that member function.
16482///
16483/// Also note that the function_decl IR returned by this function must
16484/// be passed to finish_member_function_reading because several
16485/// properties from the DIE are actually read by that function, and
16486/// the corresponding properties on the function_decl IR are updated
16487/// accordingly. This is done to support "updating" a function_decl
16488/// IR with properties scathered across several DIEs.
16489///
16490/// @param rdr the DWARF reader to use.
16491///
16492/// @param scope the scope of the function we are looking at.
16493///
16494/// @param fn_die the DIE representing the function we are looking at.
16495///
16496/// @param where_offset the offset of the DIE where we are "logically"
16497/// positionned at, in the DIE tree. This is useful when @p die is
16498/// e.g, DW_TAG_partial_unit that can be included in several places in
16499/// the DIE tree.
16500///
16501/// @param is_declaration_only is true if the DIE denoted by @p fn_die
16502/// is a declaration-only DIE.
16503///
16504/// @param result if this is set to an existing function_decl, this
16505/// means that the function will append the new properties it sees on
16506/// @p fn_die to that exising function_decl. Otherwise, if this
16507/// parameter is NULL, a new function_decl is going to be allocated
16508/// and returned.
16509///
16510/// @return a pointer to the newly created var_decl. If the var_decl
16511/// could not be built, this function returns NULL.
16512static function_decl_sptr
16513build_or_get_fn_decl_if_not_suppressed(reader& rdr,
16514 scope_decl *scope,
16515 Dwarf_Die *fn_die,
16516 size_t where_offset,
16517 bool is_declaration_only,
16518 function_decl_sptr result)
16519{
16521 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
16522 {
16523 ++rdr.stats_.number_of_suppressed_functions;
16524 return fn;
16525 }
16526
16527 string name = die_name(fn_die);
16528 string linkage_name = die_linkage_name(fn_die);
16529 bool is_dtor = !name.empty() && name[0]== '~';
16530 bool is_virtual = false;
16531 if (is_dtor)
16532 {
16533 Dwarf_Attribute attr;
16534 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
16535 DW_AT_vtable_elem_location,
16536 &attr))
16537 is_virtual = true;
16538 }
16539
16540
16541 // If we've already built an IR for a function with the same
16542 // signature (from another DIE), reuse it, unless that function is a
16543 // virtual C++ destructor. Several virtual C++ destructors with the
16544 // same signature can be implemented by several different ELF
16545 // symbols. So re-using C++ destructors like that can lead to us
16546 // missing some destructors.
16547 if (!result && (!(is_dtor && is_virtual)))
16548 {
16549 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
16550 {
16551 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
16552 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
16553 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
16554 return fn;
16555 }
16556 }
16557
16558 // The function was not suppressed.
16559 ++rdr.stats_.number_of_allowed_functions;
16560
16561 // If a member function with the same linkage name as the one
16562 // carried by the DIE already exists, then return it.
16563 if (class_decl* klass = is_class_type(scope))
16564 {
16565 string linkage_name = die_linkage_name(fn_die);
16566 fn = klass->find_member_function_sptr(linkage_name);
16567 if (fn)
16568 // We found a member function that has the same signature.
16569 // Let's mark it for update.
16570 result = fn;
16571 }
16572
16573 if (!fn || !fn->get_symbol())
16574 // We haven't yet been able to construct a function IR, or, we
16575 // have one 'partial' function IR that doesn't have any associated
16576 // symbol yet. Note that in the later case, a function IR without
16577 // any associated symbol will be dropped on the floor by
16578 // potential_member_fn_should_be_dropped. So let's build or a new
16579 // function IR or complete the existing partial IR.
16580 fn = build_function_decl(rdr, fn_die, where_offset, result);
16581
16582 return fn;
16583}
16584
16585/// Test if a given variable denoted by its DIE and its scope is
16586/// suppressed by any of the suppression specifications associated to
16587/// a given context of ELF/DWARF reading.
16588///
16589/// @param rdr the ELF/DWARF reading content of interest.
16590///
16591/// @param scope of the scope of the variable.
16592///
16593/// @param variable_die the DIE representing the variable.
16594///
16595/// @param is_declaration_only true if the variable is supposed to be
16596/// decl-only.
16597///
16598/// @param is_required_decl_spec if true, means that the @p
16599/// variable_die being considered is for a variable decl that is a
16600/// specification for a concrete variable being built.
16601///
16602/// @return true iff @p variable_die is suppressed by at least one
16603/// suppression specification attached to the @p rdr.
16604static bool
16605variable_is_suppressed(const reader& rdr,
16606 const scope_decl* scope,
16607 Dwarf_Die *variable_die,
16608 bool is_declaration_only,
16609 bool is_required_decl_spec)
16610{
16611 if (variable_die == 0
16612 || (dwarf_tag(variable_die) != DW_TAG_variable
16613 && dwarf_tag(variable_die) != DW_TAG_member))
16614 return false;
16615
16616 string name = die_string_attribute(variable_die, DW_AT_name);
16617 string linkage_name = die_linkage_name(variable_die);
16618 if (linkage_name.empty() && die_is_in_c(variable_die))
16619 linkage_name = name;
16620 string qualified_name = build_qualified_name(scope, name);
16621
16622 // If a non member variable that is a declaration (has no defined
16623 // and exported symbol) and is not the specification of another
16624 // concrete variable, then it's suppressed. This is a size
16625 // optimization; it removes useless declaration-only variables from
16626 // the IR.
16627 if (!is_class_type(scope)
16628 && !is_required_decl_spec
16629 // If we are asked to load undefined interfaces, then we don't
16630 // suppress declaration-only variables as they might have
16631 // undefined elf-symbols.
16632 && (!is_declaration_only || !rdr.load_undefined_interfaces()))
16633 {
16634 Dwarf_Addr var_addr = 0;
16635 if (!rdr.get_variable_address(variable_die, var_addr))
16636 return true;
16637
16638 elf_symbol_sptr symbol =
16639 rdr.variable_symbol_is_exported(var_addr);
16640 if (!symbol)
16641 return true;
16642 if (symbol->is_suppressed())
16643 return true;
16644
16645 // Since there is only one symbol in DWARF associated with an elf_symbol,
16646 // we can assume this is the main symbol then. Otherwise the main hinting
16647 // did not work as expected.
16648 ABG_ASSERT(symbol->is_main_symbol());
16649 if (symbol->has_aliases())
16650 for (elf_symbol_sptr a = symbol->get_next_alias();
16651 !a->is_main_symbol(); a = a->get_next_alias())
16652 if (a->is_suppressed())
16653 return true;
16654 }
16655
16657 qualified_name,
16658 linkage_name,
16659 /*require_drop_property=*/true);
16660}
16661
16662/// Test if a type (designated by a given DIE) in a given scope is
16663/// suppressed by the suppression specifications that are associated
16664/// to a given DWARF reader.
16665///
16666/// @param rdr the DWARF reader to consider.
16667///
16668/// @param scope of the scope of the type DIE to consider.
16669///
16670/// @param type_die the DIE that designates the type to consider.
16671///
16672/// @param type_is_opaque out parameter. If this function returns
16673/// true (the type @p type_die is suppressed) and if the type was
16674/// suppressed because it's opaque then this parameter is set to
16675/// true.
16676///
16677/// @return true iff the type designated by the DIE @p type_die, in
16678/// the scope @p scope is suppressed by at the suppression
16679/// specifications associated to the current DWARF reader.
16680static bool
16681type_is_suppressed(const reader& rdr,
16682 const scope_decl* scope,
16683 Dwarf_Die *type_die,
16684 bool &type_is_opaque)
16685{
16686 if (type_die == 0
16687 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
16688 && dwarf_tag(type_die) != DW_TAG_class_type
16689 && dwarf_tag(type_die) != DW_TAG_structure_type
16690 && dwarf_tag(type_die) != DW_TAG_union_type))
16691 return false;
16692
16693 string type_name, linkage_name;
16694 location type_location;
16695 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16696 string qualified_name = build_qualified_name(scope, type_name);
16697
16698 return suppr::is_type_suppressed(rdr,
16699 qualified_name,
16700 type_location,
16701 type_is_opaque,
16702 /*require_drop_property=*/true);
16703}
16704
16705/// Test if a type (designated by a given DIE) in a given scope is
16706/// suppressed by the suppression specifications that are associated
16707/// to a given DWARF reader.
16708///
16709/// @param rdr the DWARF reader to consider.
16710///
16711/// @param scope of the scope of the type DIE to consider.
16712///
16713/// @param type_die the DIE that designates the type to consider.
16714///
16715/// @return true iff the type designated by the DIE @p type_die, in
16716/// the scope @p scope is suppressed by at the suppression
16717/// specifications associated to the current DWARF reader.
16718static bool
16719type_is_suppressed(const reader& rdr,
16720 const scope_decl* scope,
16721 Dwarf_Die *type_die)
16722{
16723 bool type_is_opaque = false;
16724 return type_is_suppressed(rdr, scope, type_die, type_is_opaque);
16725}
16726
16727/// Get the opaque version of a type that was suppressed because it's
16728/// a private type.
16729///
16730/// The opaque version version of the type is just a declared-only
16731/// version of the type (class, union or enum type) denoted by @p
16732/// type_die.
16733///
16734/// @param rdr the DWARF reader in use.
16735///
16736/// @param scope the scope of the type die we are looking at.
16737///
16738/// @param type_die the type DIE we are looking at.
16739///
16740/// @param where_offset the offset of the DIE where we are "logically"
16741/// positionned at, in the DIE tree. This is useful when @p die is
16742/// e.g, DW_TAG_partial_unit that can be included in several places in
16743/// the DIE tree.
16744///
16745/// @return the opaque version of the type denoted by @p type_die or
16746/// nil if no opaque version was found.
16748get_opaque_version_of_type(reader &rdr,
16749 scope_decl *scope,
16750 Dwarf_Die *type_die,
16751 size_t where_offset)
16752{
16754
16755 if (type_die == 0)
16756 return result;
16757
16758 unsigned tag = dwarf_tag(type_die);
16759 if (tag != DW_TAG_class_type
16760 && tag != DW_TAG_structure_type
16761 && tag != DW_TAG_union_type
16762 && tag != DW_TAG_enumeration_type)
16763 return result;
16764
16765 string type_name, linkage_name;
16766 location type_location;
16767 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16768
16769 string qualified_name = build_qualified_name(scope, type_name);
16770
16771 //
16772 // TODO: also handle declaration-only unions. To do that, we mostly
16773 // need to adapt add_or_update_union_type to make it schedule
16774 // declaration-only unions for resolution too.
16775 //
16776 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
16777 {
16778 string_classes_or_unions_map::const_iterator i =
16779 rdr.declaration_only_classes().find(qualified_name);
16780 if (i != rdr.declaration_only_classes().end())
16781 result = i->second.back();
16782
16783 if (!result)
16784 {
16785 // So we didn't find any pre-existing forward-declared-only
16786 // class for the class definition that we could return as an
16787 // opaque type. So let's build one.
16788 //
16789 // TODO: we need to be able to do this for unions too!
16790 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
16791 /*alignment=*/0, /*size=*/0,
16792 tag == DW_TAG_structure_type,
16793 type_location,
16794 decl_base::VISIBILITY_DEFAULT));
16795 klass->set_is_declaration_only(true);
16796 klass->set_is_artificial(die_is_artificial(type_die));
16797 add_decl_to_scope(klass, scope);
16798 rdr.associate_die_to_type(type_die, klass, where_offset);
16799 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
16800 result = klass;
16801 }
16802 }
16803
16804 if (tag == DW_TAG_enumeration_type)
16805 {
16806 string_enums_map::const_iterator i =
16807 rdr.declaration_only_enums().find(qualified_name);
16808 if (i != rdr.declaration_only_enums().end())
16809 result = i->second.back();
16810
16811 if (!result)
16812 {
16813 uint64_t size = 0;
16814 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
16815 size *= 8;
16816 type_decl_sptr underlying_type =
16817 build_enum_underlying_type(rdr, type_name, size,
16818 /*anonymous=*/true);
16819 enum_type_decl::enumerators enumeratorz;
16820 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
16821 type_location,
16822 underlying_type,
16823 enumeratorz,
16824 linkage_name));
16825 enum_type->set_is_artificial(die_is_artificial(type_die));
16826 add_decl_to_scope(enum_type, scope);
16827 result = enum_type;
16828 }
16829 }
16830
16831 return result;
16832}
16833
16834/// Create a function symbol with a given name.
16835///
16836/// @param sym_name the name of the symbol to create.
16837///
16838/// @param env the environment to create the symbol in.
16839///
16840/// @return the newly created symbol.
16842create_default_fn_sym(const string& sym_name, const environment& env)
16843{
16845 elf_symbol_sptr result =
16847 /*symbol index=*/ 0,
16848 /*symbol size=*/ 0,
16849 sym_name,
16850 /*symbol type=*/ elf_symbol::FUNC_TYPE,
16851 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
16852 /*symbol is defined=*/ true,
16853 /*symbol is common=*/ false,
16854 /*symbol version=*/ ver,
16855 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
16856 return result;
16857}
16858
16859/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
16860///
16861/// @param rdr the DWARF reader to use
16862///
16863/// @param die the DW_TAG_subprogram DIE to read from.
16864///
16865/// @param where_offset the offset of the DIE where we are "logically"
16866/// positionned at, in the DIE tree. This is useful when @p die is
16867/// e.g, DW_TAG_partial_unit that can be included in several places in
16868/// the DIE tree.
16869///
16870/// @param called_for_public_decl this is set to true if the function
16871/// was called for a public (function) decl.
16872static function_decl_sptr
16873build_function_decl(reader& rdr,
16874 Dwarf_Die* die,
16875 size_t where_offset,
16877{
16878 function_decl_sptr result = fn;
16879 if (!die)
16880 return result;
16881 int tag = dwarf_tag(die);
16882 ABG_ASSERT(tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine);
16883
16884 if (!die_is_public_decl(die))
16885 return result;
16886
16887 translation_unit_sptr tu = rdr.cur_transl_unit();
16888 ABG_ASSERT(tu);
16889
16890 string fname, flinkage_name;
16891 location floc;
16892 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
16893 cleanup_decl_name(fname);
16894
16895 size_t is_inline = die_is_declared_inline(die);
16896 class_or_union_sptr is_method =
16897 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
16898
16899 if (result)
16900 {
16901 // Add the properties that might have been missing from the
16902 // first declaration of the function. For now, it usually is
16903 // the mangled name that goes missing in the first declarations.
16904 //
16905 // Also note that if 'fn' has just been cloned, the current
16906 // linkage name (of the current DIE) might be different from the
16907 // linkage name of 'fn'. In that case, update the linkage name
16908 // of 'fn' too.
16909 if (!flinkage_name.empty()
16910 && result->get_linkage_name() != flinkage_name)
16911 result->set_linkage_name(flinkage_name);
16912 if (floc)
16913 if (!result->get_location())
16914 result->set_location(floc);
16915 result->is_declared_inline(is_inline);
16916 }
16917 else
16918 {
16919 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
16920 where_offset));
16921 if (!fn_type)
16922 return result;
16923
16924 maybe_canonicalize_type(fn_type, rdr);
16925
16926 result.reset(is_method
16927 ? new method_decl(fname, fn_type,
16928 is_inline, floc,
16929 flinkage_name)
16930 : new function_decl(fname, fn_type,
16931 is_inline, floc,
16932 flinkage_name));
16933 }
16934
16935 // Set the symbol of the function. If the linkage name is not set
16936 // or is wrong, set it to the name of the underlying symbol.
16937 if (!result->get_symbol())
16938 {
16939 elf_symbol_sptr fn_sym;
16940 Dwarf_Addr fn_addr;
16941 if (rdr.get_function_address(die, fn_addr))
16942 {
16943 rdr.symtab()->
16944 update_main_symbol(fn_addr,
16945 result->get_linkage_name().empty()
16946 ? result->get_name()
16947 : result->get_linkage_name());
16948 fn_sym = rdr.function_symbol_is_exported(fn_addr);
16949 }
16950
16951 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
16952 {
16953 result->set_symbol(fn_sym);
16954 string linkage_name = result->get_linkage_name();
16955 if (linkage_name.empty())
16956 result->set_linkage_name(fn_sym->get_name());
16957 result->set_is_in_public_symbol_table(true);
16958 }
16959
16960 if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
16961 {
16962 // We are looking at a function which symbol is undefined.
16963 // let's set its symbol.
16964 string n = result->get_linkage_name();
16965 if (n.empty())
16966 n = result->get_name();
16967 fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
16968 if (fn_sym)
16969 {
16970 result->set_symbol(fn_sym);
16971 result->set_is_in_public_symbol_table(false);
16972 }
16973 }
16974 }
16975
16976 rdr.associate_die_to_type(die, result->get_type(), where_offset);
16977
16978 size_t die_offset = dwarf_dieoffset(die);
16979
16980 if (fn
16981 && is_member_function(fn)
16983 && !result->get_linkage_name().empty())
16984 // This function is a virtual member function which has its
16985 // linkage name *and* and has its underlying symbol correctly set.
16986 // It thus doesn't need any fixup related to elf symbol. So
16987 // remove it from the set of virtual member functions with linkage
16988 // names and no elf symbol that need to be fixed up.
16989 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
16990 return result;
16991}
16992
16993/// Canonicalize a type if it's suitable for early canonicalizing, or,
16994/// if it's not, schedule it for late canonicalization, after the
16995/// debug info of the current translation unit has been fully read.
16996///
16997/// A (composite) type is deemed suitable for early canonicalizing iff
16998/// all of its sub-types are canonicalized themselve. Non composite
16999/// types are always deemed suitable for early canonicalization.
17000///
17001/// Note that this function knows how to deal with anonymous classes,
17002/// structs and enums, unlike the overload below:
17003///
17004/// @param t the type DIE to consider for canonicalization.
17005///
17006/// @param rdr the @ref reader to use.
17007static void
17008maybe_canonicalize_type(const type_base_sptr& t,
17009 reader& rdr)
17010{
17011 if (!t)
17012 return;
17013
17014 rdr.schedule_type_for_late_canonicalization(t);
17015}
17016
17017/// If a given decl is a member type declaration, set its access
17018/// specifier from the DIE that represents it.
17019///
17020/// @param member_type_declaration the member type declaration to
17021/// consider.
17022static void
17023maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
17024 Dwarf_Die* die)
17025{
17026 if (is_type(member_type_declaration)
17027 && is_member_decl(member_type_declaration))
17028 {
17029 class_or_union* scope =
17030 is_class_or_union_type(member_type_declaration->get_scope());
17031 ABG_ASSERT(scope);
17032
17033 access_specifier access = public_access;
17034 if (class_decl* cl = is_class_type(scope))
17035 if (!cl->is_struct())
17036 access = private_access;
17037
17038 die_access_specifier(die, access);
17039 set_member_access_specifier(member_type_declaration, access);
17040 }
17041}
17042
17043/// Normalize a decl name so that it can be compared to other decl
17044/// names without risking to have spurious changes.
17045///
17046/// The function removes white spaces from the and normalizes
17047/// numerical litterals.
17048///
17049/// @param str in/out parameter. The string to normalize, in place.
17050static void
17051cleanup_decl_name(string& str)
17052{
17055}
17056
17057/// This function tests if a given function which might be intented to
17058/// be added to a class scope (to become a member function) should be
17059/// dropped on the floor instead and not be added to the class.
17060///
17061/// This is a subroutine of build_ir_node_from_die.
17062///
17063/// @param fn the function to consider.
17064///
17065/// @param fn_die the DWARF die of @p fn.
17066///
17067/// @param scope the scope in which @p fn is to be added.
17068///
17069/// @return true iff @p fn should be dropped on the floor.
17070static bool
17071potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
17072 const Dwarf_Die *fn_die)
17073{
17074 if (!fn || fn->get_scope())
17075 return false;
17076
17077 if (// A function that is not virtual ...
17078 !die_is_virtual(fn_die)
17079 // .. and yet has no defined ELF symbol associated ...
17080 && !fn->get_symbol())
17081 // Should not be added to its class scope.
17082 //
17083 // Why would it? It's not part of the ABI anyway, as it doesn't
17084 // have any ELF symbol associated and is not a virtual member
17085 // function. It just constitutes bloat in the IR and might even
17086 // induce spurious change reports down the road.
17087 return true;
17088
17089 return false;
17090}
17091
17092/// Build an IR node from a given DIE and add the node to the current
17093/// IR being build and held in the DWARF reader. Doing that is called
17094/// "emitting an IR node for the DIE".
17095///
17096/// @param rdr the DWARF reader.
17097///
17098/// @param die the DIE to consider.
17099///
17100/// @param scope the scope under which the resulting IR node has to be
17101/// added.
17102///
17103/// @param called_from_public_decl set to yes if this function is
17104/// called from the functions used to build a public decl (functions
17105/// and variables). In that case, this function accepts building IR
17106/// nodes representing types. Otherwise, this function only creates
17107/// IR nodes representing public decls (functions and variables).
17108/// This is done to avoid emitting IR nodes for types that are not
17109/// referenced by public functions or variables.
17110///
17111/// @param where_offset the offset of the DIE where we are "logically"
17112/// positionned at, in the DIE tree. This is useful when @p die is
17113/// e.g, DW_TAG_partial_unit that can be included in several places in
17114/// the DIE tree.
17115///
17116/// @param is_required_decl_spec if true, it means the ir node to
17117/// build is for a decl that is a specification for another decl that
17118/// is concrete. If you don't know what this is, set it to false.
17119///
17120/// @param is_declaration_only is true if the DIE denoted by @p die is
17121/// a declaration-only DIE.
17122///
17123/// @return the resulting IR node.
17125build_ir_node_from_die(reader& rdr,
17126 Dwarf_Die* die,
17127 scope_decl* scope,
17128 bool called_from_public_decl,
17129 size_t where_offset,
17130 bool is_declaration_only,
17131 bool is_required_decl_spec)
17132{
17134
17135 if (!die || !scope)
17136 return result;
17137
17138 int tag = dwarf_tag(die);
17139
17140 if (!called_from_public_decl)
17141 {
17142 if (rdr.load_all_types() && die_is_type(die))
17143 /* We were instructed to load debug info for all types,
17144 included those that are not reachable from a public
17145 declaration. So load the debug info for this type. */;
17146 else if (tag != DW_TAG_subprogram
17147 && tag != DW_TAG_variable
17148 && tag != DW_TAG_member
17149 && tag != DW_TAG_namespace)
17150 return result;
17151 }
17152
17153 const die_source source_of_die = rdr.get_die_source(die);
17154
17155 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
17156 source_of_die)))
17157 {
17158 if (rdr.load_all_types())
17159 if (called_from_public_decl)
17160 if (type_base_sptr t = is_type(result))
17161 if (corpus *abi_corpus = rdr.corpus().get())
17162 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17163
17164 return result;
17165 }
17166
17167 // This is *the* bit of code that ensures we have the right notion
17168 // of "declared" at any point in a DIE chain formed from
17169 // DW_AT_abstract_origin and DW_AT_specification links. There should
17170 // be no other callers of die_is_declaration_only.
17171 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
17172
17173 switch (tag)
17174 {
17175 // Type DIEs we support.
17176 case DW_TAG_base_type:
17177 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
17178 {
17179 result =
17180 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
17181 maybe_canonicalize_type(t, rdr);
17182 }
17183 break;
17184
17185 case DW_TAG_typedef:
17186 {
17187 typedef_decl_sptr t = build_typedef_type(rdr, die,
17188 called_from_public_decl,
17189 where_offset);
17190
17191 result = add_decl_to_scope(t, scope);
17192 if (result)
17193 {
17194 maybe_set_member_type_access_specifier(is_decl(result), die);
17195 maybe_canonicalize_type(t, rdr);
17196 }
17197 }
17198 break;
17199
17200 case DW_TAG_pointer_type:
17201 {
17203 build_pointer_type_def(rdr, die,
17204 called_from_public_decl,
17205 where_offset);
17206 if (p)
17207 {
17208 result =
17209 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
17210 ABG_ASSERT(result->get_translation_unit());
17211 maybe_canonicalize_type(p, rdr);
17212 }
17213 }
17214 break;
17215
17216 case DW_TAG_reference_type:
17217 case DW_TAG_rvalue_reference_type:
17218 {
17220 build_reference_type(rdr, die,
17221 called_from_public_decl,
17222 where_offset);
17223 if (r)
17224 {
17225 result =
17226 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
17227 maybe_canonicalize_type(r, rdr);
17228 }
17229 }
17230 break;
17231
17232 case DW_TAG_ptr_to_member_type:
17233 {
17235 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
17236 where_offset);
17237 if (p)
17238 {
17239 result =
17241 rdr.cur_transl_unit()->get_global_scope());
17242 maybe_canonicalize_type(p, rdr);
17243 }
17244 }
17245 break;
17246
17247 case DW_TAG_const_type:
17248 case DW_TAG_volatile_type:
17249 case DW_TAG_restrict_type:
17250 {
17251 type_base_sptr q =
17252 build_qualified_type(rdr, die,
17253 called_from_public_decl,
17254 where_offset);
17255 if (q)
17256 {
17257 // Strip some potentially redundant type qualifiers from
17258 // the qualified type we just built.
17259 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
17260 rdr);
17261 if (!d)
17262 d = get_type_declaration(q);
17263 ABG_ASSERT(d);
17264 type_base_sptr ty = is_type(d);
17265 // Associate the die to type ty again because 'ty'might be
17266 // different from 'q', because 'ty' is 'q' possibly
17267 // stripped from some redundant type qualifier.
17268 rdr.associate_die_to_type(die, ty, where_offset);
17269 result =
17270 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
17271 maybe_canonicalize_type(is_type(result), rdr);
17272 }
17273 }
17274 break;
17275
17276 case DW_TAG_enumeration_type:
17277 {
17278 bool type_is_opaque = false;
17279 bool type_suppressed =
17280 type_is_suppressed(rdr, scope, die, type_is_opaque);
17281 if (type_suppressed && type_is_opaque)
17282 {
17283 // The type is suppressed because it's private. If other
17284 // non-suppressed and declaration-only instances of this
17285 // type exist in the current corpus, then it means those
17286 // non-suppressed instances are opaque versions of the
17287 // suppressed private type. Lets return one of these opaque
17288 // types then.
17289 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17290 maybe_canonicalize_type(is_type(result), rdr);
17291 }
17292 else if (!type_suppressed)
17293 {
17294 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
17295 where_offset,
17296 is_declaration_only);
17297 result = add_decl_to_scope(e, scope);
17298 if (result)
17299 {
17300 maybe_set_member_type_access_specifier(is_decl(result), die);
17301 maybe_canonicalize_type(is_type(result), rdr);
17302 }
17303 }
17304 }
17305 break;
17306
17307 case DW_TAG_class_type:
17308 case DW_TAG_structure_type:
17309 {
17310 bool type_is_opaque = false;
17311 bool type_suppressed=
17312 type_is_suppressed(rdr, scope, die, type_is_opaque);
17313
17314 if (type_suppressed && type_is_opaque)
17315 {
17316 // The type is suppressed because it's private. If other
17317 // non-suppressed and declaration-only instances of this
17318 // type exist in the current corpus, then it means those
17319 // non-suppressed instances are opaque versions of the
17320 // suppressed private type. Lets return one of these opaque
17321 // types then.
17322 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17323 maybe_canonicalize_type(is_type(result), rdr);
17324 }
17325 else if (!type_suppressed)
17326 {
17327 class_decl_sptr klass;
17328 Dwarf_Die spec_die;
17329 if (die_die_attribute(die, DW_AT_specification, spec_die))
17330 {
17331 scope_decl_sptr skope =
17332 get_scope_for_die(rdr, &spec_die,
17333 called_from_public_decl,
17334 where_offset);
17335 ABG_ASSERT(skope);
17336 decl_base_sptr cl =
17337 is_decl(build_ir_node_from_die(rdr, &spec_die,
17338 skope.get(),
17339 called_from_public_decl,
17340 where_offset,
17341 is_declaration_only,
17342 /*is_required_decl_spec=*/false));
17343 ABG_ASSERT(cl);
17344 klass = dynamic_pointer_cast<class_decl>(cl);
17345 ABG_ASSERT(klass);
17346
17347 klass =
17348 add_or_update_class_type(rdr, die,
17349 skope.get(),
17350 tag == DW_TAG_structure_type,
17351 klass,
17352 called_from_public_decl,
17353 where_offset,
17354 is_declaration_only);
17355 }
17356 else
17357 {
17358 if (class_decl* class_sc = is_class_type(scope))
17359 {
17360 string type_name = die_type_name(rdr, die,
17361 /*qualified_name=*/false,
17362 where_offset);
17363 if (class_decl_sptr c =
17364 is_class_type(class_sc->find_member_type(type_name)))
17365 klass = c;
17366 else
17367 klass =
17368 add_or_update_class_type(rdr, die, scope,
17369 tag == DW_TAG_structure_type,
17371 called_from_public_decl,
17372 where_offset,
17373 is_declaration_only);
17374 }
17375 else
17376 klass =
17377 add_or_update_class_type(rdr, die, scope,
17378 tag == DW_TAG_structure_type,
17380 called_from_public_decl,
17381 where_offset,
17382 is_declaration_only);
17383 }
17384 if (klass)
17385 {
17386 maybe_set_member_type_access_specifier(klass, die);
17387 maybe_canonicalize_type(klass, rdr);
17388 }
17389 result = klass;
17390 }
17391 }
17392 break;
17393 case DW_TAG_union_type:
17394 if (!type_is_suppressed(rdr, scope, die))
17395 {
17396 union_decl_sptr union_type;
17397 if (class_decl* class_sc = is_class_type(scope))
17398 {
17399 string type_name = die_type_name(rdr, die,
17400 /*qualified_name=*/false,
17401 where_offset);
17402 if (union_decl_sptr u =
17403 is_union_type(class_sc->find_member_type(type_name)))
17404 union_type = u;
17405 }
17406
17407 if (!union_type)
17408 union_type =
17409 add_or_update_union_type(rdr, die, scope,
17410 union_decl_sptr(),
17411 called_from_public_decl,
17412 where_offset,
17413 is_declaration_only);
17414
17415 if (union_type)
17416 {
17417 maybe_set_member_type_access_specifier(union_type, die);
17418 maybe_canonicalize_type(union_type, rdr);
17419 result = union_type;
17420 }
17421 }
17422 break;
17423 case DW_TAG_string_type:
17424 break;
17425 case DW_TAG_subroutine_type:
17426 {
17427 function_type_sptr f = build_function_type(rdr, die,
17429 where_offset);
17430 if (f)
17431 {
17432 result = f;
17433 result->set_is_artificial(false);
17434 maybe_canonicalize_type(f, rdr);
17435 }
17436 }
17437 break;
17438 case DW_TAG_array_type:
17439 {
17440 array_type_def_sptr a = build_array_type(rdr,
17441 die,
17442 called_from_public_decl,
17443 where_offset);
17444 if (a)
17445 {
17446 result =
17447 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
17448 maybe_canonicalize_type(a, rdr);
17449 }
17450 break;
17451 }
17452 case DW_TAG_subrange_type:
17453 {
17454 // If we got here, this means the subrange type is a "free
17455 // form" defined in the global namespace of the current
17456 // translation unit, like what is found in Ada.
17458 build_subrange_type(rdr, die, where_offset,
17459 /*associate_type_to_die=*/true);
17460 if (s)
17461 {
17462 result =
17463 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
17464 maybe_canonicalize_type(s, rdr);
17465 }
17466 }
17467 break;
17468 case DW_TAG_packed_type:
17469 break;
17470 case DW_TAG_set_type:
17471 break;
17472 case DW_TAG_file_type:
17473 break;
17474 case DW_TAG_thrown_type:
17475 break;
17476 case DW_TAG_interface_type:
17477 break;
17478 case DW_TAG_unspecified_type:
17479 break;
17480 case DW_TAG_shared_type:
17481 break;
17482
17483 case DW_TAG_compile_unit:
17484 // We shouldn't reach this point b/c this should be handled by
17485 // build_translation_unit.
17487
17488 case DW_TAG_namespace:
17489 case DW_TAG_module:
17490 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
17491 break;
17492
17493 case DW_TAG_variable:
17494 case DW_TAG_member:
17495 {
17496 if (tag == DW_TAG_member)
17497 ABG_ASSERT(!die_is_in_c(die));
17498
17499 scope_decl_sptr var_scope =
17500 get_scope_for_die(rdr, die,
17501 /*called_from_public_decl=*/
17502 die_is_effectively_public_decl(rdr, die),
17503 where_offset);
17504 var_decl_sptr v =
17505 build_or_get_var_decl_if_not_suppressed(rdr, var_scope.get(), die,
17506 where_offset,
17507 is_declaration_only,
17508 /*result=*/var_decl_sptr(),
17509 is_required_decl_spec);
17510 if (v && is_data_member(v))
17511 // We might have gotten a pre-existing data member variable
17512 // that was already built. This means this DIE is a
17513 // concrete implementation of a previous specification.
17514 // Read the specific attributes of this concrete
17515 // implementation and add them to the existing IR node we
17516 // have.
17517 v = build_var_decl(rdr, die, where_offset, v);
17518
17519 Dwarf_Addr addr = 0;
17520 bool has_data_location = false;
17521 has_data_location = rdr.get_variable_address(die, addr);
17522
17523 if ((v && has_data_location && is_class_type(var_scope))
17524 // This is most likely for a static data member's variable
17525 // that has data location ...
17526 || (v && rdr.is_decl_die_with_undefined_symbol(die))
17527 || (v && rdr.is_decl_die_with_exported_symbol(die))
17528 // ... or this is for an undefined or defined & exported
17529 // global variable.
17530 )
17531 {
17532 add_decl_to_scope(v, var_scope);
17533 if (is_data_member(v))
17534 // We are sure this is a static data member at this
17535 // point because a non-static data member would have
17536 // been encountered as a child of a class or union DIE
17537 // and thus handled by add_or_update_class_type or
17538 // add_or_update_union_type.
17539 set_member_is_static(v, true);
17540 else
17541 rdr.var_decls_to_re_add_to_tree().push_back(v);
17542 rdr.add_var_to_exported_or_undefined_decls(v);
17543 rdr.associate_die_to_decl(die, v, where_offset,
17544 /*associate_by_repr=*/false);
17545 result = v;
17546 }
17547 }
17548 break;
17549
17550 case DW_TAG_subprogram:
17551 case DW_TAG_inlined_subroutine:
17552 {
17553 if (die_is_artificial(die))
17554 break;
17555
17556 Dwarf_Die abstract_origin_die;
17557 bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
17558 abstract_origin_die,
17559 /*recursive=*/true);
17560
17561
17562 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
17563 where_offset);
17564 scope_decl* interface_scope = scope ? scope : s.get();
17565
17566 class_decl* class_scope = is_class_type(interface_scope);
17567 string linkage_name = die_linkage_name(die);
17568 string spec_linkage_name;
17569 function_decl_sptr existing_fn;
17570
17571 if (class_scope)
17572 {
17573 // The scope of the function DIE we are looking at is a
17574 // class. So we are looking at a member function.
17575 if (!linkage_name.empty())
17576 {
17577 if ((existing_fn =
17578 class_scope->find_member_function_sptr(linkage_name)))
17579 {
17580 // A function with the same linkage name has
17581 // already been created. Let's see if we are a
17582 // clone of it or not.
17583 spec_linkage_name = existing_fn->get_linkage_name();
17584 if (has_abstract_origin
17585 && !spec_linkage_name.empty()
17586 && linkage_name != spec_linkage_name)
17587 {
17588 // The current DIE has 'existing_fn' as
17589 // abstract orign, and has a linkage name that
17590 // is different from from the linkage name of
17591 // 'existing_fn'. That means, the current DIE
17592 // represents a clone of 'existing_fn'.
17593 existing_fn = existing_fn->clone();
17594 }
17595 }
17596 }
17597 }
17598 else if (has_abstract_origin)
17599 // Let's see if this function is the implementation of an
17600 // existing interface. In that case, let's read the
17601 // specification of the origin interface ...
17602 existing_fn = build_function_decl(rdr, &abstract_origin_die, where_offset,
17603 /*existing_fn=*/nullptr);
17604
17605 rdr.scope_stack().push(interface_scope);
17606
17607 // Either we create a brand new IR for the current function
17608 // DIE we are looking at, or we complete an existing IR node
17609 // with the new completementary information carried by this
17610 // DIE for that IR node.
17611 result =
17612 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
17613 die, where_offset,
17614 is_declaration_only,
17615 existing_fn);
17616
17617 if (result && !existing_fn)
17618 {
17619 // We built a brand new IR for the function DIE. Now
17620 // there should be enough information on that IR to know
17621 // if we should drop it on the floor or keep it ...
17622 if (potential_member_fn_should_be_dropped(is_function_decl(result), die)
17623 && !is_required_decl_spec)
17624 {
17625 // So apparently we should drop that function IR on
17626 // the floor. Let's do so.
17627 result.reset();
17628 break;
17629 }
17630 }
17631
17632 // OK so we came to the conclusion that we need to keep
17633 // the function. So let's add it to its scope.
17634 result = add_decl_to_scope(is_decl(result), interface_scope);
17635
17637 if (fn && is_member_function(fn))
17638 {
17639 class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
17640 sptr_utils::noop_deleter());
17641 ABG_ASSERT(klass);
17642 finish_member_function_reading(die, fn, klass, rdr);
17643 }
17644
17645 if (fn)
17646 {
17647 if (!is_member_function(fn)
17649 // Virtual member functions are added to the set of
17650 // functions exported by the current ABI corpus *after*
17651 // the canonicalization of their parent type. So let's
17652 // not do it here.
17653 rdr.add_fn_to_exported_or_undefined_decls(fn.get());
17654 rdr.associate_die_to_decl(die, fn, where_offset,
17655 /*associate_by_repr=*/false);
17656 maybe_canonicalize_type(fn->get_type(), rdr);
17657 }
17658
17659 rdr.scope_stack().pop();
17660 }
17661 break;
17662
17663 case DW_TAG_formal_parameter:
17664 // We should not read this case as it should have been dealt
17665 // with by build_function_decl above.
17667
17668 case DW_TAG_constant:
17669 break;
17670 case DW_TAG_enumerator:
17671 break;
17672
17673 case DW_TAG_partial_unit:
17674 case DW_TAG_imported_unit:
17675 // For now, the DIEs under these are read lazily when they are
17676 // referenced by a public decl DIE that is under a
17677 // DW_TAG_compile_unit, so we shouldn't get here.
17679
17680 // Other declaration we don't really intend to support yet.
17681 case DW_TAG_dwarf_procedure:
17682 case DW_TAG_imported_declaration:
17683 case DW_TAG_entry_point:
17684 case DW_TAG_label:
17685 case DW_TAG_lexical_block:
17686 case DW_TAG_unspecified_parameters:
17687 case DW_TAG_variant:
17688 case DW_TAG_common_block:
17689 case DW_TAG_common_inclusion:
17690 case DW_TAG_inheritance:
17691 case DW_TAG_with_stmt:
17692 case DW_TAG_access_declaration:
17693 case DW_TAG_catch_block:
17694 case DW_TAG_friend:
17695 case DW_TAG_namelist:
17696 case DW_TAG_namelist_item:
17697 case DW_TAG_template_type_parameter:
17698 case DW_TAG_template_value_parameter:
17699 case DW_TAG_try_block:
17700 case DW_TAG_variant_part:
17701 case DW_TAG_imported_module:
17702 case DW_TAG_condition:
17703 case DW_TAG_type_unit:
17704 case DW_TAG_template_alias:
17705 case DW_TAG_lo_user:
17706 case DW_TAG_MIPS_loop:
17707 case DW_TAG_format_label:
17708 case DW_TAG_function_template:
17709 case DW_TAG_class_template:
17710 case DW_TAG_GNU_BINCL:
17711 case DW_TAG_GNU_EINCL:
17712 case DW_TAG_GNU_template_template_param:
17713 case DW_TAG_GNU_template_parameter_pack:
17714 case DW_TAG_GNU_formal_parameter_pack:
17715 case DW_TAG_GNU_call_site:
17716 case DW_TAG_GNU_call_site_parameter:
17717 case DW_TAG_hi_user:
17718 default:
17719 break;
17720 }
17721
17722 if (result && tag != DW_TAG_subroutine_type)
17723 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
17724 /*associate_by_repr=*/false);
17725
17726 if (result)
17727 if (rdr.load_all_types())
17728 if (called_from_public_decl)
17729 if (type_base_sptr t = is_type(result))
17730 if (corpus *abi_corpus = scope->get_corpus())
17731 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17732
17733 rdr.maybe_schedule_decl_only_type_for_resolution(result);
17734
17735 return result;
17736}
17737
17738/// Build the IR node for a void type.
17739///
17740/// @param rdr the DWARF reader to use.
17741///
17742/// @return the void type node.
17743static decl_base_sptr
17744build_ir_node_for_void_type(reader& rdr)
17745{
17746 const environment& env = rdr.env();
17747
17748 type_base_sptr t = env.get_void_type();
17749 decl_base_sptr type_declaration = get_type_declaration(t);
17750 if (!has_scope(type_declaration))
17751 {
17752 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17753 rdr.schedule_type_for_late_canonicalization(t);
17754 }
17755 return type_declaration;
17756}
17757
17758/// Build the IR node for a "pointer to void type".
17759///
17760/// That IR node is shared across the ABI corpus.
17761///
17762/// Note that this function just gets that IR node from the
17763/// environment and, if it's not added to any scope yet, adds it to
17764/// the global scope associated to the current translation unit.
17765///
17766/// @param rdr the DWARF reader to consider.
17767///
17768/// @return the IR node.
17770build_ir_node_for_void_pointer_type(reader& rdr)
17771{
17772 const environment& env = rdr.env();
17773 type_base_sptr t = env.get_void_pointer_type();
17774 decl_base_sptr type_declaration = get_type_declaration(t);
17775 if (!has_scope(type_declaration))
17776 {
17777 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17778 rdr.schedule_type_for_late_canonicalization(t);
17779 }
17780 return type_declaration;
17781}
17782
17783/// Build the IR node for a variadic parameter type.
17784///
17785/// @param rdr the DWARF reader to use.
17786///
17787/// @return the variadic parameter type.
17788static decl_base_sptr
17789build_ir_node_for_variadic_parameter_type(reader &rdr)
17790{
17791
17792 const environment& env = rdr.env();
17793 type_base_sptr t = env.get_variadic_parameter_type();
17794 decl_base_sptr type_declaration = get_type_declaration(t);
17795 if (!has_scope(type_declaration))
17796 {
17797 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17798 rdr.schedule_type_for_late_canonicalization(t);
17799 }
17800 return type_declaration;
17801}
17802
17803/// Build an IR node from a given DIE and add the node to the current
17804/// IR being build and held in the DWARF reader. Doing that is called
17805/// "emitting an IR node for the DIE".
17806///
17807/// @param rdr the DWARF reader.
17808///
17809/// @param die the DIE to consider.
17810///
17811/// @param called_from_public_decl set to yes if this function is
17812/// called from the functions used to build a public decl (functions
17813/// and variables). In that case, this function accepts building IR
17814/// nodes representing types. Otherwise, this function only creates
17815/// IR nodes representing public decls (functions and variables).
17816/// This is done to avoid emitting IR nodes for types that are not
17817/// referenced by public functions or variables.
17818///
17819/// @param where_offset the offset of the DIE where we are "logically"
17820/// positionned at, in the DIE tree. This is useful when @p die is
17821/// e.g, DW_TAG_partial_unit that can be included in several places in
17822/// the DIE tree.
17823///
17824/// @return the resulting IR node.
17826build_ir_node_from_die(reader& rdr,
17827 Dwarf_Die* die,
17828 bool called_from_public_decl,
17829 size_t where_offset)
17830{
17831 if (!die)
17832 return decl_base_sptr();
17833
17834 // Normaly, a decl that is meant to be external has a DW_AT_external
17835 // set. But then some compilers fail to always emit that flag. For
17836 // instance, for static data members, some compilers won't emit the
17837 // DW_AT_external. In that case, we assume that if the variable is
17838 // at global or named namespace scope, then we can assume it's
17839 // external. If the variable doesn't have any ELF symbol associated
17840 // to it, it'll be dropped on the floor anyway. Those variable
17841 // decls are considered as being "effectively public".
17842 bool consider_as_called_from_public_decl =
17843 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
17844 scope_decl_sptr scope = get_scope_for_die(rdr, die,
17845 consider_as_called_from_public_decl,
17846 where_offset);
17847 if (!scope)
17848 scope = rdr.global_scope();
17849
17850 return build_ir_node_from_die(rdr, die, scope.get(),
17851 called_from_public_decl,
17852 where_offset, true);
17853}
17854
17855/// Create a dwarf::reader.
17856///
17857/// @param elf_path the path to the elf file the reader is to be used
17858/// for.
17859///
17860/// @param debug_info_root_paths a vector to the paths to the
17861/// directories under which the debug info is to be found for @p
17862/// elf_path. Pass an empty vector if the debug info is not in a
17863/// split file.
17864///
17865/// @param environment the environment used by the current context.
17866/// This environment contains resources needed by the DWARF reader and by
17867/// the types and declarations that are to be created later. Note
17868/// that ABI artifacts that are to be compared all need to be created
17869/// within the same environment.
17870///
17871/// Please also note that the life time of this environment object
17872/// must be greater than the life time of the resulting @ref
17873/// reader the context uses resources that are allocated in the
17874/// environment.
17875///
17876/// @param load_all_types if set to false only the types that are
17877/// reachable from publicly exported declarations (of functions and
17878/// variables) are read. If set to true then all types found in the
17879/// debug information are loaded.
17880///
17881/// @param linux_kernel_mode if set to true, then consider the special
17882/// linux kernel symbol tables when determining if a symbol is
17883/// exported or not.
17884///
17885/// @return a smart pointer to the resulting dwarf::reader.
17886elf_based_reader_sptr
17887create_reader(const std::string& elf_path,
17888 const vector<string>& debug_info_root_paths,
17890 bool load_all_types,
17891 bool linux_kernel_mode)
17892{
17893
17894 reader_sptr r = reader::create(elf_path,
17895 debug_info_root_paths,
17897 load_all_types,
17898 linux_kernel_mode);
17899 return static_pointer_cast<elf_based_reader>(r);
17900}
17901
17902/// Re-initialize a reader so that it can re-used to read
17903/// another binary.
17904///
17905/// @param rdr the context to re-initialize.
17906///
17907/// @param elf_path the path to the elf file the context is to be used
17908/// for.
17909///
17910/// @param debug_info_root_path a pointer to the path to the root
17911/// directory under which the debug info is to be found for @p
17912/// elf_path. Leave this to NULL if the debug info is not in a split
17913/// file.
17914///
17915/// @param environment the environment used by the current context.
17916/// This environment contains resources needed by the DWARF reader and by
17917/// the types and declarations that are to be created later. Note
17918/// that ABI artifacts that are to be compared all need to be created
17919/// within the same environment.
17920///
17921/// Please also note that the life time of this environment object
17922/// must be greater than the life time of the resulting @ref
17923/// reader the context uses resources that are allocated in the
17924/// environment.
17925///
17926/// @param load_all_types if set to false only the types that are
17927/// reachable from publicly exported declarations (of functions and
17928/// variables) are read. If set to true then all types found in the
17929/// debug information are loaded.
17930///
17931/// @param linux_kernel_mode if set to true, then consider the special
17932/// linux kernel symbol tables when determining if a symbol is
17933/// exported or not.
17934///
17935/// @return a smart pointer to the resulting dwarf::reader.
17936void
17938 const std::string& elf_path,
17939 const vector<string>&debug_info_root_path,
17940 bool read_all_types,
17941 bool linux_kernel_mode)
17942{
17943 reader& r = dynamic_cast<reader&>(rdr);
17944 r.initialize(elf_path, debug_info_root_path,
17945 read_all_types, linux_kernel_mode);
17946}
17947
17948/// Read all @ref abigail::translation_unit possible from the debug info
17949/// accessible from an elf file, stuff them into a libabigail ABI
17950/// Corpus and return it.
17951///
17952/// @param elf_path the path to the elf file.
17953///
17954/// @param debug_info_root_paths a vector of pointers to root paths
17955/// under which to look for the debug info of the elf files that are
17956/// later handled by the Dwfl. This for cases where the debug info is
17957/// split into a different file from the binary we want to inspect.
17958/// On Red Hat compatible systems, this root path is usually
17959/// /usr/lib/debug by default. If this argument is set to NULL, then
17960/// "./debug" and /usr/lib/debug will be searched for sub-directories
17961/// containing the debug info file.
17962///
17963/// @param environment the environment used by the current context.
17964/// This environment contains resources needed by the DWARF reader and by
17965/// the types and declarations that are to be created later. Note
17966/// that ABI artifacts that are to be compared all need to be created
17967/// within the same environment. Also, the lifetime of the
17968/// environment must be greater than the lifetime of the resulting
17969/// corpus because the corpus uses resources that are allocated in the
17970/// environment.
17971///
17972/// @param load_all_types if set to false only the types that are
17973/// reachable from publicly exported declarations (of functions and
17974/// variables) are read. If set to true then all types found in the
17975/// debug information are loaded.
17976///
17977/// @param resulting_corp a pointer to the resulting abigail::corpus.
17978///
17979/// @return the resulting status.
17980corpus_sptr
17981read_corpus_from_elf(const std::string& elf_path,
17982 const vector<string>& debug_info_root_paths,
17984 bool load_all_types,
17985 fe_iface::status& status)
17986{
17987 elf_based_reader_sptr rdr =
17988 dwarf::reader::create(elf_path, debug_info_root_paths,
17989 environment, load_all_types,
17990 /*linux_kernel_mode=*/false);
17991
17992 return rdr->read_corpus(status);
17993}
17994
17995/// Look into the symbol tables of a given elf file and see if we find
17996/// a given symbol.
17997///
17998/// @param env the environment we are operating from.
17999///
18000/// @param elf_path the path to the elf file to consider.
18001///
18002/// @param symbol_name the name of the symbol to look for.
18003///
18004/// @param demangle if true, try to demangle the symbol name found in
18005/// the symbol table.
18006///
18007/// @param syms the vector of symbols found with the name @p symbol_name.
18008///
18009/// @return true iff the symbol was found among the publicly exported
18010/// symbols of the ELF file.
18011bool
18012lookup_symbol_from_elf(const environment& env,
18013 const string& elf_path,
18014 const string& symbol_name,
18015 bool demangle,
18016 vector<elf_symbol_sptr>& syms)
18017
18018{
18019 if (elf_version(EV_CURRENT) == EV_NONE)
18020 return false;
18021
18022 int fd = open(elf_path.c_str(), O_RDONLY);
18023 if (fd < 0)
18024 return false;
18025
18026 struct stat s;
18027 if (fstat(fd, &s))
18028 return false;
18029
18030 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
18031 if (elf == 0)
18032 return false;
18033
18034 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
18035 demangle, syms);
18036 elf_end(elf);
18037 close(fd);
18038
18039 return value;
18040}
18041
18042/// Look into the symbol tables of an elf file to see if a public
18043/// function of a given name is found.
18044///
18045/// @param env the environment we are operating from.
18046///
18047/// @param elf_path the path to the elf file to consider.
18048///
18049/// @param symbol_name the name of the function to look for.
18050///
18051/// @param syms the vector of public function symbols found with the
18052/// name @p symname.
18053///
18054/// @return true iff a function with symbol name @p symbol_name is
18055/// found.
18056bool
18057lookup_public_function_symbol_from_elf(environment& env,
18058 const string& path,
18059 const string& symname,
18060 vector<elf_symbol_sptr>& syms)
18061{
18062 if (elf_version(EV_CURRENT) == EV_NONE)
18063 return false;
18064
18065 int fd = open(path.c_str(), O_RDONLY);
18066 if (fd < 0)
18067 return false;
18068
18069 struct stat s;
18070 if (fstat(fd, &s))
18071 return false;
18072
18073 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
18074 if (elf == 0)
18075 return false;
18076
18077 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
18078 elf_end(elf);
18079 close(fd);
18080
18081 return value;
18082}
18083
18084}// end namespace dwarf
18085
18086}// 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:19525
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:4182
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:4799
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:3177
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition abg-ir.h:3180
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:16860
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:4121
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:6461
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:12455
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:12319
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:14036
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:14198
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:11797
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition abg-ir.cc:10817
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition abg-ir.cc:5406
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:12225
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:16778
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition abg-ir.cc:8482
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:10214
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:11721
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:12345
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:11175
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:6722
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:10868
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:7340
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:6121
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:13899
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition abg-ir.cc:6489
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:12786
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:11406
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:6545
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:6922
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:10919
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:11868
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:8693
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition abg-ir.cc:5551
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:11128
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition abg-ir.cc:11021
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:14066
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:12495
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:11624
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:11661
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:11110
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:8550
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:24773
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:12985
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:12532
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:7308
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition abg-ir.cc:6375
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:10757
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:25724
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition abg-ir.cc:5471
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:8457
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:29096
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition abg-ir.cc:5522
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:6648
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:11489
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:11928
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition abg-ir.cc:11455
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:13854
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:24841
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:12204
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition abg-ir.cc:5620
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:13803
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition abg-ir.cc:10236
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition abg-ir.cc:26824
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:12133
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:7045
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:15438
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:7675
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:14161
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition abg-ir.cc:10705
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:11898
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:11848
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:12565
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:8739
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:11958
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition abg-ir.cc:5424
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:6432
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.