libabigail
abg-dwarf-reader.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2// -*- Mode: C++ -*-
3//
4// Copyright (C) 2013-2025 Red Hat, Inc.
5//
6// Author: Dodji Seketeli
7
8/// @file
9///
10/// This file contains the definitions of the entry points to
11/// de-serialize an instance of @ref abigail::corpus from a file in
12/// elf format, containing dwarf information.
13
14#include "abg-internal.h"
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <libgen.h>
20#include <assert.h>
21#include <limits.h>
22#include <elfutils/libdwfl.h>
23#include <dwarf.h>
24#include <algorithm>
25#include <cmath>
26#include <cstring>
27#include <deque>
28#include <list>
29#include <memory>
30#include <ostream>
31#include <sstream>
32#include <stack>
33#include <unordered_map>
34#include <unordered_set>
35#include <map>
36
37#include "abg-ir-priv.h"
39#include "abg-corpus-priv.h"
40#include "abg-symtab-reader.h"
41
42// <headers defining libabigail's API go under here>
43ABG_BEGIN_EXPORT_DECLARATIONS
44
45#include "abg-dwarf-reader.h"
47#include "abg-sptr-utils.h"
48#include "abg-tools-utils.h"
49#include "abg-elf-helpers.h"
50
51ABG_END_EXPORT_DECLARATIONS
52// </headers defining libabigail's API>
53
54#ifndef UINT64_MAX
55#define UINT64_MAX 0xffffffffffffffff
56#endif
57
58using std::string;
59
60namespace abigail
61{
62
63using std::cerr;
64
65/// The namespace for the DWARF reader.
66namespace dwarf
67{
68
69using std::dynamic_pointer_cast;
70using std::static_pointer_cast;
71using std::unordered_map;
72using std::unordered_set;
73using std::stack;
74using std::deque;
75using std::list;
76using std::map;
78
79using namespace elf_helpers; // TODO: avoid using namespace
80
81/// Where a DIE comes from. For instance, a DIE can come from the main
82/// debug info section, the alternate debug info section or from the
83/// type unit section.
85{
86 NO_DEBUG_INFO_DIE_SOURCE,
87 PRIMARY_DEBUG_INFO_DIE_SOURCE,
88 ALT_DEBUG_INFO_DIE_SOURCE,
89 TYPE_UNIT_DIE_SOURCE,
90 NUMBER_OF_DIE_SOURCES, // This one must always be the latest
91 // enumerator
92};
93
94
95/// A convenience typedef for a vector of Dwarf_Off.
96typedef vector<Dwarf_Off> dwarf_offsets_type;
97
98/// Convenience typedef for a map which key is the offset of a dwarf
99/// die and which value is the corresponding artefact.
100typedef unordered_map<Dwarf_Off, type_or_decl_base_sptr> die_artefact_map_type;
101
102/// Convenience typedef for a map which key is the offset of a dwarf
103/// die, (given by dwarf_dieoffset()) and which value is the
104/// corresponding class_decl.
105typedef unordered_map<Dwarf_Off, class_decl_sptr> die_class_map_type;
106
107/// Convenience typedef for a map which key is the offset of a dwarf
108/// die, (given by dwarf_dieoffset()) and which value is the
109/// corresponding class_or_union_sptr.
110typedef unordered_map<Dwarf_Off, class_or_union_sptr> die_class_or_union_map_type;
111
112/// Convenience typedef for a map which key the offset of a dwarf die
113/// and which value is the corresponding function_decl.
114typedef unordered_map<Dwarf_Off, function_decl_sptr> die_function_decl_map_type;
115
116/// Convenience typedef for a map which key is the offset of a dwarf
117/// die and which value is the corresponding function_type.
118typedef unordered_map<Dwarf_Off, function_type_sptr> die_function_type_map_type;
119
120/// Convenience typedef for a map which key is the offset of a
121/// DW_TAG_compile_unit and the value is the corresponding @ref
122/// translation_unit_sptr.
123typedef unordered_map<Dwarf_Off, translation_unit_sptr> die_tu_map_type;
124
125/// Convenience typedef for a map which key is the offset of a DIE and
126/// the value is the corresponding qualified name of the DIE.
127typedef unordered_map<Dwarf_Off, interned_string> die_istring_map_type;
128
129/// Convenience typedef for a map which is an interned_string and
130/// which value is a vector of offsets.
131typedef unordered_map<interned_string,
135
136/// A hasher for a pair of Dwarf_Off. This is used as a hasher for
137/// the type @ref dwarf_offset_pair_set_type.
138struct dwarf_offset_pair_hash
139{
140 size_t
141 operator()(const std::pair<Dwarf_Off, Dwarf_Off>& p) const
142 {return *abigail::hashing::combine_hashes(hash_t(p.first), hash_t(p.second));}
143};// end struct dwarf_offset_pair_hash
144
145typedef unordered_set<std::pair<Dwarf_Off,
146 Dwarf_Off>,
147 dwarf_offset_pair_hash> dwarf_offset_pair_set_type;
148
149/// An abstraction of a DIE offset that also encapsulate the source of
150/// the DIE.
151struct offset_type
152{
153 die_source source_;
154 Dwarf_Off offset_;
155
156 offset_type()
157 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
158 offset_(0)
159 {}
160
161 offset_type(die_source source, Dwarf_Off offset)
162 : source_(source),
163 offset_(offset)
164 {}
165
166 offset_type(Dwarf_Off offset)
167 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
168 offset_(offset)
169 {}
170
171 bool operator==(const offset_type& o) const
172 {return source_ == o.source_ && offset_ == o.offset_;}
173
174 operator Dwarf_Off() const
175 {return offset_;}
176}; // end struct offset_type
177
178/// A convenience typedef for a pair of offset_type.
179typedef std::pair<offset_type, offset_type> offset_pair_type;
180
181/// A hasher for an instance of offset_type.
182struct offset_hash
183{
184 size_t
185 operator()(const offset_type& p) const
186 {
187 return *abigail::hashing::combine_hashes(hash_t(p.source_),
188 hash_t(p.offset_));
189 }
190};// end struct offset_hash
191
192/// A hasher for a pair of offset_type. This is used as a hasher for
193/// the type @ref offset_pair_set_type, for instance.
194struct offset_pair_hash
195{
196 size_t
197 operator()(const std::pair<offset_type, offset_type>& p) const
198 {
199 hash_t h1 = abigail::hashing::combine_hashes(hash_t(p.first.source_),
200 hash_t(p.first.offset_));
201 hash_t h2 = abigail::hashing::combine_hashes(hash_t(p.second.source_),
202 hash_t(p.second.offset_));
203 return *abigail::hashing::combine_hashes(h1, h2);
204 }
205};// end struct offset_pair_hash
206
207/// A convenience typedef for an unordered set of DIE offsets.
208typedef unordered_set<offset_type, offset_hash> offset_set_type;
209
210///A convenience typedef for an unordered set of pairs of offset_type.
211typedef unordered_set<std::pair<offset_type,
212 offset_type>,
213 offset_pair_hash> offset_pair_set_type;
214
215/// A convenience typedef for a vector of pairs of offset_type.
216typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
217
218/// A convenience typedef for an unordered map that associates a pair
219/// of offset_type to a vector of pairs offset_type.
220typedef unordered_map<std::pair<offset_type, offset_type>,
222 offset_pair_hash> offset_pair_vect_map_type;
223
224/// A convenience typedef for an unordered_map that associates a pair
225/// of offset_type to a set of pairs of offset_type.
226typedef unordered_map<std::pair<offset_type, offset_type>,
228 offset_pair_hash> offset_pair_set_map_type;
229
230/// A convenience typedef for a vector of pairs of offset_type.
231typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
232
233class reader;
234
236build_translation_unit_and_add_to_ir(reader& rdr,
237 Dwarf_Die* die,
238 char address_size);
239
240static void
241maybe_propagate_canonical_type(const reader& rdr,
242 const Dwarf_Die* l,
243 const Dwarf_Die* r);
244
245static void
246propagate_canonical_type(const reader& rdr,
247 const Dwarf_Die* l,
248 const Dwarf_Die* r);
249
250static void
251maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
252 Dwarf_Die* die);
253
254static void
255cleanup_decl_name(string&);
256
257/// Convenience typedef for a shared pointer to an
258/// addr_elf_symbol_sptr_map_type.
259typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
260
261/// Convenience typedef for a map that associates an @ref
262/// interned_string to a @ref function_type_sptr.
263typedef unordered_map<interned_string,
266
267/// Convenience typedef for a stack containing the scopes up to the
268/// current point in the abigail Internal Representation (aka IR) tree
269/// that is being built.
270typedef stack<scope_decl*> scope_stack_type;
271
272/// Convenience typedef for a map which key is a dwarf offset. The
273/// value is also a dwarf offset.
274typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
275
276/// Convenience typedef for a map which key is a string and which
277/// value is a vector of smart pointer to a class_or_union_sptr.
278typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_map;
279
280/// Convenience typedef for a map which key is a string and which
281/// value is a vector of smart pointer to a class.
282typedef unordered_map<string, classes_type> string_classes_map;
283
284/// Convenience typedef for a map which key is a string and which
285/// value is a vector of smart pointer to a enum.
286typedef unordered_map<string, enums_type> string_enums_map;
287
288/// The abstraction of the place where a partial unit has been
289/// imported. This is what the DW_TAG_imported_unit DIE expresses.
290///
291/// This type thus contains:
292/// - the offset to which the partial unit is imported
293/// - the offset of the imported partial unit.
294/// - the offset of the imported partial unit.
295struct imported_unit_point
296{
297 Dwarf_Off offset_of_import;
298 // The boolean below is true iff the imported unit comes from the
299 // alternate debug info file.
300 die_source imported_unit_die_source;
301 Dwarf_Off imported_unit_die_off;
302 Dwarf_Off imported_unit_cu_off;
303 Dwarf_Off imported_unit_child_off;
304
305 /// Default constructor for @ref the type imported_unit_point.
306 imported_unit_point()
307 : offset_of_import(),
308 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
309 imported_unit_die_off(),
310 imported_unit_cu_off(),
311 imported_unit_child_off()
312 {}
313
314 /// Constructor of @ref the type imported_unit_point.
315 ///
316 /// @param import_off the offset of the point at which the unit has
317 /// been imported.
318 imported_unit_point(Dwarf_Off import_off)
319 : offset_of_import(import_off),
320 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
321 imported_unit_die_off(),
322 imported_unit_cu_off(),
323 imported_unit_child_off()
324 {}
325
326 /// Constructor of @ref the type imported_unit_point.
327 ///
328 /// @param import_off the offset of the point at which the unit has
329 /// been imported.
330 ///
331 /// @param from where the imported DIE comes from.
332 ///
333 /// @param imported_die the die of the unit that has been imported.
334 imported_unit_point(Dwarf_Off import_off,
335 const Dwarf_Die& imported_die,
336 die_source from)
337 : offset_of_import(import_off),
338 imported_unit_die_source(from),
339 imported_unit_die_off(dwarf_dieoffset
340 (const_cast<Dwarf_Die*>(&imported_die))),
341 imported_unit_cu_off(),
342 imported_unit_child_off()
343 {
344 Dwarf_Die imported_unit_child;
345
346 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
347 &imported_unit_child) == 0);
348
349 imported_unit_child_off =
350 dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
351
352 Dwarf_Die cu_die_memory;
353 Dwarf_Die *cu_die;
354
355 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
356 &cu_die_memory, 0, 0);
357 imported_unit_cu_off = dwarf_dieoffset(cu_die);
358 }
359}; // struct imported_unit_point
360
361/// Convenience typedef for a vector of @ref imported_unit_point.
362typedef vector<imported_unit_point> imported_unit_points_type;
363
364/// Convenience typedef for a vector of @ref imported_unit_point.
365typedef unordered_map<Dwarf_Off, imported_unit_points_type>
367
368/// "Less than" operator for instances of @ref imported_unit_point
369/// type.
370///
371/// @param the left hand side operand of the "Less than" operator.
372///
373/// @param the right hand side operand of the "Less than" operator.
374///
375/// @return true iff @p l is less than @p r.
376static bool
377operator<(const imported_unit_point& l, const imported_unit_point& r)
378{return l.offset_of_import < r.offset_of_import;}
379
380static bool
381get_parent_die(const reader& rdr,
382 const Dwarf_Die* die,
383 Dwarf_Die& parent_die,
384 size_t where_offset);
385
386static bool
387get_scope_die(const reader& rdr,
388 const Dwarf_Die* die,
389 size_t where_offset,
390 Dwarf_Die& scope_die);
391
392static bool
393get_die_language(const Dwarf_Die *die, translation_unit::language &lang) ;
394
395static bool
396die_is_in_c(const Dwarf_Die *die);
397
398static bool
399die_is_in_cplus_plus(const Dwarf_Die *die);
400
401static bool
402die_is_in_c_or_cplusplus(const Dwarf_Die *die);
403
404static bool
405die_is_anonymous(const Dwarf_Die* die);
406
407static bool
408die_is_anonymous_data_member(const Dwarf_Die* die);
409
410static bool
411die_is_type(const Dwarf_Die* die);
412
413static bool
414die_is_decl(const Dwarf_Die* die);
415
416static bool
417die_is_declaration_only(Dwarf_Die* die);
418
419static bool
420die_is_variable_decl(const Dwarf_Die *die);
421
422static bool
423die_is_function_decl(const Dwarf_Die *die);
424
425static bool
426die_has_size_attribute(const Dwarf_Die *die);
427
428static bool
429die_has_no_child(const Dwarf_Die *die);
430
431static bool
432die_is_namespace(const Dwarf_Die* die);
433
434static bool
435die_is_unspecified(Dwarf_Die* die);
436
437static bool
438die_is_void_type(Dwarf_Die* die);
439
440static bool
441die_is_pointer_type(const Dwarf_Die* die);
442
443static bool
444pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
445
446static bool
447die_is_reference_type(const Dwarf_Die* die);
448
449static bool
450die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
451
452static bool
453die_is_pointer_or_reference_type(const Dwarf_Die* die);
454
455static bool
456die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
457
458static bool
459die_is_class_type(const Dwarf_Die* die);
460
461static bool
462die_is_qualified_type(const Dwarf_Die* die);
463
464static bool
465die_is_function_type(const Dwarf_Die *die);
466
467static bool
468die_has_object_pointer(const Dwarf_Die* die,
469 Dwarf_Die& object_pointer);
470
471static bool
472die_has_children(const Dwarf_Die* die);
473
474static bool
475fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die);
476
477static bool
478member_fn_die_has_this_pointer(const reader& rdr,
479 const Dwarf_Die* die,
480 size_t where_offset,
481 Dwarf_Die& class_die,
482 Dwarf_Die& object_pointer_die);
483
484static bool
485die_this_pointer_from_object_pointer(Dwarf_Die* die,
486 Dwarf_Die& this_pointer);
487
488static bool
489die_this_pointer_is_const(Dwarf_Die* die);
490
491static bool
492die_object_pointer_is_for_const_method(Dwarf_Die* die);
493
494static bool
495is_type_die_to_be_canonicalized(const Dwarf_Die *die);
496
497static bool
498die_is_at_class_scope(const reader& rdr,
499 const Dwarf_Die* die,
500 size_t where_offset,
501 Dwarf_Die& class_scope_die);
502static bool
503eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
504 size_t expr_len,
505 int64_t& value,
506 bool& is_tls_address);
507
509dwarf_language_to_tu_language(size_t l);
510
511static bool
512die_unsigned_constant_attribute(const Dwarf_Die* die,
513 unsigned attr_name,
514 uint64_t& cst);
515
516static bool
517die_signed_constant_attribute(const Dwarf_Die*die,
518 unsigned attr_name,
519 int64_t& cst);
520
521static bool
522die_constant_attribute(const Dwarf_Die *die,
523 unsigned attr_name,
524 bool is_signed,
526
527static bool
528die_member_offset(const reader& rdr,
529 const Dwarf_Die* die,
530 int64_t& offset);
531
532static bool
533form_is_DW_FORM_strx(unsigned form);
534
535static bool
536form_is_DW_FORM_line_strp(unsigned form);
537
538static bool
539die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
540
541static string
542die_name(const Dwarf_Die* die);
543
544static void
545die_name_and_linkage_name(const Dwarf_Die* die,
546 string& name,
547 string& linkage_name);
548static location
549die_location(const reader& rdr, const Dwarf_Die* die);
550
551static bool
552die_location_address(Dwarf_Die* die,
553 Dwarf_Addr& address,
554 bool& is_tls_address);
555
556static bool
557die_die_attribute(const Dwarf_Die* die,
558 unsigned attr_name,
559 Dwarf_Die& result,
560 bool recursively = true);
561
562static bool
563die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
564
565static bool
566subrange_die_indirect_bound_value(const Dwarf_Die *die,
567 unsigned attr_name,
569 bool& is_signed);
570
571static bool
572subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
573 unsigned attr_name,
574 Dwarf_Die& referenced_subrange);
575static string
576get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
577
578static string
579build_internal_anonymous_die_name(const string &base_name,
580 size_t anonymous_type_index);
581
582static string
583die_qualified_type_name(const reader& rdr,
584 const Dwarf_Die* die,
585 size_t where,
586 unordered_set<uint64_t>& guard);
587
588static string
589die_qualified_decl_name(const reader& rdr,
590 const Dwarf_Die* die,
591 size_t where,
592 unordered_set<uint64_t>& guard);
593
594static string
595die_qualified_name(const reader& rdr,
596 const Dwarf_Die* die,
597 size_t where,
598 unordered_set<uint64_t>& guard);
599
600static string
601die_qualified_name(const reader& rdr,
602 const Dwarf_Die* die,
603 size_t where);
604
605static string
606die_type_name(const reader& rdr, const Dwarf_Die* die,
607 bool qualified_name, size_t where_offset,
608 unordered_set<uint64_t>& infinite_loop_guard);
609
610static string
611die_type_name(const reader& rdr, const Dwarf_Die* die,
612 bool qualified_name, size_t where_offset);
613
614static bool
615die_qualified_type_name_empty(const reader& rdr,
616 const Dwarf_Die* die, size_t where,
617 string &qualified_name,
618 unordered_set<uint64_t>& infinite_loop_guard);
619
620static void
621die_return_and_parm_names_from_fn_type_die(const reader& rdr,
622 const Dwarf_Die* die,
623 size_t where_offset,
624 bool pretty_print,
625 bool qualified_name,
626 bool &is_method_type,
627 string &return_type_name,
628 string &class_name,
629 vector<string>& parm_names,
630 bool& is_const,
631 bool& is_static,
632 unordered_set<uint64_t>& infinite_loop_guard);
633
634static string
635die_function_signature(const reader& rdr,
636 const Dwarf_Die *die,
637 bool qualified_name,
638 size_t where_offset,
639 unordered_set<uint64_t>& infinite_loop_guard);
640
641static bool
642die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
643
644static bool
645die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
646
647static bool
648die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die);
649
650static bool
651die_function_type_is_method_type(const reader& rdr,
652 const Dwarf_Die *die,
653 size_t where_offset,
654 Dwarf_Die& object_pointer_die,
655 Dwarf_Die& class_die,
656 bool& is_static);
657
658static string
659die_enum_flat_representation(const reader& rdr,
660 const Dwarf_Die* die,
661 const string& indent,
662 bool one_line,
663 bool qualified_names,
664 size_t where_offset);
665
666static string
667die_class_flat_representation(const reader& rdr,
668 const Dwarf_Die* die,
669 const string& indent,
670 bool one_line,
671 bool qualified_names,
672 size_t where_offset,
673 unordered_set<uint64_t>& infinite_loop_guard);
674
675static string
676die_class_or_enum_flat_representation(const reader& rdr,
677 const Dwarf_Die* die,
678 const string& indent,
679 bool one_line,
680 bool qualified_names,
681 size_t where_offset,
682 unordered_set<uint64_t>& infinite_loop_guard);
683
684static string
685die_class_or_enum_flat_representation(const reader& rdr,
686 const Dwarf_Die* die,
687 const string& indent,
688 bool one_line,
689 bool qualified_names,
690 size_t where_offset);
691
692static string
693die_pretty_print_type(const reader& rdr,
694 const Dwarf_Die* die,
695 size_t where_offset,
696 unordered_set<uint64_t>& guard);
697
698static string
699die_pretty_print_decl(const reader& rdr,
700 const Dwarf_Die* die,
701 bool qualified_name,
702 bool include_fns,
703 size_t where_offset,
704 unordered_set<uint64_t>& infinite_loop_guard);
705
706static string
707die_pretty_print(reader& rdr,
708 const Dwarf_Die* die,
709 size_t where_offset,
710 unordered_set<uint64_t>& infinite_loop_guard);
711
712static void
713maybe_canonicalize_type(const type_base_sptr& t,
714 reader& rdr);
715
716static uint64_t
717get_default_array_lower_bound(translation_unit::language l);
718
719static bool
720find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
721 Dwarf_Off,
722 imported_unit_points_type::const_iterator&);
723
725build_subrange_type(reader& rdr,
726 const Dwarf_Die* die,
727 size_t where_offset,
728 bool associate_type_to_die = true);
729
730static void
731build_subranges_from_array_type_die(const reader& rdr,
732 const Dwarf_Die* die,
734 size_t where_offset,
735 bool associate_type_to_die = true);
736
738compare_dies(const reader& rdr,
739 const Dwarf_Die *l, const Dwarf_Die *r,
740 bool update_canonical_dies_on_the_fly);
741
742static bool
743compare_dies_during_canonicalization(reader& rdr,
744 const Dwarf_Die *l, const Dwarf_Die *r,
745 bool update_canonical_dies_on_the_fly);
746
747static bool
748get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
749
750static bool
751get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member);
752
753/// Get the language used to generate a given DIE.
754///
755/// @param die the DIE to consider.
756///
757/// @param lang the resulting language.
758///
759/// @return true iff the language of the DIE was found.
760static bool
761get_die_language(const Dwarf_Die *die, translation_unit::language &lang)
762{
763 Dwarf_Die cu_die;
764 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
765
766 uint64_t l = 0;
767 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
768 return false;
769
770 lang = dwarf_language_to_tu_language(l);
771 return true;
772}
773
774/// Test if a given DIE originates from a program written in the C
775/// language.
776///
777/// @param die the DIE to consider.
778///
779/// @return true iff @p die originates from a program in the C
780/// language.
781static bool
782die_is_in_c(const Dwarf_Die *die)
783{
784 translation_unit::language l = translation_unit::LANG_UNKNOWN;
785 if (!get_die_language(die, l))
786 return false;
787 return is_c_language(l);
788}
789
790/// Test if a given DIE originates from a program written in the C++
791/// language.
792///
793/// @param die the DIE to consider.
794///
795/// @return true iff @p die originates from a program in the C++
796/// language.
797static bool
798die_is_in_cplus_plus(const Dwarf_Die *die)
799{
800 translation_unit::language l = translation_unit::LANG_UNKNOWN;
801 if (!get_die_language(die, l))
802 return false;
803 return is_cplus_plus_language(l);
804}
805
806/// Test if a given DIE originates from a program written either in
807/// C or C++.
808///
809/// @param die the DIE to consider.
810///
811/// @return true iff @p die originates from a program written either in
812/// C or C++.
813static bool
814die_is_in_c_or_cplusplus(const Dwarf_Die *die)
815{
816 translation_unit::language l = translation_unit::LANG_UNKNOWN;
817 if (!get_die_language(die, l))
818 return false;
819 return (is_cplus_plus_language(l) || is_c_language(l));
820}
821
822/// Compare a symbol name against another name, possibly demangling
823/// the symbol_name before performing the comparison.
824///
825/// @param symbol_name the symbol_name to take in account.
826///
827/// @param name the second name to take in account.
828///
829/// @param demangle if true, demangle @p symbol_name and compare the
830/// result of the demangling with @p name.
831///
832/// @return true iff symbol_name equals name.
833static bool
834compare_symbol_name(const string& symbol_name,
835 const string& name,
836 bool demangle)
837{
838 if (demangle)
839 {
840 string m = demangle_cplus_mangled_name(symbol_name);
841 return m == name;
842 }
843 return symbol_name == name;
844}
845
846/// Lookup a symbol using the SysV ELF hash table.
847///
848/// Note that this function hasn't been tested. So it hasn't been
849/// debugged yet. IOW, it is not known to work. Or rather, it's
850/// almost like it's surely doesn't work ;-)
851///
852/// Use it at your own risks. :-)
853///
854///@parm env the environment we are operating from.
855///
856/// @param elf_handle the elf_handle to use.
857///
858/// @param sym_name the symbol name to look for.
859///
860/// @param ht_index the index (in the section headers table) of the
861/// hash table section to use.
862///
863/// @param sym_tab_index the index (in the section headers table) of
864/// the symbol table to use.
865///
866/// @param demangle if true, demangle @p sym_name before comparing it
867/// to names from the symbol table.
868///
869/// @param syms_found a vector of symbols found with the name @p
870/// sym_name. table.
871static bool
872lookup_symbol_from_sysv_hash_tab(const environment& env,
873 Elf* elf_handle,
874 const string& sym_name,
875 size_t ht_index,
876 size_t sym_tab_index,
877 bool demangle,
878 vector<elf_symbol_sptr>& syms_found)
879{
880 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
881 ABG_ASSERT(sym_tab_section);
882
883 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
884 ABG_ASSERT(sym_tab_data);
885
886 GElf_Shdr sheader_mem;
887 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
888 &sheader_mem);
889 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
890 ABG_ASSERT(hash_section);
891
892 // Poke at the different parts of the hash table and get them ready
893 // to be used.
894 unsigned long hash = elf_hash(sym_name.c_str());
895 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
896 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
897 size_t nb_buckets = ht_data[0];
898 size_t nb_chains = ht_data[1];
899
900 if (nb_buckets == 0)
901 // An empty hash table. Not sure if that is possible, but it
902 // would mean an empty table of exported symbols.
903 return false;
904
905 //size_t nb_chains = ht_data[1];
906 Elf32_Word* ht_buckets = &ht_data[2];
907 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
908
909 // Now do the real work.
910 size_t bucket = hash % nb_buckets;
911 size_t symbol_index = ht_buckets[bucket];
912
913 GElf_Sym symbol;
914 const char* sym_name_str;
915 size_t sym_size;
916 elf_symbol::type sym_type;
917 elf_symbol::binding sym_binding;
918 elf_symbol::visibility sym_visibility;
919 bool found = false;
920
921 do
922 {
923 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
924 sym_name_str = elf_strptr(elf_handle,
925 sym_tab_section_header->sh_link,
926 symbol.st_name);
927 if (sym_name_str
928 && compare_symbol_name(sym_name_str, sym_name, demangle))
929 {
930 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
931 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
932 sym_visibility =
933 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
934 sym_size = symbol.st_size;
935 elf_symbol::version ver;
936 if (get_version_for_symbol(elf_handle, symbol_index,
937 /*get_def_version=*/true, ver))
938 ABG_ASSERT(!ver.str().empty());
939 elf_symbol_sptr symbol_found =
941 symbol_index,
942 sym_size,
943 sym_name_str,
944 sym_type,
945 sym_binding,
946 symbol.st_shndx != SHN_UNDEF,
947 symbol.st_shndx == SHN_COMMON,
948 ver, sym_visibility);
949 syms_found.push_back(symbol_found);
950 found = true;
951 }
952 symbol_index = ht_chains[symbol_index];
953 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
954
955 return found;
956}
957
958/// Get the size of the elf class, in bytes.
959///
960/// @param elf_handle the elf handle to use.
961///
962/// @return the size computed.
963static char
964get_elf_class_size_in_bytes(Elf* elf_handle)
965{
966 char result = 0;
967 GElf_Ehdr hdr;
968
969 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
970 int c = hdr.e_ident[EI_CLASS];
971
972 switch (c)
973 {
974 case ELFCLASS32:
975 result = 4;
976 break;
977 case ELFCLASS64:
978 result = 8;
979 break;
980 default:
982 }
983
984 return result;
985}
986
987/// Get a given word of a bloom filter, referred to by the index of
988/// the word.
989///
990/// The bloom word size depends on the current elf class (32 bits for
991/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
992/// abstracts that nicely.
993///
994/// @param elf_handle the elf handle to use.
995///
996/// @param bloom_filter the bloom filter to consider.
997///
998/// @param index the index of the bloom filter to return.
999///
1000/// @return a 64 bits work containing the bloom word found at index @p
1001/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
1002/// most significant bytes of the result are going to be zero.
1003static Elf64_Xword
1004bloom_word_at(Elf* elf_handle,
1005 Elf32_Word* bloom_filter,
1006 size_t index)
1007{
1008 Elf64_Xword result = 0;
1009 GElf_Ehdr h;
1010 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
1011 int c;
1012 c = h.e_ident[EI_CLASS];
1013
1014 switch(c)
1015 {
1016 case ELFCLASS32:
1017 result = bloom_filter[index];
1018 break ;
1019 case ELFCLASS64:
1020 {
1021 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
1022 result = f[index];
1023 }
1024 break;
1025 default:
1026 abort();
1027 }
1028
1029 return result;
1030}
1031
1032/// The abstraction of the gnu elf hash table.
1033///
1034/// The members of this struct are explained at
1035/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1036/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1037struct gnu_ht
1038{
1039 size_t nb_buckets;
1040 Elf32_Word* buckets;
1041 Elf32_Word* chain;
1042 size_t first_sym_index;
1043 size_t bf_nwords;
1044 size_t bf_size;
1045 Elf32_Word* bloom_filter;
1046 size_t shift;
1047 size_t sym_count;
1048 Elf_Scn* sym_tab_section;
1049 GElf_Shdr sym_tab_section_header;
1050
1051 gnu_ht()
1052 : nb_buckets(0),
1053 buckets(0),
1054 chain(0),
1055 first_sym_index(0),
1056 bf_nwords(0),
1057 bf_size(0),
1058 bloom_filter(0),
1059 shift(0),
1060 sym_count(0),
1061 sym_tab_section(0)
1062 {}
1063}; // end struct gnu_ht
1064
1065/// Setup the members of the gnu hash table.
1066///
1067/// @param elf_handle a handle on the elf file to use.
1068///
1069/// @param ht_index the index (into the elf section headers table) of
1070/// the hash table section to use.
1071///
1072/// @param sym_tab_index the index (into the elf section headers
1073/// table) of the symbol table the gnu hash table is about.
1074///
1075/// @param ht the resulting hash table.
1076///
1077/// @return true iff the hash table @ ht could be setup.
1078static bool
1079setup_gnu_ht(Elf* elf_handle,
1080 size_t ht_index,
1081 size_t sym_tab_index,
1082 gnu_ht& ht)
1083{
1084 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1085 ABG_ASSERT(ht.sym_tab_section);
1086 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
1087 ht.sym_count =
1088 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
1089 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
1090 ABG_ASSERT(hash_section);
1091
1092 // Poke at the different parts of the hash table and get them ready
1093 // to be used.
1094 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
1095 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
1096
1097 ht.nb_buckets = ht_data[0];
1098 if (ht.nb_buckets == 0)
1099 // An empty hash table. Not sure if that is possible, but it
1100 // would mean an empty table of exported symbols.
1101 return false;
1102 ht.first_sym_index = ht_data[1];
1103 // The number of words used by the bloom filter. A size of a word
1104 // is ELFCLASS.
1105 ht.bf_nwords = ht_data[2];
1106 // The shift used by the bloom filter code.
1107 ht.shift = ht_data[3];
1108 // The data of the bloom filter proper.
1109 ht.bloom_filter = &ht_data[4];
1110 // The size of the bloom filter in 4 bytes word. This is going to
1111 // be used to index the 'bloom_filter' above, which is of type
1112 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
1113 // words.
1114 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
1115 // The buckets of the hash table.
1116 ht.buckets = ht.bloom_filter + ht.bf_size;
1117 // The chain of the hash table.
1118 ht.chain = ht.buckets + ht.nb_buckets;
1119
1120 return true;
1121}
1122
1123/// Look into the symbol tables of the underlying elf file and find
1124/// the symbol we are being asked.
1125///
1126/// This function uses the GNU hash table for the symbol lookup.
1127///
1128/// The reference of for the implementation of this function can be
1129/// found at:
1130/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
1131/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
1132///
1133/// @param elf_handle the elf handle to use.
1134///
1135/// @param sym_name the name of the symbol to look for.
1136///
1137/// @param ht_index the index of the hash table header to use.
1138///
1139/// @param sym_tab_index the index of the symbol table header to use
1140/// with this hash table.
1141///
1142/// @param demangle if true, demangle @p sym_name.
1143///
1144/// @param syms_found the vector of symbols found with the name @p
1145/// sym_name.
1146///
1147/// @return true if a symbol was actually found.
1148static bool
1149lookup_symbol_from_gnu_hash_tab(const environment& env,
1150 Elf* elf_handle,
1151 const string& sym_name,
1152 size_t ht_index,
1153 size_t sym_tab_index,
1154 bool demangle,
1155 vector<elf_symbol_sptr>& syms_found)
1156{
1157 gnu_ht ht;
1158 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
1159 return false;
1160
1161 // Now do the real work.
1162
1163 // Compute bloom hashes (GNU hash and second bloom specific hashes).
1164 size_t h1 = elf_gnu_hash(sym_name.c_str());
1165 size_t h2 = h1 >> ht.shift;
1166 // The size of one of the words used in the bloom
1167 // filter, in bits.
1168 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1169 int n = (h1 / c) % ht.bf_nwords;
1170 // The bitmask of the bloom filter has a size of either 32-bits on
1171 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1172 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1173 // type used here. When dealing with 32bits binaries, the upper
1174 // bits of the bitmask will be zero anyway.
1175 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1176
1177 // Test if the symbol is *NOT* present in this ELF file.
1178 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1179 return false;
1180
1181 size_t i = ht.buckets[h1 % ht.nb_buckets];
1182 if (i == STN_UNDEF)
1183 return false;
1184
1185 Elf32_Word stop_word, *stop_wordp;
1186 elf_symbol::version ver;
1187 GElf_Sym symbol;
1188 const char* sym_name_str;
1189 bool found = false;
1190
1191 elf_symbol::type sym_type;
1192 elf_symbol::binding sym_binding;
1193 elf_symbol::visibility sym_visibility;
1194
1195 // Let's walk the hash table and record the versions of all the
1196 // symbols which name equal sym_name.
1197 for (i = ht.buckets[h1 % ht.nb_buckets],
1198 stop_wordp = &ht.chain[i - ht.first_sym_index];
1199 i != STN_UNDEF
1200 && (stop_wordp
1201 < ht.chain + (ht.sym_count - ht.first_sym_index));
1202 ++i, ++stop_wordp)
1203 {
1204 stop_word = *stop_wordp;
1205 if ((stop_word & ~ 1)!= (h1 & ~1))
1206 // A given bucket can reference several hashes. Here we
1207 // stumbled across a hash value different from the one we are
1208 // looking for. Let's keep walking.
1209 continue;
1210
1211 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1212 i, &symbol));
1213 sym_name_str = elf_strptr(elf_handle,
1214 ht.sym_tab_section_header.sh_link,
1215 symbol.st_name);
1216 if (sym_name_str
1217 && compare_symbol_name(sym_name_str, sym_name, demangle))
1218 {
1219 // So we found a symbol (in the symbol table) that equals
1220 // sym_name. Now lets try to get its version and record it.
1221 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1222 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1223 sym_visibility =
1224 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1225
1226 if (get_version_for_symbol(elf_handle, i,
1227 /*get_def_version=*/true,
1228 ver))
1229 ABG_ASSERT(!ver.str().empty());
1230
1231 elf_symbol_sptr symbol_found =
1232 elf_symbol::create(env, i,
1233 symbol.st_size,
1234 sym_name_str,
1235 sym_type, sym_binding,
1236 symbol.st_shndx != SHN_UNDEF,
1237 symbol.st_shndx == SHN_COMMON,
1238 ver, sym_visibility);
1239 syms_found.push_back(symbol_found);
1240 found = true;
1241 }
1242
1243 if (stop_word & 1)
1244 // The last bit of the stop_word is 1. That means we need to
1245 // stop here. We reached the end of the chain of values
1246 // referenced by the hask bucket.
1247 break;
1248 }
1249 return found;
1250}
1251
1252/// Look into the symbol tables of the underlying elf file and find
1253/// the symbol we are being asked.
1254///
1255/// This function uses the elf hash table (be it the GNU hash table or
1256/// the sysv hash table) for the symbol lookup.
1257///
1258/// @param env the environment we are operating from.
1259///
1260/// @param elf_handle the elf handle to use.
1261///
1262/// @param ht_kind the kind of hash table to use. This is returned by
1263/// the function function find_hash_table_section_index.
1264///
1265/// @param ht_index the index (in the section headers table) of the
1266/// hash table section to use.
1267///
1268/// @param sym_tab_index the index (in section headers table) of the
1269/// symbol table index to use with this hash table.
1270///
1271/// @param symbol_name the name of the symbol to look for.
1272///
1273/// @param demangle if true, demangle @p sym_name.
1274///
1275/// @param syms_found the symbols that were actually found with the
1276/// name @p symbol_name.
1277///
1278/// @return true iff the function found the symbol from the elf hash
1279/// table.
1280static bool
1281lookup_symbol_from_elf_hash_tab(const environment& env,
1282 Elf* elf_handle,
1283 hash_table_kind ht_kind,
1284 size_t ht_index,
1285 size_t symtab_index,
1286 const string& symbol_name,
1287 bool demangle,
1288 vector<elf_symbol_sptr>& syms_found)
1289{
1290 if (elf_handle == 0 || symbol_name.empty())
1291 return false;
1292
1293 if (ht_kind == NO_HASH_TABLE_KIND)
1294 return false;
1295
1296 if (ht_kind == SYSV_HASH_TABLE_KIND)
1297 return lookup_symbol_from_sysv_hash_tab(env,
1298 elf_handle, symbol_name,
1299 ht_index,
1300 symtab_index,
1301 demangle,
1302 syms_found);
1303 else if (ht_kind == GNU_HASH_TABLE_KIND)
1304 return lookup_symbol_from_gnu_hash_tab(env,
1305 elf_handle, symbol_name,
1306 ht_index,
1307 symtab_index,
1308 demangle,
1309 syms_found);
1310 return false;
1311}
1312
1313/// Lookup a symbol from the symbol table directly.
1314///
1315///
1316/// @param env the environment we are operating from.
1317///
1318/// @param elf_handle the elf handle to use.
1319///
1320/// @param sym_name the name of the symbol to look up.
1321///
1322/// @param sym_tab_index the index (in the section headers table) of
1323/// the symbol table section.
1324///
1325/// @param demangle if true, demangle the names found in the symbol
1326/// table before comparing them with @p sym_name.
1327///
1328/// @param sym_name_found the actual name of the symbol found.
1329///
1330/// @param sym_type the type of the symbol found.
1331///
1332/// @param sym_binding the binding of the symbol found.
1333///
1334/// @param sym_versions the versions of the symbol found.
1335///
1336/// @return true iff the symbol was found.
1337static bool
1338lookup_symbol_from_symtab(const environment& env,
1339 Elf* elf_handle,
1340 const string& sym_name,
1341 size_t sym_tab_index,
1342 bool demangle,
1343 vector<elf_symbol_sptr>& syms_found)
1344{
1345 // TODO: read all of the symbol table, store it in memory in a data
1346 // structure that associates each symbol with its versions and in
1347 // which lookups of a given symbol is fast.
1348 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1349 ABG_ASSERT(sym_tab_section);
1350
1351 GElf_Shdr header_mem;
1352 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1353 &header_mem);
1354
1355 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1356 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1357 GElf_Sym* sym;
1358 char* name_str = 0;
1359 elf_symbol::version ver;
1360 bool found = false;
1361
1362 for (size_t i = 0; i < symcount; ++i)
1363 {
1364 GElf_Sym sym_mem;
1365 sym = gelf_getsym(symtab, i, &sym_mem);
1366 name_str = elf_strptr(elf_handle,
1367 sym_tab_header->sh_link,
1368 sym->st_name);
1369
1370 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1371 {
1372 elf_symbol::type sym_type =
1373 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1374 elf_symbol::binding sym_binding =
1375 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1376 elf_symbol::visibility sym_visibility =
1377 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1378 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1379 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1380
1381 if (get_version_for_symbol(elf_handle, i,
1382 /*get_def_version=*/sym_is_defined,
1383 ver))
1384 ABG_ASSERT(!ver.str().empty());
1385 elf_symbol_sptr symbol_found =
1386 elf_symbol::create(env, i, sym->st_size,
1387 name_str, sym_type,
1388 sym_binding, sym_is_defined,
1389 sym_is_common, ver, sym_visibility);
1390 syms_found.push_back(symbol_found);
1391 found = true;
1392 }
1393 }
1394
1395 if (found)
1396 return true;
1397
1398 return false;
1399}
1400
1401/// Look into the symbol tables of the underlying elf file and see
1402/// if we find a given symbol.
1403///
1404/// @param env the environment we are operating from.
1405///
1406/// @param symbol_name the name of the symbol to look for.
1407///
1408/// @param demangle if true, try to demangle the symbol name found in
1409/// the symbol table before comparing it to @p symbol_name.
1410///
1411/// @param syms_found the list of symbols found, with the name @p
1412/// symbol_name.
1413///
1414/// @param sym_type this is set to the type of the symbol found. This
1415/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1416/// STT_FUNC, STT_IFUNC, etc ...
1417///
1418/// Note that this parameter is set iff the function returns true.
1419///
1420/// @param sym_binding this is set to the binding of the symbol found.
1421/// This is a standard elf.h value of the symbol binding kind, that
1422/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1423///
1424/// @param symbol_versions the versions of the symbol @p symbol_name,
1425/// if it was found.
1426///
1427/// @return true iff a symbol with the name @p symbol_name was found.
1428static bool
1429lookup_symbol_from_elf(const environment& env,
1430 Elf* elf_handle,
1431 const string& symbol_name,
1432 bool demangle,
1433 vector<elf_symbol_sptr>& syms_found)
1434{
1435 size_t hash_table_index = 0, symbol_table_index = 0;
1436 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1437
1438 if (!demangle)
1439 ht_kind = find_hash_table_section_index(elf_handle,
1440 hash_table_index,
1441 symbol_table_index);
1442
1443 if (ht_kind == NO_HASH_TABLE_KIND)
1444 {
1445 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1446 return false;
1447
1448 return lookup_symbol_from_symtab(env,
1449 elf_handle,
1450 symbol_name,
1451 symbol_table_index,
1452 demangle,
1453 syms_found);
1454 }
1455
1456 return lookup_symbol_from_elf_hash_tab(env,
1457 elf_handle,
1458 ht_kind,
1459 hash_table_index,
1460 symbol_table_index,
1461 symbol_name,
1462 demangle,
1463 syms_found);
1464}
1465
1466/// Look into the symbol tables of the underlying elf file and see if
1467/// we find a given public (global or weak) symbol of function type.
1468///
1469/// @param env the environment we are operating from.
1470///
1471/// @param elf_handle the elf handle to use for the query.
1472///
1473/// @param symbol_name the function symbol to look for.
1474///
1475/// @param func_syms the vector of public functions symbols found, if
1476/// any.
1477///
1478/// @return true iff the symbol was found.
1479static bool
1480lookup_public_function_symbol_from_elf(environment& env,
1481 Elf* elf_handle,
1482 const string& symbol_name,
1483 vector<elf_symbol_sptr>& func_syms)
1484{
1485 vector<elf_symbol_sptr> syms_found;
1486 bool found = false;
1487
1488 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1489 /*demangle=*/false, syms_found))
1490 {
1491 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1492 i != syms_found.end();
1493 ++i)
1494 {
1495 elf_symbol::type type = (*i)->get_type();
1496 elf_symbol::binding binding = (*i)->get_binding();
1497
1498 if ((type == elf_symbol::FUNC_TYPE
1499 || type == elf_symbol::GNU_IFUNC_TYPE
1500 || type == elf_symbol::COMMON_TYPE)
1501 && (binding == elf_symbol::GLOBAL_BINDING
1502 || binding == elf_symbol::WEAK_BINDING))
1503 {
1504 func_syms.push_back(*i);
1505 found = true;
1506 }
1507 }
1508 }
1509
1510 return found;
1511}
1512
1513// ---------------------------------------
1514// <location expression evaluation types>
1515// ---------------------------------------
1516
1517/// An abstraction of a value representing the result of the
1518/// evaluation of a dwarf expression. This is abstraction represents
1519/// a partial view on the possible values because we are only
1520/// interested in extracting the latest and longuest constant
1521/// sub-expression of a given dwarf expression.
1522class expr_result
1523{
1524 bool is_const_;
1525 int64_t const_value_;
1526
1527public:
1528 expr_result()
1529 : is_const_(true),
1530 const_value_(0)
1531 {}
1532
1533 expr_result(bool is_const)
1534 : is_const_(is_const),
1535 const_value_(0)
1536 {}
1537
1538 explicit expr_result(int64_t v)
1539 :is_const_(true),
1540 const_value_(v)
1541 {}
1542
1543 /// @return true if the value is a constant. Otherwise, return
1544 /// false, meaning the value represents a quantity for which we need
1545 /// inferior (a running program) state to determine the value.
1546 bool
1547 is_const() const
1548 {return is_const_;}
1549
1550
1551 /// @param f a flag saying if the value is set to a constant or not.
1552 void
1553 is_const(bool f)
1554 {is_const_ = f;}
1555
1556 /// Get the current constant value iff this represents a
1557 /// constant.
1558 ///
1559 /// @param value the out parameter. Is set to the constant value of
1560 /// the @ref expr_result. This is set iff the function return true.
1561 ///
1562 ///@return true if this has a constant value, false otherwise.
1563 bool
1564 const_value(int64_t& value)
1565 {
1566 if (is_const())
1567 {
1568 value = const_value_;
1569 return true;
1570 }
1571 return false;
1572 }
1573
1574 /// Getter of the constant value of the current @ref expr_result.
1575 ///
1576 /// Note that the current @ref expr_result must be constant,
1577 /// otherwise the current process is aborted.
1578 ///
1579 /// @return the constant value of the current @ref expr_result.
1580 int64_t
1581 const_value() const
1582 {
1583 ABG_ASSERT(is_const());
1584 return const_value_;
1585 }
1586
1587 operator int64_t() const
1588 {return const_value();}
1589
1590 expr_result&
1591 operator=(const int64_t v)
1592 {
1593 const_value_ = v;
1594 return *this;
1595 }
1596
1597 bool
1598 operator==(const expr_result& o) const
1599 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1600
1601 bool
1602 operator>=(const expr_result& o) const
1603 {return const_value_ >= o.const_value_;}
1604
1605 bool
1606 operator<=(const expr_result& o) const
1607 {return const_value_ <= o.const_value_;}
1608
1609 bool
1610 operator>(const expr_result& o) const
1611 {return const_value_ > o.const_value_;}
1612
1613 bool
1614 operator<(const expr_result& o) const
1615 {return const_value_ < o.const_value_;}
1616
1617 expr_result
1618 operator+(const expr_result& v) const
1619 {
1620 expr_result r(*this);
1621 r.const_value_ += v.const_value_;
1622 r.is_const_ = r.is_const_ && v.is_const_;
1623 return r;
1624 }
1625
1626 expr_result&
1627 operator+=(int64_t v)
1628 {
1629 const_value_ += v;
1630 return *this;
1631 }
1632
1633 expr_result
1634 operator-(const expr_result& v) const
1635 {
1636 expr_result r(*this);
1637 r.const_value_ -= v.const_value_;
1638 r.is_const_ = r.is_const_ && v.is_const_;
1639 return r;
1640 }
1641
1642 expr_result
1643 operator%(const expr_result& v) const
1644 {
1645 expr_result r(*this);
1646 r.const_value_ %= v.const_value_;
1647 r.is_const_ = r.is_const_ && v.is_const();
1648 return r;
1649 }
1650
1651 expr_result
1652 operator*(const expr_result& v) const
1653 {
1654 expr_result r(*this);
1655 r.const_value_ *= v.const_value_;
1656 r.is_const_ = r.is_const_ && v.is_const();
1657 return r;
1658 }
1659
1660 expr_result
1661 operator|(const expr_result& v) const
1662 {
1663 expr_result r(*this);
1664 r.const_value_ |= v.const_value_;
1665 r.is_const_ = r.is_const_ && v.is_const_;
1666 return r;
1667 }
1668
1669 expr_result
1670 operator^(const expr_result& v) const
1671 {
1672 expr_result r(*this);
1673 r.const_value_ ^= v.const_value_;
1674 r.is_const_ = r.is_const_ && v.is_const_;
1675 return r;
1676 }
1677
1678 expr_result
1679 operator>>(const expr_result& v) const
1680 {
1681 expr_result r(*this);
1682 r.const_value_ = r.const_value_ >> v.const_value_;
1683 r.is_const_ = r.is_const_ && v.is_const_;
1684 return r;
1685 }
1686
1687 expr_result
1688 operator<<(const expr_result& v) const
1689 {
1690 expr_result r(*this);
1691 r.const_value_ = r.const_value_ << v.const_value_;
1692 r.is_const_ = r.is_const_ && v.is_const_;
1693 return r;
1694 }
1695
1696 expr_result
1697 operator~() const
1698 {
1699 expr_result r(*this);
1700 r.const_value_ = ~r.const_value_;
1701 return r;
1702 }
1703
1704 expr_result
1705 neg() const
1706 {
1707 expr_result r(*this);
1708 r.const_value_ = -r.const_value_;
1709 return r;
1710 }
1711
1712 expr_result
1713 abs() const
1714 {
1715 expr_result r = *this;
1716 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1717 return r;
1718 }
1719
1720 expr_result
1721 operator&(const expr_result& o)
1722 {
1723 expr_result r(*this);
1724 r.const_value_ &= o.const_value_;
1725 r.is_const_ = r.is_const_ && o.is_const_;
1726 return r;
1727 }
1728
1729 expr_result
1730 operator/(const expr_result& o)
1731 {
1732 expr_result r(*this);
1733 r.is_const_ = r.is_const_ && o.is_const_;
1734 return r.const_value() / o.const_value();
1735 }
1736};// class end expr_result;
1737
1738/// A class that implements a stack of @ref expr_result, to be used in
1739/// the engine evaluating DWARF expressions.
1740class expr_result_stack_type
1741{
1742 vector<expr_result> elems_;
1743
1744public:
1745
1746 expr_result_stack_type()
1747 {elems_.reserve(4);}
1748
1749 expr_result&
1750 operator[](unsigned i)
1751 {
1752 unsigned s = elems_.size();
1753 ABG_ASSERT(s > i);
1754 return elems_[s - 1 -i];
1755 }
1756
1757 const expr_result&
1758 operator[](unsigned i) const
1759 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1760
1761 unsigned
1762 size() const
1763 {return elems_.size();}
1764
1765 vector<expr_result>::reverse_iterator
1766 begin()
1767 {return elems_.rbegin();}
1768
1769 const vector<expr_result>::reverse_iterator
1770 begin() const
1771 {return const_cast<expr_result_stack_type*>(this)->begin();}
1772
1773 vector<expr_result>::reverse_iterator
1774 end()
1775 {return elems_.rend();}
1776
1777 const vector<expr_result>::reverse_iterator
1778 end() const
1779 {return const_cast<expr_result_stack_type*>(this)->end();}
1780
1781 expr_result&
1782 front()
1783 {return elems_.back();}
1784
1785 const expr_result&
1786 front() const
1787 {return const_cast<expr_result_stack_type*>(this)->front();}
1788
1789 void
1790 push_front(expr_result e)
1791 {elems_.push_back(e);}
1792
1793 expr_result
1794 pop_front()
1795 {
1796 expr_result r = front();
1797 elems_.pop_back();
1798 return r;
1799 }
1800
1801 void
1802 erase(vector<expr_result>::reverse_iterator i)
1803 {elems_.erase(--i.base());}
1804
1805 void
1806 clear()
1807 {elems_.clear();}
1808}; // end class expr_result_stack_type
1809
1810/// Abstraction of the evaluation context of a dwarf expression.
1811struct dwarf_expr_eval_context
1812{
1813 expr_result accum;
1814 expr_result_stack_type stack;
1815 // Is set to true if the result of the expression that got evaluated
1816 // is a TLS address.
1817 bool set_tls_addr;
1818
1819 dwarf_expr_eval_context()
1820 : accum(/*is_const=*/false),
1821 set_tls_addr(false)
1822 {
1823 stack.push_front(expr_result(true));
1824 }
1825
1826 void
1827 reset()
1828 {
1829 stack.clear();
1830 stack.push_front(expr_result(true));
1831 accum = expr_result(false);
1832 set_tls_addr = false;
1833 }
1834
1835 /// Set a flag to to tell that the result of the expression that got
1836 /// evaluated is a TLS address.
1837 ///
1838 /// @param f true iff the result of the expression that got
1839 /// evaluated is a TLS address, false otherwise.
1840 void
1841 set_tls_address(bool f)
1842 {set_tls_addr = f;}
1843
1844 /// Getter for the flag that tells if the result of the expression
1845 /// that got evaluated is a TLS address.
1846 ///
1847 /// @return true iff the result of the expression that got evaluated
1848 /// is a TLS address.
1849 bool
1850 set_tls_address() const
1851 {return set_tls_addr;}
1852
1853 expr_result
1854 pop()
1855 {
1856 expr_result r = stack.front();
1857 stack.pop_front();
1858 return r;
1859 }
1860
1861 void
1862 push(const expr_result& v)
1863 {stack.push_front(v);}
1864};//end class dwarf_expr_eval_context
1865
1866// ---------------------------------------
1867// </location expression evaluation types>
1868// ---------------------------------------
1869
1870class reader;
1871
1872typedef shared_ptr<reader> reader_sptr;
1873
1874/// The DWARF reader used to build the ABI corpus from debug info in
1875/// DWARF format.
1876///
1877/// This type is to be instanciated
1878/// abigail::dwarf::reader::create().
1879class reader : public elf_based_reader
1880{
1881public:
1882
1883 /// A set of containers that contains one container per kind of @ref
1884 /// die_source. This allows to associate DIEs to things, depending
1885 /// on the source of the DIE.
1886 template <typename ContainerType>
1887 class die_source_dependant_container_set
1888 {
1889 ContainerType primary_debug_info_container_;
1890 ContainerType alt_debug_info_container_;
1891 ContainerType type_unit_container_;
1892
1893 public:
1894
1895 /// Getter for the container associated to DIEs coming from a
1896 /// given @ref die_source.
1897 ///
1898 /// @param source the die_source for which we want the container.
1899 ///
1900 /// @return the container that associates DIEs coming from @p
1901 /// source to something.
1902 ContainerType&
1903 get_container(die_source source)
1904 {
1905 ContainerType *result = 0;
1906 switch (source)
1907 {
1908 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1909 result = &primary_debug_info_container_;
1910 break;
1911 case ALT_DEBUG_INFO_DIE_SOURCE:
1912 result = &alt_debug_info_container_;
1913 break;
1914 case TYPE_UNIT_DIE_SOURCE:
1915 result = &type_unit_container_;
1916 break;
1917 case NO_DEBUG_INFO_DIE_SOURCE:
1918 case NUMBER_OF_DIE_SOURCES:
1920 }
1921 return *result;
1922 }
1923
1924 /// Getter for the container associated to DIEs coming from a
1925 /// given @ref die_source.
1926 ///
1927 /// @param source the die_source for which we want the container.
1928 ///
1929 /// @return the container that associates DIEs coming from @p
1930 /// source to something.
1931 const ContainerType&
1932 get_container(die_source source) const
1933 {
1934 return const_cast<die_source_dependant_container_set*>(this)->
1935 get_container(source);
1936 }
1937
1938 /// Getter for the container associated to DIEs coming from the
1939 /// same source as a given DIE.
1940 ///
1941 /// @param rdr the DWARF reader to consider.
1942 ///
1943 /// @param die the DIE which should have the same source as the
1944 /// source of the container we want.
1945 ///
1946 /// @return the container that associates DIEs coming from the
1947 /// same source as @p die.
1948 ContainerType&
1949 get_container(const reader& rdr, const Dwarf_Die *die)
1950 {
1951 const die_source source = rdr.get_die_source(die);
1952 return get_container(source);
1953 }
1954
1955 /// Getter for the container associated to DIEs coming from the
1956 /// same source as a given DIE.
1957 ///
1958 /// @param rdr the DWARF reader to consider.
1959 ///
1960 /// @param die the DIE which should have the same source as the
1961 /// source of the container we want.
1962 ///
1963 /// @return the container that associates DIEs coming from the
1964 /// same source as @p die.
1965 const ContainerType&
1966 get_container(const reader& rdr, const Dwarf_Die *die) const
1967 {
1968 return const_cast<die_source_dependant_container_set*>(this)->
1969 get_container(rdr, die);
1970 }
1971
1972 /// Clear the container set.
1973 void
1974 clear()
1975 {
1976 primary_debug_info_container_.clear();
1977 alt_debug_info_container_.clear();
1978 type_unit_container_.clear();
1979 }
1980 }; // end die_dependant_container_set
1981
1982 /// Statistics to help for debugging purposes.
1983 struct stats
1984 {
1985 unsigned number_of_suppressed_functions = 0;
1986 unsigned number_of_suppressed_variables = 0;
1987 unsigned number_of_allowed_functions = 0;
1988 unsigned number_of_allowed_variables = 0;
1989
1990 /// Clear the statistic data members.
1991 void
1992 clear()
1993 {
1994 number_of_suppressed_functions = 0;
1995 number_of_suppressed_variables = 0;
1996 number_of_allowed_functions = 0;
1997 number_of_allowed_variables = 0;
1998 }
1999 };
2000
2001 unsigned short dwarf_version_;
2002 Dwarf_Die* cur_tu_die_;
2003 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
2004 // A set of maps (one per kind of die source) that associates a decl
2005 // string representation with the DIEs (offsets) representing that
2006 // decl.
2007 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2008 decl_die_repr_die_offsets_maps_;
2009 // A set of maps (one per kind of die source) that associates a type
2010 // string representation with the DIEs (offsets) representing that
2011 // type.
2012 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
2013 type_die_repr_die_offsets_maps_;
2014 mutable die_source_dependant_container_set<die_istring_map_type>
2015 die_qualified_name_maps_;
2016 mutable die_source_dependant_container_set<die_istring_map_type>
2017 die_pretty_repr_maps_;
2018 mutable die_source_dependant_container_set<die_istring_map_type>
2019 die_pretty_type_repr_maps_;
2020 // A set of maps (one per kind of die source) that associates the
2021 // offset of a decl die to its corresponding decl artifact.
2022 mutable die_source_dependant_container_set<die_artefact_map_type>
2023 decl_die_artefact_maps_;
2024 // A set of maps (one per kind of die source) that associates the
2025 // offset of a type die to its corresponding type artifact.
2026 mutable die_source_dependant_container_set<die_artefact_map_type>
2027 type_die_artefact_maps_;
2028 /// A set of vectors (one per kind of die source) that associates
2029 /// the offset of a type DIE to the offset of its canonical DIE.
2030 mutable die_source_dependant_container_set<offset_offset_map_type>
2031 canonical_type_die_offsets_;
2032 /// A set of vectors (one per kind of die source) that associates
2033 /// the offset of a decl DIE to the offset of its canonical DIE.
2034 mutable die_source_dependant_container_set<offset_offset_map_type>
2035 canonical_decl_die_offsets_;
2036 /// A map that associates a function type representations to
2037 /// function types, inside a translation unit.
2038 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
2039 /// A map that associates a pair of DIE offsets to the result of the
2040 /// comparison of that pair.
2041 mutable std::unordered_map<std::pair<offset_type,offset_type>,
2043 dwarf_offset_pair_hash> die_comparison_results_;
2044 // The set of types pair that have been canonical-type-propagated.
2045 mutable offset_pair_set_type propagated_types_;
2046 die_class_or_union_map_type die_wip_classes_map_;
2047 die_class_or_union_map_type alternate_die_wip_classes_map_;
2048 die_class_or_union_map_type type_unit_die_wip_classes_map_;
2049 die_function_type_map_type die_wip_function_types_map_;
2050 die_function_type_map_type alternate_die_wip_function_types_map_;
2051 die_function_type_map_type type_unit_die_wip_function_types_map_;
2052 die_function_decl_map_type die_function_with_no_symbol_map_;
2053 vector<type_base_sptr> types_to_canonicalize_;
2054 string_classes_or_unions_map decl_only_classes_map_;
2055 string_enums_map decl_only_enums_map_;
2056 die_tu_map_type die_tu_map_;
2057 translation_unit_sptr cur_tu_;
2058 scope_decl_sptr nil_scope_;
2059 scope_stack_type scope_stack_;
2060 offset_offset_map_type primary_die_parent_map_;
2061 // A map that associates each tu die to a vector of unit import
2062 // points, in the main debug info
2063 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
2064 // A map that associates each tu die to a vector of unit import
2065 // points, in the alternate debug info
2066 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
2067 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
2068 // A DIE -> parent map for DIEs coming from the alternate debug info
2069 // file.
2070 offset_offset_map_type alternate_die_parent_map_;
2071 offset_offset_map_type type_section_die_parent_map_;
2072 list<var_decl_sptr> var_decls_to_add_;
2073#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
2074 bool debug_die_canonicalization_is_on_;
2075 bool use_canonical_die_comparison_;
2076#endif
2077 mutable size_t compare_count_;
2078 mutable size_t canonical_propagated_count_;
2079 mutable size_t cancelled_propagation_count_;
2080 mutable optional<bool> leverage_dwarf_factorization_;
2081 mutable stats stats_;
2082
2083protected:
2084
2085 reader() = delete;
2086
2087 /// Constructor of reader.
2088 ///
2089 /// @param elf_path the path to the elf file the context is to be
2090 /// used for.
2091 ///
2092 /// @param debug_info_root_paths a vector of pointers to the path to
2093 /// the root directory under which the debug info is to be found for
2094 /// @p elf_path. Leave this empty if the debug info is not in a
2095 /// split file.
2096 ///
2097 /// @param environment the environment used by the current context.
2098 /// This environment contains resources needed by the DWARF reader and by
2099 /// the types and declarations that are to be created later. Note
2100 /// that ABI artifacts that are to be compared all need to be
2101 /// created within the same environment.
2102 ///
2103 /// Please also note that the life time of this environment object
2104 /// must be greater than the life time of the resulting @ref
2105 /// reader the context uses resources that are allocated in
2106 /// the environment.
2107 ///
2108 /// @param load_all_types if set to false only the types that are
2109 /// reachable from publicly exported declarations (of functions and
2110 /// variables) are read. If set to true then all types found in the
2111 /// debug information are loaded.
2112 ///
2113 /// @param linux_kernel_mode if set to true, then consider the special
2114 /// linux kernel symbol tables when determining if a symbol is
2115 /// exported or not.
2116 reader(const string& elf_path,
2117 const vector<char**>& debug_info_root_paths,
2118 environment& environment,
2119 bool load_all_types,
2120 bool linux_kernel_mode)
2121 : elf_based_reader(elf_path,
2122 debug_info_root_paths,
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<char**>& debug_info_root_paths,
2219 bool load_all_types,
2220 bool linux_kernel_mode)
2221 {
2222 elf_based_reader::initialize(elf_path, debug_info_root_paths);
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<char**>& 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 {
2271 status = STATUS_UNKNOWN;
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 ...
2296 ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2297 && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
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_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.
3291 ///
3292 /// If the name of the DIE was already computed before just return
3293 /// that name from a cache. Otherwise, build the name, cache it and
3294 /// return it.
3295 ///
3296 /// @param die the DIE to consider.
3297 ///
3298 /// @param where_offset where in the DIE stream we logically are.
3299 ///
3300 /// @return the interned string representing the qualified name of
3301 /// @p die.
3302 interned_string
3303 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3304 {
3305 return const_cast<reader*>(this)->
3306 get_die_qualified_name(die, where_offset);
3307 }
3308
3309 /// Get the qualified name of a given DIE which is considered to be
3310 /// the DIE for a type.
3311 ///
3312 /// For instance, for a DW_TAG_subprogram DIE, this function
3313 /// computes the name of the function *type* that corresponds to the
3314 /// function.
3315 ///
3316 /// If the name of the DIE was already computed before just return
3317 /// that name from a cache. Otherwise, build the name, cache it and
3318 /// return it.
3319 ///
3320 /// @param die the DIE to consider.
3321 ///
3322 /// @param where_offset where in the DIE stream we logically are.
3323 ///
3324 /// @param guard the set of DIE offsets of the stack of DIEs
3325 /// involved in the construction of the qualified name of the type.
3326 /// This set is used to detect (and avoid) cycles in the stack of
3327 /// DIEs that is going to be walked to compute the qualified type
3328 /// name.
3329 ///
3330 /// @return the interned string representing the qualified name of
3331 /// @p die.
3332 interned_string
3333 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset,
3334 unordered_set<uint64_t>& guard) const
3335 {
3336 ABG_ASSERT(die);
3337
3338 // The name of the translation unit die is "".
3339 if (die == cur_tu_die())
3340 return env().intern("");
3341
3343 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3344 die);
3345
3346 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3347 die_istring_map_type::const_iterator i =
3348 map.find(die_offset);
3349
3350 if (i == map.end())
3351 {
3352 reader& rdr = *const_cast<reader*>(this);
3353 string qualified_name;
3354 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3355 if ((tag == DW_TAG_structure_type
3356 || tag == DW_TAG_class_type
3357 || tag == DW_TAG_union_type)
3358 && die_is_anonymous(die))
3359 qualified_name =
3360 die_class_or_enum_flat_representation(*this, die, /*indent=*/"",
3361 /*one_line=*/true,
3362 /*qualified_name=*/false,
3363 where_offset,
3364 guard);
3365 else
3366 qualified_name = die_qualified_type_name(rdr, die,
3367 where_offset,
3368 guard);
3369
3370 interned_string istr = env().intern(qualified_name);
3371 map[die_offset] = istr;
3372 return istr;
3373 }
3374
3375 return i->second;
3376 }
3377
3378 /// Get the pretty representation of a DIE that represents a type.
3379 ///
3380 /// For instance, for the DW_TAG_subprogram, this function computes
3381 /// the pretty representation of the type of the function, not the
3382 /// pretty representation of the function declaration.
3383 ///
3384 /// Once the pretty representation is computed, it's stored in a
3385 /// cache. Subsequent invocations of this function on the same DIE
3386 /// will yield the cached name.
3387 ///
3388 /// @param die the DIE to consider.
3389 ///
3390 /// @param where_offset where in the DIE stream we logically are.
3391 ///
3392 /// @param guard the set of DIE offsets of the stack of DIEs
3393 /// involved in the construction of the pretty representation of the
3394 /// type. This set is used to detect (and avoid) cycles in the
3395 /// stack of DIEs that is going to be walked to compute the
3396 /// pretty representation.
3397 ///
3398 /// @return the interned_string that represents the pretty
3399 /// representation.
3400 interned_string
3401 get_die_pretty_type_representation(const Dwarf_Die *die,
3402 size_t where_offset,
3403 unordered_set<uint64_t>& guard) const
3404 {
3405 ABG_ASSERT(die);
3407 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3408 die);
3409
3410 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3411 die_istring_map_type::const_iterator i = map.find(die_offset);
3412
3413 if (i == map.end())
3414 {
3415 reader& rdr = *const_cast<reader*>(this);
3416 string pretty_representation =
3417 die_pretty_print_type(rdr, die, where_offset, guard);
3418 interned_string istr = env().intern(pretty_representation);
3419 map[die_offset] = istr;
3420 return istr;
3421 }
3422
3423 return i->second;
3424 }
3425
3426
3427 /// Get the pretty representation of a DIE that represents a type.
3428 ///
3429 /// For instance, for the DW_TAG_subprogram, this function computes
3430 /// the pretty representation of the type of the function, not the
3431 /// pretty representation of the function declaration.
3432 ///
3433 /// Once the pretty representation is computed, it's stored in a
3434 /// cache. Subsequent invocations of this function on the same DIE
3435 /// will yield the cached name.
3436 ///
3437 /// @param die the DIE to consider.
3438 ///
3439 /// @param where_offset where in the DIE stream we logically are.
3440 ///
3441 /// @return the interned_string that represents the pretty
3442 /// representation.
3443 interned_string
3444 get_die_pretty_type_representation(const Dwarf_Die *die,
3445 size_t where_offset) const
3446 {
3447 unordered_set<uint64_t> guard;
3448 return get_die_pretty_type_representation(die, where_offset, guard);
3449 }
3450
3451 /// Get the pretty representation of a DIE.
3452 ///
3453 /// Once the pretty representation is computed, it's stored in a
3454 /// cache. Subsequent invocations of this function on the same DIE
3455 /// will yield the cached name.
3456 ///
3457 /// @param die the DIE to consider.
3458 ///
3459 /// @param where_offset where in the DIE stream we logically are.
3460 ///
3461 /// @param guard the set of DIE offsets of the stack of DIEs
3462 /// involved in the construction of the pretty representation of the
3463 /// type. This set is used to detect (and avoid) cycles in the
3464 /// stack of DIEs that is going to be walked to compute the
3465 /// pretty representation.
3466 ///
3467 /// @return the interned_string that represents the pretty
3468 /// representation.
3469 interned_string
3470 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset,
3471 unordered_set<uint64_t>& guard) const
3472 {
3473 ABG_ASSERT(die);
3474
3476 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3477 die);
3478
3479 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3480 die_istring_map_type::const_iterator i = map.find(die_offset);
3481
3482 if (i == map.end())
3483 {
3484 reader& rdr = *const_cast<reader*>(this);
3485 string pretty_representation =
3486 die_pretty_print(rdr, die, where_offset, guard);
3487 interned_string istr = env().intern(pretty_representation);
3488 map[die_offset] = istr;
3489 return istr;
3490 }
3491
3492 return i->second;
3493 }
3494
3495 /// Get the pretty representation of a DIE.
3496 ///
3497 /// Once the pretty representation is computed, it's stored in a
3498 /// cache. Subsequent invocations of this function on the same DIE
3499 /// will yield the cached name.
3500 ///
3501 /// @param die the DIE to consider.
3502 ///
3503 /// @param where_offset where in the DIE stream we logically are.
3504 ///
3505 /// @return the interned_string that represents the pretty
3506 /// representation.
3507 interned_string
3508 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3509 {
3510 unordered_set<uint64_t> guard;
3511 return get_die_pretty_representation(die, where_offset, guard);
3512 }
3513
3514 /// Lookup the artifact that was built to represent a type that has
3515 /// the same pretty representation as the type denoted by a given
3516 /// DIE.
3517 ///
3518 /// Note that the DIE must have previously been associated with the
3519 /// artifact using the functions associate_die_to_decl or
3520 /// associate_die_to_type.
3521 ///
3522 /// Also, note that the scope of the lookup is the current ABI
3523 /// corpus.
3524 ///
3525 /// @param die the DIE to consider.
3526 ///
3527 /// @param where_offset where in the DIE stream we logically are.
3528 ///
3529 /// @return the type artifact found.
3531 lookup_type_artifact_from_die(Dwarf_Die *die) const
3532 {
3533 type_or_decl_base_sptr artifact =
3534 lookup_artifact_from_die(die, /*type_as_die=*/true);
3535 if (function_decl_sptr fn = is_function_decl(artifact))
3536 return fn->get_type();
3537 return artifact;
3538 }
3539
3540 /// Lookup the artifact that was built to represent a type or a
3541 /// declaration that has the same pretty representation as the type
3542 /// denoted by a given DIE.
3543 ///
3544 /// Note that the DIE must have previously been associated with the
3545 /// artifact using the functions associate_die_to_decl or
3546 /// associate_die_to_type.
3547 ///
3548 /// Also, note that the scope of the lookup is the current ABI
3549 /// corpus.
3550 ///
3551 /// @param die the DIE to consider.
3552 ///
3553 /// @param where_offset where in the DIE stream we logically are.
3554 ///
3555 /// @param die_as_type if true, it means the DIE is to be considered
3556 /// as a type.
3557 ///
3558 /// @return the artifact found.
3560 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3561 {
3562 Dwarf_Die equiv_die;
3563 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3564 return type_or_decl_base_sptr();
3565
3566 const die_artefact_map_type& m =
3567 die_as_type
3568 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3569 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3570
3571 size_t die_offset = dwarf_dieoffset(&equiv_die);
3572 die_artefact_map_type::const_iterator i = m.find(die_offset);
3573
3574 if (i == m.end())
3575 return type_or_decl_base_sptr();
3576 return i->second;
3577 }
3578
3579 /// Lookup the artifact that was built to represent a type or a
3580 /// declaration that has the same pretty representation as the type
3581 /// denoted by the offset of a given DIE.
3582 ///
3583 /// Note that the DIE must have previously been associated with the
3584 /// artifact using either associate_die_to_decl or
3585 /// associate_die_to_type.
3586 ///
3587 /// Also, note that the scope of the lookup is the current ABI
3588 /// corpus.
3589 ///
3590 /// @param die the DIE to consider.
3591 ///
3592 /// @param where_offset where in the DIE stream we logically are.
3593 ///
3594 /// @param die_as_type if true, it means the DIE is to be considered
3595 /// as a type.
3596 ///
3597 /// @return the artifact found.
3599 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3600 die_source source,
3601 bool die_as_type = false) const
3602 {
3603 const die_artefact_map_type& m =
3604 die_as_type
3605 ? type_die_artefact_maps().get_container(source)
3606 : decl_die_artefact_maps().get_container(source);
3607
3608 die_artefact_map_type::const_iterator i = m.find(die_offset);
3609 if (i == m.end())
3610 return type_or_decl_base_sptr();
3611 return i->second;
3612 }
3613
3614 /// Check if we can assume the One Definition Rule[1] to be relevant
3615 /// for the current translation unit.
3616 ///
3617 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3618 ///
3619 /// At the moment this returns true if the current translation unit
3620 /// is in C++ language. In that case, it's relevant to assume that
3621 /// we use optimizations based on the ODR.
3622 bool
3623 odr_is_relevant() const
3624 {return odr_is_relevant(cur_transl_unit()->get_language());}
3625
3626 /// Check if we can assume the One Definition Rule[1] to be relevant
3627 /// for a given language.
3628 ///
3629 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3630 ///
3631 /// At the moment this returns true if the language considered
3632 /// is C++, Java or Ada.
3633 bool
3635 {
3636 return (is_cplus_plus_language(l)
3637 || is_java_language(l)
3638 || is_ada_language(l));
3639 }
3640
3641 /// Check if we can assume the One Definition Rule to be relevant
3642 /// for a given DIE.
3643 ///
3644 /// @param die the DIE to consider.
3645 ///
3646 /// @return true if the ODR is relevant for @p die.
3647 bool
3648 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3649 {
3650 Dwarf_Die die;
3651 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3652 die_offset, &die));
3653 return odr_is_relevant(&die);
3654 }
3655
3656 /// Check if we can assume the One Definition Rule to be relevant
3657 /// for a given DIE.
3658 ///
3659 /// @param die the DIE to consider.
3660 ///
3661 /// @return true if the ODR is relevant for @p die.
3662 bool
3663 odr_is_relevant(const Dwarf_Die *die) const
3664 {
3666 if (!get_die_language(die, lang))
3667 return odr_is_relevant();
3668
3669 return odr_is_relevant(lang);
3670 }
3671
3672 /// Getter for the maps set that associates a decl DIE offset to an
3673 /// artifact.
3674 ///
3675 /// @return the maps set that associates a decl DIE offset to an
3676 /// artifact.
3677 die_source_dependant_container_set<die_artefact_map_type>&
3678 decl_die_artefact_maps()
3679 {return decl_die_artefact_maps_;}
3680
3681 /// Getter for the maps set that associates a decl DIE offset to an
3682 /// artifact.
3683 ///
3684 /// @return the maps set that associates a decl DIE offset to an
3685 /// artifact.
3686 const die_source_dependant_container_set<die_artefact_map_type>&
3687 decl_die_artefact_maps() const
3688 {return decl_die_artefact_maps_;}
3689
3690 /// Getter for the maps set that associates a type DIE offset to an
3691 /// artifact.
3692 ///
3693 /// @return the maps set that associates a type DIE offset to an
3694 /// artifact.
3695 die_source_dependant_container_set<die_artefact_map_type>&
3696 type_die_artefact_maps()
3697 {return type_die_artefact_maps_;}
3698
3699 /// Getter for the maps set that associates a type DIE offset to an
3700 /// artifact.
3701 ///
3702 /// @return the maps set that associates a type DIE offset to an
3703 /// artifact.
3704 const die_source_dependant_container_set<die_artefact_map_type>&
3705 type_die_artefact_maps() const
3706 {return type_die_artefact_maps_;}
3707
3708 /// Getter of the maps that associates function type representations
3709 /// to function types, inside a translation unit.
3710 ///
3711 /// @return the maps that associates function type representations
3712 /// to function types, inside a translation unit.
3714 per_tu_repr_to_fn_type_maps()
3715 {return per_tu_repr_to_fn_type_maps_;}
3716
3717 /// Getter of the maps that associates function type representations
3718 /// to function types, inside a translation unit.
3719 ///
3720 /// @return the maps that associates function type representations
3721 /// to function types, inside a translation unit.
3723 per_tu_repr_to_fn_type_maps() const
3724 {return per_tu_repr_to_fn_type_maps_;}
3725
3726 /// Associate the representation of a function type DIE to a given
3727 /// function type, inside the current translation unit.
3728 ///
3729 /// @param die the DIE to associate to the function type, using its
3730 /// representation.
3731 ///
3732 /// @param fn_type the function type to associate to @p die.
3733 void
3734 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3735 const function_type_sptr &fn_type)
3736 {
3737 if (!die_is_function_type(die))
3738 return;
3739
3740 interned_string repr =
3741 get_die_pretty_type_representation(die, /*where=*/0);
3742 ABG_ASSERT(!repr.empty());
3743
3744 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3745 }
3746
3747 /// Lookup the function type associated to a given function type
3748 /// DIE, in the current translation unit.
3749 ///
3750 /// @param die the DIE of function type to consider.
3751 ///
3752 /// @return the @ref function_type_sptr associated to @p die, or nil
3753 /// of no function_type is associated to @p die.
3755 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3756 {
3757 if (!die_is_function_type(die))
3758 return function_type_sptr();
3759
3760 interned_string repr = die_name(die).empty() ?
3761 get_die_pretty_type_representation(die, /*where=*/0)
3762 : get_die_pretty_representation(die, /*where=*/0);
3763 ABG_ASSERT(!repr.empty());
3764
3765 istring_fn_type_map_type::const_iterator i =
3766 per_tu_repr_to_fn_type_maps().find(repr);
3767
3768 if (i == per_tu_repr_to_fn_type_maps().end())
3769 return function_type_sptr();
3770
3771 return i->second;
3772 }
3773
3774 /// Set the canonical DIE offset of a given DIE.
3775 ///
3776 /// @param canonical_dies the vector that holds canonical DIEs.
3777 ///
3778 /// @param die_offset the offset of the DIE to set the canonical DIE
3779 /// for.
3780 ///
3781 /// @param canonical_die_offset the canonical DIE offset to
3782 /// associate to @p die_offset.
3783 void
3784 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3785 Dwarf_Off die_offset,
3786 Dwarf_Off canonical_die_offset) const
3787 {
3788 canonical_dies[die_offset] = canonical_die_offset;}
3789
3790 /// Set the canonical DIE offset of a given DIE.
3791 ///
3792 ///
3793 /// @param die_offset the offset of the DIE to set the canonical DIE
3794 /// for.
3795 ///
3796 /// @param source the source of the DIE denoted by @p die_offset.
3797 ///
3798 /// @param canonical_die_offset the canonical DIE offset to
3799 /// associate to @p die_offset.
3800 ///
3801 /// @param die_as_type if true, it means that @p die_offset has to
3802 /// be considered as a type.
3803 void
3804 set_canonical_die_offset(Dwarf_Off die_offset,
3805 die_source source,
3806 Dwarf_Off canonical_die_offset,
3807 bool die_as_type) const
3808 {
3809 offset_offset_map_type &canonical_dies =
3810 die_as_type
3811 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3812 get_container(source)
3813 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3814 get_container(source);
3815
3816 set_canonical_die_offset(canonical_dies,
3817 die_offset,
3818 canonical_die_offset);
3819 }
3820
3821 /// Set the canonical DIE offset of a given DIE.
3822 ///
3823 ///
3824 /// @param die the DIE to set the canonical DIE for.
3825 ///
3826 /// @param canonical_die_offset the canonical DIE offset to
3827 /// associate to @p die_offset.
3828 ///
3829 /// @param die_as_type if true, it means that @p die has to be
3830 /// considered as a type.
3831 void
3832 set_canonical_die_offset(const Dwarf_Die *die,
3833 Dwarf_Off canonical_die_offset,
3834 bool die_as_type) const
3835 {
3836 const die_source source = get_die_source(die);
3837
3838 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3839
3840 set_canonical_die_offset(die_offset, source,
3841 canonical_die_offset,
3842 die_as_type);
3843 }
3844
3845 /// Get the canonical DIE offset of a given DIE.
3846 ///
3847 /// @param canonical_dies the vector that contains canonical DIES.
3848 ///
3849 /// @param die_offset the offset of the DIE to consider.
3850 ///
3851 /// @return the canonical of the DIE denoted by @p die_offset, or
3852 /// zero if no canonical DIE was found.
3853 Dwarf_Off
3854 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3855 Dwarf_Off die_offset) const
3856 {
3857 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3858 if (it == canonical_dies.end())
3859 return 0;
3860 return it->second;
3861 }
3862
3863 /// Get the canonical DIE offset of a given DIE.
3864 ///
3865 /// @param die_offset the offset of the DIE to consider.
3866 ///
3867 /// @param source the source of the DIE denoted by @p die_offset.
3868 ///
3869 /// @param die_as_type if true, it means that @p is to be considered
3870 /// as a type DIE.
3871 ///
3872 /// @return the canonical of the DIE denoted by @p die_offset, or
3873 /// zero if no canonical DIE was found.
3874 Dwarf_Off
3875 get_canonical_die_offset(Dwarf_Off die_offset,
3876 die_source source,
3877 bool die_as_type) const
3878 {
3879 offset_offset_map_type &canonical_dies =
3880 die_as_type
3881 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3882 get_container(source)
3883 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3884 get_container(source);
3885
3886 return get_canonical_die_offset(canonical_dies, die_offset);
3887 }
3888
3889 /// Erase the canonical type of a given DIE.
3890 ///
3891 /// @param die_offset the offset of the DIE to consider.
3892 ///
3893 /// @param source the source of the canonical type.
3894 ///
3895 /// @param die_as_type if true, it means that @p is to be considered
3896 /// as a type DIE.
3897 ///
3898 /// @return the canonical of the DIE denoted by @p die_offset, or
3899 /// zero if no canonical DIE was found and erased..
3900 bool
3901 erase_canonical_die_offset(Dwarf_Off die_offset,
3902 die_source source,
3903 bool die_as_type) const
3904 {
3905 offset_offset_map_type &canonical_dies =
3906 die_as_type
3907 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3908 get_container(source)
3909 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3910 get_container(source);
3911
3912 return canonical_dies.erase(die_offset);
3913 }
3914
3915
3916 /// Associate a DIE (representing a type) to the type that it
3917 /// represents.
3918 ///
3919 /// @param die the DIE to consider.
3920 ///
3921 /// @param type the type to associate the DIE to.
3922 ///
3923 /// @param where_offset where in the DIE stream we logically are.
3924 void
3925 associate_die_to_type(const Dwarf_Die *die,
3926 type_base_sptr type,
3927 size_t where)
3928 {
3929 if (!type)
3930 return;
3931
3932 Dwarf_Die equiv_die;
3933 if (!get_or_compute_canonical_die(die, equiv_die, where,
3934 /*die_as_type=*/true))
3935 return;
3936
3938 type_die_artefact_maps().get_container(*this, &equiv_die);
3939
3940 size_t die_offset = dwarf_dieoffset(&equiv_die);
3941 m[die_offset] = type;
3942 }
3943
3944 /// Lookup the type associated to a given DIE.
3945 ///
3946 /// Note that the DIE must have been associated to type by a
3947 /// previous invocation of the function
3948 /// reader::associate_die_to_type().
3949 ///
3950 /// @param die the DIE to consider.
3951 ///
3952 /// @return the type associated to the DIE or NULL if no type is
3953 /// associated to the DIE.
3954 type_base_sptr
3955 lookup_type_from_die(const Dwarf_Die* die) const
3956 {
3957 type_or_decl_base_sptr artifact =
3958 lookup_artifact_from_die(die, /*die_as_type=*/true);
3959 if (function_decl_sptr fn = is_function_decl(artifact))
3960 return fn->get_type();
3961 return is_type(artifact);
3962 }
3963
3964 /// Lookup the type associated to a DIE at a given offset, from a
3965 /// given source.
3966 ///
3967 /// Note that the DIE must have been associated to type by a
3968 /// previous invocation of the function
3969 /// reader::associate_die_to_type().
3970 ///
3971 /// @param die_offset the offset of the DIE to consider.
3972 ///
3973 /// @param source the source of the DIE to consider.
3974 ///
3975 /// @return the type associated to the DIE or NULL if no type is
3976 /// associated to the DIE.
3977 type_base_sptr
3978 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3979 {
3980 type_base_sptr result;
3981 const die_artefact_map_type& m =
3982 type_die_artefact_maps().get_container(source);
3983 die_artefact_map_type::const_iterator i = m.find(die_offset);
3984 if (i != m.end())
3985 {
3986 if (function_decl_sptr fn = is_function_decl(i->second))
3987 return fn->get_type();
3988 result = is_type(i->second);
3989 }
3990
3991 if (!result)
3992 {
3993 // Maybe we are looking for a class type being constructed?
3994 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3995 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3996
3997 if (i != m.end())
3998 result = i->second;
3999 }
4000
4001 if (!result)
4002 {
4003 // Maybe we are looking for a function type being constructed?
4005 die_wip_function_types_map(source);
4006 die_function_type_map_type::const_iterator i = m.find(die_offset);
4007
4008 if (i != m.end())
4009 result = i->second;
4010 }
4011
4012 return result;
4013 }
4014
4015 /// Getter of a map that associates a die that represents a
4016 /// class/struct with the declaration of the class, while the class
4017 /// is being constructed.
4018 ///
4019 /// @param source where the DIE is from.
4020 ///
4021 /// @return the map that associates a DIE to the class that is being
4022 /// built.
4024 die_wip_classes_map(die_source source) const
4025 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
4026
4027 /// Getter of a map that associates a die that represents a
4028 /// class/struct with the declaration of the class, while the class
4029 /// is being constructed.
4030 ///
4031 /// @param source where the DIE comes from.
4032 ///
4033 /// @return the map that associates a DIE to the class that is being
4034 /// built.
4036 die_wip_classes_map(die_source source)
4037 {
4038 switch (source)
4039 {
4040 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4041 break;
4042 case ALT_DEBUG_INFO_DIE_SOURCE:
4043 return alternate_die_wip_classes_map_;
4044 case TYPE_UNIT_DIE_SOURCE:
4045 return type_unit_die_wip_classes_map_;
4046 case NO_DEBUG_INFO_DIE_SOURCE:
4047 case NUMBER_OF_DIE_SOURCES:
4049 }
4050 return die_wip_classes_map_;
4051 }
4052
4053 /// Getter for a map that associates a die (that represents a
4054 /// function type) whith a function type, while the function type is
4055 /// being constructed (WIP == work in progress).
4056 ///
4057 /// @param source where the DIE comes from.n
4058 ///
4059 /// @return the map of wip function types.
4061 die_wip_function_types_map(die_source source) const
4062 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
4063
4064 /// Getter for a map that associates a die (that represents a
4065 /// function type) whith a function type, while the function type is
4066 /// being constructed (WIP == work in progress).
4067 ///
4068 /// @param source where DIEs of the map come from.
4069 ///
4070 /// @return the map of wip function types.
4072 die_wip_function_types_map(die_source source)
4073 {
4074 switch (source)
4075 {
4076 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4077 break;
4078 case ALT_DEBUG_INFO_DIE_SOURCE:
4079 return alternate_die_wip_function_types_map_;
4080 case TYPE_UNIT_DIE_SOURCE:
4081 return type_unit_die_wip_function_types_map_;
4082 case NO_DEBUG_INFO_DIE_SOURCE:
4083 case NUMBER_OF_DIE_SOURCES:
4085 }
4086 return die_wip_function_types_map_;
4087 }
4088
4089 /// Getter for a map that associates a die with a function decl
4090 /// which has a linkage name but no elf symbol yet.
4091 ///
4092 /// This is to fixup function decls with linkage names, but with no
4093 /// link to their underlying elf symbol. There are some DIEs like
4094 /// that in DWARF sometimes, especially when the compiler optimizes
4095 /// stuff aggressively.
4097 die_function_decl_with_no_symbol_map()
4098 {return die_function_with_no_symbol_map_;}
4099
4100 /// Return true iff a given offset is for the DIE of a class that is
4101 /// being built, but that is not fully built yet. WIP == "work in
4102 /// progress".
4103 ///
4104 /// @param offset the DIE offset to consider.
4105 ///
4106 /// @param source where the DIE of the map come from.
4107 ///
4108 /// @return true iff @p offset is the offset of the DIE of a class
4109 /// that is being currently built.
4110 bool
4111 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
4112 {
4113 die_class_or_union_map_type::const_iterator i =
4114 die_wip_classes_map(source).find(offset);
4115 return (i != die_wip_classes_map(source).end());
4116 }
4117
4118 /// Return true iff a given offset is for the DIE of a function type
4119 /// that is being built at the moment, but is not fully built yet.
4120 /// WIP == work in progress.
4121 ///
4122 /// @param offset DIE offset to consider.
4123 ///
4124 /// @param source where the DIE comes from.
4125 ///
4126 /// @return true iff @p offset is the offset of the DIE of a
4127 /// function type that is being currently built.
4128 bool
4129 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
4130 {
4131 die_function_type_map_type::const_iterator i =
4132 die_wip_function_types_map(source).find(offset);
4133 return (i != die_wip_function_types_map(source).end());
4134 }
4135
4136 /// Sometimes, a data member die can erroneously have an empty name as
4137 /// a result of a bug of the DWARF emitter.
4138 ///
4139 /// This is what happens in
4140 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
4141 ///
4142 /// In that case, this function constructs an artificial name for that
4143 /// data member. The pattern of the name is as follows:
4144 ///
4145 /// "unnamed-@-<location>".
4146 ///
4147 ///location is either the value of the data member location of the
4148 ///data member if it has one or concatenation of its source location
4149 ///if it has none. If no location can be calculated then the function
4150 ///returns the empty string.
4151 string
4152 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
4153 {
4154 string result;
4155 // Let's make sure we are looking at a data member with an empty
4156 // name ...
4157 if (!die
4158 || dwarf_tag(die) != DW_TAG_member
4159 || !die_name(die).empty())
4160 return result;
4161
4162 // ... and yet, it's not an anonymous data member (aka unnamed
4163 // field) as described in
4164 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
4165 if (die_is_anonymous_data_member(die))
4166 return result;
4167
4168 // If we come this far, it means we are looking at a buggy data
4169 // member with no name. Let's build a name for it so that it can be
4170 // addressed.
4171 int64_t offset_in_bits = 0;
4172 bool has_offset = die_member_offset(*this, die, offset_in_bits);
4173 location loc;
4174 if (!has_offset)
4175 {
4176 loc = die_location(*this, die);
4177 if (!loc)
4178 return result;
4179 }
4180
4181 std::ostringstream o;
4182 o << "unnamed-dm-@-";
4183 if (has_offset)
4184 o << "offset-" << offset_in_bits << "bits";
4185 else
4186 o << "loc-" << loc.expand();
4187
4188 return o.str();
4189 }
4190
4191 /// Getter for the map of declaration-only classes that are to be
4192 /// resolved to their definition classes by the end of the corpus
4193 /// loading.
4194 ///
4195 /// @return a map of string -> vector of classes where the key is
4196 /// the fully qualified name of the class and the value is the
4197 /// vector of declaration-only class.
4199 declaration_only_classes() const
4200 {return decl_only_classes_map_;}
4201
4202 /// Getter for the map of declaration-only classes that are to be
4203 /// resolved to their definition classes by the end of the corpus
4204 /// loading.
4205 ///
4206 /// @return a map of string -> vector of classes where the key is
4207 /// the fully qualified name of the class and the value is the
4208 /// vector of declaration-only class.
4210 declaration_only_classes()
4211 {return decl_only_classes_map_;}
4212
4213 /// If a given artifact is a class, union or enum that is
4214 /// declaration-only, then stash it on the side so that at the end
4215 /// of the construction of the IR for the ABI corpus, we can resolve
4216 /// that declaration to its definition.
4217 ///
4218 /// @parameter t the ABI artifact to consider.
4219 void
4220 maybe_schedule_decl_only_type_for_resolution(const type_or_decl_base_sptr& t)
4221 {
4222 if (class_or_union_sptr cou = is_class_or_union_type(t))
4223 maybe_schedule_declaration_only_class_for_resolution(cou);
4224 else if (enum_type_decl_sptr e = is_enum_type(t))
4225 maybe_schedule_declaration_only_enum_for_resolution(e);
4226 }
4227
4228 /// If a given class is a declaration-only class then stash it on
4229 /// the side so that at the end of the corpus reading we can resolve
4230 /// it to its definition.
4231 ///
4232 /// @param klass the class to consider.
4233 void
4234 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
4235 {
4236 if (cou->get_is_declaration_only()
4237 && cou->get_definition_of_declaration() == 0
4238 // Make sure the class is not anonymous. Anonymous classes
4239 // are usually later named by a typedef. At that time, after
4240 // being named by a typedef, this method is going to be called
4241 // with the class being named by the typedef.
4242 && !cou->get_qualified_name().empty())
4243 {
4244 string qn = cou->get_qualified_name();
4245 string_classes_or_unions_map::iterator record =
4246 declaration_only_classes().find(qn);
4247 if (record == declaration_only_classes().end())
4248 declaration_only_classes()[qn].push_back(cou);
4249 else
4250 record->second.push_back(cou);
4251 }
4252 }
4253
4254 /// Test if a given declaration-only class has been scheduled for
4255 /// resolution to a defined class.
4256 ///
4257 /// @param klass the class to consider for the test.
4258 ///
4259 /// @return true iff @p klass is a declaration-only class and if
4260 /// it's been scheduled for resolution to a defined class.
4261 bool
4262 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4263 {
4264 if (cou->get_is_declaration_only())
4265 return ((declaration_only_classes().find(cou->get_qualified_name())
4266 != declaration_only_classes().end())
4267 || (declaration_only_classes().find(cou->get_name())
4268 != declaration_only_classes().end()));
4269
4270 return false;
4271 }
4272
4273 /// Compare two ABI artifacts in a context which canonicalization
4274 /// has not be done yet.
4275 ///
4276 /// Please note that this should only be called on IR nodes that
4277 /// belong to the same binary.
4278 ///
4279 /// @param l the left-hand-side operand of the comparison
4280 ///
4281 /// @param r the right-hand-side operand of the comparison.
4282 ///
4283 /// @return true if @p l equals @p r.
4284 bool
4285 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4286 const type_or_decl_base_sptr &r)
4287 {
4288 if (!l || !r)
4289 return !!l == !!r;
4290
4291 const environment& e = l->get_environment();
4292 ABG_ASSERT(!e.canonicalization_is_done());
4293
4294 if (is_decl(l) && is_decl(r)
4295 && l->kind() == r->kind()
4296 && ((l->get_corpus() && r->get_corpus()
4297 && (l->get_corpus() == r->get_corpus()))
4298 ||(l->get_translation_unit()
4299 && r->get_translation_unit()
4300 && l->get_translation_unit() == r->get_translation_unit())))
4301 {
4302 // Fast path optimization. If the two types are declared at
4303 // the same location (in the same binary) then it very likely
4304 // means the two types are equal.
4305 //
4306 // We really need every bit of optimization here because
4307 // otherwise, comparing types before canonicalization can take
4308 // forever.*
4309 decl_base *ld = is_decl(l.get());
4310 decl_base *rd = is_decl(r.get());
4311 ABG_ASSERT(ld && rd);
4312 if (ld->get_qualified_name() != rd->get_qualified_name())
4313 return false;
4314
4315 location ll = ld->get_location(), rl = rd->get_location();
4316 if (ll && rl)
4317 {
4318 string l1 = ll.expand();
4319 string l2 = rl.expand();
4320 if (l1 == l2)
4321 return true;
4322 }
4323 }
4324
4325 e.priv_->allow_type_comparison_results_caching(true);
4326 bool s0 = e.decl_only_class_equals_definition();
4327 e.decl_only_class_equals_definition(true);
4328 bool equal = l == r;
4329 e.decl_only_class_equals_definition(s0);
4330 e.priv_->clear_type_comparison_results_cache();
4331 e.priv_->allow_type_comparison_results_caching(false);
4332 return equal;
4333 }
4334
4335 /// Walk the declaration-only classes that have been found during
4336 /// the building of the corpus and resolve them to their definitions.
4337 void
4338 resolve_declaration_only_classes()
4339 {
4340 vector<string> resolved_classes;
4341
4342 for (string_classes_or_unions_map::iterator i =
4343 declaration_only_classes().begin();
4344 i != declaration_only_classes().end();
4345 ++i)
4346 {
4347 bool to_resolve = false;
4348 for (classes_or_unions_type::iterator j = i->second.begin();
4349 j != i->second.end();
4350 ++j)
4351 if ((*j)->get_is_declaration_only()
4352 && ((*j)->get_definition_of_declaration() == 0))
4353 to_resolve = true;
4354
4355 if (!to_resolve)
4356 {
4357 resolved_classes.push_back(i->first);
4358 continue;
4359 }
4360
4361 // Now, for each decl-only class that have the current name
4362 // 'i->first', let's try to poke at the fully defined class
4363 // that is defined in the same translation unit as the
4364 // declaration.
4365 //
4366 // If we find one class (defined in the TU of the declaration)
4367 // that defines the declaration, then the declaration can be
4368 // resolved to that class.
4369 //
4370 // If no defining class is found in the TU of the declaration,
4371 // then there are possibly three cases to consider:
4372 //
4373 // 1/ There is exactly one class that defines the
4374 // declaration and that class is defined in another TU. In
4375 // this case, the declaration is resolved to that
4376 // definition.
4377 //
4378 // 2/ There are more than one class that define that
4379 // declaration and none of them is defined in the TU of the
4380 // declaration. If those classes are all different, then
4381 // the declaration is left unresolved.
4382 //
4383 // 3/ No class defines the declaration. In this case, the
4384 // declaration is left unresoved.
4385
4386 // So get the classes that might define the current
4387 // declarations which name is i->first.
4388 const type_base_wptrs_type *classes =
4389 lookup_class_types(i->first, *corpus());
4390 if (!classes)
4391 classes = lookup_union_types(i->first, *corpus());
4392
4393 if (!classes)
4394 continue;
4395
4396 // This is a map that associates the translation unit path to
4397 // the class (that potentially defines the declarations that
4398 // we consider) that are defined in that translation unit. It
4399 // should stay ordered by using the TU path as key to ensure
4400 // stability of the order of classe definitions in ABIXML
4401 // output.
4402 map<string, class_or_union_sptr> per_tu_class_map;
4403 for (type_base_wptrs_type::const_iterator c = classes->begin();
4404 c != classes->end();
4405 ++c)
4406 {
4407 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4408 ABG_ASSERT(klass);
4409
4411 if (klass->get_is_declaration_only())
4412 continue;
4413
4414 string tu_path = klass->get_translation_unit()->get_absolute_path();
4415 if (tu_path.empty())
4416 continue;
4417
4418 // Build a map that associates the translation unit path
4419 // to the class (that potentially defines the declarations
4420 // that we consider) that are defined in that translation unit.
4421 per_tu_class_map[tu_path] = klass;
4422 }
4423
4424 if (!per_tu_class_map.empty())
4425 {
4426 // Walk the declarations to resolve and resolve them
4427 // either to the definitions that are in the same TU as
4428 // the declaration, or to the definition found elsewhere,
4429 // if there is only one such definition.
4430 for (classes_or_unions_type::iterator j = i->second.begin();
4431 j != i->second.end();
4432 ++j)
4433 {
4434 if ((*j)->get_is_declaration_only()
4435 && ((*j)->get_definition_of_declaration() == 0))
4436 {
4437 string tu_path =
4438 (*j)->get_translation_unit()->get_absolute_path();
4439 map<string, class_or_union_sptr>::const_iterator e =
4440 per_tu_class_map.find(tu_path);
4441 if (e != per_tu_class_map.end())
4442 (*j)->set_definition_of_declaration(e->second);
4443 else if (per_tu_class_map.size() == 1)
4444 (*j)->set_definition_of_declaration
4445 (per_tu_class_map.begin()->second);
4446 else
4447 {
4448 // We are in case where there are more than
4449 // one definition for the declaration. Let's
4450 // see if they are all equal. If they are,
4451 // then the declaration resolves to the
4452 // definition. Otherwise, we are in the case
4453 // 3/ described above.
4454 map<string,
4455 class_or_union_sptr>::const_iterator it;
4456 class_or_union_sptr first_class =
4457 per_tu_class_map.begin()->second;
4458 bool all_class_definitions_are_equal = true;
4459 for (it = per_tu_class_map.begin();
4460 it != per_tu_class_map.end();
4461 ++it)
4462 {
4463 if (it == per_tu_class_map.begin())
4464 continue;
4465 else
4466 {
4467 if (!compare_before_canonicalisation(it->second,
4468 first_class))
4469 {
4470 all_class_definitions_are_equal = false;
4471 break;
4472 }
4473 }
4474 }
4475 if (all_class_definitions_are_equal)
4476 (*j)->set_definition_of_declaration(first_class);
4477 }
4478 }
4479 }
4480 resolved_classes.push_back(i->first);
4481 }
4482 }
4483
4484 size_t num_decl_only_classes = declaration_only_classes().size(),
4485 num_resolved = resolved_classes.size();
4486 if (show_stats())
4487 cerr << "resolved " << num_resolved
4488 << " class declarations out of "
4489 << num_decl_only_classes
4490 << "\n";
4491
4492 for (vector<string>::const_iterator i = resolved_classes.begin();
4493 i != resolved_classes.end();
4494 ++i)
4495 declaration_only_classes().erase(*i);
4496
4497 if (show_stats() && !declaration_only_classes().empty())
4498 {
4499 cerr << "Here are the "
4500 << num_decl_only_classes - num_resolved
4501 << " unresolved class declarations:\n";
4502 for (string_classes_or_unions_map::iterator i =
4503 declaration_only_classes().begin();
4504 i != declaration_only_classes().end();
4505 ++i)
4506 cerr << " " << i->first << "\n";
4507 }
4508 }
4509
4510 /// Getter for the map of declaration-only enums that are to be
4511 /// resolved to their definition enums by the end of the corpus
4512 /// loading.
4513 ///
4514 /// @return a map of string -> vector of enums where the key is
4515 /// the fully qualified name of the enum and the value is the
4516 /// vector of declaration-only enum.
4517 const string_enums_map&
4518 declaration_only_enums() const
4519 {return decl_only_enums_map_;}
4520
4521 /// Getter for the map of declaration-only enums that are to be
4522 /// resolved to their definition enums by the end of the corpus
4523 /// loading.
4524 ///
4525 /// @return a map of string -> vector of enums where the key is
4526 /// the fully qualified name of the enum and the value is the
4527 /// vector of declaration-only enum.
4529 declaration_only_enums()
4530 {return decl_only_enums_map_;}
4531
4532 /// If a given enum is a declaration-only enum then stash it on
4533 /// the side so that at the end of the corpus reading we can resolve
4534 /// it to its definition.
4535 ///
4536 /// @param enom the enum to consider.
4537 void
4538 maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4539 {
4540 if (enom->get_is_declaration_only()
4541 && enom->get_definition_of_declaration() == 0
4542 // Make sure the enum is not anonymous. Anonymous enums are
4543 // usually later named by a typedef. At that time, after
4544 // being named by a typedef, this method is going to be called
4545 // with the enum being named by the typedef.
4546 && !enom->get_qualified_name().empty())
4547 {
4548 string qn = enom->get_qualified_name();
4549 string_enums_map::iterator record =
4550 declaration_only_enums().find(qn);
4551 if (record == declaration_only_enums().end())
4552 declaration_only_enums()[qn].push_back(enom);
4553 else
4554 record->second.push_back(enom);
4555 }
4556 }
4557
4558 /// Test if a given declaration-only enum has been scheduled for
4559 /// resolution to a defined enum.
4560 ///
4561 /// @param enom the enum to consider for the test.
4562 ///
4563 /// @return true iff @p enom is a declaration-only enum and if
4564 /// it's been scheduled for resolution to a defined enum.
4565 bool
4566 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4567 {
4568 if (enom->get_is_declaration_only())
4569 return (declaration_only_enums().find(enom->get_qualified_name())
4570 != declaration_only_enums().end());
4571
4572 return false;
4573 }
4574
4575 /// Walk the declaration-only enums that have been found during
4576 /// the building of the corpus and resolve them to their definitions.
4577 ///
4578 /// TODO: Do away with this function by factorizing it with
4579 /// resolve_declaration_only_classes. All declaration-only decls
4580 /// could be handled the same way as declaration-only-ness is a
4581 /// property of abigail::ir::decl_base now.
4582 void
4583 resolve_declaration_only_enums()
4584 {
4585 vector<string> resolved_enums;
4586
4587 for (string_enums_map::iterator i =
4588 declaration_only_enums().begin();
4589 i != declaration_only_enums().end();
4590 ++i)
4591 {
4592 bool to_resolve = false;
4593 for (enums_type::iterator j = i->second.begin();
4594 j != i->second.end();
4595 ++j)
4596 if ((*j)->get_is_declaration_only()
4597 && ((*j)->get_definition_of_declaration() == 0))
4598 to_resolve = true;
4599
4600 if (!to_resolve)
4601 {
4602 resolved_enums.push_back(i->first);
4603 continue;
4604 }
4605
4606 // Now, for each decl-only enum that have the current name
4607 // 'i->first', let's try to poke at the fully defined enum
4608 // that is defined in the same translation unit as the
4609 // declaration.
4610 //
4611 // If we find one enum (defined in the TU of the declaration)
4612 // that defines the declaration, then the declaration can be
4613 // resolved to that enum.
4614 //
4615 // If no defining enum is found in the TU of the declaration,
4616 // then there are possibly three cases to consider:
4617 //
4618 // 1/ There is exactly one enum that defines the
4619 // declaration and that enum is defined in another TU. In
4620 // this case, the declaration is resolved to that
4621 // definition.
4622 //
4623 // 2/ There are more than one (different) enum that define
4624 // that declaration and none of them is defined in the TU of
4625 // the declaration. In this case, the declaration is left
4626 // unresolved.
4627 //
4628 // 3/ No enum defines the declaration. In this case, the
4629 // declaration is left unresolved.
4630
4631 // So get the enums that might define the current
4632 // declarations which name is i->first.
4633 const type_base_wptrs_type *enums =
4634 lookup_enum_types(i->first, *corpus());
4635 if (!enums)
4636 continue;
4637
4638 // This is a map that associates the translation unit path to
4639 // the enum (that potentially defines the declarations that
4640 // we consider) that are defined in that translation unit. It
4641 // should stay ordered by using the TU path as key to ensure
4642 // stability of the order of enum definitions in ABIXML
4643 // output.
4644 map<string, enum_type_decl_sptr> per_tu_enum_map;
4645 for (type_base_wptrs_type::const_iterator c = enums->begin();
4646 c != enums->end();
4647 ++c)
4648 {
4649 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4650 ABG_ASSERT(enom);
4651
4653 if (enom->get_is_declaration_only())
4654 continue;
4655
4656 string tu_path = enom->get_translation_unit()->get_absolute_path();
4657 if (tu_path.empty())
4658 continue;
4659
4660 // Build a map that associates the translation unit path
4661 // to the enum (that potentially defines the declarations
4662 // that we consider) that are defined in that translation unit.
4663 per_tu_enum_map[tu_path] = enom;
4664 }
4665
4666 if (!per_tu_enum_map.empty())
4667 {
4668 // Walk the declarations to resolve and resolve them
4669 // either to the definitions that are in the same TU as
4670 // the declaration, or to the definition found elsewhere,
4671 // if there is only one such definition.
4672 for (enums_type::iterator j = i->second.begin();
4673 j != i->second.end();
4674 ++j)
4675 {
4676 if ((*j)->get_is_declaration_only()
4677 && ((*j)->get_definition_of_declaration() == 0))
4678 {
4679 string tu_path =
4680 (*j)->get_translation_unit()->get_absolute_path();
4681 map<string, enum_type_decl_sptr>::const_iterator e =
4682 per_tu_enum_map.find(tu_path);
4683 if (e != per_tu_enum_map.end())
4684 (*j)->set_definition_of_declaration(e->second);
4685 else if (per_tu_enum_map.size() == 1)
4686 (*j)->set_definition_of_declaration
4687 (per_tu_enum_map.begin()->second);
4688 else
4689 {
4690 // We are in case where there are more than
4691 // one definition for the declaration. Let's
4692 // see if they are all equal. If they are,
4693 // then the declaration resolves to the
4694 // definition. Otherwise, we are in the case
4695 // 3/ described above.
4696 map<string,
4697 enum_type_decl_sptr>::const_iterator it;
4698 enum_type_decl_sptr first_enum =
4699 per_tu_enum_map.begin()->second;
4700 bool all_enum_definitions_are_equal = true;
4701 for (it = per_tu_enum_map.begin();
4702 it != per_tu_enum_map.end();
4703 ++it)
4704 {
4705 if (it == per_tu_enum_map.begin())
4706 continue;
4707 else
4708 {
4709 if (!compare_before_canonicalisation(it->second,
4710 first_enum))
4711 {
4712 all_enum_definitions_are_equal = false;
4713 break;
4714 }
4715 }
4716 }
4717 if (all_enum_definitions_are_equal)
4718 (*j)->set_definition_of_declaration(first_enum);
4719 }
4720 }
4721 }
4722 resolved_enums.push_back(i->first);
4723 }
4724 }
4725
4726 size_t num_decl_only_enums = declaration_only_enums().size(),
4727 num_resolved = resolved_enums.size();
4728 if (show_stats())
4729 cerr << "resolved " << num_resolved
4730 << " enum declarations out of "
4731 << num_decl_only_enums
4732 << "\n";
4733
4734 for (vector<string>::const_iterator i = resolved_enums.begin();
4735 i != resolved_enums.end();
4736 ++i)
4737 declaration_only_enums().erase(*i);
4738
4739 if (show_stats() && !declaration_only_enums().empty())
4740 {
4741 cerr << "Here are the "
4742 << num_decl_only_enums - num_resolved
4743 << " unresolved enum declarations:\n";
4744 for (string_enums_map::iterator i = declaration_only_enums().begin();
4745 i != declaration_only_enums().end();
4746 ++i)
4747 cerr << " " << i->first << "\n";
4748 }
4749 }
4750
4751 /// Test if a symbol belongs to a function of the current ABI
4752 /// corpus.
4753 ///
4754 /// This is a sub-routine of fixup_functions_with_no_symbols.
4755 ///
4756 /// @param fn the function symbol to consider.
4757 ///
4758 /// @returnt true if @p fn belongs to a function of the current ABI
4759 /// corpus.
4760 bool
4761 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4762 {
4763 corpus_sptr corp = corpus();
4764 if (!corp)
4765 return false;
4766
4767 interned_string id = corp->get_environment().intern(fn->get_id_string());
4768
4769 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4770 if (!fns)
4771 return false;
4772
4773 for (auto f : *fns)
4774 if (f->get_symbol())
4775 return true;
4776
4777 return false;
4778 }
4779
4780 /// Some functions described by DWARF may have their linkage name
4781 /// set, but no link to their actual underlying elf symbol. When
4782 /// these are virtual member functions, comparing the enclosing type
4783 /// against another one which has its underlying symbol properly set
4784 /// might lead to spurious type changes.
4785 ///
4786 /// If the corpus contains a symbol with the same name as the
4787 /// linkage name of the function, then set up the link between the
4788 /// function and its underlying symbol.
4789 ///
4790 /// Note that for the moment, only virtual member functions are
4791 /// fixed up like this. This is because they really are the only
4792 /// fuctions of functions that can affect types (in spurious ways).
4793 void
4794 fixup_functions_with_no_symbols()
4795 {
4796 corpus_sptr corp = corpus();
4797 if (!corp)
4798 return;
4799
4800 die_function_decl_map_type &fns_with_no_symbol =
4801 die_function_decl_with_no_symbol_map();
4802
4803 if (do_log())
4804 cerr << fns_with_no_symbol.size()
4805 << " functions to fixup, potentially\n";
4806
4807 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4808 i != fns_with_no_symbol.end();
4809 ++i)
4810 if (elf_symbol_sptr sym =
4811 corp->lookup_function_symbol(i->second->get_linkage_name()))
4812 {
4813 // So i->second is a virtual member function that was
4814 // previously scheduled to be set a function symbol.
4815 //
4816 // But if it appears that it now has a symbol already set,
4817 // then do not set a symbol to it again.
4818 //
4819 // Or if it appears that another virtual member function
4820 // from the current ABI Corpus, with the same linkage
4821 // (mangled) name has already been set a symbol, then do not
4822 // set a symbol to this function either. Otherwise, there
4823 // will be two virtual member functions with the same symbol
4824 // in the class and that leads to spurious hard-to-debug
4825 // change reports later down the road.
4826 if (i->second->get_symbol()
4827 || symbol_already_belongs_to_a_function(sym))
4828 continue;
4829
4830 ABG_ASSERT(is_member_function(i->second));
4832 i->second->set_symbol(sym);
4833
4834 if (do_log())
4835 cerr << "fixed up '"
4836 << i->second->get_pretty_representation()
4837 << "' with symbol '"
4838 << sym->get_id_string()
4839 << "'\n";
4840 }
4841
4842 fns_with_no_symbol.clear();
4843 }
4844
4845 /// Copy missing member functions from a source @ref class_decl to a
4846 /// destination one.
4847 ///
4848 /// If a function is present on the source @ref class_decl and not
4849 /// on the destination one, then it's copied from the source class
4850 /// to the destination one.
4851 void
4852 copy_missing_member_functions(const class_decl_sptr& dest_class,
4853 const class_decl_sptr& src_class)
4854 {
4855 for (auto method : src_class->get_member_functions())
4856 if (!method->get_linkage_name().empty())
4857 if (!dest_class->find_member_function(method->get_linkage_name()))
4858 {
4859 method_decl_sptr copied_method =
4860 copy_member_function(dest_class, method);
4861 ABG_ASSERT(copied_method);
4862 schedule_type_for_late_canonicalization(copied_method->get_type());
4863 }
4864 }
4865
4866 /// Test if there is an interator in a given range that points to
4867 /// an anonymous class.
4868 ///
4869 /// @param begin the start of the iterator range to consider.
4870 ///
4871 /// @param end the end of the iterator range to consider. This
4872 /// points to after the range.
4873 template <typename iterator_type>
4874 bool
4875 contains_anonymous_class(const iterator_type& begin,
4876 const iterator_type& end)
4877 {
4878 for (auto i = begin; i < end; ++i)
4879 {
4880 type_base_sptr t(*i);
4882 if (c && c->get_is_anonymous())
4883 return true;
4884 }
4885 return false;
4886 }
4887
4888 /// Ensure that all classes of the same name have the same virtual
4889 /// member functions. So copy the virtual member functions from a
4890 /// class C that have them to another class C that doesn't.
4891 ///
4892 /// @param begin an iterator to the first member of the set of
4893 /// classes which to merge virtual member functions for.
4894 ///
4895 /// @param end an iterator to the last member (one past the end
4896 /// actually) of the set of classes which to merge virtual member
4897 /// functions for.
4898 template <typename iterator_type>
4899 void
4900 merge_member_functions_of_classes(const iterator_type& begin,
4901 const iterator_type& end)
4902 {
4903 if (contains_anonymous_class(begin, end))
4904 return;
4905
4906 for (auto i = begin; i < end; ++i)
4907 {
4908 type_base_sptr t(*i);
4909 class_decl_sptr reference_class = is_class_type(t);
4910 if (!reference_class)
4911 continue;
4912
4913 string n1 = reference_class->get_pretty_representation(true, true);
4914 string n2;
4915 for (auto j = begin; j < end; ++j)
4916 {
4917 if (j == i)
4918 continue;
4919
4920 type_base_sptr type(*j);
4921 class_decl_sptr klass = is_class_type(type);
4922 if (!klass)
4923 continue;
4924
4925 n2 = klass->get_pretty_representation(true, true);
4926 ABG_ASSERT(n1 == n2);
4927
4928 copy_missing_member_functions(reference_class, klass);
4929 copy_missing_member_functions(klass, reference_class);
4930 }
4931 }
4932 }
4933
4934 /// Ensure that all classes of the same name have the same virtual
4935 /// member functions. So copy the virtual member functions from a
4936 /// class C that have them to another class C that doesn't.
4937 void
4938 merge_member_functions_in_classes_of_same_names()
4939 {
4940 corpus_sptr abi = corpus();
4941 if (!abi)
4942 return;
4943
4945 abi->get_types().class_types();
4946
4947 for (auto entry : class_types)
4948 {
4949 auto& classes = entry.second;
4950 if (classes.size() > 1)
4951 {
4952 bool a_class_has_member_fns = false;
4953 for (auto& c : classes)
4954 {
4955 type_base_sptr t(c);
4956 if (class_decl_sptr klass = is_class_type(t))
4957 if (!klass->get_member_functions().empty())
4958 {
4959 a_class_has_member_fns = true;
4960 break;
4961 }
4962 }
4963 if (a_class_has_member_fns)
4964 merge_member_functions_of_classes(classes.begin(),
4965 classes.end());
4966 }
4967 }
4968 }
4969
4970 /// @return vectors of types created during the analysis of the
4971 /// DWARF and in the need of being canonicalized.
4972 const vector<type_base_sptr>&
4973 types_to_canonicalize() const
4974 {return types_to_canonicalize_;}
4975
4976 /// @return vectors of types created during the analysis of the
4977 /// DWARF and in the need of being canonicalized.
4978 vector<type_base_sptr>&
4979 types_to_canonicalize()
4980 {return types_to_canonicalize_;}
4981
4982 /// Clear the containers holding types to canonicalize.
4983 void
4984 clear_types_to_canonicalize()
4985 {
4986 types_to_canonicalize_.clear();
4987 }
4988
4989 /// Types that were created but not tied to a particular DIE, must
4990 /// be scheduled for late canonicalization using this method.
4991 ///
4992 /// @param t the type to schedule for late canonicalization.
4993 void
4994 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4995 {
4996 types_to_canonicalize_.push_back(t);
4997 }
4998
4999 /// Canonicalize types which DIE offsets are stored in vectors on
5000 /// the side. This is a sub-routine of
5001 /// reader::perform_late_type_canonicalizing().
5002 ///
5003 /// @param source where the DIE of the types to canonicalize are
5004 /// from.
5005 void
5006 canonicalize_types_scheduled()
5007 {
5008 tools_utils::timer cn_timer;
5009 if (do_log())
5010 {
5011 cerr << "DWARF Reader is going to canonicalize "
5012 << std::dec
5013 << types_to_canonicalize().size()
5014 << " types";
5015 corpus_sptr c = corpus();
5016 if (c)
5017 cerr << " from corpus " << corpus()->get_path() << "\n";
5018 cn_timer.start();
5019 }
5020
5022 (types_to_canonicalize().begin(),
5023 types_to_canonicalize().end(),
5024 [](const vector<type_base_sptr>::const_iterator& i)
5025 {return *i;}, do_log(), show_stats());
5026
5027 if (do_log())
5028 {
5029 cn_timer.stop();
5030 cerr << "DWARF Reader finished types "
5031 << "sorting, hashing & canonicalizing in: "
5032 << cn_timer << "\n";
5033 }
5034 }
5035
5036 /// Compute the number of canonicalized and missed types in the late
5037 /// canonicalization phase.
5038 ///
5039 /// @param source where the DIEs of the canonicalized types are
5040 /// from.
5041 ///
5042 /// @param canonicalized the number of types that got canonicalized
5043 /// is added to the value already present in this parameter.
5044 ///
5045 /// @param missed the number of types scheduled for late
5046 /// canonicalization and which couldn't be canonicalized (for a
5047 /// reason) is added to the value already present in this parameter.
5048 void
5049 add_late_canonicalized_types_stats(size_t& canonicalized,
5050 size_t& missed) const
5051 {
5052 for (auto t : types_to_canonicalize())
5053 {
5054 if (t->get_canonical_type())
5055 ++canonicalized;
5056 else
5057 ++missed;
5058 }
5059 }
5060
5061 // Look at the types that need to be canonicalized after the
5062 // translation unit has been constructed and canonicalize them.
5063 void
5064 perform_late_type_canonicalizing()
5065 {
5066 canonicalize_types_scheduled();
5067
5068 if (show_stats())
5069 {
5070 size_t num_canonicalized = 0, num_missed = 0, total = 0;
5071 add_late_canonicalized_types_stats(num_canonicalized,
5072 num_missed);
5073 total = num_canonicalized + num_missed;
5074 cerr << "binary: "
5075 << elf_path()
5076 << "\n";
5077 cerr << " # late canonicalized types: "
5078 << num_canonicalized;
5079 if (total)
5080 cerr << " (" << num_canonicalized * 100 / total << "%)";
5081 cerr << "\n"
5082 << " # missed canonicalization opportunities: "
5083 << num_missed;
5084 if (total)
5085 cerr << " (" << num_missed * 100 / total << "%)";
5086 cerr << "\n";
5087 }
5088
5089 }
5090
5091 const die_tu_map_type&
5092 die_tu_map() const
5093 {return die_tu_map_;}
5094
5096 die_tu_map()
5097 {return die_tu_map_;}
5098
5099 /// Getter for the map that associates a translation unit DIE to the
5100 /// vector of imported unit points that it contains.
5101 ///
5102 /// @param source where the DIEs are from.
5103 ///
5104 /// @return the map.
5106 tu_die_imported_unit_points_map(die_source source) const
5107 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
5108
5109 /// Getter for the map that associates a translation unit DIE to the
5110 /// vector of imported unit points that it contains.
5111 ///
5112 /// @param source where the DIEs are from.
5113 ///
5114 /// @return the map.
5116 tu_die_imported_unit_points_map(die_source source)
5117 {
5118 switch (source)
5119 {
5120 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5121 break;
5122 case ALT_DEBUG_INFO_DIE_SOURCE:
5123 return alt_tu_die_imported_unit_points_map_;
5124 case TYPE_UNIT_DIE_SOURCE:
5125 return type_units_tu_die_imported_unit_points_map_;
5126 case NO_DEBUG_INFO_DIE_SOURCE:
5127 case NUMBER_OF_DIE_SOURCES:
5128 // We cannot reach this point.
5130 }
5131 return tu_die_imported_unit_points_map_;
5132 }
5133
5134 /// Reset the current corpus being constructed.
5135 ///
5136 /// This actually deletes the current corpus being constructed.
5137 void
5138 reset_corpus()
5139 {corpus().reset();}
5140
5141 /// Get the map that associates each DIE to its parent DIE. This is
5142 /// for DIEs coming from the main debug info sections.
5143 ///
5144 /// @param source where the DIEs in the map come from.
5145 ///
5146 /// @return the DIE -> parent map.
5148 die_parent_map(die_source source) const
5149 {return const_cast<reader*>(this)->die_parent_map(source);}
5150
5151 /// Get the map that associates each DIE to its parent DIE. This is
5152 /// for DIEs coming from the main debug info sections.
5153 ///
5154 /// @param source where the DIEs in the map come from.
5155 ///
5156 /// @return the DIE -> parent map.
5158 die_parent_map(die_source source)
5159 {
5160 switch (source)
5161 {
5162 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
5163 break;
5164 case ALT_DEBUG_INFO_DIE_SOURCE:
5165 return alternate_die_parent_map_;
5166 case TYPE_UNIT_DIE_SOURCE:
5167 return type_section_die_parent_map();
5168 case NO_DEBUG_INFO_DIE_SOURCE:
5169 case NUMBER_OF_DIE_SOURCES:
5171 }
5172 return primary_die_parent_map_;
5173 }
5174
5176 type_section_die_parent_map() const
5177 {return type_section_die_parent_map_;}
5178
5180 type_section_die_parent_map()
5181 {return type_section_die_parent_map_;}
5182
5183 /// Getter of the current translation unit.
5184 ///
5185 /// @return the current translation unit being constructed.
5187 cur_transl_unit() const
5188 {return cur_tu_;}
5189
5190 /// Getter of the current translation unit.
5191 ///
5192 /// @return the current translation unit being constructed.
5194 cur_transl_unit()
5195 {return cur_tu_;}
5196
5197 /// Setter of the current translation unit.
5198 ///
5199 /// @param tu the current translation unit being constructed.
5200 void
5201 cur_transl_unit(translation_unit_sptr tu)
5202 {
5203 if (tu)
5204 cur_tu_ = tu;
5205 }
5206
5207 /// Return the global scope of the current translation unit.
5208 ///
5209 /// @return the global scope of the current translation unit.
5210 const scope_decl_sptr&
5211 global_scope() const
5212 {return cur_transl_unit()->get_global_scope();}
5213
5214 /// Return a scope that is nil.
5215 ///
5216 /// @return a scope that is nil.
5217 const scope_decl_sptr&
5218 nil_scope() const
5219 {return nil_scope_;}
5220
5221 const scope_stack_type&
5222 scope_stack() const
5223 {return scope_stack_;}
5224
5226 scope_stack()
5227 {return scope_stack_;}
5228
5229 scope_decl*
5230 current_scope()
5231 {
5232 if (scope_stack().empty())
5233 {
5234 if (cur_transl_unit())
5235 scope_stack().push(cur_transl_unit()->get_global_scope().get());
5236 }
5237 return scope_stack().top();
5238 }
5239
5240 list<var_decl_sptr>&
5241 var_decls_to_re_add_to_tree()
5242 {return var_decls_to_add_;}
5243
5244 /// Test if a DIE represents a decl (function or variable) that has
5245 /// a symbol that is exported, whatever that means. This is
5246 /// supposed to work for Linux Kernel binaries as well.
5247 ///
5248 /// This is useful to limit the amount of DIEs taken into account to
5249 /// the strict limit of what an ABI actually means. Limiting the
5250 /// volume of DIEs analyzed this way is an important optimization to
5251 /// keep big binaries "manageable" by libabigail.
5252 ///
5253 /// @param DIE the die to consider.
5254 bool
5255 is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
5256 {
5257 if (!die || !die_is_decl(die))
5258 return false;
5259
5260 bool result = false, address_found = false, symbol_is_exported = false;;
5261 Dwarf_Addr decl_symbol_address = 0;
5262
5263 if (die_is_variable_decl(die))
5264 {
5265 if ((address_found = get_variable_address(die, decl_symbol_address)))
5266 symbol_is_exported =
5267 !!variable_symbol_is_exported(decl_symbol_address);
5268 }
5269 else if (die_is_function_decl(die))
5270 {
5271 if ((address_found = get_function_address(die, decl_symbol_address)))
5272 symbol_is_exported =
5273 !!function_symbol_is_exported(decl_symbol_address);
5274 }
5275
5276 if (address_found)
5277 result = symbol_is_exported;
5278
5279 return result;
5280 }
5281
5282 /// Test if a DIE is a variable or function DIE which name denotes
5283 /// an undefined ELF symbol.
5284 ///
5285 /// @return true iff @p die represents a function or variable that
5286 /// has an undefined symbol.
5287 bool
5288 is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
5289 {
5290 if (is_decl_die_with_exported_symbol(die))
5291 return false;
5292
5293 string name, linkage_name;
5294 die_name_and_linkage_name(die, name, linkage_name);
5295 if (linkage_name.empty())
5296 linkage_name = name;
5297
5298 bool result = false;
5299 if ((die_is_variable_decl(die)
5300 && symtab()->variable_symbol_is_undefined(linkage_name))
5301 ||
5302 (die_is_function_decl(die)
5303 && symtab()->function_symbol_is_undefined(linkage_name)))
5304 result = true;
5305
5306 return result;
5307 }
5308
5309 /// This is a sub-routine of maybe_adjust_fn_sym_address and
5310 /// maybe_adjust_var_sym_address.
5311 ///
5312 /// Given an address that we got by looking at some debug
5313 /// information (e.g, a symbol's address referred to by a DWARF
5314 /// TAG), If the ELF file we are interested in is a shared library
5315 /// or an executable, then adjust the address to be coherent with
5316 /// where the executable (or shared library) is loaded. That way,
5317 /// the address can be used to look for symbols in the executable or
5318 /// shared library.
5319 ///
5320 /// @return the adjusted address, or the same address as @p addr if
5321 /// it didn't need any adjustment.
5322 Dwarf_Addr
5323 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
5324 {
5325 if (addr == 0)
5326 return addr;
5327
5328 GElf_Ehdr eh_mem;
5329 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
5330
5331 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
5332 {
5333 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
5334 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
5335 dwarf_elf_load_address));
5337 elf_load_address));
5338 if (dwarf_is_splitted()
5339 && (dwarf_elf_load_address != elf_load_address))
5340 // This means that in theory the DWARF and the executable are
5341 // not loaded at the same address. And addr is meaningful
5342 // only in the context of the DWARF.
5343 //
5344 // So let's transform addr into an offset relative to where
5345 // the DWARF is loaded, and let's add that relative offset
5346 // to the load address of the executable. That way, addr
5347 // becomes meaningful in the context of the executable and
5348 // can thus be used to compare against the address of
5349 // symbols of the executable, for instance.
5350 addr = addr - dwarf_elf_load_address + elf_load_address;
5351 }
5352
5353 return addr;
5354 }
5355
5356 /// For a relocatable (*.o) elf file, this function expects an
5357 /// absolute address, representing a function symbol. It then
5358 /// extracts the address of the .text section from the symbol
5359 /// absolute address to get the relative address of the function
5360 /// from the beginning of the .text section.
5361 ///
5362 /// For executable or shared library, this function expects an
5363 /// address of a function symbol that was retrieved by looking at a
5364 /// DWARF "file". The function thus adjusts the address to make it
5365 /// be meaningful in the context of the ELF file.
5366 ///
5367 /// In both cases, the address can then be compared against the
5368 /// st_value field of a function symbol from the ELF file.
5369 ///
5370 /// @param addr an adress for a function symbol that was retrieved
5371 /// from a DWARF file.
5372 ///
5373 /// @return the (possibly) adjusted address, or just @p addr if no
5374 /// adjustment took place.
5375 Dwarf_Addr
5376 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
5377 {
5378 if (addr == 0)
5379 return addr;
5380
5381 Elf* elf = elf_handle();
5382 GElf_Ehdr eh_mem;
5383 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5384
5385 if (elf_header->e_type == ET_REL)
5386 // We are looking at a relocatable file. In this case, we don't
5387 // do anything because:
5388 //
5389 // 1/ the addresses from DWARF are absolute (relative to the
5390 // beginning of the relocatable file)
5391 //
5392 // 2/ The ELF symbol addresses that we store in our lookup
5393 // tables are translated from section-related to absolute as
5394 // well. So we don't have anything to do at this point for
5395 // ET_REL files.
5396 ;
5397 else
5398 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5399
5400 return addr;
5401 }
5402
5403 /// For a relocatable (*.o) elf file, this function expects an
5404 /// absolute address, representing a global variable symbol. It
5405 /// then extracts the address of the {.data,.data1,.rodata,.bss}
5406 /// section from the symbol absolute address to get the relative
5407 /// address of the variable from the beginning of the data section.
5408 ///
5409 /// For executable or shared library, this function expects an
5410 /// address of a variable symbol that was retrieved by looking at a
5411 /// DWARF "file". The function thus adjusts the address to make it
5412 /// be meaningful in the context of the ELF file.
5413 ///
5414 /// In both cases, the address can then be compared against the
5415 /// st_value field of a function symbol from the ELF file.
5416 ///
5417 /// @param addr an address for a global variable symbol that was
5418 /// retrieved from a DWARF file.
5419 ///
5420 /// @return the (possibly) adjusted address, or just @p addr if no
5421 /// adjustment took place.
5422 Dwarf_Addr
5423 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5424 {
5425 Elf* elf = elf_handle();
5426 GElf_Ehdr eh_mem;
5427 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5428
5429 if (elf_header->e_type == ET_REL)
5430 // We are looking at a relocatable file. In this case, we don't
5431 // do anything because:
5432 //
5433 // 1/ the addresses from DWARF are absolute (relative to the
5434 // beginning of the relocatable file)
5435 //
5436 // 2/ The ELF symbol addresses that we store in our lookup
5437 // tables are translated from section-related to absolute as
5438 // well. So we don't have anything to do at this point for
5439 // ET_REL files.
5440 ;
5441 else
5442 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5443
5444 return addr;
5445 }
5446
5447 /// Get the first exported function address in the set of addresses
5448 /// referred to by the DW_AT_ranges attribute of a given DIE.
5449 ///
5450 /// @param die the DIE we are considering.
5451 ///
5452 /// @param address output parameter. This is set to the first
5453 /// address found in the sequence pointed to by the DW_AT_ranges
5454 /// attribute found on the DIE @p die, iff the function returns
5455 /// true. Otherwise, no value is set into this output parameter.
5456 ///
5457 /// @return true iff the DIE @p die does have a DW_AT_ranges
5458 /// attribute and an address of an exported function was found in
5459 /// its sequence value.
5460 bool
5461 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5462 Dwarf_Addr& address) const
5463 {
5464 Dwarf_Addr base;
5465 Dwarf_Addr end_addr;
5466 ptrdiff_t offset = 0;
5467
5468 do
5469 {
5470 Dwarf_Addr addr = 0, fn_addr = 0;
5471 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5472 {
5473 fn_addr = maybe_adjust_fn_sym_address(addr);
5474 if (function_symbol_is_exported(fn_addr))
5475 {
5476 address = fn_addr;
5477 return true;
5478 }
5479 }
5480 } while (offset > 0);
5481 return false;
5482 }
5483
5484 /// Get the address of the function.
5485 ///
5486 /// The address of the function is considered to be the value of the
5487 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5488 /// only) to not point to an absolute address anymore, but rather to
5489 /// the address of the function inside the .text segment.
5490 ///
5491 /// @param function_die the die of the function to consider.
5492 ///
5493 /// @param address the resulting address iff the function returns
5494 /// true.
5495 ///
5496 /// @return true if the function address was found.
5497 bool
5498 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5499 {
5500 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5501 DW_AT_low_pc, address))
5502 // So no DW_AT_low_pc was found. Let's see if the function DIE
5503 // has got a DW_AT_ranges attribute instead. If it does, the
5504 // first address of the set of addresses represented by the
5505 // value of that DW_AT_ranges represents the function (symbol)
5506 // address we are looking for.
5507 if (!get_first_exported_fn_address_from_DW_AT_ranges
5508 (const_cast<Dwarf_Die*>(function_die),
5509 address))
5510 return false;
5511
5512 address = maybe_adjust_fn_sym_address(address);
5513 return true;
5514 }
5515
5516 /// Get the address of the global variable.
5517 ///
5518 /// The address of the global variable is considered to be the value
5519 /// of the DW_AT_location attribute, possibly adjusted (in
5520 /// relocatable files only) to not point to an absolute address
5521 /// anymore, but rather to the address of the global variable inside
5522 /// the data segment.
5523 ///
5524 /// @param variable_die the die of the function to consider.
5525 ///
5526 /// @param address the resulting address iff this function returns
5527 /// true.
5528 ///
5529 /// @return true if the variable address was found.
5530 bool
5531 get_variable_address(const Dwarf_Die* variable_die,
5532 Dwarf_Addr& address) const
5533 {
5534 bool is_tls_address = false;
5535 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5536 address, is_tls_address))
5537 return false;
5538 if (!is_tls_address)
5539 address = maybe_adjust_var_sym_address(address);
5540 return true;
5541 }
5542
5543 /// Getter of the exported decls builder object.
5544 ///
5545 /// @return the exported decls builder.
5546 corpus::exported_decls_builder*
5547 exported_decls_builder()
5548 {return corpus()->get_exported_decls_builder().get();}
5549
5550 /// Getter of the "load_all_types" flag. This flag tells if all the
5551 /// types (including those not reachable by public declarations) are
5552 /// to be read and represented in the final ABI corpus.
5553 ///
5554 /// @return the load_all_types flag.
5555 bool
5556 load_all_types() const
5557 {return options().load_all_types;}
5558
5559 /// Setter of the "load_all_types" flag. This flag tells if all the
5560 /// types (including those not reachable by public declarations) are
5561 /// to be read and represented in the final ABI corpus.
5562 ///
5563 /// @param f the new load_all_types flag.
5564 void
5565 load_all_types(bool f)
5566 {options().load_all_types = f;}
5567
5568 bool
5569 load_in_linux_kernel_mode() const
5570 {return options().load_in_linux_kernel_mode;}
5571
5572 void
5573 load_in_linux_kernel_mode(bool f)
5574 {options().load_in_linux_kernel_mode = f;}
5575
5576 /// Getter of the 'load-undefined-interface' property.
5577 ///
5578 /// That property tells the reader if it should load the interfaces
5579 /// that are undefined in the binary. An undefined interface is a
5580 /// variable or function which has a symbol that is not defined in
5581 /// the binary.
5582 ///
5583 /// @return true iff the front-end has to load the undefined
5584 /// interfaces.
5585 bool
5586 load_undefined_interfaces() const
5588
5589 /// Test if it's allowed to assume that the DWARF debug info has
5590 /// been factorized (for instance, with the DWZ tool) so that if two
5591 /// type DIEs originating from the .gnu_debugaltlink section have
5592 /// different offsets, they represent different types.
5593 ///
5594 /// @return true iff we can assume that the DWARF debug info has
5595 /// been factorized.
5596 bool
5597 leverage_dwarf_factorization() const
5598 {
5599 if (!leverage_dwarf_factorization_.has_value())
5600 {
5601 if (options().leverage_dwarf_factorization
5602 && elf_helpers::find_section_by_name(elf_handle(),
5603 ".gnu_debugaltlink"))
5604 leverage_dwarf_factorization_ = true;
5605 else
5606 leverage_dwarf_factorization_ = false;
5607 }
5608 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5609
5610 return *leverage_dwarf_factorization_;
5611 }
5612
5613 /// Getter of the "show_stats" flag.
5614 ///
5615 /// This flag tells if we should emit statistics about various
5616 /// internal stuff.
5617 ///
5618 /// @return the value of the flag.
5619 bool
5620 show_stats() const
5621 {return options().show_stats;}
5622
5623 /// Setter of the "show_stats" flag.
5624 ///
5625 /// This flag tells if we should emit statistics about various
5626 /// internal stuff.
5627 ///
5628 /// @param f the value of the flag.
5629 void
5630 show_stats(bool f)
5631 {options().show_stats = f;}
5632
5633 /// Getter of the "do_log" flag.
5634 ///
5635 /// This flag tells if we should log about various internal
5636 /// details.
5637 ///
5638 /// return the "do_log" flag.
5639 bool
5640 do_log() const
5641 {return options().do_log;}
5642
5643 /// Setter of the "do_log" flag.
5644 ///
5645 /// This flag tells if we should log about various internal details.
5646 ///
5647 /// @param f the new value of the flag.
5648 void
5649 do_log(bool f)
5650 {options().do_log = f;}
5651
5652 /// Walk the DIEs under a given die and for each child, populate the
5653 /// die -> parent map to record the child -> parent relationship
5654 /// that
5655 /// exists between the child and the given die.
5656 ///
5657 /// The function also builds the vector of places where units are
5658 /// imported.
5659 ///
5660 /// This is done recursively as for each child DIE, this function
5661 /// walks its children as well.
5662 ///
5663 /// @param die the DIE whose children to walk recursively.
5664 ///
5665 /// @param source where the DIE @p die comes from.
5666 ///
5667 /// @param imported_units a vector containing all the offsets of the
5668 /// points where unit have been imported, under @p die.
5669 void
5670 build_die_parent_relations_under(Dwarf_Die* die,
5671 die_source source,
5672 imported_unit_points_type & imported_units)
5673 {
5674 if (!die)
5675 return;
5676
5677 offset_offset_map_type& parent_of = die_parent_map(source);
5678
5679 Dwarf_Die child;
5680 if (dwarf_child(die, &child) != 0)
5681 return;
5682
5683 do
5684 {
5685 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5686 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5687 {
5688 Dwarf_Die imported_unit;
5689 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5690 // If the imported_unit has a sub-tree, let's record
5691 // this point at which the sub-tree is imported into
5692 // the current debug info.
5693 //
5694 // Otherwise, if the imported_unit has no sub-tree,
5695 // there is no point in recording where a non-existent
5696 // sub-tree is being imported.
5697 //
5698 // Note that the imported_unit_points_type type below
5699 // expects the imported_unit to have a sub-tree.
5700 && die_has_children(&imported_unit))
5701 {
5702 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5703 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5704 imported_units.push_back
5705 (imported_unit_point(dwarf_dieoffset(&child),
5706 imported_unit,
5707 imported_unit_die_source));
5708 }
5709 }
5710 build_die_parent_relations_under(&child, source, imported_units);
5711 }
5712 while (dwarf_siblingof(&child, &child) == 0);
5713
5714 }
5715
5716 /// Determine if we do have to build a DIE -> parent map, depending
5717 /// on a given language.
5718 ///
5719 /// Some languages like C++, Ada etc, do have the concept of
5720 /// namespace and yet, the DIE data structure doesn't provide us
5721 /// with a way to get the parent namespace of a given DIE. So for
5722 /// those languages, we need to build a DIE -> parent map so that we
5723 /// can get the namespace DIE (or more generally the scope DIE) of a given
5724 /// DIE as we need it.
5725 ///
5726 /// But then some more basic languages like C or assembly don't have
5727 /// that need.
5728 ///
5729 /// This function, depending on the language, tells us if we need to
5730 /// build the DIE -> parent map or not.
5731 ///
5732 /// @param lang the language to consider.
5733 ///
5734 /// @return true iff we need to build the DIE -> parent map for this
5735 /// language.
5736 bool
5737 do_we_build_die_parent_maps(translation_unit::language lang)
5738 {
5739 if (is_c_language(lang))
5740 return false;
5741
5742 switch (lang)
5743 {
5744 case translation_unit::LANG_UNKNOWN:
5745#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5746 case translation_unit::LANG_Mips_Assembler:
5747#endif
5748 return false;
5749 default:
5750 break;
5751 }
5752 return true;
5753 }
5754
5755 /// Walk all the DIEs accessible in the debug info (and in the
5756 /// alternate debug info as well) and build maps representing the
5757 /// relationship DIE -> parent. That is, make it so that we can get
5758 /// the parent for a given DIE.
5759 ///
5760 /// Note that the goal of this map is to be able to get the parent
5761 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5762 /// when we get a DIE of a type, and we want to build an internal
5763 /// representation for it, we need to get its fully qualified name.
5764 /// For that, we need to know what is the parent DIE of that type
5765 /// DIE, so that we can know what the namespace of that type is.
5766 ///
5767 /// Note that as the C language doesn't have namespaces (all types
5768 /// are defined in the same global namespace), this function doesn't
5769 /// build the DIE -> parent map if the current translation unit
5770 /// comes from C. This saves time on big C ELF files with a lot of
5771 /// DIEs.
5772 void
5773 build_die_parent_maps()
5774 {
5775 bool we_do_have_to_build_die_parent_map = false;
5776 uint8_t address_size = 0;
5777 size_t header_size = 0;
5778 // Get the DIE of the current translation unit, look at it to get
5779 // its language. If that language is in C, then all types are in
5780 // the global namespace so we don't need to build the DIE ->
5781 // parent map. So we dont build it in that case.
5782 for (Dwarf_Off offset = 0, next_offset = 0;
5783 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5784 offset, &next_offset, &header_size,
5785 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5786 offset = next_offset)
5787 {
5788 Dwarf_Off die_offset = offset + header_size;
5789 Dwarf_Die cu;
5790 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5791 die_offset, &cu))
5792 continue;
5793
5794 uint64_t l = 0;
5795 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5796 translation_unit::language lang = dwarf_language_to_tu_language(l);
5797 if (do_we_build_die_parent_maps(lang))
5798 we_do_have_to_build_die_parent_map = true;
5799 }
5800
5801 if (!we_do_have_to_build_die_parent_map)
5802 return;
5803
5804 // Build the DIE -> parent relation for DIEs coming from the
5805 // .debug_info section in the alternate debug info file.
5806 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5807 for (Dwarf_Off offset = 0, next_offset = 0;
5808 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5809 offset, &next_offset, &header_size,
5810 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5811 offset = next_offset)
5812 {
5813 Dwarf_Off die_offset = offset + header_size;
5814 Dwarf_Die cu;
5815 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5816 die_offset, &cu))
5817 continue;
5818 cur_tu_die(&cu);
5819
5820 imported_unit_points_type& imported_units =
5821 tu_die_imported_unit_points_map(source)[die_offset] =
5823 build_die_parent_relations_under(&cu, source, imported_units);
5824 }
5825
5826 // Build the DIE -> parent relation for DIEs coming from the
5827 // .debug_info section of the main debug info file.
5828 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5829 address_size = 0;
5830 header_size = 0;
5831 for (Dwarf_Off offset = 0, next_offset = 0;
5832 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5833 offset, &next_offset, &header_size,
5834 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5835 offset = next_offset)
5836 {
5837 Dwarf_Off die_offset = offset + header_size;
5838 Dwarf_Die cu;
5839 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5840 die_offset, &cu))
5841 continue;
5842 cur_tu_die(&cu);
5843 imported_unit_points_type& imported_units =
5844 tu_die_imported_unit_points_map(source)[die_offset] =
5846 build_die_parent_relations_under(&cu, source, imported_units);
5847 }
5848
5849 // Build the DIE -> parent relation for DIEs coming from the
5850 // .debug_types section.
5851 source = TYPE_UNIT_DIE_SOURCE;
5852 address_size = 0;
5853 header_size = 0;
5854 uint64_t type_signature = 0;
5855 Dwarf_Off type_offset;
5856 for (Dwarf_Off offset = 0, next_offset = 0;
5857 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5858 offset, &next_offset, &header_size,
5859 NULL, NULL, &address_size, NULL,
5860 &type_signature, &type_offset) == 0);
5861 offset = next_offset)
5862 {
5863 Dwarf_Off die_offset = offset + header_size;
5864 Dwarf_Die cu;
5865
5866 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5867 die_offset, &cu))
5868 continue;
5869 cur_tu_die(&cu);
5870 imported_unit_points_type& imported_units =
5871 tu_die_imported_unit_points_map(source)[die_offset] =
5873 build_die_parent_relations_under(&cu, source, imported_units);
5874 }
5875 }
5876};// end class reader.
5877
5878/// The type of the aggregates being compared during a DIE comparison.
5879///
5880/// This encapsulates the stack of aggregates being compared at any
5881/// single point.
5882///
5883/// This is useful to detect "comparison cycles" and thus avoid the
5884/// resulting infinite loops.
5885///
5886/// This is also useful for implementing a very important optimization
5887/// that takes place during the canonicalization
5888struct offset_pairs_stack_type
5889{
5890 // The DWARF DWARF reader that is useful for so many things.
5891 const reader& rdr_;
5892 // The set of types that are being compared. This is to speed up
5893 // searches.
5895 // The stack of types that are being compared. The top of the
5896 // stack is the back of the vector.
5898 // A map that associates a redundant type pair to the vector of
5899 // types that depends on it.
5900 offset_pair_vect_map_type redundant_types_;
5901 // A map that associates a dependant type to the vector of redundant
5902 // types it depends on.
5903 offset_pair_vect_map_type dependant_types_;
5904
5905 offset_pairs_stack_type(const reader& rdr)
5906 : rdr_ (rdr)
5907 {}
5908
5909 /// Add a pair of types being compared to the stack of aggregates
5910 /// being compared.
5911 ///
5912 /// @param p the pair of offsets of the type DIEs to consider.
5913 void
5914 add(const offset_pair_type& p)
5915 {
5916 set_.insert(p);
5917 vect_.push_back(p);
5918 }
5919
5920 /// Erase a pair of types being compared from the stack of
5921 /// aggregates being compared.
5922 ///
5923 /// @param p the pair of offsets of the type DIEs to consider.
5924 ///
5925 /// @return true iff @p was found and erased from the stack.
5926 bool
5927 erase(const offset_pair_type& p)
5928 {
5929 if (set_.erase(p))
5930 {
5931 offset_pair_vector_type::iterator i;
5932
5933 for (i = vect_.begin();i < vect_.end(); ++i)
5934 if (*i == p)
5935 break;
5936
5937 if (i != vect_.end())
5938 vect_.erase(i);
5939
5940 return true;
5941 }
5942
5943 return false;
5944 }
5945
5946 /// Test if a pair of type DIEs is part of the stack of type DIEs
5947 /// being compared.
5948 ///
5949 /// @param p the pair of offsets of the type DIEs to consider.
5950 ///
5951 /// @return true iff @p was found in the stack of types being
5952 /// compared.
5953 bool
5954 contains(const offset_pair_type &p) const
5955 {
5956 if (set_.find(p) == set_.end())
5957 return false;
5958 return true;
5959 }
5960
5961 /// Get the set of comparison pair that depends on a given
5962 /// comparison pair.
5963 ///
5964 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5965 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5966 /// words, the pair T appears in the comparison stack BEFORE the
5967 /// pair P.
5968 ///
5969 /// So, this function returns the vector of comparison pairs that
5970 /// appear in the comparison stack AFTER a given comparison pair.
5971 ///
5972 /// @param p the comparison pair to consider.
5973 ///
5974 /// @param pairs out parameter. This is filled with the comparison
5975 /// pairs that depend on @p, iff the function returns true.
5976 ///
5977 /// @return true iff comparison pairs depending on @p have been
5978 /// found and collected in @pairs.
5979 bool
5980 get_pairs_that_depend_on(const offset_pair_type& p,
5981 offset_pair_vector_type& pairs) const
5982 {
5983 bool result = false;
5984 if (!contains(p))
5985 return result;
5986
5987 // First, get an iterator on the position of 'p'.
5988 offset_pair_vector_type::const_iterator i;
5989 for (i = vect_.begin(); i != vect_.end(); ++i)
5990 if (*i == p)
5991 break;
5992
5993 if (i == vect_.end())
5994 return result;
5995
5996 // Then, harvest all the comparison pairs that come after the
5997 // position of 'p'.
5998 for (++i; i != vect_.end(); ++i)
5999 {
6000 pairs.push_back(*i);
6001 result = true;
6002 }
6003
6004 return result;
6005 }
6006
6007 /// Record the fact that a set of comparison pairs depends on a
6008 /// given comparison pair.
6009 ///
6010 /// Set a map that associates each dependant comparison pair to the
6011 /// pair it depends on.
6012 ///
6013 /// @param p the comparison pair that the set depends on.
6014 ///
6015 /// @param dependant_types the set of types that depends on @p.
6016 void
6017 record_dependant_types(const offset_pair_type& p,
6018 const offset_pair_vector_type& dependant_types)
6019 {
6020 for (auto type_pair : dependant_types)
6021 dependant_types_[type_pair].push_back(p);
6022 }
6023
6024 /// Record a comparison pair as being redundant.
6025 ///
6026 ///
6027 /// @param p the comparison pair to record as redundant.
6028 void
6029 record_redundant_type_die_pair(const offset_pair_type& p)
6030 {
6031 offset_pair_vector_type dependant_types;
6032 get_pairs_that_depend_on(p, dependant_types);
6033
6034 // First, record the relationship "p -> [pairs that depend on p]".
6035 auto it = redundant_types_.find(p);
6036 if (it == redundant_types_.end())
6037 {
6038 auto entry = std::make_pair(p, dependant_types);
6039 redundant_types_.insert(entry);
6040 }
6041 else
6042 it->second.insert(it->second.end(),
6043 dependant_types.begin(),
6044 dependant_types.end());
6045
6046 // For each dependant type pair, record the association:
6047 // dependant_pair --> [vect of redundant types]
6048 record_dependant_types(p, dependant_types);
6049 }
6050
6051 /// Test if a given pair has been detected as redundant.
6052 ///
6053 /// @param p the pair of DIEs to consider.
6054 ///
6055 /// @return iff @p is redundant.
6056 bool
6057 is_redundant(const offset_pair_type& p)
6058 {
6059 auto i = redundant_types_.find(p);
6060 if (i != redundant_types_.end())
6061 return true;
6062 return false;
6063 }
6064
6065 /// Test if a given pair is dependant on at least a redundant type.
6066 ///
6067 /// @param p the pair to consider.
6068 ///
6069 /// @return true iff @p depends on a redundant type.
6070 bool
6071 depends_on_redundant_types(const offset_pair_type& p)
6072 {
6073 auto i = dependant_types_.find(p);
6074 if (i == dependant_types_.end())
6075 return false;
6076 return true;
6077 }
6078
6079 /// Remove a redundant pair from the system.
6080 ///
6081 /// This needs updating the system to also remove the dependant
6082 /// types that depend on the redundant pair (if they depend only on
6083 /// that redundant pair).
6084 ///
6085 /// @param p the pair to consider.
6086 ///
6087 /// @param erase_canonical_die_offset if true then erase the cached
6088 /// comparison results for the redundant pair and its dependant
6089 /// types.
6090 void
6091 erase_redundant_type_pair_entry(const offset_pair_type& p,
6092 bool erase_cached_results = false)
6093 {
6094 // First, update the dependant types that depend on the redundant
6095 // type pair
6096 auto redundant_type = redundant_types_.find(p);
6097 if (redundant_type != redundant_types_.end())
6098 {
6099 for (auto dependant_type : redundant_type->second)
6100 {
6101 // Each dependant_type depends on the redundant type 'p',
6102 // among others.
6103 auto dependant_types_it = dependant_types_.find(dependant_type);
6104 ABG_ASSERT(dependant_types_it != dependant_types_.end());
6105 // Erase the redundant type 'p' from the redundant types
6106 // that dependant_type depends on.
6107 {
6108 auto i = dependant_types_it->second.begin();
6109 for (; i!= dependant_types_it->second.end();++i)
6110 if (*i == p)
6111 break;
6112 if (i != dependant_types_it->second.end())
6113 dependant_types_it->second.erase(i);
6114 }
6115 // If the dependant type itself doesn't depend on ANY
6116 // redundant type anymore, then remove the depend type
6117 // from the map of the dependant types.
6118 if (dependant_types_it->second.empty())
6119 {
6120 if (erase_cached_results)
6121 rdr_.die_comparison_results_.erase(dependant_type);
6122 dependant_types_.erase(dependant_types_it);
6123 }
6124 }
6125 }
6126 if (erase_cached_results)
6127 rdr_.die_comparison_results_.erase(p);
6128 redundant_types_.erase(p);
6129 }
6130
6131 /// If a comparison pair has been detected as redundant, stop
6132 /// tracking it as well as its dependant pairs. That will
6133 /// essentially make it impossible to reset/cancel the canonical
6134 /// propagated types for those depdant pairs, but will also save
6135 /// ressources.
6136 ///
6137 /// @param p the comparison pair to consider.
6138 void
6139 confirm_canonical_propagated_type(const offset_pair_type& p)
6140 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
6141
6142 /// Walk the types that depend on a comparison pair and cancel their
6143 /// canonical-propagate-type, that means remove their canonical
6144 /// types and mark them as not being canonically-propagated. Also,
6145 /// erase their cached comparison results that was likely set to
6146 /// COMPARISON_RESULT_UNKNOWN.
6147 ///
6148 /// @param p the pair to consider.
6149 void
6150 cancel_canonical_propagated_type(const offset_pair_type& p)
6151 {
6152 offset_pair_set_type dependant_types;
6153 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
6154 for (auto dependant_type : dependant_types)
6155 {
6156 // If this dependant type was canonical-type-propagated then
6157 // erase that canonical type.
6158 if (rdr_.propagated_types_.find(dependant_type)
6159 != rdr_.propagated_types_.end())
6160 {
6161 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
6162 dependant_type.first.source_,
6163 /*die_as_type=*/true);
6164 rdr_.propagated_types_.erase(dependant_type);
6165 rdr_.cancelled_propagation_count_++;
6166 }
6167 // Update the cached result. We know the comparison result
6168 // must now be different.
6169 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
6170 if (comp_result_it != rdr_.die_comparison_results_.end())
6171 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6172 }
6173
6174 // Update the cached result of the root type to cancel too.
6175 auto comp_result_it = rdr_.die_comparison_results_.find(p);
6176 if (comp_result_it != rdr_.die_comparison_results_.end())
6177 {
6178 // At this point, the result of p is either
6179 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
6180 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
6181 // Make sure it's the cached result is now
6182 // COMPARISON_RESULT_DIFFERENT.
6183 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
6184 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
6185 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
6186 }
6187
6188 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
6189 {
6190 rdr_.erase_canonical_die_offset(p.first.offset_,
6191 p.first.source_,
6192 /*die_as_type=*/true);
6193 rdr_.propagated_types_.erase(p);
6194 rdr_.cancelled_propagation_count_++;
6195 }
6196 }
6197
6198 /// Get the set of comparison pairs that depend on a given pair.
6199 ///
6200 /// @param p the pair to consider.
6201 ///
6202 /// @param result this is set to the pairs that depend on @p, iff
6203 /// the function returned true.
6204 ///
6205 /// @param transitive_closure if set to true, the transitive closure
6206 /// of the @result is set to it.
6207 ///
6208 /// @return true iff @result could be filled with the dependant
6209 /// types.
6210 bool
6211 get_dependant_types(const offset_pair_type& p,
6212 offset_pair_set_type& result,
6213 bool transitive_closure = false)
6214 {
6215 auto i = redundant_types_.find(p);
6216 if (i != redundant_types_.end())
6217 {
6218 for (auto dependant_type : i->second)
6219 if (result.find(dependant_type) == result.end())
6220 {
6221 result.insert(dependant_type);
6222 if (transitive_closure)
6223 get_dependant_types(p, result, /*transitive_closure=*/true);
6224 }
6225 return true;
6226 }
6227 return false;
6228 }
6229}; // end struct offset_pairs_stack_type
6230
6232build_ir_node_from_die(reader& rdr,
6233 Dwarf_Die* die,
6234 scope_decl* scope,
6235 bool called_from_public_decl,
6236 size_t where_offset,
6237 bool is_declaration_only = true,
6238 bool is_required_decl_spec = false);
6239
6241build_ir_node_from_die(reader& rdr,
6242 Dwarf_Die* die,
6243 bool called_from_public_decl,
6244 size_t where_offset);
6245
6246static decl_base_sptr
6247build_ir_node_for_void_type(reader& rdr);
6248
6250build_ir_node_for_void_pointer_type(reader& rdr);
6251
6252static class_decl_sptr
6253add_or_update_class_type(reader& rdr,
6254 Dwarf_Die* die,
6255 scope_decl* scope,
6256 bool is_struct,
6257 class_decl_sptr klass,
6258 bool called_from_public_decl,
6259 size_t where_offset,
6260 bool is_declaration_only);
6261
6262static union_decl_sptr
6263add_or_update_union_type(reader& rdr,
6264 Dwarf_Die* die,
6265 scope_decl* scope,
6266 union_decl_sptr union_type,
6267 bool called_from_public_decl,
6268 size_t where_offset,
6269 bool is_declaration_only);
6270
6271static decl_base_sptr
6272build_ir_node_for_void_type(reader& rdr);
6273
6274static decl_base_sptr
6275build_ir_node_for_variadic_parameter_type(reader &rdr);
6276
6277static function_decl_sptr
6278build_function_decl(reader& rdr,
6279 Dwarf_Die* die,
6280 size_t where_offset,
6282
6283static bool
6284function_is_suppressed(const reader& rdr,
6285 const scope_decl* scope,
6286 Dwarf_Die *function_die,
6287 bool is_declaration_only);
6288
6289static function_decl_sptr
6290build_or_get_fn_decl_if_not_suppressed(reader& rdr,
6291 scope_decl *scope,
6292 Dwarf_Die *die,
6293 size_t where_offset,
6294 bool is_declaration_only,
6296
6297static var_decl_sptr
6298build_var_decl(reader& rdr,
6299 Dwarf_Die *die,
6300 size_t where_offset,
6301 var_decl_sptr result = var_decl_sptr());
6302
6303static var_decl_sptr
6304build_or_get_var_decl_if_not_suppressed(reader& rdr,
6305 scope_decl *scope,
6306 Dwarf_Die *die,
6307 size_t where_offset,
6308 bool is_declaration_only,
6310 bool is_required_decl_spec = false);
6311static bool
6312variable_is_suppressed(const reader& rdr,
6313 const scope_decl* scope,
6314 Dwarf_Die *variable_die,
6315 bool is_declaration_only,
6316 bool is_required_decl_spec = false);
6317
6318static void
6319finish_member_function_reading(Dwarf_Die* die,
6320 const function_decl_sptr& f,
6321 const class_or_union_sptr klass,
6322 reader& rdr);
6323
6324/// Test if a given DIE is anonymous
6325///
6326/// @param die the DIE to consider.
6327///
6328/// @return true iff @p die is anonymous.
6329static bool
6330die_is_anonymous(const Dwarf_Die* die)
6331{
6332 Dwarf_Attribute attr;
6333 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
6334 return true;
6335 return false;
6336}
6337
6338/// Test if a DIE is an anonymous data member, aka, "unnamed field".
6339///
6340/// Unnamed fields are specified at
6341/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
6342///
6343/// @param die the DIE to consider.
6344///
6345/// @return true iff @p die is an anonymous data member.
6346static bool
6347die_is_anonymous_data_member(const Dwarf_Die* die)
6348{
6349 if (!die
6350 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
6351 || !die_name(die).empty())
6352 return false;
6353
6354 Dwarf_Die type_die;
6355 if (!die_die_attribute(die, DW_AT_type, type_die))
6356 return false;
6357
6358 if (dwarf_tag(&type_die) != DW_TAG_structure_type
6359 && dwarf_tag(&type_die) != DW_TAG_union_type)
6360 return false;
6361
6362 return true;
6363}
6364
6365/// Get the value of an attribute that is supposed to be a string, or
6366/// an empty string if the attribute could not be found.
6367///
6368/// @param die the DIE to get the attribute value from.
6369///
6370/// @param attr_name the attribute name. Must come from dwarf.h and
6371/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6372///
6373/// @return the string representing the value of the attribute, or an
6374/// empty string if no string attribute could be found.
6375static string
6376die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
6377{
6378 if (!die)
6379 return "";
6380
6381 Dwarf_Attribute attr;
6382 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6383 return "";
6384
6385 const char* str = dwarf_formstring(&attr);
6386 return str ? str : "";
6387}
6388
6389/// Get the value of an attribute that is supposed to be a string, or
6390/// an empty string if the attribute could not be found.
6391///
6392/// @param die the DIE to get the attribute value from.
6393///
6394/// @param attr_name the attribute name. Must come from dwarf.h and
6395/// be an enumerator representing an attribute like, e.g, DW_AT_name.
6396///
6397/// @return the char* representing the value of the attribute, or an
6398/// empty string if no string attribute could be found.
6399static const char*
6400die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
6401{
6402 if (!die)
6403 return nullptr;
6404
6405 Dwarf_Attribute attr;
6406 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6407 return nullptr;
6408
6409 const char* str = dwarf_formstring(&attr);
6410 return str;
6411}
6412
6413/// Get the value of an attribute that is supposed to be an unsigned
6414/// constant.
6415///
6416/// @param die the DIE to read the information from.
6417///
6418/// @param attr_name the DW_AT_* name of the attribute. Must come
6419/// from dwarf.h and be an enumerator representing an attribute like,
6420/// e.g, DW_AT_decl_line.
6421///
6422///@param cst the output parameter that is set to the value of the
6423/// attribute @p attr_name. This parameter is set iff the function
6424/// return true.
6425///
6426/// @return true if there was an attribute of the name @p attr_name
6427/// and with a value that is a constant, false otherwise.
6428static bool
6429die_unsigned_constant_attribute(const Dwarf_Die* die,
6430 unsigned attr_name,
6431 uint64_t& cst)
6432{
6433 if (!die)
6434 return false;
6435
6436 Dwarf_Attribute attr;
6437 Dwarf_Word result = 0;
6438 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6439 || dwarf_formudata(&attr, &result))
6440 return false;
6441
6442 cst = result;
6443 return true;
6444}
6445
6446/// Read a signed constant value from a given attribute.
6447///
6448/// The signed constant expected must be of constant form.
6449///
6450/// @param die the DIE to get the attribute from.
6451///
6452/// @param attr_name the attribute name.
6453///
6454/// @param cst the resulting signed constant read.
6455///
6456/// @return true iff a signed constant attribute of the name @p
6457/// attr_name was found on the DIE @p die.
6458static bool
6459die_signed_constant_attribute(const Dwarf_Die *die,
6460 unsigned attr_name,
6461 int64_t& cst)
6462{
6463 if (!die)
6464 return false;
6465
6466 Dwarf_Attribute attr;
6467 Dwarf_Sword result = 0;
6468 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6469 || dwarf_formsdata(&attr, &result))
6470 return false;
6471
6472 cst = result;
6473 return true;
6474}
6475
6476/// Read the value of a constant attribute that is either signed or
6477/// unsigned into a array_type_def::subrange_type::bound_value value.
6478///
6479/// The bound_value instance will capture the actual signedness of the
6480/// read attribute.
6481///
6482/// @param die the DIE from which to read the value of the attribute.
6483///
6484/// @param attr_name the attribute name to consider.
6485///
6486/// @param is_signed true if the attribute value has to read as
6487/// signed.
6488///
6489/// @param value the resulting value read from attribute @p attr_name
6490/// on DIE @p die.
6491///
6492/// @return true iff DIE @p die has an attribute named @p attr_name
6493/// with a constant value.
6494static bool
6495die_constant_attribute(const Dwarf_Die *die,
6496 unsigned attr_name,
6497 bool is_signed,
6498 array_type_def::subrange_type::bound_value &value)
6499{
6500 if (!is_signed)
6501 {
6502 uint64_t l = 0;
6503 if (!die_unsigned_constant_attribute(die, attr_name, l))
6504 return false;
6505 value.set_unsigned(l);
6506 }
6507 else
6508 {
6509 int64_t l = 0;
6510 if (!die_signed_constant_attribute(die, attr_name, l))
6511 return false;
6512 value.set_signed(l);
6513 }
6514 return true;
6515}
6516
6517/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6518///
6519/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6520/// enum in dwarf.h so we have to use an unsigned int for the form,
6521/// grrr.
6522///
6523/// @param form the form to consider.
6524///
6525/// @return true iff @p form is DW_FORM_strx{1,4}.
6526static bool
6527form_is_DW_FORM_strx(unsigned form)
6528{
6529 if (form)
6530 {
6531#if defined HAVE_DW_FORM_strx1 \
6532 && defined HAVE_DW_FORM_strx2 \
6533 && defined HAVE_DW_FORM_strx3 \
6534 && defined HAVE_DW_FORM_strx4
6535 if (form == DW_FORM_strx1
6536 || form == DW_FORM_strx2
6537 || form == DW_FORM_strx3
6538 ||form == DW_FORM_strx4)
6539 return true;
6540#endif
6541 }
6542 return false;
6543}
6544
6545/// Test if a given DWARF form is DW_FORM_line_strp.
6546///
6547/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6548/// enum in dwarf.h so we have to use an unsigned int for the form,
6549/// grrr.
6550///
6551/// @param form the form to consider.
6552///
6553/// @return true iff @p form is DW_FORM_line_strp.
6554static bool
6555form_is_DW_FORM_line_strp(unsigned form)
6556{
6557 if (form)
6558 {
6559#if defined HAVE_DW_FORM_line_strp
6560 if (form == DW_FORM_line_strp)
6561 return true;
6562#endif
6563 }
6564 return false;
6565}
6566
6567/// Get the value of a DIE attribute; that value is meant to be a
6568/// flag.
6569///
6570/// @param die the DIE to get the attribute from.
6571///
6572/// @param attr_name the DW_AT_* name of the attribute. Must come
6573/// from dwarf.h and be an enumerator representing an attribute like,
6574/// e.g, DW_AT_external.
6575///
6576/// @param flag the output parameter to store the flag value into.
6577/// This is set iff the function returns true.
6578///
6579/// @param recursively if true, the function looks through the
6580/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6581/// all the way down to the initial DIE that is cloned and look on
6582/// that DIE to see if it has the @p attr_name attribute.
6583///
6584/// @return true if the DIE has a flag attribute named @p attr_name,
6585/// false otherwise.
6586static bool
6587die_flag_attribute(const Dwarf_Die* die,
6588 unsigned attr_name,
6589 bool& flag,
6590 bool recursively = true)
6591{
6592 Dwarf_Attribute attr;
6593 if (recursively
6594 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6595 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6596 return false;
6597
6598 bool f = false;
6599 if (dwarf_formflag(&attr, &f))
6600 return false;
6601
6602 flag = f;
6603 return true;
6604}
6605
6606/// Get the mangled name from a given DIE.
6607///
6608/// @param die the DIE to read the mangled name from.
6609///
6610/// @return the mangled name if it's present in the DIE, or just an
6611/// empty string if it's not.
6612static string
6613die_linkage_name(const Dwarf_Die* die)
6614{
6615 if (!die)
6616 return "";
6617
6618 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6619 if (linkage_name.empty())
6620 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6621 return linkage_name;
6622}
6623
6624/// Get the file path that is the value of the DW_AT_decl_file
6625/// attribute on a given DIE, if the DIE is a decl DIE having that
6626/// attribute.
6627///
6628/// @param die the DIE to consider.
6629///
6630/// @return a string containing the file path that is the logical
6631/// value of the DW_AT_decl_file attribute. If the DIE @p die
6632/// doesn't have a DW_AT_decl_file attribute, then the return value is
6633/// just an empty string.
6634static string
6635die_decl_file_attribute(const Dwarf_Die* die)
6636{
6637 if (!die)
6638 return "";
6639
6640 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6641
6642 return str ? str : "";
6643}
6644
6645/// Get the value of an attribute which value is supposed to be a
6646/// reference to a DIE.
6647///
6648/// @param die the DIE to read the value from.
6649///
6650/// @param attr_name the DW_AT_* attribute name to read.
6651///
6652/// @param result the DIE resulting from reading the attribute value.
6653/// This is set iff the function returns true.
6654///
6655/// @param recursively if true, the function looks through the
6656/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6657/// all the way down to the initial DIE that is cloned and look on
6658/// that DIE to see if it has the @p attr_name attribute.
6659///
6660/// @return true if the DIE @p die contains an attribute named @p
6661/// attr_name that is a DIE reference, false otherwise.
6662static bool
6663die_die_attribute(const Dwarf_Die* die,
6664 unsigned attr_name,
6665 Dwarf_Die& result,
6666 bool recursively)
6667{
6668 Dwarf_Attribute attr;
6669 if (recursively
6670 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6671 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6672 return false;
6673
6674 return dwarf_formref_die(&attr, &result);
6675}
6676
6677/// Get the DIE that is the "origin" of the current one.
6678///
6679/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6680/// attribute. Those DIEs represent a concrete instance of an
6681/// abstract entity. The concrete instance can be a concrete instance
6682/// of an inline function, or the concrete implementation of an
6683/// abstract interface. On both cases, we call the abstract instance
6684/// from which the concrete instance derives the "origin".
6685///
6686/// This function returns the ultimate origin DIE of a given DIE by
6687/// following the chain of its DW_AT_abstract_origin and
6688/// DW_AT_specification attributes.
6689///
6690/// @param die the DIE to consider.
6691///
6692/// @param origin_die this is an output parameter that is set by this
6693/// function to the resulting origin DIE iff the function returns
6694/// true.
6695///
6696/// @return true iff the function actually found an origin DIE and
6697/// set it to the @p origin_die parameter.
6698static bool
6699die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6700{
6701 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6702 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6703 {
6704 while (die_die_attribute(&origin_die,
6705 DW_AT_specification,
6706 origin_die, true)
6707 || die_die_attribute(&origin_die,
6708 DW_AT_abstract_origin,
6709 origin_die, true))
6710 {
6711 // Keep looking for the origin die ...
6712 ;
6713 }
6714 return true;
6715 }
6716 return false;
6717}
6718
6719/// Test if a subrange DIE indirectly references another subrange DIE
6720/// through a given attribute.
6721///
6722/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6723/// attribute be a reference to either a data member or a variable
6724/// which type is itself a DW_TAG_subrange_type. This latter subrange
6725/// DIE is said to be "indirectly referenced" by the former subrange
6726/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6727/// the value we want for the DW_AT_upper_bound of the former.
6728///
6729/// This function tests if the former subrange DIE does indirectly
6730/// reference another subrange DIE through a given attribute (not
6731/// necessarily DW_AT_upper_bound).
6732///
6733/// @param die the DIE to consider. Note that It must be a
6734/// DW_TAG_subrange_type.
6735///
6736/// @param attr_name the name of the attribute to look through for the
6737/// indirectly referenced subrange DIE.
6738///
6739/// @param referenced_subrange if the function returns true, then the
6740/// argument of this parameter is set to the indirectly referenced
6741/// DW_TAG_subrange_type DIE.
6742///
6743/// @return true iff @p DIE indirectly references a subrange DIE
6744/// through the attribute @p attr_name.
6745static bool
6746subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6747 unsigned attr_name,
6748 Dwarf_Die& referenced_subrange)
6749{
6750 bool result = false;
6751
6752 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6753 return result;
6754
6755 Dwarf_Die referenced_die;
6756 if (die_die_attribute(die, attr_name, referenced_die))
6757 {
6758 unsigned tag = dwarf_tag(&referenced_die);
6759 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6760 {
6761 Dwarf_Die type_die;
6762 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6763 {
6764 tag = dwarf_tag(&type_die);
6765 if (tag == DW_TAG_subrange_type)
6766 {
6767 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6768 result = true;
6769 }
6770 }
6771 }
6772 }
6773 return result;
6774}
6775
6776/// Return the bound value of subrange die by looking at an indirectly
6777/// referenced subrange DIE.
6778///
6779/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6780/// attribute be a reference to either a data member or a variable
6781/// which type is itself a DW_TAG_subrange_type. This latter subrange
6782/// DIE is said to be "indirectly referenced" by the former subrange
6783/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6784/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6785///
6786/// This function gets the DW_AT_{lower,upper}_bound value of a
6787/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6788/// the indirectly referenced subrange type, if it exists.
6789///
6790/// @param die the subrange DIE to consider.
6791///
6792/// @param attr_name the name of the attribute to consider, typically,
6793/// DW_AT_{lower,upper}_bound.
6794///
6795/// @param v the found value, iff this function returned true.
6796///
6797/// @param is_signed, this is set to true if @p v is signed. This
6798/// parameter is set at all only if the function returns true.
6799///
6800/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6801/// indirectly referenced subrange type.
6802static bool
6803subrange_die_indirect_bound_value(const Dwarf_Die *die,
6804 unsigned attr_name,
6805 array_type_def::subrange_type::bound_value& v,
6806 bool& is_signed)
6807{
6808 bool result = false;
6809
6810 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6811 return result;
6812
6813 Dwarf_Die subrange_die;
6814 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6815 subrange_die))
6816 {
6817 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6818 result = true;
6819 }
6820 return result;
6821}
6822
6823/// Read and return an addresss class attribute from a given DIE.
6824///
6825/// @param die the DIE to consider.
6826///
6827/// @param attr_name the name of the address class attribute to read
6828/// the value from.
6829///
6830/// @param the resulting address.
6831///
6832/// @return true iff the attribute could be read, was of the expected
6833/// address class and could thus be translated into the @p result.
6834static bool
6835die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6836{
6837 Dwarf_Attribute attr;
6838 if (!dwarf_attr_integrate(die, attr_name, &attr))
6839 return false;
6840 return dwarf_formaddr(&attr, &result) == 0;
6841}
6842
6843/// Returns the source location associated with a decl DIE.
6844///
6845/// @param rdr the @ref reader to use.
6846///
6847/// @param die the DIE the read the source location from.
6848///
6849/// @return the location associated with @p die.
6850static location
6851die_location(const reader& rdr, const Dwarf_Die* die)
6852{
6853 if (!die)
6854 return location();
6855
6856 string file = die_decl_file_attribute(die);
6857 uint64_t line = 0;
6858 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6859
6860 if (!file.empty() && line != 0)
6861 {
6862 translation_unit_sptr tu = rdr.cur_transl_unit();
6863 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6864 return l;
6865 }
6866 return location();
6867}
6868
6869/// Return a copy of the name of a DIE.
6870///
6871/// @param die the DIE to consider.
6872///
6873/// @return a copy of the name of the DIE.
6874static string
6875die_name(const Dwarf_Die* die)
6876{
6877 string name = die_string_attribute(die, DW_AT_name);
6878 return name;
6879}
6880
6881/// Return the location, the name and the mangled name of a given DIE.
6882///
6883/// @param rdr the DWARF reader to use.
6884///
6885/// @param die the DIE to read location and names from.
6886///
6887/// @param loc the location output parameter to set.
6888///
6889/// @param name the name output parameter to set.
6890///
6891/// @param linkage_name the linkage_name output parameter to set.
6892static void
6893die_loc_and_name(const reader& rdr,
6894 Dwarf_Die* die,
6895 location& loc,
6896 string& name,
6897 string& linkage_name)
6898{
6899 loc = die_location(rdr, die);
6900 name = die_name(die);
6901 linkage_name = die_linkage_name(die);
6902}
6903
6904/// Return the name and the mangled name of a given DIE.
6905///
6906/// @param die the DIE to read location and names from.
6907///
6908/// @param name the name output parameter to set.
6909///
6910/// @param linkage_name the linkage_name output parameter to set.
6911static void
6912die_name_and_linkage_name(const Dwarf_Die* die,
6913 string& name,
6914 string& linkage_name)
6915{
6916 name = die_name(die);
6917 linkage_name = die_linkage_name(die);
6918}
6919
6920/// Get the size of a (type) DIE as the value for the parameter
6921/// DW_AT_byte_size or DW_AT_bit_size.
6922///
6923/// @param die the DIE to read the information from.
6924///
6925/// @param size the resulting size in bits. This is set iff the
6926/// function return true.
6927///
6928/// @return true if the size attribute was found.
6929static bool
6930die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6931{
6932 if (!die)
6933 return false;
6934
6935 uint64_t byte_size = 0, bit_size = 0;
6936
6937 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6938 {
6939 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6940 return false;
6941 }
6942 else
6943 bit_size = byte_size * 8;
6944
6945 size = bit_size;
6946
6947 return true;
6948}
6949
6950/// Get the access specifier (from the DW_AT_accessibility attribute
6951/// value) of a given DIE.
6952///
6953/// @param die the DIE to consider.
6954///
6955/// @param access the resulting access. This is set iff the function
6956/// returns true.
6957///
6958/// @return bool if the DIE contains the DW_AT_accessibility die.
6959static bool
6960die_access_specifier(Dwarf_Die * die, access_specifier& access)
6961{
6962 if (!die)
6963 return false;
6964
6965 uint64_t a = 0;
6966 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6967 return false;
6968
6969 access_specifier result = private_access;
6970
6971 switch (a)
6972 {
6973 case private_access:
6974 result = private_access;
6975 break;
6976
6977 case protected_access:
6978 result = protected_access;
6979 break;
6980
6981 case public_access:
6982 result = public_access;
6983 break;
6984
6985 default:
6986 break;
6987 }
6988
6989 access = result;
6990 return true;
6991}
6992
6993/// Test whether a given DIE represents a decl that is public. That
6994/// is, one with the DW_AT_external attribute set.
6995///
6996/// @param die the DIE to consider for testing.
6997///
6998/// @return true if a DW_AT_external attribute is present and its
6999/// value is set to the true; return false otherwise.
7000static bool
7001die_is_public_decl(const Dwarf_Die* die)
7002{
7003 if (!die)
7004 return false;
7005 bool is_public = false;
7006
7007 // If this is a DW_TAG_subprogram DIE, look for the
7008 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
7009 // then it's public. In all other cases, this should return false.
7010
7011 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7012 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
7013 die_flag_attribute(die, DW_AT_external, is_public);
7014 else if (tag == DW_TAG_namespace)
7015 {
7016 string name = die_name(die);
7017 is_public = !name.empty();
7018 }
7019
7020 return is_public;
7021}
7022
7023/// Test if a DIE is effectively public.
7024///
7025/// This is meant to return true when either the DIE is public or when
7026/// it's a variable DIE that is at (global) namespace level.
7027///
7028/// @return true iff either the DIE is public or is a variable DIE
7029/// that is at (global) namespace level.
7030static bool
7031die_is_effectively_public_decl(const reader& rdr,
7032 const Dwarf_Die* die)
7033{
7034 if (die_is_public_decl(die))
7035 return true;
7036
7037 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7038 if (tag == DW_TAG_variable || tag == DW_TAG_member)
7039 {
7040 // The DIE is a variable.
7041 Dwarf_Die parent_die;
7042 size_t where_offset = 0;
7043 if (!get_parent_die(rdr, die, parent_die, where_offset))
7044 return false;
7045
7046 tag = dwarf_tag(&parent_die);
7047 if (tag == DW_TAG_compile_unit
7048 || tag == DW_TAG_partial_unit
7049 || tag == DW_TAG_type_unit)
7050 // The DIE is at global scope.
7051 return true;
7052
7053 if (tag == DW_TAG_namespace)
7054 {
7055 string name = die_name(&parent_die);
7056 if (name.empty())
7057 // The DIE at unnamed namespace scope, so it's not public.
7058 return false;
7059 // The DIE is at namespace scope.
7060 return true;
7061 }
7062 }
7063 return false;
7064}
7065
7066/// Test whether a given DIE represents a declaration-only DIE.
7067///
7068/// That is, if the DIE has the DW_AT_declaration flag set.
7069///
7070/// @param die the DIE to consider.
7071//
7072/// @return true if a DW_AT_declaration is present, false otherwise.
7073static bool
7074die_is_declaration_only(Dwarf_Die* die)
7075{
7076 bool is_declaration = false;
7077 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
7078 if (is_declaration && (!die_has_size_attribute(die)
7079 || !die_has_children(die)))
7080 return true;
7081 return false;
7082}
7083
7084/// Test if a DIE is for a function decl.
7085///
7086/// @param die the DIE to consider.
7087///
7088/// @return true iff @p die represents a function decl.
7089static bool
7090die_is_function_decl(const Dwarf_Die *die)
7091{
7092 if (!die)
7093 return false;
7094
7095 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7096 if (tag == DW_TAG_subprogram)
7097 return true;
7098 return false;
7099}
7100
7101/// Test if a DIE is for a variable decl.
7102///
7103/// @param die the DIE to consider.
7104///
7105/// @return true iff @p die represents a variable decl.
7106static bool
7107die_is_variable_decl(const Dwarf_Die *die)
7108{
7109 if (!die)
7110 return false;
7111
7112 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7113 if (tag == DW_TAG_variable)
7114 return true;
7115 return false;
7116}
7117
7118/// Test if a DIE has size attribute.
7119///
7120/// @param die the DIE to consider.
7121///
7122/// @return true if the DIE has a size attribute.
7123static bool
7124die_has_size_attribute(const Dwarf_Die *die)
7125{
7126 uint64_t s;
7127 if (die_size_in_bits(die, s))
7128 return true;
7129 return false;
7130}
7131
7132/// Test that a DIE has no child DIE.
7133///
7134/// @param die the DIE to consider.
7135///
7136/// @return true iff @p die has no child DIE.
7137static bool
7138die_has_no_child(const Dwarf_Die *die)
7139{
7140 if (!die)
7141 return true;
7142
7143 Dwarf_Die child;
7144 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7145 return false;
7146 return true;
7147}
7148
7149/// Test whether a given DIE represents a declaration-only DIE.
7150///
7151/// That is, if the DIE has the DW_AT_declaration flag set.
7152///
7153/// @param die the DIE to consider.
7154//
7155/// @return true if a DW_AT_declaration is present, false otherwise.
7156static bool
7157die_is_declaration_only(const Dwarf_Die* die)
7158{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
7159
7160/// Tests whether a given DIE is artificial.
7161///
7162/// @param die the test to test for.
7163///
7164/// @return true if the DIE is artificial, false otherwise.
7165static bool
7166die_is_artificial(Dwarf_Die* die)
7167{
7168 bool is_artificial;
7169 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
7170}
7171
7172///@return true if a tag represents a type, false otherwise.
7173///
7174///@param tag the tag to consider.
7175static bool
7176is_type_tag(unsigned tag)
7177{
7178 bool result = false;
7179
7180 switch (tag)
7181 {
7182 case DW_TAG_array_type:
7183 case DW_TAG_class_type:
7184 case DW_TAG_enumeration_type:
7185 case DW_TAG_pointer_type:
7186 case DW_TAG_reference_type:
7187 case DW_TAG_string_type:
7188 case DW_TAG_structure_type:
7189 case DW_TAG_subroutine_type:
7190 case DW_TAG_typedef:
7191 case DW_TAG_union_type:
7192 case DW_TAG_ptr_to_member_type:
7193 case DW_TAG_set_type:
7194 case DW_TAG_subrange_type:
7195 case DW_TAG_base_type:
7196 case DW_TAG_const_type:
7197 case DW_TAG_file_type:
7198 case DW_TAG_packed_type:
7199 case DW_TAG_thrown_type:
7200 case DW_TAG_volatile_type:
7201 case DW_TAG_restrict_type:
7202 case DW_TAG_interface_type:
7203 case DW_TAG_unspecified_type:
7204 case DW_TAG_shared_type:
7205 case DW_TAG_rvalue_reference_type:
7206 case DW_TAG_coarray_type:
7207 case DW_TAG_atomic_type:
7208 case DW_TAG_immutable_type:
7209 result = true;
7210 break;
7211
7212 default:
7213 result = false;
7214 break;
7215 }
7216
7217 return result;
7218}
7219
7220/// Test if a given DIE is a type whose canonical type is to be
7221/// propagated during DIE canonicalization
7222///
7223/// This is a sub-routine of compare_dies.
7224///
7225/// @param tag the tag of the DIE to consider.
7226///
7227/// @return true iff the DIE of tag @p tag is can see its canonical
7228/// type be propagated during the type comparison that happens during
7229/// DIE canonicalization.
7230static bool
7231is_canon_type_to_be_propagated_tag(unsigned tag)
7232{
7233 bool result = false;
7234
7235 switch (tag)
7236 {
7237 case DW_TAG_class_type:
7238 case DW_TAG_structure_type:
7239 case DW_TAG_union_type:
7240 case DW_TAG_subroutine_type:
7241 case DW_TAG_subprogram:
7242 result = true;
7243 break;
7244
7245 default:
7246 result = false;
7247 break;
7248 }
7249
7250 return result;
7251}
7252
7253/// Test if a given kind of DIE ought to have its comparison result
7254/// cached by compare_dies, so that subsequent invocations of
7255/// compare_dies can be faster.
7256///
7257/// @param tag the tag of the DIE to consider.
7258///
7259/// @return true iff DIEs of the tag @p tag ought to have its
7260/// comparison results cached.
7261static bool
7262type_comparison_result_to_be_cached(unsigned tag)
7263{
7264 bool r = false;
7265 switch (tag)
7266 {
7267 case DW_TAG_class_type:
7268 case DW_TAG_structure_type:
7269 case DW_TAG_union_type:
7270 case DW_TAG_subroutine_type:
7271 case DW_TAG_subprogram:
7272 r = true;
7273 break;
7274
7275 default:
7276 r = false;
7277 break;
7278 }
7279 return r;
7280}
7281
7282/// Cache the result of comparing to type DIEs.
7283///
7284/// @param rdr the context to consider.
7285///
7286/// @param tag the tag of the DIEs to consider.
7287///
7288/// @param p the offsets of the pair of DIEs being compared.
7289///
7290/// @param result the comparison result to be cached.
7291static bool
7292maybe_cache_type_comparison_result(const reader& rdr,
7293 int tag,
7294 const offset_pair_type& p,
7295 comparison_result result)
7296{
7297 if (!type_comparison_result_to_be_cached(tag)
7298 || (result != COMPARISON_RESULT_EQUAL
7299 && result != COMPARISON_RESULT_DIFFERENT))
7300 return false;
7301
7302 rdr.die_comparison_results_[p] = result;
7303
7304 return true;
7305
7306}
7307
7308/// Get the cached result of the comparison of a pair of DIEs.
7309///
7310/// @param rdr the context to consider.
7311///
7312/// @param tag the tag of the pair of DIEs to consider.
7313///
7314/// @param p the offsets of the pair of DIEs to consider.
7315///
7316/// @param result out parameter set to the cached result of the
7317/// comparison of @p p if it has been found.
7318///
7319/// @return true iff a cached result for the comparisonof @p has been
7320/// found and set into @p result.
7321static bool
7322get_cached_type_comparison_result(const reader& rdr,
7323 const offset_pair_type& p,
7324 comparison_result& result)
7325{
7326 auto i = rdr.die_comparison_results_.find(p);
7327 if (i != rdr.die_comparison_results_.end())
7328 {
7329 result = i->second;
7330 return true;
7331 }
7332 return false;
7333}
7334
7335/// Get the cached result of the comparison of a pair of DIEs, if the
7336/// kind of DIEs ought to have its comparison results cached.
7337///
7338/// @param rdr the context to consider.
7339///
7340/// @param tag the tag of the pair of DIEs to consider.
7341///
7342/// @param p the offsets of the pair of DIEs to consider.
7343///
7344/// @param result out parameter set to the cached result of the
7345/// comparison of @p p if it has been found.
7346///
7347/// @return true iff a cached result for the comparisonof @p has been
7348/// found and set into @p result.
7349static bool
7350maybe_get_cached_type_comparison_result(const reader& rdr,
7351 int tag,
7352 const offset_pair_type& p,
7353 comparison_result& result)
7354{
7355 if (type_comparison_result_to_be_cached(tag))
7356 {
7357 // Types of this kind might have their comparison result cached
7358 // when they are not canonicalized. So let's see if we have a
7359 // cached comparison result.
7360 if (get_cached_type_comparison_result(rdr, p, result))
7361 return true;
7362 }
7363 return false;
7364}
7365
7366/// Test if a given DIE is to be canonicalized.
7367///
7368/// @param die the DIE to consider.
7369///
7370/// @return true iff @p die is to be canonicalized.
7371static bool
7372is_type_die_to_be_canonicalized(const Dwarf_Die *die)
7373{
7374 bool result = false;
7375 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7376
7377 if (!is_type_tag(tag))
7378 return false;
7379
7380 switch (tag)
7381 {
7382 case DW_TAG_class_type:
7383 case DW_TAG_structure_type:
7384 case DW_TAG_union_type:
7385 result = !die_is_declaration_only(die);
7386 break;
7387
7388 case DW_TAG_subroutine_type:
7389 case DW_TAG_subprogram:
7390 case DW_TAG_array_type:
7391 result = true;
7392
7393 default:
7394 break;
7395 }
7396
7397 return result;
7398}
7399
7400/// Test if a DIE tag represents a declaration.
7401///
7402/// @param tag the DWARF tag to consider.
7403///
7404/// @return true iff @p tag is for a declaration.
7405static bool
7406is_decl_tag(unsigned tag)
7407{
7408 switch (tag)
7409 {
7410 case DW_TAG_formal_parameter:
7411 case DW_TAG_imported_declaration:
7412 case DW_TAG_member:
7413 case DW_TAG_unspecified_parameters:
7414 case DW_TAG_subprogram:
7415 case DW_TAG_variable:
7416 case DW_TAG_namespace:
7417 case DW_TAG_GNU_template_template_param:
7418 case DW_TAG_GNU_template_parameter_pack:
7419 case DW_TAG_GNU_formal_parameter_pack:
7420 return true;
7421 }
7422 return false;
7423}
7424
7425/// Test if a DIE represents a type DIE.
7426///
7427/// @param die the DIE to consider.
7428///
7429/// @return true if @p die represents a type, false otherwise.
7430static bool
7431die_is_type(const Dwarf_Die* die)
7432{
7433 if (!die)
7434 return false;
7435 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7436}
7437
7438/// Test if a DIE represents a declaration.
7439///
7440/// @param die the DIE to consider.
7441///
7442/// @return true if @p die represents a decl, false otherwise.
7443static bool
7444die_is_decl(const Dwarf_Die* die)
7445{
7446 if (!die)
7447 return false;
7448 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
7449}
7450
7451/// Test if a DIE represents a namespace.
7452///
7453/// @param die the DIE to consider.
7454///
7455/// @return true if @p die represents a namespace, false otherwise.
7456static bool
7457die_is_namespace(const Dwarf_Die* die)
7458{
7459 if (!die)
7460 return false;
7461 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7462}
7463
7464/// Test if a DIE has tag DW_TAG_unspecified_type.
7465///
7466/// @param die the DIE to consider.
7467///
7468/// @return true if @p die has tag DW_TAG_unspecified_type.
7469static bool
7470die_is_unspecified(Dwarf_Die* die)
7471{
7472 if (!die)
7473 return false;
7474 return (dwarf_tag(die) == DW_TAG_unspecified_type);
7475}
7476
7477/// Test if a DIE represents a void type.
7478///
7479/// @param die the DIE to consider.
7480///
7481/// @return true if @p die represents a void type, false otherwise.
7482static bool
7483die_is_void_type(Dwarf_Die* die)
7484{
7485 if (!die || dwarf_tag(die) != DW_TAG_base_type)
7486 return false;
7487
7488 string name = die_name(die);
7489 if (name == "void")
7490 return true;
7491
7492 return false;
7493}
7494
7495/// Test if a DIE represents a pointer type.
7496///
7497/// @param die the die to consider.
7498///
7499/// @return true iff @p die represents a pointer type.
7500static bool
7501die_is_pointer_type(const Dwarf_Die* die)
7502{
7503 if (!die)
7504 return false;
7505
7506 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7507 if (tag == DW_TAG_pointer_type)
7508 return true;
7509
7510 return false;
7511}
7512
7513/// Test if a DIE is for a pointer, reference or qualified type to
7514/// anonymous class or struct.
7515///
7516/// @param die the DIE to consider.
7517///
7518/// @return true iff @p is for a pointer, reference or qualified type
7519/// to anonymous class or struct.
7520static bool
7521pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7522{
7523 if (!die_is_pointer_array_or_reference_type(die)
7524 && !die_is_qualified_type(die))
7525 return false;
7526
7527 Dwarf_Die underlying_type_die;
7528 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7529 return false;
7530
7531 if (!die_is_class_type(&underlying_type_die))
7532 return false;
7533
7534 string name = die_name(&underlying_type_die);
7535
7536 return name.empty();
7537}
7538
7539/// Test if a DIE represents a reference type.
7540///
7541/// @param die the die to consider.
7542///
7543/// @return true iff @p die represents a reference type.
7544static bool
7545die_is_reference_type(const Dwarf_Die* die)
7546{
7547 if (!die)
7548 return false;
7549
7550 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7551 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7552 return true;
7553
7554 return false;
7555}
7556
7557/// Test if a DIE represents an array type.
7558///
7559/// @param die the die to consider.
7560///
7561/// @return true iff @p die represents an array type.
7562static bool
7563die_is_array_type(const Dwarf_Die* die)
7564{
7565 if (!die)
7566 return false;
7567
7568 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7569 if (tag == DW_TAG_array_type)
7570 return true;
7571
7572 return false;
7573}
7574
7575/// Test if a DIE represents a pointer, reference or array type.
7576///
7577/// @param die the die to consider.
7578///
7579/// @return true iff @p die represents a pointer or reference type.
7580static bool
7581die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7582{return (die_is_pointer_type(die)
7583 || die_is_reference_type(die)
7584 || die_is_array_type(die));}
7585
7586/// Test if a DIE represents a pointer or a reference type.
7587///
7588/// @param die the die to consider.
7589///
7590/// @return true iff @p die represents a pointer or reference type.
7591static bool
7592die_is_pointer_or_reference_type(const Dwarf_Die* die)
7593{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7594
7595/// Test if a DIE represents a pointer, a reference or a typedef type.
7596///
7597/// @param die the die to consider.
7598///
7599/// @return true iff @p die represents a pointer, a reference or a
7600/// typedef type.
7601static bool
7602die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7603{return (die_is_pointer_array_or_reference_type(die)
7604 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7605
7606/// Test if a DIE represents a class type.
7607///
7608/// @param die the die to consider.
7609///
7610/// @return true iff @p die represents a class type.
7611static bool
7612die_is_class_type(const Dwarf_Die* die)
7613{
7614 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7615
7616 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7617 return true;
7618
7619 return false;
7620}
7621
7622/// Test if a DIE is for a qualified type.
7623///
7624/// @param die the DIE to consider.
7625///
7626/// @return true iff @p die is for a qualified type.
7627static bool
7628die_is_qualified_type(const Dwarf_Die* die)
7629{
7630 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7631 if (tag == DW_TAG_const_type
7632 || tag == DW_TAG_volatile_type
7633 || tag == DW_TAG_restrict_type)
7634 return true;
7635
7636 return false;
7637}
7638
7639/// Test if a DIE is for a function type.
7640///
7641/// @param die the DIE to consider.
7642///
7643/// @return true iff @p die is for a function type.
7644static bool
7645die_is_function_type(const Dwarf_Die *die)
7646{
7647 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7648 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7649 return true;
7650
7651 return false;
7652}
7653
7654/// Test if a DIE for a function pointer or member function has an
7655/// DW_AT_object_pointer attribute.
7656///
7657/// @param die the DIE to consider.
7658///
7659/// @param object_pointer out parameter. It's set to the DIE for the
7660/// object pointer iff the function returns true.
7661///
7662/// @return true iff the DIE @p die has an object pointer. In that
7663/// case, the parameter @p object_pointer is set to the DIE of that
7664/// object pointer.
7665static bool
7666die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7667{
7668 if (!die)
7669 return false;
7670
7671 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7672 return true;
7673
7674 return false;
7675}
7676
7677/// Test if a DIE has children DIEs.
7678///
7679/// @param die the DIE to consider.
7680///
7681/// @return true iff @p DIE has at least one child node.
7682static bool
7683die_has_children(const Dwarf_Die* die)
7684{
7685 if (!die)
7686 return false;
7687
7688 Dwarf_Die child;
7689 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7690 return true;
7691
7692 return false;
7693}
7694
7695/// Get the DIE representing the first parameter of the function
7696/// denoted by a given DIE.
7697///
7698/// @param die the function DIE to consider. Note that if this
7699/// parameter is neither a DW_TAG_subprogram nor a
7700/// DW_TAG_subroutine_type, then the current process is aborted.
7701///
7702/// @param first_parm_die output parameter. This is set to the DIE of
7703/// the first parameter of the function denoted by @p die. This
7704/// output parameter is set iff the function returns true.
7705///
7706/// @return true iff the first parameter of the function denoted by @p
7707/// die is returned in output parameter @p first_parm_die.
7708static bool
7709fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die)
7710{
7711 if (!die)
7712 return false;
7713
7714 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7715 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7716
7717 Dwarf_Die child;
7718 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7719 {
7720 int child_tag = dwarf_tag(&child);
7721 if (child_tag == DW_TAG_formal_parameter)
7722 {
7723 memcpy(&first_parm_die, &child, sizeof(Dwarf_Die));
7724 return true;
7725 }
7726 }
7727 return false;
7728}
7729
7730/// Test if a member function denoted by a given DIE has a parameter
7731/// which is a "this pointer".
7732///
7733/// Please note that if the member function denotes a static member
7734/// function or if the DIE does not denote a member function to begin
7735/// with, then the function will return false because no "this
7736/// pointer" will be found.
7737///
7738/// @param rdr the current DWARF reader in use.
7739///
7740/// @param die the DIE of the member function this function should
7741/// inspect.
7742///
7743/// @param where_offset where in the DIE stream we logically are.
7744///
7745/// @param class_die output parameter. This is set iff a "this
7746/// pointer" was found as the first parameters of the member function
7747/// denoted by @p die, and thus the function returns true If set, this
7748/// then points to the DIE of the class containing the member function
7749/// denoted by @p die.
7750///
7751/// @param object_pointer_die output parameter. This is set to the
7752/// DIE of the function parameter that carries the "this pointe".
7753/// This is set iff this function return true.
7754///
7755/// @return true iff the first parameter of the member function
7756/// denoted by @p die points to a "this pointer".
7757static bool
7758member_fn_die_has_this_pointer(const reader& rdr,
7759 const Dwarf_Die* die,
7760 size_t where_offset,
7761 Dwarf_Die& class_die,
7762 Dwarf_Die& object_pointer_die)
7763{
7764 if (!die)
7765 return false;
7766
7767 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7768 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
7769 return false;
7770
7771 if (tag == DW_TAG_subprogram
7772 && !die_is_at_class_scope(rdr, die, where_offset, class_die))
7773 return false;
7774
7775 Dwarf_Die first_parm_die;
7776 Dwarf_Die parm_type_die;
7777 if (die_has_object_pointer(die, object_pointer_die))
7778 {
7779 // This can be either a member function with a
7780 // DW_AT_object_pointer attribute or a DW_TAG_subroutine_type
7781 // with a DW_AT_object_pointer. In the later case, we are
7782 // looking at a member function type.
7783 memcpy(&first_parm_die, &object_pointer_die, sizeof(Dwarf_Die));
7784 if (!die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7785 return false;
7786 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7787 die_peel_typedef(&parm_type_die, parm_type_die);
7788 }
7789 else if (fn_die_first_parameter_die(die, first_parm_die))
7790 {
7791 memcpy(&object_pointer_die, &first_parm_die, sizeof(Dwarf_Die));
7792 bool is_artificial = false;
7793 if (die_flag_attribute(&first_parm_die, DW_AT_artificial, is_artificial))
7794 {
7795 if (die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7796 {
7797 tag = dwarf_tag(&parm_type_die);
7798 if (tag == DW_TAG_pointer_type)
7799 {
7800 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7801 die_peel_typedef(&parm_type_die, parm_type_die);
7802 }
7803 else
7804 return false;
7805 }
7806 else
7807 return false;
7808 }
7809 else
7810 return false;
7811 }
7812 else
7813 return false;
7814
7815 tag = dwarf_tag(&parm_type_die);
7816 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7817 {
7818 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7819 return true;
7820 }
7821 return false;
7822}
7823
7824/// When given the object pointer DIE of a function type or member
7825/// function DIE, this function returns the "this" pointer that points
7826/// to the associated class.
7827///
7828/// @param die the DIE of the object pointer of the function or member
7829/// function to consider.
7830///
7831/// @param this_pointer_die out parameter. This is set to the DIE of
7832/// the "this" pointer iff the function returns true.
7833///
7834/// @return true iff the function found the "this" pointer from the
7835/// object pointer DIE @p die. In that case, the parameter @p
7836/// this_pointer_die is set to the DIE of that "this" pointer.
7837static bool
7838die_this_pointer_from_object_pointer(Dwarf_Die* die,
7839 Dwarf_Die& this_pointer_die)
7840{
7841 ABG_ASSERT(die);
7842 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7843
7844 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7845 return true;
7846
7847 return false;
7848}
7849
7850/// Test if a given "this" pointer that points to a particular class
7851/// type is for a const class or not. If it's for a const class, then
7852/// it means the function type or the member function associated to
7853/// that "this" pointer is const.
7854///
7855/// @param dye the DIE of the "this" pointer to consider.
7856///
7857/// @return true iff @p die points to a const class type.
7858static bool
7859die_this_pointer_is_const(Dwarf_Die* dye)
7860{
7861 ABG_ASSERT(dye);
7862
7863 Dwarf_Die die;
7864 memcpy(&die, dye, sizeof(Dwarf_Die));
7865 if (dwarf_tag(&die) == DW_TAG_const_type)
7866 ABG_ASSERT(die_peel_qualified(&die, die));
7867
7868 if (dwarf_tag(&die) == DW_TAG_pointer_type)
7869 {
7870 Dwarf_Die pointed_to_type_die;
7871 if (die_die_attribute(&die, DW_AT_type, pointed_to_type_die))
7872 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7873 return true;
7874 }
7875
7876 return false;
7877}
7878
7879/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7880/// attribute) points to a const implicit class and so is for a const
7881/// method or or a const member function type.
7882///
7883/// @param die the DIE of the object pointer to consider.
7884///
7885/// @return true iff the object pointer represented by @p die is for a
7886/// a const method or const member function type.
7887static bool
7888die_object_pointer_is_for_const_method(Dwarf_Die* die)
7889{
7890 ABG_ASSERT(die);
7891 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7892
7893 Dwarf_Die this_pointer_die;
7894 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7895 if (die_this_pointer_is_const(&this_pointer_die))
7896 return true;
7897
7898 return false;
7899}
7900
7901/// Test if a DIE represents an entity that is at class scope.
7902///
7903/// @param rdr the DWARF reader to use.
7904///
7905/// @param die the DIE to consider.
7906///
7907/// @param where_offset where we are logically at in the DIE stream.
7908///
7909/// @param class_scope_die out parameter. Set to the DIE of the
7910/// containing class iff @p die happens to be at class scope; that is,
7911/// iff the function returns true.
7912///
7913/// @return true iff @p die is at class scope. In that case, @p
7914/// class_scope_die is set to the DIE of the class that contains @p
7915/// die.
7916static bool
7917die_is_at_class_scope(const reader& rdr,
7918 const Dwarf_Die* die,
7919 size_t where_offset,
7920 Dwarf_Die& class_scope_die)
7921{
7922 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7923 return false;
7924
7925 int tag = dwarf_tag(&class_scope_die);
7926
7927 return (tag == DW_TAG_structure_type
7928 || tag == DW_TAG_class_type
7929 || tag == DW_TAG_union_type);
7930}
7931
7932/// Return the leaf object under a pointer, reference or qualified
7933/// type DIE.
7934///
7935/// @param die the DIE of the type to consider.
7936///
7937/// @param peeled_die out parameter. Set to the DIE of the leaf
7938/// object iff the function actually peeled anything.
7939///
7940/// @return true upon successful completion.
7941static bool
7942die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7943{
7944 if (!die)
7945 return false;
7946
7947 int tag = dwarf_tag(die);
7948
7949 if (tag == DW_TAG_const_type
7950 || tag == DW_TAG_volatile_type
7951 || tag == DW_TAG_restrict_type
7952 || tag == DW_TAG_pointer_type
7953 || tag == DW_TAG_reference_type
7954 || tag == DW_TAG_rvalue_reference_type)
7955 {
7956 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7957 return false;
7958 }
7959 else
7960 return false;
7961
7962 memcpy(&peeled_die, die, sizeof(peeled_die));
7963
7964 while (tag == DW_TAG_const_type
7965 || tag == DW_TAG_volatile_type
7966 || tag == DW_TAG_restrict_type
7967 || tag == DW_TAG_pointer_type
7968 || tag == DW_TAG_reference_type
7969 || tag == DW_TAG_rvalue_reference_type)
7970 {
7971 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7972 break;
7973 tag = dwarf_tag(&peeled_die);
7974 }
7975
7976 return true;
7977}
7978
7979/// Return the leaf object under a qualified type DIE.
7980///
7981/// @param die the DIE of the type to consider.
7982///
7983/// @param peeled_die out parameter. Set to the DIE of the leaf
7984/// object iff the function actually peeled anything.
7985///
7986/// @return true upon successful completion.
7987static bool
7988die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7989{
7990 if (!die)
7991 return false;
7992
7993 memcpy(&peeled_die, die, sizeof(peeled_die));
7994
7995 int tag = dwarf_tag(&peeled_die);
7996
7997 bool result = false;
7998 while (tag == DW_TAG_const_type
7999 || tag == DW_TAG_volatile_type
8000 || tag == DW_TAG_restrict_type)
8001 {
8002 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8003 break;
8004 tag = dwarf_tag(&peeled_die);
8005 result = true;
8006 }
8007
8008 return result;
8009}
8010
8011/// Return the leaf object under a typedef type DIE.
8012///
8013/// @param die the DIE of the type to consider.
8014///
8015/// @param peeled_die out parameter. Set to the DIE of the leaf
8016/// object iff the function actually peeled anything.
8017///
8018/// @return true upon successful completion.
8019static bool
8020die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
8021{
8022 if (!die)
8023 return false;
8024
8025 int tag = dwarf_tag(die);
8026
8027 memcpy(&peeled_die, die, sizeof(peeled_die));
8028
8029 if (tag == DW_TAG_typedef)
8030 {
8031 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8032 return false;
8033 }
8034 else
8035 return false;
8036
8037 while (tag == DW_TAG_typedef)
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
8048/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
8049///
8050/// @param die the DIE to consider.
8051///
8052/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
8053/// iff the function returned true.
8054///
8055/// @return true iff the function could peel @p die.
8056static bool
8057die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
8058{
8059 if (!die)
8060 return false;
8061
8062 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8063
8064 if (tag == DW_TAG_pointer_type
8065 || tag == DW_TAG_reference_type
8066 || tag == DW_TAG_rvalue_reference_type
8067 || tag == DW_TAG_typedef)
8068 {
8069 if (!die_die_attribute(die, DW_AT_type, peeled_die))
8070 return false;
8071 }
8072 else
8073 return false;
8074
8075 while (tag == DW_TAG_pointer_type
8076 || tag == DW_TAG_reference_type
8077 || tag == DW_TAG_rvalue_reference_type
8078 || tag == DW_TAG_typedef)
8079 {
8080 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
8081 break;
8082 tag = dwarf_tag(&peeled_die);
8083 }
8084 return true;
8085}
8086
8087/// Test if a DIE for a function type represents a method type.
8088///
8089/// @param rdr the DWARF reader.
8090///
8091/// @param die the DIE to consider.
8092///
8093/// @param where_offset where we logically are in the stream of DIEs.
8094///
8095/// @param object_pointer_die out parameter. This is set by the
8096/// function to the DIE that refers to the formal function parameter
8097/// which holds the implicit "this" pointer of the method. That die
8098/// is called the object pointer DIE. This is set iff the member
8099/// function is a non-static member function and if the function
8100/// returns true. In other words, this is only set if the is_static
8101/// out parameter is set to false and the function returns true.
8102///
8103/// @param class_die out parameter. This is set by the function to
8104/// the DIE that represents the class of the method type. This is set
8105/// iff the function returns true.
8106///
8107/// @param is_static out parameter. This is set to true by the
8108/// function if @p die is a static method or a the type of a static
8109/// method. This is set iff the function returns true.
8110///
8111/// @return true iff @p die is a DIE for a method type.
8112static bool
8113die_function_type_is_method_type(const reader& rdr,
8114 const Dwarf_Die *die,
8115 size_t where_offset,
8116 Dwarf_Die& object_pointer_die,
8117 Dwarf_Die& class_die,
8118 bool& is_static)
8119{
8120 if (!die)
8121 return false;
8122
8123 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8124 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
8125
8126 if (member_fn_die_has_this_pointer(rdr, die, where_offset, class_die, object_pointer_die))
8127 {
8128 is_static = false;
8129 return true;
8130 }
8131 else if (die_is_at_class_scope(rdr, die, where_offset, class_die))
8132 {
8133 is_static = true;
8134 return true;
8135 }
8136
8137 return false;
8138}
8139
8140enum virtuality
8141{
8142 VIRTUALITY_NOT_VIRTUAL,
8143 VIRTUALITY_VIRTUAL,
8144 VIRTUALITY_PURE_VIRTUAL
8145};
8146
8147/// Get the virtual-ness of a given DIE, that is, the value of the
8148/// DW_AT_virtuality attribute.
8149///
8150/// @param die the DIE to read from.
8151///
8152/// @param virt the resulting virtuality attribute. This is set iff
8153/// the function returns true.
8154///
8155/// @return true if the virtual-ness could be determined.
8156static bool
8157die_virtuality(const Dwarf_Die* die, virtuality& virt)
8158{
8159 if (!die)
8160 return false;
8161
8162 uint64_t v = 0;
8163 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
8164
8165 if (v == DW_VIRTUALITY_virtual)
8166 virt = VIRTUALITY_VIRTUAL;
8167 else if (v == DW_VIRTUALITY_pure_virtual)
8168 virt = VIRTUALITY_PURE_VIRTUAL;
8169 else
8170 virt = VIRTUALITY_NOT_VIRTUAL;
8171
8172 return true;
8173}
8174
8175/// Test whether the DIE represent either a virtual base or function.
8176///
8177/// @param die the DIE to consider.
8178///
8179/// @return bool if the DIE represents a virtual base or function,
8180/// false othersise.
8181static bool
8182die_is_virtual(const Dwarf_Die* die)
8183{
8184 virtuality v;
8185 if (!die_virtuality(die, v))
8186 return false;
8187
8188 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
8189}
8190
8191/// Test if the DIE represents an entity that was declared inlined.
8192///
8193/// @param die the DIE to test for.
8194///
8195/// @return true if the DIE represents an entity that was declared
8196/// inlined.
8197static bool
8198die_is_declared_inline(Dwarf_Die* die)
8199{
8200 uint64_t inline_value = 0;
8201 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
8202 return false;
8203 return (inline_value == DW_INL_declared_inlined
8204 || inline_value == DW_INL_declared_not_inlined);
8205}
8206
8207/// Compare two DWARF strings using the most accurate (and slowest)
8208/// method possible.
8209///
8210/// @param l the DIE that carries the first string to consider, as an
8211/// attribute value.
8212///
8213/// @param attr_name the name of the attribute which value is the
8214/// string to compare.
8215///
8216/// @return true iff the string carried by @p l equals the one carried
8217/// by @p r.
8218static bool
8219slowly_compare_strings(const Dwarf_Die *l,
8220 const Dwarf_Die *r,
8221 unsigned attr_name)
8222{
8223 const char *l_str = die_char_str_attribute(l, attr_name),
8224 *r_str = die_char_str_attribute(r, attr_name);
8225 if (!l_str && !r_str)
8226 return true;
8227 return l_str && r_str && !strcmp(l_str, r_str);
8228}
8229
8230/// This function is a fast routine (optimization) to compare the
8231/// values of two string attributes of two DIEs.
8232///
8233/// @param l the first DIE to consider.
8234///
8235/// @param r the second DIE to consider.
8236///
8237/// @param attr_name the name of the attribute to compare, on the two
8238/// DIEs above.
8239///
8240/// @param result out parameter. This is set to the result of the
8241/// comparison. If the value of attribute @p attr_name on DIE @p l
8242/// equals the value of attribute @p attr_name on DIE @p r, then the
8243/// the argument of this parameter is set to true. Otherwise, it's
8244/// set to false. Note that the argument of this parameter is set iff
8245/// the function returned true.
8246///
8247/// @return true iff the comparison could be performed. There are
8248/// cases in which the comparison cannot be performed. For instance,
8249/// if one of the DIEs does not have the attribute @p attr_name. In
8250/// any case, if this function returns true, then the parameter @p
8251/// result is set to the result of the comparison.
8252static bool
8253compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
8254 unsigned attr_name,
8255 bool &result)
8256{
8257 Dwarf_Attribute l_attr, r_attr;
8258 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
8259 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
8260 return false;
8261
8262 ABG_ASSERT(l_attr.form == DW_FORM_strp
8263 || l_attr.form == DW_FORM_string
8264 || l_attr.form == DW_FORM_GNU_strp_alt
8265 || form_is_DW_FORM_strx(l_attr.form)
8266 || form_is_DW_FORM_line_strp(l_attr.form));
8267
8268 ABG_ASSERT(r_attr.form == DW_FORM_strp
8269 || r_attr.form == DW_FORM_string
8270 || r_attr.form == DW_FORM_GNU_strp_alt
8271 || form_is_DW_FORM_strx(r_attr.form)
8272 || form_is_DW_FORM_line_strp(r_attr.form));
8273
8274 if ((l_attr.form == DW_FORM_strp
8275 && r_attr.form == DW_FORM_strp)
8276 || (l_attr.form == DW_FORM_GNU_strp_alt
8277 && r_attr.form == DW_FORM_GNU_strp_alt)
8278 || (form_is_DW_FORM_strx(l_attr.form)
8279 && form_is_DW_FORM_strx(r_attr.form))
8280 || (form_is_DW_FORM_line_strp(l_attr.form)
8281 && form_is_DW_FORM_line_strp(r_attr.form)))
8282 {
8283 // So these string attributes are actually pointers into a
8284 // string table. The string table is most likely de-duplicated
8285 // so comparing the *values* of the pointers should be enough.
8286 //
8287 // This is the fast path.
8288 if (l_attr.valp == r_attr.valp)
8289 {
8290#if WITH_DEBUG_TYPE_CANONICALIZATION
8291 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
8292#endif
8293 result = true;
8294 return true;
8295 }
8296 }
8297
8298 // If we reached this point it means we couldn't use the fast path
8299 // because the string atttributes are strings that are "inline" in
8300 // the debug info section. Let's just compare them the slow and
8301 // obvious way.
8302 result = slowly_compare_strings(l, r, attr_name);
8303 return true;
8304}
8305
8306/// Compare the file path of the compilation units (aka CUs)
8307/// associated to two DIEs.
8308///
8309/// If the DIEs are for pointers or typedefs, this function also
8310/// compares the file paths of the CUs of the leaf DIEs (underlying
8311/// DIEs of the pointer or the typedef).
8312///
8313/// @param l the first type DIE to consider.
8314///
8315/// @param r the second type DIE to consider.
8316///
8317/// @return true iff the file paths of the DIEs of the two types are
8318/// equal.
8319static bool
8320compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
8321{
8322 Dwarf_Die l_cu, r_cu;
8323 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
8324 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
8325 return false;
8326
8327 bool compared =
8328 compare_dies_string_attribute_value(&l_cu, &r_cu,
8329 DW_AT_name,
8330 result);
8331 if (compared && result)
8332 {
8333 Dwarf_Die peeled_l, peeled_r;
8334 if (die_is_pointer_reference_or_typedef_type(l)
8335 && die_is_pointer_reference_or_typedef_type(r)
8336 && die_peel_pointer_and_typedef(l, peeled_l)
8337 && die_peel_pointer_and_typedef(r, peeled_r))
8338 {
8339 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
8340 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
8341 return false;
8342 compared =
8343 compare_dies_string_attribute_value(&l_cu, &r_cu,
8344 DW_AT_name,
8345 result);
8346 }
8347 }
8348
8349 return compared;
8350}
8351
8352// -----------------------------------
8353// <location expression evaluation>
8354// -----------------------------------
8355
8356/// Get the value of a given DIE attribute, knowing that it must be a
8357/// location expression.
8358///
8359/// @param die the DIE to read the attribute from.
8360///
8361/// @param attr_name the name of the attribute to read the value for.
8362///
8363/// @param expr the pointer to allocate and fill with the resulting
8364/// array of operators + operands forming a dwarf expression. This is
8365/// set iff the function returns true.
8366///
8367/// @param expr_len the length of the resulting dwarf expression.
8368/// This is set iff the function returns true.
8369///
8370/// @return true if the attribute exists and has a non-empty dwarf expression
8371/// as value. In that case the expr and expr_len arguments are set to the
8372/// resulting dwarf expression.
8373static bool
8374die_location_expr(const Dwarf_Die* die,
8375 unsigned attr_name,
8376 Dwarf_Op** expr,
8377 size_t* expr_len)
8378{
8379 if (!die)
8380 return false;
8381
8382 Dwarf_Attribute attr;
8383 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
8384 return false;
8385
8386 size_t len = 0;
8387 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
8388
8389 // Ignore location expressions where reading them succeeded but
8390 // their length is 0.
8391 result &= len > 0;
8392
8393 if (result)
8394 *expr_len = len;
8395
8396 return result;
8397}
8398
8399/// If the current operation in the dwarf expression represents a push
8400/// of a constant value onto the dwarf expr virtual machine (aka
8401/// DEVM), perform the operation and update the DEVM.
8402///
8403/// If the result of the operation is a constant, update the DEVM
8404/// accumulator with its value. Otherwise, the DEVM accumulator is
8405/// left with its previous value.
8406///
8407/// @param ops the array of the dwarf expression operations to consider.
8408///
8409/// @param ops_len the lengths of @p ops array above.
8410///
8411/// @param index the index of the operation to interpret, in @p ops.
8412///
8413/// @param next_index the index of the operation to interpret at the
8414/// next step, after this function completed and returned. This is
8415/// set an output parameter that is set iff the function returns true.
8416///
8417/// @param ctxt the DEVM evaluation context.
8418///
8419/// @return true if the current operation actually pushes a constant
8420/// value onto the DEVM stack, false otherwise.
8421static bool
8422op_pushes_constant_value(Dwarf_Op* ops,
8423 size_t ops_len,
8424 size_t index,
8425 size_t& next_index,
8426 dwarf_expr_eval_context& ctxt)
8427{
8428 ABG_ASSERT(index < ops_len);
8429
8430 Dwarf_Op& op = ops[index];
8431 int64_t value = 0;
8432
8433 switch (op.atom)
8434 {
8435 case DW_OP_addr:
8436 value = ops[index].number;
8437 break;
8438
8439 case DW_OP_const1u:
8440 case DW_OP_const1s:
8441 case DW_OP_const2u:
8442 case DW_OP_const2s:
8443 case DW_OP_const4u:
8444 case DW_OP_const4s:
8445 case DW_OP_const8u:
8446 case DW_OP_const8s:
8447 case DW_OP_constu:
8448 case DW_OP_consts:
8449 value = ops[index].number;
8450 break;
8451
8452 case DW_OP_lit0:
8453 value = 0;
8454 break;
8455 case DW_OP_lit1:
8456 value = 1;
8457 break;
8458 case DW_OP_lit2:
8459 value = 2;
8460 break;
8461 case DW_OP_lit3:
8462 value = 3;
8463 break;
8464 case DW_OP_lit4:
8465 value = 4;
8466 break;
8467 case DW_OP_lit5:
8468 value = 5;
8469 break;
8470 case DW_OP_lit6:
8471 value = 6;
8472 break;
8473 case DW_OP_lit7:
8474 value = 7;
8475 break;
8476 case DW_OP_lit8:
8477 value = 8;
8478 break;
8479 case DW_OP_lit9:
8480 value = 9;
8481 break;
8482 case DW_OP_lit10:
8483 value = 10;
8484 break;
8485 case DW_OP_lit11:
8486 value = 11;
8487 break;
8488 case DW_OP_lit12:
8489 value = 12;
8490 break;
8491 case DW_OP_lit13:
8492 value = 13;
8493 break;
8494 case DW_OP_lit14:
8495 value = 14;
8496 break;
8497 case DW_OP_lit15:
8498 value = 15;
8499 break;
8500 case DW_OP_lit16:
8501 value = 16;
8502 break;
8503 case DW_OP_lit17:
8504 value = 17;
8505 break;
8506 case DW_OP_lit18:
8507 value = 18;
8508 break;
8509 case DW_OP_lit19:
8510 value = 19;
8511 break;
8512 case DW_OP_lit20:
8513 value = 20;
8514 break;
8515 case DW_OP_lit21:
8516 value = 21;
8517 break;
8518 case DW_OP_lit22:
8519 value = 22;
8520 break;
8521 case DW_OP_lit23:
8522 value = 23;
8523 break;
8524 case DW_OP_lit24:
8525 value = 24;
8526 break;
8527 case DW_OP_lit25:
8528 value = 25;
8529 break;
8530 case DW_OP_lit26:
8531 value = 26;
8532 break;
8533 case DW_OP_lit27:
8534 value = 27;
8535 break;
8536 case DW_OP_lit28:
8537 value = 28;
8538 break;
8539 case DW_OP_lit29:
8540 value = 29;
8541 break;
8542 case DW_OP_lit30:
8543 value = 30;
8544 break;
8545 case DW_OP_lit31:
8546 value = 31;
8547 break;
8548
8549 default:
8550 return false;
8551 }
8552
8553 expr_result r(value);
8554 ctxt.push(r);
8555 ctxt.accum = r;
8556 next_index = index + 1;
8557
8558 return true;
8559}
8560
8561/// If the current operation in the dwarf expression represents a push
8562/// of a non-constant value onto the dwarf expr virtual machine (aka
8563/// DEVM), perform the operation and update the DEVM. A non-constant
8564/// is namely a quantity for which we need inferior (a running program
8565/// image) state to know the exact value.
8566///
8567/// Upon successful completion, as the result of the operation is a
8568/// non-constant the DEVM accumulator value is left to its state as of
8569/// before the invocation of this function.
8570///
8571/// @param ops the array of the dwarf expression operations to consider.
8572///
8573/// @param ops_len the lengths of @p ops array above.
8574///
8575/// @param index the index of the operation to interpret, in @p ops.
8576///
8577/// @param next_index the index of the operation to interpret at the
8578/// next step, after this function completed and returned. This is
8579/// set an output parameter that is set iff the function returns true.
8580///
8581/// @param ctxt the DEVM evaluation context.
8582///
8583/// @return true if the current operation actually pushes a
8584/// non-constant value onto the DEVM stack, false otherwise.
8585static bool
8586op_pushes_non_constant_value(Dwarf_Op* ops,
8587 size_t ops_len,
8588 size_t index,
8589 size_t& next_index,
8590 dwarf_expr_eval_context& ctxt)
8591{
8592 ABG_ASSERT(index < ops_len);
8593 Dwarf_Op& op = ops[index];
8594
8595 switch (op.atom)
8596 {
8597 case DW_OP_reg0:
8598 case DW_OP_reg1:
8599 case DW_OP_reg2:
8600 case DW_OP_reg3:
8601 case DW_OP_reg4:
8602 case DW_OP_reg5:
8603 case DW_OP_reg6:
8604 case DW_OP_reg7:
8605 case DW_OP_reg8:
8606 case DW_OP_reg9:
8607 case DW_OP_reg10:
8608 case DW_OP_reg11:
8609 case DW_OP_reg12:
8610 case DW_OP_reg13:
8611 case DW_OP_reg14:
8612 case DW_OP_reg15:
8613 case DW_OP_reg16:
8614 case DW_OP_reg17:
8615 case DW_OP_reg18:
8616 case DW_OP_reg19:
8617 case DW_OP_reg20:
8618 case DW_OP_reg21:
8619 case DW_OP_reg22:
8620 case DW_OP_reg23:
8621 case DW_OP_reg24:
8622 case DW_OP_reg25:
8623 case DW_OP_reg26:
8624 case DW_OP_reg27:
8625 case DW_OP_reg28:
8626 case DW_OP_reg29:
8627 case DW_OP_reg30:
8628 case DW_OP_reg31:
8629 next_index = index + 1;
8630 break;
8631
8632 case DW_OP_breg0:
8633 case DW_OP_breg1:
8634 case DW_OP_breg2:
8635 case DW_OP_breg3:
8636 case DW_OP_breg4:
8637 case DW_OP_breg5:
8638 case DW_OP_breg6:
8639 case DW_OP_breg7:
8640 case DW_OP_breg8:
8641 case DW_OP_breg9:
8642 case DW_OP_breg10:
8643 case DW_OP_breg11:
8644 case DW_OP_breg12:
8645 case DW_OP_breg13:
8646 case DW_OP_breg14:
8647 case DW_OP_breg15:
8648 case DW_OP_breg16:
8649 case DW_OP_breg17:
8650 case DW_OP_breg18:
8651 case DW_OP_breg19:
8652 case DW_OP_breg20:
8653 case DW_OP_breg21:
8654 case DW_OP_breg22:
8655 case DW_OP_breg23:
8656 case DW_OP_breg24:
8657 case DW_OP_breg25:
8658 case DW_OP_breg26:
8659 case DW_OP_breg27:
8660 case DW_OP_breg28:
8661 case DW_OP_breg29:
8662 case DW_OP_breg30:
8663 case DW_OP_breg31:
8664 next_index = index + 1;
8665 break;
8666
8667 case DW_OP_regx:
8668 next_index = index + 2;
8669 break;
8670
8671 case DW_OP_fbreg:
8672 next_index = index + 1;
8673 break;
8674
8675 case DW_OP_bregx:
8676 next_index = index + 1;
8677 break;
8678
8679 case DW_OP_GNU_variable_value:
8680 next_index = index + 1;
8681 break;
8682
8683 default:
8684 return false;
8685 }
8686
8687 expr_result r(false);
8688 ctxt.push(r);
8689
8690 return true;
8691}
8692
8693/// If the current operation in the dwarf expression represents a
8694/// manipulation of the stack of the DWARF Expression Virtual Machine
8695/// (aka DEVM), this function performs the operation and updates the
8696/// state of the DEVM. If the result of the operation represents a
8697/// constant value, then the accumulator of the DEVM is set to that
8698/// result's value, Otherwise, the DEVM accumulator is left with its
8699/// previous value.
8700///
8701/// @param expr the array of the dwarf expression operations to consider.
8702///
8703/// @param expr_len the lengths of @p ops array above.
8704///
8705/// @param index the index of the operation to interpret, in @p ops.
8706///
8707/// @param next_index the index of the operation to interpret at the
8708/// next step, after this function completed and returned. This is
8709/// set an output parameter that is set iff the function returns true.
8710///
8711/// @param ctxt the DEVM evaluation context.
8712///
8713/// @return true if the current operation actually manipulates the
8714/// DEVM stack, false otherwise.
8715static bool
8716op_manipulates_stack(Dwarf_Op* expr,
8717 size_t expr_len,
8718 size_t index,
8719 size_t& next_index,
8720 dwarf_expr_eval_context& ctxt)
8721{
8722 Dwarf_Op& op = expr[index];
8723 expr_result v;
8724
8725 switch (op.atom)
8726 {
8727 case DW_OP_dup:
8728 v = ctxt.stack.front();
8729 ctxt.push(v);
8730 break;
8731
8732 case DW_OP_drop:
8733 v = ctxt.stack.front();
8734 ctxt.pop();
8735 break;
8736
8737 case DW_OP_over:
8738 ABG_ASSERT(ctxt.stack.size() > 1);
8739 v = ctxt.stack[1];
8740 ctxt.push(v);
8741 break;
8742
8743 case DW_OP_pick:
8744 ABG_ASSERT(index + 1 < expr_len);
8745 v = op.number;
8746 ctxt.push(v);
8747 break;
8748
8749 case DW_OP_swap:
8750 ABG_ASSERT(ctxt.stack.size() > 1);
8751 v = ctxt.stack[1];
8752 ctxt.stack.erase(ctxt.stack.begin() + 1);
8753 ctxt.push(v);
8754 break;
8755
8756 case DW_OP_rot:
8757 ABG_ASSERT(ctxt.stack.size() > 2);
8758 v = ctxt.stack[2];
8759 ctxt.stack.erase(ctxt.stack.begin() + 2);
8760 ctxt.push(v);
8761 break;
8762
8763 case DW_OP_deref:
8764 case DW_OP_deref_size:
8765 ABG_ASSERT(ctxt.stack.size() > 0);
8766 ctxt.pop();
8767 v.is_const(false);
8768 ctxt.push(v);
8769 break;
8770
8771 case DW_OP_xderef:
8772 case DW_OP_xderef_size:
8773 ABG_ASSERT(ctxt.stack.size() > 1);
8774 ctxt.pop();
8775 ctxt.pop();
8776 v.is_const(false);
8777 ctxt.push(v);
8778 break;
8779
8780 case DW_OP_push_object_address:
8781 v.is_const(false);
8782 ctxt.push(v);
8783 break;
8784
8785 case DW_OP_form_tls_address:
8786 case DW_OP_GNU_push_tls_address:
8787 ABG_ASSERT(ctxt.stack.size() > 0);
8788 v = ctxt.pop();
8789 if (op.atom == DW_OP_form_tls_address)
8790 v.is_const(false);
8791 ctxt.push(v);
8792 break;
8793
8794 case DW_OP_call_frame_cfa:
8795 v.is_const(false);
8796 ctxt.push(v);
8797 break;
8798
8799 default:
8800 return false;
8801 }
8802
8803 if (v.is_const())
8804 ctxt.accum = v;
8805
8806 if (op.atom == DW_OP_form_tls_address
8807 || op.atom == DW_OP_GNU_push_tls_address)
8808 ctxt.set_tls_address(true);
8809 else
8810 ctxt.set_tls_address(false);
8811
8812 next_index = index + 1;
8813
8814 return true;
8815}
8816
8817/// If the current operation in the dwarf expression represents a push
8818/// of an arithmetic or logic operation onto the dwarf expr virtual
8819/// machine (aka DEVM), perform the operation and update the DEVM.
8820///
8821/// If the result of the operation is a constant, update the DEVM
8822/// accumulator with its value. Otherwise, the DEVM accumulator is
8823/// left with its previous value.
8824///
8825/// @param expr the array of the dwarf expression operations to consider.
8826///
8827/// @param expr_len the lengths of @p expr array above.
8828///
8829/// @param index the index of the operation to interpret, in @p expr.
8830///
8831/// @param next_index the index of the operation to interpret at the
8832/// next step, after this function completed and returned. This is
8833/// set an output parameter that is set iff the function returns true.
8834///
8835/// @param ctxt the DEVM evaluation context.
8836///
8837/// @return true if the current operation actually represent an
8838/// arithmetic or logic operation.
8839static bool
8840op_is_arith_logic(Dwarf_Op* expr,
8841 size_t expr_len,
8842 size_t index,
8843 size_t& next_index,
8844 dwarf_expr_eval_context& ctxt)
8845{
8846 ABG_ASSERT(index < expr_len);
8847
8848 Dwarf_Op& op = expr[index];
8849 expr_result val1, val2;
8850 bool result = false;
8851
8852 switch (op.atom)
8853 {
8854 case DW_OP_abs:
8855 ABG_ASSERT(ctxt.stack.size() > 0);
8856 val1 = ctxt.pop();
8857 val1 = val1.abs();
8858 ctxt.push(val1);
8859 result = true;
8860 break;
8861
8862 case DW_OP_and:
8863 ABG_ASSERT(ctxt.stack.size() > 1);
8864 val1 = ctxt.pop();
8865 val2 = ctxt.pop();
8866 ctxt.push(val1 & val2);
8867 break;
8868
8869 case DW_OP_div:
8870 ABG_ASSERT(ctxt.stack.size() > 1);
8871 val1 = ctxt.pop();
8872 val2 = ctxt.pop();
8873 if (!val1.is_const())
8874 val1 = 1;
8875 ctxt.push(val2 / val1);
8876 result = true;
8877 break;
8878
8879 case DW_OP_minus:
8880 ABG_ASSERT(ctxt.stack.size() > 1);
8881 val1 = ctxt.pop();
8882 val2 = ctxt.pop();
8883 ctxt.push(val2 - val1);
8884 result = true;
8885 break;
8886
8887 case DW_OP_mod:
8888 ABG_ASSERT(ctxt.stack.size() > 1);
8889 val1 = ctxt.pop();
8890 val2 = ctxt.pop();
8891 ctxt.push(val2 % val1);
8892 result = true;
8893 break;
8894
8895 case DW_OP_mul:
8896 ABG_ASSERT(ctxt.stack.size() > 1);
8897 val1 = ctxt.pop();
8898 val2 = ctxt.pop();
8899 ctxt.push(val2 * val1);
8900 result = true;
8901 break;
8902
8903 case DW_OP_neg:
8904 ABG_ASSERT(ctxt.stack.size() > 0);
8905 val1 = ctxt.pop();
8906 ctxt.push(-val1);
8907 result = true;
8908 break;
8909
8910 case DW_OP_not:
8911 ABG_ASSERT(ctxt.stack.size() > 0);
8912 val1 = ctxt.pop();
8913 ctxt.push(~val1);
8914 result = true;
8915 break;
8916
8917 case DW_OP_or:
8918 ABG_ASSERT(ctxt.stack.size() > 1);
8919 val1 = ctxt.pop();
8920 val2 = ctxt.pop();
8921 ctxt.push(val1 | val2);
8922 result = true;
8923 break;
8924
8925 case DW_OP_plus:
8926 ABG_ASSERT(ctxt.stack.size() > 1);
8927 val1 = ctxt.pop();
8928 val2 = ctxt.pop();
8929 ctxt.push(val2 + val1);
8930 result = true;
8931 break;
8932
8933 case DW_OP_plus_uconst:
8934 ABG_ASSERT(ctxt.stack.size() > 0);
8935 val1 = ctxt.pop();
8936 val1 += op.number;
8937 ctxt.push(val1);
8938 result = true;
8939 break;
8940
8941 case DW_OP_shl:
8942 ABG_ASSERT(ctxt.stack.size() > 1);
8943 val1 = ctxt.pop();
8944 val2 = ctxt.pop();
8945 ctxt.push(val2 << val1);
8946 result = true;
8947 break;
8948
8949 case DW_OP_shr:
8950 case DW_OP_shra:
8951 ABG_ASSERT(ctxt.stack.size() > 1);
8952 val1 = ctxt.pop();
8953 val2 = ctxt.pop();
8954 ctxt.push(val2 >> val1);
8955 result = true;
8956 break;
8957
8958 case DW_OP_xor:
8959 ABG_ASSERT(ctxt.stack.size() > 1);
8960 val1 = ctxt.pop();
8961 val2 = ctxt.pop();
8962 ctxt.push(val2 ^ val1);
8963 result = true;
8964 break;
8965
8966 default:
8967 break;
8968 }
8969
8970 if (result == true)
8971 {
8972 if (ctxt.stack.front().is_const())
8973 ctxt.accum = ctxt.stack.front();
8974
8975 next_index = index + 1;
8976 }
8977 return result;;
8978}
8979
8980/// If the current operation in the dwarf expression represents a push
8981/// of a control flow operation onto the dwarf expr virtual machine
8982/// (aka DEVM), perform the operation and update the DEVM.
8983///
8984/// If the result of the operation is a constant, update the DEVM
8985/// accumulator with its value. Otherwise, the DEVM accumulator is
8986/// left with its previous value.
8987///
8988/// @param expr the array of the dwarf expression operations to consider.
8989///
8990/// @param expr_len the lengths of @p expr array above.
8991///
8992/// @param index the index of the operation to interpret, in @p expr.
8993///
8994/// @param next_index the index of the operation to interpret at the
8995/// next step, after this function completed and returned. This is
8996/// set an output parameter that is set iff the function returns true.
8997///
8998/// @param ctxt the DEVM evaluation context.
8999///
9000/// @return true if the current operation actually represents a
9001/// control flow operation, false otherwise.
9002static bool
9003op_is_control_flow(Dwarf_Op* expr,
9004 size_t expr_len,
9005 size_t index,
9006 size_t& next_index,
9007 dwarf_expr_eval_context& ctxt)
9008{
9009 ABG_ASSERT(index < expr_len);
9010
9011 Dwarf_Op& op = expr[index];
9012 expr_result val1, val2;
9013
9014 switch (op.atom)
9015 {
9016 case DW_OP_eq:
9017 case DW_OP_ge:
9018 case DW_OP_gt:
9019 case DW_OP_le:
9020 case DW_OP_lt:
9021 case DW_OP_ne:
9022 {
9023 bool value = true;
9024 val1 = ctxt.pop();
9025 val2 = ctxt.pop();
9026 if (op.atom == DW_OP_eq)
9027 value = val2 == val1;
9028 else if (op.atom == DW_OP_ge)
9029 value = val2 >= val1;
9030 else if (op.atom == DW_OP_gt)
9031 value = val2 > val1;
9032 else if (op.atom == DW_OP_le)
9033 value = val2 <= val1;
9034 else if (op.atom == DW_OP_lt)
9035 value = val2 < val1;
9036 else if (op.atom == DW_OP_ne)
9037 value = val2 != val1;
9038
9039 val1 = value ? 1 : 0;
9040 ctxt.push(val1);
9041 }
9042 break;
9043
9044 case DW_OP_skip:
9045 if (op.number > 0)
9046 index += op.number - 1;
9047 break;
9048
9049 case DW_OP_bra:
9050 val1 = ctxt.pop();
9051 if (val1.const_value() != 0)
9052 index += val1.const_value() - 1;
9053 break;
9054
9055 case DW_OP_call2:
9056 case DW_OP_call4:
9057 case DW_OP_call_ref:
9058 case DW_OP_nop:
9059 break;
9060
9061 default:
9062 return false;
9063 }
9064
9065 if (ctxt.stack.front().is_const())
9066 ctxt.accum = ctxt.stack.front();
9067
9068 next_index = index + 1;
9069 return true;
9070}
9071
9072/// This function quickly evaluates a DWARF expression that is a
9073/// constant.
9074///
9075/// This is a "fast path" function that quickly evaluates a DWARF
9076/// expression that is only made of a DW_OP_plus_uconst operator.
9077///
9078/// This is a sub-routine of die_member_offset.
9079///
9080/// @param expr the DWARF expression to evaluate.
9081///
9082/// @param expr_len the length of the expression @p expr.
9083///
9084/// @param value out parameter. This is set to the result of the
9085/// evaluation of @p expr, iff this function returns true.
9086///
9087/// @return true iff the evaluation of @p expr went OK.
9088static bool
9089eval_quickly(Dwarf_Op* expr,
9090 uint64_t expr_len,
9091 int64_t& value)
9092{
9093 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
9094 {
9095 value = expr[0].number;
9096 return true;
9097 }
9098 return false;
9099}
9100
9101/// Evaluate the value of the last sub-expression that is a constant,
9102/// inside a given DWARF expression.
9103///
9104/// @param expr the DWARF expression to consider.
9105///
9106/// @param expr_len the length of the expression to consider.
9107///
9108/// @param value the resulting value of the last constant
9109/// sub-expression of the DWARF expression. This is set iff the
9110/// function returns true.
9111///
9112/// @param is_tls_address out parameter. This is set to true iff
9113/// the resulting value of the evaluation is a TLS (thread local
9114/// storage) address.
9115///
9116/// @param eval_ctxt the evaluation context to (re)use. Note that
9117/// this function initializes this context before using it.
9118///
9119/// @return true if the function could find a constant sub-expression
9120/// to evaluate, false otherwise.
9121static bool
9122eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9123 size_t expr_len,
9124 int64_t& value,
9125 bool& is_tls_address,
9126 dwarf_expr_eval_context &eval_ctxt)
9127{
9128 // Reset the evaluation context before evaluating the constant sub
9129 // expression contained in the DWARF expression 'expr'.
9130 eval_ctxt.reset();
9131
9132 size_t index = 0, next_index = 0;
9133 do
9134 {
9135 if (op_is_arith_logic(expr, expr_len, index,
9136 next_index, eval_ctxt)
9137 || op_pushes_constant_value(expr, expr_len, index,
9138 next_index, eval_ctxt)
9139 || op_manipulates_stack(expr, expr_len, index,
9140 next_index, eval_ctxt)
9141 || op_pushes_non_constant_value(expr, expr_len, index,
9142 next_index, eval_ctxt)
9143 || op_is_control_flow(expr, expr_len, index,
9144 next_index, eval_ctxt))
9145 ;
9146 else
9147 next_index = index + 1;
9148
9149 ABG_ASSERT(next_index > index);
9150 index = next_index;
9151 } while (index < expr_len);
9152
9153 is_tls_address = eval_ctxt.set_tls_address();
9154 if (eval_ctxt.accum.is_const())
9155 {
9156 value = eval_ctxt.accum;
9157 return true;
9158 }
9159 return false;
9160}
9161
9162/// Evaluate the value of the last sub-expression that is a constant,
9163/// inside a given DWARF expression.
9164///
9165/// @param expr the DWARF expression to consider.
9166///
9167/// @param expr_len the length of the expression to consider.
9168///
9169/// @param value the resulting value of the last constant
9170/// sub-expression of the DWARF expression. This is set iff the
9171/// function returns true.
9172///
9173/// @return true if the function could find a constant sub-expression
9174/// to evaluate, false otherwise.
9175static bool
9176eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
9177 size_t expr_len,
9178 int64_t& value,
9179 bool& is_tls_address)
9180{
9181 dwarf_expr_eval_context eval_ctxt;
9182 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
9183 is_tls_address, eval_ctxt);
9184}
9185
9186// -----------------------------------
9187// </location expression evaluation>
9188// -----------------------------------
9189
9190/// Convert a DW_AT_bit_offset attribute value into the same value as
9191/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
9192///
9193/// On big endian machines, the value of the DW_AT_bit_offset
9194/// attribute + 8 * the value of the DW_AT_data_member_location
9195/// attribute is the same as the value of the DW_AT_data_bit_offset
9196/// attribute.
9197///
9198/// On little endian machines however, the situation is different.
9199/// The DW_AT_bit_offset value for a bit field is the number of bits
9200/// to the left of the most significant bit of the bit field, within
9201/// the integer value at DW_AT_data_member_location.
9202///
9203/// The DW_AT_data_bit_offset offset value is the number of bits to
9204/// the right of the least significant bit of the bit field, again
9205/// relative to the containing integer value.
9206///
9207/// In other words, DW_AT_data_bit_offset is what everybody would
9208/// instinctively think of as being the "offset of the bit field". 8 *
9209/// DW_AT_data_member_location + DW_AT_bit_offset however is very
9210/// counter-intuitive on little endian machines.
9211///
9212/// This function thus reads the value of a DW_AT_bit_offset property
9213/// of a DIE and converts it into what the DW_AT_data_bit_offset would
9214/// have been if it was present, ignoring the contribution of
9215/// DW_AT_data_member_location.
9216///
9217/// Note that DW_AT_bit_offset has been made obsolete starting from
9218/// DWARF5 (for GCC; Clang still emits it).
9219///
9220/// If you like coffee and it's not too late, now might be a good time
9221/// to have a coffee break. Otherwise if it's late at night, you
9222/// might want to consider an herbal tea break. Then come back to
9223/// read this.
9224///
9225///
9226/// In what follows, the bit fields are all contained within the first
9227/// whole int of the struct, so DW_AT_data_member_location is 0.
9228///
9229/// Okay, to have a better idea of what DW_AT_bit_offset and
9230/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
9231/// have bit fields data members defined as:
9232///
9233/// struct S
9234/// {
9235/// int j:5;
9236/// int k:6;
9237/// int m:5;
9238/// int n:8;
9239/// };
9240///
9241/// The below wonderful (at least!) ASCII art sketch describes the
9242/// layout of the bitfields of 'struct S' on a little endian machine.
9243/// You need to read the sketch from the bottom-up.
9244///
9245/// So please scroll down to its bottom. Note how the 32 bits integer
9246/// word containing the bit fields is laid out with its least
9247/// significant bit starting on the right hand side, at index 0.
9248///
9249/// Then slowly scroll up starting from there, and take the time to
9250/// read each line and see how the bit fields are laid out and what
9251/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
9252/// the bit fields.
9253///
9254/// DW_AT_bit_offset(n)
9255/// < - - - - - - >
9256/// | | n |
9257/// ^ ^< - - - - >^
9258/// DW_AT_data_bit_offset(n)
9259/// < - - - - - - - - - - - - - - - >
9260/// | |
9261/// ^ ^
9262/// DW_AT_bit_offset(m)
9263/// <--------------------------------->
9264/// | | m |
9265/// ^ ^< - >^
9266/// DW_AT_data_bit_offset(m)
9267/// < - - - - - - - - - - >
9268/// | |
9269/// ^ ^
9270/// DW_AT_bit_offset(k)
9271/// <-------------------------------------------->
9272/// | | k |
9273/// ^ ^< - - >^
9274/// DW_AT_data_bit_offset(k)
9275/// < - - - - >
9276/// | |
9277/// ^ ^
9278/// DW_AT_bit_offset(j)
9279/// <-------------------------------------------------------->
9280/// | |
9281/// ^ ^
9282/// n m k j
9283/// < - - - - - - > < - - - > < - - - - > < - - - >
9284///
9285/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
9286/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
9287/// 31 27 23 16 15 11 10 6 5 4 0
9288///
9289/// So, the different bit fields all fit in one 32 bits word, assuming
9290/// the bit fields are tightly packed.
9291///
9292/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
9293/// on this little endian machine and let's see how it relates to
9294/// DW_AT_data_bit_offset of j.
9295///
9296/// DW_AT_bit_offset(j) would be equal to the number of bits from the
9297/// left of the 32 bits word (i.e from bit number 31) to the most
9298/// significant bit of the j bit field (i.e, bit number 4). Thus:
9299///
9300/// DW_AT_bit_offset(j) =
9301/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
9302///
9303/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
9304/// 32 bits word (i.e, bit number 0) to the lest significant bit of
9305/// the 'j' bit field (ie, bit number 0). Thus:
9306///
9307/// DW_AT_data_bit_offset(j) = 0.
9308///
9309/// More generally, we can notice that:
9310///
9311/// sizeof_in_bits(int) =
9312/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
9313///
9314/// It follows that:
9315///
9316/// DW_AT_data_bit_offset(j) =
9317/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
9318///
9319/// Thus:
9320///
9321/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
9322///
9323/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
9324/// from the right hand side of the word. It is what we would
9325/// intuitively think it is. DW_AT_bit_offset however is super
9326/// counter-intuitive, pfff.
9327///
9328/// Anyway, this general equation holds true for all bit fields.
9329///
9330/// Similarly, it follows that:
9331///
9332/// DW_AT_bit_offset(k) =
9333/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
9334///
9335/// Thus:
9336/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
9337///
9338///
9339/// Likewise:
9340///
9341/// DW_AT_bit_offset(m) =
9342/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
9343///
9344///
9345/// Thus:
9346/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
9347///
9348/// And:
9349///
9350///
9351/// Lastly:
9352///
9353/// DW_AT_bit_offset(n) =
9354/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
9355///
9356/// Thus:
9357/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
9358///
9359/// Luckily, the body of the function is much smaller than this
9360/// comment. Enjoy!
9361///
9362/// @param die the DIE to consider.
9363///
9364/// @param is_big_endian this is true iff the machine we are looking at
9365/// is big endian.
9366///
9367/// @param offset this is the output parameter into which the value of
9368/// the DW_AT_bit_offset is put, converted as if it was the value of
9369/// the DW_AT_data_bit_offset parameter, less the contribution of
9370/// DW_AT_data_member_location. This parameter is set iff the
9371/// function returns true.
9372///
9373/// @return true if DW_AT_bit_offset was found on @p die.
9374static bool
9375read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
9376 bool is_big_endian,
9377 uint64_t &offset)
9378{
9379 uint64_t off = 0;
9380 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
9381 return false;
9382
9383 if (is_big_endian)
9384 {
9385 offset = off;
9386 return true;
9387 }
9388
9389 // Okay, we are looking at a little endian machine. We need to
9390 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
9391 // have been. To understand this, you really need to read the
9392 // preliminary comment of this function.
9393 uint64_t containing_anonymous_object_size = 0;
9394 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
9395 containing_anonymous_object_size));
9396 containing_anonymous_object_size *= 8;
9397
9398 uint64_t bitfield_size = 0;
9399 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
9400 bitfield_size));
9401
9402 // As noted in the the preliminary comment of this function if we
9403 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
9404 // its DW_AT_bit_offset value, the equation is:
9405 //
9406 // DW_AT_data_bit_offset(k) =
9407 // sizeof_in_bits(containing_anonymous_object_size)
9408 // - DW_AT_data_bit_offset(k)
9409 // - sizeof_in_bits(k)
9410 offset = containing_anonymous_object_size - off - bitfield_size;
9411
9412 return true;
9413}
9414
9415/// Get the value of the DW_AT_data_member_location of the given DIE
9416/// attribute as an constant.
9417///
9418/// @param die the DIE to read the attribute from.
9419///
9420/// @param offset the attribute as a constant value. This is set iff
9421/// the function returns true.
9422///
9423/// @return true if the attribute exists and has a constant value. In
9424/// that case the offset is set to the value.
9425static bool
9426die_constant_data_member_location(const Dwarf_Die *die,
9427 int64_t& offset)
9428{
9429 if (!die)
9430 return false;
9431
9432 Dwarf_Attribute attr;
9433 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
9434 DW_AT_data_member_location,
9435 &attr))
9436 return false;
9437
9438 Dwarf_Word val;
9439 if (dwarf_formudata(&attr, &val) != 0)
9440 return false;
9441
9442 offset = val;
9443 return true;
9444}
9445
9446/// Get the offset of a struct/class member as represented by the
9447/// value of the DW_AT_data_member_location attribute.
9448///
9449/// There is a huge gotcha in here. The value of the
9450/// DW_AT_data_member_location is not necessarily a constant that one
9451/// would just read and be done with it. Rather, it can be a DWARF
9452/// expression that one has to interpret. In general, the offset can
9453/// be given by the DW_AT_data_bit_offset or by the
9454/// DW_AT_data_member_location attribute and optionally the
9455/// DW_AT_bit_offset attribute. The bit offset attributes are
9456/// always simple constants, but the DW_AT_data_member_location
9457/// attribute is a DWARF location expression.
9458///
9459/// When it's the DW_AT_data_member_location that is present,
9460/// there are three cases to possibly take into account:
9461///
9462/// 1/ The offset in the vtable where the offset of a virtual base
9463/// can be found, aka vptr offset. Given the address of a
9464/// given object O, the vptr offset for B is given by the
9465/// (DWARF) expression:
9466///
9467/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
9468///
9469/// where VIRTUAL_OFFSET is a constant value; In this case,
9470/// this function returns the constant VIRTUAL_OFFSET, as this
9471/// is enough to detect changes in a given virtual base
9472/// relative to the other virtual bases.
9473///
9474/// 2/ The offset of a regular data member. Given the address of
9475/// a struct object named O, the memory location for a
9476/// particular data member is given by the (DWARF) expression:
9477///
9478/// address(O) + OFFSET
9479///
9480/// where OFFSET is a constant. In this case, this function
9481/// returns the OFFSET constant.
9482///
9483/// 3/ The offset of a virtual member function in the virtual
9484/// pointer. The DWARF expression is a constant that designates
9485/// the offset of the function in the vtable. In this case this
9486/// function returns that constant.
9487///
9488/// @param rdr the DWARF reader to consider.
9489///
9490/// @param die the DIE to read the information from.
9491///
9492/// @param offset the resulting constant offset, in bits. This
9493/// argument is set iff the function returns true.
9494static bool
9495die_member_offset(const reader& rdr,
9496 const Dwarf_Die* die,
9497 int64_t& offset)
9498{
9499 Dwarf_Op* expr = NULL;
9500 size_t expr_len = 0;
9501 uint64_t bit_offset = 0;
9502
9503 // First let's see if the DW_AT_data_bit_offset attribute is
9504 // present.
9505 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
9506 {
9507 offset = bit_offset;
9508 return true;
9509 }
9510
9511 // First try to read DW_AT_data_member_location as a plain constant.
9512 // We do this because the generic method using die_location_expr
9513 // might hit a bug in elfutils libdw dwarf_location_expression only
9514 // fixed in elfutils 0.184+. The bug only triggers if the attribute
9515 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
9516 // handle all constants here because that is more consistent (and
9517 // slightly faster in the general case where the attribute isn't a
9518 // full DWARF expression).
9519 if (!die_constant_data_member_location(die, offset))
9520 {
9521 // Otherwise, let's see if the DW_AT_data_member_location
9522 // attribute and, optionally, the DW_AT_bit_offset attributes
9523 // are present.
9524 if (!die_location_expr(die, DW_AT_data_member_location,
9525 &expr, &expr_len))
9526 return false;
9527
9528 // The DW_AT_data_member_location attribute is present. Let's
9529 // evaluate it and get its constant sub-expression and return
9530 // that one.
9531 if (!eval_quickly(expr, expr_len, offset))
9532 {
9533 bool is_tls_address = false;
9534 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
9535 offset, is_tls_address,
9536 rdr.dwarf_expr_eval_ctxt()))
9537 return false;
9538 }
9539 }
9540 offset *= 8;
9541
9542 // On little endian machines, we need to convert the
9543 // DW_AT_bit_offset attribute into a relative offset to 8 *
9544 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
9545 // would be if it were used instead.
9546 //
9547 // In other words, before adding it to 8 *
9548 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9549 // converted into a human-understandable form that represents the
9550 // offset of the bitfield data member it describes. For details
9551 // about the conversion, please read the extensive comments of
9552 // read_and_convert_DW_at_bit_offset.
9553 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9554 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9555 offset += bit_offset;
9556
9557 return true;
9558}
9559
9560/// Read the value of the DW_AT_location attribute from a DIE,
9561/// evaluate the resulting DWARF expression and, if it's a constant
9562/// expression, return it.
9563///
9564/// @param die the DIE to consider.
9565///
9566/// @param address the resulting constant address. This is set iff
9567/// the function returns true.
9568///
9569/// @return true iff the whole sequence of action described above
9570/// could be completed normally.
9571static bool
9572die_location_address(Dwarf_Die* die,
9573 Dwarf_Addr& address,
9574 bool& is_tls_address)
9575{
9576 Dwarf_Op* expr = NULL;
9577 size_t expr_len = 0;
9578
9579 is_tls_address = false;
9580
9581 if (!die)
9582 return false;
9583
9584 Dwarf_Attribute attr;
9585 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9586 return false;
9587
9588 if (dwarf_getlocation(&attr, &expr, &expr_len))
9589 return false;
9590 // Ignore location expressions where reading them succeeded but
9591 // their length is 0.
9592 if (expr_len == 0)
9593 return false;
9594
9595 Dwarf_Attribute result;
9596 if (!dwarf_getlocation_attr(&attr, expr, &result))
9597 // A location that has been interpreted as an address.
9598 return !dwarf_formaddr(&result, &address);
9599
9600 // Just get the address out of the number field.
9601 address = expr->number;
9602 return true;
9603}
9604
9605/// Return the index of a function in its virtual table. That is,
9606/// return the value of the DW_AT_vtable_elem_location attribute.
9607///
9608/// @param die the DIE of the function to consider.
9609///
9610/// @param vindex the resulting index. This is set iff the function
9611/// returns true.
9612///
9613/// @return true if the DIE has a DW_AT_vtable_elem_location
9614/// attribute.
9615static bool
9616die_virtual_function_index(Dwarf_Die* die,
9617 int64_t& vindex)
9618{
9619 if (!die)
9620 return false;
9621
9622 Dwarf_Op* expr = NULL;
9623 size_t expr_len = 0;
9624 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9625 &expr, &expr_len))
9626 return false;
9627
9628 int64_t i = 0;
9629 bool is_tls_addr = false;
9630 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9631 return false;
9632
9633 vindex = i;
9634 return true;
9635}
9636
9637/// Test if a given DIE represents an anonymous type.
9638///
9639/// Anonymous types we are interested in are classes, unions and
9640/// enumerations.
9641///
9642/// @param die the DIE to consider.
9643///
9644/// @return true iff @p die represents an anonymous type.
9645bool
9647{
9648 int tag = dwarf_tag(die);
9649
9650 if (tag == DW_TAG_class_type
9651 || tag == DW_TAG_structure_type
9652 || tag == DW_TAG_union_type
9653 || tag == DW_TAG_enumeration_type)
9654 return die_is_anonymous(die);
9655
9656 return false;
9657}
9658
9659/// Return the base of the internal name to represent an anonymous
9660/// type.
9661///
9662/// Typically, anonymous enums would be named
9663/// __anonymous_enum__<number>, anonymous struct or classes would be
9664/// named __anonymous_struct__<number> and anonymous unions would be
9665/// named __anonymous_union__<number>. The first part of these
9666/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9667/// the base name. This function returns that base name, depending on
9668/// the kind of type DIE we are looking at.
9669///
9670/// @param die the type DIE to look at. This function expects a type
9671/// DIE with an empty DW_AT_name property value (anonymous).
9672///
9673/// @return a string representing the base of the internal anonymous
9674/// name.
9675static string
9676get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9677{
9678 ABG_ASSERT(die_is_type(die));
9679 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9680
9681 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9682 string type_name;
9683 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9685 else if (tag == DW_TAG_union_type)
9687 else if (tag == DW_TAG_enumeration_type)
9689
9690 return type_name;
9691}
9692
9693/// Build a full internal anonymous type name.
9694///
9695/// @param base_name this is the base name as returned by the function
9696/// @ref get_internal_anonymous_die_prefix_name.
9697///
9698/// @param anonymous_type_index this is the index of the anonymous
9699/// type in its scope. That is, if there are more than one anonymous
9700/// types of a given kind in a scope, this index is what tells them
9701/// appart, starting from 0.
9702///
9703/// @return the built string, which is a concatenation of @p base_name
9704/// and @p anonymous_type_index.
9705static string
9706build_internal_anonymous_die_name(const string &base_name,
9707 size_t anonymous_type_index)
9708{
9709 string name = base_name;
9710 if (anonymous_type_index && !base_name.empty())
9711 {
9712 std::ostringstream o;
9713 o << base_name << anonymous_type_index;
9714 name = o.str();
9715 }
9716 return name;
9717}
9718
9719// ------------------------------------
9720// <DIE pretty printer>
9721// ------------------------------------
9722
9723/// Compute the qualified name of a DIE that represents a type.
9724///
9725/// For instance, if the DIE tag is DW_TAG_subprogram then this
9726/// function computes the name of the function *type*.
9727///
9728/// @param rdr the DWARF reader.
9729///
9730/// @param die the DIE to consider.
9731///
9732/// @param where_offset where in the are logically are in the DIE
9733/// stream.
9734///
9735/// @param guard the set of DIE offsets of the stack of DIEs involved
9736/// in the construction of the qualified name of the type. This set
9737/// is used to detect (and avoid) cycles in the stack of DIEs that is
9738/// going to be walked to compute the qualified type name.
9739///
9740/// @return a copy of the qualified name of the type.
9741static string
9742die_qualified_type_name(const reader& rdr,
9743 const Dwarf_Die* die,
9744 size_t where_offset,
9745 unordered_set<uint64_t>& guard)
9746{
9747 if (!die)
9748 return "";
9749
9750 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9751 if (tag == DW_TAG_compile_unit
9752 || tag == DW_TAG_partial_unit
9753 || tag == DW_TAG_type_unit)
9754 return "";
9755
9756 string name = die_name(die);
9757
9758 Dwarf_Die scope_die;
9759 if (!get_scope_die(rdr, die, where_offset, scope_die))
9760 return "";
9761
9762 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9763 string separator = colon_colon ? "::" : ".";
9764
9765 string repr;
9766
9767 switch (tag)
9768 {
9769 case DW_TAG_unspecified_type:
9770 break;
9771
9772 case DW_TAG_base_type:
9773 {
9774 abigail::ir::real_type real_type;
9775 if (parse_real_type(name, real_type))
9776 repr = real_type;
9777 else
9778 repr = name;
9779 }
9780 break;
9781
9782 case DW_TAG_typedef:
9783 ABG_ASSERT(!name.empty());
9784 // fall through
9785
9786 case DW_TAG_enumeration_type:
9787 case DW_TAG_structure_type:
9788 case DW_TAG_class_type:
9789 case DW_TAG_union_type:
9790 {
9791 if (die_is_anonymous(die))
9792 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
9793 /*one_line=*/true,
9794 /*qualed_name=*/false,
9795 where_offset, guard);
9796 else
9797 {
9798 string parent_name = die_qualified_name(rdr, &scope_die,
9799 where_offset, guard);
9800 repr = parent_name.empty() ? name : parent_name + separator + name;
9801 }
9802 }
9803 break;
9804
9805 case DW_TAG_const_type:
9806 case DW_TAG_volatile_type:
9807 case DW_TAG_restrict_type:
9808 {
9809 Dwarf_Die underlying_type_die;
9810 bool has_underlying_type_die =
9811 die_die_attribute(die, DW_AT_type, underlying_type_die);
9812
9813 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9814 break;
9815
9816 if (tag == DW_TAG_const_type)
9817 {
9818 if (has_underlying_type_die
9819 && die_is_reference_type(&underlying_type_die))
9820 // A reference is always const. So, to lower false
9821 // positive reports in diff computations, we consider a
9822 // const reference just as a reference. But we need to
9823 // keep the qualified-ness of the type. So we introduce
9824 // a 'no-op' qualifier here. Please remember that this
9825 // has to be kept in sync with what is done in
9826 // get_name_of_qualified_type. So if you change this
9827 // here, you have to change that code there too.
9828 repr = "";
9829 else if (!has_underlying_type_die
9830 || die_is_void_type(&underlying_type_die))
9831 {
9832 repr = "void";
9833 break;
9834 }
9835 else
9836 repr = "const";
9837 }
9838 else if (tag == DW_TAG_volatile_type)
9839 repr = "volatile";
9840 else if (tag == DW_TAG_restrict_type)
9841 repr = "restrict";
9842 else
9844
9845 string underlying_type_repr;
9846 if (has_underlying_type_die)
9847 underlying_type_repr =
9848 die_qualified_type_name(rdr, &underlying_type_die,
9849 where_offset, guard);
9850 else
9851 underlying_type_repr = "void";
9852
9853 if (underlying_type_repr.empty())
9854 repr.clear();
9855 else
9856 {
9857 if (has_underlying_type_die)
9858 {
9859 Dwarf_Die peeled;
9860 die_peel_qualified(&underlying_type_die, peeled);
9861 if (die_is_pointer_or_reference_type(&peeled))
9862 repr = underlying_type_repr + " " + repr;
9863 else
9864 repr += " " + underlying_type_repr;
9865 }
9866 else
9867 repr += " " + underlying_type_repr;
9868 }
9869 }
9870 break;
9871
9872 case DW_TAG_pointer_type:
9873 case DW_TAG_reference_type:
9874 case DW_TAG_rvalue_reference_type:
9875 {
9876 Dwarf_Die pointed_to_type_die;
9877 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9878 {
9879 if (tag == DW_TAG_pointer_type)
9880 repr = "void*";
9881 break;
9882 }
9883
9884 if (die_is_unspecified(&pointed_to_type_die))
9885 break;
9886
9887 string pointed_type_repr =
9888 die_qualified_type_name(rdr, &pointed_to_type_die,
9889 where_offset, guard);
9890
9891 repr = pointed_type_repr;
9892 if (repr.empty())
9893 break;
9894
9895 if (tag == DW_TAG_pointer_type)
9896 repr += "*";
9897 else if (tag == DW_TAG_reference_type)
9898 repr += "&";
9899 else if (tag == DW_TAG_rvalue_reference_type)
9900 repr += "&&";
9901 else
9903 }
9904 break;
9905
9906 case DW_TAG_subrange_type:
9907 {
9908 // In Ada, this one can be generated on its own, that is, not
9909 // as a sub-type of an array. So we need to support it on its
9910 // own. Note that when it's emitted as the sub-type of an
9911 // array like in C and C++, this is handled differently, for
9912 // now. But we try to make this usable by other languages
9913 // that are not Ada, even if we modelled it after Ada.
9914
9915 // So we build a subrange type for the sole purpose of using
9916 // the ::as_string() method of that type. So we don't add
9917 // that type to the current type tree being built.
9919 build_subrange_type(const_cast<reader&>(rdr),
9920 die, where_offset,
9921 /*associate_die_to_type=*/false);
9922 repr += s->as_string();
9923 break;
9924 }
9925
9926 case DW_TAG_array_type:
9927 {
9928 Dwarf_Die element_type_die;
9929 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9930 break;
9931 string element_type_name =
9932 die_qualified_type_name(rdr, &element_type_die, where_offset, guard);
9933 if (element_type_name.empty())
9934 break;
9935
9937 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9938 die, subranges, where_offset,
9939 /*associate_type_to_die=*/false);
9940
9941 repr = element_type_name;
9942 repr += array_type_def::subrange_type::vector_as_string(subranges);
9943 }
9944 break;
9945
9946 case DW_TAG_subroutine_type:
9947 case DW_TAG_subprogram:
9948 {
9949 string return_type_name;
9950 string class_name;
9951 vector<string> parm_names;
9952 bool is_const = false;
9953 bool is_static = false;
9954 bool is_method_type = false;
9955 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9956 /*pretty_print=*/true,
9957 /*qualified_name=*/true,
9959 return_type_name, class_name,
9960 parm_names, is_const,
9961 is_static, guard);
9962 if (return_type_name.empty())
9963 return_type_name = "void";
9964
9965 repr = return_type_name;
9966
9967 if (is_method_type)
9968 // This is a method, so print the class name.
9969 repr += " (" + class_name + "::*)";
9970
9971 // Now parameters.
9972 repr += " (";
9973 for (vector<string>::const_iterator i = parm_names.begin();
9974 i != parm_names.end();
9975 ++i)
9976 {
9977 if (i != parm_names.begin())
9978 repr += ", ";
9979 repr += *i;
9980 }
9981 repr += ")";
9982
9983 }
9984 break;
9985
9986 case DW_TAG_string_type:
9987 case DW_TAG_ptr_to_member_type:
9988 case DW_TAG_set_type:
9989 case DW_TAG_file_type:
9990 case DW_TAG_packed_type:
9991 case DW_TAG_thrown_type:
9992 case DW_TAG_interface_type:
9993 case DW_TAG_shared_type:
9994 break;
9995 }
9996
9997 return repr;
9998}
9999
10000/// Compute the name of a type represented by a DIE.
10001///
10002/// @param rdr the reader to use.
10003///
10004/// @param die the type DIE to consider.
10005///
10006/// @param qualified_name if true then compute a qualified name.
10007///
10008/// @param where_offset where in the are logically are in the DIE
10009/// stream.
10010///
10011/// @param guard the set of DIE offsets of the stack of DIEs involved
10012/// in the construction of the name of the type. This set is used to
10013/// detect (and avoid) cycles in the stack of DIEs that is going to be
10014/// walked to compute the type name.
10015///
10016/// @return a copy of the string representing the type represented by
10017/// @p die.
10018static string
10019die_type_name(const reader& rdr,
10020 const Dwarf_Die* die,
10021 bool qualified_name,
10022 size_t where_offset,
10023 unordered_set<uint64_t>& guard)
10024{
10025 if (!die)
10026 return "";
10027
10028 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
10029 if (tag == DW_TAG_compile_unit
10030 || tag == DW_TAG_partial_unit
10031 || tag == DW_TAG_type_unit)
10032 return "";
10033
10034 string name = die_name(die);
10035
10036 Dwarf_Die scope_die;
10037 if (!get_scope_die(rdr, die, where_offset, scope_die))
10038 return "";
10039
10040 bool colon_colon = die_is_type(die) || die_is_namespace(die);
10041 string separator = colon_colon ? "::" : ".";
10042
10043 string repr;
10044
10045 switch (tag)
10046 {
10047 case DW_TAG_unspecified_type:
10048 break;
10049
10050 case DW_TAG_base_type:
10051 {
10052 abigail::ir::real_type int_type;
10053 if (parse_real_type(name, int_type))
10054 repr = int_type;
10055 else
10056 repr = name;
10057 }
10058 break;
10059
10060 case DW_TAG_typedef:
10061 ABG_ASSERT(!name.empty());
10062 // fall through
10063
10064 case DW_TAG_enumeration_type:
10065 case DW_TAG_structure_type:
10066 case DW_TAG_class_type:
10067 case DW_TAG_union_type:
10068 {
10069 if (die_is_anonymous(die))
10070 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
10071 /*one_line=*/true,
10072 /*qualed_name=*/false,
10073 where_offset,
10074 guard);
10075 else
10076 {
10077 string parent_name;
10078 if (qualified_name)
10079 {
10080 if (!is_anonymous_type_die(&scope_die))
10081 parent_name = die_qualified_name(rdr, &scope_die,
10082 where_offset, guard);
10083 }
10084 repr = parent_name.empty() ? name : parent_name + separator + name;
10085 }
10086 }
10087 break;
10088
10089 case DW_TAG_const_type:
10090 case DW_TAG_volatile_type:
10091 case DW_TAG_restrict_type:
10092 {
10093 Dwarf_Die underlying_type_die;
10094 bool has_underlying_type_die =
10095 die_die_attribute(die, DW_AT_type, underlying_type_die);
10096
10097 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
10098 break;
10099
10100 if (tag == DW_TAG_const_type)
10101 {
10102 if (has_underlying_type_die
10103 && die_is_reference_type(&underlying_type_die))
10104 // A reference is always const. So, to lower false
10105 // positive reports in diff computations, we consider a
10106 // const reference just as a reference. But we need to
10107 // keep the qualified-ness of the type. So we introduce
10108 // a 'no-op' qualifier here. Please remember that this
10109 // has to be kept in sync with what is done in
10110 // get_name_of_qualified_type. So if you change this
10111 // here, you have to change that code there too.
10112 repr = "";
10113 else if (!has_underlying_type_die
10114 || die_is_void_type(&underlying_type_die))
10115 {
10116 repr = "void";
10117 break;
10118 }
10119 else
10120 repr = "const";
10121 }
10122 else if (tag == DW_TAG_volatile_type)
10123 repr = "volatile";
10124 else if (tag == DW_TAG_restrict_type)
10125 repr = "restrict";
10126 else
10128
10129 string underlying_type_repr;
10130 if (has_underlying_type_die)
10131 underlying_type_repr =
10132 die_type_name(rdr, &underlying_type_die,
10133 qualified_name, where_offset,
10134 guard);
10135 else
10136 underlying_type_repr = "void";
10137
10138 if (underlying_type_repr.empty())
10139 repr.clear();
10140 else
10141 {
10142 if (has_underlying_type_die)
10143 {
10144 Dwarf_Die peeled;
10145 die_peel_qualified(&underlying_type_die, peeled);
10146 if (die_is_pointer_or_reference_type(&peeled))
10147 repr = underlying_type_repr + " " + repr;
10148 else
10149 repr += " " + underlying_type_repr;
10150 }
10151 else
10152 repr += " " + underlying_type_repr;
10153 }
10154 }
10155 break;
10156
10157 case DW_TAG_pointer_type:
10158 case DW_TAG_reference_type:
10159 case DW_TAG_rvalue_reference_type:
10160 {
10161 Dwarf_Die pointed_to_type_die;
10162 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
10163 {
10164 if (tag == DW_TAG_pointer_type)
10165 repr = "void*";
10166 break;
10167 }
10168
10169 if (die_is_unspecified(&pointed_to_type_die))
10170 break;
10171
10172 string pointed_type_repr =
10173 die_type_name(rdr, &pointed_to_type_die,
10174 qualified_name, where_offset,
10175 guard);
10176
10177 repr = pointed_type_repr;
10178 if (repr.empty())
10179 break;
10180
10181 if (tag == DW_TAG_pointer_type)
10182 repr += "*";
10183 else if (tag == DW_TAG_reference_type)
10184 repr += "&";
10185 else if (tag == DW_TAG_rvalue_reference_type)
10186 repr += "&&";
10187 else
10189 }
10190 break;
10191
10192 case DW_TAG_subrange_type:
10193 {
10194 // In Ada, this one can be generated on its own, that is, not
10195 // as a sub-type of an array. So we need to support it on its
10196 // own. Note that when it's emitted as the sub-type of an
10197 // array like in C and C++, this is handled differently, for
10198 // now. But we try to make this usable by other languages
10199 // that are not Ada, even if we modelled it after Ada.
10200
10201 // So we build a subrange type for the sole purpose of using
10202 // the ::as_string() method of that type. So we don't add
10203 // that type to the current type tree being built.
10205 build_subrange_type(const_cast<reader&>(rdr),
10206 die, where_offset,
10207 /*associate_die_to_type=*/false);
10208 repr += s->as_string();
10209 break;
10210 }
10211
10212 case DW_TAG_array_type:
10213 {
10214 Dwarf_Die element_type_die;
10215 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10216 break;
10217 string element_type_name =
10218 die_type_name(rdr, &element_type_die,
10219 qualified_name, where_offset,
10220 guard);
10221 if (element_type_name.empty())
10222 break;
10223
10225 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
10226 die, subranges, where_offset,
10227 /*associate_type_to_die=*/false);
10228
10229 repr = element_type_name;
10230 repr += array_type_def::subrange_type::vector_as_string(subranges);
10231 }
10232 break;
10233
10234 case DW_TAG_subroutine_type:
10235 case DW_TAG_subprogram:
10236 {
10237 string return_type_name;
10238 string class_name;
10239 vector<string> parm_names;
10240 bool is_const = false;
10241 bool is_static = false;
10242 bool is_method_type = false;
10243 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
10244 /*pretty_print=*/true,
10245 qualified_name,
10247 return_type_name,
10248 class_name,
10249 parm_names, is_const,
10250 is_static, guard);
10251 if (return_type_name.empty())
10252 return_type_name = "void";
10253
10254 repr = return_type_name;
10255
10256 if (is_method_type)
10257 {
10258 // This is a method, so print the class name.
10259 repr += " (" + class_name + "::*)";
10260 }
10261
10262 // Now parameters.
10263 repr += " (";
10264 for (vector<string>::const_iterator i = parm_names.begin();
10265 i != parm_names.end();
10266 ++i)
10267 {
10268 if (i != parm_names.begin())
10269 repr += ", ";
10270 repr += *i;
10271 }
10272 repr += ")";
10273
10274 }
10275 break;
10276
10277 case DW_TAG_string_type:
10278 case DW_TAG_ptr_to_member_type:
10279 case DW_TAG_set_type:
10280 case DW_TAG_file_type:
10281 case DW_TAG_packed_type:
10282 case DW_TAG_thrown_type:
10283 case DW_TAG_interface_type:
10284 case DW_TAG_shared_type:
10285 break;
10286 }
10287
10288 return repr;
10289}
10290
10291/// Compute the name of a type represented by a DIE.
10292///
10293/// @param rdr the reader to use.
10294///
10295/// @param die the type DIE to consider.
10296///
10297/// @param qualified_name if true then compute a qualified name.
10298///
10299/// @param where_offset where in the are logically are in the DIE
10300/// stream.
10301///
10302/// @return a copy of the string representing the type represented by
10303/// @p die.
10304static string
10305die_type_name(const reader& rdr,
10306 const Dwarf_Die* die,
10307 bool qualified_name,
10308 size_t where_offset)
10309{
10310 unordered_set<uint64_t> guard;
10311 return die_type_name(rdr, die, qualified_name, where_offset, guard);
10312}
10313
10314/// Compute the qualified name of a decl represented by a given DIE.
10315///
10316/// For instance, for a DIE of tag DW_TAG_subprogram this function
10317/// computes the signature of the function *declaration*.
10318///
10319/// @param rdr the DWARF reader.
10320///
10321/// @param die the DIE to consider.
10322///
10323/// @param where_offset where we are logically at in the DIE stream.
10324///
10325/// @param guard the set of DIE offsets of the stack of DIEs involved
10326/// in the construction of the qualified name of the decl. This set
10327/// is used to detect (and avoid) cycles in the stack of DIEs that is
10328/// going to be walked to compute the qualified decl name.
10329///
10330/// @return a copy of the computed name.
10331static string
10332die_qualified_decl_name(const reader& rdr,
10333 const Dwarf_Die* die,
10334 size_t where_offset,
10335 unordered_set<uint64_t>& guard)
10336{
10337 if (!die || !die_is_decl(die))
10338 return "";
10339
10340 string name = die_name(die);
10341
10342 Dwarf_Die scope_die;
10343 if (!get_scope_die(rdr, die, where_offset, scope_die))
10344 return "";
10345
10346 string scope_name = die_qualified_name(rdr, &scope_die, where_offset, guard);
10347 string separator = "::";
10348
10349 string repr;
10350
10351 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10352 switch (tag)
10353 {
10354 case DW_TAG_namespace:
10355 case DW_TAG_member:
10356 case DW_TAG_variable:
10357 repr = scope_name.empty() ? name : scope_name + separator + name;
10358 break;
10359 case DW_TAG_subprogram:
10360 repr = die_function_signature(rdr, die,
10361 /*qualified_name=*/true,
10362 where_offset, guard);
10363 break;
10364
10365 case DW_TAG_unspecified_parameters:
10366 repr = "...";
10367 break;
10368
10369 case DW_TAG_formal_parameter:
10370 case DW_TAG_imported_declaration:
10371 case DW_TAG_GNU_template_template_param:
10372 case DW_TAG_GNU_template_parameter_pack:
10373 case DW_TAG_GNU_formal_parameter_pack:
10374 break;
10375 }
10376 return repr;
10377}
10378
10379/// Compute the qualified name of the artifact represented by a given
10380/// DIE.
10381///
10382/// If the DIE represents a type, then the function computes the name
10383/// of the type. Otherwise, if the DIE represents a decl then the
10384/// function computes the name of the decl. Note that a DIE of tag
10385/// DW_TAG_subprogram is going to be considered as a "type" -- just
10386/// like if it was a DW_TAG_subroutine_type.
10387///
10388/// @param rdr the DWARF reader.
10389///
10390/// @param die the DIE to consider.
10391///
10392/// @param where_offset where we are logically at in the DIE stream.
10393///
10394/// @param guard the set of DIE offsets of the stack of DIEs involved
10395/// in the construction of the qualified name of the DIE. This set is
10396/// used to detect (and avoid) cycles in the stack of DIEs that is
10397/// going to be walked to compute the qualified DIE name.
10398///
10399/// @return a copy of the computed name.
10400static string
10401die_qualified_name(const reader& rdr, const Dwarf_Die* die,
10402 size_t where, unordered_set<uint64_t>& guard)
10403{
10404 if (die_is_type(die))
10405 return die_qualified_type_name(rdr, die, where, guard);
10406 else if (die_is_decl(die))
10407 return die_qualified_decl_name(rdr, die, where, guard);
10408 return "";
10409}
10410
10411/// Compute the qualified name of the artifact represented by a given
10412/// DIE.
10413///
10414/// If the DIE represents a type, then the function computes the name
10415/// of the type. Otherwise, if the DIE represents a decl then the
10416/// function computes the name of the decl. Note that a DIE of tag
10417/// DW_TAG_subprogram is going to be considered as a "type" -- just
10418/// like if it was a DW_TAG_subroutine_type.
10419///
10420/// @param rdr the DWARF reader.
10421///
10422/// @param die the DIE to consider.
10423///
10424/// @param where_offset where we are logically at in the DIE stream.
10425///
10426/// @return a copy of the computed name.
10427static string
10428die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
10429{
10430 unordered_set<uint64_t> guard;
10431 return die_qualified_name(rdr, die, where, guard);
10432}
10433
10434/// Test if the qualified name of a given type should be empty.
10435///
10436/// The reason why the name of a DIE with a given tag would be empty
10437/// is that libabigail's internal representation doesn't yet support
10438/// that tag; or if the DIE's qualified name is built from names of
10439/// sub-types DIEs whose tags are not yet supported.
10440///
10441/// @param rdr the DWARF reader.
10442///
10443/// @param die the DIE to consider.
10444///
10445/// @param where where we are logically at, in the DIE stream.
10446///
10447/// @param qualified_name the qualified name of the DIE. This is set
10448/// only iff the function returns false.
10449///
10450/// @param guard the set of DIE offsets of the stack of DIEs involved
10451/// in the construction of the qualified name of the type. This set
10452/// is used to detect (and avoid) cycles in the stack of DIEs that is
10453/// going to be walked to compute the qualified type name.
10454///
10455/// @return true if the qualified name of the DIE is empty.
10456static bool
10457die_qualified_type_name_empty(const reader& rdr,
10458 const Dwarf_Die* die,
10459 size_t where, string &qualified_name,
10460 unordered_set<uint64_t>& guard)
10461{
10462 if (!die)
10463 return true;
10464
10465 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10466
10467 string qname;
10468 if (tag == DW_TAG_typedef
10469 || tag == DW_TAG_pointer_type
10470 || tag == DW_TAG_reference_type
10471 || tag == DW_TAG_rvalue_reference_type
10472 || tag == DW_TAG_array_type
10473 || tag == DW_TAG_const_type
10474 || tag == DW_TAG_volatile_type
10475 || tag == DW_TAG_restrict_type)
10476 {
10477 Dwarf_Die underlying_type_die;
10478 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
10479 {
10480 string name =
10481 die_qualified_type_name(rdr, &underlying_type_die, where, guard);
10482 if (name.empty())
10483 return true;
10484 }
10485 }
10486 else
10487 {
10488 string name = die_qualified_type_name(rdr, die, where, guard);
10489 if (name.empty())
10490 return true;
10491 }
10492
10493 qname = die_qualified_type_name(rdr, die, where, guard);
10494 if (qname.empty())
10495 return true;
10496
10497 qualified_name = qname;
10498 return false;
10499}
10500
10501/// Given the DIE that represents a function type, compute the names
10502/// of the following properties the function's type:
10503///
10504/// - return type
10505/// - enclosing class (if the function is a member function)
10506/// - function parameter types
10507///
10508/// When the function we are looking at is a member function, it also
10509/// tells if it's const.
10510///
10511/// @param rdr the DWARF reader.
10512///
10513/// @param die the DIE of the function or function type we are looking
10514/// at.
10515///
10516/// @param where_offset where we are logically at in the DIE stream.
10517///
10518/// @param pretty_print if set to yes, the type names are going to be
10519/// pretty-printed names; otherwise, they are just qualified type
10520/// names.
10521///
10522/// @param qualified_name if true then the names returned are
10523/// qualified.
10524///
10525/// @param is_method_type output parameter. This is set by the
10526/// function to true iff the DIE @p die represents a method.
10527///
10528/// @param return_type_name out parameter. This contains the name of
10529/// the return type of the function.
10530///
10531/// @param class_name out parameter. If the function is a member
10532/// function, this contains the name of the enclosing class.
10533///
10534/// @param parm_names out parameter. This vector is set to the names
10535/// of the types of the parameters of the function.
10536///
10537/// @param is_const out parameter. If the function is a member
10538/// function, this is set to true iff the member function is const.
10539///
10540/// @param is_static out parameter. If the function is a static
10541/// member function, then this is set to true.
10542///
10543/// @param guard the set of DIE offsets of the stack of DIEs involved
10544/// in the construction of the qualified name of the function type.
10545/// This set is used to detect (and avoid) cycles in the stack of DIEs
10546/// that is going to be walked to compute the qualified function type
10547/// name.
10548static void
10549die_return_and_parm_names_from_fn_type_die(const reader& rdr,
10550 const Dwarf_Die* die,
10551 size_t where_offset,
10552 bool pretty_print,
10553 bool qualified_name,
10554 bool &is_method_type,
10555 string &return_type_name,
10556 string &class_name,
10557 vector<string>& parm_names,
10558 bool& is_const,
10559 bool& is_static,
10560 unordered_set<uint64_t>& guard)
10561{
10562 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10563 if (guard.find(off) != guard.end())
10564 return;
10565 guard.insert(off);
10566
10567 Dwarf_Die child;
10568 Dwarf_Die ret_type_die;
10569 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
10570 return_type_name = "void";
10571 else
10572 {
10573 return_type_name =
10574 pretty_print
10575 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset, guard)
10576 : die_type_name(rdr, &ret_type_die, qualified_name,
10577 where_offset, guard);
10578 }
10579
10580 if (return_type_name.empty())
10581 return_type_name = "void";
10582
10583 Dwarf_Die object_pointer_die, class_die;
10585 die_function_type_is_method_type(rdr, die, where_offset,
10586 object_pointer_die,
10587 class_die, is_static);
10588
10589 is_const = false;
10590 if (is_method_type)
10591 {
10592 if (!is_anonymous_type_die(&class_die))
10593 class_name = die_type_name(rdr, &class_die, qualified_name,
10594 where_offset, guard);
10595
10596 Dwarf_Die this_pointer_die;
10597 Dwarf_Die pointed_to_die;
10598 if (!is_static
10599 && die_die_attribute(&object_pointer_die, DW_AT_type,
10600 this_pointer_die))
10601 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
10602 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
10603 is_const = true;
10604
10605 string fn_name = die_name(die);
10606 string non_qualified_class_name = die_name(&class_die);
10607 bool is_ctor = fn_name == non_qualified_class_name;
10608 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
10609
10610 if (is_ctor || is_dtor)
10611 return_type_name.clear();
10612 }
10613
10614 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
10615 do
10616 {
10617 int child_tag = dwarf_tag(&child);
10618 bool first_parm = true;
10619 if (child_tag == DW_TAG_formal_parameter)
10620 {
10621 // Skip the first parameter of a method.
10622 if (first_parm)
10623 {
10624 first_parm = false;
10625 if (is_method_type)
10626 continue;
10627 }
10628 Dwarf_Die parm_type_die;
10629 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
10630 continue;
10631 string qname =
10632 pretty_print
10633 ? rdr.get_die_pretty_representation(&parm_type_die,
10634 where_offset, guard)
10635 : die_type_name(rdr, &parm_type_die,
10636 qualified_name, where_offset, guard);
10637
10638 if (qname.empty())
10639 continue;
10640 parm_names.push_back(qname);
10641 }
10642 else if (child_tag == DW_TAG_unspecified_parameters)
10643 {
10644 // This is a variadic function parameter.
10645 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
10646 // After a DW_TAG_unspecified_parameters tag, we shouldn't
10647 // keep reading for parameters. The
10648 // unspecified_parameters TAG should be the last parameter
10649 // that we record. For instance, if there are multiple
10650 // DW_TAG_unspecified_parameters DIEs then we should care
10651 // only for the first one.
10652 break;
10653 }
10654 }
10655 while (dwarf_siblingof(&child, &child) == 0);
10656
10657 if (class_name.empty())
10658 {
10659 Dwarf_Die parent_die;
10660 if (get_parent_die(rdr, die, parent_die, where_offset))
10661 {
10662 if (die_is_class_type(&parent_die)
10663 && !is_anonymous_type_die(&parent_die))
10664 class_name = die_type_name(rdr, &parent_die,
10665 qualified_name,
10666 where_offset,
10667 guard);
10668 }
10669 }
10670
10671 guard.erase(off);
10672}
10673
10674/// This computes the signature of the a function declaration
10675/// represented by a DIE.
10676///
10677/// @param rdr the DWARF reader.
10678///
10679/// @param fn_die the DIE of the function to consider.
10680///
10681/// @param qualified_name if set to true then a qualified name is
10682/// going to be computed.
10683///
10684/// @param where_offset where we are logically at in the stream of
10685/// DIEs.
10686///
10687/// @param guard the set of DIE offsets of the stack of DIEs involved
10688/// in the construction of the signature of the function type. This
10689/// set is used to detect (and avoid) cycles in the stack of DIEs that
10690/// is going to be walked to compute the signature.
10691///
10692/// @return a copy of the computed function signature string.
10693static string
10694die_function_signature(const reader& rdr,
10695 const Dwarf_Die *fn_die,
10696 bool qualified_name,
10697 size_t where_offset,
10698 unordered_set<uint64_t>& guard)
10699{
10700
10702 bool has_lang = false;
10703 if ((has_lang = get_die_language(fn_die, lang)))
10704 {
10705 // In a binary originating from the C language, it's OK to use
10706 // the linkage name of the function as a key for the map which
10707 // is meant to reduce the number of DIE comparisons involved
10708 // during DIE canonicalization computation.
10709 if (is_c_language(lang))
10710 {
10711 string fn_name = die_linkage_name(fn_die);
10712 if (fn_name.empty())
10713 fn_name = die_name(fn_die);
10714 return fn_name;
10715 }
10716 }
10717
10718 // TODO: When we can structurally compare DIEs originating from C++
10719 // as well, we can use the linkage name of functions in C++ too, to
10720 // reduce the number of comparisons involved during DIE
10721 // canonicalization.
10722
10723 string return_type_name;
10724 Dwarf_Die ret_type_die;
10725 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
10726 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
10727 where_offset,
10728 guard);
10729
10730 if (return_type_name.empty())
10731 return_type_name = "void";
10732
10733 Dwarf_Die scope_die;
10734 string scope_name;
10735 if (qualified_name && get_scope_die(rdr, fn_die, where_offset, scope_die))
10736 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset, guard);
10737 string fn_name = die_name(fn_die);
10738 if (!scope_name.empty())
10739 fn_name = scope_name + "::" + fn_name;
10740
10741 string class_name;
10742 vector<string> parm_names;
10743 bool is_const = false;
10744 bool is_static = false;
10745 bool is_method_type = false;
10746
10747 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
10748 /*pretty_print=*/false,
10749 qualified_name, is_method_type,
10750 return_type_name, class_name,
10751 parm_names, is_const, is_static,
10752 guard);
10753
10754 bool is_virtual = die_is_virtual(fn_die);
10755
10756 string repr = is_method_type? "method" : "function";
10757 if (is_virtual)
10758 repr += " virtual";
10759
10760 if (!return_type_name.empty())
10761 repr += " " + return_type_name;
10762
10763 repr += " " + fn_name;
10764
10765 // Now parameters.
10766 repr += "(";
10767 bool some_parm_emitted = false;
10768 for (vector<string>::const_iterator i = parm_names.begin();
10769 i != parm_names.end();
10770 ++i)
10771 {
10772 if (i != parm_names.begin())
10773 {
10774 if (some_parm_emitted)
10775 repr += ", ";
10776 }
10777 else
10778 if (!is_static && is_method_type)
10779 // We are printing a non-static method name, skip the implicit "this"
10780 // parameter type.
10781 continue;
10782 repr += *i;
10783 some_parm_emitted = true;
10784 }
10785 repr += ")";
10786
10787 if (is_const)
10788 {
10790 repr += " const";
10791 }
10792
10793 return repr;
10794}
10795
10796/// Compute the flat representation string of a struct, class or union
10797/// type represented by a DIE.
10798///
10799/// The flat representation string looks like:
10800/// "struct {int foo; char blah;}.
10801///
10802/// That is useful to designate a struct (class or union) that is
10803/// anonymous.
10804///
10805/// @param rdr the DWARF reader to consider.
10806///
10807/// @param die the DIE of the type to return the flat representation
10808/// for.
10809///
10810/// @param indent the indentation string to use for the
10811/// representation.
10812///
10813/// @param one_line if true then the flat representation is
10814/// constructed on one line. Otherwise, each data member is
10815/// represented on its own line.
10816///
10817/// @param qualified_names if true then the data member (and their
10818/// type) names using in the representation are qualified.
10819///
10820/// @param where_offset where in the are logically are in the DIE
10821/// stream.
10822///
10823/// @param guard the set of DIE offsets of the stack of DIEs involved
10824/// in the construction of the flat representation of the type. This
10825/// set is used to detect (and avoid) cycles in the stack of DIEs that
10826/// is going to be walked to compute the flat representation.
10827static string
10828die_class_flat_representation(const reader& rdr,
10829 const Dwarf_Die* die,
10830 const string& indent,
10831 bool one_line,
10832 bool qualified_names,
10833 size_t where_offset,
10834 unordered_set<uint64_t>& guard)
10835{
10836 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10837
10838 string repr = indent;
10839 string local_indent = " ";
10840 string real_indent;
10841
10842 if (tag == DW_TAG_union_type)
10843 repr += "union";
10844 else if (tag == DW_TAG_structure_type)
10845 repr += "struct";
10846 else if (tag == DW_TAG_class_type)
10847 repr += "class";
10848 else
10850
10851 repr += " ";
10852
10853 if (die_is_anonymous(die))
10854 {
10855 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10856 if (guard.find(off) != guard.end())
10857 {
10858 repr += "{}";
10859 return repr;
10860 }
10861 guard.insert(off);
10862 }
10863
10864 if (!die_is_anonymous(die))
10865 repr += die_qualified_name(rdr, die, where_offset, guard);
10866
10867 repr += "{";
10868
10869 if (!one_line)
10870 repr += "\n";
10871
10872 Dwarf_Die member_child_die;
10873 bool first_sibling = true;
10874 for (bool got_it = get_member_child_die(die, &member_child_die);
10875 got_it;
10876 got_it = get_next_member_sibling_die(&member_child_die,
10877 &member_child_die),
10878 first_sibling = false)
10879 {
10880 // A member of the class is either a declaration or an anonymous
10881 // type. Otherwise, let's skip it.
10882 if (!die_is_decl(&member_child_die)
10883 && !(die_is_type(&member_child_die)
10884 && die_is_anonymous(&member_child_die)))
10885 continue;
10886
10887 if (one_line)
10888 real_indent = first_sibling ? "" : " " ;
10889 else
10890 real_indent = (first_sibling ? "": "\n") + indent + local_indent;
10891
10892 repr += real_indent;
10893
10894 repr += die_pretty_print_decl(rdr, &member_child_die,
10895 qualified_names,
10896 /*include_fns=*/false,
10897 where_offset,
10898 guard);
10899 repr += ";";
10900 }
10901
10902 if (one_line)
10903 repr += "}";
10904 else
10905 repr += indent + "}";
10906
10907 if (die_is_anonymous(die))
10908 {
10909 uint64_t off = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
10910 guard.erase(off);
10911 }
10912 return repr;
10913}
10914
10915/// Compute the flat representation string of a enum type represented
10916/// by a DIE.
10917///
10918/// The flat representation string looks like:
10919/// "enum {int foo; char blah;}.
10920///
10921/// That is useful to designate an enum that is anonymous.
10922///
10923/// @param rdr the DWARF reader to consider.
10924///
10925/// @param die the DIE of the type to return the flat representation
10926/// for.
10927///
10928/// @param indent the indentation string to use for the
10929/// representation.
10930///
10931/// @param one_line if true then the flat representation is
10932/// constructed on one line. Otherwise, each data member is
10933/// represented on its own line.
10934///
10935/// @param qualified_names if true then the data member (and their
10936/// type) names using in the representation are qualified.
10937///
10938/// @param where_offset where in the are logically are in the DIE
10939/// stream.
10940static string
10941die_enum_flat_representation(const reader& rdr,
10942 const Dwarf_Die* die,
10943 const string& indent,
10944 bool one_line,
10945 bool qualified_names,
10946 size_t where_offset)
10947{
10948 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10949
10950 std::ostringstream o;
10951 string local_indent = " ";
10952 string real_indent;
10953
10954 if (tag == DW_TAG_enumeration_type)
10955 o << "enum";
10956 else
10958
10959 o << " ";
10960
10961 if (!die_is_anonymous(die))
10962 o << (qualified_names
10963 ? die_qualified_name(rdr, die, where_offset)
10964 : die_name(die));
10965
10966 o << "{";
10967
10968 if (!one_line)
10969 o << "\n";
10970
10972 Dwarf_Die child;
10973 bool first_enumerator= true;
10974 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
10975 {
10976 do
10977 {
10978 if (dwarf_tag(&child) != DW_TAG_enumerator)
10979 continue;
10980
10981 string name, m;
10982 location l;
10983 die_loc_and_name(rdr, &child, l, name, m);
10984 uint64_t val = 0;
10985 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
10986
10987 if (one_line)
10988 real_indent = first_enumerator ? "" : ", ";
10989 else
10990 real_indent = first_enumerator ? "" : ",\n" + indent + local_indent;
10991 o << name + " = " << val;
10992 first_enumerator = false;
10993 }
10994 while (dwarf_siblingof(&child, &child) == 0);
10995 }
10996
10997 o << one_line ? string("}") : "\n" + indent;
10998 o << "}";
10999
11000 return o.str();
11001}
11002
11003/// Compute the flat representation string of a class or enum type
11004/// represented by a DIE.
11005///
11006/// The flat representation string looks like:
11007/// "union {int foo; char blah;}.
11008///
11009/// That is useful to designate a class or enum type that is
11010/// anonymous.
11011///
11012/// @param rdr the DWARF reader to consider.
11013///
11014/// @param die the DIE of the type to return the flat representation
11015/// for.
11016///
11017/// @param indent the indentation string to use for the
11018/// representation.
11019///
11020/// @param one_line if true then the flat representation is
11021/// constructed on one line. Otherwise, each data member is
11022/// represented on its own line.
11023///
11024/// @param qualified_names if true then the data member (and their
11025/// type) names using in the representation are qualified.
11026///
11027/// @param where_offset where in the are logically are in the DIE
11028/// stream.
11029///
11030/// @param guard the set of DIE offsets of the stack of DIEs involved
11031/// in the construction of the flat representation of the type. This
11032/// set is used to detect (and avoid) cycles in the stack of DIEs that
11033/// is going to be walked to compute the flat representation.
11034static string
11035die_class_or_enum_flat_representation(const reader& rdr,
11036 const Dwarf_Die* die,
11037 const string& indent,
11038 bool one_line,
11039 bool qualified_names,
11040 size_t where_offset,
11041 unordered_set<uint64_t>& guard)
11042{
11043 if (!die)
11044 return string();
11045
11046 string result;
11047 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11048
11049 switch (tag)
11050 {
11051 case DW_TAG_class_type:
11052 case DW_TAG_structure_type:
11053 case DW_TAG_union_type:
11054 result = die_class_flat_representation(rdr, die, indent,
11055 one_line, qualified_names,
11056 where_offset,
11057 guard);
11058 break;
11059 case DW_TAG_enumeration_type:
11060 result = die_enum_flat_representation(rdr, die, indent,
11061 one_line, qualified_names,
11062 where_offset);
11063 break;
11064 default:
11066 }
11067
11068 return result;
11069}
11070
11071/// Compute the flat representation string of a class or enum type
11072/// represented by a DIE.
11073///
11074/// The flat representation string looks like:
11075/// "union {int foo; char blah;}.
11076///
11077/// That is useful to designate a class or enum type that is
11078/// anonymous.
11079///
11080/// @param rdr the DWARF reader to consider.
11081///
11082/// @param die the DIE of the type to return the flat representation
11083/// for.
11084///
11085/// @param indent the indentation string to use for the
11086/// representation.
11087///
11088/// @param one_line if true then the flat representation is
11089/// constructed on one line. Otherwise, each data member is
11090/// represented on its own line.
11091///
11092/// @param qualified_names if true then the data member (and their
11093/// type) names using in the representation are qualified.
11094///
11095/// @param where_offset where in the are logically are in the DIE
11096/// stream.
11097static string
11098die_class_or_enum_flat_representation(const reader& rdr,
11099 const Dwarf_Die* die,
11100 const string& indent,
11101 bool one_line,
11102 bool qualified_names,
11103 size_t where_offset)
11104{
11105 unordered_set<uint64_t> guard;
11106 return die_class_or_enum_flat_representation(rdr, die, indent,
11107 one_line, qualified_names,
11108 where_offset, guard);
11109}
11110
11111/// Return a pretty string representation of a type, for internal purposes.
11112///
11113/// By internal purpose, we mean things like key-ing types for lookup
11114/// purposes and so on.
11115///
11116/// Note that this function is also used to pretty print functions.
11117/// For functions, it prints the *type* of the function.
11118///
11119/// @param rdr the context to use.
11120///
11121/// @param the DIE of the type to pretty print.
11122///
11123/// @param where_offset where we logically are placed when calling
11124/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11125/// entries.
11126///
11127/// @param guard the set of DIE offsets of the stack of DIEs involved
11128/// in the construction of the pretty representation of the type.
11129/// This set is used to detect (and avoid) cycles in the stack of DIEs
11130/// that is going to be walked to compute the pretty representation.
11131///
11132/// @return the resulting pretty representation.
11133static string
11134die_pretty_print_type(const reader& rdr,
11135 const Dwarf_Die* die,
11136 size_t where_offset,
11137 unordered_set<uint64_t>& guard)
11138{
11139 if (!die
11140 || (!die_is_type(die)
11141 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
11142 return "";
11143
11144 string repr;
11145
11146 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11147 switch (tag)
11148 {
11149 case DW_TAG_string_type:
11150 // For now, we won't try to go get the actual representation of
11151 // the string because this would make things more complicated;
11152 // for that we'd need to interpret some location expressions to
11153 // get the length of the string. And for dynamically allocated
11154 // strings, the result of the location expression evaluation
11155 // might not even be a constant. So at the moment I consider
11156 // this to be a lot of hassle for no great return. Until proven
11157 // otherwise, of course.
11158 repr = "string type";
11159
11160 case DW_TAG_unspecified_type:
11161 case DW_TAG_ptr_to_member_type:
11162 break;
11163
11164 case DW_TAG_namespace:
11165 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset,
11166 guard);
11167 break;
11168
11169 case DW_TAG_base_type:
11170 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11171 break;
11172
11173 case DW_TAG_typedef:
11174 {
11175 string qualified_name;
11176 if (!die_qualified_type_name_empty(rdr, die,
11177 where_offset,
11178 qualified_name,
11179 guard))
11180 repr = "typedef " + qualified_name;
11181 }
11182 break;
11183
11184 case DW_TAG_const_type:
11185 case DW_TAG_volatile_type:
11186 case DW_TAG_restrict_type:
11187 case DW_TAG_pointer_type:
11188 case DW_TAG_reference_type:
11189 case DW_TAG_rvalue_reference_type:
11190 repr = rdr.get_die_qualified_type_name(die, where_offset, guard);
11191 break;
11192
11193 case DW_TAG_enumeration_type:
11194 {
11195 string qualified_name =
11196 rdr.get_die_qualified_type_name(die, where_offset, guard);
11197 repr = "enum " + qualified_name;
11198 }
11199 break;
11200
11201 case DW_TAG_structure_type:
11202 case DW_TAG_class_type:
11203 {
11204 string qualified_name =
11205 rdr.get_die_qualified_type_name(die, where_offset, guard);
11206 repr = "class " + qualified_name;
11207 }
11208 break;
11209
11210 case DW_TAG_union_type:
11211 {
11212 string qualified_name =
11213 rdr.get_die_qualified_type_name(die, where_offset, guard);
11214 repr = "union " + qualified_name;
11215 }
11216 break;
11217
11218 case DW_TAG_array_type:
11219 {
11220 Dwarf_Die element_type_die;
11221 if (!die_die_attribute(die, DW_AT_type, element_type_die))
11222 break;
11223 string element_type_name =
11224 rdr.get_die_qualified_type_name(&element_type_die,
11225 where_offset, guard);
11226 if (element_type_name.empty())
11227 break;
11228
11230 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
11231 /*associate_type_to_die=*/false);
11232
11233 repr = element_type_name;
11234 repr += array_type_def::subrange_type::vector_as_string(subranges);
11235 }
11236 break;
11237
11238 case DW_TAG_subrange_type:
11239 {
11240 // So this can be generated by Ada, on its own; that is, not
11241 // as a subtype of an array. In that case we need to handle
11242 // it properly.
11243
11244 // For now, we consider that the pretty printed name of the
11245 // subrange type is its name. We might need something more
11246 // advance, should the needs of the users get more
11247 // complicated.
11248 repr += die_qualified_type_name(rdr, die, where_offset, guard);
11249 }
11250 break;
11251
11252 case DW_TAG_subroutine_type:
11253 case DW_TAG_subprogram:
11254 {
11255 string return_type_name;
11256 string class_name;
11257 vector<string> parm_names;
11258 bool is_const = false;
11259 bool is_static = false;
11260 bool is_method_type = false;
11261 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
11262 /*pretty_print=*/true,
11263 /*qualified_name=*/true,
11265 return_type_name, class_name,
11266 parm_names, is_const,
11267 is_static, guard);
11268 if (!is_method_type)
11269 repr = "function type";
11270 else
11271 repr = "method type";
11272 repr += " " + rdr.get_die_qualified_type_name(die, where_offset, guard);
11273 }
11274 break;
11275
11276 case DW_TAG_set_type:
11277 case DW_TAG_file_type:
11278 case DW_TAG_packed_type:
11279 case DW_TAG_thrown_type:
11280 case DW_TAG_interface_type:
11281 case DW_TAG_shared_type:
11283 }
11284
11285 return repr;
11286}
11287
11288/// Return a pretty string representation of a declaration, for
11289/// internal purposes.
11290///
11291/// By internal purpose, we mean things like key-ing declarations for
11292/// lookup purposes and so on.
11293///
11294/// Note that this function is also used to pretty print functions.
11295/// For functions, it prints the signature of the function.
11296///
11297/// @param rdr the context to use.
11298///
11299/// @param die the DIE of the declaration to pretty print.
11300///
11301/// @param qualified_name if true then use qualified names.
11302///
11303/// @param where_offset where we logically are placed when calling
11304/// this. It's useful to handle inclusion of DW_TAG_compile_unit
11305/// entries.
11306///
11307/// @param guard the set of DIE offsets of the stack of DIEs involved
11308/// in the construction of the pretty representation of the decl.
11309/// This set is used to detect (and avoid) cycles in the stack of DIEs
11310/// that is going to be walked to compute the pretty representation.
11311///
11312/// @return the resulting pretty representation.
11313static string
11314die_pretty_print_decl(const reader& rdr,
11315 const Dwarf_Die* die,
11316 bool qualified_name,
11317 bool include_fns,
11318 size_t where_offset,
11319 unordered_set<uint64_t>& guard)
11320{
11321 if (!die || !die_is_decl(die))
11322 return "";
11323
11324 string repr;
11325
11326 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11327 switch (tag)
11328 {
11329 case DW_TAG_namespace:
11330 repr = "namespace " + die_qualified_name(rdr, die, where_offset, guard);
11331 break;
11332
11333 case DW_TAG_member:
11334 case DW_TAG_variable:
11335 {
11336 string type_repr = "void";
11337 Dwarf_Die type_die;
11338 if (die_die_attribute(die, DW_AT_type, type_die))
11339 type_repr = die_type_name(rdr, &type_die,
11340 qualified_name,
11341 where_offset,
11342 guard);
11343 repr = (qualified_name
11344 ? die_qualified_name(rdr, die, where_offset, guard)
11345 : die_name(die));
11346
11347 if (repr.empty())
11348 repr = type_repr;
11349 else
11350 repr = type_repr + " " + repr;
11351 }
11352 break;
11353
11354 case DW_TAG_subprogram:
11355 if (include_fns)
11356 repr = die_function_signature(rdr, die, qualified_name,
11357 where_offset, guard);
11358 break;
11359
11360 default:
11361 break;
11362 }
11363 return repr;
11364}
11365
11366/// Compute the pretty printed representation of an artifact
11367/// represented by a DIE.
11368///
11369/// If the DIE is a type, compute the its pretty representation as a
11370/// type; otherwise, if it's a declaration, compute its pretty
11371/// representation as a declaration. Note for For instance, that a
11372/// DW_TAG_subprogram DIE is going to be represented as a function
11373/// *type*.
11374///
11375/// @param rdr the DWARF reader.
11376///
11377/// @param die the DIE to consider.
11378///
11379/// @param where_offset we in the DIE stream we are logically at.
11380///
11381/// @param guard the set of DIE offsets of the stack of DIEs involved
11382/// in the construction of the pretty representation of the DIe. This
11383/// set is used to detect (and avoid) cycles in the stack of DIEs that
11384/// is going to be walked to compute the pretty representation.
11385///
11386/// @return a copy of the pretty printed artifact.
11387static string
11388die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset,
11389 unordered_set<uint64_t>& guard)
11390{
11391 if (die_is_type(die))
11392 return die_pretty_print_type(rdr, die, where_offset, guard);
11393 else if (die_is_decl(die))
11394 return die_pretty_print_decl(rdr, die,
11395 /*qualified_names=*/true,
11396 /*include_fns=*/true,
11397 where_offset, guard);
11398 return "";
11399}
11400
11401// -----------------------------------
11402// </die pretty printer>
11403// -----------------------------------
11404
11405
11406// ----------------------------------
11407// <die comparison engine>
11408// ---------------------------------
11409
11410/// Compares two decls DIEs
11411///
11412/// This works only for DIEs emitted by the C language.
11413///
11414/// This implementation doesn't yet support namespaces.
11415///
11416/// This is a subroutine of compare_dies.
11417///
11418/// @return true iff @p l equals @p r.
11419static bool
11420compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
11421{
11422 ABG_ASSERT(l && r);
11423
11424 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11425 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11426 if (l_tag != r_tag)
11427 return false;
11428
11429 bool result = false;
11430
11431 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
11432 {
11433 // Fast path for functions and global variables.
11434 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
11435 result)
11436 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
11437 result))
11438 {
11439 if (!result)
11440 return false;
11441 }
11442
11443 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11444 result))
11445 {
11446 if (!result)
11447 return false;
11448 }
11449 return true;
11450 }
11451
11452 // Fast path for types.
11453 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
11454 result))
11455 return result;
11456 return true;
11457}
11458
11459/// Test if at least one of two ODR-relevant DIEs is decl-only.
11460///
11461/// @param rdr the DWARF reader to consider.
11462///
11463/// @param l the first type DIE to consider.
11464///
11465/// @param r the second type DIE to consider.
11466///
11467/// @return true iff either @p l or @p r is decl-only and both are
11468/// ODR-relevant.
11469static bool
11470at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
11471 const Dwarf_Die *l,
11472 const Dwarf_Die *r)
11473{
11474 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
11475 return false;
11476
11477 if ((die_is_declaration_only(l) && die_has_no_child(l))
11478 || (die_is_declaration_only(r) && die_has_no_child(r)))
11479 return true;
11480 return false;
11481}
11482
11483/// Compares two type DIEs
11484///
11485/// This is a subroutine of compare_dies.
11486///
11487/// Note that this function doesn't look at the name of the DIEs.
11488/// Naming is taken into account by the function compare_as_decl_dies.
11489///
11490/// If the two DIEs are from a translation unit that is subject to the
11491/// ONE Definition Rule, then the function considers that if one DIE
11492/// is a declaration, then it's equivalent to the second. In that
11493/// case, the sizes of the two DIEs are not compared. This is so that
11494/// a declaration of a type compares equal to the definition of the
11495/// type.
11496///
11497/// @param rdr the DWARF reader to consider.
11498///
11499/// @param l the left operand of the comparison operator.
11500///
11501/// @param r the right operand of the comparison operator.
11502///
11503/// @return true iff @p l equals @p r.
11504static bool
11505compare_as_type_dies(const reader& rdr,
11506 const Dwarf_Die *l,
11507 const Dwarf_Die *r)
11508{
11509 ABG_ASSERT(l && r);
11510 ABG_ASSERT(die_is_type(l));
11511 ABG_ASSERT(die_is_type(r));
11512
11513 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
11514 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
11515 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11516 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
11517 // For now, we cannot compare DW_TAG_string_type because of its
11518 // string_length attribute that is a location descriptor that is
11519 // not necessarily a constant. So it's super hard to evaluate it
11520 // in a libabigail context. So for now, we just say that all
11521 // DW_TAG_string_type DIEs are different, by default.
11522 return false;
11523
11524 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11525 // A declaration of a type compares equal to the definition of the
11526 // type.
11527 return true;
11528
11529 uint64_t l_size = 0, r_size = 0;
11530 die_size_in_bits(l, l_size);
11531 die_size_in_bits(r, r_size);
11532
11533 return l_size == r_size;
11534}
11535
11536/// Compare two DIEs as decls (looking as their names etc) and as
11537/// types (looking at their size etc).
11538///
11539/// @param rdr the DWARF reader to consider.
11540///
11541/// @param l the first DIE to consider.
11542///
11543/// @param r the second DIE to consider.
11544///
11545/// @return TRUE iff @p l equals @p r as far as naming and size is
11546/// concerned.
11547static bool
11548compare_as_decl_and_type_dies(const reader &rdr,
11549 const Dwarf_Die *l,
11550 const Dwarf_Die *r)
11551{
11552 if (!compare_as_decl_dies(l, r)
11553 || !compare_as_type_dies(rdr, l, r))
11554 return false;
11555
11556 return true;
11557}
11558
11559/// Test if two DIEs representing function declarations have the same
11560/// linkage name, and thus are considered equal if they are C or C++,
11561/// because the two DIEs represent functions in the same binary.
11562///
11563/// If the DIEs don't have a linkage name, the function compares their
11564/// name. But in that case, the caller of the function must know that
11565/// in C++ for instance, that doesn't imply that the two functions are
11566/// equal.
11567///
11568/// @param l the first function DIE to consider.
11569///
11570/// @param r the second function DIE to consider.
11571///
11572/// @return true iff the function represented by @p l have the same
11573/// linkage name as the function represented by @p r.
11574static bool
11575fn_die_equal_by_linkage_name(const Dwarf_Die *l,
11576 const Dwarf_Die *r)
11577{
11578 if (!!l != !!r)
11579 return false;
11580
11581 if (!l)
11582 return false;
11583
11584 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11585 ABG_ASSERT(tag == DW_TAG_subprogram);
11586 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11587 ABG_ASSERT(tag == DW_TAG_subprogram);
11588
11589 string lname = die_name(l), rname = die_name(r);
11590 string llinkage_name = die_linkage_name(l),
11591 rlinkage_name = die_linkage_name(r);
11592
11593 if (die_is_in_c_or_cplusplus(l)
11594 && die_is_in_c_or_cplusplus(r))
11595 {
11596 if (!llinkage_name.empty() && !rlinkage_name.empty())
11597 return llinkage_name == rlinkage_name;
11598 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
11599 return false;
11600 else
11601 return lname == rname;
11602 }
11603
11604 return (!llinkage_name.empty()
11605 && !rlinkage_name.empty()
11606 && llinkage_name == rlinkage_name);
11607}
11608
11609/// Compare two DIEs in the context of DIE canonicalization.
11610///
11611/// If DIE canonicalization is on, the function compares the DIEs
11612/// canonically and structurally. The two types of comparison should
11613/// be equal, of course.
11614///
11615/// @param rdr the DWARF reader.
11616///
11617/// @param l_offset the offset of the first canonical DIE to compare.
11618///
11619/// @param r_offset the offset of the second canonical DIE to compare.
11620///
11621/// @param l_die_source the source of the DIE denoted by the offset @p
11622/// l_offset.
11623///
11624/// @param r_die_source the source of the DIE denoted by the offset @p
11625/// r_offset.
11626///
11627/// @param l_has_canonical_die_offset output parameter. Is set to
11628/// true if @p l_offset has a canonical DIE.
11629///
11630/// @param r_has_canonical_die_offset output parameter. Is set to
11631/// true if @p r_offset has a canonical DIE.
11632///
11633/// @param l_canonical_die_offset output parameter. If @p
11634/// l_has_canonical_die_offset is set to true, then this parameter is
11635/// set to the offset of the canonical DIE of the DIE designated by @p
11636/// l_offset.
11637static bool
11638try_canonical_die_comparison(const reader& rdr,
11639 Dwarf_Off l_offset, Dwarf_Off r_offset,
11640 die_source l_die_source, die_source r_die_source,
11641 bool& l_has_canonical_die_offset,
11642 bool& r_has_canonical_die_offset,
11643 Dwarf_Off& l_canonical_die_offset,
11644 Dwarf_Off& r_canonical_die_offset,
11645 bool& result)
11646{
11647#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11648 if (rdr.debug_die_canonicalization_is_on_
11649 && !rdr.use_canonical_die_comparison_)
11650 return false;
11651#endif
11652
11653
11654 l_has_canonical_die_offset =
11655 (l_canonical_die_offset =
11656 rdr.get_canonical_die_offset(l_offset, l_die_source,
11657 /*die_as_type=*/true));
11658
11659 r_has_canonical_die_offset =
11660 (r_canonical_die_offset =
11661 rdr.get_canonical_die_offset(r_offset, r_die_source,
11662 /*die_as_type=*/true));
11663
11664 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
11665 {
11666 result = (l_canonical_die_offset == r_canonical_die_offset);
11667 return true;
11668 }
11669
11670 return false;
11671}
11672
11673#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11674/// This function is called whenever a DIE comparison fails.
11675///
11676/// This function is intended for debugging purposes. The idea is for
11677/// hackers to set a breakpoint on this function so that they can
11678/// discover why exactly the comparison failed. They then can execute
11679/// the program from compare_dies_during_canonicalization, for
11680/// instance.
11681///
11682/// @param @l the left-hand side of the DIE comparison.
11683///
11684/// @param @r the right-hand side of the DIE comparison.
11685static void
11686notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
11687{
11688}
11689
11690#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
11691 notify_die_comparison_failed(l, r)
11692#else
11693#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
11694#endif
11695
11696/// A macro used to return from DIE comparison routines.
11697///
11698/// If the return value is false, the macro invokes the
11699/// notify_die_comparison_failed signalling function before returning.
11700/// That way, hackers willing to learn more about why the comparison
11701/// routine returned "false" can just set a breakpoint on
11702/// notify_die_comparison_failed and execute the program from
11703/// compare_dies_during_canonicalization, for instance.
11704///
11705/// @param value the value to return from the DIE comparison routines.
11706#define ABG_RETURN(value) \
11707 do \
11708 { \
11709 if ((value) == COMPARISON_RESULT_DIFFERENT) \
11710 { \
11711 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11712 } \
11713 return return_comparison_result(l, r, dies_being_compared, \
11714 value, aggregates_being_compared, \
11715 update_canonical_dies_on_the_fly); \
11716 } \
11717 while(false)
11718
11719/// A macro used to return the "false" boolean from DIE comparison
11720/// routines.
11721///
11722/// As the return value is false, the macro invokes the
11723/// notify_die_comparison_failed signalling function before returning.
11724///
11725/// @param value the value to return from the DIE comparison routines.
11726#define ABG_RETURN_FALSE \
11727 do \
11728 { \
11729 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11730 return return_comparison_result(l, r, dies_being_compared, \
11731 COMPARISON_RESULT_DIFFERENT, \
11732 aggregates_being_compared, \
11733 update_canonical_dies_on_the_fly); \
11734 } while(false)
11735
11736/// A macro to set the 'result' variable to 'false'.
11737///
11738/// The macro invokes the notify_die_comparison_failed function so
11739/// that the hacker can set a debugging breakpoint on
11740/// notify_die_comparison_failed to know where a DIE comparison failed
11741/// during compare_dies_during_canonicalization for instance.
11742///
11743/// @param result the 'result' variable to set.
11744///
11745/// @param l the first DIE of the comparison operation.
11746///
11747/// @param r the second DIE of the comparison operation.
11748#define SET_RESULT_TO_FALSE(result, l , r) \
11749 do \
11750 { \
11751 result = COMPARISON_RESULT_DIFFERENT; \
11752 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11753 } while(false)
11754
11755/// A macro to set the 'result' variable to a given value.
11756///
11757/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
11758/// invokes the notify_die_comparison_failed function so that the
11759/// hacker can set a debugging breakpoint on
11760/// notify_die_comparison_failed to know where a DIE comparison failed
11761/// during compare_dies_during_canonicalization for instance.
11762///
11763/// @param result the 'result' variable to set.
11764///
11765/// @param l the first DIE of the comparison operation.
11766///
11767/// @param r the second DIE of the comparison operation.
11768#define SET_RESULT_TO(result, value, l , r) \
11769 do \
11770 { \
11771 result = (value); \
11772 if (result == COMPARISON_RESULT_DIFFERENT) \
11773 { \
11774 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
11775 } \
11776 } while(false)
11777
11778#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
11779 do \
11780 { \
11781 if (aggregates_being_compared.contains(dies_being_compared)) \
11782 { \
11783 result = COMPARISON_RESULT_CYCLE_DETECTED; \
11784 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
11785 ABG_RETURN(result); \
11786 } \
11787 } \
11788 while(false)
11789
11790/// Get the next member sibling of a given class or union member DIE.
11791///
11792/// @param die the DIE to consider.
11793///
11794/// @param member out parameter. This is set to the next member
11795/// sibling, iff the function returns TRUE.
11796///
11797/// @return TRUE iff the function set @p member to the next member
11798/// sibling DIE.
11799static bool
11800get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
11801{
11802 if (!die)
11803 return false;
11804
11805 bool found_member = false;
11806 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
11807 member) == 0);
11808 found_member;
11809 found_member = (dwarf_siblingof(member, member) == 0))
11810 {
11811 int tag = dwarf_tag(member);
11812 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
11813 break;
11814 }
11815
11816 return found_member;
11817}
11818
11819/// Get the first child DIE of a class/struct/union DIE that is a
11820/// member DIE.
11821///
11822/// Note that a member DIE is represented by a DWARF tag that is
11823/// either DW_TAG_member, DW_TAG_inheritance.
11824///
11825/// @param die the DIE to consider.
11826///
11827/// @param child out parameter. This is set to the first child DIE of
11828/// @p iff this function returns TRUE.
11829///
11830/// @return TRUE iff @p child is set to the first child DIE of @p die
11831/// that is a member DIE.
11832static bool
11833get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
11834{
11835 if (!die)
11836 return false;
11837
11838 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
11839 ABG_ASSERT(tag == DW_TAG_structure_type
11840 || tag == DW_TAG_union_type
11841 || tag == DW_TAG_class_type);
11842
11843 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die), child) == 0);
11844
11845 if (!found_child)
11846 return false;
11847
11848 tag = dwarf_tag(child);
11849
11850 if (!(tag == DW_TAG_member
11851 || tag == DW_TAG_inheritance
11852 || tag == DW_TAG_subprogram))
11853 found_child = get_next_member_sibling_die(child, child);
11854
11855 return found_child;
11856}
11857
11858/// This is a sub-routine of return_comparison_result.
11859///
11860/// Propagate the canonical type of a the right-hand-side DIE to the
11861/// lef-hand-side DIE. This is a optimization that is done when the
11862/// two DIEs compare equal.
11863///
11864/// If the right-hand-side DIE is not canonicalized, the function
11865/// performs its canonicalization.
11866///
11867/// This optimization is performed only if
11868/// is_canon_type_to_be_propagated_tag returns true.
11869///
11870/// @param rdr the current context to consider.
11871///
11872/// @param l the left-hand-side DIE of the comparison. It's going to
11873/// receive the canonical type of the other DIE.
11874///
11875/// @param r the right-hand-side DIE of the comparison. Its canonical
11876/// type is propagated to @p l.
11877static void
11878maybe_propagate_canonical_type(const reader& rdr,
11879 const Dwarf_Die* l,
11880 const Dwarf_Die* r)
11881{
11882 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
11883 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
11884
11885 if (l_tag != r_tag)
11886 return;
11887
11888 if (is_canon_type_to_be_propagated_tag(l_tag))
11889 propagate_canonical_type(rdr, l, r);
11890}
11891
11892/// Propagate the canonical type of a the right-hand-side DIE to the
11893/// left-hand-side DIE. This is a optimization that is done when the
11894/// two DIEs compare equal.
11895///
11896/// If the right-hand-side DIE is not canonicalized, the function
11897/// performs its canonicalization.
11898///
11899/// @param rdr the current context to consider.
11900///
11901/// @param l the left-hand-side DIE of the comparison. It's going to
11902/// receive the canonical type of the other DIE.
11903///
11904/// @param r the right-hand-side DIE of the comparison. Its canonical
11905/// type is propagated to @p l.
11906static void
11907propagate_canonical_type(const reader& rdr,
11908 const Dwarf_Die* l,
11909 const Dwarf_Die* r)
11910{
11911 ABG_ASSERT(l && r);
11912
11913 // If 'l' has no canonical DIE and if 'r' has one, then propagage
11914 // the canonical DIE of 'r' to 'l'.
11915 //
11916 // In case 'r' has no canonical DIE, then compute it, and then
11917 // propagate that canonical DIE to 'r'.
11918 const die_source l_source = rdr.get_die_source(l);
11919 const die_source r_source = rdr.get_die_source(r);
11920
11921 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
11922 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
11923 bool l_has_canonical_die_offset = false;
11924 bool r_has_canonical_die_offset = false;
11925 Dwarf_Off l_canonical_die_offset = 0;
11926 Dwarf_Off r_canonical_die_offset = 0;
11927
11928 l_has_canonical_die_offset =
11929 (l_canonical_die_offset =
11930 rdr.get_canonical_die_offset(l_offset, l_source,
11931 /*die_as_type=*/true));
11932
11933 r_has_canonical_die_offset =
11934 (r_canonical_die_offset =
11935 rdr.get_canonical_die_offset(r_offset, r_source,
11936 /*die_as_type=*/true));
11937
11938
11939 if (!l_has_canonical_die_offset
11940 && r_has_canonical_die_offset
11941 // A DIE can be equivalent only to another DIE of the same
11942 // source.
11943 && l_source == r_source)
11944 {
11945 ABG_ASSERT(r_canonical_die_offset);
11946 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
11947 /*die_as_type=*/true);
11948 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
11949 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
11950 rdr.canonical_propagated_count_++;
11951 }
11952}
11953
11954/// This function does the book keeping of comparison pairs necessary
11955/// to handle
11956///
11957/// * the detection of cycles during the comparison of aggregate
11958/// types, in conjuction with the macro
11959/// RETURN_IF_COMPARISON_CYCLE_DETECTED
11960///
11961/// * the handling of the canonical type propagation optimisation
11962/// to speed-up type canonicalization.
11963///
11964///
11965/// Note that this function is essentially a sub-routine of
11966/// compare_dies.
11967///
11968/// @param l the left-hand-side DIE being compared.
11969///
11970/// @param r the right-hand-side DIE being compared.
11971///
11972/// @param cur_dies the pair of die offsets of l and r. This is
11973/// redundant as it can been computed from @p l and @p r. However,
11974/// getting it as an argument is an optimization to avoid computing it
11975/// over and over again, given how often this function is invoked from
11976/// compare_dies.
11977///
11978/// @param return the result of comparing @p l against @p r.
11979///
11980/// @param comparison_stack the stack of pair of type DIEs being
11981/// compared.
11982///
11983/// @param do_propagate_canonical_type if true then the function
11984/// performs canonical DIEs propagation, meaning that if @p l equals
11985/// @p r and if @p r has a canonical type, then the canonical type of
11986/// @p l is set to the canonical type of @p r.
11987static comparison_result
11988return_comparison_result(const Dwarf_Die* l,
11989 const Dwarf_Die* r,
11990 const offset_pair_type& cur_dies,
11991 comparison_result result,
11992 offset_pairs_stack_type& comparison_stack,
11993 bool do_propagate_canonical_type = true)
11994{
11995 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
11996
11997 if (result == COMPARISON_RESULT_EQUAL)
11998 {
11999 // The result comparing the two types is "true", basically. So
12000 // let's propagate the canonical type of r onto l, so that we
12001 // don't need to compute the canonical type of r.
12002 if (do_propagate_canonical_type)
12003 {
12004 // Propagate canonical type.
12005 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12006
12007 // TODO: do we need to confirm any tentative canonical
12008 // propagation?
12009 }
12010 }
12011 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12012 {
12013 // So upon detection of the comparison cycle, compare_dies
12014 // returned early with the comparison result
12015 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
12016 // carry on with the comparison of all the OTHER sub-types of
12017 // the redundant type. If they all compare equal, then it means
12018 // the redundant type pair compared equal. Otherwise, it
12019 // compared different.
12020 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
12021 // Let's fall through to let the end of this function set the
12022 // result to COMPARISON_RESULT_UNKNOWN;
12023 }
12024 else if (result == COMPARISON_RESULT_UNKNOWN)
12025 {
12026 // Here is an introductory comment describing what we are going
12027 // to do in this case where the result of the comparison of the
12028 // current pair of type is not "false", basically.
12029 //
12030 // This means that we don't yet know what the result of
12031 // comparing these two types is, because one of the sub-types of
12032 // the types being compared is "redundant", meaning it appears
12033 // more than once in the comparison stack, so if we were to
12034 // naively try to carry on with the comparison member-wise, we'd
12035 // end up with an endless loop, a.k.a "comparison cycle".
12036 //
12037 // If the current type pair is redundant then:
12038 //
12039 // * This is a redundant type that has just been fully
12040 // compared. In that case, all the types that depend on
12041 // this redundant type and that have been tentatively
12042 // canonical-type-propagated must see their canonical types
12043 // "confirmed". This means that this type is going to be
12044 // considered as not being redundant anymore, meaning all
12045 // the types that depend on it must be updated as not being
12046 // dependant on it anymore, and the type itsef must be
12047 // removed from the map of redundant types.
12048 //
12049 // After the type's canonical-type-propagation is confirmed,
12050 // the result of its comparison must also be changed into
12051 // COMPARISON_RESULT_EQUAL.
12052 //
12053 // After that, If the current type depends on a redundant type,
12054 // then propagate its canonical type AND track it as having its
12055 // type being canonical-type-propagated.
12056 //
12057 // If the current type is not redundant however, then it must be
12058 // dependant on a redundant type. If it's not dependant on a
12059 // redundant type, then it must be of those types which
12060 // comparisons are not tracked for cycle, probably because they
12061 // are not aggregates. Otherwise, ABORT to understand why. I
12062 // believe this should not happen. In any case, after that
12063 // safety check is passed, we just need to return at this point.
12064
12065 if (comparison_stack.is_redundant(cur_dies)
12066 && comparison_stack.vect_.back() == cur_dies)
12067 {
12068 // We are in the case described above of a redundant type
12069 // that has been fully compared.
12070 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12071 comparison_stack.confirm_canonical_propagated_type(cur_dies);
12072
12073 result = COMPARISON_RESULT_EQUAL;
12074 }
12075 else if (is_canon_type_to_be_propagated_tag(l_tag)
12076 && comparison_stack.vect_.back() == cur_dies)
12077 {
12078 // The current type is not redundant. So, as described in
12079 // the introductory comment above, it must be dependant on a
12080 // redundant type.
12081 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
12082 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
12083 // Then pass through.
12084 }
12085 }
12086 else if (result == COMPARISON_RESULT_DIFFERENT)
12087 {
12088 // Here is an introductory comment describing what we are going
12089 // to do in this case where the result of the comparison of the
12090 // current pair of type is "false", basically.
12091 //
12092 // If the type pair {l,r} is redundant then cancel the
12093 // canonical-type-propagation of all the dependant pairs that
12094 // depends on this redundant {l, r}. This means walk the types
12095 // that depends on {l, r} and cancel their
12096 // canonical-propagate-type, that means remove their canonical
12097 // types and mark them as not being canonically-propagated.
12098 // Also, erase their cached comparison results that was likely
12099 // set to COMPARISON_RESULT_UNKNOWN.
12100 //
12101 // Also, update the cached result for this pair, that was likely
12102 // to be COMPARISON_RESULT_UNKNOWN.
12103 if (comparison_stack.is_redundant(cur_dies)
12104 && comparison_stack.vect_.back() == cur_dies)
12105 comparison_stack.cancel_canonical_propagated_type(cur_dies);
12106 }
12107 else
12108 {
12109 // We should never reach here.
12111 }
12112
12113 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
12114 result = COMPARISON_RESULT_UNKNOWN;
12115 else if (is_canon_type_to_be_propagated_tag(l_tag)
12116 && !comparison_stack.vect_.empty()
12117 && comparison_stack.vect_.back() == cur_dies)
12118 //Finally pop the pair types being compared from comparison_stack
12119 //iff {l,r} is on the top of the stack. If it's not, then it means
12120 //we are looking at a type that was detected as a being redundant
12121 //and thus hasn't been pushed to the stack yet gain.
12122 comparison_stack.erase(cur_dies);
12123
12124 maybe_cache_type_comparison_result(comparison_stack.rdr_,
12125 l_tag, cur_dies, result);
12126
12127 return result;
12128}
12129
12130/// Compare two DIEs emitted by a C compiler.
12131///
12132/// @param rdr the DWARF reader used to load the DWARF information.
12133///
12134/// @param l the left-hand-side argument of this comparison operator.
12135///
12136/// @param r the righ-hand-side argument of this comparison operator.
12137///
12138/// @param aggregates_being_compared this holds the names of the set
12139/// of aggregates being compared. It's used by the comparison
12140/// function to avoid recursing infinitely when faced with types
12141/// referencing themselves through pointers or references. By
12142/// default, just pass an empty instance of @ref istring_set_type to
12143/// it.
12144///
12145/// @param update_canonical_dies_on_the_fly if true, when two
12146/// sub-types compare equal (during the comparison of @p l and @p r)
12147/// update their canonical type. That way, two types of the same name
12148/// are structurally compared to each other only once. So the
12149/// non-linear structural comparison of two types of the same name
12150/// only happen once.
12151///
12152/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12153static comparison_result
12154compare_dies(const reader& rdr,
12155 const Dwarf_Die *l, const Dwarf_Die *r,
12156 offset_pairs_stack_type& aggregates_being_compared,
12157 bool update_canonical_dies_on_the_fly)
12158{
12159 ABG_ASSERT(l);
12160 ABG_ASSERT(r);
12161
12162 const die_source l_die_source = rdr.get_die_source(l);
12163 const die_source r_die_source = rdr.get_die_source(r);
12164
12165 offset_type l_offset =
12166 {
12167 l_die_source,
12168 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12169 };
12170
12171 offset_type r_offset =
12172 {
12173 r_die_source,
12174 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12175 };
12176
12177 offset_pair_type dies_being_compared(l_offset, r_offset);
12178
12179 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
12180 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
12181
12182 if (l_tag != r_tag)
12184
12185 if (l_offset == r_offset)
12186 return COMPARISON_RESULT_EQUAL;
12187
12188 if (rdr.leverage_dwarf_factorization()
12189 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
12190 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
12191 if (l_offset != r_offset)
12192 return COMPARISON_RESULT_DIFFERENT;
12193
12194 comparison_result result = COMPARISON_RESULT_EQUAL;
12195 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
12196 dies_being_compared,
12197 result))
12198 return result;
12199
12200 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
12201 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
12202
12203 // If 'l' and 'r' already have canonical DIEs, then just compare the
12204 // offsets of their canonical DIEs.
12205 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
12206 {
12207 bool canonical_compare_result = false;
12208 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
12209 l_die_source, r_die_source,
12210 l_has_canonical_die_offset,
12211 r_has_canonical_die_offset,
12212 l_canonical_die_offset,
12213 r_canonical_die_offset,
12214 canonical_compare_result))
12215 {
12216 comparison_result result;
12217 SET_RESULT_TO(result,
12218 (canonical_compare_result
12219 ? COMPARISON_RESULT_EQUAL
12220 : COMPARISON_RESULT_DIFFERENT),
12221 l, r);
12222 return result;
12223 }
12224 }
12225
12226
12227
12228 switch (l_tag)
12229 {
12230 case DW_TAG_base_type:
12231 case DW_TAG_string_type:
12232 case DW_TAG_unspecified_type:
12233 if (!compare_as_decl_and_type_dies(rdr, l, r))
12234 SET_RESULT_TO_FALSE(result, l, r);
12235 break;
12236
12237 case DW_TAG_typedef:
12238 case DW_TAG_pointer_type:
12239 case DW_TAG_reference_type:
12240 case DW_TAG_rvalue_reference_type:
12241 case DW_TAG_const_type:
12242 case DW_TAG_volatile_type:
12243 case DW_TAG_restrict_type:
12244 {
12245 if (!compare_as_type_dies(rdr, l, r))
12246 {
12247 SET_RESULT_TO_FALSE(result, l, r);
12248 break;
12249 }
12250
12251 bool from_the_same_tu = false;
12252 if (!pointer_or_qual_die_of_anonymous_class_type(l)
12253 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
12254 && from_the_same_tu)
12255 {
12256 // These two typedefs, pointer, reference, or qualified
12257 // types have the same name and are defined in the same TU.
12258 // They thus ought to be the same.
12259 //
12260 // Note that pointers, reference or qualified types to
12261 // anonymous types are not taking into account here because
12262 // those always need to be structurally compared.
12263 SET_RESULT_TO_FALSE(result, l, r);
12264 break;
12265 }
12266 }
12267
12268 {
12269 // No fancy optimization in this case. We need to
12270 // structurally compare the two DIEs.
12271 Dwarf_Die lu_type_die, ru_type_die;
12272 bool lu_is_void, ru_is_void;
12273
12274 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
12275 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
12276
12277 if (lu_is_void && ru_is_void)
12278 result = COMPARISON_RESULT_EQUAL;
12279 else if (lu_is_void != ru_is_void)
12280 SET_RESULT_TO_FALSE(result, l, r);
12281 else
12282 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
12283 aggregates_being_compared,
12284 update_canonical_dies_on_the_fly);
12285 }
12286 break;
12287
12288 case DW_TAG_enumeration_type:
12289 if (!compare_as_decl_and_type_dies(rdr, l, r))
12290 SET_RESULT_TO_FALSE(result, l, r);
12291 else
12292 {
12293 // Walk the enumerators.
12294 Dwarf_Die l_enumtor, r_enumtor;
12295 bool found_l_enumtor = true, found_r_enumtor = true;
12296
12297 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12298 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
12299 &l_enumtor) == 0,
12300 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
12301 &r_enumtor) == 0;
12302 found_l_enumtor && found_r_enumtor;
12303 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
12304 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
12305 {
12306 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
12307 if ( l_tag != r_tag)
12308 {
12309 SET_RESULT_TO_FALSE(result, l, r);
12310 break;
12311 }
12312
12313 if (l_tag != DW_TAG_enumerator)
12314 continue;
12315
12316 uint64_t l_val = 0, r_val = 0;
12317 die_unsigned_constant_attribute(&l_enumtor,
12318 DW_AT_const_value,
12319 l_val);
12320 die_unsigned_constant_attribute(&r_enumtor,
12321 DW_AT_const_value,
12322 r_val);
12323 if (l_val != r_val)
12324 {
12325 SET_RESULT_TO_FALSE(result, l, r);
12326 break;
12327 }
12328 }
12329 if (found_l_enumtor != found_r_enumtor )
12330 SET_RESULT_TO_FALSE(result, l, r);
12331 }
12332 break;
12333
12334 case DW_TAG_structure_type:
12335 case DW_TAG_union_type:
12336 case DW_TAG_class_type:
12337 {
12338 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12339
12340 rdr.compare_count_++;
12341
12342 if (!compare_as_decl_and_type_dies(rdr, l, r))
12343 SET_RESULT_TO_FALSE(result, l, r);
12344 else if (rdr.options().assume_odr_for_cplusplus
12345 && rdr.odr_is_relevant(l)
12346 && rdr.odr_is_relevant(r)
12347 && !die_is_anonymous(l)
12348 && !die_is_anonymous(r))
12349 result = COMPARISON_RESULT_EQUAL;
12350 else
12351 {
12352 aggregates_being_compared.add(dies_being_compared);
12353
12354 Dwarf_Die l_member, r_member;
12355 bool found_l_member = true, found_r_member = true;
12356
12357 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
12358 for (found_l_member = get_member_child_die(l, &l_member),
12359 found_r_member = get_member_child_die(r, &r_member);
12360 found_l_member && found_r_member;
12361 found_l_member = get_next_member_sibling_die(&l_member,
12362 &l_member),
12363 found_r_member = get_next_member_sibling_die(&r_member,
12364 &r_member))
12365 {
12366 int l_tag = dwarf_tag(&l_member),
12367 r_tag = dwarf_tag(&r_member);
12368
12369 if (l_tag != r_tag)
12370 {
12371 SET_RESULT_TO_FALSE(result, l, r);
12372 break;
12373 }
12374
12375 ABG_ASSERT(l_tag == DW_TAG_member
12376 || l_tag == DW_TAG_variable
12377 || l_tag == DW_TAG_inheritance
12378 || l_tag == DW_TAG_subprogram);
12379
12380 comparison_result local_result =
12381 compare_dies(rdr, &l_member, &r_member,
12382 aggregates_being_compared,
12383 update_canonical_dies_on_the_fly);
12384
12385 if (local_result == COMPARISON_RESULT_UNKNOWN)
12386 // Note that if the result of comparing any
12387 // sub-type is COMPARISON_RESULT_EQUAL, just
12388 // because we have at least one sub-type's
12389 // comparison being COMPARISON_RESULT_UNKNOWN
12390 // means that the comparison of this type will
12391 // return COMPARISON_RESULT_UNKNOWN to show
12392 // callers that this type (and all the types that
12393 // depend on it) depends on a redundant type
12394 result = local_result;
12395
12396 if (local_result == COMPARISON_RESULT_DIFFERENT)
12397 {
12398 SET_RESULT_TO_FALSE(result, l, r);
12399 break;
12400 }
12401 }
12402 if (found_l_member != found_r_member)
12403 {
12404 SET_RESULT_TO_FALSE(result, l, r);
12405 break;
12406 }
12407 }
12408 }
12409 break;
12410
12411 case DW_TAG_array_type:
12412 {
12413 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12414
12415 aggregates_being_compared.add(dies_being_compared);
12416
12417 rdr.compare_count_++;
12418
12419 Dwarf_Die l_child, r_child;
12420 bool found_l_child, found_r_child;
12421 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12422 &l_child) == 0,
12423 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12424 &r_child) == 0;
12425 found_l_child && found_r_child;
12426 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
12427 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
12428 {
12429 int l_child_tag = dwarf_tag(&l_child),
12430 r_child_tag = dwarf_tag(&r_child);
12431 if (l_child_tag == DW_TAG_subrange_type
12432 || r_child_tag == DW_TAG_subrange_type)
12433 {
12434 result = compare_dies(rdr, &l_child, &r_child,
12435 aggregates_being_compared,
12436 update_canonical_dies_on_the_fly);
12437 if (!result)
12438 {
12439 SET_RESULT_TO_FALSE(result, l, r);
12440 break;
12441 }
12442 }
12443 }
12444 if (found_l_child != found_r_child)
12445 SET_RESULT_TO_FALSE(result, l, r);
12446 // Compare the types of the elements of the array.
12447 Dwarf_Die ltype_die, rtype_die;
12448 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
12449 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
12450 ABG_ASSERT(found_ltype && found_rtype);
12451
12452 result = compare_dies(rdr, &ltype_die, &rtype_die,
12453 aggregates_being_compared,
12454 update_canonical_dies_on_the_fly);
12455 if (!result)
12457 }
12458 break;
12459
12460 case DW_TAG_subrange_type:
12461 {
12462 uint64_t l_lower_bound = 0, r_lower_bound = 0,
12463 l_upper_bound = 0, r_upper_bound = 0;
12464 bool l_lower_bound_set = false, r_lower_bound_set = false,
12465 l_upper_bound_set = false, r_upper_bound_set = false;
12466
12467 l_lower_bound_set =
12468 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
12469 r_lower_bound_set =
12470 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
12471
12472 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
12473 l_upper_bound))
12474 {
12475 uint64_t l_count = 0;
12476 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
12477 {
12478 l_upper_bound = l_lower_bound + l_count;
12479 l_upper_bound_set = true;
12480 if (l_upper_bound)
12481 --l_upper_bound;
12482 }
12483 }
12484 else
12485 l_upper_bound_set = true;
12486
12487 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
12488 r_upper_bound))
12489 {
12490 uint64_t r_count = 0;
12491 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
12492 {
12493 r_upper_bound = r_lower_bound + r_count;
12494 r_upper_bound_set = true;
12495 if (r_upper_bound)
12496 --r_upper_bound;
12497 }
12498 }
12499 else
12500 r_upper_bound_set = true;
12501
12502 if ((l_lower_bound_set != r_lower_bound_set)
12503 || (l_upper_bound_set != r_upper_bound_set)
12504 || (l_lower_bound != r_lower_bound)
12505 || (l_upper_bound != r_upper_bound))
12506 SET_RESULT_TO_FALSE(result, l, r);
12507 }
12508 break;
12509
12510 case DW_TAG_subroutine_type:
12511 case DW_TAG_subprogram:
12512 {
12513 RETURN_IF_COMPARISON_CYCLE_DETECTED;
12514
12515 aggregates_being_compared.add(dies_being_compared);
12516
12517 rdr.compare_count_++;
12518
12519 if (l_tag == DW_TAG_subprogram
12520 && !fn_die_equal_by_linkage_name(l, r))
12521 {
12522 SET_RESULT_TO_FALSE(result, l, r);
12523 break;
12524 }
12525 else if (l_tag == DW_TAG_subprogram
12526 && die_is_in_c(l) && die_is_in_c(r))
12527 {
12528 result = COMPARISON_RESULT_EQUAL;
12529 break;
12530 }
12531 else if (!die_is_in_c(l) && !die_is_in_c(r))
12532 {
12533 // In C, we cannot have two different functions with the
12534 // same linkage name in a given binary. But here we are
12535 // looking at DIEs that don't originate from C. So we
12536 // need to compare return types and parameter types.
12537 Dwarf_Die l_return_type, r_return_type;
12538 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
12539 l_return_type);
12540 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
12541 r_return_type);
12542 if (l_return_type_is_void != r_return_type_is_void
12543 || (!l_return_type_is_void
12544 && !compare_dies(rdr,
12545 &l_return_type, &r_return_type,
12546 aggregates_being_compared,
12547 update_canonical_dies_on_the_fly)))
12548 SET_RESULT_TO_FALSE(result, l, r);
12549 else
12550 {
12551 Dwarf_Die l_child, r_child;
12552 bool found_l_child, found_r_child;
12553 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
12554 &l_child) == 0,
12555 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
12556 &r_child) == 0;
12557 found_l_child && found_r_child;
12558 found_l_child = dwarf_siblingof(&l_child,
12559 &l_child) == 0,
12560 found_r_child = dwarf_siblingof(&r_child,
12561 &r_child)==0)
12562 {
12563 int l_child_tag = dwarf_tag(&l_child);
12564 int r_child_tag = dwarf_tag(&r_child);
12565 comparison_result local_result =
12566 COMPARISON_RESULT_EQUAL;
12567 if (l_child_tag != r_child_tag)
12568 local_result = COMPARISON_RESULT_DIFFERENT;
12569 if (l_child_tag == DW_TAG_formal_parameter)
12570 local_result =
12571 compare_dies(rdr, &l_child, &r_child,
12572 aggregates_being_compared,
12573 update_canonical_dies_on_the_fly);
12574 if (local_result == COMPARISON_RESULT_DIFFERENT)
12575 {
12576 result = local_result;
12577 SET_RESULT_TO_FALSE(result, l, r);
12578 break;
12579 }
12580 if (local_result == COMPARISON_RESULT_UNKNOWN)
12581 // Note that if the result of comparing any
12582 // sub-type is COMPARISON_RESULT_EQUAL, just
12583 // because we have at least one sub-type's
12584 // comparison being COMPARISON_RESULT_UNKNOWN
12585 // means that the comparison of this type will
12586 // return COMPARISON_RESULT_UNKNOWN to show
12587 // callers that this type (and all the types
12588 // that depend on it) depends on a redundant
12589 // type and so, can't be
12590 // canonical-type-propagated.
12591 result = local_result;
12592 }
12593 if (found_l_child != found_r_child)
12594 {
12595 SET_RESULT_TO_FALSE(result, l, r);
12596 break;
12597 }
12598 }
12599 }
12600 }
12601 break;
12602
12603 case DW_TAG_formal_parameter:
12604 {
12605 Dwarf_Die l_type, r_type;
12606 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
12607 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
12608 if (l_type_is_void != r_type_is_void)
12609 SET_RESULT_TO_FALSE(result, l, r);
12610 else if (!l_type_is_void)
12611 {
12612 comparison_result local_result =
12613 compare_dies(rdr, &l_type, &r_type,
12614 aggregates_being_compared,
12615 update_canonical_dies_on_the_fly);
12616 SET_RESULT_TO(result, local_result, l, r);
12617 }
12618 }
12619 break;
12620
12621 case DW_TAG_variable:
12622 case DW_TAG_member:
12623 if (compare_as_decl_dies(l, r))
12624 {
12625 // Compare the offsets of the data members
12626 if (l_tag == DW_TAG_member)
12627 {
12628 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12629 die_member_offset(rdr, l, l_offset_in_bits);
12630 die_member_offset(rdr, r, r_offset_in_bits);
12631 if (l_offset_in_bits != r_offset_in_bits)
12632 SET_RESULT_TO_FALSE(result, l, r);
12633 }
12634 if (result)
12635 {
12636 // Compare the types of the data members or variables.
12637 Dwarf_Die l_type, r_type;
12638 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12639 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12640 comparison_result local_result =
12641 compare_dies(rdr, &l_type, &r_type,
12642 aggregates_being_compared,
12643 update_canonical_dies_on_the_fly);
12644 SET_RESULT_TO(result, local_result, l, r);
12645 }
12646 }
12647 else
12648 SET_RESULT_TO_FALSE(result, l, r);
12649 break;
12650
12651 case DW_TAG_inheritance:
12652 {
12653 Dwarf_Die l_type, r_type;
12654 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12655 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12656 result = compare_dies(rdr, &l_type, &r_type,
12657 aggregates_being_compared,
12658 update_canonical_dies_on_the_fly);
12659 if (!result)
12660 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12661
12662 uint64_t l_a = 0, r_a = 0;
12663 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
12664 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
12665 if (l_a != r_a)
12666 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12667
12668 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
12669 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
12670 if (l_a != r_a)
12671 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12672
12673 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
12674 die_member_offset(rdr, l, l_offset_in_bits);
12675 die_member_offset(rdr, r, r_offset_in_bits);
12676 if (l_offset_in_bits != r_offset_in_bits)
12677 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12678 }
12679 break;
12680
12681 case DW_TAG_ptr_to_member_type:
12682 {
12683 bool comp_result = false;
12684 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
12685 if (!comp_result)
12686 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
12687
12688 Dwarf_Die l_type, r_type;
12689 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
12690 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
12691 result = compare_dies(rdr, &l_type, &r_type,
12692 aggregates_being_compared,
12693 update_canonical_dies_on_the_fly);
12694 if (!result)
12695 ABG_RETURN(result);
12696
12697 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
12698 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
12699 result = compare_dies(rdr, &l_type, &r_type,
12700 aggregates_being_compared,
12701 update_canonical_dies_on_the_fly);
12702 if (!result)
12703 ABG_RETURN(result);
12704 }
12705 break;
12706
12707 case DW_TAG_enumerator:
12708 case DW_TAG_packed_type:
12709 case DW_TAG_set_type:
12710 case DW_TAG_file_type:
12711 case DW_TAG_thrown_type:
12712 case DW_TAG_interface_type:
12713 case DW_TAG_shared_type:
12714 case DW_TAG_compile_unit:
12715 case DW_TAG_namespace:
12716 case DW_TAG_module:
12717 case DW_TAG_constant:
12718 case DW_TAG_partial_unit:
12719 case DW_TAG_imported_unit:
12720 case DW_TAG_dwarf_procedure:
12721 case DW_TAG_imported_declaration:
12722 case DW_TAG_entry_point:
12723 case DW_TAG_label:
12724 case DW_TAG_lexical_block:
12725 case DW_TAG_unspecified_parameters:
12726 case DW_TAG_variant:
12727 case DW_TAG_common_block:
12728 case DW_TAG_common_inclusion:
12729 case DW_TAG_inlined_subroutine:
12730 case DW_TAG_with_stmt:
12731 case DW_TAG_access_declaration:
12732 case DW_TAG_catch_block:
12733 case DW_TAG_friend:
12734 case DW_TAG_namelist:
12735 case DW_TAG_namelist_item:
12736 case DW_TAG_template_type_parameter:
12737 case DW_TAG_template_value_parameter:
12738 case DW_TAG_try_block:
12739 case DW_TAG_variant_part:
12740 case DW_TAG_imported_module:
12741 case DW_TAG_condition:
12742 case DW_TAG_type_unit:
12743 case DW_TAG_template_alias:
12744 case DW_TAG_lo_user:
12745 case DW_TAG_MIPS_loop:
12746 case DW_TAG_format_label:
12747 case DW_TAG_function_template:
12748 case DW_TAG_class_template:
12749 case DW_TAG_GNU_BINCL:
12750 case DW_TAG_GNU_EINCL:
12751 case DW_TAG_GNU_template_template_param:
12752 case DW_TAG_GNU_template_parameter_pack:
12753 case DW_TAG_GNU_formal_parameter_pack:
12754 case DW_TAG_GNU_call_site:
12755 case DW_TAG_GNU_call_site_parameter:
12756 case DW_TAG_hi_user:
12757#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12758 if (rdr.debug_die_canonicalization_is_on_)
12760#endif
12762 break;
12763 }
12764
12765 ABG_RETURN(result);
12766}
12767
12768/// Compare two DIEs emitted by a C compiler.
12769///
12770/// @param rdr the DWARF reader used to load the DWARF information.
12771///
12772/// @param l the left-hand-side argument of this comparison operator.
12773///
12774/// @param r the righ-hand-side argument of this comparison operator.
12775///
12776/// @param update_canonical_dies_on_the_fly if yes, then this function
12777/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
12778/// comparing l and r. This helps in making so that sub-type DIEs of
12779/// 'l' and 'r' are compared structurally only once. This is how we
12780/// turn this exponential comparison problem into a problem that is a
12781/// closer to a linear one.
12782///
12783/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
12784static comparison_result
12785compare_dies(const reader& rdr,
12786 const Dwarf_Die *l,
12787 const Dwarf_Die *r,
12788 bool update_canonical_dies_on_the_fly)
12789{
12790 offset_pairs_stack_type aggregates_being_compared(rdr);
12791 return compare_dies(rdr, l, r, aggregates_being_compared,
12792 update_canonical_dies_on_the_fly);
12793}
12794
12795/// Compare two DIEs for the purpose of canonicalization.
12796///
12797/// This is a sub-routine of reader::get_canonical_die.
12798///
12799/// When DIE canonicalization debugging is on, this function performs
12800/// both structural and canonical comparison. It expects that both
12801/// comparison yield the same result.
12802///
12803/// @param rdr the DWARF reader.
12804///
12805/// @param l the left-hand-side comparison operand DIE.
12806///
12807/// @param r the right-hand-side comparison operand DIE.
12808///
12809/// @param update_canonical_dies_on_the_fly if true, then some
12810/// aggregate DIEs will see their canonical types propagated.
12811///
12812/// @return true iff @p l equals @p r.
12813static bool
12814compare_dies_during_canonicalization(reader& rdr,
12815 const Dwarf_Die *l,
12816 const Dwarf_Die *r,
12817 bool update_canonical_dies_on_the_fly)
12818{
12819#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
12820 if (rdr.debug_die_canonicalization_is_on_)
12821 {
12822 bool canonical_equality = false, structural_equality = false;
12823 rdr.use_canonical_die_comparison_ = false;
12824 structural_equality = compare_dies(rdr, l, r,
12825 /*update_canonical_dies_on_the_fly=*/false);
12826 rdr.use_canonical_die_comparison_ = true;
12827 canonical_equality = compare_dies(rdr, l, r,
12828 update_canonical_dies_on_the_fly);
12829 if (canonical_equality != structural_equality)
12830 {
12831 std::cerr << "structural & canonical equality different for DIEs: "
12832 << std::hex
12833 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
12834 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
12835 << std::dec
12836 << ", repr: '"
12837 << rdr.get_die_pretty_type_representation(l, 0)
12838 << "'"
12839 << std::endl;
12841 }
12842 return structural_equality;
12843 }
12844#endif
12845 return compare_dies(rdr, l, r,
12846 update_canonical_dies_on_the_fly);
12847}
12848
12849// ----------------------------------
12850// </die comparison engine>
12851// ---------------------------------
12852
12853/// Get the point where a DW_AT_import DIE is used to import a given
12854/// (unit) DIE, between two DIEs.
12855///
12856/// @param rdr the dwarf reader to consider.
12857///
12858/// @param partial_unit_offset the imported unit for which we want to
12859/// know the insertion point. This is usually a partial unit (with
12860/// tag DW_TAG_partial_unit) but it does not necessarily have to be
12861/// so.
12862///
12863/// @param first_die_offset the offset of the DIE from which this
12864/// function starts looking for the import point of
12865/// @partial_unit_offset. Note that this offset is excluded from the
12866/// set of potential solutions.
12867///
12868/// @param first_die_cu_offset the offset of the (compilation) unit
12869/// that @p first_die_cu_offset belongs to.
12870///
12871/// @param source where the DIE of first_die_cu_offset unit comes
12872/// from.
12873///
12874/// @param last_die_offset the offset of the last DIE of the up to
12875/// which this function looks for the import point of @p
12876/// partial_unit_offset. Note that this offset is excluded from the
12877/// set of potential solutions.
12878///
12879/// @param imported_point_offset. The resulting
12880/// imported_point_offset. Note that if the imported DIE @p
12881/// partial_unit_offset is not found between @p first_die_offset and
12882/// @p last_die_offset, this parameter is left untouched by this
12883/// function.
12884///
12885/// @return true iff an imported unit is found between @p
12886/// first_die_offset and @p last_die_offset.
12887static bool
12888find_import_unit_point_between_dies(const reader& rdr,
12889 size_t partial_unit_offset,
12890 Dwarf_Off first_die_offset,
12891 Dwarf_Off first_die_cu_offset,
12892 die_source source,
12893 size_t last_die_offset,
12894 size_t& imported_point_offset)
12895{
12896 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
12897 rdr.tu_die_imported_unit_points_map(source);
12898
12899 tu_die_imported_unit_points_map_type::const_iterator iter =
12900 tu_die_imported_unit_points_map.find(first_die_cu_offset);
12901
12902 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
12903
12904 const imported_unit_points_type& imported_unit_points = iter->second;
12905 if (imported_unit_points.empty())
12906 return false;
12907
12908 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
12909 imported_unit_points_type::const_iterator e = imported_unit_points.end();
12910
12911 find_lower_bound_in_imported_unit_points(imported_unit_points,
12912 first_die_offset,
12913 b);
12914
12915 if (last_die_offset != static_cast<size_t>(-1))
12916 find_lower_bound_in_imported_unit_points(imported_unit_points,
12917 last_die_offset,
12918 e);
12919
12920 if (e != imported_unit_points.end())
12921 {
12922 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12923 if (i->imported_unit_die_off == partial_unit_offset)
12924 {
12925 imported_point_offset = i->offset_of_import ;
12926 return true;
12927 }
12928
12929 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
12930 {
12931 if (find_import_unit_point_between_dies(rdr,
12932 partial_unit_offset,
12933 i->imported_unit_child_off,
12934 i->imported_unit_cu_off,
12935 i->imported_unit_die_source,
12936 /*(Dwarf_Off)*/-1,
12937 imported_point_offset))
12938 return true;
12939 }
12940 }
12941 else
12942 {
12943 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
12944 if (i->imported_unit_die_off == partial_unit_offset)
12945 {
12946 imported_point_offset = i->offset_of_import ;
12947 return true;
12948 }
12949
12950 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
12951 {
12952 if (find_import_unit_point_between_dies(rdr,
12953 partial_unit_offset,
12954 i->imported_unit_child_off,
12955 i->imported_unit_cu_off,
12956 i->imported_unit_die_source,
12957 /*(Dwarf_Off)*/-1,
12958 imported_point_offset))
12959 return true;
12960 }
12961 }
12962
12963 return false;
12964}
12965
12966/// In the current translation unit, get the last point where a
12967/// DW_AT_import DIE is used to import a given (unit) DIE, before a
12968/// given DIE is found. That given DIE is called the limit DIE.
12969///
12970/// Said otherwise, this function returns the last import point of a
12971/// unit, before a limit.
12972///
12973/// @param rdr the dwarf reader to consider.
12974///
12975/// @param partial_unit_offset the imported unit for which we want to
12976/// know the insertion point of. This is usually a partial unit (with
12977/// tag DW_TAG_partial_unit) but it does not necessarily have to be
12978/// so.
12979///
12980/// @param where_offset the offset of the limit DIE.
12981///
12982/// @param imported_point_offset. The resulting imported_point_offset.
12983/// Note that if the imported DIE @p partial_unit_offset is not found
12984/// before @p die_offset, this is set to the last @p
12985/// partial_unit_offset found under @p parent_die.
12986///
12987/// @return true iff an imported unit is found before @p die_offset.
12988/// Note that if an imported unit is found after @p die_offset then @p
12989/// imported_point_offset is set and the function return false.
12990static bool
12991find_import_unit_point_before_die(const reader& rdr,
12992 size_t partial_unit_offset,
12993 size_t where_offset,
12994 size_t& imported_point_offset)
12995{
12996 size_t import_point_offset = 0;
12997 Dwarf_Die first_die_of_tu;
12998
12999 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
13000 &first_die_of_tu) != 0)
13001 return false;
13002
13003 Dwarf_Die cu_die_memory;
13004 Dwarf_Die *cu_die;
13005
13006 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
13007 &cu_die_memory, 0, 0);
13008
13009 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
13010 dwarf_dieoffset(&first_die_of_tu),
13011 dwarf_dieoffset(cu_die),
13012 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
13013 where_offset,
13014 import_point_offset))
13015 {
13016 imported_point_offset = import_point_offset;
13017 return true;
13018 }
13019
13020 if (import_point_offset)
13021 {
13022 imported_point_offset = import_point_offset;
13023 return true;
13024 }
13025
13026 return false;
13027}
13028
13029/// Return the parent DIE for a given DIE.
13030///
13031/// Note that the function build_die_parent_map() must have been
13032/// called before this one can work. This function either succeeds or
13033/// aborts the current process.
13034///
13035/// @param rdr the DWARF reader to consider.
13036///
13037/// @param die the DIE for which we want the parent.
13038///
13039/// @param parent_die the output parameter set to the parent die of
13040/// @p die. Its memory must be allocated and handled by the caller.
13041///
13042/// @param where_offset the offset of the DIE where we are "logically"
13043/// positionned at, in the DIE tree. This is useful when @p die is
13044/// e.g, DW_TAG_partial_unit that can be included in several places in
13045/// the DIE tree.
13046///
13047/// @return true if the function could get a parent DIE, false
13048/// otherwise.
13049static bool
13050get_parent_die(const reader& rdr,
13051 const Dwarf_Die* die,
13052 Dwarf_Die& parent_die,
13053 size_t where_offset)
13054{
13055 ABG_ASSERT(rdr.dwarf_debug_info());
13056
13057 const die_source source = rdr.get_die_source(die);
13058
13059 const offset_offset_map_type& m = rdr.die_parent_map(source);
13060 offset_offset_map_type::const_iterator i =
13061 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
13062
13063 if (i == m.end())
13064 return false;
13065
13066 switch (source)
13067 {
13068 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
13069 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13070 i->second, &parent_die));
13071 break;
13072 case ALT_DEBUG_INFO_DIE_SOURCE:
13073 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
13074 i->second, &parent_die));
13075 break;
13076 case TYPE_UNIT_DIE_SOURCE:
13077 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13078 i->second, &parent_die));
13079 break;
13080 case NO_DEBUG_INFO_DIE_SOURCE:
13081 case NUMBER_OF_DIE_SOURCES:
13083 }
13084
13085 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
13086 {
13087 if (where_offset == 0)
13088 {
13089 parent_die = *rdr.cur_tu_die();
13090 return true;
13091 }
13092 size_t import_point_offset = 0;
13093 bool found =
13094 find_import_unit_point_before_die(rdr,
13095 dwarf_dieoffset(&parent_die),
13096 where_offset,
13097 import_point_offset);
13098 if (!found)
13099 // It looks like parent_die (which comes from the alternate
13100 // debug info file) hasn't been imported into this TU. So,
13101 // Let's assume its logical parent is the DIE of the current
13102 // TU.
13103 parent_die = *rdr.cur_tu_die();
13104 else
13105 {
13106 ABG_ASSERT(import_point_offset);
13107 Dwarf_Die import_point_die;
13108 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
13109 import_point_offset,
13110 &import_point_die));
13111 return get_parent_die(rdr, &import_point_die,
13112 parent_die, where_offset);
13113 }
13114 }
13115
13116 return true;
13117}
13118
13119/// Get the DIE representing the scope of a given DIE.
13120///
13121/// Please note that when the DIE we are looking at has a
13122/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
13123/// DIE is the parent DIE of the DIE referred to by that attribute.
13124/// In other words, this function returns the scope of the origin DIE
13125/// of the current DIE.
13126///
13127/// So, the scope DIE can be different from the parent DIE of a given
13128/// DIE.
13129///
13130/// Also note that if the current translation unit is from C, then
13131/// this returns the global scope.
13132///
13133/// @param rdr the DWARF reader to use.
13134///
13135/// @param dye the DIE to consider.
13136///
13137/// @param where_offset where we are logically at in the DIE stream.
13138///
13139/// @param scope_die out parameter. This is set to the resulting
13140/// scope DIE iff the function returns true.
13141///
13142/// @return true iff the scope was found and returned in the @p
13143/// scope_die parameter.
13144static bool
13145get_scope_die(const reader& rdr,
13146 const Dwarf_Die* dye,
13147 size_t where_offset,
13148 Dwarf_Die& scope_die)
13149{
13150 Dwarf_Die origin_die_mem;
13151 Dwarf_Die *die = &origin_die_mem;
13152 if (!die_origin_die(dye, origin_die_mem))
13153 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13154
13155 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13156 get_die_language(die, die_lang);
13157 if (is_c_language(die_lang)
13158 || rdr.die_parent_map(rdr.get_die_source(die)).empty())
13159 {
13160 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
13161 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
13162 }
13163
13164 if (!get_parent_die(rdr, die, scope_die, where_offset))
13165 return false;
13166
13167 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
13168 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
13169 || dwarf_tag(&scope_die) == DW_TAG_array_type)
13170 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
13171
13172 return true;
13173}
13174
13175/// Return the abigail IR node representing the scope of a given DIE.
13176///
13177/// Note that it is the logical scope that is returned. That is, if
13178/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
13179/// attribute, it's the scope of the referred-to DIE (via these
13180/// attributes) that is returned. In other words, its the scope of
13181/// the origin DIE that is returned.
13182///
13183/// Also note that if the current translation unit is from C, then
13184/// this returns the global scope.
13185///
13186/// @param rdr the dwarf reader to use.
13187///
13188/// @param dye the DIE to get the scope for.
13189///
13190/// @param called_from_public_decl is true if this function has been
13191/// initially called within the context of a public decl.
13192///
13193/// @param where_offset the offset of the DIE where we are "logically"
13194/// positionned at, in the DIE tree. This is useful when @p die is
13195/// e.g, DW_TAG_partial_unit that can be included in several places in
13196/// the DIE tree.
13197///
13198/// @return the resulting scope, or nil if could not be computed.
13199static scope_decl_sptr
13200get_scope_for_die(reader& rdr,
13201 Dwarf_Die* dye,
13202 bool called_for_public_decl,
13203 size_t where_offset)
13204{
13205 Dwarf_Die origin_die_mem;
13206 Dwarf_Die *die = &origin_die_mem;
13207
13208 if (!die_origin_die(dye, origin_die_mem))
13209 // There was no origin DIE found, so let's make the "die" pointer
13210 // above point to the content of the input "dye".
13211 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
13212
13213 const die_source source_of_die = rdr.get_die_source(die);
13214
13215 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
13216 get_die_language(die, die_lang);
13217 if (is_c_language(die_lang)
13218 || rdr.die_parent_map(source_of_die).empty())
13219 {
13220 // In units for the C languages all decls belong to the global
13221 // namespace. This is generally the case if Libabigail
13222 // determined that no DIE -> parent map was needed.
13223 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
13224 return rdr.global_scope();
13225 }
13226
13227 Dwarf_Die parent_die;
13228
13229 if (!get_parent_die(rdr, die, parent_die, where_offset))
13230 return rdr.nil_scope();
13231
13232 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
13233 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
13234 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13235 {
13236 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
13237 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
13238 {
13239 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
13240 || source_of_die == TYPE_UNIT_DIE_SOURCE);
13241 return rdr.cur_transl_unit()->get_global_scope();
13242 }
13243
13244 // For top level DIEs like DW_TAG_compile_unit, we just want to
13245 // return the global scope for the corresponding translation
13246 // unit. This must have been set by
13247 // build_translation_unit_and_add_to_ir if we already started to
13248 // build the translation unit of parent_die. Otherwise, just
13249 // return the global scope of the current translation unit.
13250 die_tu_map_type::const_iterator i =
13251 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
13252 if (i != rdr.die_tu_map().end())
13253 return i->second->get_global_scope();
13254 return rdr.cur_transl_unit()->get_global_scope();
13255 }
13256
13259 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
13260 || dwarf_tag(&parent_die) == DW_TAG_array_type
13261 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
13262 // this is an entity defined in a scope that is a function.
13263 // Normally, I would say that this should be dropped. But I have
13264 // seen a case where a typedef DIE needed by a function parameter
13265 // was defined right before the parameter, under the scope of the
13266 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
13267 // the function parm too. So for that case, let's say that the
13268 // scope is the scope of the function itself. Note that this is
13269 // an error of the DWARF emitter. We should never see this DIE in
13270 // this context.
13271 {
13272 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
13273 called_for_public_decl,
13274 where_offset);
13275 if (is_anonymous_type_die(die))
13276 // For anonymous type that have nothing to do in a function or
13277 // array type context, let's put it in the containing
13278 // namespace. That is, do not let it be in a containing class
13279 // or union where it has nothing to do.
13280 while (is_class_or_union_type(s))
13281 {
13282 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
13283 return rdr.nil_scope();
13284 s = get_scope_for_die(rdr, &parent_die,
13285 called_for_public_decl,
13286 where_offset);
13287 }
13288 return s;
13289 }
13290 else
13291 d = build_ir_node_from_die(rdr, &parent_die,
13292 called_for_public_decl,
13293 where_offset);
13294 s = dynamic_pointer_cast<scope_decl>(d);
13295 if (!s)
13296 // this is an entity defined in someting that is not a scope.
13297 // Let's drop it.
13298 return rdr.nil_scope();
13299
13300 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
13301 if (cl && cl->get_is_declaration_only())
13302 {
13303 scope_decl_sptr scop =
13304 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
13305 if (scop)
13306 s = scop;
13307 else
13308 s = cl;
13309 }
13310 return s;
13311}
13312
13313/// Convert a DWARF constant representing the value of the
13314/// DW_AT_language property into the translation_unit::language
13315/// enumerator.
13316///
13317/// @param l the DWARF constant to convert.
13318///
13319/// @return the resulting translation_unit::language enumerator.
13321dwarf_language_to_tu_language(size_t l)
13322{
13323 switch (l)
13324 {
13325 case DW_LANG_C89:
13326 return translation_unit::LANG_C89;
13327 case DW_LANG_C99:
13328 return translation_unit::LANG_C99;
13329#ifdef HAVE_DW_LANG_C11_enumerator
13330 case DW_LANG_C11:
13331 return translation_unit::LANG_C11;
13332#endif
13333#ifdef HAVE_DW_LANG_C17
13334 case DW_LANG_C17:
13335 return translation_unit::LANG_C17;
13336#endif
13337#ifdef HAVE_DW_LANG_C23
13338 case DW_LANG_C23:
13339 return translation_unit::LANG_C23;
13340#endif
13341 case DW_LANG_C:
13342 return translation_unit::LANG_C;
13343#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
13344 case DW_LANG_C_plus_plus_03:
13345 return translation_unit::LANG_C_plus_plus_03;
13346#endif
13347
13348#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
13349 case DW_LANG_C_plus_plus_11:
13350 return translation_unit::LANG_C_plus_plus_11;
13351#endif
13352
13353#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
13354 case DW_LANG_C_plus_plus_14:
13355 return translation_unit::LANG_C_plus_plus_14;
13356#endif
13357#ifdef HAVE_DW_LANG_C_plus_plus_17
13358 case DW_LANG_C_plus_plus_17:
13359 return translation_unit::LANG_C_plus_plus_17;
13360#endif
13361
13362#ifdef HAVE_DW_LANG_C_plus_plus_20
13363 case DW_LANG_C_plus_plus_20:
13364 return translation_unit::LANG_C_plus_plus_20;
13365#endif
13366#ifdef HAVE_DW_LANG_C_plus_plus_23
13367 case DW_LANG_C_plus_plus_23:
13368 return translation_unit::LANG_C_plus_plus_23;
13369#endif
13370 case DW_LANG_C_plus_plus:
13371 return translation_unit::LANG_C_plus_plus;
13372#ifdef HAVE_DW_LANG_D_enumerator
13373 case DW_LANG_D:
13374 return translation_unit::LANG_D;
13375#endif
13376#ifdef HAVE_DW_LANG_OCaml_enumerator
13377 case DW_LANG_OCaml:
13378 return translation_unit::LANG_OCaml;
13379#endif
13380#ifdef HAVE_DW_LANG_Go_enumerator
13381 case DW_LANG_Go:
13382 return translation_unit::LANG_Go;
13383#endif
13384#ifdef HAVE_DW_LANG_Rust_enumerator
13385 case DW_LANG_Rust:
13386 return translation_unit::LANG_Rust;
13387#endif
13388#ifdef HAVE_DW_LANG_Zig
13389 case DW_LANG_Zig:
13390 return translation_unit::LANG_Zig;
13391#endif
13392#ifdef HAVE_DW_LANG_Metal
13393 case DW_LANG_Metal:
13394 return translation_unit::LANG_Metal;
13395#endif
13396 case DW_LANG_Ada83:
13397 return translation_unit::LANG_Ada83;
13398 case DW_LANG_Ada95:
13399 return translation_unit::LANG_Ada95;
13400#ifdef HAVE_DW_LANG_Ada2005
13401 case DW_LANG_Ada2005:
13402 return translation_unit::LANG_Ada2005;
13403#endif
13404
13405#ifdef HAVE_DW_LANG_Ada2012
13406 case DW_LANG_Ada2012:
13407 return translation_unit::LANG_Ada2012;
13408#endif
13409 case DW_LANG_Cobol74:
13410 return translation_unit::LANG_Cobol74;
13411 case DW_LANG_Cobol85:
13412 return translation_unit::LANG_Cobol85;
13413 case DW_LANG_Fortran77:
13414 return translation_unit::LANG_Fortran77;
13415 case DW_LANG_Fortran90:
13416 return translation_unit::LANG_Fortran90;
13417 case DW_LANG_Fortran95:
13418 return translation_unit::LANG_Fortran95;
13419#ifdef HAVE_DW_LANG_Fortran18
13420 case DW_LANG_Fortran18:
13421 return translation_unit::LANG_Fortran18;
13422#endif
13423#ifdef HAVE_DW_LANG_Fortran23
13424 case DW_LANG_Fortran23:
13425 return translation_unit::LANG_Fortran23;
13426#endif
13427 case DW_LANG_Pascal83:
13428 return translation_unit::LANG_Pascal83;
13429 case DW_LANG_Modula2:
13430 return translation_unit::LANG_Modula2;
13431 case DW_LANG_Java:
13432 return translation_unit::LANG_Java;
13433#ifdef HAVE_DW_LANG_Kotlin
13434 case DW_LANG_Kotlin:
13435 return translation_unit::LANG_Kotlin;
13436#endif
13437 case DW_LANG_PLI:
13438 return translation_unit::LANG_PLI;
13439 case DW_LANG_ObjC:
13440 return translation_unit::LANG_ObjC;
13441 case DW_LANG_ObjC_plus_plus:
13442 return translation_unit::LANG_ObjC_plus_plus;
13443
13444#ifdef HAVE_DW_LANG_UPC_enumerator
13445 case DW_LANG_UPC:
13446 return translation_unit::LANG_UPC;
13447#endif
13448#ifdef HAVE_DW_LANG_Python_enumerator
13449 case DW_LANG_Python:
13450 return translation_unit::LANG_Python;
13451#endif
13452#ifdef HAVE_DW_LANG_Ruby
13453 case DW_LANG_Ruby:
13454 return translation_unit::LANG_Ruby;
13455#endif
13456#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
13457 case DW_LANG_Mips_Assembler:
13458 return translation_unit::LANG_Mips_Assembler;
13459#endif
13460#ifdef HAVE_DW_LANG_Assembly
13461 case DW_LANG_Assembly:
13462 return translation_unit::LANG_Assembly;
13463#endif
13464#ifdef HAVE_DW_LANG_Crystal
13465 case DW_LANG_Crystal:
13466 return translation_unit::LANG_Crystal;
13467#endif
13468#ifdef HAVE_DW_LANG_HIP
13469 case DW_LANG_HIP:
13470 return translation_unit::LANG_HIP;
13471#endif
13472#ifdef HAVE_DW_LANG_C_sharp
13473 case DW_LANG_C_sharp:
13474 return translation_unit::LANG_C_sharp;
13475#endif
13476#ifdef HAVE_DW_LANG_Mojo
13477 case DW_LANG_Mojo:
13478 return translation_unit::LANG_Mojo;
13479#endif
13480#ifdef HAVE_DW_LANG_GLSL
13481 case DW_LANG_GLSL:
13482 return translation_unit::LANG_GLSL;
13483#endif
13484#ifdef HAVE_DW_LANG_GLSL_ES
13485 case DW_LANG_GLSL_ES:
13486 return translation_unit::LANG_GLSL_ES;
13487#endif
13488#ifdef HAVE_DW_LANG_HLSL
13489 case DW_LANG_HLSL:
13490 return translation_unit::LANG_HLSL;
13491#endif
13492#ifdef HAVE_DW_LANG_OpenCL_CPP
13493 case DW_LANG_OpenCL_CPP:
13494 return translation_unit::LANG_OpenCL_CPP;
13495#endif
13496#ifdef HAVE_DW_LANG_CPP_for_OpenCL
13497 case DW_LANG_CPP_for_OpenCL:
13498 return translation_unit::LANG_CPP_for_OpenCL;
13499#endif
13500#ifdef HAVE_DW_LANG_SYCL
13501 case DW_LANG_SYCL:
13502 return translation_unit::LANG_SYCL;
13503#endif
13504#ifdef HAVE_DW_LANG_Odin
13505 case DW_LANG_Odin:
13506 return translation_unit::LANG_Odin;
13507#endif
13508#ifdef HAVE_DW_LANG_P4
13509 case DW_LANG_P4:
13510 return translation_unit::LANG_P4;
13511#endif
13512#ifdef HAVE_DW_LANG_Move
13513 case DW_LANG_Move:
13514 return translation_unit::LANG_Move;
13515#endif
13516#ifdef HAVE_DW_LANG_Hylo
13517 case DW_LANG_Hylo:
13518 return translation_unit::LANG_Hylo;
13519#endif
13520
13521 default:
13522 return translation_unit::LANG_UNKNOWN;
13523 }
13524}
13525
13526/// Get the default array lower bound value as defined by the DWARF
13527/// specification, version 4, depending on the language of the
13528/// translation unit.
13529///
13530/// @param l the language of the translation unit.
13531///
13532/// @return the default array lower bound value.
13533static uint64_t
13534get_default_array_lower_bound(translation_unit::language l)
13535{
13536 int value = 0;
13537 switch (l)
13538 {
13539 case translation_unit::LANG_UNKNOWN:
13540 case translation_unit::LANG_C89:
13541 case translation_unit::LANG_C99:
13542 case translation_unit::LANG_C11:
13543 case translation_unit::LANG_C17:
13544 case translation_unit::LANG_C23:
13545 case translation_unit::LANG_C:
13546 case translation_unit::LANG_C_plus_plus_03:
13547 case translation_unit::LANG_C_plus_plus_11:
13548 case translation_unit::LANG_C_plus_plus_14:
13549 case translation_unit::LANG_C_plus_plus_17:
13550 case translation_unit::LANG_C_plus_plus_20:
13551 case translation_unit::LANG_C_plus_plus_23:
13552 case translation_unit::LANG_C_plus_plus:
13553 case translation_unit::LANG_OCaml:
13554 case translation_unit::LANG_ObjC:
13555 case translation_unit::LANG_ObjC_plus_plus:
13556 case translation_unit::LANG_D:
13557 case translation_unit::LANG_Rust:
13558 case translation_unit::LANG_Go:
13559 case translation_unit::LANG_Zig:
13560 case translation_unit::LANG_Metal:
13561 case translation_unit::LANG_Java:
13562 case translation_unit::LANG_Kotlin:
13563 case translation_unit::LANG_Python:
13564 case translation_unit::LANG_Ruby:
13565 case translation_unit::LANG_UPC:
13566 case translation_unit::LANG_Mips_Assembler:
13567 case translation_unit::LANG_Assembly:
13568 case translation_unit::LANG_Crystal:
13569 case translation_unit::LANG_HIP:
13570 case translation_unit::LANG_C_sharp:
13571 case translation_unit::LANG_Mojo:
13572 case translation_unit::LANG_GLSL:
13573 case translation_unit::LANG_GLSL_ES:
13574 case translation_unit::LANG_HLSL:
13575 case translation_unit::LANG_Odin:
13576 case translation_unit::LANG_P4:
13577 case translation_unit::LANG_OpenCL_CPP:
13578 case translation_unit::LANG_CPP_for_OpenCL:
13579 case translation_unit::LANG_SYCL:
13580 case translation_unit::LANG_Move:
13581 case translation_unit::LANG_Hylo:
13582 value = 0;
13583 break;
13584 case translation_unit::LANG_Cobol74:
13585 case translation_unit::LANG_Cobol85:
13586 case translation_unit::LANG_Fortran77:
13587 case translation_unit::LANG_Fortran90:
13588 case translation_unit::LANG_Fortran95:
13589 case translation_unit::LANG_Fortran18:
13590 case translation_unit::LANG_Fortran23:
13591 case translation_unit::LANG_Ada83:
13592 case translation_unit::LANG_Ada95:
13593 case translation_unit::LANG_Ada2005:
13594 case translation_unit::LANG_Ada2012:
13595 case translation_unit::LANG_Pascal83:
13596 case translation_unit::LANG_Modula2:
13597 case translation_unit::LANG_PLI:
13598 value = 1;
13599 break;
13600 }
13601
13602 return value;
13603}
13604
13605/// For a given offset, find the lower bound of a sorted vector of
13606/// imported unit point offset.
13607///
13608/// The lower bound is the smallest point (the point with the smallest
13609/// offset) which is the greater than a given offset.
13610///
13611/// @param imported_unit_points_type the sorted vector of imported
13612/// unit points.
13613///
13614/// @param val the offset to consider when looking for the lower
13615/// bound.
13616///
13617/// @param r an iterator to the lower bound found. This parameter is
13618/// set iff the function returns true.
13619///
13620/// @return true iff the lower bound has been found.
13621static bool
13622find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
13623 Dwarf_Off val,
13624 imported_unit_points_type::const_iterator& r)
13625{
13626 imported_unit_point v(val);
13627 imported_unit_points_type::const_iterator result =
13628 std::lower_bound(p.begin(), p.end(), v);
13629
13630 bool is_ok = result != p.end();
13631
13632 if (is_ok)
13633 r = result;
13634
13635 return is_ok;
13636}
13637
13638/// Given a DW_TAG_compile_unit, build and return the corresponding
13639/// abigail::translation_unit ir node. Note that this function
13640/// recursively reads the children dies of the current DIE and
13641/// populates the resulting translation unit.
13642///
13643/// @param rdr the DWARF reader to use.
13644///
13645/// @param die the DW_TAG_compile_unit DIE to consider.
13646///
13647/// @param address_size the size of the addresses expressed in this
13648/// translation unit in general.
13649///
13650/// @return a pointer to the resulting translation_unit.
13652build_translation_unit_and_add_to_ir(reader& rdr,
13653 Dwarf_Die* die,
13654 char address_size)
13655{
13656 translation_unit_sptr result;
13657
13658 if (!die)
13659 return result;
13660 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
13661
13662 // Clear the part of the context that is dependent on the translation
13663 // unit we are reading.
13664 rdr.clear_per_translation_unit_data();
13665
13666 rdr.cur_tu_die(die);
13667
13668 string path = die_string_attribute(die, DW_AT_name);
13669 if (path == "<artificial>")
13670 {
13671 // This is a file artificially generated by the compiler, so its
13672 // name is '<artificial>'. As we want all different translation
13673 // units to have unique path names, let's suffix this path name
13674 // with its die offset.
13675 std::ostringstream o;
13676 o << path << "-" << std::hex << dwarf_dieoffset(die);
13677 path = o.str();
13678 }
13679 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
13680
13681 // See if the same translation unit exits already in the current
13682 // corpus. Sometimes, the same translation unit can be present
13683 // several times in the same debug info. The content of the
13684 // different instances of the translation unit are different. So to
13685 // represent that, we are going to re-use the same translation
13686 // unit. That is, it's going to be the union of all the translation
13687 // units of the same path.
13688 {
13689 const string& abs_path =
13690 compilation_dir.empty() ? path : compilation_dir + "/" + path;
13691 result = rdr.corpus()->find_translation_unit(abs_path);
13692 }
13693
13694 if (!result)
13695 {
13696 result.reset(new translation_unit(rdr.env(),
13697 path,
13698 address_size));
13699 result->set_compilation_dir_path(compilation_dir);
13700 rdr.corpus()->add(result);
13701 uint64_t l = 0;
13702 die_unsigned_constant_attribute(die, DW_AT_language, l);
13703 result->set_language(dwarf_language_to_tu_language(l));
13704 }
13705
13706 rdr.cur_transl_unit(result);
13707 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
13708
13709 Dwarf_Die child;
13710 if (dwarf_child(die, &child) != 0)
13711 return result;
13712
13713 result->set_is_constructed(false);
13714 int tag = dwarf_tag(&child);
13715 do
13716 if (rdr.load_undefined_interfaces()
13717 && (rdr.is_decl_die_with_undefined_symbol(&child)
13718 || tag == DW_TAG_class_type // Top-level classes might
13719 // have undefined interfaces
13720 // that need to be
13721 // represented, so let's
13722 // analyze them as well.
13723 || ((tag == DW_TAG_union_type || tag == DW_TAG_structure_type)
13724 && die_is_in_cplus_plus(&child))))
13725 {
13726 // Analyze undefined functions & variables for the purpose of
13727 // analyzing compatibility matters.
13728 build_ir_node_from_die(rdr, &child,
13729 // Pretend the DIE is publicly defined
13730 // so that types that are reachable
13731 // from it get analyzed as well.
13732 /*die_is_public=*/true,
13733 dwarf_dieoffset(&child));
13734 }
13735 else if (!rdr.env().analyze_exported_interfaces_only()
13736 || rdr.is_decl_die_with_exported_symbol(&child))
13737 {
13738 // Analyze all the DIEs we encounter unless we are asked to only
13739 // analyze exported interfaces and the types reachables from them.
13740 build_ir_node_from_die(rdr, &child,
13741 die_is_public_decl(&child),
13742 dwarf_dieoffset(&child));
13743 }
13744 while (dwarf_siblingof(&child, &child) == 0);
13745
13746 if (!rdr.var_decls_to_re_add_to_tree().empty())
13747 for (list<var_decl_sptr>::const_iterator v =
13748 rdr.var_decls_to_re_add_to_tree().begin();
13749 v != rdr.var_decls_to_re_add_to_tree().end();
13750 ++v)
13751 {
13752 if (is_member_decl(*v))
13753 continue;
13754
13755 ABG_ASSERT((*v)->get_scope());
13756 string demangled_name =
13757 demangle_cplus_mangled_name((*v)->get_linkage_name());
13758 if (!demangled_name.empty())
13759 {
13760 std::list<string> fqn_comps;
13761 fqn_to_components(demangled_name, fqn_comps);
13762 string mem_name = fqn_comps.back();
13763 fqn_comps.pop_back();
13764 class_decl_sptr class_type;
13765 string ty_name;
13766 if (!fqn_comps.empty())
13767 {
13768 ty_name = components_to_type_name(fqn_comps);
13769 class_type =
13770 lookup_class_type(ty_name, *rdr.cur_transl_unit());
13771 }
13772 if (class_type)
13773 {
13774 // So we are seeing a member variable for which there
13775 // is a global variable definition DIE not having a
13776 // reference attribute pointing back to the member
13777 // variable declaration DIE. Thus remove the global
13778 // variable definition from its current non-class
13779 // scope ...
13780 decl_base_sptr d;
13781 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
13782 // This is the data member with the same name in cl.
13783 // We just need to flag it as static.
13784 ;
13785 else
13786 {
13787 // In this case there is no data member with the
13788 // same name in cl already. Let's add it there then
13789 // ...
13791 d = add_decl_to_scope(*v, class_type);
13792 }
13793
13794 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
13795 // Let's flag the data member as static.
13796 set_member_is_static(d, true);
13797 }
13798 }
13799 }
13800 rdr.var_decls_to_re_add_to_tree().clear();
13801
13802 result->set_is_constructed(true);
13803
13804 return result;
13805}
13806
13807/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
13808/// DW_TAG_module (for fortran) DIE.
13809///
13810/// Note that this function connects the DW_TAG_namespace to the IR
13811/// being currently created, reads the children of the DIE and
13812/// connects them to the IR as well.
13813///
13814/// @param rdr the DWARF reader to use.
13815///
13816/// @param die the DIE to read from. Must be either DW_TAG_namespace
13817/// or DW_TAG_module.
13818///
13819/// @param where_offset the offset of the DIE where we are "logically"
13820/// positionned at, in the DIE tree. This is useful when @p die is
13821/// e.g, DW_TAG_partial_unit that can be included in several places in
13822/// the DIE tree.
13823///
13824/// @return the resulting @ref abigail::namespace_decl or NULL if it
13825/// couldn't be created.
13827build_namespace_decl_and_add_to_ir(reader& rdr,
13828 Dwarf_Die* die,
13829 size_t where_offset)
13830{
13831 namespace_decl_sptr result;
13832
13833 if (!die)
13834 return result;
13835
13836 unsigned tag = dwarf_tag(die);
13837 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
13838 return result;
13839
13840 scope_decl_sptr scope = get_scope_for_die(rdr, die,
13841 /*called_for_public_decl=*/false,
13842 where_offset);
13843
13844 string name, linkage_name;
13845 location loc;
13846 die_loc_and_name(rdr, die, loc, name, linkage_name);
13847
13848 result.reset(new namespace_decl(rdr.env(), name, loc));
13849 add_decl_to_scope(result, scope.get());
13850 rdr.associate_die_to_decl(die, result, where_offset);
13851
13852 Dwarf_Die child;
13853 if (dwarf_child(die, &child) != 0)
13854 return result;
13855
13856 rdr.scope_stack().push(result.get());
13857 do
13858 build_ir_node_from_die(rdr, &child,
13859 // If this namespace DIE is private
13860 // (anonymous) then all its content is
13861 // considered private. Otherwise, its
13862 // public decls are considered public.
13863 /*called_from_public_decl=*/
13864 die_is_public_decl(die) && die_is_public_decl(&child),
13865 where_offset);
13866 while (dwarf_siblingof(&child, &child) == 0);
13867 rdr.scope_stack().pop();
13868
13869 return result;
13870}
13871
13872/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
13873///
13874/// @param rdr the DWARF reader to use.
13875///
13876/// @param die the DW_TAG_base_type to consider.
13877///
13878/// @param where_offset where we are logically at in the DIE stream.
13879///
13880/// @return the resulting decl_base_sptr.
13881static type_decl_sptr
13882build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
13883{
13884 type_decl_sptr result;
13885
13886 if (!die)
13887 return result;
13888 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
13889
13890 uint64_t byte_size = 0, bit_size = 0;
13891 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
13892 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
13893 return result;
13894
13895 if (bit_size == 0 && byte_size != 0)
13896 // Update the bit size.
13897 bit_size = byte_size * 8;
13898
13899 string type_name, linkage_name;
13900 location loc;
13901 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
13902
13903 if (byte_size == 0)
13904 {
13905 // The size of the type is zero, that must mean that we are
13906 // looking at the definition of the void type.
13907 if (type_name == "void")
13908 result = is_type_decl(build_ir_node_for_void_type(rdr));
13909 else
13910 // A type of size zero that is not void? Hmmh, I am not sure
13911 // what that means. Return nil for now.
13912 return result;
13913 }
13914
13915 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13916 {
13917 string normalized_type_name = type_name;
13918 real_type real_type;
13919 if (parse_real_type(type_name, real_type))
13920 normalized_type_name = real_type.to_string();
13921 result = lookup_basic_type(normalized_type_name, *corp);
13922 }
13923
13924 if (!result)
13925 if (corpus_sptr corp = rdr.corpus())
13926 result = lookup_basic_type(type_name, *corp);
13927 if (!result)
13928 result.reset(new type_decl(rdr.env(), type_name, bit_size,
13929 /*alignment=*/0, loc, linkage_name));
13930 rdr.associate_die_to_type(die, result, where_offset);
13931 return result;
13932}
13933
13934/// Construct the type that is to be used as the underlying type of an
13935/// enum.
13936///
13937/// @param rdr the DWARF reader to use.
13938///
13939/// @param enum_name the name of the enum that this type is going to
13940/// be the underlying type of.
13941///
13942/// @param enum_size the size of the enum.
13943///
13944/// @param is_anonymous whether the underlying type is anonymous or
13945/// not. By default, this should be set to true as before c++11 (and
13946/// in C), it's almost the case.
13947static type_decl_sptr
13948build_enum_underlying_type(reader& rdr,
13949 string enum_name,
13950 uint64_t enum_size,
13951 bool is_anonymous = true)
13952{
13953 string underlying_type_name =
13954 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
13955 enum_size);
13956
13957 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
13958 enum_size, enum_size, location()));
13959 result->set_is_anonymous(is_anonymous);
13960 result->set_is_artificial(true);
13961 translation_unit_sptr tu = rdr.cur_transl_unit();
13962 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
13963 result = dynamic_pointer_cast<type_decl>(d);
13964 ABG_ASSERT(result);
13965 maybe_canonicalize_type(result, rdr);
13966 return result;
13967}
13968
13969/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
13970///
13971/// @param rdr the DWARF reader to use.
13972///
13973/// @param die the DIE to read from.
13974///
13975/// @param scope the scope of the final enum. Note that this function
13976/// does *NOT* add the built type to this scope. The scope is just so
13977/// that the function knows how to name anonymous enums.
13978///
13979/// @param is_declaration_only is true if the DIE denoted by @p die is
13980/// a declaration-only DIE.
13981///
13982/// @return the built enum_type_decl or NULL if it could not be built.
13984build_enum_type(reader& rdr,
13985 Dwarf_Die* die,
13986 scope_decl* scope,
13987 size_t where_offset,
13988 bool is_declaration_only)
13989{
13990 enum_type_decl_sptr result;
13991 if (!die)
13992 return result;
13993
13994 unsigned tag = dwarf_tag(die);
13995 if (tag != DW_TAG_enumeration_type)
13996 return result;
13997
13998 string name, linkage_name;
13999 location loc;
14000 die_loc_and_name(rdr, die, loc, name, linkage_name);
14001
14002 bool is_anonymous = false;
14003 // If the enum is anonymous, let's give it a name.
14004 if (name.empty())
14005 {
14006 name = get_internal_anonymous_die_prefix_name(die);
14007 ABG_ASSERT(!name.empty());
14008 // But we remember that the type is anonymous.
14009 is_anonymous = true;
14010
14011 scope_decl* sc = scope ? scope : rdr.global_scope().get();
14012 if (size_t s = sc->get_num_anonymous_member_enums())
14013 name = build_internal_anonymous_die_name(name, s);
14014 }
14015
14016 bool use_odr = rdr.odr_is_relevant(die);
14017 // If the type has location, then associate it to its
14018 // representation. This way, all occurences of types with the same
14019 // representation (name) and location can be later detected as being
14020 // for the same type.
14021
14022 if (!is_anonymous)
14023 {
14024 if (use_odr)
14025 {
14026 if (enum_type_decl_sptr pre_existing_enum =
14027 is_enum_type(rdr.lookup_artifact_from_die(die)))
14028 result = pre_existing_enum;
14029 }
14030 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14031 {
14032 if (loc)
14033 result = lookup_enum_type_per_location(loc.expand(), *corp);
14034 }
14035 else if (loc)
14036 {
14037 if (enum_type_decl_sptr pre_existing_enum =
14038 is_enum_type(rdr.lookup_artifact_from_die(die)))
14039 if (pre_existing_enum->get_location() == loc)
14040 result = pre_existing_enum;
14041 }
14042
14043 if (result)
14044 {
14045 rdr.associate_die_to_type(die, result, where_offset);
14046 return result;
14047 }
14048 }
14049 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
14050 // we can look them up?
14051
14052 uint64_t size = 0;
14053 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14054 size *= 8;
14055 bool is_artificial = die_is_artificial(die);
14056
14057 // for now we consider that underlying types of enums are all anonymous
14058 bool enum_underlying_type_is_anonymous= true;
14059
14061 Dwarf_Die child;
14062 if (dwarf_child(die, &child) == 0)
14063 {
14064 do
14065 {
14066 if (dwarf_tag(&child) != DW_TAG_enumerator)
14067 continue;
14068
14069 string n, m;
14070 location l;
14071 die_loc_and_name(rdr, &child, l, n, m);
14072 uint64_t val = 0;
14073 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
14074 enms.push_back(enum_type_decl::enumerator(n, val));
14075 }
14076 while (dwarf_siblingof(&child, &child) == 0);
14077 }
14078
14079 // DWARF up to version 4 (at least) doesn't seem to carry the
14080 // underlying type, so let's create an artificial one here, which
14081 // sole purpose is to be passed to the constructor of the
14082 // enum_type_decl type.
14083 type_decl_sptr t =
14084 build_enum_underlying_type(rdr, name, size,
14085 enum_underlying_type_is_anonymous);
14086 t->set_is_declaration_only(is_declaration_only);
14087
14088 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
14089 result->set_is_anonymous(is_anonymous);
14090 result->set_is_declaration_only(is_declaration_only);
14091 result->set_is_artificial(is_artificial);
14092 rdr.associate_die_to_type(die, result, where_offset);
14093
14094 return result;
14095}
14096
14097/// Once a function_decl has been built and added to a class as a
14098/// member function, this function updates the information of the
14099/// function_decl concerning the properties of its relationship with
14100/// the member class. That is, it updates properties like
14101/// virtualness, access, constness, cdtorness, etc ...
14102///
14103/// @param die the DIE of the function_decl that has been just built.
14104///
14105/// @param f the function_decl that has just been built from @p die.
14106///
14107/// @param klass the @ref class_or_union that @p f belongs to.
14108///
14109/// @param rdr the context used to read the ELF/DWARF information.
14110static void
14111finish_member_function_reading(Dwarf_Die* die,
14112 const function_decl_sptr& f,
14113 const class_or_union_sptr klass,
14114 reader& rdr)
14115{
14116 ABG_ASSERT(klass);
14117
14118 method_decl_sptr m = is_method_decl(f);
14119 ABG_ASSERT(m);
14120
14121 method_type_sptr method_t = is_method_type(m->get_type());
14122 ABG_ASSERT(method_t);
14123
14124 size_t is_inline = die_is_declared_inline(die);
14125 bool is_ctor = (f->get_name() == klass->get_name());
14126 bool is_dtor = (!f->get_name().empty()
14127 && static_cast<string>(f->get_name())[0] == '~');
14128 bool is_virtual = die_is_virtual(die);
14129 int64_t vindex = -1;
14130 if (is_virtual)
14131 die_virtual_function_index(die, vindex);
14132 access_specifier access = public_access;
14133 if (class_decl_sptr c = is_class_type(klass))
14134 if (!c->is_struct())
14135 access = private_access;
14136 die_access_specifier(die, access);
14137
14138 m->is_declared_inline(is_inline);
14139 set_member_access_specifier(m, access);
14140 if (is_virtual)
14141 set_member_function_virtuality(m, is_virtual, vindex);
14142 bool is_static = method_t->get_is_for_static_method();
14143 set_member_is_static(m, is_static);
14144 set_member_function_is_ctor(m, is_ctor);
14145 set_member_function_is_dtor(m, is_dtor);
14146 set_member_function_is_const(m, method_t->get_is_const());
14147
14149
14150 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol()
14152 {
14153 // This is a virtual member function which has a linkage name
14154 // but has no underlying symbol set.
14155 //
14156 // The underlying elf symbol to set to this function can show up
14157 // later in the DWARF input or it can be that, because of some
14158 // compiler optimization, the relation between this function and
14159 // its underlying elf symbol is simply not emitted in the DWARF.
14160 //
14161 // Let's thus schedule this function for a later fixup pass
14162 // (performed by
14163 // reader::fixup_functions_with_no_symbols()) that will
14164 // set its underlying symbol.
14165 //
14166 // Note that if the underying symbol is encountered later in the
14167 // DWARF input, then the part of build_function_decl() that
14168 // updates the function to set its underlying symbol will
14169 // de-schedule this function wrt fixup pass.
14170 Dwarf_Off die_offset = dwarf_dieoffset(die);
14171 die_function_decl_map_type &fns_with_no_symbol =
14172 rdr.die_function_decl_with_no_symbol_map();
14173 die_function_decl_map_type::const_iterator i =
14174 fns_with_no_symbol.find(die_offset);
14175 if (i == fns_with_no_symbol.end())
14176 fns_with_no_symbol[die_offset] = f;
14177 }
14178
14179}
14180
14181/// If a function DIE has attributes which have not yet been read and
14182/// added to the internal representation that represents that function
14183/// then read those extra attributes and update the internal
14184/// representation.
14185///
14186/// @param rdr the DWARF reader to use.
14187///
14188/// @param die the function DIE to consider.
14189///
14190/// @param where_offset where we logical are, currently, in the stream
14191/// of DIEs. If you don't know what this is, you can just set it to zero.
14192///
14193/// @param existing_fn the representation of the function to update.
14194///
14195/// @return the updated function representation.
14196static function_decl_sptr
14197maybe_finish_function_decl_reading(reader& rdr,
14198 Dwarf_Die* die,
14199 size_t where_offset,
14200 const function_decl_sptr& existing_fn)
14201{
14202 function_decl_sptr result = build_function_decl(rdr, die,
14203 where_offset,
14204 existing_fn);
14205
14206 return result;
14207}
14208
14209/// Lookup a class or a typedef with a given qualified name in the
14210/// corpus that a given scope belongs to.
14211///
14212/// @param scope the scope to consider.
14213///
14214/// @param type_name the qualified name of the type to look for.
14215///
14216/// @return the typedef or class type found.
14217static type_base_sptr
14218lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
14219{
14220 string qname = build_qualified_name(scope, type_name);
14221 corpus* corp = scope->get_corpus();
14222 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
14223 return result;
14224}
14225
14226/// Lookup a class of typedef type from the current corpus being
14227/// constructed.
14228///
14229/// The type being looked for has the same name as a given DIE.
14230///
14231/// @param rdr the DWARF reader to use.
14232///
14233/// @param die the DIE which has the same name as the type we are
14234/// looking for.
14235///
14236/// @param called_for_public_decl whether this function is being
14237/// called from a a publicly defined declaration.
14238///
14239/// @param where_offset where we are logically at in the DIE stream.
14240///
14241/// @return the type found.
14242static type_base_sptr
14243lookup_class_or_typedef_from_corpus(reader& rdr,
14244 Dwarf_Die* die,
14245 bool called_for_public_decl,
14246 size_t where_offset)
14247{
14248 if (!die)
14249 return class_decl_sptr();
14250
14251 string class_name = die_string_attribute(die, DW_AT_name);
14252 if (class_name.empty())
14253 return class_decl_sptr();
14254
14255 scope_decl_sptr scope = get_scope_for_die(rdr, die,
14256 called_for_public_decl,
14257 where_offset);
14258 if (scope)
14259 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
14260
14261 return type_base_sptr();
14262}
14263
14264/// Test if a DIE represents a function that is a member of a given
14265/// class type.
14266///
14267/// @param rdr the DWARF reader.
14268///
14269/// @param function_die the DIE of the function to consider.
14270///
14271/// @param class_type the class type to consider.
14272///
14273/// @param where_offset where we are logically at in the DIE stream.
14274///
14275/// @return the method declaration corresponding to the member
14276/// function of @p class_type, iff @p function_die is for a member
14277/// function of @p class_type.
14278static method_decl_sptr
14279is_function_for_die_a_member_of_class(reader& rdr,
14280 Dwarf_Die* function_die,
14281 const class_or_union_sptr& class_type)
14282{
14283 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
14284
14285 if (!artifact)
14286 return method_decl_sptr();
14287
14288 method_decl_sptr method = is_method_decl(artifact);
14289 method_type_sptr method_type;
14290
14291 if (method)
14292 method_type = method->get_type();
14293 else
14294 method_type = is_method_type(artifact);
14295 ABG_ASSERT(method_type);
14296
14297 class_or_union_sptr method_class = method_type->get_class_type();
14298 ABG_ASSERT(method_class);
14299
14300 string method_class_name = method_class->get_qualified_name(),
14301 class_type_name = class_type->get_qualified_name();
14302
14303 if (method_class_name == class_type_name)
14304 {
14305 //ABG_ASSERT(class_type.get() == method_class.get());
14306 return method;
14307 }
14308
14309 return method_decl_sptr();
14310}
14311
14312/// If a given function DIE represents an existing member function of
14313/// a given class, then update that member function with new
14314/// properties present in the DIE. Otherwise, if the DIE represents a
14315/// new member function that is not already present in the class then
14316/// add that new member function to the class.
14317///
14318/// @param rdr the DWARF reader.
14319///
14320/// @param function_die the DIE of the potential member function to
14321/// consider.
14322///
14323/// @param class_type the class type to consider.
14324///
14325/// @param called_from_public_decl is true iff this function was
14326/// called from a publicly defined and exported declaration.
14327///
14328/// @param where_offset where we are logically at in the DIE stream.
14329///
14330/// @return the method decl representing the member function.
14331static method_decl_sptr
14332add_or_update_member_function(reader& rdr,
14333 Dwarf_Die* function_die,
14334 const class_or_union_sptr& class_type,
14335 bool called_from_public_decl,
14336 size_t where_offset)
14337{
14338 method_decl_sptr method =
14339 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
14340
14341 if (!method)
14342 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
14343 class_type.get(),
14344 called_from_public_decl,
14345 where_offset));
14346 if (!method)
14347 return method_decl_sptr();
14348
14349 finish_member_function_reading(function_die,
14350 is_function_decl(method),
14351 class_type, rdr);
14352 return method;
14353}
14354
14355/// Build a an IR node for class type from a DW_TAG_structure_type or
14356/// DW_TAG_class_type DIE and add that node to the ABI corpus being
14357/// currently built.
14358///
14359/// If the represents class type that already exists, then update the
14360/// existing class type with the new properties found in the DIE.
14361///
14362/// It meanst that this function can also update an existing
14363/// class_decl node with data members, member functions and other
14364/// properties coming from the DIE.
14365///
14366/// @param rdr the DWARF reader to consider.
14367///
14368/// @param die the DIE to read information from. Must be either a
14369/// DW_TAG_structure_type or a DW_TAG_class_type.
14370///
14371/// @param scope a pointer to the scope_decl* under which this class
14372/// is to be added to.
14373///
14374/// @param is_struct whether the class was declared as a struct.
14375///
14376/// @param klass if non-null, this is a klass to append the members
14377/// to. Otherwise, this function just builds the class from scratch.
14378///
14379/// @param called_from_public_decl set to true if this class is being
14380/// called from a "Public declaration like vars or public symbols".
14381///
14382/// @param where_offset the offset of the DIE where we are "logically"
14383/// positionned at, in the DIE tree. This is useful when @p die is
14384/// e.g, DW_TAG_partial_unit that can be included in several places in
14385/// the DIE tree.
14386///
14387/// @param is_declaration_only is true if the DIE denoted by @p die is
14388/// a declaration-only DIE.
14389///
14390/// @return the resulting class_type.
14391static class_decl_sptr
14392add_or_update_class_type(reader& rdr,
14393 Dwarf_Die* die,
14394 scope_decl* scope,
14395 bool is_struct,
14396 class_decl_sptr klass,
14397 bool called_from_public_decl,
14398 size_t where_offset,
14399 bool is_declaration_only)
14400{
14401 class_decl_sptr result;
14402 if (!die)
14403 return result;
14404
14405 const die_source source = rdr.get_die_source(die);
14406
14407 unsigned tag = dwarf_tag(die);
14408
14409 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
14410 return result;
14411
14412 {
14413 die_class_or_union_map_type::const_iterator i =
14414 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14415 if (i != rdr.die_wip_classes_map(source).end())
14416 {
14417 class_decl_sptr class_type = is_class_type(i->second);
14418 ABG_ASSERT(class_type);
14419 return class_type;
14420 }
14421 }
14422
14423 string name, linkage_name;
14424 location loc;
14425 die_loc_and_name(rdr, die, loc, name, linkage_name);
14426 cleanup_decl_name(name);
14427
14428 bool is_anonymous = false;
14429 if (name.empty())
14430 {
14431 // So we are looking at an anonymous struct. Let's
14432 // give it a name.
14433 name = get_internal_anonymous_die_prefix_name(die);
14434 ABG_ASSERT(!name.empty());
14435 // But we remember that the type is anonymous.
14436 is_anonymous = true;
14437
14438 size_t s = 0;
14439 if (scope)
14440 s = scope->get_num_anonymous_member_classes();
14441 else
14442 s = rdr.global_scope()->get_num_anonymous_member_classes();
14443 name = build_internal_anonymous_die_name(name, s);
14444 }
14445
14446 if (!is_anonymous)
14447 {
14448 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14449 {
14450 if (loc)
14451 // TODO: if there is only one class defined in the corpus
14452 // for this location, then re-use it. But if there are
14453 // more than one, then do not re-use it, for now.
14454 result = lookup_class_type_per_location(loc.expand(), *corp);
14455 else
14456 // TODO: if there is just one class for that name defined,
14457 // then re-use it. Otherwise, don't.
14458 result = lookup_class_type(name, *corp);
14459 if (result
14460 // If we are seeing a declaration of a definition we
14461 // already had, or if we are seing a type with the same
14462 // declaration-only-ness that we had before, then keep
14463 // the one we already had.
14464 && (result->get_is_declaration_only() == is_declaration_only
14465 || (!result->get_is_declaration_only()
14466 && is_declaration_only)))
14467 {
14468 rdr.associate_die_to_type(die, result, where_offset);
14469 return result;
14470 }
14471 else
14472 // We might be seeing the definition of a declaration we
14473 // already had. In that case, keep the definition and
14474 // drop the declaration.
14475 result.reset();
14476 }
14477 }
14478
14479 // If we've already seen the same class as 'die', then let's re-use
14480 // that one, unless it's an anonymous class. We can't really safely
14481 // re-use anonymous classes as they have no name, by construction.
14482 // What we can do, rather, is to reuse the typedef that name them,
14483 // when they do have a naming typedef.
14484 if (!is_anonymous)
14485 if (class_decl_sptr pre_existing_class =
14486 is_class_type(rdr.lookup_type_artifact_from_die(die)))
14487 klass = pre_existing_class;
14488
14489 uint64_t size = 0;
14490 die_size_in_bits(die, size);
14491 bool is_artificial = die_is_artificial(die);
14492
14493 Dwarf_Die child;
14494 bool has_child = (dwarf_child(die, &child) == 0);
14495
14496 decl_base_sptr res;
14497 if (klass)
14498 {
14499 res = result = klass;
14500 if (has_child && klass->get_is_declaration_only()
14501 && klass->get_definition_of_declaration())
14502 res = result = is_class_type(klass->get_definition_of_declaration());
14503 if (loc)
14504 result->set_location(loc);
14505 }
14506 else
14507 {
14508 result.reset(new class_decl(rdr.env(), name, size,
14509 /*alignment=*/0, is_struct, loc,
14510 decl_base::VISIBILITY_DEFAULT,
14511 is_anonymous));
14512
14513 result->set_is_declaration_only(is_declaration_only);
14514
14515 res = add_decl_to_scope(result, scope);
14516 result = dynamic_pointer_cast<class_decl>(res);
14517 ABG_ASSERT(result);
14518 }
14519
14520 if (!klass || klass->get_is_declaration_only())
14521 if (size != result->get_size_in_bits())
14522 result->set_size_in_bits(size);
14523
14524 if (klass)
14525 // We are amending a class that was built before. So let's check
14526 // if we need to amend its "declaration-only-ness" status.
14527 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
14528 // The size of the class doesn't match its
14529 // 'declaration-only-ness". We might have a non-zero sized
14530 // class which is declaration-only, or a zero sized class that
14531 // is not declaration-only. Let's set the declaration-only-ness
14532 // according to what we are instructed to.
14533 //
14534 // Note however that there are binaries out there emitted by
14535 // compilers (Clang, in C++) emit declarations-only classes that
14536 // have non-zero size. So we must honor these too. That is why
14537 // we are not forcing the declaration-only-ness to false when a
14538 // class has non-zero size. An example of such binary is
14539 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
14540 result->set_is_declaration_only(is_declaration_only);
14541
14542 // If a non-decl-only class has children node and is advertized as
14543 // having a non-zero size let's trust that.
14544 if (!result->get_is_declaration_only() && has_child)
14545 if (result->get_size_in_bits() == 0 && size != 0)
14546 result->set_size_in_bits(size);
14547
14548 result->set_is_artificial(is_artificial);
14549
14550 rdr.associate_die_to_type(die, result, where_offset);
14551
14552 if (!has_child)
14553 // TODO: set the access specifier for the declaration-only class
14554 // here.
14555 return result;
14556
14557 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14558
14559 bool is_incomplete_type = false;
14560 if (is_declaration_only && size == 0 && has_child)
14561 // this is an incomplete DWARF type as defined by [5.7.1]
14562 //
14563 // An incomplete structure, union or class type is represented by
14564 // a structure, union or class entry that does not have a byte
14565 // size attribute and that has a DW_AT_declaration attribute.
14566 //
14567 // Let's consider that it's thus a decl-only class, likely
14568 // referred to by a pointer. If we later encounter a definition
14569 // for this decl-only class type, then this decl-only class will
14570 // be resolved to it by the code in
14571 // reader::resolve_declaration_only_classes.
14572 is_incomplete_type = true;
14573
14574 scope_decl_sptr scop =
14575 dynamic_pointer_cast<scope_decl>(res);
14576 ABG_ASSERT(scop);
14577 rdr.scope_stack().push(scop.get());
14578
14579 if (has_child && !is_incomplete_type)
14580 {
14581 do
14582 {
14583 tag = dwarf_tag(&child);
14584
14585 // Handle base classes.
14586 if (tag == DW_TAG_inheritance)
14587 {
14588 result->set_is_declaration_only(false);
14589
14590 Dwarf_Die type_die;
14591 if (!die_die_attribute(&child, DW_AT_type, type_die))
14592 continue;
14593
14594 string type_name = die_type_name(rdr, &type_die,
14595 /*qualified_name=*/true,
14596 where_offset);
14597 type_base_sptr base_type;
14598 if (!type_name.empty())
14599 {
14600 base_type = result->find_base_class(type_name);
14601 if (base_type)
14602 continue;
14603 }
14604
14605 base_type =
14606 lookup_class_or_typedef_from_corpus(rdr, &type_die,
14607 called_from_public_decl,
14608 where_offset);
14609 if (!base_type)
14610 base_type =
14611 is_type(build_ir_node_from_die(rdr, &type_die,
14612 called_from_public_decl,
14613 where_offset));
14614
14615 // Sometimes base_type can be a typedef. Let's make
14616 // sure that typedef is compatible with a class type.
14618 if (!b)
14619 continue;
14620
14621 access_specifier access =
14622 is_struct
14623 ? public_access
14624 : private_access;
14625
14626 die_access_specifier(&child, access);
14627
14628 bool is_virt= die_is_virtual(&child);
14629 int64_t offset = 0;
14630 bool is_offset_present =
14631 die_member_offset(rdr, &child, offset);
14632
14633 class_decl::base_spec_sptr base(new class_decl::base_spec
14634 (b, access,
14635 is_offset_present ? offset : -1,
14636 is_virt));
14637 if (b->get_is_declaration_only()
14638 // Only non-anonymous decl-only classes are
14639 // scheduled for resolution to their definition.
14640 // Anonymous classes that are decl-only are likely
14641 // only artificially created by
14642 // get_opaque_version_of_type, from anonymous fully
14643 // defined classes. Those are never defined.
14644 && !b->get_qualified_name().empty())
14645 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
14646 if (result->find_base_class(b->get_qualified_name()))
14647 continue;
14648 result->add_base_specifier(base);
14649 }
14650 // Handle data members.
14651 else if (tag == DW_TAG_member
14652 || tag == DW_TAG_variable)
14653 {
14654 Dwarf_Die type_die;
14655 if (!die_die_attribute(&child, DW_AT_type, type_die))
14656 continue;
14657
14658 string n, m;
14659 location loc;
14660 die_loc_and_name(rdr, &child, loc, n, m);
14661 /// For now, we skip the hidden vtable pointer.
14662 /// Currently, we're looking for a member starting with
14663 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
14664 /// use as a name for the hidden vtable pointer.
14665 if (n.substr(0, 5) == "_vptr"
14666 && n.size() > 5
14667 && !std::isalnum(n.at(5))
14668 && n.at(5) != '_')
14669 continue;
14670
14671 // If the variable is already a member of this class,
14672 // move on. If it's an anonymous data member, we need
14673 // to handle it differently. We'll do that later below.
14674 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14675 continue;
14676
14677 int64_t offset_in_bits = 0;
14678 bool is_laid_out = die_member_offset(rdr, &child,
14679 offset_in_bits);
14680 // For now, is_static == !is_laid_out. When we have
14681 // templates, we'll try to be more specific. For now,
14682 // this approximation should do OK.
14683 bool is_static = !is_laid_out;
14684
14685 if (is_static)
14686 // We are looking at the *declaration* of a static
14687 // data member. The definition comes later (or
14688 // somewhere else, rather)in the DWARF. It's the
14689 // definition that we are interested in because it has
14690 // attributes of the concrete representation of the
14691 // static data member like, the ELF symbol (storage
14692 // address) of the variable, etc. It's at that point
14693 // that the IR of the data member is going to be
14694 // created (by build_ir_node_from_die, in the
14695 // DW_TAG_variable case) and added to this class/struct
14696 // being created. So for now, just ignore it.
14697 continue;
14698
14699 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
14700 called_from_public_decl,
14701 where_offset));
14702 type_base_sptr t = is_type(ty);
14703 if (!t)
14704 continue;
14705
14706 if (n.empty() && !die_is_anonymous_data_member(&child))
14707 {
14708 // We must be in a case where the data member has an
14709 // empty name because the DWARF emitter has a bug.
14710 // Let's generate an artificial name for that data
14711 // member.
14712 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
14713 ABG_ASSERT(!n.empty());
14714 }
14715
14716 // The call to build_ir_node_from_die above could have
14717 // triggered the adding of a data member named 'n' into
14718 // result. So let's check again if the variable is
14719 // already a member of this class. Here again, if it's
14720 // an anonymous data member, we need to handle it
14721 // differently. We'll do that later below.
14722 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14723 continue;
14724
14725 if (!is_static)
14726 // We have a non-static data member. So this class
14727 // cannot be a declaration-only class anymore, even if
14728 // some DWARF emitters might consider it otherwise.
14729 result->set_is_declaration_only(false);
14730 access_specifier access =
14731 is_struct
14732 ? public_access
14733 : private_access;
14734
14735 die_access_specifier(&child, access);
14736
14737 var_decl_sptr dm(new var_decl(n, t, loc, m));
14738 if (n.empty()
14740 // dm is an anonymous data member that was already
14741 // present in the current class so let's not add it.
14742 continue;
14743 result->add_data_member(dm, access, is_laid_out,
14744 is_static, offset_in_bits);
14745 ABG_ASSERT(has_scope(dm));
14746 rdr.associate_die_to_decl(&child, dm, where_offset,
14747 /*associate_by_repr=*/false);
14748 }
14749 // Handle member functions;
14750 else if (tag == DW_TAG_subprogram)
14751 {
14752 decl_base_sptr r =
14753 add_or_update_member_function(rdr, &child, result,
14754 called_from_public_decl,
14755 where_offset);
14757 rdr.associate_die_to_decl(&child, f, where_offset,
14758 /*associate_by_repr=*/true);
14759 }
14760 // Handle member types
14761 else if (die_is_type(&child))
14762 {
14763 // if the type is not already a member of this class,
14764 // then add it to the class.
14765 if (!is_anonymous_type_die(&child)
14766 && !result->find_member_type(die_name(&child)))
14767 build_ir_node_from_die(rdr, &child, result.get(),
14768 called_from_public_decl,
14769 where_offset);
14770 else if (is_anonymous_type_die(&child))
14771 {
14772 // Lookup the anonymous type DIE direcly by building
14773 // its flat representation & using it as the name of
14774 // the anonymous struct/union.
14775 string anonymous_type_name =
14776 die_class_or_enum_flat_representation(rdr, &child,
14777 /*indent=*/"",
14778 /*one_line=*/true,
14779 /*qualed_name=*/false,
14780 where_offset);
14781 if (type_base_sptr member_t =
14782 result->find_member_type(anonymous_type_name))
14783 rdr.associate_die_to_decl(&child, is_decl(member_t),
14784 where_offset,
14785 /*Associate_by_repr=*/false);
14786 else
14787 {
14788 type_base_sptr t =
14789 is_type(build_ir_node_from_die(rdr, &child,
14790 /*scope=*/result.get(),
14791 called_from_public_decl,
14792 where_offset));
14793 if (t)
14794 {
14795 add_decl_to_scope(is_decl(t), result.get());
14796 maybe_set_member_type_access_specifier(result,
14797 &child);
14798 }
14799 }
14800 }
14801 }
14802 } while (dwarf_siblingof(&child, &child) == 0);
14803 }
14804
14805 rdr.scope_stack().pop();
14806
14807 {
14808 die_class_or_union_map_type::const_iterator i =
14809 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14810 if (i != rdr.die_wip_classes_map(source).end())
14811 {
14812 if (is_member_type(i->second))
14814 get_member_access_specifier(i->second));
14815 rdr.die_wip_classes_map(source).erase(i);
14816 }
14817 }
14818
14819 return result;
14820}
14821
14822/// Build an @ref union_decl from a DW_TAG_union_type DIE.
14823///
14824/// @param rdr the DWARF reader to use.
14825///
14826/// @param die the DIE to read from.
14827///
14828/// @param scope the scope the resulting @ref union_decl belongs to.
14829///
14830/// @param union_type if this parameter is non-nil, then this function
14831/// updates the @ref union_decl that it points to, rather than
14832/// creating a new @ref union_decl.
14833///
14834/// @param called_from_public_decl is true if this function has been
14835/// initially called within the context of a public decl.
14836///
14837/// @param where_offset the offset of the DIE where we are "logically"
14838/// positionned at, in the DIE tree. This is useful when @p die is
14839/// e.g, DW_TAG_partial_unit that can be included in several places in
14840/// the DIE tree.
14841///
14842/// @param is_declaration_only is true if the DIE denoted by @p die is
14843/// a declaration-only DIE.
14844///
14845/// @return the resulting @ref union_decl type.
14846static union_decl_sptr
14847add_or_update_union_type(reader& rdr,
14848 Dwarf_Die* die,
14849 scope_decl* scope,
14850 union_decl_sptr union_type,
14851 bool called_from_public_decl,
14852 size_t where_offset,
14853 bool is_declaration_only)
14854{
14855 union_decl_sptr result;
14856 if (!die)
14857 return result;
14858
14859 unsigned tag = dwarf_tag(die);
14860
14861 if (tag != DW_TAG_union_type)
14862 return result;
14863
14864 const die_source source = rdr.get_die_source(die);
14865 {
14866 die_class_or_union_map_type::const_iterator i =
14867 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
14868 if (i != rdr.die_wip_classes_map(source).end())
14869 {
14870 union_decl_sptr u = is_union_type(i->second);
14871 ABG_ASSERT(u);
14872 return u;
14873 }
14874 }
14875
14876 string name, linkage_name;
14877 location loc;
14878 die_loc_and_name(rdr, die, loc, name, linkage_name);
14879 cleanup_decl_name(name);
14880
14881 bool is_anonymous = false;
14882 if (name.empty())
14883 {
14884 // So we are looking at an anonymous union. Let's give it a
14885 // name.
14886 name = get_internal_anonymous_die_prefix_name(die);
14887 ABG_ASSERT(!name.empty());
14888 // But we remember that the type is anonymous.
14889 is_anonymous = true;
14890
14891 size_t s = 0;
14892 if (scope)
14893 s = scope->get_num_anonymous_member_unions();
14894 else
14895 s = rdr.global_scope()->get_num_anonymous_member_classes();
14896 name = build_internal_anonymous_die_name(name, s);
14897 }
14898
14899 // If the type has location, then associate it to its
14900 // representation. This way, all occurences of types with the same
14901 // representation (name) and location can be later detected as being
14902 // for the same type.
14903
14904 if (!is_anonymous)
14905 {
14906 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14907 {
14908 if (loc)
14909 result = lookup_union_type_per_location(loc.expand(), *corp);
14910 else
14911 result = lookup_union_type(name, *corp);
14912
14913 if (result)
14914 {
14915 rdr.associate_die_to_type(die, result, where_offset);
14916 return result;
14917 }
14918 }
14919 }
14920
14921 // if we've already seen a union with the same union as 'die' then
14922 // let's re-use that one. We can't really safely re-use anonymous
14923 // unions as they have no name, by construction. What we can do,
14924 // rather, is to reuse the typedef that name them, when they do have
14925 // a naming typedef.
14926 if (!is_anonymous)
14927 if (union_decl_sptr pre_existing_union =
14928 is_union_type(rdr.lookup_artifact_from_die(die)))
14929 union_type = pre_existing_union;
14930
14931 uint64_t size = 0;
14932 die_size_in_bits(die, size);
14933 bool is_artificial = die_is_artificial(die);
14934
14935 if (union_type)
14936 {
14937 result = union_type;
14938 result->set_location(loc);
14939 }
14940 else
14941 {
14942 result.reset(new union_decl(rdr.env(), name, size, loc,
14943 decl_base::VISIBILITY_DEFAULT,
14944 is_anonymous));
14945 if (is_declaration_only)
14946 result->set_is_declaration_only(true);
14947 result = is_union_type(add_decl_to_scope(result, scope));
14948 ABG_ASSERT(result);
14949 }
14950
14951 if (size)
14952 {
14953 result->set_size_in_bits(size);
14954 result->set_is_declaration_only(false);
14955 }
14956
14957 result->set_is_artificial(is_artificial);
14958
14959 rdr.associate_die_to_type(die, result, where_offset);
14960
14961 Dwarf_Die child;
14962 bool has_child = (dwarf_child(die, &child) == 0);
14963 if (!has_child)
14964 return result;
14965
14966 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
14967
14968 scope_decl_sptr scop =
14969 dynamic_pointer_cast<scope_decl>(result);
14970 ABG_ASSERT(scop);
14971 rdr.scope_stack().push(scop.get());
14972
14973 if (has_child)
14974 {
14975 do
14976 {
14977 tag = dwarf_tag(&child);
14978 // Handle data members.
14979 if (tag == DW_TAG_member || tag == DW_TAG_variable)
14980 {
14981 Dwarf_Die type_die;
14982 if (!die_die_attribute(&child, DW_AT_type, type_die))
14983 continue;
14984
14985 string n, m;
14986 location loc;
14987 die_loc_and_name(rdr, &child, loc, n, m);
14988
14989 // Because we can be updating an existing union, let's
14990 // make sure we don't already have a member of the same
14991 // name. Anonymous member are handled a bit later below
14992 // so let's not consider them here.
14993 if (!n.empty() && lookup_var_decl_in_scope(n, result))
14994 continue;
14995
14996 ssize_t offset_in_bits = 0;
14997 decl_base_sptr ty =
14998 is_decl(build_ir_node_from_die(rdr, &type_die,
14999 called_from_public_decl,
15000 where_offset));
15001 type_base_sptr t = is_type(ty);
15002 if (!t)
15003 continue;
15004
15005 // We have a non-static data member. So this union
15006 // cannot be a declaration-only union anymore, even if
15007 // some DWARF emitters might consider it otherwise.
15008 result->set_is_declaration_only(false);
15009 access_specifier access = public_access;
15010
15011 die_access_specifier(&child, access);
15012
15013 var_decl_sptr dm(new var_decl(n, t, loc, m));
15014 // If dm is an anonymous data member, let's make sure
15015 // the current union doesn't already have it as a data
15016 // member.
15017 if (n.empty() && result->find_data_member(dm))
15018 continue;
15019
15020 if (!n.empty() && lookup_var_decl_in_scope(n, result))
15021 continue;
15022
15023 result->add_data_member(dm, access, /*is_laid_out=*/true,
15024 /*is_static=*/false,
15025 offset_in_bits);
15026 ABG_ASSERT(has_scope(dm));
15027 rdr.associate_die_to_decl(&child, dm, where_offset,
15028 /*associate_by_repr=*/false);
15029 }
15030 // Handle member functions;
15031 else if (tag == DW_TAG_subprogram)
15032 {
15033 decl_base_sptr r =
15034 is_decl(build_ir_node_from_die(rdr, &child,
15035 result.get(),
15036 called_from_public_decl,
15037 where_offset));
15038 if (!r)
15039 continue;
15040
15041 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
15042 ABG_ASSERT(f);
15043
15044 finish_member_function_reading(&child, f, result, rdr);
15045
15046 rdr.associate_die_to_decl(&child, f, where_offset,
15047 /*associate_by_repr=*/false);
15048 }
15049 // Handle member types
15050 else if (die_is_type(&child))
15051 {
15052 string type_name = die_type_name(rdr, &child,
15053 /*qualified_name=*/false,
15054 where_offset);
15055 if (type_base_sptr member_t = result->find_member_type(type_name))
15056 rdr.associate_die_to_decl(&child, is_decl(member_t),
15057 where_offset,
15058 /*associate_by_repr=*/false);
15059 else
15060 decl_base_sptr td =
15061 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
15062 called_from_public_decl,
15063 where_offset));
15064 }
15065 } while (dwarf_siblingof(&child, &child) == 0);
15066 }
15067
15068 rdr.scope_stack().pop();
15069
15070 {
15071 die_class_or_union_map_type::const_iterator i =
15072 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
15073 if (i != rdr.die_wip_classes_map(source).end())
15074 {
15075 if (is_member_type(i->second))
15077 get_member_access_specifier(i->second));
15078 rdr.die_wip_classes_map(source).erase(i);
15079 }
15080 }
15081
15082 return result;
15083}
15084
15085/// build a qualified type from a DW_TAG_const_type,
15086/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
15087///
15088/// @param rdr the DWARF reader to consider.
15089///
15090/// @param die the input DIE to read from.
15091///
15092/// @param called_from_public_decl true if this function was called
15093/// from a context where either a public function or a public variable
15094/// is being built.
15095///
15096/// @param where_offset the offset of the DIE where we are "logically"
15097/// positionned at, in the DIE tree. This is useful when @p die is
15098/// e.g, DW_TAG_partial_unit that can be included in several places in
15099/// the DIE tree.
15100///
15101/// @return the resulting qualified_type_def.
15102static type_base_sptr
15103build_qualified_type(reader& rdr,
15104 Dwarf_Die* die,
15105 bool called_from_public_decl,
15106 size_t where_offset)
15107{
15108 type_base_sptr result;
15109 if (!die)
15110 return result;
15111
15112 unsigned tag = dwarf_tag(die);
15113
15114 if (tag != DW_TAG_const_type
15115 && tag != DW_TAG_volatile_type
15116 && tag != DW_TAG_restrict_type)
15117 return result;
15118
15119 Dwarf_Die underlying_type_die;
15120 decl_base_sptr utype_decl;
15121 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15122 // So, if no DW_AT_type is present, then this means (if we are
15123 // looking at a debug info emitted by GCC) that we are looking
15124 // at a qualified void type.
15125 utype_decl = build_ir_node_for_void_type(rdr);
15126
15127 if (!utype_decl)
15128 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
15129 called_from_public_decl,
15130 where_offset));
15131 if (!utype_decl)
15132 return result;
15133
15134 // The call to build_ir_node_from_die() could have triggered the
15135 // creation of the type for this DIE. In that case, just return it.
15136 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15137 {
15138 result = t;
15139 rdr.associate_die_to_type(die, result, where_offset);
15140 return result;
15141 }
15142
15143 type_base_sptr utype = is_type(utype_decl);
15144 ABG_ASSERT(utype);
15145
15146 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
15147 if (tag == DW_TAG_const_type)
15148 qual |= qualified_type_def::CV_CONST;
15149 else if (tag == DW_TAG_volatile_type)
15150 qual |= qualified_type_def::CV_VOLATILE;
15151 else if (tag == DW_TAG_restrict_type)
15152 qual |= qualified_type_def::CV_RESTRICT;
15153 else
15155
15156 if (!result)
15157 result.reset(new qualified_type_def(utype, qual, location()));
15158
15159 rdr.associate_die_to_type(die, result, where_offset);
15160
15161 return result;
15162}
15163
15164/// Walk a tree of typedef of qualified arrays and schedule all type
15165/// nodes for canonicalization.
15166///
15167/// This is to be used after an array tree has been cloned. In that
15168/// case, the newly cloned type nodes have to be scheduled for
15169/// canonicalization.
15170///
15171/// This is a subroutine of maybe_strip_qualification.
15172///
15173/// @param t the type node to be scheduled for canonicalization.
15174///
15175/// @param rdr the DWARF reader to use.
15176static void
15177schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
15178 reader &rdr)
15179{
15180 if (typedef_decl_sptr type = is_typedef(t))
15181 {
15182 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15183 rdr);
15184 rdr.schedule_type_for_late_canonicalization(t);
15185 }
15186 else if (qualified_type_def_sptr type = is_qualified_type(t))
15187 {
15188 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
15189 rdr);
15190 rdr.schedule_type_for_late_canonicalization(t);
15191 }
15192 else if (array_type_def_sptr type = is_array_type(t))
15193 {
15194 for (vector<array_type_def::subrange_sptr>::const_iterator i =
15195 type->get_subranges().begin();
15196 i != type->get_subranges().end();
15197 ++i)
15198 {
15199 if (!(*i)->get_scope())
15200 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
15201 rdr.schedule_type_for_late_canonicalization(*i);
15202
15203 }
15204 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
15205 rdr);
15206 rdr.schedule_type_for_late_canonicalization(type);
15207 }
15208}
15209
15210/// Strip qualification from a qualified type, when it makes sense.
15211///
15212/// DWARF constructs "const reference". This is redundant because a
15213/// reference is always const. The issue is these redundant types then
15214/// leak into the IR and make for bad diagnostics.
15215///
15216/// This function thus strips the const qualifier from the type in
15217/// that case. It might contain code to strip other cases like this
15218/// in the future.
15219///
15220/// @param t the type to strip const qualification from.
15221///
15222/// @param rdr the @ref reader to use.
15223///
15224/// @return the stripped type or just return @p t.
15225static decl_base_sptr
15226maybe_strip_qualification(const qualified_type_def_sptr t,
15227 reader &rdr)
15228{
15229 if (!t)
15230 return t;
15231
15232 decl_base_sptr result = t;
15233 type_base_sptr u = t->get_underlying_type();
15234
15237 if (result.get() != t.get())
15238 return result;
15239
15241 {
15242 array_type_def_sptr array;
15243 scope_decl * scope = 0;
15244 if ((array = is_array_type(u)))
15245 {
15246 scope = array->get_scope();
15247 ABG_ASSERT(scope);
15248 array = is_array_type(clone_array_tree(array));
15249 schedule_array_tree_for_late_canonicalization(array, rdr);
15250 add_decl_to_scope(array, scope);
15251 t->set_underlying_type(array);
15252 u = t->get_underlying_type();
15253 }
15254 else if (is_typedef_of_array(u))
15255 {
15256 scope = is_decl(u)->get_scope();
15257 ABG_ASSERT(scope);
15258 typedef_decl_sptr typdef =
15260 schedule_array_tree_for_late_canonicalization(typdef, rdr);
15261 ABG_ASSERT(typdef);
15262 add_decl_to_scope(typdef, scope);
15263 t->set_underlying_type(typdef);
15264 u = t->get_underlying_type();
15265 array = is_typedef_of_array(u);
15266 }
15267 else
15269
15270 ABG_ASSERT(array);
15271 // We should not be editing types that are already canonicalized.
15272 ABG_ASSERT(!array->get_canonical_type());
15273 type_base_sptr element_type = array->get_element_type();
15274
15275 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
15276 {
15277 // We should not be editing types that are already canonicalized.
15278 ABG_ASSERT(!qualified->get_canonical_type());
15279 qualified_type_def::CV quals = qualified->get_cv_quals();
15280 quals |= t->get_cv_quals();
15281 qualified->set_cv_quals(quals);
15283 result = is_decl(u);
15284 }
15285 else
15286 {
15287 qualified_type_def_sptr qual_type
15288 (new qualified_type_def(element_type,
15289 t->get_cv_quals(),
15290 t->get_location()));
15292 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
15293 array->set_element_type(qual_type);
15294 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
15295 result = is_decl(u);
15296 }
15297 }
15298
15299 return result;
15300}
15301
15302/// Build a pointer type from a DW_TAG_pointer_type DIE.
15303///
15304/// @param rdr the DWARF reader to consider.
15305///
15306/// @param die the DIE to read information from.
15307///
15308/// @param called_from_public_decl true if this function was called
15309/// from a context where either a public function or a public variable
15310/// is being built.
15311///
15312/// @param where_offset the offset of the DIE where we are "logically"
15313/// positionned at, in the DIE tree. This is useful when @p die is
15314/// e.g, DW_TAG_partial_unit that can be included in several places in
15315/// the DIE tree.
15316///
15317/// @return the resulting pointer to pointer_type_def.
15319build_pointer_type_def(reader& rdr,
15320 Dwarf_Die* die,
15321 bool called_from_public_decl,
15322 size_t where_offset)
15323{
15324 pointer_type_def_sptr result;
15325
15326 if (!die)
15327 return result;
15328
15329 unsigned tag = dwarf_tag(die);
15330 if (tag != DW_TAG_pointer_type)
15331 return result;
15332
15333 type_or_decl_base_sptr utype_decl;
15334 Dwarf_Die underlying_type_die;
15335 bool has_underlying_type_die = false;
15336 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15337 // If the DW_AT_type attribute is missing, that means we are
15338 // looking at a pointer to "void".
15339 utype_decl = build_ir_node_for_void_type(rdr);
15340 else
15341 has_underlying_type_die = true;
15342
15343 if (!utype_decl && has_underlying_type_die)
15344 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
15345 called_from_public_decl,
15346 where_offset);
15347 if (!utype_decl)
15348 return result;
15349
15350 // The call to build_ir_node_from_die() could have triggered the
15351 // creation of the type for this DIE. In that case, just return it.
15352 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15353 {
15354 result = is_pointer_type(t);
15355 ABG_ASSERT(result);
15356 return result;
15357 }
15358
15359 type_base_sptr utype = is_type(utype_decl);
15360 ABG_ASSERT(utype);
15361
15362 // if the DIE for the pointer type doesn't have a byte_size
15363 // attribute then we assume the size of the pointer is the address
15364 // size of the current translation unit.
15365 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15366 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15367 // The size as expressed by DW_AT_byte_size is in byte, so let's
15368 // convert it to bits.
15369 size *= 8;
15370
15371 // And the size of the pointer must be the same as the address size
15372 // of the current translation unit.
15373 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15374
15375 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
15376 ABG_ASSERT(result->get_pointed_to_type());
15377
15378 if (is_void_pointer_type(result))
15379 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
15380
15381 rdr.associate_die_to_type(die, result, where_offset);
15382 return result;
15383}
15384
15385/// Build a reference type from either a DW_TAG_reference_type or
15386/// DW_TAG_rvalue_reference_type DIE.
15387///
15388/// @param rdr the DWARF reader to consider.
15389///
15390/// @param die the DIE to read from.
15391///
15392/// @param called_from_public_decl true if this function was called
15393/// from a context where either a public function or a public variable
15394/// is being built.
15395///
15396/// @param where_offset the offset of the DIE where we are "logically"
15397/// positionned at, in the DIE tree. This is useful when @p die is
15398/// e.g, DW_TAG_partial_unit that can be included in several places in
15399/// the DIE tree.
15400///
15401/// @return a pointer to the resulting reference_type_def.
15403build_reference_type(reader& rdr,
15404 Dwarf_Die* die,
15405 bool called_from_public_decl,
15406 size_t where_offset)
15407{
15409
15410 if (!die)
15411 return result;
15412
15413 unsigned tag = dwarf_tag(die);
15414 if (tag != DW_TAG_reference_type
15415 && tag != DW_TAG_rvalue_reference_type)
15416 return result;
15417
15418 Dwarf_Die underlying_type_die;
15419 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
15420 return result;
15421
15422 type_or_decl_base_sptr utype_decl =
15423 build_ir_node_from_die(rdr, &underlying_type_die,
15424 called_from_public_decl,
15425 where_offset);
15426 if (!utype_decl)
15427 return result;
15428
15429 // The call to build_ir_node_from_die() could have triggered the
15430 // creation of the type for this DIE. In that case, just return it.
15431 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15432 {
15433 result = is_reference_type(t);
15434 ABG_ASSERT(result);
15435 return result;
15436 }
15437
15438 type_base_sptr utype = is_type(utype_decl);
15439 ABG_ASSERT(utype);
15440
15441 // if the DIE for the reference type doesn't have a byte_size
15442 // attribute then we assume the size of the reference is the address
15443 // size of the current translation unit.
15444 uint64_t size = rdr.cur_transl_unit()->get_address_size();
15445 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
15446 size *= 8;
15447
15448 // And the size of the pointer must be the same as the address size
15449 // of the current translation unit.
15450 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
15451
15452 bool is_lvalue = tag == DW_TAG_reference_type;
15453
15454 result.reset(new reference_type_def(utype, is_lvalue, size,
15455 /*alignment=*/0,
15456 location()));
15457 if (corpus_sptr corp = rdr.corpus())
15458 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
15459 result = t;
15460 rdr.associate_die_to_type(die, result, where_offset);
15461 return result;
15462}
15463
15464/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
15465/// DW_TAG_ptr_to_member_type.
15466///
15467/// @param the DWARF reader touse.
15468///
15469/// @param the DIE to consider. It must carry the tag
15470/// DW_TAG_ptr_to_member_type.
15471///
15472/// @param called_from_public_decl true if this function was called
15473/// from a context where either a public function or a public variable
15474/// is being built.
15475///
15476/// @param where_offset the offset of the DIE where we are "logically"
15477/// positionned at, in the DIE tree. This is useful when @p die is
15478/// e.g, DW_TAG_partial_unit that can be included in several places in
15479/// the DIE tree.
15480///
15481/// @return a pointer to the resulting @ref ptr_to_mbr_type.
15483build_ptr_to_mbr_type(reader& rdr,
15484 Dwarf_Die* die,
15485 bool called_from_public_decl,
15486 size_t where_offset)
15487{
15488 ptr_to_mbr_type_sptr result;
15489
15490 if (!die)
15491 return result;
15492
15493 unsigned tag = dwarf_tag(die);
15494 if (tag != DW_TAG_ptr_to_member_type)
15495 return result;
15496
15497 Dwarf_Die data_member_type_die, containing_type_die;
15498
15499 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
15500 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
15501 return result;
15502
15503 type_or_decl_base_sptr data_member_type =
15504 build_ir_node_from_die(rdr, &data_member_type_die,
15505 called_from_public_decl, where_offset);
15506 if (!data_member_type)
15507 return result;
15508
15509 type_or_decl_base_sptr containing_type =
15510 build_ir_node_from_die(rdr, &containing_type_die,
15511 called_from_public_decl, where_offset);
15512 if (!containing_type)
15513 return result;
15514
15516 (is_type(containing_type)))
15517 return result;
15518
15519 if (type_base_sptr t = rdr.lookup_type_from_die(die))
15520 {
15521 result = is_ptr_to_mbr_type(t);
15522 ABG_ASSERT(result);
15523 return result;
15524 }
15525
15526 uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
15527
15528 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
15529 is_type(data_member_type),
15530 is_type(containing_type),
15531 size_in_bits,
15532 /*alignment=*/0,
15533 location()));
15534
15535 rdr.associate_die_to_type(die, result, where_offset);
15536 return result;
15537}
15538
15539/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
15540///
15541/// @param rdr the DWARF reader to consider.
15542///
15543/// @param die the DIE to read from.
15544///
15545/// @param is_method points to a class or union declaration iff we're
15546/// building the type for a method. This is the enclosing class or
15547/// union of the method.
15548///
15549/// @param where_offset the offset of the DIE where we are "logically"
15550/// positioned 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 function_type_sptr.
15555static function_type_sptr
15556build_function_type(reader& rdr,
15557 Dwarf_Die* die,
15558 class_or_union_sptr is_method,
15559 size_t where_offset)
15560{
15561 function_type_sptr result;
15562
15563 if (!die)
15564 return result;
15565
15566 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
15567 || dwarf_tag(die) == DW_TAG_subprogram);
15568
15569 const die_source source = rdr.get_die_source(die);
15570
15571 {
15572 size_t off = dwarf_dieoffset(die);
15573 auto i = rdr.die_wip_function_types_map(source).find(off);
15574 if (i != rdr.die_wip_function_types_map(source).end())
15575 {
15576 function_type_sptr fn_type = is_function_type(i->second);
15577 ABG_ASSERT(fn_type);
15578 return fn_type;
15579 }
15580 }
15581
15582 decl_base_sptr type_decl;
15583
15584 translation_unit_sptr tu = rdr.cur_transl_unit();
15585 ABG_ASSERT(tu);
15586
15587 /// If, inside the current translation unit, we've already seen a
15588 /// function type with the same text representation, then reuse that
15589 /// one instead.
15590 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
15591 {
15592 result = is_function_type(t);
15593 ABG_ASSERT(result);
15594 rdr.associate_die_to_type(die, result, where_offset);
15595 return result;
15596 }
15597
15598 bool odr_is_relevant = rdr.odr_is_relevant(die);
15599 if (odr_is_relevant)
15600 {
15601 // So we can rely on the One Definition Rule to say that if
15602 // several different function types have the same name (or
15603 // rather, representation) across the entire binary, then they
15604 // ought to designate the same function type. So let's ensure
15605 // that if we've already seen a function type with the same
15606 // representation as the function type 'die', then it's the same
15607 // type as the one denoted by 'die'.
15608 if (function_type_sptr fn_type =
15609 is_function_type(rdr.lookup_type_artifact_from_die(die)))
15610 {
15611 rdr.associate_die_to_type(die, fn_type, where_offset);
15612 return fn_type;
15613 }
15614 }
15615
15616 // Let's look at the DIE to detect if it's the DIE for a method
15617 // (type). If it is, we can deduce the name of its enclosing class
15618 // and if it's a static or const.
15619 bool is_const = false;
15620 bool is_static = false;
15621 Dwarf_Die object_pointer_die;
15622 Dwarf_Die class_type_die;
15623 bool has_this_parm_die =
15624 die_function_type_is_method_type(rdr, die, where_offset,
15625 object_pointer_die,
15626 class_type_die,
15627 is_static);
15628 if (has_this_parm_die)
15629 {
15630 // The function (type) has a "this" parameter DIE. It means it's
15631 // a member function DIE.
15632 if (!is_static)
15633 if (die_object_pointer_is_for_const_method(&object_pointer_die))
15634 is_const = true;
15635
15636 if (!is_method)
15637 {
15638 // We were initially called as if the function represented
15639 // by DIE was *NOT* a member function. But now we know it's
15640 // a member function. Let's take that into account.
15641 class_or_union_sptr klass_type =
15642 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
15643 /*called_from_pub_decl=*/true,
15644 where_offset));
15645 if (!klass_type)
15646 {
15647 // We could not create the class type. For instance,
15648 // this can be due to the fact that the class is
15649 // suppressed. In those cases, we just bail out.
15650 return nullptr;
15651 }
15652 is_method = klass_type;
15653 }
15654 }
15655
15656 // Let's create the type early and record it as being for the DIE
15657 // 'die'. This way, when building the sub-type triggers the
15658 // creation of a type matching the same 'die', then we'll reuse this
15659 // one.
15660
15661 result.reset(is_method
15662 ? new method_type(is_method, is_const,
15663 tu->get_address_size(),
15664 /*alignment=*/0)
15665 : new function_type(rdr.env(), tu->get_address_size(),
15666 /*alignment=*/0));
15667 rdr.associate_die_to_type(die, result, where_offset);
15668 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
15669
15670 type_base_sptr return_type;
15671 Dwarf_Die ret_type_die;
15672 if (die_die_attribute(die, DW_AT_type, ret_type_die))
15673 return_type =
15674 is_type(build_ir_node_from_die(rdr, &ret_type_die,
15675 /*called_from_public_decl=*/true,
15676 where_offset));
15677 if (!return_type)
15678 return_type = is_type(build_ir_node_for_void_type(rdr));
15679 result->set_return_type(return_type);
15680
15681 Dwarf_Die child;
15682 function_decl::parameters function_parms;
15683
15684 if (dwarf_child(die, &child) == 0)
15685 do
15686 {
15687 int child_tag = dwarf_tag(&child);
15688 if (child_tag == DW_TAG_formal_parameter)
15689 {
15690 // This is a "normal" function parameter.
15691 string name, linkage_name;
15692 location loc;
15693 die_loc_and_name(rdr, &child, loc, name, linkage_name);
15695 // Sometimes, bogus compiler emit names that are
15696 // non-ascii garbage. Let's just ditch that for now.
15697 name.clear();
15698 bool is_artificial = die_is_artificial(&child);
15699 type_base_sptr parm_type;
15700 Dwarf_Die parm_type_die;
15701 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
15702 parm_type =
15703 is_type(build_ir_node_from_die(rdr, &parm_type_die,
15704 /*called_from_public_decl=*/true,
15705 where_offset));
15706 if (!parm_type)
15707 continue;
15708 if (is_method
15709 && is_const_qualified_type(parm_type)
15710 && function_parms.empty())
15711 // We are looking at the first (implicit) parameter of a
15712 // method. This is basically the "this pointer". For
15713 // concrete instances of abstract methods, GCC sometimes
15714 // represents that pointer as a const pointer, whereas
15715 // in the abstract interface representing that method
15716 // the this-pointer is represented as a non-qualified
15717 // pointer. Let's trim the const qualifier away. That
15718 // will minize the chance to have spurious
15719 // const-qualifier changes on implicit parameters when
15720 // comparing methods that otherwise have no meaningful
15721 // ABI changes.
15722 parm_type =
15724
15726 (new function_decl::parameter(parm_type, name, loc,
15727 /*variadic_marker=*/false,
15728 is_artificial));
15729 function_parms.push_back(p);
15730 }
15731 else if (child_tag == DW_TAG_unspecified_parameters)
15732 {
15733 // This is a variadic function parameter.
15734 bool is_artificial = die_is_artificial(&child);
15735
15736 type_base_sptr parm_type =
15737 is_type(build_ir_node_for_variadic_parameter_type(rdr));
15739 (new function_decl::parameter(parm_type,
15740 /*name=*/"",
15741 location(),
15742 /*variadic_marker=*/true,
15743 is_artificial));
15744 function_parms.push_back(p);
15745 // After a DW_TAG_unspecified_parameters tag, we shouldn't
15746 // keep reading for parameters. The
15747 // unspecified_parameters TAG should be the last parameter
15748 // that we record. For instance, if there are multiple
15749 // DW_TAG_unspecified_parameters DIEs then we should care
15750 // only for the first one.
15751 break;
15752 }
15753 }
15754 while (dwarf_siblingof(&child, &child) == 0);
15755
15756 result->set_parameters(function_parms);
15757
15758 tu->bind_function_type_life_time(result);
15759
15760 result->set_is_artificial(true);
15761
15762 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
15763
15764 {
15765 die_function_type_map_type::const_iterator i =
15766 rdr.die_wip_function_types_map(source).
15767 find(dwarf_dieoffset(die));
15768 if (i != rdr.die_wip_function_types_map(source).end())
15769 rdr.die_wip_function_types_map(source).erase(i);
15770 }
15771
15772 maybe_canonicalize_type(result, rdr);
15773 return result;
15774}
15775
15776/// Build a subrange type from a DW_TAG_subrange_type.
15777///
15778/// @param rdr the DWARF reader to consider.
15779///
15780/// @param die the DIE to read from.
15781///
15782/// @param where_offset the offset of the DIE where we are "logically"
15783/// positionned at in the DIE tree. This is useful when @p die is
15784/// e,g, DW_TAG_partial_unit that can be included in several places in
15785/// the DIE tree.
15786///
15787/// @param associate_die_to_type if this is true then the resulting
15788/// type is associated to the @p die, so that next time when the
15789/// system looks up the type associated to it, the current resulting
15790/// type is returned. If false, then no association is done and the
15791/// resulting type can be destroyed right after. This can be useful
15792/// when the sole purpose of building the @ref
15793/// array_type_def::subrange_type is to use some of its method like,
15794/// e.g, its name pretty printing methods.
15795///
15796/// @return the newly built instance of @ref
15797/// array_type_def::subrange_type, or nil if no type could be built.
15799build_subrange_type(reader& rdr,
15800 const Dwarf_Die* die,
15801 size_t where_offset,
15802 bool associate_type_to_die)
15803{
15805
15806 if (!die)
15807 return result;
15808
15809 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
15810 if (tag != DW_TAG_subrange_type)
15811 return result;
15812
15813 string name = die_name(die);
15814
15815 // load the underlying type.
15816 Dwarf_Die underlying_type_die;
15817 type_base_sptr underlying_type;
15818 /* Unless there is an underlying type which says differently. */
15819 bool is_signed = false;
15820 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
15821 underlying_type =
15822 is_type(build_ir_node_from_die(rdr,
15823 &underlying_type_die,
15824 /*called_from_public_decl=*/true,
15825 where_offset));
15826
15827 if (underlying_type)
15828 {
15829 uint64_t ate;
15830 if (die_unsigned_constant_attribute (&underlying_type_die,
15831 DW_AT_encoding,
15832 ate))
15833 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
15834 }
15835
15836 // The DW_TAG_subrange_type DIE may have some size related
15837 // attributes (DW_AT_byte_size or DW_AT_bit_size). If not, then the
15838 // size is deduced from the size of its underlying type.
15839 bool has_size_info = false;
15840 uint64_t size = 0;
15841 if ((has_size_info = die_unsigned_constant_attribute(die,
15842 DW_AT_byte_size, size)))
15843 size *= 8;
15844 else
15845 has_size_info = die_unsigned_constant_attribute(die,
15846 DW_AT_bit_size, size);
15847
15848 translation_unit::language language = rdr.cur_transl_unit()->get_language();
15849 array_type_def::subrange_type::bound_value lower_bound =
15850 get_default_array_lower_bound(language);
15851 array_type_def::subrange_type::bound_value upper_bound;
15852 uint64_t count = 0;
15853 bool is_non_finite = false;
15854 bool non_zero_count_present = false;
15855
15856 // The DWARF 4 specifications says, in [5.11 Subrange
15857 // Type Entries]:
15858 //
15859 // The subrange entry may have the attributes
15860 // DW_AT_lower_bound and DW_AT_upper_bound to
15861 // specify, respectively, the lower and upper bound
15862 // values of the subrange.
15863 //
15864 // So let's look for DW_AT_lower_bound first.
15865 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
15866
15867 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
15868 is_signed, upper_bound);
15869 if (!found_upper_bound)
15870 found_upper_bound = subrange_die_indirect_bound_value(die,
15871 DW_AT_upper_bound,
15872 upper_bound,
15873 is_signed);
15874 // Then, DW_AT_upper_bound.
15875 if (!found_upper_bound)
15876 {
15877 // The DWARF 4 spec says, in [5.11 Subrange Type
15878 // Entries]:
15879 //
15880 // The DW_AT_upper_bound attribute may be replaced
15881 // by a DW_AT_count attribute, whose value
15882 // describes the number of elements in the
15883 // subrange rather than the value of the last
15884 // element."
15885 //
15886 // So, as DW_AT_upper_bound is not present in this
15887 // case, let's see if there is a DW_AT_count.
15888 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
15889 {
15890 if (count)
15891 // DW_AT_count can be present and be set to zero. This is
15892 // for instance the case to model this gcc extension to
15893 // represent flexible arrays:
15894 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
15895 // For instance: int flex_array[0];
15896 non_zero_count_present = true;
15897
15898 // When the count is present and non-zero, we can deduce the
15899 // upper_bound from the lower_bound and the number of
15900 // elements of the array:
15901 int64_t u = lower_bound.get_signed_value() + count;
15902 if (u)
15903 upper_bound = u - 1;
15904 }
15905
15906 if (!non_zero_count_present)
15907 // No upper_bound nor count was present on the DIE, this means
15908 // the array is considered to have an infinite (or rather not
15909 // known) size.
15910 is_non_finite = true;
15911 }
15912
15913 if (UINT64_MAX == upper_bound.get_unsigned_value())
15914 // If the upper_bound size is the max of the integer value
15915 // then it most certainly means unknown size.
15916 is_non_finite = true;
15917
15918 result.reset
15919 (new array_type_def::subrange_type(rdr.env(),
15920 name,
15921 lower_bound,
15922 upper_bound,
15923 underlying_type,
15924 location()));
15925 result->is_non_finite(is_non_finite);
15926
15927 if (has_size_info)
15928 result->set_size_in_bits(size);
15929 else
15930 {
15931 // The DW_TAG_subrange_type doesn't appear to have any size
15932 // attribute. In that case, the size is deduced from the size
15933 // of the underlying type. If there is no underlying type
15934 // specified, then the size of the subrange type is the size
15935 if (!underlying_type)
15936 result->set_size_in_bits(rdr.cur_transl_unit()->get_address_size());
15937 }
15938
15939 // Let's ensure the resulting subrange looks metabolically healthy.
15940 ABG_ASSERT(result->is_non_finite()
15941 || (result->get_length() ==
15942 (uint64_t) (result->get_upper_bound()
15943 - result->get_lower_bound() + 1)));
15944
15945 if (associate_type_to_die)
15946 rdr.associate_die_to_type(die, result, where_offset);
15947
15948 return result;
15949}
15950
15951/// Build the sub-ranges of an array type.
15952///
15953/// This is a sub-routine of build_array_type().
15954///
15955/// @param rdr the context to read from.
15956///
15957/// @param die the DIE of tag DW_TAG_array_type which contains
15958/// children DIEs that represent the sub-ranges.
15959///
15960/// @param subranges out parameter. This is set to the sub-ranges
15961/// that are built from @p die.
15962///
15963/// @param where_offset the offset of the DIE where we are "logically"
15964/// positioned at, in the DIE tree. This is useful when @p die is
15965/// e.g, DW_TAG_partial_unit that can be included in several places in
15966/// the DIE tree.
15967static void
15968build_subranges_from_array_type_die(const reader& rdr,
15969 const Dwarf_Die* die,
15971 size_t where_offset,
15972 bool associate_type_to_die)
15973{
15974 Dwarf_Die child;
15975
15976 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
15977 {
15978 do
15979 {
15980 int child_tag = dwarf_tag(&child);
15981 if (child_tag == DW_TAG_subrange_type)
15982 {
15984 if (associate_type_to_die)
15985 {
15986 // We are being called to create the type, add it to
15987 // the current type graph and associate it to the
15988 // DIE it's been created from.
15990 build_ir_node_from_die(const_cast<reader&>(rdr), &child,
15991 /*called_from_public_decl=*/true,
15992 where_offset);
15993 s = is_subrange_type(t);
15994 }
15995 else
15996 // We are being called to create the type but *NOT*
15997 // add it to the current tyupe tree, *NOR* associate
15998 // it to the DIE it's been created from.
15999 s = build_subrange_type(const_cast<reader&>(rdr), &child,
16000 where_offset,
16001 /*associate_type_to_die=*/false);
16002 if (s)
16003 subranges.push_back(s);
16004 }
16005 }
16006 while (dwarf_siblingof(&child, &child) == 0);
16007 }
16008}
16009
16010/// Build an array type from a DW_TAG_array_type DIE.
16011///
16012/// @param rdr the DWARF reader to consider.
16013///
16014/// @param die the DIE to read from.
16015///
16016/// @param called_from_public_decl true if this function was called
16017/// from a context where either a public function or a public variable
16018/// is being built.
16019///
16020/// @param where_offset the offset of the DIE where we are "logically"
16021/// positioned at, in the DIE tree. This is useful when @p die is
16022/// e.g, DW_TAG_partial_unit that can be included in several places in
16023/// the DIE tree.
16024///
16025/// @return a pointer to the resulting array_type_def.
16027build_array_type(reader& rdr,
16028 Dwarf_Die* die,
16029 bool called_from_public_decl,
16030 size_t where_offset)
16031{
16032 array_type_def_sptr result;
16033
16034 if (!die)
16035 return result;
16036
16037 unsigned tag = dwarf_tag(die);
16038 if (tag != DW_TAG_array_type)
16039 return result;
16040
16041 decl_base_sptr type_decl;
16042 Dwarf_Die type_die;
16043
16044 if (die_die_attribute(die, DW_AT_type, type_die))
16045 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
16046 called_from_public_decl,
16047 where_offset));
16048 if (!type_decl)
16049 return result;
16050
16051 // The call to build_ir_node_from_die() could have triggered the
16052 // creation of the type for this DIE. In that case, just return it.
16053 if (type_base_sptr t = rdr.lookup_type_from_die(die))
16054 {
16055 result = is_array_type(t);
16056 ABG_ASSERT(result);
16057 return result;
16058 }
16059
16060 type_base_sptr type = is_type(type_decl);
16061 ABG_ASSERT(type);
16062
16064
16065 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
16066
16067 result.reset(new array_type_def(type, subranges, location()));
16068 rdr.associate_die_to_type(die, result, where_offset);
16069 return result;
16070}
16071
16072/// Create a typedef_decl from a DW_TAG_typedef DIE.
16073///
16074/// @param rdr the DWARF reader to consider.
16075///
16076/// @param die the DIE to read from.
16077///
16078/// @param called_from_public_decl true if this function was called
16079/// from a context where either a public function or a public variable
16080/// is being built.
16081///
16082/// @param where_offset the offset of the DIE where we are "logically"
16083/// positionned at, in the DIE tree. This is useful when @p die is
16084/// e.g, DW_TAG_partial_unit that can be included in several places in
16085/// the DIE tree.
16086///
16087/// @return the newly created typedef_decl.
16088static typedef_decl_sptr
16089build_typedef_type(reader& rdr,
16090 Dwarf_Die* die,
16091 bool called_from_public_decl,
16092 size_t where_offset)
16093{
16094 typedef_decl_sptr result;
16095
16096 if (!die)
16097 return result;
16098
16099 unsigned tag = dwarf_tag(die);
16100 if (tag != DW_TAG_typedef)
16101 return result;
16102
16103 string name, linkage_name;
16104 location loc;
16105 die_loc_and_name(rdr, die, loc, name, linkage_name);
16106
16107 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
16108 if (loc)
16109 result = lookup_typedef_type_per_location(loc.expand(), *corp);
16110
16111 if (!result)
16112 {
16113 type_base_sptr utype;
16114 Dwarf_Die underlying_type_die;
16115 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
16116 // A typedef DIE with no underlying type means a typedef to
16117 // void type.
16118 utype = rdr.env().get_void_type();
16119
16120 if (!utype)
16121 utype =
16122 is_type(build_ir_node_from_die(rdr,
16123 &underlying_type_die,
16124 called_from_public_decl,
16125 where_offset));
16126 if (!utype)
16127 return result;
16128
16129 ABG_ASSERT(utype);
16130 result.reset(new typedef_decl(name, utype, loc, linkage_name));
16131
16132 if ((is_class_or_union_type(utype) || is_enum_type(utype))
16133 && is_anonymous_type(utype))
16134 {
16135 // This is a naming typedef for an enum or a class. Let's
16136 // mark the underlying decl as such.
16137 decl_base_sptr decl = is_decl(utype);
16138 ABG_ASSERT(decl);
16139 decl->set_naming_typedef(result);
16140 rdr.maybe_schedule_decl_only_type_for_resolution(utype);
16141 }
16142 }
16143
16144 rdr.associate_die_to_type(die, result, where_offset);
16145
16146 return result;
16147}
16148
16149/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
16150/// denoted by the DIE is not suppressed by a suppression
16151/// specification associated to the current DWARF reader.
16152///
16153/// Note that if a member variable declaration with the same name as
16154/// the name of the DIE we are looking at exists, this function returns
16155/// that existing variable declaration.
16156///
16157/// @param rdr the DWARF reader to use.
16158///
16159/// @param die the DIE representing the variable we are looking at.
16160///
16161/// @param where_offset the offset of the DIE where we are "logically"
16162/// positionned at, in the DIE tree. This is useful when @p die is
16163/// e.g, DW_TAG_partial_unit that can be included in several places in
16164/// the DIE tree.
16165///
16166/// @param is_declaration_only if true, it means the variable DIE has
16167/// the is_declaration_only only attribute.
16168///
16169/// @param result if this is set to an existing var_decl, this means
16170/// that the function will append the new properties it sees on @p die
16171/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16172/// new var_decl is going to be allocated and returned.
16173///
16174/// @param is_required_decl_spec this is true iff the variable to
16175/// build is referred to as being the specification of another
16176/// variable.
16177///
16178/// @return a pointer to the newly created var_decl. If the var_decl
16179/// could not be built, this function returns NULL.
16180static var_decl_sptr
16181build_or_get_var_decl_if_not_suppressed(reader& rdr,
16182 scope_decl *scope,
16183 Dwarf_Die *die,
16184 size_t where_offset,
16185 bool is_declaration_only,
16186 var_decl_sptr result,
16187 bool is_required_decl_spec)
16188{
16189 var_decl_sptr var;
16190 if (variable_is_suppressed(rdr, scope, die,
16191 is_declaration_only,
16192 is_required_decl_spec))
16193 {
16194 ++rdr.stats_.number_of_suppressed_variables;
16195 return var;
16196 }
16197
16198 if (class_decl* class_type = is_class_type(scope))
16199 {
16200 string var_name = die_name(die);
16201 if (!var_name.empty())
16202 if ((var = class_type->find_data_member(var_name)))
16203 return var;
16204 }
16205
16206 // The variable was not suppressed.
16207 ++rdr.stats_.number_of_suppressed_variables;
16208
16209 var = build_var_decl(rdr, die, where_offset, result);
16210 return var;
16211}
16212
16213/// Build a @ref var_decl out of a DW_TAG_variable DIE.
16214///
16215/// @param rdr the DWARF reader to use.
16216///
16217/// @param die the DIE representing the variable we are looking at.
16218///
16219/// @param where_offset the offset of the DIE where we are "logically"
16220/// positionned at, in the DIE tree. This is useful when @p die is
16221/// e.g, DW_TAG_partial_unit that can be included in several places in
16222/// the DIE tree.
16223///
16224/// @param result if this is set to an existing var_decl, this means
16225/// that the function will append the new properties it sees on @p die
16226/// to that exising var_decl. Otherwise, if this parameter is NULL, a
16227/// new var_decl is going to be allocated and returned.
16228///
16229/// @return a pointer to the newly created var_decl. If the var_decl
16230/// could not be built, this function returns NULL.
16231static var_decl_sptr
16232build_var_decl(reader& rdr,
16233 Dwarf_Die *die,
16234 size_t where_offset,
16235 var_decl_sptr result)
16236{
16237 if (!die)
16238 return result;
16239
16240 int tag = dwarf_tag(die);
16241 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
16242
16243 if (!die_is_public_decl(die))
16244 return result;
16245
16246 type_base_sptr type;
16247 Dwarf_Die type_die;
16248 if (die_die_attribute(die, DW_AT_type, type_die))
16249 {
16250 decl_base_sptr ty =
16251 is_decl(build_ir_node_from_die(rdr, &type_die,
16252 /*called_from_public_decl=*/true,
16253 where_offset));
16254 if (!ty)
16255 return result;
16256 type = is_type(ty);
16257 ABG_ASSERT(type);
16258 }
16259
16260 if (!type && !result)
16261 return result;
16262
16263 string name, linkage_name;
16264 location loc;
16265 die_loc_and_name(rdr, die, loc, name, linkage_name);
16266
16267 if (!result)
16268 result.reset(new var_decl(name, type, loc, linkage_name));
16269 else
16270 {
16271 // We were called to append properties that might have been
16272 // missing from the first version of the variable. And usually
16273 // that missing property is the mangled name or the type.
16274 if (!linkage_name.empty())
16275 result->set_linkage_name(linkage_name);
16276
16277 if (type)
16278 result->set_type(type);
16279 }
16280
16281 // Check if a variable symbol with this name is exported by the elf
16282 // binary. If it is, then set the symbol of the variable, if it's
16283 // not set already.
16284 if (!result->get_symbol())
16285 {
16286 elf_symbol_sptr var_sym;
16287 Dwarf_Addr var_addr;
16288
16289 if (rdr.get_variable_address(die, var_addr))
16290 {
16291 rdr.symtab()->
16292 update_main_symbol(var_addr,
16293 result->get_linkage_name().empty()
16294 ? result->get_name()
16295 : result->get_linkage_name());
16296 var_sym = rdr.variable_symbol_is_exported(var_addr);
16297 }
16298
16299 if (var_sym)
16300 {
16301 result->set_symbol(var_sym);
16302 // If the linkage name is not set or is wrong, set it to
16303 // the name of the underlying symbol.
16304 string linkage_name = result->get_linkage_name();
16305 if (linkage_name.empty()
16306 || !var_sym->get_alias_from_name(linkage_name))
16307 result->set_linkage_name(var_sym->get_name());
16308 result->set_is_in_public_symbol_table(true);
16309 }
16310
16311 if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
16312 {
16313 // We are looking at a global variable which symbol is
16314 // undefined. Let's set its symbol.
16315 string n = result->get_linkage_name();
16316 if (n.empty())
16317 n = result->get_name();
16318 var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
16319 if (var_sym)
16320 {
16321 result->set_symbol(var_sym);
16322 result->set_is_in_public_symbol_table(false);
16323 }
16324 }
16325 }
16326
16327 return result;
16328}
16329
16330/// Test if a given function denoted by its DIE and its scope is
16331/// suppressed by any of the suppression specifications associated to
16332/// a given context of ELF/DWARF reading.
16333///
16334/// Note that a non-member function which symbol is not exported is
16335/// also suppressed.
16336///
16337/// @param rdr the ELF/DWARF reading content of interest.
16338///
16339/// @param scope of the scope of the function.
16340///
16341/// @param function_die the DIE representing the function.
16342///
16343/// @param is_declaration_only is true if the DIE denoted by @p die is
16344/// a declaration-only DIE.
16345///
16346/// @return true iff @p function_die is suppressed by at least one
16347/// suppression specification attached to the @p rdr.
16348static bool
16349function_is_suppressed(const reader& rdr,
16350 const scope_decl* scope,
16351 Dwarf_Die *function_die,
16352 bool is_declaration_only)
16353{
16354 if (function_die == 0
16355 || dwarf_tag(function_die) != DW_TAG_subprogram)
16356 return false;
16357
16358 string fname = die_string_attribute(function_die, DW_AT_name);
16359 string flinkage_name = die_linkage_name(function_die);
16360 if (flinkage_name.empty() && die_is_in_c(function_die))
16361 flinkage_name = fname;
16362 string qualified_name = build_qualified_name(scope, fname);
16363
16364 // A non-member non-static function which symbol is not exported is
16365 // suppressed.
16366 //
16367 // Note that if the non-member non-static function has an undefined
16368 // symbol, by default, it's not suppressed. Unless we are asked to
16369 // drop undefined symbols too.
16370 if (!is_class_type(scope)
16371 && (!is_declaration_only || rdr.drop_undefined_syms()))
16372 {
16373 Dwarf_Addr fn_addr;
16374 if (!rdr.get_function_address(function_die, fn_addr))
16375 return true;
16376
16377 elf_symbol_sptr symbol =
16378 rdr.function_symbol_is_exported(fn_addr);
16379 if (!symbol)
16380 return true;
16381 if (symbol->is_suppressed())
16382 return true;
16383
16384 // Since there is only one symbol in DWARF associated with an elf_symbol,
16385 // we can assume this is the main symbol then. Otherwise the main hinting
16386 // did not work as expected.
16387 ABG_ASSERT(symbol->is_main_symbol());
16388 if (symbol->has_aliases())
16389 for (elf_symbol_sptr a = symbol->get_next_alias();
16390 !a->is_main_symbol(); a = a->get_next_alias())
16391 if (a->is_suppressed())
16392 return true;
16393 }
16394
16395 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
16396 /*require_drop_property=*/true);
16397}
16398
16399/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
16400/// function denoted by the DIE is not suppressed by a suppression
16401/// specification associated to the current DWARF reader.
16402///
16403/// Note that if a member function declaration with the same signature
16404/// (pretty representation) as one of the DIE we are looking at
16405/// exists, this function returns that existing function declaration.
16406/// Similarly, if there is already a constructed member function with
16407/// the same linkage name as the one on the DIE, this function returns
16408/// that member function.
16409///
16410/// Also note that the function_decl IR returned by this function must
16411/// be passed to finish_member_function_reading because several
16412/// properties from the DIE are actually read by that function, and
16413/// the corresponding properties on the function_decl IR are updated
16414/// accordingly. This is done to support "updating" a function_decl
16415/// IR with properties scathered across several DIEs.
16416///
16417/// @param rdr the DWARF reader to use.
16418///
16419/// @param scope the scope of the function we are looking at.
16420///
16421/// @param fn_die the DIE representing the function we are looking at.
16422///
16423/// @param where_offset the offset of the DIE where we are "logically"
16424/// positionned at, in the DIE tree. This is useful when @p die is
16425/// e.g, DW_TAG_partial_unit that can be included in several places in
16426/// the DIE tree.
16427///
16428/// @param is_declaration_only is true if the DIE denoted by @p fn_die
16429/// is a declaration-only DIE.
16430///
16431/// @param result if this is set to an existing function_decl, this
16432/// means that the function will append the new properties it sees on
16433/// @p fn_die to that exising function_decl. Otherwise, if this
16434/// parameter is NULL, a new function_decl is going to be allocated
16435/// and returned.
16436///
16437/// @return a pointer to the newly created var_decl. If the var_decl
16438/// could not be built, this function returns NULL.
16439static function_decl_sptr
16440build_or_get_fn_decl_if_not_suppressed(reader& rdr,
16441 scope_decl *scope,
16442 Dwarf_Die *fn_die,
16443 size_t where_offset,
16444 bool is_declaration_only,
16445 function_decl_sptr result)
16446{
16448 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
16449 {
16450 ++rdr.stats_.number_of_suppressed_functions;
16451 return fn;
16452 }
16453
16454 string name = die_name(fn_die);
16455 string linkage_name = die_linkage_name(fn_die);
16456 bool is_dtor = !name.empty() && name[0]== '~';
16457 bool is_virtual = false;
16458 if (is_dtor)
16459 {
16460 Dwarf_Attribute attr;
16461 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
16462 DW_AT_vtable_elem_location,
16463 &attr))
16464 is_virtual = true;
16465 }
16466
16467
16468 // If we've already built an IR for a function with the same
16469 // signature (from another DIE), reuse it, unless that function is a
16470 // virtual C++ destructor. Several virtual C++ destructors with the
16471 // same signature can be implemented by several different ELF
16472 // symbols. So re-using C++ destructors like that can lead to us
16473 // missing some destructors.
16474 if (!result && (!(is_dtor && is_virtual)))
16475 {
16476 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
16477 {
16478 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
16479 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
16480 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
16481 return fn;
16482 }
16483 }
16484
16485 // The function was not suppressed.
16486 ++rdr.stats_.number_of_allowed_functions;
16487
16488 // If a member function with the same linkage name as the one
16489 // carried by the DIE already exists, then return it.
16490 if (class_decl* klass = is_class_type(scope))
16491 {
16492 string linkage_name = die_linkage_name(fn_die);
16493 fn = klass->find_member_function_sptr(linkage_name);
16494 if (fn)
16495 // We found a member function that has the same signature.
16496 // Let's mark it for update.
16497 result = fn;
16498 }
16499
16500 if (!fn || !fn->get_symbol())
16501 // We haven't yet been able to construct a function IR, or, we
16502 // have one 'partial' function IR that doesn't have any associated
16503 // symbol yet. Note that in the later case, a function IR without
16504 // any associated symbol will be dropped on the floor by
16505 // potential_member_fn_should_be_dropped. So let's build or a new
16506 // function IR or complete the existing partial IR.
16507 fn = build_function_decl(rdr, fn_die, where_offset, result);
16508
16509 return fn;
16510}
16511
16512/// Test if a given variable denoted by its DIE and its scope is
16513/// suppressed by any of the suppression specifications associated to
16514/// a given context of ELF/DWARF reading.
16515///
16516/// @param rdr the ELF/DWARF reading content of interest.
16517///
16518/// @param scope of the scope of the variable.
16519///
16520/// @param variable_die the DIE representing the variable.
16521///
16522/// @param is_declaration_only true if the variable is supposed to be
16523/// decl-only.
16524///
16525/// @param is_required_decl_spec if true, means that the @p
16526/// variable_die being considered is for a variable decl that is a
16527/// specification for a concrete variable being built.
16528///
16529/// @return true iff @p variable_die is suppressed by at least one
16530/// suppression specification attached to the @p rdr.
16531static bool
16532variable_is_suppressed(const reader& rdr,
16533 const scope_decl* scope,
16534 Dwarf_Die *variable_die,
16535 bool is_declaration_only,
16536 bool is_required_decl_spec)
16537{
16538 if (variable_die == 0
16539 || (dwarf_tag(variable_die) != DW_TAG_variable
16540 && dwarf_tag(variable_die) != DW_TAG_member))
16541 return false;
16542
16543 string name = die_string_attribute(variable_die, DW_AT_name);
16544 string linkage_name = die_linkage_name(variable_die);
16545 if (linkage_name.empty() && die_is_in_c(variable_die))
16546 linkage_name = name;
16547 string qualified_name = build_qualified_name(scope, name);
16548
16549 // If a non member variable that is a declaration (has no defined
16550 // and exported symbol) and is not the specification of another
16551 // concrete variable, then it's suppressed. This is a size
16552 // optimization; it removes useless declaration-only variables from
16553 // the IR.
16554 if (!is_class_type(scope)
16555 && !is_required_decl_spec
16556 // If we are asked to load undefined interfaces, then we don't
16557 // suppress declaration-only variables as they might have
16558 // undefined elf-symbols.
16559 && (!is_declaration_only || !rdr.load_undefined_interfaces()))
16560 {
16561 Dwarf_Addr var_addr = 0;
16562 if (!rdr.get_variable_address(variable_die, var_addr))
16563 return true;
16564
16565 elf_symbol_sptr symbol =
16566 rdr.variable_symbol_is_exported(var_addr);
16567 if (!symbol)
16568 return true;
16569 if (symbol->is_suppressed())
16570 return true;
16571
16572 // Since there is only one symbol in DWARF associated with an elf_symbol,
16573 // we can assume this is the main symbol then. Otherwise the main hinting
16574 // did not work as expected.
16575 ABG_ASSERT(symbol->is_main_symbol());
16576 if (symbol->has_aliases())
16577 for (elf_symbol_sptr a = symbol->get_next_alias();
16578 !a->is_main_symbol(); a = a->get_next_alias())
16579 if (a->is_suppressed())
16580 return true;
16581 }
16582
16584 qualified_name,
16585 linkage_name,
16586 /*require_drop_property=*/true);
16587}
16588
16589/// Test if a type (designated by a given DIE) in a given scope is
16590/// suppressed by the suppression specifications that are associated
16591/// to a given DWARF reader.
16592///
16593/// @param rdr the DWARF reader to consider.
16594///
16595/// @param scope of the scope of the type DIE to consider.
16596///
16597/// @param type_die the DIE that designates the type to consider.
16598///
16599/// @param type_is_opaque out parameter. If this function returns
16600/// true (the type @p type_die is suppressed) and if the type was
16601/// suppressed because it's opaque then this parameter is set to
16602/// true.
16603///
16604/// @return true iff the type designated by the DIE @p type_die, in
16605/// the scope @p scope is suppressed by at the suppression
16606/// specifications associated to the current DWARF reader.
16607static bool
16608type_is_suppressed(const reader& rdr,
16609 const scope_decl* scope,
16610 Dwarf_Die *type_die,
16611 bool &type_is_opaque)
16612{
16613 if (type_die == 0
16614 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
16615 && dwarf_tag(type_die) != DW_TAG_class_type
16616 && dwarf_tag(type_die) != DW_TAG_structure_type
16617 && dwarf_tag(type_die) != DW_TAG_union_type))
16618 return false;
16619
16620 string type_name, linkage_name;
16621 location type_location;
16622 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16623 string qualified_name = build_qualified_name(scope, type_name);
16624
16625 return suppr::is_type_suppressed(rdr,
16626 qualified_name,
16627 type_location,
16628 type_is_opaque,
16629 /*require_drop_property=*/true);
16630}
16631
16632/// Test if a type (designated by a given DIE) in a given scope is
16633/// suppressed by the suppression specifications that are associated
16634/// to a given DWARF reader.
16635///
16636/// @param rdr the DWARF reader to consider.
16637///
16638/// @param scope of the scope of the type DIE to consider.
16639///
16640/// @param type_die the DIE that designates the type to consider.
16641///
16642/// @return true iff the type designated by the DIE @p type_die, in
16643/// the scope @p scope is suppressed by at the suppression
16644/// specifications associated to the current DWARF reader.
16645static bool
16646type_is_suppressed(const reader& rdr,
16647 const scope_decl* scope,
16648 Dwarf_Die *type_die)
16649{
16650 bool type_is_opaque = false;
16651 return type_is_suppressed(rdr, scope, type_die, type_is_opaque);
16652}
16653
16654/// Get the opaque version of a type that was suppressed because it's
16655/// a private type.
16656///
16657/// The opaque version version of the type is just a declared-only
16658/// version of the type (class, union or enum type) denoted by @p
16659/// type_die.
16660///
16661/// @param rdr the DWARF reader in use.
16662///
16663/// @param scope the scope of the type die we are looking at.
16664///
16665/// @param type_die the type DIE we are looking at.
16666///
16667/// @param where_offset the offset of the DIE where we are "logically"
16668/// positionned at, in the DIE tree. This is useful when @p die is
16669/// e.g, DW_TAG_partial_unit that can be included in several places in
16670/// the DIE tree.
16671///
16672/// @return the opaque version of the type denoted by @p type_die or
16673/// nil if no opaque version was found.
16675get_opaque_version_of_type(reader &rdr,
16676 scope_decl *scope,
16677 Dwarf_Die *type_die,
16678 size_t where_offset)
16679{
16681
16682 if (type_die == 0)
16683 return result;
16684
16685 unsigned tag = dwarf_tag(type_die);
16686 if (tag != DW_TAG_class_type
16687 && tag != DW_TAG_structure_type
16688 && tag != DW_TAG_union_type
16689 && tag != DW_TAG_enumeration_type)
16690 return result;
16691
16692 string type_name, linkage_name;
16693 location type_location;
16694 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
16695
16696 string qualified_name = build_qualified_name(scope, type_name);
16697
16698 //
16699 // TODO: also handle declaration-only unions. To do that, we mostly
16700 // need to adapt add_or_update_union_type to make it schedule
16701 // declaration-only unions for resolution too.
16702 //
16703 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
16704 {
16705 string_classes_or_unions_map::const_iterator i =
16706 rdr.declaration_only_classes().find(qualified_name);
16707 if (i != rdr.declaration_only_classes().end())
16708 result = i->second.back();
16709
16710 if (!result)
16711 {
16712 // So we didn't find any pre-existing forward-declared-only
16713 // class for the class definition that we could return as an
16714 // opaque type. So let's build one.
16715 //
16716 // TODO: we need to be able to do this for unions too!
16717 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
16718 /*alignment=*/0, /*size=*/0,
16719 tag == DW_TAG_structure_type,
16720 type_location,
16721 decl_base::VISIBILITY_DEFAULT));
16722 klass->set_is_declaration_only(true);
16723 klass->set_is_artificial(die_is_artificial(type_die));
16724 add_decl_to_scope(klass, scope);
16725 rdr.associate_die_to_type(type_die, klass, where_offset);
16726 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
16727 result = klass;
16728 }
16729 }
16730
16731 if (tag == DW_TAG_enumeration_type)
16732 {
16733 string_enums_map::const_iterator i =
16734 rdr.declaration_only_enums().find(qualified_name);
16735 if (i != rdr.declaration_only_enums().end())
16736 result = i->second.back();
16737
16738 if (!result)
16739 {
16740 uint64_t size = 0;
16741 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
16742 size *= 8;
16743 type_decl_sptr underlying_type =
16744 build_enum_underlying_type(rdr, type_name, size,
16745 /*anonymous=*/true);
16746 enum_type_decl::enumerators enumeratorz;
16747 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
16748 type_location,
16749 underlying_type,
16750 enumeratorz,
16751 linkage_name));
16752 enum_type->set_is_artificial(die_is_artificial(type_die));
16753 add_decl_to_scope(enum_type, scope);
16754 result = enum_type;
16755 }
16756 }
16757
16758 return result;
16759}
16760
16761/// Create a function symbol with a given name.
16762///
16763/// @param sym_name the name of the symbol to create.
16764///
16765/// @param env the environment to create the symbol in.
16766///
16767/// @return the newly created symbol.
16769create_default_fn_sym(const string& sym_name, const environment& env)
16770{
16772 elf_symbol_sptr result =
16774 /*symbol index=*/ 0,
16775 /*symbol size=*/ 0,
16776 sym_name,
16777 /*symbol type=*/ elf_symbol::FUNC_TYPE,
16778 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
16779 /*symbol is defined=*/ true,
16780 /*symbol is common=*/ false,
16781 /*symbol version=*/ ver,
16782 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
16783 return result;
16784}
16785
16786/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
16787///
16788/// @param rdr the DWARF reader to use
16789///
16790/// @param die the DW_TAG_subprogram DIE to read from.
16791///
16792/// @param where_offset the offset of the DIE where we are "logically"
16793/// positionned at, in the DIE tree. This is useful when @p die is
16794/// e.g, DW_TAG_partial_unit that can be included in several places in
16795/// the DIE tree.
16796///
16797/// @param called_for_public_decl this is set to true if the function
16798/// was called for a public (function) decl.
16799static function_decl_sptr
16800build_function_decl(reader& rdr,
16801 Dwarf_Die* die,
16802 size_t where_offset,
16804{
16805 function_decl_sptr result = fn;
16806 if (!die)
16807 return result;
16808 int tag = dwarf_tag(die);
16809 ABG_ASSERT(tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine);
16810
16811 if (!die_is_public_decl(die))
16812 return result;
16813
16814 translation_unit_sptr tu = rdr.cur_transl_unit();
16815 ABG_ASSERT(tu);
16816
16817 string fname, flinkage_name;
16818 location floc;
16819 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
16820 cleanup_decl_name(fname);
16821
16822 size_t is_inline = die_is_declared_inline(die);
16823 class_or_union_sptr is_method =
16824 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
16825
16826 if (result)
16827 {
16828 // Add the properties that might have been missing from the
16829 // first declaration of the function. For now, it usually is
16830 // the mangled name that goes missing in the first declarations.
16831 //
16832 // Also note that if 'fn' has just been cloned, the current
16833 // linkage name (of the current DIE) might be different from the
16834 // linkage name of 'fn'. In that case, update the linkage name
16835 // of 'fn' too.
16836 if (!flinkage_name.empty()
16837 && result->get_linkage_name() != flinkage_name)
16838 result->set_linkage_name(flinkage_name);
16839 if (floc)
16840 if (!result->get_location())
16841 result->set_location(floc);
16842 result->is_declared_inline(is_inline);
16843 }
16844 else
16845 {
16846 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
16847 where_offset));
16848 if (!fn_type)
16849 return result;
16850
16851 maybe_canonicalize_type(fn_type, rdr);
16852
16853 result.reset(is_method
16854 ? new method_decl(fname, fn_type,
16855 is_inline, floc,
16856 flinkage_name)
16857 : new function_decl(fname, fn_type,
16858 is_inline, floc,
16859 flinkage_name));
16860 }
16861
16862 // Set the symbol of the function. If the linkage name is not set
16863 // or is wrong, set it to the name of the underlying symbol.
16864 if (!result->get_symbol())
16865 {
16866 elf_symbol_sptr fn_sym;
16867 Dwarf_Addr fn_addr;
16868 if (rdr.get_function_address(die, fn_addr))
16869 {
16870 rdr.symtab()->
16871 update_main_symbol(fn_addr,
16872 result->get_linkage_name().empty()
16873 ? result->get_name()
16874 : result->get_linkage_name());
16875 fn_sym = rdr.function_symbol_is_exported(fn_addr);
16876 }
16877
16878 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
16879 {
16880 result->set_symbol(fn_sym);
16881 string linkage_name = result->get_linkage_name();
16882 if (linkage_name.empty())
16883 result->set_linkage_name(fn_sym->get_name());
16884 result->set_is_in_public_symbol_table(true);
16885 }
16886
16887 if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
16888 {
16889 // We are looking at a function which symbol is undefined.
16890 // let's set its symbol.
16891 string n = result->get_linkage_name();
16892 if (n.empty())
16893 n = result->get_name();
16894 fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
16895 if (fn_sym)
16896 {
16897 result->set_symbol(fn_sym);
16898 result->set_is_in_public_symbol_table(false);
16899 }
16900 }
16901 }
16902
16903 rdr.associate_die_to_type(die, result->get_type(), where_offset);
16904
16905 size_t die_offset = dwarf_dieoffset(die);
16906
16907 if (fn
16908 && is_member_function(fn)
16910 && !result->get_linkage_name().empty())
16911 // This function is a virtual member function which has its
16912 // linkage name *and* and has its underlying symbol correctly set.
16913 // It thus doesn't need any fixup related to elf symbol. So
16914 // remove it from the set of virtual member functions with linkage
16915 // names and no elf symbol that need to be fixed up.
16916 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
16917 return result;
16918}
16919
16920/// Canonicalize a type if it's suitable for early canonicalizing, or,
16921/// if it's not, schedule it for late canonicalization, after the
16922/// debug info of the current translation unit has been fully read.
16923///
16924/// A (composite) type is deemed suitable for early canonicalizing iff
16925/// all of its sub-types are canonicalized themselve. Non composite
16926/// types are always deemed suitable for early canonicalization.
16927///
16928/// Note that this function knows how to deal with anonymous classes,
16929/// structs and enums, unlike the overload below:
16930///
16931/// @param t the type DIE to consider for canonicalization.
16932///
16933/// @param rdr the @ref reader to use.
16934static void
16935maybe_canonicalize_type(const type_base_sptr& t,
16936 reader& rdr)
16937{
16938 if (!t)
16939 return;
16940
16941 rdr.schedule_type_for_late_canonicalization(t);
16942}
16943
16944/// If a given decl is a member type declaration, set its access
16945/// specifier from the DIE that represents it.
16946///
16947/// @param member_type_declaration the member type declaration to
16948/// consider.
16949static void
16950maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
16951 Dwarf_Die* die)
16952{
16953 if (is_type(member_type_declaration)
16954 && is_member_decl(member_type_declaration))
16955 {
16956 class_or_union* scope =
16957 is_class_or_union_type(member_type_declaration->get_scope());
16958 ABG_ASSERT(scope);
16959
16960 access_specifier access = public_access;
16961 if (class_decl* cl = is_class_type(scope))
16962 if (!cl->is_struct())
16963 access = private_access;
16964
16965 die_access_specifier(die, access);
16966 set_member_access_specifier(member_type_declaration, access);
16967 }
16968}
16969
16970/// Normalize a decl name so that it can be compared to other decl
16971/// names without risking to have spurious changes.
16972///
16973/// The function removes white spaces from the and normalizes
16974/// numerical litterals.
16975///
16976/// @param str in/out parameter. The string to normalize, in place.
16977static void
16978cleanup_decl_name(string& str)
16979{
16982}
16983
16984/// This function tests if a given function which might be intented to
16985/// be added to a class scope (to become a member function) should be
16986/// dropped on the floor instead and not be added to the class.
16987///
16988/// This is a subroutine of build_ir_node_from_die.
16989///
16990/// @param fn the function to consider.
16991///
16992/// @param fn_die the DWARF die of @p fn.
16993///
16994/// @param scope the scope in which @p fn is to be added.
16995///
16996/// @return true iff @p fn should be dropped on the floor.
16997static bool
16998potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
16999 const Dwarf_Die *fn_die)
17000{
17001 if (!fn || fn->get_scope())
17002 return false;
17003
17004 if (// A function that is not virtual ...
17005 !die_is_virtual(fn_die)
17006 // .. and yet has no defined ELF symbol associated ...
17007 && !fn->get_symbol())
17008 // Should not be added to its class scope.
17009 //
17010 // Why would it? It's not part of the ABI anyway, as it doesn't
17011 // have any ELF symbol associated and is not a virtual member
17012 // function. It just constitutes bloat in the IR and might even
17013 // induce spurious change reports down the road.
17014 return true;
17015
17016 return false;
17017}
17018
17019/// Build an IR node from a given DIE and add the node to the current
17020/// IR being build and held in the DWARF reader. Doing that is called
17021/// "emitting an IR node for the DIE".
17022///
17023/// @param rdr the DWARF reader.
17024///
17025/// @param die the DIE to consider.
17026///
17027/// @param scope the scope under which the resulting IR node has to be
17028/// added.
17029///
17030/// @param called_from_public_decl set to yes if this function is
17031/// called from the functions used to build a public decl (functions
17032/// and variables). In that case, this function accepts building IR
17033/// nodes representing types. Otherwise, this function only creates
17034/// IR nodes representing public decls (functions and variables).
17035/// This is done to avoid emitting IR nodes for types that are not
17036/// referenced by public functions or variables.
17037///
17038/// @param where_offset the offset of the DIE where we are "logically"
17039/// positionned at, in the DIE tree. This is useful when @p die is
17040/// e.g, DW_TAG_partial_unit that can be included in several places in
17041/// the DIE tree.
17042///
17043/// @param is_required_decl_spec if true, it means the ir node to
17044/// build is for a decl that is a specification for another decl that
17045/// is concrete. If you don't know what this is, set it to false.
17046///
17047/// @param is_declaration_only is true if the DIE denoted by @p die is
17048/// a declaration-only DIE.
17049///
17050/// @return the resulting IR node.
17052build_ir_node_from_die(reader& rdr,
17053 Dwarf_Die* die,
17054 scope_decl* scope,
17055 bool called_from_public_decl,
17056 size_t where_offset,
17057 bool is_declaration_only,
17058 bool is_required_decl_spec)
17059{
17061
17062 if (!die || !scope)
17063 return result;
17064
17065 int tag = dwarf_tag(die);
17066
17067 if (!called_from_public_decl)
17068 {
17069 if (rdr.load_all_types() && die_is_type(die))
17070 /* We were instructed to load debug info for all types,
17071 included those that are not reachable from a public
17072 declaration. So load the debug info for this type. */;
17073 else if (tag != DW_TAG_subprogram
17074 && tag != DW_TAG_variable
17075 && tag != DW_TAG_member
17076 && tag != DW_TAG_namespace)
17077 return result;
17078 }
17079
17080 const die_source source_of_die = rdr.get_die_source(die);
17081
17082 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
17083 source_of_die)))
17084 {
17085 if (rdr.load_all_types())
17086 if (called_from_public_decl)
17087 if (type_base_sptr t = is_type(result))
17088 if (corpus *abi_corpus = rdr.corpus().get())
17089 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17090
17091 return result;
17092 }
17093
17094 // This is *the* bit of code that ensures we have the right notion
17095 // of "declared" at any point in a DIE chain formed from
17096 // DW_AT_abstract_origin and DW_AT_specification links. There should
17097 // be no other callers of die_is_declaration_only.
17098 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
17099
17100 switch (tag)
17101 {
17102 // Type DIEs we support.
17103 case DW_TAG_base_type:
17104 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
17105 {
17106 result =
17107 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
17108 maybe_canonicalize_type(t, rdr);
17109 }
17110 break;
17111
17112 case DW_TAG_typedef:
17113 {
17114 typedef_decl_sptr t = build_typedef_type(rdr, die,
17115 called_from_public_decl,
17116 where_offset);
17117
17118 result = add_decl_to_scope(t, scope);
17119 if (result)
17120 {
17121 maybe_set_member_type_access_specifier(is_decl(result), die);
17122 maybe_canonicalize_type(t, rdr);
17123 }
17124 }
17125 break;
17126
17127 case DW_TAG_pointer_type:
17128 {
17130 build_pointer_type_def(rdr, die,
17131 called_from_public_decl,
17132 where_offset);
17133 if (p)
17134 {
17135 result =
17136 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
17137 ABG_ASSERT(result->get_translation_unit());
17138 maybe_canonicalize_type(p, rdr);
17139 }
17140 }
17141 break;
17142
17143 case DW_TAG_reference_type:
17144 case DW_TAG_rvalue_reference_type:
17145 {
17147 build_reference_type(rdr, die,
17148 called_from_public_decl,
17149 where_offset);
17150 if (r)
17151 {
17152 result =
17153 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
17154 maybe_canonicalize_type(r, rdr);
17155 }
17156 }
17157 break;
17158
17159 case DW_TAG_ptr_to_member_type:
17160 {
17162 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
17163 where_offset);
17164 if (p)
17165 {
17166 result =
17168 rdr.cur_transl_unit()->get_global_scope());
17169 maybe_canonicalize_type(p, rdr);
17170 }
17171 }
17172 break;
17173
17174 case DW_TAG_const_type:
17175 case DW_TAG_volatile_type:
17176 case DW_TAG_restrict_type:
17177 {
17178 type_base_sptr q =
17179 build_qualified_type(rdr, die,
17180 called_from_public_decl,
17181 where_offset);
17182 if (q)
17183 {
17184 // Strip some potentially redundant type qualifiers from
17185 // the qualified type we just built.
17186 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
17187 rdr);
17188 if (!d)
17189 d = get_type_declaration(q);
17190 ABG_ASSERT(d);
17191 type_base_sptr ty = is_type(d);
17192 // Associate the die to type ty again because 'ty'might be
17193 // different from 'q', because 'ty' is 'q' possibly
17194 // stripped from some redundant type qualifier.
17195 rdr.associate_die_to_type(die, ty, where_offset);
17196 result =
17197 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
17198 maybe_canonicalize_type(is_type(result), rdr);
17199 }
17200 }
17201 break;
17202
17203 case DW_TAG_enumeration_type:
17204 {
17205 bool type_is_opaque = false;
17206 bool type_suppressed =
17207 type_is_suppressed(rdr, scope, die, type_is_opaque);
17208 if (type_suppressed && type_is_opaque)
17209 {
17210 // The type is suppressed because it's private. If other
17211 // non-suppressed and declaration-only instances of this
17212 // type exist in the current corpus, then it means those
17213 // non-suppressed instances are opaque versions of the
17214 // suppressed private type. Lets return one of these opaque
17215 // types then.
17216 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17217 maybe_canonicalize_type(is_type(result), rdr);
17218 }
17219 else if (!type_suppressed)
17220 {
17221 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
17222 where_offset,
17223 is_declaration_only);
17224 result = add_decl_to_scope(e, scope);
17225 if (result)
17226 {
17227 maybe_set_member_type_access_specifier(is_decl(result), die);
17228 maybe_canonicalize_type(is_type(result), rdr);
17229 }
17230 }
17231 }
17232 break;
17233
17234 case DW_TAG_class_type:
17235 case DW_TAG_structure_type:
17236 {
17237 bool type_is_opaque = false;
17238 bool type_suppressed=
17239 type_is_suppressed(rdr, scope, die, type_is_opaque);
17240
17241 if (type_suppressed && type_is_opaque)
17242 {
17243 // The type is suppressed because it's private. If other
17244 // non-suppressed and declaration-only instances of this
17245 // type exist in the current corpus, then it means those
17246 // non-suppressed instances are opaque versions of the
17247 // suppressed private type. Lets return one of these opaque
17248 // types then.
17249 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
17250 maybe_canonicalize_type(is_type(result), rdr);
17251 }
17252 else if (!type_suppressed)
17253 {
17254 class_decl_sptr klass;
17255 Dwarf_Die spec_die;
17256 if (die_die_attribute(die, DW_AT_specification, spec_die))
17257 {
17258 scope_decl_sptr skope =
17259 get_scope_for_die(rdr, &spec_die,
17260 called_from_public_decl,
17261 where_offset);
17262 ABG_ASSERT(skope);
17263 decl_base_sptr cl =
17264 is_decl(build_ir_node_from_die(rdr, &spec_die,
17265 skope.get(),
17266 called_from_public_decl,
17267 where_offset,
17268 is_declaration_only,
17269 /*is_required_decl_spec=*/false));
17270 ABG_ASSERT(cl);
17271 klass = dynamic_pointer_cast<class_decl>(cl);
17272 ABG_ASSERT(klass);
17273
17274 klass =
17275 add_or_update_class_type(rdr, die,
17276 skope.get(),
17277 tag == DW_TAG_structure_type,
17278 klass,
17279 called_from_public_decl,
17280 where_offset,
17281 is_declaration_only);
17282 }
17283 else
17284 {
17285 if (class_decl* class_sc = is_class_type(scope))
17286 {
17287 string type_name = die_type_name(rdr, die,
17288 /*qualified_name=*/false,
17289 where_offset);
17290 if (class_decl_sptr c =
17291 is_class_type(class_sc->find_member_type(type_name)))
17292 klass = c;
17293 else
17294 klass =
17295 add_or_update_class_type(rdr, die, scope,
17296 tag == DW_TAG_structure_type,
17298 called_from_public_decl,
17299 where_offset,
17300 is_declaration_only);
17301 }
17302 else
17303 klass =
17304 add_or_update_class_type(rdr, die, scope,
17305 tag == DW_TAG_structure_type,
17307 called_from_public_decl,
17308 where_offset,
17309 is_declaration_only);
17310 }
17311 if (klass)
17312 {
17313 maybe_set_member_type_access_specifier(klass, die);
17314 maybe_canonicalize_type(klass, rdr);
17315 }
17316 result = klass;
17317 }
17318 }
17319 break;
17320 case DW_TAG_union_type:
17321 if (!type_is_suppressed(rdr, scope, die))
17322 {
17323 union_decl_sptr union_type;
17324 if (class_decl* class_sc = is_class_type(scope))
17325 {
17326 string type_name = die_type_name(rdr, die,
17327 /*qualified_name=*/false,
17328 where_offset);
17329 if (union_decl_sptr u =
17330 is_union_type(class_sc->find_member_type(type_name)))
17331 union_type = u;
17332 }
17333
17334 if (!union_type)
17335 union_type =
17336 add_or_update_union_type(rdr, die, scope,
17337 union_decl_sptr(),
17338 called_from_public_decl,
17339 where_offset,
17340 is_declaration_only);
17341
17342 if (union_type)
17343 {
17344 maybe_set_member_type_access_specifier(union_type, die);
17345 maybe_canonicalize_type(union_type, rdr);
17346 result = union_type;
17347 }
17348 }
17349 break;
17350 case DW_TAG_string_type:
17351 break;
17352 case DW_TAG_subroutine_type:
17353 {
17354 function_type_sptr f = build_function_type(rdr, die,
17356 where_offset);
17357 if (f)
17358 {
17359 result = f;
17360 result->set_is_artificial(false);
17361 maybe_canonicalize_type(f, rdr);
17362 }
17363 }
17364 break;
17365 case DW_TAG_array_type:
17366 {
17367 array_type_def_sptr a = build_array_type(rdr,
17368 die,
17369 called_from_public_decl,
17370 where_offset);
17371 if (a)
17372 {
17373 result =
17374 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
17375 maybe_canonicalize_type(a, rdr);
17376 }
17377 break;
17378 }
17379 case DW_TAG_subrange_type:
17380 {
17381 // If we got here, this means the subrange type is a "free
17382 // form" defined in the global namespace of the current
17383 // translation unit, like what is found in Ada.
17385 build_subrange_type(rdr, die, where_offset,
17386 /*associate_type_to_die=*/true);
17387 if (s)
17388 {
17389 result =
17390 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
17391 maybe_canonicalize_type(s, rdr);
17392 }
17393 }
17394 break;
17395 case DW_TAG_packed_type:
17396 break;
17397 case DW_TAG_set_type:
17398 break;
17399 case DW_TAG_file_type:
17400 break;
17401 case DW_TAG_thrown_type:
17402 break;
17403 case DW_TAG_interface_type:
17404 break;
17405 case DW_TAG_unspecified_type:
17406 break;
17407 case DW_TAG_shared_type:
17408 break;
17409
17410 case DW_TAG_compile_unit:
17411 // We shouldn't reach this point b/c this should be handled by
17412 // build_translation_unit.
17414
17415 case DW_TAG_namespace:
17416 case DW_TAG_module:
17417 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
17418 break;
17419
17420 case DW_TAG_variable:
17421 case DW_TAG_member:
17422 {
17423 if (tag == DW_TAG_member)
17424 ABG_ASSERT(!die_is_in_c(die));
17425
17426 scope_decl_sptr var_scope =
17427 get_scope_for_die(rdr, die,
17428 /*called_from_public_decl=*/
17429 die_is_effectively_public_decl(rdr, die),
17430 where_offset);
17431 var_decl_sptr v =
17432 build_or_get_var_decl_if_not_suppressed(rdr, var_scope.get(), die,
17433 where_offset,
17434 is_declaration_only,
17435 /*result=*/var_decl_sptr(),
17436 is_required_decl_spec);
17437 if (v && is_data_member(v))
17438 // We might have gotten a pre-existing data member variable
17439 // that was already built. This means this DIE is a
17440 // concrete implementation of a previous specification.
17441 // Read the specific attributes of this concrete
17442 // implementation and add them to the existing IR node we
17443 // have.
17444 v = build_var_decl(rdr, die, where_offset, v);
17445
17446 if (v)
17447 {
17448 add_decl_to_scope(v, var_scope);
17449 if (is_data_member(v))
17450 // We are sure this is a static data member at this
17451 // point because a non-static data member would have
17452 // been encountered a a child of a class or union DIE
17453 // and thus handled by add_or_update_class_type or
17454 // add_or_update_union_type.
17455 set_member_is_static(v, true);
17456 else
17457 rdr.var_decls_to_re_add_to_tree().push_back(v);
17458 rdr.add_var_to_exported_or_undefined_decls(v);
17459 rdr.associate_die_to_decl(die, v, where_offset,
17460 /*associate_by_repr=*/false);
17461 result = v;
17462 }
17463 }
17464 break;
17465
17466 case DW_TAG_subprogram:
17467 case DW_TAG_inlined_subroutine:
17468 {
17469 if (die_is_artificial(die))
17470 break;
17471
17472 Dwarf_Die abstract_origin_die;
17473 bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
17474 abstract_origin_die,
17475 /*recursive=*/true);
17476
17477
17478 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
17479 where_offset);
17480 scope_decl* interface_scope = scope ? scope : s.get();
17481
17482 class_decl* class_scope = is_class_type(interface_scope);
17483 string linkage_name = die_linkage_name(die);
17484 string spec_linkage_name;
17485 function_decl_sptr existing_fn;
17486
17487 if (class_scope)
17488 {
17489 // The scope of the function DIE we are looking at is a
17490 // class. So we are looking at a member function.
17491 if (!linkage_name.empty())
17492 {
17493 if ((existing_fn =
17494 class_scope->find_member_function_sptr(linkage_name)))
17495 {
17496 // A function with the same linkage name has
17497 // already been created. Let's see if we are a
17498 // clone of it or not.
17499 spec_linkage_name = existing_fn->get_linkage_name();
17500 if (has_abstract_origin
17501 && !spec_linkage_name.empty()
17502 && linkage_name != spec_linkage_name)
17503 {
17504 // The current DIE has 'existing_fn' as
17505 // abstract orign, and has a linkage name that
17506 // is different from from the linkage name of
17507 // 'existing_fn'. That means, the current DIE
17508 // represents a clone of 'existing_fn'.
17509 existing_fn = existing_fn->clone();
17510 }
17511 }
17512 }
17513 }
17514 else if (has_abstract_origin)
17515 // Let's see if this function is the implementation of an
17516 // existing interface. In that case, let's read the
17517 // specification of the origin interface ...
17518 existing_fn = build_function_decl(rdr, &abstract_origin_die, where_offset,
17519 /*existing_fn=*/nullptr);
17520
17521 rdr.scope_stack().push(interface_scope);
17522
17523 // Either we create a brand new IR for the current function
17524 // DIE we are looking at, or we complete an existing IR node
17525 // with the new completementary information carried by this
17526 // DIE for that IR node.
17527 result =
17528 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
17529 die, where_offset,
17530 is_declaration_only,
17531 existing_fn);
17532
17533 if (result && !existing_fn)
17534 {
17535 // We built a brand new IR for the function DIE. Now
17536 // there should be enough information on that IR to know
17537 // if we should drop it on the floor or keep it ...
17538 if (potential_member_fn_should_be_dropped(is_function_decl(result), die)
17539 && !is_required_decl_spec)
17540 {
17541 // So apparently we should drop that function IR on
17542 // the floor. Let's do so.
17543 result.reset();
17544 break;
17545 }
17546 }
17547
17548 // OK so we came to the conclusion that we need to keep
17549 // the function. So let's add it to its scope.
17550 result = add_decl_to_scope(is_decl(result), interface_scope);
17551
17553 if (fn && is_member_function(fn))
17554 {
17555 class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
17556 sptr_utils::noop_deleter());
17557 ABG_ASSERT(klass);
17558 finish_member_function_reading(die, fn, klass, rdr);
17559 }
17560
17561 if (fn)
17562 {
17563 if (!is_member_function(fn)
17565 // Virtual member functions are added to the set of
17566 // functions exported by the current ABI corpus *after*
17567 // the canonicalization of their parent type. So let's
17568 // not do it here.
17569 rdr.add_fn_to_exported_or_undefined_decls(fn.get());
17570 rdr.associate_die_to_decl(die, fn, where_offset,
17571 /*associate_by_repr=*/false);
17572 maybe_canonicalize_type(fn->get_type(), rdr);
17573 }
17574
17575 rdr.scope_stack().pop();
17576 }
17577 break;
17578
17579 case DW_TAG_formal_parameter:
17580 // We should not read this case as it should have been dealt
17581 // with by build_function_decl above.
17583
17584 case DW_TAG_constant:
17585 break;
17586 case DW_TAG_enumerator:
17587 break;
17588
17589 case DW_TAG_partial_unit:
17590 case DW_TAG_imported_unit:
17591 // For now, the DIEs under these are read lazily when they are
17592 // referenced by a public decl DIE that is under a
17593 // DW_TAG_compile_unit, so we shouldn't get here.
17595
17596 // Other declaration we don't really intend to support yet.
17597 case DW_TAG_dwarf_procedure:
17598 case DW_TAG_imported_declaration:
17599 case DW_TAG_entry_point:
17600 case DW_TAG_label:
17601 case DW_TAG_lexical_block:
17602 case DW_TAG_unspecified_parameters:
17603 case DW_TAG_variant:
17604 case DW_TAG_common_block:
17605 case DW_TAG_common_inclusion:
17606 case DW_TAG_inheritance:
17607 case DW_TAG_with_stmt:
17608 case DW_TAG_access_declaration:
17609 case DW_TAG_catch_block:
17610 case DW_TAG_friend:
17611 case DW_TAG_namelist:
17612 case DW_TAG_namelist_item:
17613 case DW_TAG_template_type_parameter:
17614 case DW_TAG_template_value_parameter:
17615 case DW_TAG_try_block:
17616 case DW_TAG_variant_part:
17617 case DW_TAG_imported_module:
17618 case DW_TAG_condition:
17619 case DW_TAG_type_unit:
17620 case DW_TAG_template_alias:
17621 case DW_TAG_lo_user:
17622 case DW_TAG_MIPS_loop:
17623 case DW_TAG_format_label:
17624 case DW_TAG_function_template:
17625 case DW_TAG_class_template:
17626 case DW_TAG_GNU_BINCL:
17627 case DW_TAG_GNU_EINCL:
17628 case DW_TAG_GNU_template_template_param:
17629 case DW_TAG_GNU_template_parameter_pack:
17630 case DW_TAG_GNU_formal_parameter_pack:
17631 case DW_TAG_GNU_call_site:
17632 case DW_TAG_GNU_call_site_parameter:
17633 case DW_TAG_hi_user:
17634 default:
17635 break;
17636 }
17637
17638 if (result && tag != DW_TAG_subroutine_type)
17639 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
17640 /*associate_by_repr=*/false);
17641
17642 if (result)
17643 if (rdr.load_all_types())
17644 if (called_from_public_decl)
17645 if (type_base_sptr t = is_type(result))
17646 if (corpus *abi_corpus = scope->get_corpus())
17647 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
17648
17649 rdr.maybe_schedule_decl_only_type_for_resolution(result);
17650
17651 return result;
17652}
17653
17654/// Build the IR node for a void type.
17655///
17656/// @param rdr the DWARF reader to use.
17657///
17658/// @return the void type node.
17659static decl_base_sptr
17660build_ir_node_for_void_type(reader& rdr)
17661{
17662 const environment& env = rdr.env();
17663
17664 type_base_sptr t = env.get_void_type();
17665 decl_base_sptr type_declaration = get_type_declaration(t);
17666 if (!has_scope(type_declaration))
17667 {
17668 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17669 rdr.schedule_type_for_late_canonicalization(t);
17670 }
17671 return type_declaration;
17672}
17673
17674/// Build the IR node for a "pointer to void type".
17675///
17676/// That IR node is shared across the ABI corpus.
17677///
17678/// Note that this function just gets that IR node from the
17679/// environment and, if it's not added to any scope yet, adds it to
17680/// the global scope associated to the current translation unit.
17681///
17682/// @param rdr the DWARF reader to consider.
17683///
17684/// @return the IR node.
17686build_ir_node_for_void_pointer_type(reader& rdr)
17687{
17688 const environment& env = rdr.env();
17689 type_base_sptr t = env.get_void_pointer_type();
17690 decl_base_sptr type_declaration = get_type_declaration(t);
17691 if (!has_scope(type_declaration))
17692 {
17693 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17694 rdr.schedule_type_for_late_canonicalization(t);
17695 }
17696 return type_declaration;
17697}
17698
17699/// Build the IR node for a variadic parameter type.
17700///
17701/// @param rdr the DWARF reader to use.
17702///
17703/// @return the variadic parameter type.
17704static decl_base_sptr
17705build_ir_node_for_variadic_parameter_type(reader &rdr)
17706{
17707
17708 const environment& env = rdr.env();
17709 type_base_sptr t = env.get_variadic_parameter_type();
17710 decl_base_sptr type_declaration = get_type_declaration(t);
17711 if (!has_scope(type_declaration))
17712 {
17713 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
17714 rdr.schedule_type_for_late_canonicalization(t);
17715 }
17716 return type_declaration;
17717}
17718
17719/// Build an IR node from a given DIE and add the node to the current
17720/// IR being build and held in the DWARF reader. Doing that is called
17721/// "emitting an IR node for the DIE".
17722///
17723/// @param rdr the DWARF reader.
17724///
17725/// @param die the DIE to consider.
17726///
17727/// @param called_from_public_decl set to yes if this function is
17728/// called from the functions used to build a public decl (functions
17729/// and variables). In that case, this function accepts building IR
17730/// nodes representing types. Otherwise, this function only creates
17731/// IR nodes representing public decls (functions and variables).
17732/// This is done to avoid emitting IR nodes for types that are not
17733/// referenced by public functions or variables.
17734///
17735/// @param where_offset the offset of the DIE where we are "logically"
17736/// positionned at, in the DIE tree. This is useful when @p die is
17737/// e.g, DW_TAG_partial_unit that can be included in several places in
17738/// the DIE tree.
17739///
17740/// @return the resulting IR node.
17742build_ir_node_from_die(reader& rdr,
17743 Dwarf_Die* die,
17744 bool called_from_public_decl,
17745 size_t where_offset)
17746{
17747 if (!die)
17748 return decl_base_sptr();
17749
17750 // Normaly, a decl that is meant to be external has a DW_AT_external
17751 // set. But then some compilers fail to always emit that flag. For
17752 // instance, for static data members, some compilers won't emit the
17753 // DW_AT_external. In that case, we assume that if the variable is
17754 // at global or named namespace scope, then we can assume it's
17755 // external. If the variable doesn't have any ELF symbol associated
17756 // to it, it'll be dropped on the floor anyway. Those variable
17757 // decls are considered as being "effectively public".
17758 bool consider_as_called_from_public_decl =
17759 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
17760 scope_decl_sptr scope = get_scope_for_die(rdr, die,
17761 consider_as_called_from_public_decl,
17762 where_offset);
17763 if (!scope)
17764 scope = rdr.global_scope();
17765
17766 return build_ir_node_from_die(rdr, die, scope.get(),
17767 called_from_public_decl,
17768 where_offset, true);
17769}
17770
17771/// Create a dwarf::reader.
17772///
17773/// @param elf_path the path to the elf file the reader is to be used
17774/// for.
17775///
17776/// @param debug_info_root_paths a vector to the paths to the
17777/// directories under which the debug info is to be found for @p
17778/// elf_path. Pass an empty vector if the debug info is not in a
17779/// split file.
17780///
17781/// @param environment the environment used by the current context.
17782/// This environment contains resources needed by the DWARF reader and by
17783/// the types and declarations that are to be created later. Note
17784/// that ABI artifacts that are to be compared all need to be created
17785/// within the same environment.
17786///
17787/// Please also note that the life time of this environment object
17788/// must be greater than the life time of the resulting @ref
17789/// reader the context uses resources that are allocated in the
17790/// environment.
17791///
17792/// @param load_all_types if set to false only the types that are
17793/// reachable from publicly exported declarations (of functions and
17794/// variables) are read. If set to true then all types found in the
17795/// debug information are loaded.
17796///
17797/// @param linux_kernel_mode if set to true, then consider the special
17798/// linux kernel symbol tables when determining if a symbol is
17799/// exported or not.
17800///
17801/// @return a smart pointer to the resulting dwarf::reader.
17802elf_based_reader_sptr
17803create_reader(const std::string& elf_path,
17804 const vector<char**>& debug_info_root_paths,
17806 bool load_all_types,
17807 bool linux_kernel_mode)
17808{
17809
17810 reader_sptr r = reader::create(elf_path,
17811 debug_info_root_paths,
17813 load_all_types,
17814 linux_kernel_mode);
17815 return static_pointer_cast<elf_based_reader>(r);
17816}
17817
17818/// Re-initialize a reader so that it can re-used to read
17819/// another binary.
17820///
17821/// @param rdr the context to re-initialize.
17822///
17823/// @param elf_path the path to the elf file the context is to be used
17824/// for.
17825///
17826/// @param debug_info_root_path a pointer to the path to the root
17827/// directory under which the debug info is to be found for @p
17828/// elf_path. Leave this to NULL if the debug info is not in a split
17829/// file.
17830///
17831/// @param environment the environment used by the current context.
17832/// This environment contains resources needed by the DWARF reader and by
17833/// the types and declarations that are to be created later. Note
17834/// that ABI artifacts that are to be compared all need to be created
17835/// within the same environment.
17836///
17837/// Please also note that the life time of this environment object
17838/// must be greater than the life time of the resulting @ref
17839/// reader the context uses resources that are allocated in the
17840/// environment.
17841///
17842/// @param load_all_types if set to false only the types that are
17843/// reachable from publicly exported declarations (of functions and
17844/// variables) are read. If set to true then all types found in the
17845/// debug information are loaded.
17846///
17847/// @param linux_kernel_mode if set to true, then consider the special
17848/// linux kernel symbol tables when determining if a symbol is
17849/// exported or not.
17850///
17851/// @return a smart pointer to the resulting dwarf::reader.
17852void
17854 const std::string& elf_path,
17855 const vector<char**>&debug_info_root_path,
17856 bool read_all_types,
17857 bool linux_kernel_mode)
17858{
17859 reader& r = dynamic_cast<reader&>(rdr);
17860 r.initialize(elf_path, debug_info_root_path,
17861 read_all_types, linux_kernel_mode);
17862}
17863
17864/// Read all @ref abigail::translation_unit possible from the debug info
17865/// accessible from an elf file, stuff them into a libabigail ABI
17866/// Corpus and return it.
17867///
17868/// @param elf_path the path to the elf file.
17869///
17870/// @param debug_info_root_paths a vector of pointers to root paths
17871/// under which to look for the debug info of the elf files that are
17872/// later handled by the Dwfl. This for cases where the debug info is
17873/// split into a different file from the binary we want to inspect.
17874/// On Red Hat compatible systems, this root path is usually
17875/// /usr/lib/debug by default. If this argument is set to NULL, then
17876/// "./debug" and /usr/lib/debug will be searched for sub-directories
17877/// containing the debug info file.
17878///
17879/// @param environment the environment used by the current context.
17880/// This environment contains resources needed by the DWARF reader and by
17881/// the types and declarations that are to be created later. Note
17882/// that ABI artifacts that are to be compared all need to be created
17883/// within the same environment. Also, the lifetime of the
17884/// environment must be greater than the lifetime of the resulting
17885/// corpus because the corpus uses resources that are allocated in the
17886/// environment.
17887///
17888/// @param load_all_types if set to false only the types that are
17889/// reachable from publicly exported declarations (of functions and
17890/// variables) are read. If set to true then all types found in the
17891/// debug information are loaded.
17892///
17893/// @param resulting_corp a pointer to the resulting abigail::corpus.
17894///
17895/// @return the resulting status.
17896corpus_sptr
17897read_corpus_from_elf(const std::string& elf_path,
17898 const vector<char**>& debug_info_root_paths,
17900 bool load_all_types,
17901 fe_iface::status& status)
17902{
17903 elf_based_reader_sptr rdr =
17904 dwarf::reader::create(elf_path, debug_info_root_paths,
17905 environment, load_all_types,
17906 /*linux_kernel_mode=*/false);
17907
17908 return rdr->read_corpus(status);
17909}
17910
17911/// Look into the symbol tables of a given elf file and see if we find
17912/// a given symbol.
17913///
17914/// @param env the environment we are operating from.
17915///
17916/// @param elf_path the path to the elf file to consider.
17917///
17918/// @param symbol_name the name of the symbol to look for.
17919///
17920/// @param demangle if true, try to demangle the symbol name found in
17921/// the symbol table.
17922///
17923/// @param syms the vector of symbols found with the name @p symbol_name.
17924///
17925/// @return true iff the symbol was found among the publicly exported
17926/// symbols of the ELF file.
17927bool
17928lookup_symbol_from_elf(const environment& env,
17929 const string& elf_path,
17930 const string& symbol_name,
17931 bool demangle,
17932 vector<elf_symbol_sptr>& syms)
17933
17934{
17935 if (elf_version(EV_CURRENT) == EV_NONE)
17936 return false;
17937
17938 int fd = open(elf_path.c_str(), O_RDONLY);
17939 if (fd < 0)
17940 return false;
17941
17942 struct stat s;
17943 if (fstat(fd, &s))
17944 return false;
17945
17946 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
17947 if (elf == 0)
17948 return false;
17949
17950 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
17951 demangle, syms);
17952 elf_end(elf);
17953 close(fd);
17954
17955 return value;
17956}
17957
17958/// Look into the symbol tables of an elf file to see if a public
17959/// function of a given name is found.
17960///
17961/// @param env the environment we are operating from.
17962///
17963/// @param elf_path the path to the elf file to consider.
17964///
17965/// @param symbol_name the name of the function to look for.
17966///
17967/// @param syms the vector of public function symbols found with the
17968/// name @p symname.
17969///
17970/// @return true iff a function with symbol name @p symbol_name is
17971/// found.
17972bool
17974 const string& path,
17975 const string& symname,
17976 vector<elf_symbol_sptr>& syms)
17977{
17978 if (elf_version(EV_CURRENT) == EV_NONE)
17979 return false;
17980
17981 int fd = open(path.c_str(), O_RDONLY);
17982 if (fd < 0)
17983 return false;
17984
17985 struct stat s;
17986 if (fstat(fd, &s))
17987 return false;
17988
17989 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
17990 if (elf == 0)
17991 return false;
17992
17993 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
17994 elf_end(elf);
17995 close(fd);
17996
17997 return value;
17998}
17999
18000}// end namespace dwarf
18001
18002}// 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:1737
This contains the private implementation of the suppression engine of libabigail.
Utilities to ease the wrapping of C types into std::shared_ptr.
This contains the private implementation of the suppression engine of libabigail.
This contains the declarations for the symtab reader.
#define ABG_ASSERT_NOT_REACHED
A macro that expands to aborting the program when executed.
Simplified implementation of std::optional just enough to be used as a replacement for our purposes a...
elf_symbol_sptr function_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined function symbol.
const string & elf_architecture() const
Get the value of the 'ARCHITECTURE' property of the current ELF file.
const Dwarf * dwarf_debug_info() const
Getter of the handle used to access DWARF information from the current ELF file.
virtual ir::corpus_sptr read_corpus(status &status)
Read the ELF information associated to the current ELF file and construct an ABI representation from ...
const Dwarf * alternate_dwarf_debug_info() const
Getter of the handle use to access DWARF information from the alternate split DWARF information.
bool refers_to_alt_debug_info(string &alt_di_path) const
Check if the underlying elf file refers to an alternate debug info file associated to it.
elf_symbol_sptr variable_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined variable symbol.
elf_symbol_sptr variable_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given variable symbol has been exported.
elf_symbol_sptr function_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given function symbol has been exported.
const vector< string > & dt_needed() const
Get the value of the DT_NEEDED property of the current ELF file.
The common interface of readers based on ELF.
elf_based_reader(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &env)
Readers that implement this interface must provide a factory method to create a reader instance as th...
virtual void initialize(const std::string &elf_path, const vector< char ** > &debug_info_root_paths)
(re)Initialize) the resources used by the current reader.
status
The status of the fe_iface::read_corpus call.
Definition: abg-fe-iface.h:38
@ STATUS_DEBUG_INFO_NOT_FOUND
This status is for when the debug info could not be read.
Definition: abg-fe-iface.h:46
@ STATUS_ALT_DEBUG_INFO_NOT_FOUND
This status is for when the alternate debug info could not be found.
Definition: abg-fe-iface.h:50
@ STATUS_UNKNOWN
The status is in an unknown state.
Definition: abg-fe-iface.h:40
const options_type & options() const
Getter of the the options of the current Front End Interface.
Definition: abg-fe-iface.cc:91
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
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:4193
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition: abg-corpus.h:51
scope_decl * get_scope() const
Return the type containing the current decl, if any.
Definition: abg-ir.cc:4792
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1232
binding
The binding of a symbol.
Definition: abg-ir.h:978
type
The type of a symbol.
Definition: abg-ir.h:965
static elf_symbol_sptr create(const environment &e, size_t i, size_t s, const string &n, type t, binding b, bool d, bool c, const version &ve, visibility vi, bool is_in_ksymtab=false, const abg_compat::optional< uint32_t > &crc={}, const abg_compat::optional< std::string > &ns={}, bool is_suppressed=false)
Factory of instances of elf_symbol.
Definition: abg-ir.cc:2063
visibility
The visibility of the symbol.
Definition: abg-ir.h:987
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2801
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition: abg-ir.h:148
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3187
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3190
The source location of a token.
Definition: abg-ir.h:307
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition: abg-ir.h:2255
The internal representation of an integral type.
Definition: abg-ir-priv.h:48
string to_string(bool internal=false) const
Return the string representation of the current instance of real_type.
Definition: abg-ir.cc:16762
language
The language of the translation unit.
Definition: abg-ir.h:708
enum type_or_decl_kind kind() const
Getter for the "kind" property of type_or_decl_base type.
Definition: abg-ir.cc:4114
visiting_kind operator~(visiting_kind l)
The overloaded 'bit inversion' operator for visiting_kind.
unordered_map< std::pair< offset_type, offset_type >, offset_pair_set_type, offset_pair_hash > offset_pair_set_map_type
A convenience typedef for an unordered_map that associates a pair of offset_type to a set of pairs of...
unordered_map< Dwarf_Off, translation_unit_sptr > die_tu_map_type
Convenience typedef for a map which key is the offset of a DW_TAG_compile_unit and the value is the c...
bool lookup_symbol_from_elf(const environment &env, const string &elf_path, const string &symbol_name, bool demangle, vector< elf_symbol_sptr > &syms)
Look into the symbol tables of a given elf file and see if we find a given symbol.
void reset_reader(elf_based_reader &rdr, const std::string &elf_path, const vector< char ** > &debug_info_root_path, bool read_all_types, bool linux_kernel_mode)
Re-initialize a reader so that it can re-used to read another binary.
unordered_map< string, classes_type > string_classes_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
std::pair< offset_type, offset_type > offset_pair_type
A convenience typedef for a pair of offset_type.
shared_ptr< addr_elf_symbol_sptr_map_type > addr_elf_symbol_sptr_map_sptr
Convenience typedef for a shared pointer to an addr_elf_symbol_sptr_map_type.
bool is_anonymous_type_die(Dwarf_Die *die)
Test if a given DIE represents an anonymous type.
unordered_map< Dwarf_Off, class_or_union_sptr > die_class_or_union_map_type
Convenience typedef for a map which key is the offset of a dwarf die, (given by dwarf_dieoffset()) an...
unordered_map< Dwarf_Off, function_type_sptr > die_function_type_map_type
Convenience typedef for a map which key is the offset of a dwarf die and which value is the correspon...
unordered_map< Dwarf_Off, interned_string > die_istring_map_type
Convenience typedef for a map which key is the offset of a DIE and the value is the corresponding qua...
unordered_map< Dwarf_Off, function_decl_sptr > die_function_decl_map_type
Convenience typedef for a map which key the offset of a dwarf die and which value is the correspondin...
bool lookup_public_function_symbol_from_elf(environment &env, const string &path, const string &symname, vector< elf_symbol_sptr > &syms)
Look into the symbol tables of an elf file to see if a public function of a given name is found.
unordered_set< offset_type, offset_hash > offset_set_type
A convenience typedef for an unordered set of DIE offsets.
unordered_map< Dwarf_Off, type_or_decl_base_sptr > die_artefact_map_type
Convenience typedef for a map which key is the offset of a dwarf die and which value is the correspon...
unordered_map< std::pair< offset_type, offset_type >, offset_pair_vector_type, offset_pair_hash > offset_pair_vect_map_type
A convenience typedef for an unordered map that associates a pair of offset_type to a vector of pairs...
unordered_map< interned_string, function_type_sptr, hash_interned_string > istring_fn_type_map_type
Convenience typedef for a map that associates an interned_string to a function_type_sptr.
unordered_map< Dwarf_Off, class_decl_sptr > die_class_map_type
Convenience typedef for a map which key is the offset of a dwarf die, (given by dwarf_dieoffset()) an...
unordered_map< string, classes_or_unions_type > string_classes_or_unions_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
elf_symbol_sptr create_default_fn_sym(const string &sym_name, const environment &env)
Create a function symbol with a given name.
unordered_map< Dwarf_Off, imported_unit_points_type > tu_die_imported_unit_points_map_type
Convenience typedef for a vector of imported_unit_point.
vector< Dwarf_Off > dwarf_offsets_type
A convenience typedef for a vector of Dwarf_Off.
unordered_map< Dwarf_Off, Dwarf_Off > offset_offset_map_type
Convenience typedef for a map which key is a dwarf offset. The value is also a dwarf offset.
unordered_set< std::pair< offset_type, offset_type >, offset_pair_hash > offset_pair_set_type
A convenience typedef for an unordered set of pairs of offset_type.
unordered_map< string, enums_type > string_enums_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
stack< scope_decl * > scope_stack_type
Convenience typedef for a stack containing the scopes up to the current point in the abigail Internal...
vector< std::pair< offset_type, offset_type > > offset_pair_vector_type
A convenience typedef for a vector of pairs of offset_type.
elf_based_reader_sptr create_reader(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &environment, bool load_all_types, bool linux_kernel_mode)
Create a dwarf::reader.
die_source
Where a DIE comes from. For instance, a DIE can come from the main debug info section,...
unordered_map< interned_string, dwarf_offsets_type, hash_interned_string > istring_dwarf_offsets_map_type
Convenience typedef for a map which is an interned_string and which value is a vector of offsets.
vector< imported_unit_point > imported_unit_points_type
Convenience typedef for a vector of imported_unit_point.
corpus_sptr read_corpus_from_elf(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &environment, bool load_all_types, fe_iface::status &status)
Read all abigail::translation_unit possible from the debug info accessible from an elf file,...
hash_t combine_hashes(hash_t val1, hash_t val2)
Combine two hash values to produce a third hash value.
Definition: abg-hash.cc:172
hash_t hash(uint64_t v, uint64_t seed)
Hash an integer value and combine it with a hash previously computed.
Definition: abg-hash.cc:196
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:235
bool get_member_function_is_dtor(const function_decl &f)
Test whether a member function is a destructor.
Definition: abg-ir.cc:6454
type_decl_sptr lookup_basic_type(const interned_string &type_name, const translation_unit &tu)
Lookup a basic type from a translation unit.
Definition: abg-ir.cc:12365
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:12229
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:13946
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:14108
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:11707
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10759
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5399
array_type_def::subrange_type * is_subrange_type(const type_or_decl_base *type)
Test if a type is an array_type_def::subrange_type.
Definition: abg-ir.cc:12135
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:926
class_decl_sptr is_compatible_with_class_type(const type_base_sptr &t)
Test if a type is a class. This function looks through typedefs.
Definition: abg-ir.cc:11053
bool parse_real_type(const string &type_name, real_type &type)
Parse a real type from a string.
Definition: abg-ir.cc:16680
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8475
bool odr_is_relevant(const type_or_decl_base &artifact)
By looking at the language of the TU a given ABI artifact belongs to, test if the ONE Definition Rule...
Definition: abg-ir.cc:10201
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:11631
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:12255
comparison_result
The result of structural comparison of type ABI artifacts.
Definition: abg-ir-priv.h:35
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition: abg-ir.cc:11085
void set_member_function_virtuality(function_decl &fn, bool is_virtual, ssize_t voffset)
Set the virtual-ness of a member fcuntion.
Definition: abg-ir.cc:6715
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:244
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition: abg-ir.cc:10810
type_base_sptr peel_const_qualified_type(const qualified_type_def_sptr &q)
If a qualified type is const, then return its underlying type.
Definition: abg-ir.cc:7333
bool anonymous_data_member_exists_in_class(const var_decl &anon_dm, const class_or_union &clazz)
Test if a given anonymous data member exists in a class or union.
Definition: abg-ir.cc:6114
class_decl_sptr lookup_class_type_per_location(const interned_string &loc, const corpus &corp)
Look up a class_decl from a given corpus by its location.
Definition: abg-ir.cc:13809
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6482
reference_type_def_sptr lookup_reference_type(const interned_string &type_name, const translation_unit &tu)
Lookup a reference type from a translation unit.
Definition: abg-ir.cc:12696
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:11316
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:193
void set_member_function_is_const(function_decl &f, bool is_const)
set the const-ness property of a member function.
Definition: abg-ir.cc:6538
decl_base_sptr strip_useless_const_qualification(const qualified_type_def_sptr t)
Strip qualification from a qualified type, when it makes sense.
Definition: abg-ir.cc:6915
const type_decl * is_type_decl(const type_or_decl_base *t)
Test whether a type is a type_decl (a builtin type).
Definition: abg-ir.cc:10861
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:11778
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5544
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10963
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:13976
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:12405
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:11534
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:11571
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:11035
unordered_map< interned_string, type_base_wptrs_type, hash_interned_string > istring_type_base_wptrs_map_type
A convenience typedef for a map which key is an interned_string and which value is a vector of type_b...
Definition: abg-fwd.h:148
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8543
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:256
shared_ptr< ptr_to_mbr_type > ptr_to_mbr_type_sptr
Convenience typedef for a shared pointer to a ptr_to_mbr_type.
Definition: abg-fwd.h:239
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.
Definition: abg-ir-priv.h:1523
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:12895
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:12442
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:226
bool is_const_qualified_type(const qualified_type_def_sptr &t)
Test if a given qualified type is const.
Definition: abg-ir.cc:7301
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6368
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1792
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10699
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:25641
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5464
decl_base_sptr add_decl_to_scope(decl_base_sptr decl, scope_decl *scope)
Appends a declaration to a given scope, if the declaration doesn't already belong to one and if the d...
Definition: abg-ir.cc:8450
string build_internal_underlying_enum_type_name(const string &base_name, bool is_anonymous, uint64_t size)
Build the internal name of the underlying type of an enum.
Definition: abg-ir.cc:29013
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5515
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:175
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6641
const pointer_type_def * is_pointer_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:11399
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:11838
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:11365
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:13764
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:12114
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5613
const type_base_wptrs_type * lookup_class_types(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find the class type*s* that have a given qualified name.
Definition: abg-ir.cc:13713
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:10223
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:26741
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:12043
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:161
void strip_redundant_quals_from_underyling_types(const qualified_type_def_sptr &t)
Merge redundant qualifiers from a tree of qualified types.
Definition: abg-ir.cc:7038
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition: abg-fwd.h:284
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1833
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:15348
type_base_sptr clone_array_tree(const type_base_sptr t)
Clone a type tree made of an array or a typedef of array.
Definition: abg-ir.cc:7668
typedef_decl_sptr lookup_typedef_type_per_location(const interned_string &loc, const corpus &corp)
Lookup a typedef_decl from a corpus, by its location.
Definition: abg-ir.cc:14071
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10647
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:11808
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:11758
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:12475
string build_qualified_name(const scope_decl *scope, const string &name)
Build and return a qualified name from a name and its scope.
Definition: abg-ir.cc:8732
method_decl_sptr copy_member_function(const class_or_union_sptr &t, const method_decl_sptr &method)
Copy a method of a class_or_union into a new class_or_union.
Definition: abg-ir.cc:24754
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:11868
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5417
void set_member_function_is_ctor(function_decl &f, bool c)
Setter for the is_ctor property of the member function.
Definition: abg-ir.cc:6425
bool is_function_suppressed(const fe_iface &fe, const string &fn_name, const string &fn_linkage_name, bool require_drop_property)
Test if a function is matched by at least one suppression specification associated with a given front...
bool is_type_suppressed(const fe_iface &fe, const string &type_name, const location &type_location, bool &type_is_opaque, bool require_drop_property)
Test if a type is matched by at least one suppression specification associated with a given front-end...
bool is_variable_suppressed(const fe_iface &fe, const string &var_name, const string &var_linkage_name, bool require_drop_property)
Test if a variable is matched by at least one suppression specification associated with a given front...
bool base_name(string const &path, string &file_name)
Return the file name part of a file part.
bool normalize_litterals(string &str)
Normalize the numerical litteral in a string.
bool remove_white_spaces(string &str)
Remove white spaces from a string.
void initialize()
This function needs to be called before any libabigail function.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool string_is_ascii_identifier(const string &str)
Test if a string is made of ascii characters which are identifiers acceptable in C or C++ programs.
Toplevel namespace for libabigail.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition: abg-ir.cc:151
fe_iface::status operator&(fe_iface::status l, fe_iface::status r)
The bitwise AND operator for the fe_iface::status type.
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition: abg-ir.cc:185
fe_iface::status operator|(fe_iface::status l, fe_iface::status r)
The bitwise OR operator for the fe_iface::status type.
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:168
bool load_undefined_interfaces
If this option is set to true, then the functions and variables that have an undefined symbol are goi...
Definition: abg-fe-iface.h:67
A functor to hash instances of interned_string.