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-2023 Red Hat, Inc.
5//
6// Author: Dodji Seketeli
7
8/// @file
9///
10/// This file contains the definitions of the entry points to
11/// de-serialize an instance of @ref abigail::corpus from a file in
12/// elf format, containing dwarf information.
13
14#include "abg-internal.h"
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19#include <libgen.h>
20#include <assert.h>
21#include <limits.h>
22#include <elfutils/libdwfl.h>
23#include <dwarf.h>
24#include <algorithm>
25#include <cmath>
26#include <cstring>
27#include <deque>
28#include <list>
29#include <memory>
30#include <ostream>
31#include <sstream>
32#include <stack>
33#include <unordered_map>
34#include <unordered_set>
35#include <map>
36
37#include "abg-ir-priv.h"
39#include "abg-corpus-priv.h"
40#include "abg-symtab-reader.h"
41
42// <headers defining libabigail's API go under here>
43ABG_BEGIN_EXPORT_DECLARATIONS
44
45#include "abg-dwarf-reader.h"
47#include "abg-sptr-utils.h"
48#include "abg-tools-utils.h"
49#include "abg-elf-helpers.h"
50
51ABG_END_EXPORT_DECLARATIONS
52// </headers defining libabigail's API>
53
54#ifndef UINT64_MAX
55#define UINT64_MAX 0xffffffffffffffff
56#endif
57
58using std::string;
59
60namespace abigail
61{
62
63using std::cerr;
64
65/// The namespace for the DWARF reader.
66namespace dwarf
67{
68
69using std::dynamic_pointer_cast;
70using std::static_pointer_cast;
71using std::unordered_map;
72using std::unordered_set;
73using std::stack;
74using std::deque;
75using std::list;
76using std::map;
78
79using namespace elf_helpers; // TODO: avoid using namespace
80
81/// Where a DIE comes from. For instance, a DIE can come from the main
82/// debug info section, the alternate debug info section or from the
83/// type unit section.
85{
86 NO_DEBUG_INFO_DIE_SOURCE,
87 PRIMARY_DEBUG_INFO_DIE_SOURCE,
88 ALT_DEBUG_INFO_DIE_SOURCE,
89 TYPE_UNIT_DIE_SOURCE,
90 NUMBER_OF_DIE_SOURCES, // This one must always be the latest
91 // enumerator
92};
93
94
95/// A convenience typedef for a vector of Dwarf_Off.
96typedef vector<Dwarf_Off> dwarf_offsets_type;
97
98/// Convenience typedef for a map which key is the offset of a dwarf
99/// die and which value is the corresponding artefact.
100typedef unordered_map<Dwarf_Off, type_or_decl_base_sptr> die_artefact_map_type;
101
102/// Convenience typedef for a map which key is the offset of a dwarf
103/// die, (given by dwarf_dieoffset()) and which value is the
104/// corresponding class_decl.
105typedef unordered_map<Dwarf_Off, class_decl_sptr> die_class_map_type;
106
107/// Convenience typedef for a map which key is the offset of a dwarf
108/// die, (given by dwarf_dieoffset()) and which value is the
109/// corresponding class_or_union_sptr.
110typedef unordered_map<Dwarf_Off, class_or_union_sptr> die_class_or_union_map_type;
111
112/// Convenience typedef for a map which key the offset of a dwarf die
113/// and which value is the corresponding function_decl.
114typedef unordered_map<Dwarf_Off, function_decl_sptr> die_function_decl_map_type;
115
116/// Convenience typedef for a map which key is the offset of a dwarf
117/// die and which value is the corresponding function_type.
118typedef unordered_map<Dwarf_Off, function_type_sptr> die_function_type_map_type;
119
120/// Convenience typedef for a map which key is the offset of a
121/// DW_TAG_compile_unit and the value is the corresponding @ref
122/// translation_unit_sptr.
123typedef unordered_map<Dwarf_Off, translation_unit_sptr> die_tu_map_type;
124
125/// Convenience typedef for a map which key is the offset of a DIE and
126/// the value is the corresponding qualified name of the DIE.
127typedef unordered_map<Dwarf_Off, interned_string> die_istring_map_type;
128
129/// Convenience typedef for a map which is an interned_string and
130/// which value is a vector of offsets.
131typedef unordered_map<interned_string,
135
136/// A hasher for a pair of Dwarf_Off. This is used as a hasher for
137/// the type @ref dwarf_offset_pair_set_type.
138struct dwarf_offset_pair_hash
139{
140 size_t
141 operator()(const std::pair<Dwarf_Off, Dwarf_Off>& p) const
142 {return abigail::hashing::combine_hashes(p.first, p.second);}
143};// end struct dwarf_offset_pair_hash
144
145typedef unordered_set<std::pair<Dwarf_Off,
146 Dwarf_Off>,
147 dwarf_offset_pair_hash> dwarf_offset_pair_set_type;
148
149/// An abstraction of a DIE offset that also encapsulate the source of
150/// the DIE.
151struct offset_type
152{
153 die_source source_;
154 Dwarf_Off offset_;
155
156 offset_type()
157 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
158 offset_(0)
159 {}
160
161 offset_type(die_source source, Dwarf_Off offset)
162 : source_(source),
163 offset_(offset)
164 {}
165
166 offset_type(Dwarf_Off offset)
167 : source_(PRIMARY_DEBUG_INFO_DIE_SOURCE),
168 offset_(offset)
169 {}
170
171 bool operator==(const offset_type& o) const
172 {return source_ == o.source_ && offset_ == o.offset_;}
173
174 operator Dwarf_Off() const
175 {return offset_;}
176}; // end struct offset_type
177
178/// A convenience typedef for a pair of offset_type.
179typedef std::pair<offset_type, offset_type> offset_pair_type;
180
181/// A hasher for an instance of offset_type.
182struct offset_hash
183{
184 size_t
185 operator()(const offset_type& p) const
186 {return abigail::hashing::combine_hashes(p.source_, p.offset_);}
187};// end struct offset_hash
188
189/// A hasher for a pair of offset_type. This is used as a hasher for
190/// the type @ref offset_pair_set_type, for instance.
191struct offset_pair_hash
192{
193 size_t
194 operator()(const std::pair<offset_type, offset_type>& p) const
195 {
196 size_t h1 = abigail::hashing::combine_hashes(p.first.source_,
197 p.first.offset_);
198 size_t h2 = abigail::hashing::combine_hashes(p.second.source_,
199 p.second.offset_);
200 return abigail::hashing::combine_hashes(h1, h2);
201 }
202};// end struct offset_pair_hash
203
204/// A convenience typedef for an unordered set of DIE offsets.
205typedef unordered_set<offset_type, offset_hash> offset_set_type;
206
207///A convenience typedef for an unordered set of pairs of offset_type.
208typedef unordered_set<std::pair<offset_type,
209 offset_type>,
210 offset_pair_hash> offset_pair_set_type;
211
212/// A convenience typedef for a vector of pairs of offset_type.
213typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
214
215/// A convenience typedef for an unordered map that associates a pair
216/// of offset_type to a vector of pairs offset_type.
217typedef unordered_map<std::pair<offset_type, offset_type>,
219 offset_pair_hash> offset_pair_vect_map_type;
220
221/// A convenience typedef for an unordered_map that associates a pair
222/// of offset_type to a set of pairs of offset_type.
223typedef unordered_map<std::pair<offset_type, offset_type>,
225 offset_pair_hash> offset_pair_set_map_type;
226
227/// A convenience typedef for a vector of pairs of offset_type.
228typedef vector<std::pair<offset_type, offset_type>> offset_pair_vector_type;
229
230class reader;
231
233build_translation_unit_and_add_to_ir(reader& rdr,
234 Dwarf_Die* die,
235 char address_size);
236
237static void
238maybe_propagate_canonical_type(const reader& rdr,
239 const Dwarf_Die* l,
240 const Dwarf_Die* r);
241
242static void
243propagate_canonical_type(const reader& rdr,
244 const Dwarf_Die* l,
245 const Dwarf_Die* r);
246
247/// Convenience typedef for a shared pointer to an
248/// addr_elf_symbol_sptr_map_type.
249typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
250
251/// Convenience typedef for a map that associates an @ref
252/// interned_string to a @ref function_type_sptr.
253typedef unordered_map<interned_string,
256
257/// Convenience typedef for a stack containing the scopes up to the
258/// current point in the abigail Internal Representation (aka IR) tree
259/// that is being built.
260typedef stack<scope_decl*> scope_stack_type;
261
262/// Convenience typedef for a map which key is a dwarf offset. The
263/// value is also a dwarf offset.
264typedef unordered_map<Dwarf_Off, Dwarf_Off> offset_offset_map_type;
265
266/// Convenience typedef for a map which key is a string and which
267/// value is a vector of smart pointer to a class_or_union_sptr.
268typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_map;
269
270/// Convenience typedef for a map which key is a string and which
271/// value is a vector of smart pointer to a class.
272typedef unordered_map<string, classes_type> string_classes_map;
273
274/// Convenience typedef for a map which key is a string and which
275/// value is a vector of smart pointer to a enum.
276typedef unordered_map<string, enums_type> string_enums_map;
277
278/// The abstraction of the place where a partial unit has been
279/// imported. This is what the DW_TAG_imported_unit DIE expresses.
280///
281/// This type thus contains:
282/// - the offset to which the partial unit is imported
283/// - the offset of the imported partial unit.
284/// - the offset of the imported partial unit.
285struct imported_unit_point
286{
287 Dwarf_Off offset_of_import;
288 // The boolean below is true iff the imported unit comes from the
289 // alternate debug info file.
290 die_source imported_unit_die_source;
291 Dwarf_Off imported_unit_die_off;
292 Dwarf_Off imported_unit_cu_off;
293 Dwarf_Off imported_unit_child_off;
294
295 /// Default constructor for @ref the type imported_unit_point.
296 imported_unit_point()
297 : offset_of_import(),
298 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
299 imported_unit_die_off(),
300 imported_unit_cu_off(),
301 imported_unit_child_off()
302 {}
303
304 /// Constructor of @ref the type imported_unit_point.
305 ///
306 /// @param import_off the offset of the point at which the unit has
307 /// been imported.
308 imported_unit_point(Dwarf_Off import_off)
309 : offset_of_import(import_off),
310 imported_unit_die_source(PRIMARY_DEBUG_INFO_DIE_SOURCE),
311 imported_unit_die_off(),
312 imported_unit_cu_off(),
313 imported_unit_child_off()
314 {}
315
316 /// Constructor of @ref the type imported_unit_point.
317 ///
318 /// @param import_off the offset of the point at which the unit has
319 /// been imported.
320 ///
321 /// @param from where the imported DIE comes from.
322 ///
323 /// @param imported_die the die of the unit that has been imported.
324 imported_unit_point(Dwarf_Off import_off,
325 const Dwarf_Die& imported_die,
326 die_source from)
327 : offset_of_import(import_off),
328 imported_unit_die_source(from),
329 imported_unit_die_off(dwarf_dieoffset
330 (const_cast<Dwarf_Die*>(&imported_die))),
331 imported_unit_cu_off(),
332 imported_unit_child_off()
333 {
334 Dwarf_Die imported_unit_child;
335
336 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
337 &imported_unit_child) == 0);
338
339 imported_unit_child_off =
340 dwarf_dieoffset(const_cast<Dwarf_Die*>(&imported_unit_child));
341
342 Dwarf_Die cu_die_memory;
343 Dwarf_Die *cu_die;
344
345 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&imported_unit_child),
346 &cu_die_memory, 0, 0);
347 imported_unit_cu_off = dwarf_dieoffset(cu_die);
348 }
349}; // struct imported_unit_point
350
351/// Convenience typedef for a vector of @ref imported_unit_point.
352typedef vector<imported_unit_point> imported_unit_points_type;
353
354/// Convenience typedef for a vector of @ref imported_unit_point.
355typedef unordered_map<Dwarf_Off, imported_unit_points_type>
357
358/// "Less than" operator for instances of @ref imported_unit_point
359/// type.
360///
361/// @param the left hand side operand of the "Less than" operator.
362///
363/// @param the right hand side operand of the "Less than" operator.
364///
365/// @return true iff @p l is less than @p r.
366static bool
367operator<(const imported_unit_point& l, const imported_unit_point& r)
368{return l.offset_of_import < r.offset_of_import;}
369
370static bool
371get_parent_die(const reader& rdr,
372 const Dwarf_Die* die,
373 Dwarf_Die& parent_die,
374 size_t where_offset);
375
376static bool
377get_scope_die(const reader& rdr,
378 const Dwarf_Die* die,
379 size_t where_offset,
380 Dwarf_Die& scope_die);
381
382static bool
383die_is_anonymous(const Dwarf_Die* die);
384
385static bool
386die_is_anonymous_data_member(const Dwarf_Die* die);
387
388static bool
389die_is_type(const Dwarf_Die* die);
390
391static bool
392die_is_decl(const Dwarf_Die* die);
393
394static bool
395die_is_declaration_only(Dwarf_Die* die);
396
397static bool
398die_is_variable_decl(const Dwarf_Die *die);
399
400static bool
401die_is_function_decl(const Dwarf_Die *die);
402
403static bool
404die_has_size_attribute(const Dwarf_Die *die);
405
406static bool
407die_has_no_child(const Dwarf_Die *die);
408
409static bool
410die_is_namespace(const Dwarf_Die* die);
411
412static bool
413die_is_unspecified(Dwarf_Die* die);
414
415static bool
416die_is_void_type(Dwarf_Die* die);
417
418static bool
419die_is_pointer_type(const Dwarf_Die* die);
420
421static bool
422pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die);
423
424static bool
425die_is_reference_type(const Dwarf_Die* die);
426
427static bool
428die_is_pointer_array_or_reference_type(const Dwarf_Die* die);
429
430static bool
431die_is_pointer_or_reference_type(const Dwarf_Die* die);
432
433static bool
434die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die);
435
436static bool
437die_is_class_type(const Dwarf_Die* die);
438
439static bool
440die_is_qualified_type(const Dwarf_Die* die);
441
442static bool
443die_is_function_type(const Dwarf_Die *die);
444
445static bool
446die_has_object_pointer(const Dwarf_Die* die,
447 Dwarf_Die& object_pointer);
448
449static bool
450die_has_children(const Dwarf_Die* die);
451
452static bool
453die_this_pointer_from_object_pointer(Dwarf_Die* die,
454 Dwarf_Die& this_pointer);
455
456static bool
457die_this_pointer_is_const(Dwarf_Die* die);
458
459static bool
460die_object_pointer_is_for_const_method(Dwarf_Die* die);
461
462static bool
463is_type_die_to_be_canonicalized(const Dwarf_Die *die);
464
465static bool
466die_is_at_class_scope(const reader& rdr,
467 const Dwarf_Die* die,
468 size_t where_offset,
469 Dwarf_Die& class_scope_die);
470static bool
471eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
472 size_t expr_len,
473 int64_t& value,
474 bool& is_tls_address);
475
477dwarf_language_to_tu_language(size_t l);
478
479static bool
480die_unsigned_constant_attribute(const Dwarf_Die* die,
481 unsigned attr_name,
482 uint64_t& cst);
483
484static bool
485die_signed_constant_attribute(const Dwarf_Die*die,
486 unsigned attr_name,
487 int64_t& cst);
488
489static bool
490die_constant_attribute(const Dwarf_Die *die,
491 unsigned attr_name,
492 bool is_signed,
493 array_type_def::subrange_type::bound_value &value);
494
495static bool
496die_member_offset(const reader& rdr,
497 const Dwarf_Die* die,
498 int64_t& offset);
499
500static bool
501form_is_DW_FORM_strx(unsigned form);
502
503static bool
504form_is_DW_FORM_line_strp(unsigned form);
505
506static bool
507die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
508
509static string
510die_name(const Dwarf_Die* die);
511
512static location
513die_location(const reader& rdr, const Dwarf_Die* die);
514
515static bool
516die_location_address(Dwarf_Die* die,
517 Dwarf_Addr& address,
518 bool& is_tls_address);
519
520static bool
521die_die_attribute(const Dwarf_Die* die,
522 unsigned attr_name,
523 Dwarf_Die& result,
524 bool recursively = true);
525
526static bool
527die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
528
529static bool
530subrange_die_indirect_bound_value(const Dwarf_Die *die,
531 unsigned attr_name,
532 array_type_def::subrange_type::bound_value& v,
533 bool& is_signed);
534
535static bool
536subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
537 unsigned attr_name,
538 Dwarf_Die& referenced_subrange);
539static string
540get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
541
542static string
543build_internal_anonymous_die_name(const string &base_name,
544 size_t anonymous_type_index);
545
546static string
547get_internal_anonymous_die_name(Dwarf_Die *die,
548 size_t anonymous_type_index);
549
550static string
551die_qualified_type_name(const reader& rdr,
552 const Dwarf_Die* die,
553 size_t where);
554
555static string
556die_qualified_decl_name(const reader& rdr,
557 const Dwarf_Die* die,
558 size_t where);
559
560static string
561die_qualified_name(const reader& rdr,
562 const Dwarf_Die* die,
563 size_t where);
564
565static bool
566die_qualified_type_name_empty(const reader& rdr,
567 const Dwarf_Die* die, size_t where,
568 string &qualified_name);
569
570static void
571die_return_and_parm_names_from_fn_type_die(const reader& rdr,
572 const Dwarf_Die* die,
573 size_t where_offset,
574 bool pretty_print,
575 string &return_type_name,
576 string &class_name,
577 vector<string>& parm_names,
578 bool& is_const,
579 bool& is_static);
580
581static string
582die_function_signature(const reader& rdr,
583 const Dwarf_Die *die,
584 size_t where_offset);
585
586static bool
587die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
588
589static bool
590die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
591
592static bool
593die_function_type_is_method_type(const reader& rdr,
594 const Dwarf_Die *die,
595 size_t where_offset,
596 Dwarf_Die& object_pointer_die,
597 Dwarf_Die& class_die,
598 bool& is_static);
599
600static string
601die_pretty_print_type(reader& rdr,
602 const Dwarf_Die* die,
603 size_t where_offset);
604
605static string
606die_pretty_print_decl(reader& rdr,
607 const Dwarf_Die* die,
608 size_t where_offset);
609
610static string
611die_pretty_print(reader& rdr,
612 const Dwarf_Die* die,
613 size_t where_offset);
614
615static void
616maybe_canonicalize_type(const type_base_sptr& t,
617 reader& rdr);
618
619static uint64_t
620get_default_array_lower_bound(translation_unit::language l);
621
622static bool
623find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
624 Dwarf_Off,
625 imported_unit_points_type::const_iterator&);
626
628build_subrange_type(reader& rdr,
629 const Dwarf_Die* die,
630 size_t where_offset,
631 bool associate_type_to_die = true);
632
633static void
634build_subranges_from_array_type_die(reader& rdr,
635 const Dwarf_Die* die,
637 size_t where_offset,
638 bool associate_type_to_die = true);
639
641compare_dies(const reader& rdr,
642 const Dwarf_Die *l, const Dwarf_Die *r,
643 bool update_canonical_dies_on_the_fly);
644
645static bool
646compare_dies_during_canonicalization(reader& rdr,
647 const Dwarf_Die *l, const Dwarf_Die *r,
648 bool update_canonical_dies_on_the_fly);
649
650static bool
651get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
652
653/// Compare a symbol name against another name, possibly demangling
654/// the symbol_name before performing the comparison.
655///
656/// @param symbol_name the symbol_name to take in account.
657///
658/// @param name the second name to take in account.
659///
660/// @param demangle if true, demangle @p symbol_name and compare the
661/// result of the demangling with @p name.
662///
663/// @return true iff symbol_name equals name.
664static bool
665compare_symbol_name(const string& symbol_name,
666 const string& name,
667 bool demangle)
668{
669 if (demangle)
670 {
671 string m = demangle_cplus_mangled_name(symbol_name);
672 return m == name;
673 }
674 return symbol_name == name;
675}
676
677/// Lookup a symbol using the SysV ELF hash table.
678///
679/// Note that this function hasn't been tested. So it hasn't been
680/// debugged yet. IOW, it is not known to work. Or rather, it's
681/// almost like it's surely doesn't work ;-)
682///
683/// Use it at your own risks. :-)
684///
685///@parm env the environment we are operating from.
686///
687/// @param elf_handle the elf_handle to use.
688///
689/// @param sym_name the symbol name to look for.
690///
691/// @param ht_index the index (in the section headers table) of the
692/// hash table section to use.
693///
694/// @param sym_tab_index the index (in the section headers table) of
695/// the symbol table to use.
696///
697/// @param demangle if true, demangle @p sym_name before comparing it
698/// to names from the symbol table.
699///
700/// @param syms_found a vector of symbols found with the name @p
701/// sym_name. table.
702static bool
703lookup_symbol_from_sysv_hash_tab(const environment& env,
704 Elf* elf_handle,
705 const string& sym_name,
706 size_t ht_index,
707 size_t sym_tab_index,
708 bool demangle,
709 vector<elf_symbol_sptr>& syms_found)
710{
711 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
712 ABG_ASSERT(sym_tab_section);
713
714 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
715 ABG_ASSERT(sym_tab_data);
716
717 GElf_Shdr sheader_mem;
718 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
719 &sheader_mem);
720 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
721 ABG_ASSERT(hash_section);
722
723 // Poke at the different parts of the hash table and get them ready
724 // to be used.
725 unsigned long hash = elf_hash(sym_name.c_str());
726 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
727 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
728 size_t nb_buckets = ht_data[0];
729 size_t nb_chains = ht_data[1];
730
731 if (nb_buckets == 0)
732 // An empty hash table. Not sure if that is possible, but it
733 // would mean an empty table of exported symbols.
734 return false;
735
736 //size_t nb_chains = ht_data[1];
737 Elf32_Word* ht_buckets = &ht_data[2];
738 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
739
740 // Now do the real work.
741 size_t bucket = hash % nb_buckets;
742 size_t symbol_index = ht_buckets[bucket];
743
744 GElf_Sym symbol;
745 const char* sym_name_str;
746 size_t sym_size;
747 elf_symbol::type sym_type;
748 elf_symbol::binding sym_binding;
749 elf_symbol::visibility sym_visibility;
750 bool found = false;
751
752 do
753 {
754 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
755 sym_name_str = elf_strptr(elf_handle,
756 sym_tab_section_header->sh_link,
757 symbol.st_name);
758 if (sym_name_str
759 && compare_symbol_name(sym_name_str, sym_name, demangle))
760 {
761 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
762 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
763 sym_visibility =
764 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
765 sym_size = symbol.st_size;
766 elf_symbol::version ver;
767 if (get_version_for_symbol(elf_handle, symbol_index,
768 /*get_def_version=*/true, ver))
769 ABG_ASSERT(!ver.str().empty());
770 elf_symbol_sptr symbol_found =
772 symbol_index,
773 sym_size,
774 sym_name_str,
775 sym_type,
776 sym_binding,
777 symbol.st_shndx != SHN_UNDEF,
778 symbol.st_shndx == SHN_COMMON,
779 ver, sym_visibility);
780 syms_found.push_back(symbol_found);
781 found = true;
782 }
783 symbol_index = ht_chains[symbol_index];
784 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
785
786 return found;
787}
788
789/// Get the size of the elf class, in bytes.
790///
791/// @param elf_handle the elf handle to use.
792///
793/// @return the size computed.
794static char
795get_elf_class_size_in_bytes(Elf* elf_handle)
796{
797 char result = 0;
798 GElf_Ehdr hdr;
799
800 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
801 int c = hdr.e_ident[EI_CLASS];
802
803 switch (c)
804 {
805 case ELFCLASS32:
806 result = 4;
807 break;
808 case ELFCLASS64:
809 result = 8;
810 break;
811 default:
813 }
814
815 return result;
816}
817
818/// Get a given word of a bloom filter, referred to by the index of
819/// the word.
820///
821/// The bloom word size depends on the current elf class (32 bits for
822/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
823/// abstracts that nicely.
824///
825/// @param elf_handle the elf handle to use.
826///
827/// @param bloom_filter the bloom filter to consider.
828///
829/// @param index the index of the bloom filter to return.
830///
831/// @return a 64 bits work containing the bloom word found at index @p
832/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
833/// most significant bytes of the result are going to be zero.
834static Elf64_Xword
835bloom_word_at(Elf* elf_handle,
836 Elf32_Word* bloom_filter,
837 size_t index)
838{
839 Elf64_Xword result = 0;
840 GElf_Ehdr h;
841 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
842 int c;
843 c = h.e_ident[EI_CLASS];
844
845 switch(c)
846 {
847 case ELFCLASS32:
848 result = bloom_filter[index];
849 break ;
850 case ELFCLASS64:
851 {
852 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
853 result = f[index];
854 }
855 break;
856 default:
857 abort();
858 }
859
860 return result;
861}
862
863/// The abstraction of the gnu elf hash table.
864///
865/// The members of this struct are explained at
866/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
867/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
868struct gnu_ht
869{
870 size_t nb_buckets;
871 Elf32_Word* buckets;
872 Elf32_Word* chain;
873 size_t first_sym_index;
874 size_t bf_nwords;
875 size_t bf_size;
876 Elf32_Word* bloom_filter;
877 size_t shift;
878 size_t sym_count;
879 Elf_Scn* sym_tab_section;
880 GElf_Shdr sym_tab_section_header;
881
882 gnu_ht()
883 : nb_buckets(0),
884 buckets(0),
885 chain(0),
886 first_sym_index(0),
887 bf_nwords(0),
888 bf_size(0),
889 bloom_filter(0),
890 shift(0),
891 sym_count(0),
892 sym_tab_section(0)
893 {}
894}; // end struct gnu_ht
895
896/// Setup the members of the gnu hash table.
897///
898/// @param elf_handle a handle on the elf file to use.
899///
900/// @param ht_index the index (into the elf section headers table) of
901/// the hash table section to use.
902///
903/// @param sym_tab_index the index (into the elf section headers
904/// table) of the symbol table the gnu hash table is about.
905///
906/// @param ht the resulting hash table.
907///
908/// @return true iff the hash table @ ht could be setup.
909static bool
910setup_gnu_ht(Elf* elf_handle,
911 size_t ht_index,
912 size_t sym_tab_index,
913 gnu_ht& ht)
914{
915 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
916 ABG_ASSERT(ht.sym_tab_section);
917 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
918 ht.sym_count =
919 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
920 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
921 ABG_ASSERT(hash_section);
922
923 // Poke at the different parts of the hash table and get them ready
924 // to be used.
925 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
926 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
927
928 ht.nb_buckets = ht_data[0];
929 if (ht.nb_buckets == 0)
930 // An empty hash table. Not sure if that is possible, but it
931 // would mean an empty table of exported symbols.
932 return false;
933 ht.first_sym_index = ht_data[1];
934 // The number of words used by the bloom filter. A size of a word
935 // is ELFCLASS.
936 ht.bf_nwords = ht_data[2];
937 // The shift used by the bloom filter code.
938 ht.shift = ht_data[3];
939 // The data of the bloom filter proper.
940 ht.bloom_filter = &ht_data[4];
941 // The size of the bloom filter in 4 bytes word. This is going to
942 // be used to index the 'bloom_filter' above, which is of type
943 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
944 // words.
945 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
946 // The buckets of the hash table.
947 ht.buckets = ht.bloom_filter + ht.bf_size;
948 // The chain of the hash table.
949 ht.chain = ht.buckets + ht.nb_buckets;
950
951 return true;
952}
953
954/// Look into the symbol tables of the underlying elf file and find
955/// the symbol we are being asked.
956///
957/// This function uses the GNU hash table for the symbol lookup.
958///
959/// The reference of for the implementation of this function can be
960/// found at:
961/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
962/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
963///
964/// @param elf_handle the elf handle to use.
965///
966/// @param sym_name the name of the symbol to look for.
967///
968/// @param ht_index the index of the hash table header to use.
969///
970/// @param sym_tab_index the index of the symbol table header to use
971/// with this hash table.
972///
973/// @param demangle if true, demangle @p sym_name.
974///
975/// @param syms_found the vector of symbols found with the name @p
976/// sym_name.
977///
978/// @return true if a symbol was actually found.
979static bool
980lookup_symbol_from_gnu_hash_tab(const environment& env,
981 Elf* elf_handle,
982 const string& sym_name,
983 size_t ht_index,
984 size_t sym_tab_index,
985 bool demangle,
986 vector<elf_symbol_sptr>& syms_found)
987{
988 gnu_ht ht;
989 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
990 return false;
991
992 // Now do the real work.
993
994 // Compute bloom hashes (GNU hash and second bloom specific hashes).
995 size_t h1 = elf_gnu_hash(sym_name.c_str());
996 size_t h2 = h1 >> ht.shift;
997 // The size of one of the words used in the bloom
998 // filter, in bits.
999 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
1000 int n = (h1 / c) % ht.bf_nwords;
1001 // The bitmask of the bloom filter has a size of either 32-bits on
1002 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1003 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1004 // type used here. When dealing with 32bits binaries, the upper
1005 // bits of the bitmask will be zero anyway.
1006 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1007
1008 // Test if the symbol is *NOT* present in this ELF file.
1009 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1010 return false;
1011
1012 size_t i = ht.buckets[h1 % ht.nb_buckets];
1013 if (i == STN_UNDEF)
1014 return false;
1015
1016 Elf32_Word stop_word, *stop_wordp;
1017 elf_symbol::version ver;
1018 GElf_Sym symbol;
1019 const char* sym_name_str;
1020 bool found = false;
1021
1022 elf_symbol::type sym_type;
1023 elf_symbol::binding sym_binding;
1024 elf_symbol::visibility sym_visibility;
1025
1026 // Let's walk the hash table and record the versions of all the
1027 // symbols which name equal sym_name.
1028 for (i = ht.buckets[h1 % ht.nb_buckets],
1029 stop_wordp = &ht.chain[i - ht.first_sym_index];
1030 i != STN_UNDEF
1031 && (stop_wordp
1032 < ht.chain + (ht.sym_count - ht.first_sym_index));
1033 ++i, ++stop_wordp)
1034 {
1035 stop_word = *stop_wordp;
1036 if ((stop_word & ~ 1)!= (h1 & ~1))
1037 // A given bucket can reference several hashes. Here we
1038 // stumbled across a hash value different from the one we are
1039 // looking for. Let's keep walking.
1040 continue;
1041
1042 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1043 i, &symbol));
1044 sym_name_str = elf_strptr(elf_handle,
1045 ht.sym_tab_section_header.sh_link,
1046 symbol.st_name);
1047 if (sym_name_str
1048 && compare_symbol_name(sym_name_str, sym_name, demangle))
1049 {
1050 // So we found a symbol (in the symbol table) that equals
1051 // sym_name. Now lets try to get its version and record it.
1052 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1053 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1054 sym_visibility =
1055 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1056
1057 if (get_version_for_symbol(elf_handle, i,
1058 /*get_def_version=*/true,
1059 ver))
1060 ABG_ASSERT(!ver.str().empty());
1061
1062 elf_symbol_sptr symbol_found =
1063 elf_symbol::create(env, i,
1064 symbol.st_size,
1065 sym_name_str,
1066 sym_type, sym_binding,
1067 symbol.st_shndx != SHN_UNDEF,
1068 symbol.st_shndx == SHN_COMMON,
1069 ver, sym_visibility);
1070 syms_found.push_back(symbol_found);
1071 found = true;
1072 }
1073
1074 if (stop_word & 1)
1075 // The last bit of the stop_word is 1. That means we need to
1076 // stop here. We reached the end of the chain of values
1077 // referenced by the hask bucket.
1078 break;
1079 }
1080 return found;
1081}
1082
1083/// Look into the symbol tables of the underlying elf file and find
1084/// the symbol we are being asked.
1085///
1086/// This function uses the elf hash table (be it the GNU hash table or
1087/// the sysv hash table) for the symbol lookup.
1088///
1089/// @param env the environment we are operating from.
1090///
1091/// @param elf_handle the elf handle to use.
1092///
1093/// @param ht_kind the kind of hash table to use. This is returned by
1094/// the function function find_hash_table_section_index.
1095///
1096/// @param ht_index the index (in the section headers table) of the
1097/// hash table section to use.
1098///
1099/// @param sym_tab_index the index (in section headers table) of the
1100/// symbol table index to use with this hash table.
1101///
1102/// @param symbol_name the name of the symbol to look for.
1103///
1104/// @param demangle if true, demangle @p sym_name.
1105///
1106/// @param syms_found the symbols that were actually found with the
1107/// name @p symbol_name.
1108///
1109/// @return true iff the function found the symbol from the elf hash
1110/// table.
1111static bool
1112lookup_symbol_from_elf_hash_tab(const environment& env,
1113 Elf* elf_handle,
1114 hash_table_kind ht_kind,
1115 size_t ht_index,
1116 size_t symtab_index,
1117 const string& symbol_name,
1118 bool demangle,
1119 vector<elf_symbol_sptr>& syms_found)
1120{
1121 if (elf_handle == 0 || symbol_name.empty())
1122 return false;
1123
1124 if (ht_kind == NO_HASH_TABLE_KIND)
1125 return false;
1126
1127 if (ht_kind == SYSV_HASH_TABLE_KIND)
1128 return lookup_symbol_from_sysv_hash_tab(env,
1129 elf_handle, symbol_name,
1130 ht_index,
1131 symtab_index,
1132 demangle,
1133 syms_found);
1134 else if (ht_kind == GNU_HASH_TABLE_KIND)
1135 return lookup_symbol_from_gnu_hash_tab(env,
1136 elf_handle, symbol_name,
1137 ht_index,
1138 symtab_index,
1139 demangle,
1140 syms_found);
1141 return false;
1142}
1143
1144/// Lookup a symbol from the symbol table directly.
1145///
1146///
1147/// @param env the environment we are operating from.
1148///
1149/// @param elf_handle the elf handle to use.
1150///
1151/// @param sym_name the name of the symbol to look up.
1152///
1153/// @param sym_tab_index the index (in the section headers table) of
1154/// the symbol table section.
1155///
1156/// @param demangle if true, demangle the names found in the symbol
1157/// table before comparing them with @p sym_name.
1158///
1159/// @param sym_name_found the actual name of the symbol found.
1160///
1161/// @param sym_type the type of the symbol found.
1162///
1163/// @param sym_binding the binding of the symbol found.
1164///
1165/// @param sym_versions the versions of the symbol found.
1166///
1167/// @return true iff the symbol was found.
1168static bool
1169lookup_symbol_from_symtab(const environment& env,
1170 Elf* elf_handle,
1171 const string& sym_name,
1172 size_t sym_tab_index,
1173 bool demangle,
1174 vector<elf_symbol_sptr>& syms_found)
1175{
1176 // TODO: read all of the symbol table, store it in memory in a data
1177 // structure that associates each symbol with its versions and in
1178 // which lookups of a given symbol is fast.
1179 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1180 ABG_ASSERT(sym_tab_section);
1181
1182 GElf_Shdr header_mem;
1183 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1184 &header_mem);
1185
1186 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1187 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1188 GElf_Sym* sym;
1189 char* name_str = 0;
1190 elf_symbol::version ver;
1191 bool found = false;
1192
1193 for (size_t i = 0; i < symcount; ++i)
1194 {
1195 GElf_Sym sym_mem;
1196 sym = gelf_getsym(symtab, i, &sym_mem);
1197 name_str = elf_strptr(elf_handle,
1198 sym_tab_header->sh_link,
1199 sym->st_name);
1200
1201 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1202 {
1203 elf_symbol::type sym_type =
1204 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1205 elf_symbol::binding sym_binding =
1206 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1207 elf_symbol::visibility sym_visibility =
1208 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1209 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1210 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1211
1212 if (get_version_for_symbol(elf_handle, i,
1213 /*get_def_version=*/sym_is_defined,
1214 ver))
1215 ABG_ASSERT(!ver.str().empty());
1216 elf_symbol_sptr symbol_found =
1217 elf_symbol::create(env, i, sym->st_size,
1218 name_str, sym_type,
1219 sym_binding, sym_is_defined,
1220 sym_is_common, ver, sym_visibility);
1221 syms_found.push_back(symbol_found);
1222 found = true;
1223 }
1224 }
1225
1226 if (found)
1227 return true;
1228
1229 return false;
1230}
1231
1232/// Look into the symbol tables of the underlying elf file and see
1233/// if we find a given symbol.
1234///
1235/// @param env the environment we are operating from.
1236///
1237/// @param symbol_name the name of the symbol to look for.
1238///
1239/// @param demangle if true, try to demangle the symbol name found in
1240/// the symbol table before comparing it to @p symbol_name.
1241///
1242/// @param syms_found the list of symbols found, with the name @p
1243/// symbol_name.
1244///
1245/// @param sym_type this is set to the type of the symbol found. This
1246/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1247/// STT_FUNC, STT_IFUNC, etc ...
1248///
1249/// Note that this parameter is set iff the function returns true.
1250///
1251/// @param sym_binding this is set to the binding of the symbol found.
1252/// This is a standard elf.h value of the symbol binding kind, that
1253/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1254///
1255/// @param symbol_versions the versions of the symbol @p symbol_name,
1256/// if it was found.
1257///
1258/// @return true iff a symbol with the name @p symbol_name was found.
1259static bool
1260lookup_symbol_from_elf(const environment& env,
1261 Elf* elf_handle,
1262 const string& symbol_name,
1263 bool demangle,
1264 vector<elf_symbol_sptr>& syms_found)
1265{
1266 size_t hash_table_index = 0, symbol_table_index = 0;
1267 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1268
1269 if (!demangle)
1270 ht_kind = find_hash_table_section_index(elf_handle,
1271 hash_table_index,
1272 symbol_table_index);
1273
1274 if (ht_kind == NO_HASH_TABLE_KIND)
1275 {
1276 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1277 return false;
1278
1279 return lookup_symbol_from_symtab(env,
1280 elf_handle,
1281 symbol_name,
1282 symbol_table_index,
1283 demangle,
1284 syms_found);
1285 }
1286
1287 return lookup_symbol_from_elf_hash_tab(env,
1288 elf_handle,
1289 ht_kind,
1290 hash_table_index,
1291 symbol_table_index,
1292 symbol_name,
1293 demangle,
1294 syms_found);
1295}
1296
1297/// Look into the symbol tables of the underlying elf file and see if
1298/// we find a given public (global or weak) symbol of function type.
1299///
1300/// @param env the environment we are operating from.
1301///
1302/// @param elf_handle the elf handle to use for the query.
1303///
1304/// @param symbol_name the function symbol to look for.
1305///
1306/// @param func_syms the vector of public functions symbols found, if
1307/// any.
1308///
1309/// @return true iff the symbol was found.
1310static bool
1311lookup_public_function_symbol_from_elf(environment& env,
1312 Elf* elf_handle,
1313 const string& symbol_name,
1314 vector<elf_symbol_sptr>& func_syms)
1315{
1316 vector<elf_symbol_sptr> syms_found;
1317 bool found = false;
1318
1319 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1320 /*demangle=*/false, syms_found))
1321 {
1322 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1323 i != syms_found.end();
1324 ++i)
1325 {
1326 elf_symbol::type type = (*i)->get_type();
1327 elf_symbol::binding binding = (*i)->get_binding();
1328
1329 if ((type == elf_symbol::FUNC_TYPE
1330 || type == elf_symbol::GNU_IFUNC_TYPE
1331 || type == elf_symbol::COMMON_TYPE)
1332 && (binding == elf_symbol::GLOBAL_BINDING
1333 || binding == elf_symbol::WEAK_BINDING))
1334 {
1335 func_syms.push_back(*i);
1336 found = true;
1337 }
1338 }
1339 }
1340
1341 return found;
1342}
1343
1344// ---------------------------------------
1345// <location expression evaluation types>
1346// ---------------------------------------
1347
1348/// An abstraction of a value representing the result of the
1349/// evaluation of a dwarf expression. This is abstraction represents
1350/// a partial view on the possible values because we are only
1351/// interested in extracting the latest and longuest constant
1352/// sub-expression of a given dwarf expression.
1353class expr_result
1354{
1355 bool is_const_;
1356 int64_t const_value_;
1357
1358public:
1359 expr_result()
1360 : is_const_(true),
1361 const_value_(0)
1362 {}
1363
1364 expr_result(bool is_const)
1365 : is_const_(is_const),
1366 const_value_(0)
1367 {}
1368
1369 explicit expr_result(int64_t v)
1370 :is_const_(true),
1371 const_value_(v)
1372 {}
1373
1374 /// @return true if the value is a constant. Otherwise, return
1375 /// false, meaning the value represents a quantity for which we need
1376 /// inferior (a running program) state to determine the value.
1377 bool
1378 is_const() const
1379 {return is_const_;}
1380
1381
1382 /// @param f a flag saying if the value is set to a constant or not.
1383 void
1384 is_const(bool f)
1385 {is_const_ = f;}
1386
1387 /// Get the current constant value iff this represents a
1388 /// constant.
1389 ///
1390 /// @param value the out parameter. Is set to the constant value of
1391 /// the @ref expr_result. This is set iff the function return true.
1392 ///
1393 ///@return true if this has a constant value, false otherwise.
1394 bool
1395 const_value(int64_t& value)
1396 {
1397 if (is_const())
1398 {
1399 value = const_value_;
1400 return true;
1401 }
1402 return false;
1403 }
1404
1405 /// Getter of the constant value of the current @ref expr_result.
1406 ///
1407 /// Note that the current @ref expr_result must be constant,
1408 /// otherwise the current process is aborted.
1409 ///
1410 /// @return the constant value of the current @ref expr_result.
1411 int64_t
1412 const_value() const
1413 {
1414 ABG_ASSERT(is_const());
1415 return const_value_;
1416 }
1417
1418 operator int64_t() const
1419 {return const_value();}
1420
1421 expr_result&
1422 operator=(const int64_t v)
1423 {
1424 const_value_ = v;
1425 return *this;
1426 }
1427
1428 bool
1429 operator==(const expr_result& o) const
1430 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1431
1432 bool
1433 operator>=(const expr_result& o) const
1434 {return const_value_ >= o.const_value_;}
1435
1436 bool
1437 operator<=(const expr_result& o) const
1438 {return const_value_ <= o.const_value_;}
1439
1440 bool
1441 operator>(const expr_result& o) const
1442 {return const_value_ > o.const_value_;}
1443
1444 bool
1445 operator<(const expr_result& o) const
1446 {return const_value_ < o.const_value_;}
1447
1448 expr_result
1449 operator+(const expr_result& v) const
1450 {
1451 expr_result r(*this);
1452 r.const_value_ += v.const_value_;
1453 r.is_const_ = r.is_const_ && v.is_const_;
1454 return r;
1455 }
1456
1457 expr_result&
1458 operator+=(int64_t v)
1459 {
1460 const_value_ += v;
1461 return *this;
1462 }
1463
1464 expr_result
1465 operator-(const expr_result& v) const
1466 {
1467 expr_result r(*this);
1468 r.const_value_ -= v.const_value_;
1469 r.is_const_ = r.is_const_ && v.is_const_;
1470 return r;
1471 }
1472
1473 expr_result
1474 operator%(const expr_result& v) const
1475 {
1476 expr_result r(*this);
1477 r.const_value_ %= v.const_value_;
1478 r.is_const_ = r.is_const_ && v.is_const();
1479 return r;
1480 }
1481
1482 expr_result
1483 operator*(const expr_result& v) const
1484 {
1485 expr_result r(*this);
1486 r.const_value_ *= v.const_value_;
1487 r.is_const_ = r.is_const_ && v.is_const();
1488 return r;
1489 }
1490
1491 expr_result
1492 operator|(const expr_result& v) const
1493 {
1494 expr_result r(*this);
1495 r.const_value_ |= v.const_value_;
1496 r.is_const_ = r.is_const_ && v.is_const_;
1497 return r;
1498 }
1499
1500 expr_result
1501 operator^(const expr_result& v) const
1502 {
1503 expr_result r(*this);
1504 r.const_value_ ^= v.const_value_;
1505 r.is_const_ = r.is_const_ && v.is_const_;
1506 return r;
1507 }
1508
1509 expr_result
1510 operator>>(const expr_result& v) const
1511 {
1512 expr_result r(*this);
1513 r.const_value_ = r.const_value_ >> v.const_value_;
1514 r.is_const_ = r.is_const_ && v.is_const_;
1515 return r;
1516 }
1517
1518 expr_result
1519 operator<<(const expr_result& v) const
1520 {
1521 expr_result r(*this);
1522 r.const_value_ = r.const_value_ << v.const_value_;
1523 r.is_const_ = r.is_const_ && v.is_const_;
1524 return r;
1525 }
1526
1527 expr_result
1528 operator~() const
1529 {
1530 expr_result r(*this);
1531 r.const_value_ = ~r.const_value_;
1532 return r;
1533 }
1534
1535 expr_result
1536 neg() const
1537 {
1538 expr_result r(*this);
1539 r.const_value_ = -r.const_value_;
1540 return r;
1541 }
1542
1543 expr_result
1544 abs() const
1545 {
1546 expr_result r = *this;
1547 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1548 return r;
1549 }
1550
1551 expr_result
1552 operator&(const expr_result& o)
1553 {
1554 expr_result r(*this);
1555 r.const_value_ &= o.const_value_;
1556 r.is_const_ = r.is_const_ && o.is_const_;
1557 return r;
1558 }
1559
1560 expr_result
1561 operator/(const expr_result& o)
1562 {
1563 expr_result r(*this);
1564 r.is_const_ = r.is_const_ && o.is_const_;
1565 return r.const_value() / o.const_value();
1566 }
1567};// class end expr_result;
1568
1569/// A class that implements a stack of @ref expr_result, to be used in
1570/// the engine evaluating DWARF expressions.
1571class expr_result_stack_type
1572{
1573 vector<expr_result> elems_;
1574
1575public:
1576
1577 expr_result_stack_type()
1578 {elems_.reserve(4);}
1579
1580 expr_result&
1581 operator[](unsigned i)
1582 {
1583 unsigned s = elems_.size();
1584 ABG_ASSERT(s > i);
1585 return elems_[s - 1 -i];
1586 }
1587
1588 const expr_result&
1589 operator[](unsigned i) const
1590 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1591
1592 unsigned
1593 size() const
1594 {return elems_.size();}
1595
1596 vector<expr_result>::reverse_iterator
1597 begin()
1598 {return elems_.rbegin();}
1599
1600 const vector<expr_result>::reverse_iterator
1601 begin() const
1602 {return const_cast<expr_result_stack_type*>(this)->begin();}
1603
1604 vector<expr_result>::reverse_iterator
1605 end()
1606 {return elems_.rend();}
1607
1608 const vector<expr_result>::reverse_iterator
1609 end() const
1610 {return const_cast<expr_result_stack_type*>(this)->end();}
1611
1612 expr_result&
1613 front()
1614 {return elems_.back();}
1615
1616 const expr_result&
1617 front() const
1618 {return const_cast<expr_result_stack_type*>(this)->front();}
1619
1620 void
1621 push_front(expr_result e)
1622 {elems_.push_back(e);}
1623
1624 expr_result
1625 pop_front()
1626 {
1627 expr_result r = front();
1628 elems_.pop_back();
1629 return r;
1630 }
1631
1632 void
1633 erase(vector<expr_result>::reverse_iterator i)
1634 {elems_.erase(--i.base());}
1635
1636 void
1637 clear()
1638 {elems_.clear();}
1639}; // end class expr_result_stack_type
1640
1641/// Abstraction of the evaluation context of a dwarf expression.
1642struct dwarf_expr_eval_context
1643{
1644 expr_result accum;
1645 expr_result_stack_type stack;
1646 // Is set to true if the result of the expression that got evaluated
1647 // is a TLS address.
1648 bool set_tls_addr;
1649
1650 dwarf_expr_eval_context()
1651 : accum(/*is_const=*/false),
1652 set_tls_addr(false)
1653 {
1654 stack.push_front(expr_result(true));
1655 }
1656
1657 void
1658 reset()
1659 {
1660 stack.clear();
1661 stack.push_front(expr_result(true));
1662 accum = expr_result(false);
1663 set_tls_addr = false;
1664 }
1665
1666 /// Set a flag to to tell that the result of the expression that got
1667 /// evaluated is a TLS address.
1668 ///
1669 /// @param f true iff the result of the expression that got
1670 /// evaluated is a TLS address, false otherwise.
1671 void
1672 set_tls_address(bool f)
1673 {set_tls_addr = f;}
1674
1675 /// Getter for the flag that tells if the result of the expression
1676 /// that got evaluated is a TLS address.
1677 ///
1678 /// @return true iff the result of the expression that got evaluated
1679 /// is a TLS address.
1680 bool
1681 set_tls_address() const
1682 {return set_tls_addr;}
1683
1684 expr_result
1685 pop()
1686 {
1687 expr_result r = stack.front();
1688 stack.pop_front();
1689 return r;
1690 }
1691
1692 void
1693 push(const expr_result& v)
1694 {stack.push_front(v);}
1695};//end class dwarf_expr_eval_context
1696
1697// ---------------------------------------
1698// </location expression evaluation types>
1699// ---------------------------------------
1700
1701class reader;
1702
1703typedef shared_ptr<reader> reader_sptr;
1704
1705/// The DWARF reader used to build the ABI corpus from debug info in
1706/// DWARF format.
1707///
1708/// This type is to be instanciated
1709/// abigail::dwarf::reader::create().
1710class reader : public elf_based_reader
1711{
1712public:
1713
1714 /// A set of containers that contains one container per kind of @ref
1715 /// die_source. This allows to associate DIEs to things, depending
1716 /// on the source of the DIE.
1717 template <typename ContainerType>
1718 class die_source_dependant_container_set
1719 {
1720 ContainerType primary_debug_info_container_;
1721 ContainerType alt_debug_info_container_;
1722 ContainerType type_unit_container_;
1723
1724 public:
1725
1726 /// Getter for the container associated to DIEs coming from a
1727 /// given @ref die_source.
1728 ///
1729 /// @param source the die_source for which we want the container.
1730 ///
1731 /// @return the container that associates DIEs coming from @p
1732 /// source to something.
1733 ContainerType&
1734 get_container(die_source source)
1735 {
1736 ContainerType *result = 0;
1737 switch (source)
1738 {
1739 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1740 result = &primary_debug_info_container_;
1741 break;
1742 case ALT_DEBUG_INFO_DIE_SOURCE:
1743 result = &alt_debug_info_container_;
1744 break;
1745 case TYPE_UNIT_DIE_SOURCE:
1746 result = &type_unit_container_;
1747 break;
1748 case NO_DEBUG_INFO_DIE_SOURCE:
1749 case NUMBER_OF_DIE_SOURCES:
1751 }
1752 return *result;
1753 }
1754
1755 /// Getter for the container associated to DIEs coming from a
1756 /// given @ref die_source.
1757 ///
1758 /// @param source the die_source for which we want the container.
1759 ///
1760 /// @return the container that associates DIEs coming from @p
1761 /// source to something.
1762 const ContainerType&
1763 get_container(die_source source) const
1764 {
1765 return const_cast<die_source_dependant_container_set*>(this)->
1766 get_container(source);
1767 }
1768
1769 /// Getter for the container associated to DIEs coming from the
1770 /// same source as a given DIE.
1771 ///
1772 /// @param rdr the DWARF reader to consider.
1773 ///
1774 /// @param die the DIE which should have the same source as the
1775 /// source of the container we want.
1776 ///
1777 /// @return the container that associates DIEs coming from the
1778 /// same source as @p die.
1779 ContainerType&
1780 get_container(const reader& rdr, const Dwarf_Die *die)
1781 {
1782 const die_source source = rdr.get_die_source(die);
1783 return get_container(source);
1784 }
1785
1786 /// Getter for the container associated to DIEs coming from the
1787 /// same source as a given DIE.
1788 ///
1789 /// @param rdr the DWARF reader to consider.
1790 ///
1791 /// @param die the DIE which should have the same source as the
1792 /// source of the container we want.
1793 ///
1794 /// @return the container that associates DIEs coming from the
1795 /// same source as @p die.
1796 const ContainerType&
1797 get_container(const reader& rdr, const Dwarf_Die *die) const
1798 {
1799 return const_cast<die_source_dependant_container_set*>(this)->
1800 get_container(rdr, die);
1801 }
1802
1803 /// Clear the container set.
1804 void
1805 clear()
1806 {
1807 primary_debug_info_container_.clear();
1808 alt_debug_info_container_.clear();
1809 type_unit_container_.clear();
1810 }
1811 }; // end die_dependant_container_set
1812
1813 unsigned short dwarf_version_;
1814 Dwarf_Die* cur_tu_die_;
1815 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1816 // A set of maps (one per kind of die source) that associates a decl
1817 // string representation with the DIEs (offsets) representing that
1818 // decl.
1819 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1820 decl_die_repr_die_offsets_maps_;
1821 // A set of maps (one per kind of die source) that associates a type
1822 // string representation with the DIEs (offsets) representing that
1823 // type.
1824 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1825 type_die_repr_die_offsets_maps_;
1826 mutable die_source_dependant_container_set<die_istring_map_type>
1827 die_qualified_name_maps_;
1828 mutable die_source_dependant_container_set<die_istring_map_type>
1829 die_pretty_repr_maps_;
1830 mutable die_source_dependant_container_set<die_istring_map_type>
1831 die_pretty_type_repr_maps_;
1832 // A set of maps (one per kind of die source) that associates the
1833 // offset of a decl die to its corresponding decl artifact.
1834 mutable die_source_dependant_container_set<die_artefact_map_type>
1835 decl_die_artefact_maps_;
1836 // A set of maps (one per kind of die source) that associates the
1837 // offset of a type die to its corresponding type artifact.
1838 mutable die_source_dependant_container_set<die_artefact_map_type>
1839 type_die_artefact_maps_;
1840 /// A set of vectors (one per kind of die source) that associates
1841 /// the offset of a type DIE to the offset of its canonical DIE.
1842 mutable die_source_dependant_container_set<offset_offset_map_type>
1843 canonical_type_die_offsets_;
1844 /// A set of vectors (one per kind of die source) that associates
1845 /// the offset of a decl DIE to the offset of its canonical DIE.
1846 mutable die_source_dependant_container_set<offset_offset_map_type>
1847 canonical_decl_die_offsets_;
1848 /// A map that associates a function type representations to
1849 /// function types, inside a translation unit.
1850 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1851 /// A map that associates a pair of DIE offsets to the result of the
1852 /// comparison of that pair.
1853 mutable std::unordered_map<std::pair<offset_type,offset_type>,
1855 dwarf_offset_pair_hash> die_comparison_results_;
1856 // The set of types pair that have been canonical-type-propagated.
1857 mutable offset_pair_set_type propagated_types_;
1858 die_class_or_union_map_type die_wip_classes_map_;
1859 die_class_or_union_map_type alternate_die_wip_classes_map_;
1860 die_class_or_union_map_type type_unit_die_wip_classes_map_;
1861 die_function_type_map_type die_wip_function_types_map_;
1862 die_function_type_map_type alternate_die_wip_function_types_map_;
1863 die_function_type_map_type type_unit_die_wip_function_types_map_;
1864 die_function_decl_map_type die_function_with_no_symbol_map_;
1865 vector<type_base_sptr> types_to_canonicalize_;
1866 string_classes_or_unions_map decl_only_classes_map_;
1867 string_enums_map decl_only_enums_map_;
1868 die_tu_map_type die_tu_map_;
1869 translation_unit_sptr cur_tu_;
1870 scope_decl_sptr nil_scope_;
1871 scope_stack_type scope_stack_;
1872 offset_offset_map_type primary_die_parent_map_;
1873 // A map that associates each tu die to a vector of unit import
1874 // points, in the main debug info
1875 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1876 // A map that associates each tu die to a vector of unit import
1877 // points, in the alternate debug info
1878 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1879 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1880 // A DIE -> parent map for DIEs coming from the alternate debug info
1881 // file.
1882 offset_offset_map_type alternate_die_parent_map_;
1883 offset_offset_map_type type_section_die_parent_map_;
1884 list<var_decl_sptr> var_decls_to_add_;
1885#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1886 bool debug_die_canonicalization_is_on_;
1887 bool use_canonical_die_comparison_;
1888#endif
1889 mutable size_t compare_count_;
1890 mutable size_t canonical_propagated_count_;
1891 mutable size_t cancelled_propagation_count_;
1892 mutable optional<bool> leverage_dwarf_factorization_;
1893
1894protected:
1895
1896 reader() = delete;
1897
1898 /// Constructor of reader.
1899 ///
1900 /// @param elf_path the path to the elf file the context is to be
1901 /// used for.
1902 ///
1903 /// @param debug_info_root_paths a vector of pointers to the path to
1904 /// the root directory under which the debug info is to be found for
1905 /// @p elf_path. Leave this empty if the debug info is not in a
1906 /// split file.
1907 ///
1908 /// @param environment the environment used by the current context.
1909 /// This environment contains resources needed by the DWARF reader and by
1910 /// the types and declarations that are to be created later. Note
1911 /// that ABI artifacts that are to be compared all need to be
1912 /// created within the same environment.
1913 ///
1914 /// Please also note that the life time of this environment object
1915 /// must be greater than the life time of the resulting @ref
1916 /// reader the context uses resources that are allocated in
1917 /// the environment.
1918 ///
1919 /// @param load_all_types if set to false only the types that are
1920 /// reachable from publicly exported declarations (of functions and
1921 /// variables) are read. If set to true then all types found in the
1922 /// debug information are loaded.
1923 ///
1924 /// @param linux_kernel_mode if set to true, then consider the special
1925 /// linux kernel symbol tables when determining if a symbol is
1926 /// exported or not.
1927 reader(const string& elf_path,
1928 const vector<char**>& debug_info_root_paths,
1929 environment& environment,
1930 bool load_all_types,
1931 bool linux_kernel_mode)
1932 : elf_based_reader(elf_path,
1933 debug_info_root_paths,
1934 environment)
1935 {
1936 initialize(load_all_types, linux_kernel_mode);
1937 }
1938
1939public:
1940
1941 /// Initializer of reader.
1942 ///
1943 /// Resets the reader so that it can be re-used to read another binary.
1944 ///
1945 /// @param load_all_types if set to false only the types that are
1946 /// reachable from publicly exported declarations (of functions and
1947 /// variables) are read. If set to true then all types found in the
1948 /// debug information are loaded.
1949 ///
1950 /// @param linux_kernel_mode if set to true, then consider the
1951 /// special linux kernel symbol tables when determining if a symbol
1952 /// is exported or not.
1953 void
1954 initialize(bool load_all_types, bool linux_kernel_mode)
1955 {
1956 dwarf_version_ = 0;
1957 cur_tu_die_ = 0;
1958 decl_die_repr_die_offsets_maps_.clear();
1959 type_die_repr_die_offsets_maps_.clear();
1960 die_qualified_name_maps_.clear();
1961 die_pretty_repr_maps_.clear();
1962 die_pretty_type_repr_maps_.clear();
1963 decl_die_artefact_maps_.clear();
1964 type_die_artefact_maps_.clear();
1965 canonical_type_die_offsets_.clear();
1966 canonical_decl_die_offsets_.clear();
1967 die_wip_classes_map_.clear();
1968 alternate_die_wip_classes_map_.clear();
1969 type_unit_die_wip_classes_map_.clear();
1970 die_wip_function_types_map_.clear();
1971 alternate_die_wip_function_types_map_.clear();
1972 type_unit_die_wip_function_types_map_.clear();
1973 die_function_with_no_symbol_map_.clear();
1974 types_to_canonicalize_.clear();
1975 decl_only_classes_map_.clear();
1976 die_tu_map_.clear();
1977 corpus().reset();
1978 corpus_group().reset();
1979 cur_tu_.reset();
1980 primary_die_parent_map_.clear();
1981 tu_die_imported_unit_points_map_.clear();
1982 alt_tu_die_imported_unit_points_map_.clear();
1983 type_units_tu_die_imported_unit_points_map_.clear();
1984 alternate_die_parent_map_.clear();
1985 type_section_die_parent_map_.clear();
1986 var_decls_to_add_.clear();
1987 clear_per_translation_unit_data();
1988 options().load_in_linux_kernel_mode = linux_kernel_mode;
1989 options().load_all_types = load_all_types;
1990#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1991 debug_die_canonicalization_is_on_ =
1992 env().debug_die_canonicalization_is_on();
1993 use_canonical_die_comparison_ = true;
1994#endif
1995 compare_count_ = 0;
1996 canonical_propagated_count_ = 0;
1997 cancelled_propagation_count_ = 0;
1998 load_in_linux_kernel_mode(linux_kernel_mode);
1999 }
2000
2001 /// Initializer of reader.
2002 ///
2003 /// Resets the reader so that it can be re-used to read another binary.
2004 ///
2005 /// @param elf_path the path to the new ELF file.
2006 ///
2007 /// @param debug_info_root_paths the vector of debug-info path to
2008 /// look for split debug info.
2009 ///
2010 /// @param load_all_types if set to false only the types that are
2011 /// reachable from publicly exported declarations (of functions and
2012 /// variables) are read. If set to true then all types found in the
2013 /// debug information are loaded.
2014 ///
2015 /// @param linux_kernel_mode if set to true, then consider the
2016 /// special linux kernel symbol tables when determining if a symbol
2017 /// is exported or not.
2018 void
2019 initialize(const string& elf_path,
2020 const vector<char**>& debug_info_root_paths,
2021 bool load_all_types,
2022 bool linux_kernel_mode)
2023 {
2024 elf_based_reader::initialize(elf_path, debug_info_root_paths);
2025 initialize(load_all_types, linux_kernel_mode);
2026 }
2027
2028 /// Create an instance of DWARF Reader.
2029 ///
2030 /// @param elf_path the path to the ELF file to read from.
2031 ///
2032 /// @param debug_info_root_paths a vector of paths where to look up
2033 /// split debug info files.
2034 ///
2035 /// @param environment the environment to be used by the reader.
2036 ///
2037 /// @param load_all_types if set to false only the types that are
2038 /// reachable from publicly exported declarations (of functions and
2039 /// variables) are read. If set to true then all types found in the
2040 /// debug information are loaded.
2041 ///
2042 /// @param linux_kernel_mode if set to true, then consider the
2043 /// special linux kernel symbol tables when determining if a symbol
2044 /// is exported or not.
2045 static dwarf::reader_sptr
2046 create(const std::string& elf_path,
2047 const vector<char**>& debug_info_root_paths,
2048 environment& environment,
2049 bool load_all_types,
2050 bool linux_kernel_mode)
2051 {
2052 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2053 environment, load_all_types,
2054 linux_kernel_mode));
2055 return result;
2056 }
2057
2058 /// Destructor of the @ref reader type.
2059 ~reader()
2060 {
2061 }
2062
2063 /// Read and analyze the ELF and DWARF information associated with
2064 /// the underlying ELF file and build an ABI corpus out of it.
2065 ///
2066 /// @param status output parameter. This is set to the status of
2067 /// the analysis of the debug info.
2068 ///
2069 /// @return the resulting ABI corpus.
2070 corpus_sptr
2071 read_corpus(status& status)
2072 {
2073 status = STATUS_UNKNOWN;
2074
2075 // Load the generic ELF parts of the corpus.
2077
2078 if (!(status & STATUS_OK))
2079 {
2080 // Something went badly wrong. There is nothing we can do
2081 // with this ELF file. Bail out.
2082 return corpus_sptr();
2083 }
2084
2085 // If we couldn't find debug info from the elf path, then say it.
2086 if (dwarf_debug_info() == nullptr)
2088
2089 {
2090 string alt_di_path;
2091 if (refers_to_alt_debug_info(alt_di_path)
2094 }
2095
2096 if (// If debug info was found but not the required alternate debug
2097 // info ...
2098 ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2099 && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2100 // ... then we cannot handle the binary.
2101 return corpus_sptr();
2102
2103 // Read the variable and function descriptions from the debug info
2104 // we have, through the dwfl handle.
2105 corpus_sptr corp = read_debug_info_into_corpus();
2106
2107 status |= STATUS_OK;
2108
2109 return corp;
2110 }
2111
2112 /// Read an analyze the DWARF information.
2113 ///
2114 /// Construct an ABI corpus from it.
2115 ///
2116 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2117 ///
2118 /// @return the resulting ABI corpus.
2119 corpus_sptr
2120 read_debug_info_into_corpus()
2121 {
2122 clear_per_corpus_data();
2123
2124 // First set some mundane properties of the corpus gathered from
2125 // ELF.
2126 corpus::origin origin = corpus()->get_origin();
2127 origin |= corpus::DWARF_ORIGIN;
2128 corpus()->set_origin(origin);
2129
2130 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2131 && !env().user_set_analyze_exported_interfaces_only())
2132 // So we are looking at the Linux Kernel and the user has not set
2133 // any particular option regarding the amount of types to analyse.
2134 // In that case, we need to only analyze types that are reachable
2135 // from exported interfaces otherwise we get such a massive amount
2136 // of type DIEs to look at that things are just too slow down the
2137 // road.
2138 env().analyze_exported_interfaces_only(true);
2139
2140 corpus()->set_soname(dt_soname());
2141 corpus()->set_needed(dt_needed());
2142 corpus()->set_architecture_name(elf_architecture());
2143 // Set symbols information to the corpus.
2144 corpus()->set_symtab(symtab());
2145
2146 // Get out now if no debug info is found or if the symbol table is
2147 // empty.
2148 if (!dwarf_debug_info()
2149 || !corpus()->get_symtab()
2150 || !corpus()->get_symtab()->has_symbols())
2151 return corpus();
2152
2153 uint8_t address_size = 0;
2154 size_t header_size = 0;
2155
2156#ifdef WITH_DEBUG_SELF_COMPARISON
2157 if (env().self_comparison_debug_is_on())
2158 env().set_self_comparison_debug_input(corpus());
2159#endif
2160
2161 env().priv_->do_log(do_log());
2162
2163 // Walk all the DIEs of the debug info to build a DIE -> parent map
2164 // useful for get_die_parent() to work.
2165 {
2166 tools_utils::timer t;
2167 if (do_log())
2168 {
2169 cerr << "building die -> parent maps ...";
2170 t.start();
2171 }
2172
2173 build_die_parent_maps();
2174
2175 if (do_log())
2176 {
2177 t.stop();
2178 cerr << " DONE@" << corpus()->get_path()
2179 << ":"
2180 << t
2181 << "\n";
2182 }
2183 }
2184
2185 env().canonicalization_is_done(false);
2186
2187 {
2188 tools_utils::timer t;
2189 if (do_log())
2190 {
2191 cerr << "building the libabigail internal representation ...\n";
2192 t.start();
2193 }
2194 // And now walk all the DIEs again to build the libabigail IR.
2195 Dwarf_Half dwarf_vers = 0;
2196 for (Dwarf_Off offset = 0, next_offset = 0;
2197 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2198 offset, &next_offset, &header_size,
2199 &dwarf_vers, NULL, &address_size, NULL,
2200 NULL, NULL) == 0);
2201 offset = next_offset)
2202 {
2203 Dwarf_Off die_offset = offset + header_size;
2204 Dwarf_Die unit;
2205 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2206 die_offset, &unit)
2207 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2208 continue;
2209
2210 dwarf_version(dwarf_vers);
2211
2212 address_size *= 8;
2213
2214 // Build a translation_unit IR node from cu; note that cu must
2215 // be a DW_TAG_compile_unit die.
2216 translation_unit_sptr ir_node =
2217 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2218 ABG_ASSERT(ir_node);
2219 }
2220 if (do_log())
2221 {
2222 t.stop();
2223 cerr << "building the libabigail internal representation "
2224 << "DONE for corpus << corpus()->get_path()"
2225 << " in :"
2226 << t
2227 << "\n";
2228
2229 cerr << "Number of aggregate types compared: "
2230 << compare_count_ << "\n"
2231 << "Number of canonical types propagated: "
2232 << canonical_propagated_count_ << "\n"
2233 << "Number of cancelled propagated canonical types:"
2234 << cancelled_propagation_count_ << "\n";
2235 }
2236 }
2237
2238 {
2239 tools_utils::timer t;
2240 if (do_log())
2241 {
2242 cerr << "resolving declaration only classes ...";
2243 t.start();
2244 }
2245 resolve_declaration_only_classes();
2246 if (do_log())
2247 {
2248 t.stop();
2249 cerr << " DONE@" << corpus()->get_path()
2250 << ":"
2251 << t
2252 <<"\n";
2253 }
2254 }
2255
2256 {
2257 tools_utils::timer t;
2258 if (do_log())
2259 {
2260 cerr << "resolving declaration only enums ...";
2261 t.start();
2262 }
2263 resolve_declaration_only_enums();
2264 if (do_log())
2265 {
2266 t.stop();
2267 cerr << " DONE@" << corpus()->get_path()
2268 << ":"
2269 << t
2270 <<"\n";
2271 }
2272 }
2273
2274 {
2275 tools_utils::timer t;
2276 if (do_log())
2277 {
2278 cerr << "fixing up functions with linkage name but "
2279 << "no advertised underlying symbols ....";
2280 t.start();
2281 }
2282 fixup_functions_with_no_symbols();
2283 if (do_log())
2284 {
2285 t.stop();
2286 cerr << " DONE@" << corpus()->get_path()
2287 <<":"
2288 << t
2289 <<"\n";
2290 }
2291 }
2292
2293 /// Now, look at the types that needs to be canonicalized after the
2294 /// translation has been constructed (which is just now) and
2295 /// canonicalize them.
2296 ///
2297 /// These types need to be constructed at the end of the translation
2298 /// unit reading phase because some types are modified by some DIEs
2299 /// even after the principal DIE describing the type has been read;
2300 /// this happens for clones of virtual destructors (for instance) or
2301 /// even for some static data members. We need to do that for types
2302 /// are in the alternate debug info section and for types that in
2303 /// the main debug info section.
2304 {
2305 tools_utils::timer t;
2306 if (do_log())
2307 {
2308 cerr << "perform late type canonicalizing ...\n";
2309 t.start();
2310 }
2311
2312 perform_late_type_canonicalizing();
2313 if (do_log())
2314 {
2315 t.stop();
2316 cerr << "late type canonicalizing DONE for "
2317 << corpus()->get_path()
2318 << " in :"
2319 << t
2320 << "\n";
2321 }
2322 }
2323
2324 env().canonicalization_is_done(true);
2325
2326 {
2327 tools_utils::timer t;
2328 if (do_log())
2329 {
2330 cerr << "sort functions and variables ...";
2331 t.start();
2332 }
2333 corpus()->sort_functions();
2334 corpus()->sort_variables();
2335 if (do_log())
2336 {
2337 t.stop();
2338 cerr << " DONE@" << corpus()->get_path()
2339 << ":"
2340 << t
2341 <<" \n";
2342 }
2343 }
2344
2345 return corpus();
2346 }
2347
2348 /// Clear the data that is relevant only for the current translation
2349 /// unit being read. The rest of the data is relevant for the
2350 /// entire ABI corpus.
2351 void
2352 clear_per_translation_unit_data()
2353 {
2354 while (!scope_stack().empty())
2355 scope_stack().pop();
2356 var_decls_to_re_add_to_tree().clear();
2357 per_tu_repr_to_fn_type_maps().clear();
2358 }
2359
2360 /// Clear the data that is relevant for the current corpus being
2361 /// read.
2362 void
2363 clear_per_corpus_data()
2364 {
2365 die_qualified_name_maps_.clear();
2366 die_pretty_repr_maps_.clear();
2367 die_pretty_type_repr_maps_.clear();
2368 clear_types_to_canonicalize();
2369 }
2370
2371 /// Getter for the current environment.
2372 ///
2373 /// @return the current environment.
2374 environment&
2375 env()
2376 {return options().env;}
2377
2378 /// Getter for the current environment.
2379 ///
2380 /// @return the current environment.
2381 const environment&
2382 env() const
2383 {return const_cast<reader*>(this)->env();}
2384
2385 /// Getter for the flag that tells us if we are dropping functions
2386 /// and variables that have undefined symbols.
2387 ///
2388 /// @return true iff we are dropping functions and variables that have
2389 /// undefined symbols.
2390 bool
2391 drop_undefined_syms() const
2392 {return options().drop_undefined_syms;}
2393
2394 /// Setter for the flag that tells us if we are dropping functions
2395 /// and variables that have undefined symbols.
2396 ///
2397 /// @param f the new value of the flag.
2398 void
2399 drop_undefined_syms(bool f)
2400 {options().drop_undefined_syms = f;}
2401
2402 /// Getter of the DWARF version.
2403 unsigned short
2404 dwarf_version() const
2405 {return dwarf_version_;}
2406
2407 void
2408 dwarf_version(unsigned short v)
2409 {dwarf_version_ = v;}
2410
2411 /// Return the ELF descriptor used for DWARF access.
2412 ///
2413 /// This can be the same as reader::elf_handle() above, if the
2414 /// DWARF info is in the same ELF file as the one of the binary we
2415 /// are analizing. It is different if e.g, the debug info is split
2416 /// from the ELF file we are analizing.
2417 ///
2418 /// @return a pointer to the ELF descriptor used to access debug
2419 /// info.
2420 Elf*
2421 dwarf_elf_handle() const
2422 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2423
2424 /// Test if the debug information is in a separate ELF file wrt the
2425 /// main ELF file of the program (application or shared library) we
2426 /// are analizing.
2427 ///
2428 /// @return true if the debug information is in a separate ELF file
2429 /// compared to the main ELF file of the program (application or
2430 /// shared library) that we are looking at.
2431 bool
2432 dwarf_is_splitted() const
2433 {return dwarf_elf_handle() != elf_handle();}
2434
2435 /// Return the correct debug info, depending on the DIE source we
2436 /// are looking at.
2437 ///
2438 /// @param source the DIE source to consider.
2439 ///
2440 /// @return the right debug info, depending on @p source.
2441 const Dwarf*
2442 dwarf_per_die_source(die_source source) const
2443 {
2444 const Dwarf *result = 0;
2445 switch(source)
2446 {
2447 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2448 case TYPE_UNIT_DIE_SOURCE:
2449 result = dwarf_debug_info();
2450 break;
2451 case ALT_DEBUG_INFO_DIE_SOURCE:
2452 result = alternate_dwarf_debug_info();
2453 break;
2454 case NO_DEBUG_INFO_DIE_SOURCE:
2455 case NUMBER_OF_DIE_SOURCES:
2457 }
2458 return result;
2459 }
2460
2461 /// Return the path to the ELF path we are reading.
2462 ///
2463 /// @return the elf path.
2464 const string&
2465 elf_path() const
2466 {return corpus_path();}
2467
2468 const Dwarf_Die*
2469 cur_tu_die() const
2470 {return cur_tu_die_;}
2471
2472 void
2473 cur_tu_die(Dwarf_Die* cur_tu_die)
2474 {cur_tu_die_ = cur_tu_die;}
2475
2476 dwarf_expr_eval_context&
2477 dwarf_expr_eval_ctxt() const
2478 {return dwarf_expr_eval_context_;}
2479
2480 /// Getter of the maps set that associates a representation of a
2481 /// decl DIE to a vector of offsets of DIEs having that representation.
2482 ///
2483 /// @return the maps set that associates a representation of a decl
2484 /// DIE to a vector of offsets of DIEs having that representation.
2485 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2486 decl_die_repr_die_offsets_maps() const
2487 {return decl_die_repr_die_offsets_maps_;}
2488
2489 /// Getter of the maps set that associates a representation of a
2490 /// decl DIE to a vector of offsets of DIEs having that representation.
2491 ///
2492 /// @return the maps set that associates a representation of a decl
2493 /// DIE to a vector of offsets of DIEs having that representation.
2494 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2495 decl_die_repr_die_offsets_maps()
2496 {return decl_die_repr_die_offsets_maps_;}
2497
2498 /// Getter of the maps set that associate a representation of a type
2499 /// DIE to a vector of offsets of DIEs having that representation.
2500 ///
2501 /// @return the maps set that associate a representation of a type
2502 /// DIE to a vector of offsets of DIEs having that representation.
2503 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2504 type_die_repr_die_offsets_maps() const
2505 {return type_die_repr_die_offsets_maps_;}
2506
2507 /// Getter of the maps set that associate a representation of a type
2508 /// DIE to a vector of offsets of DIEs having that representation.
2509 ///
2510 /// @return the maps set that associate a representation of a type
2511 /// DIE to a vector of offsets of DIEs having that representation.
2512 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2513 type_die_repr_die_offsets_maps()
2514 {return type_die_repr_die_offsets_maps_;}
2515
2516
2517 /// Compute the offset of the canonical DIE of a given DIE.
2518 ///
2519 /// @param die the DIE to consider.
2520 ///
2521 /// @param canonical_die_offset out parameter. This is set to the
2522 /// resulting canonical DIE that was computed.
2523 ///
2524 /// @param die_as_type if yes, it means @p die has to be considered
2525 /// as a type.
2526 void
2527 compute_canonical_die_offset(const Dwarf_Die *die,
2528 Dwarf_Off &canonical_die_offset,
2529 bool die_as_type) const
2530 {
2531 offset_offset_map_type &canonical_dies =
2532 die_as_type
2533 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2534 get_container(*this, die)
2535 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2536 get_container(*this, die);
2537
2538 Dwarf_Die canonical_die;
2539 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2540
2541 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2542 }
2543
2544 /// Compute (find) the canonical DIE of a given DIE.
2545 ///
2546 /// @param die the DIE to consider.
2547 ///
2548 /// @param canonical_dies the vector in which the canonical dies ar
2549 /// stored. The index of each element is the offset of the DIE we
2550 /// want the canonical DIE for. And the value of the element at
2551 /// that index is the canonical DIE offset we are looking for.
2552 ///
2553 /// @param canonical_die_offset out parameter. This is set to the
2554 /// resulting canonical DIE that was computed.
2555 ///
2556 /// @param die_as_type if yes, it means @p die has to be considered
2557 /// as a type.
2558 void
2559 compute_canonical_die(const Dwarf_Die *die,
2560 offset_offset_map_type& canonical_dies,
2561 Dwarf_Die &canonical_die,
2562 bool die_as_type) const
2563 {
2564 const die_source source = get_die_source(die);
2565
2566 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2567
2568 compute_canonical_die(die_offset, source,
2569 canonical_dies,
2570 canonical_die, die_as_type);
2571 }
2572
2573 /// Compute (find) the canonical DIE of a given DIE.
2574 ///
2575 /// @param die_offset the offset of the DIE to consider.
2576 ///
2577 /// @param source the source of the DIE to consider.
2578 ///
2579 /// @param canonical_dies the vector in which the canonical dies ar
2580 /// stored. The index of each element is the offset of the DIE we
2581 /// want the canonical DIE for. And the value of the element at
2582 /// that index is the canonical DIE offset we are looking for.
2583 ///
2584 /// @param canonical_die_offset out parameter. This is set to the
2585 /// resulting canonical DIE that was computed.
2586 ///
2587 /// @param die_as_type if yes, it means @p die has to be considered
2588 /// as a type.
2589 void
2590 compute_canonical_die(Dwarf_Off die_offset,
2591 die_source source,
2592 offset_offset_map_type& canonical_dies,
2593 Dwarf_Die &canonical_die,
2594 bool die_as_type) const
2595 {
2596 // The map that associates the string representation of 'die'
2597 // with a vector of offsets of potentially equivalent DIEs.
2599 die_as_type
2600 ? (const_cast<reader*>(this)->
2601 type_die_repr_die_offsets_maps().get_container(source))
2602 : (const_cast<reader*>(this)->
2603 decl_die_repr_die_offsets_maps().get_container(source));
2604
2605 Dwarf_Die die;
2606 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2607 die_offset, &die));
2608
2609 // The variable repr is the the string representation of 'die'.
2610 //
2611 // Even if die_as_type is true -- which means that 'die' is said
2612 // to be considered as a type -- we always consider a
2613 // DW_TAG_subprogram DIE as a decl here, as far as its string
2614 // representation is concerned.
2615 interned_string name =
2616 (die_as_type)
2617 ? get_die_pretty_type_representation(&die, /*where=*/0)
2618 : get_die_pretty_representation(&die, /*where=*/0);
2619
2620 Dwarf_Off canonical_die_offset = 0;
2621 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2622 if (i == map.end())
2623 {
2624 dwarf_offsets_type offsets;
2625 offsets.push_back(die_offset);
2626 map[name] = offsets;
2627 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2628 get_die_from_offset(source, die_offset, &canonical_die);
2629 return;
2630 }
2631
2632 Dwarf_Off cur_die_offset;
2633 Dwarf_Die potential_canonical_die;
2634 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2635 o != i->second.end();
2636 ++o)
2637 {
2638 cur_die_offset = *o;
2639 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2640 if (compare_dies(*this, &die, &potential_canonical_die,
2641 /*update_canonical_dies_on_the_fly=*/false))
2642 {
2643 canonical_die_offset = cur_die_offset;
2644 set_canonical_die_offset(canonical_dies, die_offset,
2645 canonical_die_offset);
2646 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2647 return;
2648 }
2649 }
2650
2651 canonical_die_offset = die_offset;
2652 i->second.push_back(die_offset);
2653 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2654 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2655 }
2656
2657 /// Getter of the canonical DIE of a given DIE.
2658 ///
2659 /// @param die the DIE to consider.
2660 ///
2661 /// @param canonical_die output parameter. Is set to the resulting
2662 /// canonical die, if this function returns true.
2663 ///
2664 /// @param where the offset of the logical DIE we are supposed to be
2665 /// calling this function from. If set to zero this means this is
2666 /// to be ignored.
2667 ///
2668 /// @param die_as_type if set to yes, it means @p die is to be
2669 /// considered as a type DIE.
2670 ///
2671 /// @return true iff a canonical DIE was found for @p die.
2672 bool
2673 get_canonical_die(const Dwarf_Die *die,
2674 Dwarf_Die &canonical_die,
2675 size_t where,
2676 bool die_as_type)
2677 {
2678 const die_source source = get_die_source(die);
2679
2680 offset_offset_map_type &canonical_dies =
2681 die_as_type
2682 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2683 get_container(source)
2684 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2685 get_container(source);
2686
2687 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2688 if (Dwarf_Off canonical_die_offset =
2689 get_canonical_die_offset(canonical_dies, die_offset))
2690 {
2691 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2692 return true;
2693 }
2694
2695 // The map that associates the string representation of 'die'
2696 // with a vector of offsets of potentially equivalent DIEs.
2698 die_as_type
2699 ? (const_cast<reader*>(this)->
2700 type_die_repr_die_offsets_maps().get_container(*this, die))
2701 : (const_cast<reader*>(this)->
2702 decl_die_repr_die_offsets_maps().get_container(*this, die));
2703
2704 // The variable repr is the the string representation of 'die'.
2705 //
2706 // Even if die_as_type is true -- which means that 'die' is said
2707 // to be considered as a type -- we always consider a
2708 // DW_TAG_subprogram DIE as a decl here, as far as its string
2709 // representation is concerned.
2710 interned_string name =
2711 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2712 ? get_die_pretty_type_representation(die, where)
2713 : get_die_pretty_representation(die, where);
2714
2715 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2716 if (i == map.end())
2717 return false;
2718
2719 Dwarf_Off cur_die_offset;
2720 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2721 o != i->second.end();
2722 ++o)
2723 {
2724 cur_die_offset = *o;
2725 get_die_from_offset(source, cur_die_offset, &canonical_die);
2726 // compare die and canonical_die.
2727 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2728 die, &canonical_die,
2729 /*update_canonical_dies_on_the_fly=*/true))
2730 {
2731 set_canonical_die_offset(canonical_dies,
2732 die_offset,
2733 cur_die_offset);
2734 return true;
2735 }
2736 }
2737
2738 return false;
2739 }
2740
2741 /// Retrieve the canonical DIE of a given DIE.
2742 ///
2743 /// The canonical DIE is a DIE that is structurally equivalent to
2744 /// this one.
2745 ///
2746 /// Note that this function caches the canonical DIE that was
2747 /// computed. Subsequent invocations of this function on the same
2748 /// DIE return the same cached DIE.
2749 ///
2750 /// @param die the DIE to get a canonical type for.
2751 ///
2752 /// @param canonical_die the resulting canonical DIE.
2753 ///
2754 /// @param where the offset of the logical DIE we are supposed to be
2755 /// calling this function from. If set to zero this means this is
2756 /// to be ignored.
2757 ///
2758 /// @param die_as_type if true, consider DIE is a type.
2759 ///
2760 /// @return true if an *existing* canonical DIE was found.
2761 /// Otherwise, @p die is considered as being a canonical DIE for
2762 /// itself. @p canonical_die is thus set to the canonical die in
2763 /// either cases.
2764 bool
2765 get_or_compute_canonical_die(const Dwarf_Die* die,
2766 Dwarf_Die& canonical_die,
2767 size_t where,
2768 bool die_as_type) const
2769 {
2770 const die_source source = get_die_source(die);
2771
2772 offset_offset_map_type &canonical_dies =
2773 die_as_type
2774 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2775 get_container(source)
2776 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2777 get_container(source);
2778
2779 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2780
2781 if (Dwarf_Off canonical_die_offset =
2782 get_canonical_die_offset(canonical_dies,
2783 initial_die_offset))
2784 {
2785 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2786 return true;
2787 }
2788
2789 if (!is_type_die_to_be_canonicalized(die))
2790 return false;
2791
2792 // The map that associates the string representation of 'die'
2793 // with a vector of offsets of potentially equivalent DIEs.
2795 die_as_type
2796 ? (const_cast<reader*>(this)->
2797 type_die_repr_die_offsets_maps().get_container(*this, die))
2798 : (const_cast<reader*>(this)->
2799 decl_die_repr_die_offsets_maps().get_container(*this, die));
2800
2801 // The variable repr is the the string representation of 'die'.
2802 //
2803 // Even if die_as_type is true -- which means that 'die' is said
2804 // to be considered as a type -- we always consider a
2805 // DW_TAG_subprogram DIE as a decl here, as far as its string
2806 // representation is concerned.
2807 interned_string name =
2808 (die_as_type)
2809 ? get_die_pretty_type_representation(die, where)
2810 : get_die_pretty_representation(die, where);
2811
2812 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2813 if (i == map.end())
2814 {
2815 dwarf_offsets_type offsets;
2816 offsets.push_back(initial_die_offset);
2817 map[name] = offsets;
2818 get_die_from_offset(source, initial_die_offset, &canonical_die);
2819 set_canonical_die_offset(canonical_dies,
2820 initial_die_offset,
2821 initial_die_offset);
2822 return false;
2823 }
2824
2825 // walk i->second without any iterator (using a while loop rather
2826 // than a for loop) because compare_dies might add new content to
2827 // the end of the i->second vector during the walking.
2828 dwarf_offsets_type::size_type n = 0, s = i->second.size();
2829 while (n < s)
2830 {
2831 Dwarf_Off die_offset = i->second[n];
2832 get_die_from_offset(source, die_offset, &canonical_die);
2833 // compare die and canonical_die.
2834 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2835 die, &canonical_die,
2836 /*update_canonical_dies_on_the_fly=*/true))
2837 {
2838 set_canonical_die_offset(canonical_dies,
2839 initial_die_offset,
2840 die_offset);
2841 return true;
2842 }
2843 ++n;
2844 }
2845
2846 // We didn't find a canonical DIE for 'die'. So let's consider
2847 // that it is its own canonical DIE.
2848 get_die_from_offset(source, initial_die_offset, &canonical_die);
2849 i->second.push_back(initial_die_offset);
2850 set_canonical_die_offset(canonical_dies,
2851 initial_die_offset,
2852 initial_die_offset);
2853
2854 return false;
2855 }
2856
2857 /// Get the source of the DIE.
2858 ///
2859 /// The function returns an enumerator value saying if the DIE comes
2860 /// from the .debug_info section of the primary debug info file, the
2861 /// .debug_info section of the alternate debug info file, or the
2862 /// .debug_types section.
2863 ///
2864 /// @param die the DIE to get the source of.
2865 ///
2866 /// @return the source of the DIE if it could be determined,
2867 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2869 get_die_source(const Dwarf_Die *die) const
2870 {
2871 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2872 ABG_ASSERT(die);
2873 ABG_ASSERT(get_die_source(*die, source));
2874 return source;
2875 }
2876
2877 /// Get the source of the DIE.
2878 ///
2879 /// The function returns an enumerator value saying if the DIE comes
2880 /// from the .debug_info section of the primary debug info file, the
2881 /// .debug_info section of the alternate debug info file, or the
2882 /// .debug_types section.
2883 ///
2884 /// @param die the DIE to get the source of.
2885 ///
2886 /// @param source out parameter. The function sets this parameter
2887 /// to the source of the DIE @p iff it returns true.
2888 ///
2889 /// @return true iff the source of the DIE could be determined and
2890 /// returned.
2891 bool
2892 get_die_source(const Dwarf_Die &die, die_source &source) const
2893 {
2894 Dwarf_Die cu_die;
2895 Dwarf_Die cu_kind;
2896 uint8_t address_size = 0, offset_size = 0;
2897 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2898 &cu_die, &address_size,
2899 &offset_size))
2900 return false;
2901
2902 Dwarf_Half version = 0;
2903 Dwarf_Off abbrev_offset = 0;
2904 uint64_t type_signature = 0;
2905 Dwarf_Off type_offset = 0;
2906 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2907 &version, &abbrev_offset,
2908 &address_size, &offset_size,
2909 &type_signature, &type_offset))
2910 return false;
2911
2912 int tag = dwarf_tag(&cu_kind);
2913
2914 if (tag == DW_TAG_compile_unit
2915 || tag == DW_TAG_partial_unit)
2916 {
2917 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
2918 if (dwarf_debug_info() == die_dwarf)
2919 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
2920 else if (alternate_dwarf_debug_info() == die_dwarf)
2921 source = ALT_DEBUG_INFO_DIE_SOURCE;
2922 else
2924 }
2925 else if (tag == DW_TAG_type_unit)
2926 source = TYPE_UNIT_DIE_SOURCE;
2927 else
2928 return false;
2929
2930 return true;
2931 }
2932
2933 /// Getter for the DIE designated by an offset.
2934 ///
2935 /// @param source the source of the DIE to get.
2936 ///
2937 /// @param offset the offset of the DIE to get.
2938 ///
2939 /// @param die the resulting DIE. The pointer has to point to an
2940 /// allocated memory region.
2941 void
2942 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
2943 {
2944 if (source == TYPE_UNIT_DIE_SOURCE)
2945 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2946 offset, die));
2947 else
2948 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2949 offset, die));
2950 }
2951
2952public:
2953
2954 /// Add an entry to the relevant die->decl map.
2955 ///
2956 /// @param die the DIE to add the the map.
2957 ///
2958 /// @param decl the decl to consider.
2959 ///
2960 /// @param where_offset where in the DIE stream we logically are.
2961 ///
2962 /// @param do_associate_by_repr if true then this function
2963 /// associates the representation string of @p die with the
2964 /// declaration @p decl, in a corpus-wide manner. That is, in the
2965 /// entire current corpus, there is going to be just one declaration
2966 /// associated with a DIE of the string representation of @p die.
2967 ///
2968 /// @param do_associate_by_repr_per_tu if true, then this function
2969 /// associates the representation string of @p die with the
2970 /// declaration @p decl in a translation unit wide manner. That is,
2971 /// in the entire current translation unit, there is going to be
2972 /// just one declaration associated with a DIE of the string
2973 /// representation of @p die.
2974 void
2975 associate_die_to_decl(Dwarf_Die* die,
2976 decl_base_sptr decl,
2977 size_t where_offset,
2978 bool do_associate_by_repr = false)
2979 {
2980 const die_source source = get_die_source(die);
2981
2983 decl_die_artefact_maps().get_container(source);
2984
2985 size_t die_offset;
2986 if (do_associate_by_repr)
2987 {
2988 Dwarf_Die equiv_die;
2989 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
2990 /*die_as_type=*/false))
2991 return;
2992 die_offset = dwarf_dieoffset(&equiv_die);
2993 }
2994 else
2995 die_offset = dwarf_dieoffset(die);
2996
2997 m[die_offset] = decl;
2998 }
2999
3000 /// Lookup the decl for a given DIE.
3001 ///
3002 /// The returned decl is either the decl of the DIE that as the
3003 /// exact offset @p die_offset
3004 /// die_offset, or
3005 /// give
3006 ///
3007 /// @param die_offset the offset of the DIE to consider.
3008 ///
3009 /// @param source where the DIE represented by @p die_offset comes
3010 /// from.
3011 ///
3012 /// Note that "alternate debug info sections" is a GNU extension as
3013 /// of DWARF4 and is described at
3014 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3015 ///
3016 /// @return the resulting decl, or null if no decl is associated to
3017 /// the DIE represented by @p die_offset.
3018 decl_base_sptr
3019 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3020 {
3021 decl_base_sptr result =
3022 is_decl(lookup_artifact_from_die_offset(die_offset, source,
3023 /*die_as_type=*/false));
3024
3025 return result;
3026 }
3027
3028 /// Get the qualified name of a given DIE.
3029 ///
3030 /// If the name of the DIE was already computed before just return
3031 /// that name from a cache. Otherwise, build the name, cache it and
3032 /// return it.
3033 ///
3034 /// @param die the DIE to consider.
3035 ///
3036 /// @param where_offset where in the DIE stream we logically are.
3037 ///
3038 /// @return the interned string representing the qualified name of
3039 /// @p die.
3040 interned_string
3041 get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3042 {
3043 ABG_ASSERT(die);
3045 die_qualified_name_maps_.get_container(*this, die);
3046
3047 size_t die_offset = dwarf_dieoffset(die);
3048 die_istring_map_type::const_iterator i = map.find(die_offset);
3049
3050 if (i == map.end())
3051 {
3052 reader& rdr = *const_cast<reader*>(this);
3053 string qualified_name = die_qualified_name(rdr, die, where_offset);
3054 interned_string istr = env().intern(qualified_name);
3055 map[die_offset] = istr;
3056 return istr;
3057 }
3058
3059 return i->second;
3060 }
3061
3062 /// Get the qualified name of a given DIE.
3063 ///
3064 /// If the name of the DIE was already computed before just return
3065 /// that name from a cache. Otherwise, build the name, cache it and
3066 /// return it.
3067 ///
3068 /// @param die the DIE to consider.
3069 ///
3070 /// @param where_offset where in the DIE stream we logically are.
3071 ///
3072 /// @return the interned string representing the qualified name of
3073 /// @p die.
3074 interned_string
3075 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3076 {
3077 return const_cast<reader*>(this)->
3078 get_die_qualified_name(die, where_offset);
3079 }
3080
3081 /// Get the qualified name of a given DIE which is considered to be
3082 /// the DIE for a type.
3083 ///
3084 /// For instance, for a DW_TAG_subprogram DIE, this function
3085 /// computes the name of the function *type* that corresponds to the
3086 /// function.
3087 ///
3088 /// If the name of the DIE was already computed before just return
3089 /// that name from a cache. Otherwise, build the name, cache it and
3090 /// return it.
3091 ///
3092 /// @param die the DIE to consider.
3093 ///
3094 /// @param where_offset where in the DIE stream we logically are.
3095 ///
3096 /// @return the interned string representing the qualified name of
3097 /// @p die.
3098 interned_string
3099 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3100 {
3101 ABG_ASSERT(die);
3102
3103 // The name of the translation unit die is "".
3104 if (die == cur_tu_die())
3105 return env().intern("");
3106
3108 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3109 die);
3110
3111 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3112 die_istring_map_type::const_iterator i =
3113 map.find(die_offset);
3114
3115 if (i == map.end())
3116 {
3117 reader& rdr = *const_cast<reader*>(this);
3118 string qualified_name;
3119 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3120 if ((tag == DW_TAG_structure_type
3121 || tag == DW_TAG_class_type
3122 || tag == DW_TAG_union_type)
3123 && die_is_anonymous(die))
3124 {
3125 location l = die_location(*this, die);
3126 qualified_name = l ? l.expand() : "noloc";
3127 qualified_name = "unnamed-at-" + qualified_name;
3128 }
3129 else
3130 qualified_name =
3131 die_qualified_type_name(rdr, die, where_offset);
3132
3133 interned_string istr = env().intern(qualified_name);
3134 map[die_offset] = istr;
3135 return istr;
3136 }
3137
3138 return i->second;
3139 }
3140
3141 /// Get the pretty representation of a DIE that represents a type.
3142 ///
3143 /// For instance, for the DW_TAG_subprogram, this function computes
3144 /// the pretty representation of the type of the function, not the
3145 /// pretty representation of the function declaration.
3146 ///
3147 /// Once the pretty representation is computed, it's stored in a
3148 /// cache. Subsequent invocations of this function on the same DIE
3149 /// will yield the cached name.
3150 ///
3151 /// @param die the DIE to consider.
3152 ///
3153 /// @param where_offset where in the DIE stream we logically are.
3154 ///
3155 /// @return the interned_string that represents the pretty
3156 /// representation.
3157 interned_string
3158 get_die_pretty_type_representation(const Dwarf_Die *die,
3159 size_t where_offset) const
3160 {
3161 ABG_ASSERT(die);
3163 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3164 die);
3165
3166 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3167 die_istring_map_type::const_iterator i = map.find(die_offset);
3168
3169 if (i == map.end())
3170 {
3171 reader& rdr = *const_cast<reader*>(this);
3172 string pretty_representation =
3173 die_pretty_print_type(rdr, die, where_offset);
3174 interned_string istr = env().intern(pretty_representation);
3175 map[die_offset] = istr;
3176 return istr;
3177 }
3178
3179 return i->second;
3180 }
3181
3182 /// Get the pretty representation of a DIE.
3183 ///
3184 /// Once the pretty representation is computed, it's stored in a
3185 /// cache. Subsequent invocations of this function on the same DIE
3186 /// will yield the cached name.
3187 ///
3188 /// @param die the DIE to consider.
3189 ///
3190 /// @param where_offset where in the DIE stream we logically are.
3191 ///
3192 /// @return the interned_string that represents the pretty
3193 /// representation.
3194 interned_string
3195 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3196 {
3197 ABG_ASSERT(die);
3198
3200 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3201 die);
3202
3203 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3204 die_istring_map_type::const_iterator i = map.find(die_offset);
3205
3206 if (i == map.end())
3207 {
3208 reader& rdr = *const_cast<reader*>(this);
3209 string pretty_representation =
3210 die_pretty_print(rdr, die, where_offset);
3211 interned_string istr = env().intern(pretty_representation);
3212 map[die_offset] = istr;
3213 return istr;
3214 }
3215
3216 return i->second;
3217 }
3218
3219 /// Lookup the artifact that was built to represent a type that has
3220 /// the same pretty representation as the type denoted by a given
3221 /// DIE.
3222 ///
3223 /// Note that the DIE must have previously been associated with the
3224 /// artifact using the functions associate_die_to_decl or
3225 /// associate_die_to_type.
3226 ///
3227 /// Also, note that the scope of the lookup is the current ABI
3228 /// corpus.
3229 ///
3230 /// @param die the DIE to consider.
3231 ///
3232 /// @param where_offset where in the DIE stream we logically are.
3233 ///
3234 /// @return the type artifact found.
3236 lookup_type_artifact_from_die(Dwarf_Die *die) const
3237 {
3238 type_or_decl_base_sptr artifact =
3239 lookup_artifact_from_die(die, /*type_as_die=*/true);
3240 if (function_decl_sptr fn = is_function_decl(artifact))
3241 return fn->get_type();
3242 return artifact;
3243 }
3244
3245 /// Lookup the artifact that was built to represent a type or a
3246 /// declaration that has the same pretty representation as the type
3247 /// denoted by a given DIE.
3248 ///
3249 /// Note that the DIE must have previously been associated with the
3250 /// artifact using the functions associate_die_to_decl or
3251 /// associate_die_to_type.
3252 ///
3253 /// Also, note that the scope of the lookup is the current ABI
3254 /// corpus.
3255 ///
3256 /// @param die the DIE to consider.
3257 ///
3258 /// @param where_offset where in the DIE stream we logically are.
3259 ///
3260 /// @param die_as_type if true, it means the DIE is to be considered
3261 /// as a type.
3262 ///
3263 /// @return the artifact found.
3265 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3266 {
3267 Dwarf_Die equiv_die;
3268 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3269 return type_or_decl_base_sptr();
3270
3271 const die_artefact_map_type& m =
3272 die_as_type
3273 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3274 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3275
3276 size_t die_offset = dwarf_dieoffset(&equiv_die);
3277 die_artefact_map_type::const_iterator i = m.find(die_offset);
3278
3279 if (i == m.end())
3280 return type_or_decl_base_sptr();
3281 return i->second;
3282 }
3283
3284 /// Lookup the artifact that was built to represent a type or a
3285 /// declaration that has the same pretty representation as the type
3286 /// denoted by the offset of a given DIE.
3287 ///
3288 /// Note that the DIE must have previously been associated with the
3289 /// artifact using either associate_die_to_decl or
3290 /// associate_die_to_type.
3291 ///
3292 /// Also, note that the scope of the lookup is the current ABI
3293 /// corpus.
3294 ///
3295 /// @param die the DIE to consider.
3296 ///
3297 /// @param where_offset where in the DIE stream we logically are.
3298 ///
3299 /// @param die_as_type if true, it means the DIE is to be considered
3300 /// as a type.
3301 ///
3302 /// @return the artifact found.
3304 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3305 die_source source,
3306 bool die_as_type = false) const
3307 {
3308 const die_artefact_map_type& m =
3309 die_as_type
3310 ? type_die_artefact_maps().get_container(source)
3311 : decl_die_artefact_maps().get_container(source);
3312
3313 die_artefact_map_type::const_iterator i = m.find(die_offset);
3314 if (i == m.end())
3315 return type_or_decl_base_sptr();
3316 return i->second;
3317 }
3318
3319 /// Get the language used to generate a given DIE.
3320 ///
3321 /// @param die the DIE to consider.
3322 ///
3323 /// @param lang the resulting language.
3324 ///
3325 /// @return true iff the language of the DIE was found.
3326 bool
3327 get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
3328 {
3329 Dwarf_Die cu_die;
3330 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
3331
3332 uint64_t l = 0;
3333 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
3334 return false;
3335
3336 lang = dwarf_language_to_tu_language(l);
3337 return true;
3338 }
3339
3340 /// Test if a given DIE originates from a program written in the C
3341 /// language.
3342 ///
3343 /// @param die the DIE to consider.
3344 ///
3345 /// @return true iff @p die originates from a program in the C
3346 /// language.
3347 bool
3348 die_is_in_c(const Dwarf_Die *die) const
3349 {
3350 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3351 if (!get_die_language(die, l))
3352 return false;
3353 return is_c_language(l);
3354 }
3355
3356 /// Test if a given DIE originates from a program written in the C++
3357 /// language.
3358 ///
3359 /// @param die the DIE to consider.
3360 ///
3361 /// @return true iff @p die originates from a program in the C++
3362 /// language.
3363 bool
3364 die_is_in_cplus_plus(const Dwarf_Die *die) const
3365 {
3366 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3367 if (!get_die_language(die, l))
3368 return false;
3369 return is_cplus_plus_language(l);
3370 }
3371
3372 /// Test if a given DIE originates from a program written either in
3373 /// C or C++.
3374 ///
3375 /// @param die the DIE to consider.
3376 ///
3377 /// @return true iff @p die originates from a program written either in
3378 /// C or C++.
3379 bool
3380 die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
3381 {
3382 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3383 if (!get_die_language(die, l))
3384 return false;
3385 return (is_cplus_plus_language(l) || is_c_language(l));
3386 }
3387
3388 /// Check if we can assume the One Definition Rule[1] to be relevant
3389 /// for the current translation unit.
3390 ///
3391 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3392 ///
3393 /// At the moment this returns true if the current translation unit
3394 /// is in C++ language. In that case, it's relevant to assume that
3395 /// we use optimizations based on the ODR.
3396 bool
3397 odr_is_relevant() const
3398 {return odr_is_relevant(cur_transl_unit()->get_language());}
3399
3400 /// Check if we can assume the One Definition Rule[1] to be relevant
3401 /// for a given language.
3402 ///
3403 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3404 ///
3405 /// At the moment this returns true if the language considered
3406 /// is C++, Java or Ada.
3407 bool
3409 {
3410 return (is_cplus_plus_language(l)
3411 || is_java_language(l)
3412 || is_ada_language(l));
3413 }
3414
3415 /// Check if we can assume the One Definition Rule to be relevant
3416 /// for a given DIE.
3417 ///
3418 /// @param die the DIE to consider.
3419 ///
3420 /// @return true if the ODR is relevant for @p die.
3421 bool
3422 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3423 {
3424 Dwarf_Die die;
3425 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3426 die_offset, &die));
3427 return odr_is_relevant(&die);
3428 }
3429
3430 /// Check if we can assume the One Definition Rule to be relevant
3431 /// for a given DIE.
3432 ///
3433 /// @param die the DIE to consider.
3434 ///
3435 /// @return true if the ODR is relevant for @p die.
3436 bool
3437 odr_is_relevant(const Dwarf_Die *die) const
3438 {
3440 if (!get_die_language(die, lang))
3441 return odr_is_relevant();
3442
3443 return odr_is_relevant(lang);
3444 }
3445
3446 /// Getter for the maps set that associates a decl DIE offset to an
3447 /// artifact.
3448 ///
3449 /// @return the maps set that associates a decl DIE offset to an
3450 /// artifact.
3451 die_source_dependant_container_set<die_artefact_map_type>&
3452 decl_die_artefact_maps()
3453 {return decl_die_artefact_maps_;}
3454
3455 /// Getter for the maps set that associates a decl DIE offset to an
3456 /// artifact.
3457 ///
3458 /// @return the maps set that associates a decl DIE offset to an
3459 /// artifact.
3460 const die_source_dependant_container_set<die_artefact_map_type>&
3461 decl_die_artefact_maps() const
3462 {return decl_die_artefact_maps_;}
3463
3464 /// Getter for the maps set that associates a type DIE offset to an
3465 /// artifact.
3466 ///
3467 /// @return the maps set that associates a type DIE offset to an
3468 /// artifact.
3469 die_source_dependant_container_set<die_artefact_map_type>&
3470 type_die_artefact_maps()
3471 {return type_die_artefact_maps_;}
3472
3473 /// Getter for the maps set that associates a type DIE offset to an
3474 /// artifact.
3475 ///
3476 /// @return the maps set that associates a type DIE offset to an
3477 /// artifact.
3478 const die_source_dependant_container_set<die_artefact_map_type>&
3479 type_die_artefact_maps() const
3480 {return type_die_artefact_maps_;}
3481
3482 /// Getter of the maps that associates function type representations
3483 /// to function types, inside a translation unit.
3484 ///
3485 /// @return the maps that associates function type representations
3486 /// to function types, inside a translation unit.
3488 per_tu_repr_to_fn_type_maps()
3489 {return per_tu_repr_to_fn_type_maps_;}
3490
3491 /// Getter of the maps that associates function type representations
3492 /// to function types, inside a translation unit.
3493 ///
3494 /// @return the maps that associates function type representations
3495 /// to function types, inside a translation unit.
3497 per_tu_repr_to_fn_type_maps() const
3498 {return per_tu_repr_to_fn_type_maps_;}
3499
3500 /// Associate the representation of a function type DIE to a given
3501 /// function type, inside the current translation unit.
3502 ///
3503 /// @param die the DIE to associate to the function type, using its
3504 /// representation.
3505 ///
3506 /// @param fn_type the function type to associate to @p die.
3507 void
3508 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3509 const function_type_sptr &fn_type)
3510 {
3511 if (!die_is_function_type(die))
3512 return;
3513
3514 interned_string repr =
3515 get_die_pretty_type_representation(die, /*where=*/0);
3516 ABG_ASSERT(!repr.empty());
3517
3518 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3519 }
3520
3521 /// Lookup the function type associated to a given function type
3522 /// DIE, in the current translation unit.
3523 ///
3524 /// @param die the DIE of function type to consider.
3525 ///
3526 /// @return the @ref function_type_sptr associated to @p die, or nil
3527 /// of no function_type is associated to @p die.
3529 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3530 {
3531 if (!die_is_function_type(die))
3532 return function_type_sptr();
3533
3534 interned_string repr = die_name(die).empty() ?
3535 get_die_pretty_type_representation(die, /*where=*/0)
3536 : get_die_pretty_representation(die, /*where=*/0);
3537 ABG_ASSERT(!repr.empty());
3538
3539 istring_fn_type_map_type::const_iterator i =
3540 per_tu_repr_to_fn_type_maps().find(repr);
3541
3542 if (i == per_tu_repr_to_fn_type_maps().end())
3543 return function_type_sptr();
3544
3545 return i->second;
3546 }
3547
3548 /// Set the canonical DIE offset of a given DIE.
3549 ///
3550 /// @param canonical_dies the vector that holds canonical DIEs.
3551 ///
3552 /// @param die_offset the offset of the DIE to set the canonical DIE
3553 /// for.
3554 ///
3555 /// @param canonical_die_offset the canonical DIE offset to
3556 /// associate to @p die_offset.
3557 void
3558 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3559 Dwarf_Off die_offset,
3560 Dwarf_Off canonical_die_offset) const
3561 {
3562 canonical_dies[die_offset] = canonical_die_offset;}
3563
3564 /// Set the canonical DIE offset of a given DIE.
3565 ///
3566 ///
3567 /// @param die_offset the offset of the DIE to set the canonical DIE
3568 /// for.
3569 ///
3570 /// @param source the source of the DIE denoted by @p die_offset.
3571 ///
3572 /// @param canonical_die_offset the canonical DIE offset to
3573 /// associate to @p die_offset.
3574 ///
3575 /// @param die_as_type if true, it means that @p die_offset has to
3576 /// be considered as a type.
3577 void
3578 set_canonical_die_offset(Dwarf_Off die_offset,
3579 die_source source,
3580 Dwarf_Off canonical_die_offset,
3581 bool die_as_type) const
3582 {
3583 offset_offset_map_type &canonical_dies =
3584 die_as_type
3585 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3586 get_container(source)
3587 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3588 get_container(source);
3589
3590 set_canonical_die_offset(canonical_dies,
3591 die_offset,
3592 canonical_die_offset);
3593 }
3594
3595 /// Set the canonical DIE offset of a given DIE.
3596 ///
3597 ///
3598 /// @param die the DIE to set the canonical DIE for.
3599 ///
3600 /// @param canonical_die_offset the canonical DIE offset to
3601 /// associate to @p die_offset.
3602 ///
3603 /// @param die_as_type if true, it means that @p die has to be
3604 /// considered as a type.
3605 void
3606 set_canonical_die_offset(const Dwarf_Die *die,
3607 Dwarf_Off canonical_die_offset,
3608 bool die_as_type) const
3609 {
3610 const die_source source = get_die_source(die);
3611
3612 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3613
3614 set_canonical_die_offset(die_offset, source,
3615 canonical_die_offset,
3616 die_as_type);
3617 }
3618
3619 /// Get the canonical DIE offset of a given DIE.
3620 ///
3621 /// @param canonical_dies the vector that contains canonical DIES.
3622 ///
3623 /// @param die_offset the offset of the DIE to consider.
3624 ///
3625 /// @return the canonical of the DIE denoted by @p die_offset, or
3626 /// zero if no canonical DIE was found.
3627 Dwarf_Off
3628 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3629 Dwarf_Off die_offset) const
3630 {
3631 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3632 if (it == canonical_dies.end())
3633 return 0;
3634 return it->second;
3635 }
3636
3637 /// Get the canonical DIE offset of a given DIE.
3638 ///
3639 /// @param die_offset the offset of the DIE to consider.
3640 ///
3641 /// @param source the source of the DIE denoted by @p die_offset.
3642 ///
3643 /// @param die_as_type if true, it means that @p is to be considered
3644 /// as a type DIE.
3645 ///
3646 /// @return the canonical of the DIE denoted by @p die_offset, or
3647 /// zero if no canonical DIE was found.
3648 Dwarf_Off
3649 get_canonical_die_offset(Dwarf_Off die_offset,
3650 die_source source,
3651 bool die_as_type) const
3652 {
3653 offset_offset_map_type &canonical_dies =
3654 die_as_type
3655 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3656 get_container(source)
3657 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3658 get_container(source);
3659
3660 return get_canonical_die_offset(canonical_dies, die_offset);
3661 }
3662
3663 /// Erase the canonical type of a given DIE.
3664 ///
3665 /// @param die_offset the offset of the DIE to consider.
3666 ///
3667 /// @param source the source of the canonical type.
3668 ///
3669 /// @param die_as_type if true, it means that @p is to be considered
3670 /// as a type DIE.
3671 ///
3672 /// @return the canonical of the DIE denoted by @p die_offset, or
3673 /// zero if no canonical DIE was found and erased..
3674 bool
3675 erase_canonical_die_offset(Dwarf_Off die_offset,
3676 die_source source,
3677 bool die_as_type) const
3678 {
3679 offset_offset_map_type &canonical_dies =
3680 die_as_type
3681 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3682 get_container(source)
3683 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3684 get_container(source);
3685
3686 return canonical_dies.erase(die_offset);
3687 }
3688
3689
3690 /// Associate a DIE (representing a type) to the type that it
3691 /// represents.
3692 ///
3693 /// @param die the DIE to consider.
3694 ///
3695 /// @param type the type to associate the DIE to.
3696 ///
3697 /// @param where_offset where in the DIE stream we logically are.
3698 void
3699 associate_die_to_type(const Dwarf_Die *die,
3700 type_base_sptr type,
3701 size_t where)
3702 {
3703 if (!type)
3704 return;
3705
3706 Dwarf_Die equiv_die;
3707 if (!get_or_compute_canonical_die(die, equiv_die, where,
3708 /*die_as_type=*/true))
3709 return;
3710
3712 type_die_artefact_maps().get_container(*this, &equiv_die);
3713
3714 size_t die_offset = dwarf_dieoffset(&equiv_die);
3715 m[die_offset] = type;
3716 }
3717
3718 /// Lookup the type associated to a given DIE.
3719 ///
3720 /// Note that the DIE must have been associated to type by a
3721 /// previous invocation of the function
3722 /// reader::associate_die_to_type().
3723 ///
3724 /// @param die the DIE to consider.
3725 ///
3726 /// @return the type associated to the DIE or NULL if no type is
3727 /// associated to the DIE.
3728 type_base_sptr
3729 lookup_type_from_die(const Dwarf_Die* die) const
3730 {
3731 type_or_decl_base_sptr artifact =
3732 lookup_artifact_from_die(die, /*die_as_type=*/true);
3733 if (function_decl_sptr fn = is_function_decl(artifact))
3734 return fn->get_type();
3735 return is_type(artifact);
3736 }
3737
3738 /// Lookup the type associated to a DIE at a given offset, from a
3739 /// given source.
3740 ///
3741 /// Note that the DIE must have been associated to type by a
3742 /// previous invocation of the function
3743 /// reader::associate_die_to_type().
3744 ///
3745 /// @param die_offset the offset of the DIE to consider.
3746 ///
3747 /// @param source the source of the DIE to consider.
3748 ///
3749 /// @return the type associated to the DIE or NULL if no type is
3750 /// associated to the DIE.
3751 type_base_sptr
3752 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3753 {
3754 type_base_sptr result;
3755 const die_artefact_map_type& m =
3756 type_die_artefact_maps().get_container(source);
3757 die_artefact_map_type::const_iterator i = m.find(die_offset);
3758 if (i != m.end())
3759 {
3760 if (function_decl_sptr fn = is_function_decl(i->second))
3761 return fn->get_type();
3762 result = is_type(i->second);
3763 }
3764
3765 if (!result)
3766 {
3767 // Maybe we are looking for a class type being constructed?
3768 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3769 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3770
3771 if (i != m.end())
3772 result = i->second;
3773 }
3774
3775 if (!result)
3776 {
3777 // Maybe we are looking for a function type being constructed?
3779 die_wip_function_types_map(source);
3780 die_function_type_map_type::const_iterator i = m.find(die_offset);
3781
3782 if (i != m.end())
3783 result = i->second;
3784 }
3785
3786 return result;
3787 }
3788
3789 /// Getter of a map that associates a die that represents a
3790 /// class/struct with the declaration of the class, while the class
3791 /// is being constructed.
3792 ///
3793 /// @param source where the DIE is from.
3794 ///
3795 /// @return the map that associates a DIE to the class that is being
3796 /// built.
3798 die_wip_classes_map(die_source source) const
3799 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3800
3801 /// Getter of a map that associates a die that represents a
3802 /// class/struct with the declaration of the class, while the class
3803 /// is being constructed.
3804 ///
3805 /// @param source where the DIE comes from.
3806 ///
3807 /// @return the map that associates a DIE to the class that is being
3808 /// built.
3810 die_wip_classes_map(die_source source)
3811 {
3812 switch (source)
3813 {
3814 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3815 break;
3816 case ALT_DEBUG_INFO_DIE_SOURCE:
3817 return alternate_die_wip_classes_map_;
3818 case TYPE_UNIT_DIE_SOURCE:
3819 return type_unit_die_wip_classes_map_;
3820 case NO_DEBUG_INFO_DIE_SOURCE:
3821 case NUMBER_OF_DIE_SOURCES:
3823 }
3824 return die_wip_classes_map_;
3825 }
3826
3827 /// Getter for a map that associates a die (that represents a
3828 /// function type) whith a function type, while the function type is
3829 /// being constructed (WIP == work in progress).
3830 ///
3831 /// @param source where the DIE comes from.n
3832 ///
3833 /// @return the map of wip function types.
3835 die_wip_function_types_map(die_source source) const
3836 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3837
3838 /// Getter for a map that associates a die (that represents a
3839 /// function type) whith a function type, while the function type is
3840 /// being constructed (WIP == work in progress).
3841 ///
3842 /// @param source where DIEs of the map come from.
3843 ///
3844 /// @return the map of wip function types.
3846 die_wip_function_types_map(die_source source)
3847 {
3848 switch (source)
3849 {
3850 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3851 break;
3852 case ALT_DEBUG_INFO_DIE_SOURCE:
3853 return alternate_die_wip_function_types_map_;
3854 case TYPE_UNIT_DIE_SOURCE:
3855 return type_unit_die_wip_function_types_map_;
3856 case NO_DEBUG_INFO_DIE_SOURCE:
3857 case NUMBER_OF_DIE_SOURCES:
3859 }
3860 return die_wip_function_types_map_;
3861 }
3862
3863 /// Getter for a map that associates a die with a function decl
3864 /// which has a linkage name but no elf symbol yet.
3865 ///
3866 /// This is to fixup function decls with linkage names, but with no
3867 /// link to their underlying elf symbol. There are some DIEs like
3868 /// that in DWARF sometimes, especially when the compiler optimizes
3869 /// stuff aggressively.
3871 die_function_decl_with_no_symbol_map()
3872 {return die_function_with_no_symbol_map_;}
3873
3874 /// Return true iff a given offset is for the DIE of a class that is
3875 /// being built, but that is not fully built yet. WIP == "work in
3876 /// progress".
3877 ///
3878 /// @param offset the DIE offset to consider.
3879 ///
3880 /// @param source where the DIE of the map come from.
3881 ///
3882 /// @return true iff @p offset is the offset of the DIE of a class
3883 /// that is being currently built.
3884 bool
3885 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3886 {
3887 die_class_or_union_map_type::const_iterator i =
3888 die_wip_classes_map(source).find(offset);
3889 return (i != die_wip_classes_map(source).end());
3890 }
3891
3892 /// Return true iff a given offset is for the DIE of a function type
3893 /// that is being built at the moment, but is not fully built yet.
3894 /// WIP == work in progress.
3895 ///
3896 /// @param offset DIE offset to consider.
3897 ///
3898 /// @param source where the DIE comes from.
3899 ///
3900 /// @return true iff @p offset is the offset of the DIE of a
3901 /// function type that is being currently built.
3902 bool
3903 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3904 {
3905 die_function_type_map_type::const_iterator i =
3906 die_wip_function_types_map(source).find(offset);
3907 return (i != die_wip_function_types_map(source).end());
3908 }
3909
3910 /// Sometimes, a data member die can erroneously have an empty name as
3911 /// a result of a bug of the DWARF emitter.
3912 ///
3913 /// This is what happens in
3914 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3915 ///
3916 /// In that case, this function constructs an artificial name for that
3917 /// data member. The pattern of the name is as follows:
3918 ///
3919 /// "unnamed-@-<location>".
3920 ///
3921 ///location is either the value of the data member location of the
3922 ///data member if it has one or concatenation of its source location
3923 ///if it has none. If no location can be calculated then the function
3924 ///returns the empty string.
3925 string
3926 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
3927 {
3928 string result;
3929 // Let's make sure we are looking at a data member with an empty
3930 // name ...
3931 if (!die
3932 || dwarf_tag(die) != DW_TAG_member
3933 || !die_name(die).empty())
3934 return result;
3935
3936 // ... and yet, it's not an anonymous data member (aka unnamed
3937 // field) as described in
3938 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3939 if (die_is_anonymous_data_member(die))
3940 return result;
3941
3942 // If we come this far, it means we are looking at a buggy data
3943 // member with no name. Let's build a name for it so that it can be
3944 // addressed.
3945 int64_t offset_in_bits = 0;
3946 bool has_offset = die_member_offset(*this, die, offset_in_bits);
3947 location loc;
3948 if (!has_offset)
3949 {
3950 loc = die_location(*this, die);
3951 if (!loc)
3952 return result;
3953 }
3954
3955 std::ostringstream o;
3956 o << "unnamed-dm-@-";
3957 if (has_offset)
3958 o << "offset-" << offset_in_bits << "bits";
3959 else
3960 o << "loc-" << loc.expand();
3961
3962 return o.str();
3963 }
3964
3965 /// Getter for the map of declaration-only classes that are to be
3966 /// resolved to their definition classes by the end of the corpus
3967 /// loading.
3968 ///
3969 /// @return a map of string -> vector of classes where the key is
3970 /// the fully qualified name of the class and the value is the
3971 /// vector of declaration-only class.
3973 declaration_only_classes() const
3974 {return decl_only_classes_map_;}
3975
3976 /// Getter for the map of declaration-only classes that are to be
3977 /// resolved to their definition classes by the end of the corpus
3978 /// loading.
3979 ///
3980 /// @return a map of string -> vector of classes where the key is
3981 /// the fully qualified name of the class and the value is the
3982 /// vector of declaration-only class.
3984 declaration_only_classes()
3985 {return decl_only_classes_map_;}
3986
3987 /// If a given class is a declaration-only class then stash it on
3988 /// the side so that at the end of the corpus reading we can resolve
3989 /// it to its definition.
3990 ///
3991 /// @param klass the class to consider.
3992 void
3993 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
3994 {
3995 if (cou->get_is_declaration_only()
3996 && cou->get_definition_of_declaration() == 0
3997 // Make sure the class is not anonymous. Anonymous classes
3998 // are usually later named by a typedef. At that time, after
3999 // being named by a typedef, this method is going to be called
4000 // with the class being named by the typedef.
4001 && !cou->get_qualified_name().empty())
4002 {
4003 string qn = cou->get_qualified_name();
4004 string_classes_or_unions_map::iterator record =
4005 declaration_only_classes().find(qn);
4006 if (record == declaration_only_classes().end())
4007 declaration_only_classes()[qn].push_back(cou);
4008 else
4009 record->second.push_back(cou);
4010 }
4011 }
4012
4013 /// Test if a given declaration-only class has been scheduled for
4014 /// resolution to a defined class.
4015 ///
4016 /// @param klass the class to consider for the test.
4017 ///
4018 /// @return true iff @p klass is a declaration-only class and if
4019 /// it's been scheduled for resolution to a defined class.
4020 bool
4021 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4022 {
4023 if (cou->get_is_declaration_only())
4024 return ((declaration_only_classes().find(cou->get_qualified_name())
4025 != declaration_only_classes().end())
4026 || (declaration_only_classes().find(cou->get_name())
4027 != declaration_only_classes().end()));
4028
4029 return false;
4030 }
4031
4032 /// Compare two ABI artifacts in a context which canonicalization
4033 /// has not be done yet.
4034 ///
4035 /// @param l the left-hand-side operand of the comparison
4036 ///
4037 /// @param r the right-hand-side operand of the comparison.
4038 ///
4039 /// @return true if @p l equals @p r.
4040 bool
4041 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4042 const type_or_decl_base_sptr &r)
4043 {
4044 if (!l || !r)
4045 return !!l == !!r;
4046
4047 const environment& e = l->get_environment();
4048 ABG_ASSERT(!e.canonicalization_is_done());
4049
4050 e.priv_->allow_type_comparison_results_caching(true);
4051 bool s0 = e.decl_only_class_equals_definition();
4052 e.decl_only_class_equals_definition(true);
4053 bool equal = l == r;
4054 e.decl_only_class_equals_definition(s0);
4055 e.priv_->clear_type_comparison_results_cache();
4056 e.priv_->allow_type_comparison_results_caching(false);
4057 return equal;
4058 }
4059
4060 /// Walk the declaration-only classes that have been found during
4061 /// the building of the corpus and resolve them to their definitions.
4062 void
4063 resolve_declaration_only_classes()
4064 {
4065 vector<string> resolved_classes;
4066
4067 for (string_classes_or_unions_map::iterator i =
4068 declaration_only_classes().begin();
4069 i != declaration_only_classes().end();
4070 ++i)
4071 {
4072 bool to_resolve = false;
4073 for (classes_or_unions_type::iterator j = i->second.begin();
4074 j != i->second.end();
4075 ++j)
4076 if ((*j)->get_is_declaration_only()
4077 && ((*j)->get_definition_of_declaration() == 0))
4078 to_resolve = true;
4079
4080 if (!to_resolve)
4081 {
4082 resolved_classes.push_back(i->first);
4083 continue;
4084 }
4085
4086 // Now, for each decl-only class that have the current name
4087 // 'i->first', let's try to poke at the fully defined class
4088 // that is defined in the same translation unit as the
4089 // declaration.
4090 //
4091 // If we find one class (defined in the TU of the declaration)
4092 // that defines the declaration, then the declaration can be
4093 // resolved to that class.
4094 //
4095 // If no defining class is found in the TU of the declaration,
4096 // then there are possibly three cases to consider:
4097 //
4098 // 1/ There is exactly one class that defines the
4099 // declaration and that class is defined in another TU. In
4100 // this case, the declaration is resolved to that
4101 // definition.
4102 //
4103 // 2/ There are more than one class that define that
4104 // declaration and none of them is defined in the TU of the
4105 // declaration. If those classes are all different, then
4106 // the declaration is left unresolved.
4107 //
4108 // 3/ No class defines the declaration. In this case, the
4109 // declaration is left unresoved.
4110
4111 // So get the classes that might define the current
4112 // declarations which name is i->first.
4113 const type_base_wptrs_type *classes =
4114 lookup_class_types(i->first, *corpus());
4115 if (!classes)
4116 classes = lookup_union_types(i->first, *corpus());
4117
4118 if (!classes)
4119 continue;
4120
4121 // This is a map that associates the translation unit path to
4122 // the class (that potentially defines the declarations that
4123 // we consider) that are defined in that translation unit. It
4124 // should stay ordered by using the TU path as key to ensure
4125 // stability of the order of classe definitions in ABIXML
4126 // output.
4127 map<string, class_or_union_sptr> per_tu_class_map;
4128 for (type_base_wptrs_type::const_iterator c = classes->begin();
4129 c != classes->end();
4130 ++c)
4131 {
4132 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4133 ABG_ASSERT(klass);
4134
4136 if (klass->get_is_declaration_only())
4137 continue;
4138
4139 string tu_path = klass->get_translation_unit()->get_absolute_path();
4140 if (tu_path.empty())
4141 continue;
4142
4143 // Build a map that associates the translation unit path
4144 // to the class (that potentially defines the declarations
4145 // that we consider) that are defined in that translation unit.
4146 per_tu_class_map[tu_path] = klass;
4147 }
4148
4149 if (!per_tu_class_map.empty())
4150 {
4151 // Walk the declarations to resolve and resolve them
4152 // either to the definitions that are in the same TU as
4153 // the declaration, or to the definition found elsewhere,
4154 // if there is only one such definition.
4155 for (classes_or_unions_type::iterator j = i->second.begin();
4156 j != i->second.end();
4157 ++j)
4158 {
4159 if ((*j)->get_is_declaration_only()
4160 && ((*j)->get_definition_of_declaration() == 0))
4161 {
4162 string tu_path =
4163 (*j)->get_translation_unit()->get_absolute_path();
4164 map<string, class_or_union_sptr>::const_iterator e =
4165 per_tu_class_map.find(tu_path);
4166 if (e != per_tu_class_map.end())
4167 (*j)->set_definition_of_declaration(e->second);
4168 else if (per_tu_class_map.size() == 1)
4169 (*j)->set_definition_of_declaration
4170 (per_tu_class_map.begin()->second);
4171 else
4172 {
4173 // We are in case where there are more than
4174 // one definition for the declaration. Let's
4175 // see if they are all equal. If they are,
4176 // then the declaration resolves to the
4177 // definition. Otherwise, we are in the case
4178 // 3/ described above.
4179 map<string,
4180 class_or_union_sptr>::const_iterator it;
4181 class_or_union_sptr first_class =
4182 per_tu_class_map.begin()->second;
4183 bool all_class_definitions_are_equal = true;
4184 for (it = per_tu_class_map.begin();
4185 it != per_tu_class_map.end();
4186 ++it)
4187 {
4188 if (it == per_tu_class_map.begin())
4189 continue;
4190 else
4191 {
4192 if (!compare_before_canonicalisation(it->second,
4193 first_class))
4194 {
4195 all_class_definitions_are_equal = false;
4196 break;
4197 }
4198 }
4199 }
4200 if (all_class_definitions_are_equal)
4201 (*j)->set_definition_of_declaration(first_class);
4202 }
4203 }
4204 }
4205 resolved_classes.push_back(i->first);
4206 }
4207 }
4208
4209 size_t num_decl_only_classes = declaration_only_classes().size(),
4210 num_resolved = resolved_classes.size();
4211 if (show_stats())
4212 cerr << "resolved " << num_resolved
4213 << " class declarations out of "
4214 << num_decl_only_classes
4215 << "\n";
4216
4217 for (vector<string>::const_iterator i = resolved_classes.begin();
4218 i != resolved_classes.end();
4219 ++i)
4220 declaration_only_classes().erase(*i);
4221
4222 if (show_stats() && !declaration_only_classes().empty())
4223 {
4224 cerr << "Here are the "
4225 << num_decl_only_classes - num_resolved
4226 << " unresolved class declarations:\n";
4227 for (string_classes_or_unions_map::iterator i =
4228 declaration_only_classes().begin();
4229 i != declaration_only_classes().end();
4230 ++i)
4231 cerr << " " << i->first << "\n";
4232 }
4233 }
4234
4235 /// Getter for the map of declaration-only enums that are to be
4236 /// resolved to their definition enums by the end of the corpus
4237 /// loading.
4238 ///
4239 /// @return a map of string -> vector of enums where the key is
4240 /// the fully qualified name of the enum and the value is the
4241 /// vector of declaration-only enum.
4242 const string_enums_map&
4243 declaration_only_enums() const
4244 {return decl_only_enums_map_;}
4245
4246 /// Getter for the map of declaration-only enums that are to be
4247 /// resolved to their definition enums by the end of the corpus
4248 /// loading.
4249 ///
4250 /// @return a map of string -> vector of enums where the key is
4251 /// the fully qualified name of the enum and the value is the
4252 /// vector of declaration-only enum.
4254 declaration_only_enums()
4255 {return decl_only_enums_map_;}
4256
4257 /// If a given enum is a declaration-only enum then stash it on
4258 /// the side so that at the end of the corpus reading we can resolve
4259 /// it to its definition.
4260 ///
4261 /// @param enom the enum to consider.
4262 void
4263 maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4264 {
4265 if (enom->get_is_declaration_only()
4266 && enom->get_definition_of_declaration() == 0
4267 // Make sure the enum is not anonymous. Anonymous enums are
4268 // usually later named by a typedef. At that time, after
4269 // being named by a typedef, this method is going to be called
4270 // with the enum being named by the typedef.
4271 && !enom->get_qualified_name().empty())
4272 {
4273 string qn = enom->get_qualified_name();
4274 string_enums_map::iterator record =
4275 declaration_only_enums().find(qn);
4276 if (record == declaration_only_enums().end())
4277 declaration_only_enums()[qn].push_back(enom);
4278 else
4279 record->second.push_back(enom);
4280 }
4281 }
4282
4283 /// Test if a given declaration-only enum has been scheduled for
4284 /// resolution to a defined enum.
4285 ///
4286 /// @param enom the enum to consider for the test.
4287 ///
4288 /// @return true iff @p enom is a declaration-only enum and if
4289 /// it's been scheduled for resolution to a defined enum.
4290 bool
4291 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4292 {
4293 if (enom->get_is_declaration_only())
4294 return (declaration_only_enums().find(enom->get_qualified_name())
4295 != declaration_only_enums().end());
4296
4297 return false;
4298 }
4299
4300 /// Walk the declaration-only enums that have been found during
4301 /// the building of the corpus and resolve them to their definitions.
4302 ///
4303 /// TODO: Do away with this function by factorizing it with
4304 /// resolve_declaration_only_classes. All declaration-only decls
4305 /// could be handled the same way as declaration-only-ness is a
4306 /// property of abigail::ir::decl_base now.
4307 void
4308 resolve_declaration_only_enums()
4309 {
4310 vector<string> resolved_enums;
4311
4312 for (string_enums_map::iterator i =
4313 declaration_only_enums().begin();
4314 i != declaration_only_enums().end();
4315 ++i)
4316 {
4317 bool to_resolve = false;
4318 for (enums_type::iterator j = i->second.begin();
4319 j != i->second.end();
4320 ++j)
4321 if ((*j)->get_is_declaration_only()
4322 && ((*j)->get_definition_of_declaration() == 0))
4323 to_resolve = true;
4324
4325 if (!to_resolve)
4326 {
4327 resolved_enums.push_back(i->first);
4328 continue;
4329 }
4330
4331 // Now, for each decl-only enum that have the current name
4332 // 'i->first', let's try to poke at the fully defined enum
4333 // that is defined in the same translation unit as the
4334 // declaration.
4335 //
4336 // If we find one enum (defined in the TU of the declaration)
4337 // that defines the declaration, then the declaration can be
4338 // resolved to that enum.
4339 //
4340 // If no defining enum is found in the TU of the declaration,
4341 // then there are possibly three cases to consider:
4342 //
4343 // 1/ There is exactly one enum that defines the
4344 // declaration and that enum is defined in another TU. In
4345 // this case, the declaration is resolved to that
4346 // definition.
4347 //
4348 // 2/ There are more than one enum that define that
4349 // declaration and none of them is defined in the TU of the
4350 // declaration. In this case, the declaration is left
4351 // unresolved.
4352 //
4353 // 3/ No enum defines the declaration. In this case, the
4354 // declaration is left unresoved.
4355
4356 // So get the enums that might define the current
4357 // declarations which name is i->first.
4358 const type_base_wptrs_type *enums =
4359 lookup_enum_types(i->first, *corpus());
4360 if (!enums)
4361 continue;
4362
4363 // This is a map that associates the translation unit path to
4364 // the enum (that potentially defines the declarations that
4365 // we consider) that are defined in that translation unit. It
4366 // should stay ordered by using the TU path as key to ensure
4367 // stability of the order of enum definitions in ABIXML
4368 // output.
4369 map<string, enum_type_decl_sptr> per_tu_enum_map;
4370 for (type_base_wptrs_type::const_iterator c = enums->begin();
4371 c != enums->end();
4372 ++c)
4373 {
4374 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4375 ABG_ASSERT(enom);
4376
4378 if (enom->get_is_declaration_only())
4379 continue;
4380
4381 string tu_path = enom->get_translation_unit()->get_absolute_path();
4382 if (tu_path.empty())
4383 continue;
4384
4385 // Build a map that associates the translation unit path
4386 // to the enum (that potentially defines the declarations
4387 // that we consider) that are defined in that translation unit.
4388 per_tu_enum_map[tu_path] = enom;
4389 }
4390
4391 if (!per_tu_enum_map.empty())
4392 {
4393 // Walk the declarations to resolve and resolve them
4394 // either to the definitions that are in the same TU as
4395 // the declaration, or to the definition found elsewhere,
4396 // if there is only one such definition.
4397 for (enums_type::iterator j = i->second.begin();
4398 j != i->second.end();
4399 ++j)
4400 {
4401 if ((*j)->get_is_declaration_only()
4402 && ((*j)->get_definition_of_declaration() == 0))
4403 {
4404 string tu_path =
4405 (*j)->get_translation_unit()->get_absolute_path();
4406 map<string, enum_type_decl_sptr>::const_iterator e =
4407 per_tu_enum_map.find(tu_path);
4408 if (e != per_tu_enum_map.end())
4409 (*j)->set_definition_of_declaration(e->second);
4410 else if (per_tu_enum_map.size() == 1)
4411 (*j)->set_definition_of_declaration
4412 (per_tu_enum_map.begin()->second);
4413 else
4414 {
4415 // We are in case where there are more than
4416 // one definition for the declaration. Let's
4417 // see if they are all equal. If they are,
4418 // then the declaration resolves to the
4419 // definition. Otherwise, we are in the case
4420 // 3/ described above.
4421 map<string,
4422 enum_type_decl_sptr>::const_iterator it;
4423 enum_type_decl_sptr first_enum =
4424 per_tu_enum_map.begin()->second;
4425 bool all_enum_definitions_are_equal = true;
4426 for (it = per_tu_enum_map.begin();
4427 it != per_tu_enum_map.end();
4428 ++it)
4429 {
4430 if (it == per_tu_enum_map.begin())
4431 continue;
4432 else
4433 {
4434 if (!compare_before_canonicalisation(it->second,
4435 first_enum))
4436 {
4437 all_enum_definitions_are_equal = false;
4438 break;
4439 }
4440 }
4441 }
4442 if (all_enum_definitions_are_equal)
4443 (*j)->set_definition_of_declaration(first_enum);
4444 }
4445 }
4446 }
4447 resolved_enums.push_back(i->first);
4448 }
4449 }
4450
4451 size_t num_decl_only_enums = declaration_only_enums().size(),
4452 num_resolved = resolved_enums.size();
4453 if (show_stats())
4454 cerr << "resolved " << num_resolved
4455 << " enum declarations out of "
4456 << num_decl_only_enums
4457 << "\n";
4458
4459 for (vector<string>::const_iterator i = resolved_enums.begin();
4460 i != resolved_enums.end();
4461 ++i)
4462 declaration_only_enums().erase(*i);
4463
4464 if (show_stats() && !declaration_only_enums().empty())
4465 {
4466 cerr << "Here are the "
4467 << num_decl_only_enums - num_resolved
4468 << " unresolved enum declarations:\n";
4469 for (string_enums_map::iterator i = declaration_only_enums().begin();
4470 i != declaration_only_enums().end();
4471 ++i)
4472 cerr << " " << i->first << "\n";
4473 }
4474 }
4475
4476 /// Test if a symbol belongs to a function of the current ABI
4477 /// corpus.
4478 ///
4479 /// This is a sub-routine of fixup_functions_with_no_symbols.
4480 ///
4481 /// @param fn the function symbol to consider.
4482 ///
4483 /// @returnt true if @p fn belongs to a function of the current ABI
4484 /// corpus.
4485 bool
4486 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4487 {
4488 corpus_sptr corp = corpus();
4489 if (!corp)
4490 return false;
4491
4492 interned_string id = corp->get_environment().intern(fn->get_id_string());
4493
4494 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4495 if (!fns)
4496 return false;
4497
4498 for (auto f : *fns)
4499 if (f->get_symbol())
4500 return true;
4501
4502 return false;
4503 }
4504
4505 /// Some functions described by DWARF may have their linkage name
4506 /// set, but no link to their actual underlying elf symbol. When
4507 /// these are virtual member functions, comparing the enclosing type
4508 /// against another one which has its underlying symbol properly set
4509 /// might lead to spurious type changes.
4510 ///
4511 /// If the corpus contains a symbol with the same name as the
4512 /// linkage name of the function, then set up the link between the
4513 /// function and its underlying symbol.
4514 ///
4515 /// Note that for the moment, only virtual member functions are
4516 /// fixed up like this. This is because they really are the only
4517 /// fuctions of functions that can affect types (in spurious ways).
4518 void
4519 fixup_functions_with_no_symbols()
4520 {
4521 corpus_sptr corp = corpus();
4522 if (!corp)
4523 return;
4524
4525 die_function_decl_map_type &fns_with_no_symbol =
4526 die_function_decl_with_no_symbol_map();
4527
4528 if (do_log())
4529 cerr << fns_with_no_symbol.size()
4530 << " functions to fixup, potentially\n";
4531
4532 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4533 i != fns_with_no_symbol.end();
4534 ++i)
4535 if (elf_symbol_sptr sym =
4536 corp->lookup_function_symbol(i->second->get_linkage_name()))
4537 {
4538 // So i->second is a virtual member function that was
4539 // previously scheduled to be set a function symbol.
4540 //
4541 // But if it appears that it now has a symbol already set,
4542 // then do not set a symbol to it again.
4543 //
4544 // Or if it appears that another virtual member function
4545 // from the current ABI Corpus, with the same linkage
4546 // (mangled) name has already been set a symbol, then do not
4547 // set a symbol to this function either. Otherwise, there
4548 // will be two virtual member functions with the same symbol
4549 // in the class and that leads to spurious hard-to-debug
4550 // change reports later down the road.
4551 if (i->second->get_symbol()
4552 || symbol_already_belongs_to_a_function(sym))
4553 continue;
4554
4555 ABG_ASSERT(is_member_function(i->second));
4557 i->second->set_symbol(sym);
4558
4559 if (do_log())
4560 cerr << "fixed up '"
4561 << i->second->get_pretty_representation()
4562 << "' with symbol '"
4563 << sym->get_id_string()
4564 << "'\n";
4565 }
4566
4567 fns_with_no_symbol.clear();
4568 }
4569
4570 /// Return a reference to the vector containing the types created
4571 /// during the binary analysis but that are not tied to a given
4572 /// DWARF DIE.
4573 ///
4574 /// @return reference to the vector containing the types created
4575 /// during the binary analysis but that are not tied to a given
4576 /// DWARF DIE.
4577 const vector<type_base_sptr>&
4578 types_to_canonicalize() const
4579 {return types_to_canonicalize_;}
4580
4581 /// Clear the containers holding types to canonicalize.
4582 void
4583 clear_types_to_canonicalize()
4584 {
4585 types_to_canonicalize_.clear();
4586 }
4587
4588 /// Types that were created but not tied to a particular DIE, must
4589 /// be scheduled for late canonicalization using this method.
4590 ///
4591 /// @param t the type to schedule for late canonicalization.
4592 void
4593 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4594 {
4595 types_to_canonicalize_.push_back(t);
4596 }
4597
4598 /// Canonicalize types which DIE offsets are stored in vectors on
4599 /// the side. This is a sub-routine of
4600 /// reader::perform_late_type_canonicalizing().
4601 ///
4602 /// @param source where the DIE of the types to canonicalize are
4603 /// from.
4604 void
4605 canonicalize_types_scheduled()
4606 {
4607 tools_utils::timer cn_timer;
4608 if (do_log())
4609 {
4610 cerr << "DWARF Reader is going to canonicalize types";
4611 corpus_sptr c = corpus();
4612 if (c)
4613 cerr << " of corpus " << corpus()->get_path() << "\n";
4614 cn_timer.start();
4615 }
4616
4617 if (!types_to_canonicalize().empty())
4618 canonicalize_types(types_to_canonicalize().begin(),
4619 types_to_canonicalize().end(),
4620 [](const vector<type_base_sptr>::const_iterator& i)
4621 {return *i;});
4622
4623 if (do_log())
4624 {
4625 cn_timer.stop();
4626 cerr << "finished canonicalizing types";
4627 corpus_sptr c = corpus();
4628 if (c)
4629 cerr << " of corpus " << corpus()->get_path();
4630 cerr << ": (" << cn_timer << ")\n";
4631 }
4632 }
4633
4634 /// Compute the number of canonicalized and missed types in the late
4635 /// canonicalization phase.
4636 ///
4637 /// @param source where the DIEs of the canonicalized types are
4638 /// from.
4639 ///
4640 /// @param canonicalized the number of types that got canonicalized
4641 /// is added to the value already present in this parameter.
4642 ///
4643 /// @param missed the number of types scheduled for late
4644 /// canonicalization and which couldn't be canonicalized (for a
4645 /// reason) is added to the value already present in this parameter.
4646 void
4647 add_late_canonicalized_types_stats(size_t& canonicalized,
4648 size_t& missed) const
4649 {
4650 for (auto t : types_to_canonicalize())
4651 {
4652 if (t->get_canonical_type())
4653 ++canonicalized;
4654 else
4655 ++missed;
4656 }
4657 }
4658
4659 // Look at the types that need to be canonicalized after the
4660 // translation unit has been constructed and canonicalize them.
4661 void
4662 perform_late_type_canonicalizing()
4663 {
4664 canonicalize_types_scheduled();
4665
4666 if (show_stats())
4667 {
4668 size_t num_canonicalized = 0, num_missed = 0, total = 0;
4669 add_late_canonicalized_types_stats(num_canonicalized,
4670 num_missed);
4671 total = num_canonicalized + num_missed;
4672 cerr << "binary: "
4673 << elf_path()
4674 << "\n";
4675 cerr << " # late canonicalized types: "
4676 << num_canonicalized;
4677 if (total)
4678 cerr << " (" << num_canonicalized * 100 / total << "%)";
4679 cerr << "\n"
4680 << " # missed canonicalization opportunities: "
4681 << num_missed;
4682 if (total)
4683 cerr << " (" << num_missed * 100 / total << "%)";
4684 cerr << "\n";
4685 }
4686
4687 }
4688
4689 const die_tu_map_type&
4690 die_tu_map() const
4691 {return die_tu_map_;}
4692
4694 die_tu_map()
4695 {return die_tu_map_;}
4696
4697 /// Getter for the map that associates a translation unit DIE to the
4698 /// vector of imported unit points that it contains.
4699 ///
4700 /// @param source where the DIEs are from.
4701 ///
4702 /// @return the map.
4704 tu_die_imported_unit_points_map(die_source source) const
4705 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4706
4707 /// Getter for the map that associates a translation unit DIE to the
4708 /// vector of imported unit points that it contains.
4709 ///
4710 /// @param source where the DIEs are from.
4711 ///
4712 /// @return the map.
4714 tu_die_imported_unit_points_map(die_source source)
4715 {
4716 switch (source)
4717 {
4718 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4719 break;
4720 case ALT_DEBUG_INFO_DIE_SOURCE:
4721 return alt_tu_die_imported_unit_points_map_;
4722 case TYPE_UNIT_DIE_SOURCE:
4723 return type_units_tu_die_imported_unit_points_map_;
4724 case NO_DEBUG_INFO_DIE_SOURCE:
4725 case NUMBER_OF_DIE_SOURCES:
4726 // We cannot reach this point.
4728 }
4729 return tu_die_imported_unit_points_map_;
4730 }
4731
4732 /// Reset the current corpus being constructed.
4733 ///
4734 /// This actually deletes the current corpus being constructed.
4735 void
4736 reset_corpus()
4737 {corpus().reset();}
4738
4739 /// Get the map that associates each DIE to its parent DIE. This is
4740 /// for DIEs coming from the main debug info sections.
4741 ///
4742 /// @param source where the DIEs in the map come from.
4743 ///
4744 /// @return the DIE -> parent map.
4746 die_parent_map(die_source source) const
4747 {return const_cast<reader*>(this)->die_parent_map(source);}
4748
4749 /// Get the map that associates each DIE to its parent DIE. This is
4750 /// for DIEs coming from the main debug info sections.
4751 ///
4752 /// @param source where the DIEs in the map come from.
4753 ///
4754 /// @return the DIE -> parent map.
4756 die_parent_map(die_source source)
4757 {
4758 switch (source)
4759 {
4760 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4761 break;
4762 case ALT_DEBUG_INFO_DIE_SOURCE:
4763 return alternate_die_parent_map_;
4764 case TYPE_UNIT_DIE_SOURCE:
4765 return type_section_die_parent_map();
4766 case NO_DEBUG_INFO_DIE_SOURCE:
4767 case NUMBER_OF_DIE_SOURCES:
4769 }
4770 return primary_die_parent_map_;
4771 }
4772
4774 type_section_die_parent_map() const
4775 {return type_section_die_parent_map_;}
4776
4778 type_section_die_parent_map()
4779 {return type_section_die_parent_map_;}
4780
4781 /// Getter of the current translation unit.
4782 ///
4783 /// @return the current translation unit being constructed.
4785 cur_transl_unit() const
4786 {return cur_tu_;}
4787
4788 /// Getter of the current translation unit.
4789 ///
4790 /// @return the current translation unit being constructed.
4792 cur_transl_unit()
4793 {return cur_tu_;}
4794
4795 /// Setter of the current translation unit.
4796 ///
4797 /// @param tu the current translation unit being constructed.
4798 void
4799 cur_transl_unit(translation_unit_sptr tu)
4800 {
4801 if (tu)
4802 cur_tu_ = tu;
4803 }
4804
4805 /// Return the global scope of the current translation unit.
4806 ///
4807 /// @return the global scope of the current translation unit.
4808 const scope_decl_sptr&
4809 global_scope() const
4810 {return cur_transl_unit()->get_global_scope();}
4811
4812 /// Return a scope that is nil.
4813 ///
4814 /// @return a scope that is nil.
4815 const scope_decl_sptr&
4816 nil_scope() const
4817 {return nil_scope_;}
4818
4819 const scope_stack_type&
4820 scope_stack() const
4821 {return scope_stack_;}
4822
4824 scope_stack()
4825 {return scope_stack_;}
4826
4827 scope_decl*
4828 current_scope()
4829 {
4830 if (scope_stack().empty())
4831 {
4832 if (cur_transl_unit())
4833 scope_stack().push(cur_transl_unit()->get_global_scope().get());
4834 }
4835 return scope_stack().top();
4836 }
4837
4838 list<var_decl_sptr>&
4839 var_decls_to_re_add_to_tree()
4840 {return var_decls_to_add_;}
4841
4842 /// Test if a DIE represents a decl (function or variable) that has
4843 /// a symbol that is exported, whatever that means. This is
4844 /// supposed to work for Linux Kernel binaries as well.
4845 ///
4846 /// This is useful to limit the amount of DIEs taken into account to
4847 /// the strict limit of what an ABI actually means. Limiting the
4848 /// volume of DIEs analyzed this way is an important optimization to
4849 /// keep big binaries "manageable" by libabigail.
4850 ///
4851 /// @param DIE the die to consider.
4852 bool
4853 is_decl_die_with_exported_symbol(const Dwarf_Die *die)
4854 {
4855 if (!die || !die_is_decl(die))
4856 return false;
4857
4858 bool result = false, address_found = false, symbol_is_exported = false;;
4859 Dwarf_Addr decl_symbol_address = 0;
4860
4861 if (die_is_variable_decl(die))
4862 {
4863 if ((address_found = get_variable_address(die, decl_symbol_address)))
4864 symbol_is_exported =
4865 !!variable_symbol_is_exported(decl_symbol_address);
4866 }
4867 else if (die_is_function_decl(die))
4868 {
4869 if ((address_found = get_function_address(die, decl_symbol_address)))
4870 symbol_is_exported =
4871 !!function_symbol_is_exported(decl_symbol_address);
4872 }
4873
4874 if (address_found)
4875 result = symbol_is_exported;
4876
4877 return result;
4878 }
4879
4880 /// This is a sub-routine of maybe_adjust_fn_sym_address and
4881 /// maybe_adjust_var_sym_address.
4882 ///
4883 /// Given an address that we got by looking at some debug
4884 /// information (e.g, a symbol's address referred to by a DWARF
4885 /// TAG), If the ELF file we are interested in is a shared library
4886 /// or an executable, then adjust the address to be coherent with
4887 /// where the executable (or shared library) is loaded. That way,
4888 /// the address can be used to look for symbols in the executable or
4889 /// shared library.
4890 ///
4891 /// @return the adjusted address, or the same address as @p addr if
4892 /// it didn't need any adjustment.
4893 Dwarf_Addr
4894 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4895 {
4896 if (addr == 0)
4897 return addr;
4898
4899 GElf_Ehdr eh_mem;
4900 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4901
4902 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4903 {
4904 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4905 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
4906 dwarf_elf_load_address));
4908 elf_load_address));
4909 if (dwarf_is_splitted()
4910 && (dwarf_elf_load_address != elf_load_address))
4911 // This means that in theory the DWARF and the executable are
4912 // not loaded at the same address. And addr is meaningful
4913 // only in the context of the DWARF.
4914 //
4915 // So let's transform addr into an offset relative to where
4916 // the DWARF is loaded, and let's add that relative offset
4917 // to the load address of the executable. That way, addr
4918 // becomes meaningful in the context of the executable and
4919 // can thus be used to compare against the address of
4920 // symbols of the executable, for instance.
4921 addr = addr - dwarf_elf_load_address + elf_load_address;
4922 }
4923
4924 return addr;
4925 }
4926
4927 /// For a relocatable (*.o) elf file, this function expects an
4928 /// absolute address, representing a function symbol. It then
4929 /// extracts the address of the .text section from the symbol
4930 /// absolute address to get the relative address of the function
4931 /// from the beginning of the .text section.
4932 ///
4933 /// For executable or shared library, this function expects an
4934 /// address of a function symbol that was retrieved by looking at a
4935 /// DWARF "file". The function thus adjusts the address to make it
4936 /// be meaningful in the context of the ELF file.
4937 ///
4938 /// In both cases, the address can then be compared against the
4939 /// st_value field of a function symbol from the ELF file.
4940 ///
4941 /// @param addr an adress for a function symbol that was retrieved
4942 /// from a DWARF file.
4943 ///
4944 /// @return the (possibly) adjusted address, or just @p addr if no
4945 /// adjustment took place.
4946 Dwarf_Addr
4947 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4948 {
4949 if (addr == 0)
4950 return addr;
4951
4952 Elf* elf = elf_handle();
4953 GElf_Ehdr eh_mem;
4954 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4955
4956 if (elf_header->e_type == ET_REL)
4957 // We are looking at a relocatable file. In this case, we don't
4958 // do anything because:
4959 //
4960 // 1/ the addresses from DWARF are absolute (relative to the
4961 // beginning of the relocatable file)
4962 //
4963 // 2/ The ELF symbol addresses that we store in our lookup
4964 // tables are translated from section-related to absolute as
4965 // well. So we don't have anything to do at this point for
4966 // ET_REL files.
4967 ;
4968 else
4969 addr = maybe_adjust_address_for_exec_or_dyn(addr);
4970
4971 return addr;
4972 }
4973
4974 /// For a relocatable (*.o) elf file, this function expects an
4975 /// absolute address, representing a global variable symbol. It
4976 /// then extracts the address of the {.data,.data1,.rodata,.bss}
4977 /// section from the symbol absolute address to get the relative
4978 /// address of the variable from the beginning of the data section.
4979 ///
4980 /// For executable or shared library, this function expects an
4981 /// address of a variable symbol that was retrieved by looking at a
4982 /// DWARF "file". The function thus adjusts the address to make it
4983 /// be meaningful in the context of the ELF file.
4984 ///
4985 /// In both cases, the address can then be compared against the
4986 /// st_value field of a function symbol from the ELF file.
4987 ///
4988 /// @param addr an address for a global variable symbol that was
4989 /// retrieved from a DWARF file.
4990 ///
4991 /// @return the (possibly) adjusted address, or just @p addr if no
4992 /// adjustment took place.
4993 Dwarf_Addr
4994 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
4995 {
4996 Elf* elf = elf_handle();
4997 GElf_Ehdr eh_mem;
4998 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4999
5000 if (elf_header->e_type == ET_REL)
5001 // We are looking at a relocatable file. In this case, we don't
5002 // do anything because:
5003 //
5004 // 1/ the addresses from DWARF are absolute (relative to the
5005 // beginning of the relocatable file)
5006 //
5007 // 2/ The ELF symbol addresses that we store in our lookup
5008 // tables are translated from section-related to absolute as
5009 // well. So we don't have anything to do at this point for
5010 // ET_REL files.
5011 ;
5012 else
5013 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5014
5015 return addr;
5016 }
5017
5018 /// Get the first exported function address in the set of addresses
5019 /// referred to by the DW_AT_ranges attribute of a given DIE.
5020 ///
5021 /// @param die the DIE we are considering.
5022 ///
5023 /// @param address output parameter. This is set to the first
5024 /// address found in the sequence pointed to by the DW_AT_ranges
5025 /// attribute found on the DIE @p die, iff the function returns
5026 /// true. Otherwise, no value is set into this output parameter.
5027 ///
5028 /// @return true iff the DIE @p die does have a DW_AT_ranges
5029 /// attribute and an address of an exported function was found in
5030 /// its sequence value.
5031 bool
5032 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5033 Dwarf_Addr& address) const
5034 {
5035 Dwarf_Addr base;
5036 Dwarf_Addr end_addr;
5037 ptrdiff_t offset = 0;
5038
5039 do
5040 {
5041 Dwarf_Addr addr = 0, fn_addr = 0;
5042 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5043 {
5044 fn_addr = maybe_adjust_fn_sym_address(addr);
5045 if (function_symbol_is_exported(fn_addr))
5046 {
5047 address = fn_addr;
5048 return true;
5049 }
5050 }
5051 } while (offset > 0);
5052 return false;
5053 }
5054
5055 /// Get the address of the function.
5056 ///
5057 /// The address of the function is considered to be the value of the
5058 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5059 /// only) to not point to an absolute address anymore, but rather to
5060 /// the address of the function inside the .text segment.
5061 ///
5062 /// @param function_die the die of the function to consider.
5063 ///
5064 /// @param address the resulting address iff the function returns
5065 /// true.
5066 ///
5067 /// @return true if the function address was found.
5068 bool
5069 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5070 {
5071 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5072 DW_AT_low_pc, address))
5073 // So no DW_AT_low_pc was found. Let's see if the function DIE
5074 // has got a DW_AT_ranges attribute instead. If it does, the
5075 // first address of the set of addresses represented by the
5076 // value of that DW_AT_ranges represents the function (symbol)
5077 // address we are looking for.
5078 if (!get_first_exported_fn_address_from_DW_AT_ranges
5079 (const_cast<Dwarf_Die*>(function_die),
5080 address))
5081 return false;
5082
5083 address = maybe_adjust_fn_sym_address(address);
5084 return true;
5085 }
5086
5087 /// Get the address of the global variable.
5088 ///
5089 /// The address of the global variable is considered to be the value
5090 /// of the DW_AT_location attribute, possibly adjusted (in
5091 /// relocatable files only) to not point to an absolute address
5092 /// anymore, but rather to the address of the global variable inside
5093 /// the data segment.
5094 ///
5095 /// @param variable_die the die of the function to consider.
5096 ///
5097 /// @param address the resulting address iff this function returns
5098 /// true.
5099 ///
5100 /// @return true if the variable address was found.
5101 bool
5102 get_variable_address(const Dwarf_Die* variable_die,
5103 Dwarf_Addr& address) const
5104 {
5105 bool is_tls_address = false;
5106 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5107 address, is_tls_address))
5108 return false;
5109 if (!is_tls_address)
5110 address = maybe_adjust_var_sym_address(address);
5111 return true;
5112 }
5113
5114 /// Getter of the exported decls builder object.
5115 ///
5116 /// @return the exported decls builder.
5117 corpus::exported_decls_builder*
5118 exported_decls_builder()
5119 {return corpus()->get_exported_decls_builder().get();}
5120
5121 /// Getter of the "load_all_types" flag. This flag tells if all the
5122 /// types (including those not reachable by public declarations) are
5123 /// to be read and represented in the final ABI corpus.
5124 ///
5125 /// @return the load_all_types flag.
5126 bool
5127 load_all_types() const
5128 {return options().load_all_types;}
5129
5130 /// Setter of the "load_all_types" flag. This flag tells if all the
5131 /// types (including those not reachable by public declarations) are
5132 /// to be read and represented in the final ABI corpus.
5133 ///
5134 /// @param f the new load_all_types flag.
5135 void
5136 load_all_types(bool f)
5137 {options().load_all_types = f;}
5138
5139 bool
5140 load_in_linux_kernel_mode() const
5141 {return options().load_in_linux_kernel_mode;}
5142
5143 void
5144 load_in_linux_kernel_mode(bool f)
5145 {options().load_in_linux_kernel_mode = f;}
5146
5147 /// Test if it's allowed to assume that the DWARF debug info has
5148 /// been factorized (for instance, with the DWZ tool) so that if two
5149 /// type DIEs originating from the .gnu_debugaltlink section have
5150 /// different offsets, they represent different types.
5151 ///
5152 /// @return true iff we can assume that the DWARF debug info has
5153 /// been factorized.
5154 bool
5155 leverage_dwarf_factorization() const
5156 {
5157 if (!leverage_dwarf_factorization_.has_value())
5158 {
5159 if (options().leverage_dwarf_factorization
5160 && elf_helpers::find_section_by_name(elf_handle(),
5161 ".gnu_debugaltlink"))
5162 leverage_dwarf_factorization_ = true;
5163 else
5164 leverage_dwarf_factorization_ = false;
5165 }
5166 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5167
5168 return *leverage_dwarf_factorization_;
5169 }
5170 /// Getter of the "show_stats" flag.
5171 ///
5172 /// This flag tells if we should emit statistics about various
5173 /// internal stuff.
5174 ///
5175 /// @return the value of the flag.
5176 bool
5177 show_stats() const
5178 {return options().show_stats;}
5179
5180 /// Setter of the "show_stats" flag.
5181 ///
5182 /// This flag tells if we should emit statistics about various
5183 /// internal stuff.
5184 ///
5185 /// @param f the value of the flag.
5186 void
5187 show_stats(bool f)
5188 {options().show_stats = f;}
5189
5190 /// Getter of the "do_log" flag.
5191 ///
5192 /// This flag tells if we should log about various internal
5193 /// details.
5194 ///
5195 /// return the "do_log" flag.
5196 bool
5197 do_log() const
5198 {return options().do_log;}
5199
5200 /// Setter of the "do_log" flag.
5201 ///
5202 /// This flag tells if we should log about various internal details.
5203 ///
5204 /// @param f the new value of the flag.
5205 void
5206 do_log(bool f)
5207 {options().do_log = f;}
5208
5209 /// Walk the DIEs under a given die and for each child, populate the
5210 /// die -> parent map to record the child -> parent relationship
5211 /// that
5212 /// exists between the child and the given die.
5213 ///
5214 /// The function also builds the vector of places where units are
5215 /// imported.
5216 ///
5217 /// This is done recursively as for each child DIE, this function
5218 /// walks its children as well.
5219 ///
5220 /// @param die the DIE whose children to walk recursively.
5221 ///
5222 /// @param source where the DIE @p die comes from.
5223 ///
5224 /// @param imported_units a vector containing all the offsets of the
5225 /// points where unit have been imported, under @p die.
5226 void
5227 build_die_parent_relations_under(Dwarf_Die* die,
5228 die_source source,
5229 imported_unit_points_type & imported_units)
5230 {
5231 if (!die)
5232 return;
5233
5234 offset_offset_map_type& parent_of = die_parent_map(source);
5235
5236 Dwarf_Die child;
5237 if (dwarf_child(die, &child) != 0)
5238 return;
5239
5240 do
5241 {
5242 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5243 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5244 {
5245 Dwarf_Die imported_unit;
5246 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5247 // If the imported_unit has a sub-tree, let's record
5248 // this point at which the sub-tree is imported into
5249 // the current debug info.
5250 //
5251 // Otherwise, if the imported_unit has no sub-tree,
5252 // there is no point in recording where a non-existent
5253 // sub-tree is being imported.
5254 //
5255 // Note that the imported_unit_points_type type below
5256 // expects the imported_unit to have a sub-tree.
5257 && die_has_children(&imported_unit))
5258 {
5259 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5260 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5261 imported_units.push_back
5262 (imported_unit_point(dwarf_dieoffset(&child),
5263 imported_unit,
5264 imported_unit_die_source));
5265 }
5266 }
5267 build_die_parent_relations_under(&child, source, imported_units);
5268 }
5269 while (dwarf_siblingof(&child, &child) == 0);
5270
5271 }
5272
5273 /// Determine if we do have to build a DIE -> parent map, depending
5274 /// on a given language.
5275 ///
5276 /// Some languages like C++, Ada etc, do have the concept of
5277 /// namespace and yet, the DIE data structure doesn't provide us
5278 /// with a way to get the parent namespace of a given DIE. So for
5279 /// those languages, we need to build a DIE -> parent map so that we
5280 /// can get the namespace DIE (or more generally the scope DIE) of a given
5281 /// DIE as we need it.
5282 ///
5283 /// But then some more basic languages like C or assembly don't have
5284 /// that need.
5285 ///
5286 /// This function, depending on the language, tells us if we need to
5287 /// build the DIE -> parent map or not.
5288 ///
5289 /// @param lang the language to consider.
5290 ///
5291 /// @return true iff we need to build the DIE -> parent map for this
5292 /// language.
5293 bool
5294 do_we_build_die_parent_maps(translation_unit::language lang)
5295 {
5296 if (is_c_language(lang))
5297 return false;
5298
5299 switch (lang)
5300 {
5301 case translation_unit::LANG_UNKNOWN:
5302#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5303 case translation_unit::LANG_Mips_Assembler:
5304#endif
5305 return false;
5306 default:
5307 break;
5308 }
5309 return true;
5310 }
5311
5312 /// Walk all the DIEs accessible in the debug info (and in the
5313 /// alternate debug info as well) and build maps representing the
5314 /// relationship DIE -> parent. That is, make it so that we can get
5315 /// the parent for a given DIE.
5316 ///
5317 /// Note that the goal of this map is to be able to get the parent
5318 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5319 /// when we get a DIE of a type, and we want to build an internal
5320 /// representation for it, we need to get its fully qualified name.
5321 /// For that, we need to know what is the parent DIE of that type
5322 /// DIE, so that we can know what the namespace of that type is.
5323 ///
5324 /// Note that as the C language doesn't have namespaces (all types
5325 /// are defined in the same global namespace), this function doesn't
5326 /// build the DIE -> parent map if the current translation unit
5327 /// comes from C. This saves time on big C ELF files with a lot of
5328 /// DIEs.
5329 void
5330 build_die_parent_maps()
5331 {
5332 bool we_do_have_to_build_die_parent_map = false;
5333 uint8_t address_size = 0;
5334 size_t header_size = 0;
5335 // Get the DIE of the current translation unit, look at it to get
5336 // its language. If that language is in C, then all types are in
5337 // the global namespace so we don't need to build the DIE ->
5338 // parent map. So we dont build it in that case.
5339 for (Dwarf_Off offset = 0, next_offset = 0;
5340 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5341 offset, &next_offset, &header_size,
5342 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5343 offset = next_offset)
5344 {
5345 Dwarf_Off die_offset = offset + header_size;
5346 Dwarf_Die cu;
5347 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5348 die_offset, &cu))
5349 continue;
5350
5351 uint64_t l = 0;
5352 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5353 translation_unit::language lang = dwarf_language_to_tu_language(l);
5354 if (do_we_build_die_parent_maps(lang))
5355 we_do_have_to_build_die_parent_map = true;
5356 }
5357
5358 if (!we_do_have_to_build_die_parent_map)
5359 return;
5360
5361 // Build the DIE -> parent relation for DIEs coming from the
5362 // .debug_info section in the alternate debug info file.
5363 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5364 for (Dwarf_Off offset = 0, next_offset = 0;
5365 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5366 offset, &next_offset, &header_size,
5367 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5368 offset = next_offset)
5369 {
5370 Dwarf_Off die_offset = offset + header_size;
5371 Dwarf_Die cu;
5372 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5373 die_offset, &cu))
5374 continue;
5375 cur_tu_die(&cu);
5376
5377 imported_unit_points_type& imported_units =
5378 tu_die_imported_unit_points_map(source)[die_offset] =
5380 build_die_parent_relations_under(&cu, source, imported_units);
5381 }
5382
5383 // Build the DIE -> parent relation for DIEs coming from the
5384 // .debug_info section of the main debug info file.
5385 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5386 address_size = 0;
5387 header_size = 0;
5388 for (Dwarf_Off offset = 0, next_offset = 0;
5389 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5390 offset, &next_offset, &header_size,
5391 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5392 offset = next_offset)
5393 {
5394 Dwarf_Off die_offset = offset + header_size;
5395 Dwarf_Die cu;
5396 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5397 die_offset, &cu))
5398 continue;
5399 cur_tu_die(&cu);
5400 imported_unit_points_type& imported_units =
5401 tu_die_imported_unit_points_map(source)[die_offset] =
5403 build_die_parent_relations_under(&cu, source, imported_units);
5404 }
5405
5406 // Build the DIE -> parent relation for DIEs coming from the
5407 // .debug_types section.
5408 source = TYPE_UNIT_DIE_SOURCE;
5409 address_size = 0;
5410 header_size = 0;
5411 uint64_t type_signature = 0;
5412 Dwarf_Off type_offset;
5413 for (Dwarf_Off offset = 0, next_offset = 0;
5414 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5415 offset, &next_offset, &header_size,
5416 NULL, NULL, &address_size, NULL,
5417 &type_signature, &type_offset) == 0);
5418 offset = next_offset)
5419 {
5420 Dwarf_Off die_offset = offset + header_size;
5421 Dwarf_Die cu;
5422
5423 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5424 die_offset, &cu))
5425 continue;
5426 cur_tu_die(&cu);
5427 imported_unit_points_type& imported_units =
5428 tu_die_imported_unit_points_map(source)[die_offset] =
5430 build_die_parent_relations_under(&cu, source, imported_units);
5431 }
5432 }
5433};// end class reader.
5434
5435/// The type of the aggregates being compared during a DIE comparison.
5436///
5437/// This encapsulates the stack of aggregates being compared at any
5438/// single point.
5439///
5440/// This is useful to detect "comparison cycles" and thus avoid the
5441/// resulting infinite loops.
5442///
5443/// This is also useful for implementing a very important optimization
5444/// that takes place during the canonicalization
5445struct offset_pairs_stack_type
5446{
5447 // The DWARF DWARF reader that is useful for so many things.
5448 const reader& rdr_;
5449 // The set of types that are being compared. This is to speed up
5450 // searches.
5452 // The stack of types that are being compared. The top of the
5453 // stack is the back of the vector.
5455 // A map that associates a redundant type pair to the vector of
5456 // types that depends on it.
5457 offset_pair_vect_map_type redundant_types_;
5458 // A map that associates a dependant type to the vector of redundant
5459 // types it depends on.
5460 offset_pair_vect_map_type dependant_types_;
5461
5462 offset_pairs_stack_type(const reader& rdr)
5463 : rdr_ (rdr)
5464 {}
5465
5466 /// Add a pair of types being compared to the stack of aggregates
5467 /// being compared.
5468 ///
5469 /// @param p the pair of offsets of the type DIEs to consider.
5470 void
5471 add(const offset_pair_type& p)
5472 {
5473 set_.insert(p);
5474 vect_.push_back(p);
5475 }
5476
5477 /// Erase a pair of types being compared from the stack of
5478 /// aggregates being compared.
5479 ///
5480 /// @param p the pair of offsets of the type DIEs to consider.
5481 ///
5482 /// @return true iff @p was found and erased from the stack.
5483 bool
5484 erase(const offset_pair_type& p)
5485 {
5486 if (set_.erase(p))
5487 {
5488 offset_pair_vector_type::iterator i;
5489
5490 for (i = vect_.begin();i < vect_.end(); ++i)
5491 if (*i == p)
5492 break;
5493
5494 if (i != vect_.end())
5495 vect_.erase(i);
5496
5497 return true;
5498 }
5499
5500 return false;
5501 }
5502
5503 /// Test if a pair of type DIEs is part of the stack of type DIEs
5504 /// being compared.
5505 ///
5506 /// @param p the pair of offsets of the type DIEs to consider.
5507 ///
5508 /// @return true iff @p was found in the stack of types being
5509 /// compared.
5510 bool
5511 contains(const offset_pair_type &p) const
5512 {
5513 if (set_.find(p) == set_.end())
5514 return false;
5515 return true;
5516 }
5517
5518 /// Get the set of comparison pair that depends on a given
5519 /// comparison pair.
5520 ///
5521 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5522 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5523 /// words, the pair T appears in the comparison stack BEFORE the
5524 /// pair P.
5525 ///
5526 /// So, this function returns the vector of comparison pairs that
5527 /// appear in the comparison stack AFTER a given comparison pair.
5528 ///
5529 /// @param p the comparison pair to consider.
5530 ///
5531 /// @param pairs out parameter. This is filled with the comparison
5532 /// pairs that depend on @p, iff the function returns true.
5533 ///
5534 /// @return true iff comparison pairs depending on @p have been
5535 /// found and collected in @pairs.
5536 bool
5537 get_pairs_that_depend_on(const offset_pair_type& p,
5538 offset_pair_vector_type& pairs) const
5539 {
5540 bool result = false;
5541 if (!contains(p))
5542 return result;
5543
5544 // First, get an iterator on the position of 'p'.
5545 offset_pair_vector_type::const_iterator i;
5546 for (i = vect_.begin(); i != vect_.end(); ++i)
5547 if (*i == p)
5548 break;
5549
5550 if (i == vect_.end())
5551 return result;
5552
5553 // Then, harvest all the comparison pairs that come after the
5554 // position of 'p'.
5555 for (++i; i != vect_.end(); ++i)
5556 {
5557 pairs.push_back(*i);
5558 result = true;
5559 }
5560
5561 return result;
5562 }
5563
5564 /// Record the fact that a set of comparison pairs depends on a
5565 /// given comparison pair.
5566 ///
5567 /// Set a map that associates each dependant comparison pair to the
5568 /// pair it depends on.
5569 ///
5570 /// @param p the comparison pair that the set depends on.
5571 ///
5572 /// @param dependant_types the set of types that depends on @p.
5573 void
5574 record_dependant_types(const offset_pair_type& p,
5575 const offset_pair_vector_type& dependant_types)
5576 {
5577 for (auto type_pair : dependant_types)
5578 dependant_types_[type_pair].push_back(p);
5579 }
5580
5581 /// Record a comparison pair as being redundant.
5582 ///
5583 ///
5584 /// @param p the comparison pair to record as redundant.
5585 void
5586 record_redundant_type_die_pair(const offset_pair_type& p)
5587 {
5588 offset_pair_vector_type dependant_types;
5589 get_pairs_that_depend_on(p, dependant_types);
5590
5591 // First, record the relationship "p -> [pairs that depend on p]".
5592 auto it = redundant_types_.find(p);
5593 if (it == redundant_types_.end())
5594 {
5595 auto entry = std::make_pair(p, dependant_types);
5596 redundant_types_.insert(entry);
5597 }
5598 else
5599 it->second.insert(it->second.end(),
5600 dependant_types.begin(),
5601 dependant_types.end());
5602
5603 // For each dependant type pair, record the association:
5604 // dependant_pair --> [vect of redundant types]
5605 record_dependant_types(p, dependant_types);
5606 }
5607
5608 /// Test if a given pair has been detected as redundant.
5609 ///
5610 /// @param p the pair of DIEs to consider.
5611 ///
5612 /// @return iff @p is redundant.
5613 bool
5614 is_redundant(const offset_pair_type& p)
5615 {
5616 auto i = redundant_types_.find(p);
5617 if (i != redundant_types_.end())
5618 return true;
5619 return false;
5620 }
5621
5622 /// Test if a given pair is dependant on at least a redundant type.
5623 ///
5624 /// @param p the pair to consider.
5625 ///
5626 /// @return true iff @p depends on a redundant type.
5627 bool
5628 depends_on_redundant_types(const offset_pair_type& p)
5629 {
5630 auto i = dependant_types_.find(p);
5631 if (i == dependant_types_.end())
5632 return false;
5633 return true;
5634 }
5635
5636 /// Remove a redundant pair from the system.
5637 ///
5638 /// This needs updating the system to also remove the dependant
5639 /// types that depend on the redundant pair (if they depend only on
5640 /// that redundant pair).
5641 ///
5642 /// @param p the pair to consider.
5643 ///
5644 /// @param erase_canonical_die_offset if true then erase the cached
5645 /// comparison results for the redundant pair and its dependant
5646 /// types.
5647 void
5648 erase_redundant_type_pair_entry(const offset_pair_type& p,
5649 bool erase_cached_results = false)
5650 {
5651 // First, update the dependant types that depend on the redundant
5652 // type pair
5653 auto redundant_type = redundant_types_.find(p);
5654 if (redundant_type != redundant_types_.end())
5655 {
5656 for (auto dependant_type : redundant_type->second)
5657 {
5658 // Each dependant_type depends on the redundant type 'p',
5659 // among others.
5660 auto dependant_types_it = dependant_types_.find(dependant_type);
5661 ABG_ASSERT(dependant_types_it != dependant_types_.end());
5662 // Erase the redundant type 'p' from the redundant types
5663 // that dependant_type depends on.
5664 {
5665 auto i = dependant_types_it->second.begin();
5666 for (; i!= dependant_types_it->second.end();++i)
5667 if (*i == p)
5668 break;
5669 if (i != dependant_types_it->second.end())
5670 dependant_types_it->second.erase(i);
5671 }
5672 // If the dependant type itself doesn't depend on ANY
5673 // redundant type anymore, then remove the depend type
5674 // from the map of the dependant types.
5675 if (dependant_types_it->second.empty())
5676 {
5677 if (erase_cached_results)
5678 rdr_.die_comparison_results_.erase(dependant_type);
5679 dependant_types_.erase(dependant_types_it);
5680 }
5681 }
5682 }
5683 if (erase_cached_results)
5684 rdr_.die_comparison_results_.erase(p);
5685 redundant_types_.erase(p);
5686 }
5687
5688 /// If a comparison pair has been detected as redundant, stop
5689 /// tracking it as well as its dependant pairs. That will
5690 /// essentially make it impossible to reset/cancel the canonical
5691 /// propagated types for those depdant pairs, but will also save
5692 /// ressources.
5693 ///
5694 /// @param p the comparison pair to consider.
5695 void
5696 confirm_canonical_propagated_type(const offset_pair_type& p)
5697 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5698
5699 /// Walk the types that depend on a comparison pair and cancel their
5700 /// canonical-propagate-type, that means remove their canonical
5701 /// types and mark them as not being canonically-propagated. Also,
5702 /// erase their cached comparison results that was likely set to
5703 /// COMPARISON_RESULT_UNKNOWN.
5704 ///
5705 /// @param p the pair to consider.
5706 void
5707 cancel_canonical_propagated_type(const offset_pair_type& p)
5708 {
5709 offset_pair_set_type dependant_types;
5710 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5711 for (auto dependant_type : dependant_types)
5712 {
5713 // If this dependant type was canonical-type-propagated then
5714 // erase that canonical type.
5715 if (rdr_.propagated_types_.find(dependant_type)
5716 != rdr_.propagated_types_.end())
5717 {
5718 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5719 dependant_type.first.source_,
5720 /*die_as_type=*/true);
5721 rdr_.propagated_types_.erase(dependant_type);
5722 rdr_.cancelled_propagation_count_++;
5723 }
5724 // Update the cached result. We know the comparison result
5725 // must now be different.
5726 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
5727 if (comp_result_it != rdr_.die_comparison_results_.end())
5728 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5729 }
5730
5731 // Update the cached result of the root type to cancel too.
5732 auto comp_result_it = rdr_.die_comparison_results_.find(p);
5733 if (comp_result_it != rdr_.die_comparison_results_.end())
5734 {
5735 // At this point, the result of p is either
5736 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
5737 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
5738 // Make sure it's the cached result is now
5739 // COMPARISON_RESULT_DIFFERENT.
5740 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
5741 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5742 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
5743 }
5744
5745 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5746 {
5747 rdr_.erase_canonical_die_offset(p.first.offset_,
5748 p.first.source_,
5749 /*die_as_type=*/true);
5750 rdr_.propagated_types_.erase(p);
5751 rdr_.cancelled_propagation_count_++;
5752 }
5753 }
5754
5755 /// Get the set of comparison pairs that depend on a given pair.
5756 ///
5757 /// @param p the pair to consider.
5758 ///
5759 /// @param result this is set to the pairs that depend on @p, iff
5760 /// the function returned true.
5761 ///
5762 /// @param transitive_closure if set to true, the transitive closure
5763 /// of the @result is set to it.
5764 ///
5765 /// @return true iff @result could be filled with the dependant
5766 /// types.
5767 bool
5768 get_dependant_types(const offset_pair_type& p,
5769 offset_pair_set_type& result,
5770 bool transitive_closure = false)
5771 {
5772 auto i = redundant_types_.find(p);
5773 if (i != redundant_types_.end())
5774 {
5775 for (auto dependant_type : i->second)
5776 if (result.find(dependant_type) == result.end())
5777 {
5778 result.insert(dependant_type);
5779 if (transitive_closure)
5780 get_dependant_types(p, result, /*transitive_closure=*/true);
5781 }
5782 return true;
5783 }
5784 return false;
5785 }
5786}; // end struct offset_pairs_stack_type
5787
5789build_ir_node_from_die(reader& rdr,
5790 Dwarf_Die* die,
5791 scope_decl* scope,
5792 bool called_from_public_decl,
5793 size_t where_offset,
5794 bool is_declaration_only = true,
5795 bool is_required_decl_spec = false);
5796
5798build_ir_node_from_die(reader& rdr,
5799 Dwarf_Die* die,
5800 bool called_from_public_decl,
5801 size_t where_offset);
5802
5803static decl_base_sptr
5804build_ir_node_for_void_type(reader& rdr);
5805
5807build_ir_node_for_void_pointer_type(reader& rdr);
5808
5809static class_decl_sptr
5810add_or_update_class_type(reader& rdr,
5811 Dwarf_Die* die,
5812 scope_decl* scope,
5813 bool is_struct,
5814 class_decl_sptr klass,
5815 bool called_from_public_decl,
5816 size_t where_offset,
5817 bool is_declaration_only);
5818
5819static union_decl_sptr
5820add_or_update_union_type(reader& rdr,
5821 Dwarf_Die* die,
5822 scope_decl* scope,
5823 union_decl_sptr union_type,
5824 bool called_from_public_decl,
5825 size_t where_offset,
5826 bool is_declaration_only);
5827
5828static decl_base_sptr
5829build_ir_node_for_void_type(reader& rdr);
5830
5831static decl_base_sptr
5832build_ir_node_for_variadic_parameter_type(reader &rdr);
5833
5834static function_decl_sptr
5835build_function_decl(reader& rdr,
5836 Dwarf_Die* die,
5837 size_t where_offset,
5839
5840static bool
5841function_is_suppressed(const reader& rdr,
5842 const scope_decl* scope,
5843 Dwarf_Die *function_die,
5844 bool is_declaration_only);
5845
5846static function_decl_sptr
5847build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5848 scope_decl *scope,
5849 Dwarf_Die *die,
5850 size_t where_offset,
5851 bool is_declaration_only,
5853
5854static var_decl_sptr
5855build_var_decl(reader& rdr,
5856 Dwarf_Die *die,
5857 size_t where_offset,
5858 var_decl_sptr result = var_decl_sptr());
5859
5860static var_decl_sptr
5861build_or_get_var_decl_if_not_suppressed(reader& rdr,
5862 scope_decl *scope,
5863 Dwarf_Die *die,
5864 size_t where_offset,
5866 bool is_required_decl_spec = false);
5867static bool
5868variable_is_suppressed(const reader& rdr,
5869 const scope_decl* scope,
5870 Dwarf_Die *variable_die,
5871 bool is_required_decl_spec = false);
5872
5873static void
5874finish_member_function_reading(Dwarf_Die* die,
5875 const function_decl_sptr& f,
5876 const class_or_union_sptr klass,
5877 reader& rdr);
5878
5879/// Test if a given DIE is anonymous
5880///
5881/// @param die the DIE to consider.
5882///
5883/// @return true iff @p die is anonymous.
5884static bool
5885die_is_anonymous(const Dwarf_Die* die)
5886{
5887 Dwarf_Attribute attr;
5888 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5889 return true;
5890 return false;
5891}
5892
5893/// Test if a DIE is an anonymous data member, aka, "unnamed field".
5894///
5895/// Unnamed fields are specified at
5896/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
5897///
5898/// @param die the DIE to consider.
5899///
5900/// @return true iff @p die is an anonymous data member.
5901static bool
5902die_is_anonymous_data_member(const Dwarf_Die* die)
5903{
5904 if (!die
5905 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
5906 || !die_name(die).empty())
5907 return false;
5908
5909 Dwarf_Die type_die;
5910 if (!die_die_attribute(die, DW_AT_type, type_die))
5911 return false;
5912
5913 if (dwarf_tag(&type_die) != DW_TAG_structure_type
5914 && dwarf_tag(&type_die) != DW_TAG_union_type)
5915 return false;
5916
5917 return true;
5918}
5919
5920/// Get the value of an attribute that is supposed to be a string, or
5921/// an empty string if the attribute could not be found.
5922///
5923/// @param die the DIE to get the attribute value from.
5924///
5925/// @param attr_name the attribute name. Must come from dwarf.h and
5926/// be an enumerator representing an attribute like, e.g, DW_AT_name.
5927///
5928/// @return the string representing the value of the attribute, or an
5929/// empty string if no string attribute could be found.
5930static string
5931die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5932{
5933 if (!die)
5934 return "";
5935
5936 Dwarf_Attribute attr;
5937 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5938 return "";
5939
5940 const char* str = dwarf_formstring(&attr);
5941 return str ? str : "";
5942}
5943
5944/// Get the value of an attribute that is supposed to be a string, or
5945/// an empty string if the attribute could not be found.
5946///
5947/// @param die the DIE to get the attribute value from.
5948///
5949/// @param attr_name the attribute name. Must come from dwarf.h and
5950/// be an enumerator representing an attribute like, e.g, DW_AT_name.
5951///
5952/// @return the char* representing the value of the attribute, or an
5953/// empty string if no string attribute could be found.
5954static const char*
5955die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
5956{
5957 if (!die)
5958 return nullptr;
5959
5960 Dwarf_Attribute attr;
5961 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5962 return nullptr;
5963
5964 const char* str = dwarf_formstring(&attr);
5965 return str;
5966}
5967
5968/// Get the value of an attribute that is supposed to be an unsigned
5969/// constant.
5970///
5971/// @param die the DIE to read the information from.
5972///
5973/// @param attr_name the DW_AT_* name of the attribute. Must come
5974/// from dwarf.h and be an enumerator representing an attribute like,
5975/// e.g, DW_AT_decl_line.
5976///
5977///@param cst the output parameter that is set to the value of the
5978/// attribute @p attr_name. This parameter is set iff the function
5979/// return true.
5980///
5981/// @return true if there was an attribute of the name @p attr_name
5982/// and with a value that is a constant, false otherwise.
5983static bool
5984die_unsigned_constant_attribute(const Dwarf_Die* die,
5985 unsigned attr_name,
5986 uint64_t& cst)
5987{
5988 if (!die)
5989 return false;
5990
5991 Dwarf_Attribute attr;
5992 Dwarf_Word result = 0;
5993 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5994 || dwarf_formudata(&attr, &result))
5995 return false;
5996
5997 cst = result;
5998 return true;
5999}
6000
6001/// Read a signed constant value from a given attribute.
6002///
6003/// The signed constant expected must be of constant form.
6004///
6005/// @param die the DIE to get the attribute from.
6006///
6007/// @param attr_name the attribute name.
6008///
6009/// @param cst the resulting signed constant read.
6010///
6011/// @return true iff a signed constant attribute of the name @p
6012/// attr_name was found on the DIE @p die.
6013static bool
6014die_signed_constant_attribute(const Dwarf_Die *die,
6015 unsigned attr_name,
6016 int64_t& cst)
6017{
6018 if (!die)
6019 return false;
6020
6021 Dwarf_Attribute attr;
6022 Dwarf_Sword result = 0;
6023 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6024 || dwarf_formsdata(&attr, &result))
6025 return false;
6026
6027 cst = result;
6028 return true;
6029}
6030
6031/// Read the value of a constant attribute that is either signed or
6032/// unsigned into a array_type_def::subrange_type::bound_value value.
6033///
6034/// The bound_value instance will capture the actual signedness of the
6035/// read attribute.
6036///
6037/// @param die the DIE from which to read the value of the attribute.
6038///
6039/// @param attr_name the attribute name to consider.
6040///
6041/// @param is_signed true if the attribute value has to read as
6042/// signed.
6043///
6044/// @param value the resulting value read from attribute @p attr_name
6045/// on DIE @p die.
6046///
6047/// @return true iff DIE @p die has an attribute named @p attr_name
6048/// with a constant value.
6049static bool
6050die_constant_attribute(const Dwarf_Die *die,
6051 unsigned attr_name,
6052 bool is_signed,
6053 array_type_def::subrange_type::bound_value &value)
6054{
6055 if (!is_signed)
6056 {
6057 uint64_t l = 0;
6058 if (!die_unsigned_constant_attribute(die, attr_name, l))
6059 return false;
6060 value.set_unsigned(l);
6061 }
6062 else
6063 {
6064 int64_t l = 0;
6065 if (!die_signed_constant_attribute(die, attr_name, l))
6066 return false;
6067 value.set_signed(l);
6068 }
6069 return true;
6070}
6071
6072/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6073///
6074/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6075/// enum in dwarf.h so we have to use an unsigned int for the form,
6076/// grrr.
6077///
6078/// @param form the form to consider.
6079///
6080/// @return true iff @p form is DW_FORM_strx{1,4}.
6081static bool
6082form_is_DW_FORM_strx(unsigned form)
6083{
6084 if (form)
6085 {
6086#if defined HAVE_DW_FORM_strx1 \
6087 && defined HAVE_DW_FORM_strx2 \
6088 && defined HAVE_DW_FORM_strx3 \
6089 && defined HAVE_DW_FORM_strx4
6090 if (form == DW_FORM_strx1
6091 || form == DW_FORM_strx2
6092 || form == DW_FORM_strx3
6093 ||form == DW_FORM_strx4)
6094 return true;
6095#endif
6096 }
6097 return false;
6098}
6099
6100/// Test if a given DWARF form is DW_FORM_line_strp.
6101///
6102/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6103/// enum in dwarf.h so we have to use an unsigned int for the form,
6104/// grrr.
6105///
6106/// @param form the form to consider.
6107///
6108/// @return true iff @p form is DW_FORM_line_strp.
6109static bool
6110form_is_DW_FORM_line_strp(unsigned form)
6111{
6112 if (form)
6113 {
6114#if defined HAVE_DW_FORM_line_strp
6115 if (form == DW_FORM_line_strp)
6116 return true;
6117#endif
6118 }
6119 return false;
6120}
6121
6122/// Get the value of a DIE attribute; that value is meant to be a
6123/// flag.
6124///
6125/// @param die the DIE to get the attribute from.
6126///
6127/// @param attr_name the DW_AT_* name of the attribute. Must come
6128/// from dwarf.h and be an enumerator representing an attribute like,
6129/// e.g, DW_AT_external.
6130///
6131/// @param flag the output parameter to store the flag value into.
6132/// This is set iff the function returns true.
6133///
6134/// @param recursively if true, the function looks through the
6135/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6136/// all the way down to the initial DIE that is cloned and look on
6137/// that DIE to see if it has the @p attr_name attribute.
6138///
6139/// @return true if the DIE has a flag attribute named @p attr_name,
6140/// false otherwise.
6141static bool
6142die_flag_attribute(const Dwarf_Die* die,
6143 unsigned attr_name,
6144 bool& flag,
6145 bool recursively = true)
6146{
6147 Dwarf_Attribute attr;
6148 if (recursively
6149 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6150 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6151 return false;
6152
6153 bool f = false;
6154 if (dwarf_formflag(&attr, &f))
6155 return false;
6156
6157 flag = f;
6158 return true;
6159}
6160
6161/// Get the mangled name from a given DIE.
6162///
6163/// @param die the DIE to read the mangled name from.
6164///
6165/// @return the mangled name if it's present in the DIE, or just an
6166/// empty string if it's not.
6167static string
6168die_linkage_name(const Dwarf_Die* die)
6169{
6170 if (!die)
6171 return "";
6172
6173 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6174 if (linkage_name.empty())
6175 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6176 return linkage_name;
6177}
6178
6179/// Get the file path that is the value of the DW_AT_decl_file
6180/// attribute on a given DIE, if the DIE is a decl DIE having that
6181/// attribute.
6182///
6183/// @param die the DIE to consider.
6184///
6185/// @return a string containing the file path that is the logical
6186/// value of the DW_AT_decl_file attribute. If the DIE @p die
6187/// doesn't have a DW_AT_decl_file attribute, then the return value is
6188/// just an empty string.
6189static string
6190die_decl_file_attribute(const Dwarf_Die* die)
6191{
6192 if (!die)
6193 return "";
6194
6195 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6196
6197 return str ? str : "";
6198}
6199
6200/// Get the value of an attribute which value is supposed to be a
6201/// reference to a DIE.
6202///
6203/// @param die the DIE to read the value from.
6204///
6205/// @param attr_name the DW_AT_* attribute name to read.
6206///
6207/// @param result the DIE resulting from reading the attribute value.
6208/// This is set iff the function returns true.
6209///
6210/// @param recursively if true, the function looks through the
6211/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6212/// all the way down to the initial DIE that is cloned and look on
6213/// that DIE to see if it has the @p attr_name attribute.
6214///
6215/// @return true if the DIE @p die contains an attribute named @p
6216/// attr_name that is a DIE reference, false otherwise.
6217static bool
6218die_die_attribute(const Dwarf_Die* die,
6219 unsigned attr_name,
6220 Dwarf_Die& result,
6221 bool recursively)
6222{
6223 Dwarf_Attribute attr;
6224 if (recursively
6225 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6226 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6227 return false;
6228
6229 return dwarf_formref_die(&attr, &result);
6230}
6231
6232/// Get the DIE that is the "origin" of the current one.
6233///
6234/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6235/// attribute. Those DIEs represent a concrete instance of an
6236/// abstract entity. The concrete instance can be a concrete instance
6237/// of an inline function, or the concrete implementation of an
6238/// abstract interface. On both cases, we call the abstract instance
6239/// from which the concrete instance derives the "origin".
6240///
6241/// This function returns the ultimate origin DIE of a given DIE by
6242/// following the chain of its DW_AT_abstract_origin and
6243/// DW_AT_specification attributes.
6244///
6245/// @param die the DIE to consider.
6246///
6247/// @param origin_die this is an output parameter that is set by this
6248/// function to the resulting origin DIE iff the function returns
6249/// true.
6250///
6251/// @return true iff the function actually found an origin DIE and
6252/// set it to the @p origin_die parameter.
6253static bool
6254die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6255{
6256 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6257 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6258 {
6259 while (die_die_attribute(&origin_die,
6260 DW_AT_specification,
6261 origin_die, true)
6262 || die_die_attribute(&origin_die,
6263 DW_AT_abstract_origin,
6264 origin_die, true))
6265 {
6266 // Keep looking for the origin die ...
6267 ;
6268 }
6269 return true;
6270 }
6271 return false;
6272}
6273
6274/// Test if a subrange DIE indirectly references another subrange DIE
6275/// through a given attribute.
6276///
6277/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6278/// attribute be a reference to either a data member or a variable
6279/// which type is itself a DW_TAG_subrange_type. This latter subrange
6280/// DIE is said to be "indirectly referenced" by the former subrange
6281/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6282/// the value we want for the DW_AT_upper_bound of the former.
6283///
6284/// This function tests if the former subrange DIE does indirectly
6285/// reference another subrange DIE through a given attribute (not
6286/// necessarily DW_AT_upper_bound).
6287///
6288/// @param die the DIE to consider. Note that It must be a
6289/// DW_TAG_subrange_type.
6290///
6291/// @param attr_name the name of the attribute to look through for the
6292/// indirectly referenced subrange DIE.
6293///
6294/// @param referenced_subrange if the function returns true, then the
6295/// argument of this parameter is set to the indirectly referenced
6296/// DW_TAG_subrange_type DIE.
6297///
6298/// @return true iff @p DIE indirectly references a subrange DIE
6299/// through the attribute @p attr_name.
6300static bool
6301subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6302 unsigned attr_name,
6303 Dwarf_Die& referenced_subrange)
6304{
6305 bool result = false;
6306
6307 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6308 return result;
6309
6310 Dwarf_Die referenced_die;
6311 if (die_die_attribute(die, attr_name, referenced_die))
6312 {
6313 unsigned tag = dwarf_tag(&referenced_die);
6314 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6315 {
6316 Dwarf_Die type_die;
6317 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6318 {
6319 tag = dwarf_tag(&type_die);
6320 if (tag == DW_TAG_subrange_type)
6321 {
6322 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6323 result = true;
6324 }
6325 }
6326 }
6327 }
6328 return result;
6329}
6330
6331/// Return the bound value of subrange die by looking at an indirectly
6332/// referenced subrange DIE.
6333///
6334/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6335/// attribute be a reference to either a data member or a variable
6336/// which type is itself a DW_TAG_subrange_type. This latter subrange
6337/// DIE is said to be "indirectly referenced" by the former subrange
6338/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6339/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6340///
6341/// This function gets the DW_AT_{lower,upper}_bound value of a
6342/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6343/// the indirectly referenced subrange type, if it exists.
6344///
6345/// @param die the subrange DIE to consider.
6346///
6347/// @param attr_name the name of the attribute to consider, typically,
6348/// DW_AT_{lower,upper}_bound.
6349///
6350/// @param v the found value, iff this function returned true.
6351///
6352/// @param is_signed, this is set to true if @p v is signed. This
6353/// parameter is set at all only if the function returns true.
6354///
6355/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6356/// indirectly referenced subrange type.
6357static bool
6358subrange_die_indirect_bound_value(const Dwarf_Die *die,
6359 unsigned attr_name,
6360 array_type_def::subrange_type::bound_value& v,
6361 bool& is_signed)
6362{
6363 bool result = false;
6364
6365 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6366 return result;
6367
6368 Dwarf_Die subrange_die;
6369 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6370 subrange_die))
6371 {
6372 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6373 result = true;
6374 }
6375 return result;
6376}
6377
6378/// Read and return an addresss class attribute from a given DIE.
6379///
6380/// @param die the DIE to consider.
6381///
6382/// @param attr_name the name of the address class attribute to read
6383/// the value from.
6384///
6385/// @param the resulting address.
6386///
6387/// @return true iff the attribute could be read, was of the expected
6388/// address class and could thus be translated into the @p result.
6389static bool
6390die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6391{
6392 Dwarf_Attribute attr;
6393 if (!dwarf_attr_integrate(die, attr_name, &attr))
6394 return false;
6395 return dwarf_formaddr(&attr, &result) == 0;
6396}
6397
6398/// Returns the source location associated with a decl DIE.
6399///
6400/// @param rdr the @ref reader to use.
6401///
6402/// @param die the DIE the read the source location from.
6403///
6404/// @return the location associated with @p die.
6405static location
6406die_location(const reader& rdr, const Dwarf_Die* die)
6407{
6408 if (!die)
6409 return location();
6410
6411 string file = die_decl_file_attribute(die);
6412 uint64_t line = 0;
6413 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6414
6415 if (!file.empty() && line != 0)
6416 {
6417 translation_unit_sptr tu = rdr.cur_transl_unit();
6418 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6419 return l;
6420 }
6421 return location();
6422}
6423
6424/// Return a copy of the name of a DIE.
6425///
6426/// @param die the DIE to consider.
6427///
6428/// @return a copy of the name of the DIE.
6429static string
6430die_name(const Dwarf_Die* die)
6431{
6432 string name = die_string_attribute(die, DW_AT_name);
6433 return name;
6434}
6435
6436/// Return the location, the name and the mangled name of a given DIE.
6437///
6438/// @param rdr the DWARF reader to use.
6439///
6440/// @param die the DIE to read location and names from.
6441///
6442/// @param loc the location output parameter to set.
6443///
6444/// @param name the name output parameter to set.
6445///
6446/// @param linkage_name the linkage_name output parameter to set.
6447static void
6448die_loc_and_name(const reader& rdr,
6449 Dwarf_Die* die,
6450 location& loc,
6451 string& name,
6452 string& linkage_name)
6453{
6454 loc = die_location(rdr, die);
6455 name = die_name(die);
6456 linkage_name = die_linkage_name(die);
6457}
6458
6459/// Get the size of a (type) DIE as the value for the parameter
6460/// DW_AT_byte_size or DW_AT_bit_size.
6461///
6462/// @param die the DIE to read the information from.
6463///
6464/// @param size the resulting size in bits. This is set iff the
6465/// function return true.
6466///
6467/// @return true if the size attribute was found.
6468static bool
6469die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6470{
6471 if (!die)
6472 return false;
6473
6474 uint64_t byte_size = 0, bit_size = 0;
6475
6476 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6477 {
6478 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6479 return false;
6480 }
6481 else
6482 bit_size = byte_size * 8;
6483
6484 size = bit_size;
6485
6486 return true;
6487}
6488
6489/// Get the access specifier (from the DW_AT_accessibility attribute
6490/// value) of a given DIE.
6491///
6492/// @param die the DIE to consider.
6493///
6494/// @param access the resulting access. This is set iff the function
6495/// returns true.
6496///
6497/// @return bool if the DIE contains the DW_AT_accessibility die.
6498static bool
6499die_access_specifier(Dwarf_Die * die, access_specifier& access)
6500{
6501 if (!die)
6502 return false;
6503
6504 uint64_t a = 0;
6505 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6506 return false;
6507
6508 access_specifier result = private_access;
6509
6510 switch (a)
6511 {
6512 case private_access:
6513 result = private_access;
6514 break;
6515
6516 case protected_access:
6517 result = protected_access;
6518 break;
6519
6520 case public_access:
6521 result = public_access;
6522 break;
6523
6524 default:
6525 break;
6526 }
6527
6528 access = result;
6529 return true;
6530}
6531
6532/// Test whether a given DIE represents a decl that is public. That
6533/// is, one with the DW_AT_external attribute set.
6534///
6535/// @param die the DIE to consider for testing.
6536///
6537/// @return true if a DW_AT_external attribute is present and its
6538/// value is set to the true; return false otherwise.
6539static bool
6540die_is_public_decl(const Dwarf_Die* die)
6541{
6542 if (!die)
6543 return false;
6544 bool is_public = false;
6545
6546 // If this is a DW_TAG_subprogram DIE, look for the
6547 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6548 // then it's public. In all other cases, this should return false.
6549
6550 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6551 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6552 die_flag_attribute(die, DW_AT_external, is_public);
6553 else if (tag == DW_TAG_namespace)
6554 {
6555 string name = die_name(die);
6556 is_public = !name.empty();
6557 }
6558
6559 return is_public;
6560}
6561
6562/// Test if a DIE is effectively public.
6563///
6564/// This is meant to return true when either the DIE is public or when
6565/// it's a variable DIE that is at (global) namespace level.
6566///
6567/// @return true iff either the DIE is public or is a variable DIE
6568/// that is at (global) namespace level.
6569static bool
6570die_is_effectively_public_decl(const reader& rdr,
6571 const Dwarf_Die* die)
6572{
6573 if (die_is_public_decl(die))
6574 return true;
6575
6576 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6577 if (tag == DW_TAG_variable || tag == DW_TAG_member)
6578 {
6579 // The DIE is a variable.
6580 Dwarf_Die parent_die;
6581 size_t where_offset = 0;
6582 if (!get_parent_die(rdr, die, parent_die, where_offset))
6583 return false;
6584
6585 tag = dwarf_tag(&parent_die);
6586 if (tag == DW_TAG_compile_unit
6587 || tag == DW_TAG_partial_unit
6588 || tag == DW_TAG_type_unit)
6589 // The DIE is at global scope.
6590 return true;
6591
6592 if (tag == DW_TAG_namespace)
6593 {
6594 string name = die_name(&parent_die);
6595 if (name.empty())
6596 // The DIE at unnamed namespace scope, so it's not public.
6597 return false;
6598 // The DIE is at namespace scope.
6599 return true;
6600 }
6601 }
6602 return false;
6603}
6604
6605/// Test whether a given DIE represents a declaration-only DIE.
6606///
6607/// That is, if the DIE has the DW_AT_declaration flag set.
6608///
6609/// @param die the DIE to consider.
6610//
6611/// @return true if a DW_AT_declaration is present, false otherwise.
6612static bool
6613die_is_declaration_only(Dwarf_Die* die)
6614{
6615 bool is_declaration = false;
6616 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6617 if (is_declaration && !die_has_size_attribute(die))
6618 return true;
6619 return false;
6620}
6621
6622/// Test if a DIE is for a function decl.
6623///
6624/// @param die the DIE to consider.
6625///
6626/// @return true iff @p die represents a function decl.
6627static bool
6628die_is_function_decl(const Dwarf_Die *die)
6629{
6630 if (!die)
6631 return false;
6632
6633 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6634 if (tag == DW_TAG_subprogram)
6635 return true;
6636 return false;
6637}
6638
6639/// Test if a DIE is for a variable decl.
6640///
6641/// @param die the DIE to consider.
6642///
6643/// @return true iff @p die represents a variable decl.
6644static bool
6645die_is_variable_decl(const Dwarf_Die *die)
6646{
6647 if (!die)
6648 return false;
6649
6650 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6651 if (tag == DW_TAG_variable)
6652 return true;
6653 return false;
6654}
6655
6656/// Test if a DIE has size attribute.
6657///
6658/// @param die the DIE to consider.
6659///
6660/// @return true if the DIE has a size attribute.
6661static bool
6662die_has_size_attribute(const Dwarf_Die *die)
6663{
6664 uint64_t s;
6665 if (die_size_in_bits(die, s))
6666 return true;
6667 return false;
6668}
6669
6670/// Test that a DIE has no child DIE.
6671///
6672/// @param die the DIE to consider.
6673///
6674/// @return true iff @p die has no child DIE.
6675static bool
6676die_has_no_child(const Dwarf_Die *die)
6677{
6678 if (!die)
6679 return true;
6680
6681 Dwarf_Die child;
6682 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6683 return false;
6684 return true;
6685}
6686
6687/// Test whether a given DIE represents a declaration-only DIE.
6688///
6689/// That is, if the DIE has the DW_AT_declaration flag set.
6690///
6691/// @param die the DIE to consider.
6692//
6693/// @return true if a DW_AT_declaration is present, false otherwise.
6694static bool
6695die_is_declaration_only(const Dwarf_Die* die)
6696{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6697
6698/// Tests whether a given DIE is artificial.
6699///
6700/// @param die the test to test for.
6701///
6702/// @return true if the DIE is artificial, false otherwise.
6703static bool
6704die_is_artificial(Dwarf_Die* die)
6705{
6706 bool is_artificial;
6707 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6708}
6709
6710///@return true if a tag represents a type, false otherwise.
6711///
6712///@param tag the tag to consider.
6713static bool
6714is_type_tag(unsigned tag)
6715{
6716 bool result = false;
6717
6718 switch (tag)
6719 {
6720 case DW_TAG_array_type:
6721 case DW_TAG_class_type:
6722 case DW_TAG_enumeration_type:
6723 case DW_TAG_pointer_type:
6724 case DW_TAG_reference_type:
6725 case DW_TAG_string_type:
6726 case DW_TAG_structure_type:
6727 case DW_TAG_subroutine_type:
6728 case DW_TAG_typedef:
6729 case DW_TAG_union_type:
6730 case DW_TAG_ptr_to_member_type:
6731 case DW_TAG_set_type:
6732 case DW_TAG_subrange_type:
6733 case DW_TAG_base_type:
6734 case DW_TAG_const_type:
6735 case DW_TAG_file_type:
6736 case DW_TAG_packed_type:
6737 case DW_TAG_thrown_type:
6738 case DW_TAG_volatile_type:
6739 case DW_TAG_restrict_type:
6740 case DW_TAG_interface_type:
6741 case DW_TAG_unspecified_type:
6742 case DW_TAG_shared_type:
6743 case DW_TAG_rvalue_reference_type:
6744 case DW_TAG_coarray_type:
6745 case DW_TAG_atomic_type:
6746 case DW_TAG_immutable_type:
6747 result = true;
6748 break;
6749
6750 default:
6751 result = false;
6752 break;
6753 }
6754
6755 return result;
6756}
6757
6758/// Test if a given DIE is a type whose canonical type is to be
6759/// propagated during DIE canonicalization
6760///
6761/// This is a sub-routine of compare_dies.
6762///
6763/// @param tag the tag of the DIE to consider.
6764///
6765/// @return true iff the DIE of tag @p tag is can see its canonical
6766/// type be propagated during the type comparison that happens during
6767/// DIE canonicalization.
6768static bool
6769is_canon_type_to_be_propagated_tag(unsigned tag)
6770{
6771 bool result = false;
6772
6773 switch (tag)
6774 {
6775 case DW_TAG_class_type:
6776 case DW_TAG_structure_type:
6777 case DW_TAG_union_type:
6778 case DW_TAG_subroutine_type:
6779 case DW_TAG_subprogram:
6780 result = true;
6781 break;
6782
6783 default:
6784 result = false;
6785 break;
6786 }
6787
6788 return result;
6789}
6790
6791/// Test if a given kind of DIE ought to have its comparison result
6792/// cached by compare_dies, so that subsequent invocations of
6793/// compare_dies can be faster.
6794///
6795/// @param tag the tag of the DIE to consider.
6796///
6797/// @return true iff DIEs of the tag @p tag ought to have its
6798/// comparison results cached.
6799static bool
6800type_comparison_result_to_be_cached(unsigned tag)
6801{
6802 bool r = false;
6803 switch (tag)
6804 {
6805 case DW_TAG_class_type:
6806 case DW_TAG_structure_type:
6807 case DW_TAG_union_type:
6808 case DW_TAG_subroutine_type:
6809 case DW_TAG_subprogram:
6810 r = true;
6811 break;
6812
6813 default:
6814 r = false;
6815 break;
6816 }
6817 return r;
6818}
6819
6820/// Cache the result of comparing to type DIEs.
6821///
6822/// @param rdr the context to consider.
6823///
6824/// @param tag the tag of the DIEs to consider.
6825///
6826/// @param p the offsets of the pair of DIEs being compared.
6827///
6828/// @param result the comparison result to be cached.
6829static bool
6830maybe_cache_type_comparison_result(const reader& rdr,
6831 int tag,
6832 const offset_pair_type& p,
6833 comparison_result result)
6834{
6835 if (!type_comparison_result_to_be_cached(tag)
6836 || (result != COMPARISON_RESULT_EQUAL
6837 && result != COMPARISON_RESULT_DIFFERENT))
6838 return false;
6839
6840 rdr.die_comparison_results_[p] = result;
6841
6842 return true;
6843
6844}
6845
6846/// Get the cached result of the comparison of a pair of DIEs.
6847///
6848/// @param rdr the context to consider.
6849///
6850/// @param tag the tag of the pair of DIEs to consider.
6851///
6852/// @param p the offsets of the pair of DIEs to consider.
6853///
6854/// @param result out parameter set to the cached result of the
6855/// comparison of @p p if it has been found.
6856///
6857/// @return true iff a cached result for the comparisonof @p has been
6858/// found and set into @p result.
6859static bool
6860get_cached_type_comparison_result(const reader& rdr,
6861 const offset_pair_type& p,
6862 comparison_result& result)
6863{
6864 auto i = rdr.die_comparison_results_.find(p);
6865 if (i != rdr.die_comparison_results_.end())
6866 {
6867 result = i->second;
6868 return true;
6869 }
6870 return false;
6871}
6872
6873/// Get the cached result of the comparison of a pair of DIEs, if the
6874/// kind of DIEs ought to have its comparison results cached.
6875///
6876/// @param rdr the context to consider.
6877///
6878/// @param tag the tag of the pair of DIEs to consider.
6879///
6880/// @param p the offsets of the pair of DIEs to consider.
6881///
6882/// @param result out parameter set to the cached result of the
6883/// comparison of @p p if it has been found.
6884///
6885/// @return true iff a cached result for the comparisonof @p has been
6886/// found and set into @p result.
6887static bool
6888maybe_get_cached_type_comparison_result(const reader& rdr,
6889 int tag,
6890 const offset_pair_type& p,
6891 comparison_result& result)
6892{
6893 if (type_comparison_result_to_be_cached(tag))
6894 {
6895 // Types of this kind might have their comparison result cached
6896 // when they are not canonicalized. So let's see if we have a
6897 // cached comparison result.
6898 if (get_cached_type_comparison_result(rdr, p, result))
6899 return true;
6900 }
6901 return false;
6902}
6903
6904/// Test if a given DIE is to be canonicalized.
6905///
6906/// @param die the DIE to consider.
6907///
6908/// @return true iff @p die is to be canonicalized.
6909static bool
6910is_type_die_to_be_canonicalized(const Dwarf_Die *die)
6911{
6912 bool result = false;
6913 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6914
6915 if (!is_type_tag(tag))
6916 return false;
6917
6918 switch (tag)
6919 {
6920 case DW_TAG_class_type:
6921 case DW_TAG_structure_type:
6922 case DW_TAG_union_type:
6923 result = !die_is_declaration_only(die);
6924 break;
6925
6926 case DW_TAG_subroutine_type:
6927 case DW_TAG_subprogram:
6928 case DW_TAG_array_type:
6929 result = true;
6930
6931 default:
6932 break;
6933 }
6934
6935 return result;
6936}
6937
6938/// Test if a DIE tag represents a declaration.
6939///
6940/// @param tag the DWARF tag to consider.
6941///
6942/// @return true iff @p tag is for a declaration.
6943static bool
6944is_decl_tag(unsigned tag)
6945{
6946 switch (tag)
6947 {
6948 case DW_TAG_formal_parameter:
6949 case DW_TAG_imported_declaration:
6950 case DW_TAG_member:
6951 case DW_TAG_unspecified_parameters:
6952 case DW_TAG_subprogram:
6953 case DW_TAG_variable:
6954 case DW_TAG_namespace:
6955 case DW_TAG_GNU_template_template_param:
6956 case DW_TAG_GNU_template_parameter_pack:
6957 case DW_TAG_GNU_formal_parameter_pack:
6958 return true;
6959 }
6960 return false;
6961}
6962
6963/// Test if a DIE represents a type DIE.
6964///
6965/// @param die the DIE to consider.
6966///
6967/// @return true if @p die represents a type, false otherwise.
6968static bool
6969die_is_type(const Dwarf_Die* die)
6970{
6971 if (!die)
6972 return false;
6973 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6974}
6975
6976/// Test if a DIE represents a declaration.
6977///
6978/// @param die the DIE to consider.
6979///
6980/// @return true if @p die represents a decl, false otherwise.
6981static bool
6982die_is_decl(const Dwarf_Die* die)
6983{
6984 if (!die)
6985 return false;
6986 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6987}
6988
6989/// Test if a DIE represents a namespace.
6990///
6991/// @param die the DIE to consider.
6992///
6993/// @return true if @p die represents a namespace, false otherwise.
6994static bool
6995die_is_namespace(const Dwarf_Die* die)
6996{
6997 if (!die)
6998 return false;
6999 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
7000}
7001
7002/// Test if a DIE has tag DW_TAG_unspecified_type.
7003///
7004/// @param die the DIE to consider.
7005///
7006/// @return true if @p die has tag DW_TAG_unspecified_type.
7007static bool
7008die_is_unspecified(Dwarf_Die* die)
7009{
7010 if (!die)
7011 return false;
7012 return (dwarf_tag(die) == DW_TAG_unspecified_type);
7013}
7014
7015/// Test if a DIE represents a void type.
7016///
7017/// @param die the DIE to consider.
7018///
7019/// @return true if @p die represents a void type, false otherwise.
7020static bool
7021die_is_void_type(Dwarf_Die* die)
7022{
7023 if (!die || dwarf_tag(die) != DW_TAG_base_type)
7024 return false;
7025
7026 string name = die_name(die);
7027 if (name == "void")
7028 return true;
7029
7030 return false;
7031}
7032
7033/// Test if a DIE represents a pointer type.
7034///
7035/// @param die the die to consider.
7036///
7037/// @return true iff @p die represents a pointer type.
7038static bool
7039die_is_pointer_type(const Dwarf_Die* die)
7040{
7041 if (!die)
7042 return false;
7043
7044 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7045 if (tag == DW_TAG_pointer_type)
7046 return true;
7047
7048 return false;
7049}
7050
7051/// Test if a DIE is for a pointer, reference or qualified type to
7052/// anonymous class or struct.
7053///
7054/// @param die the DIE to consider.
7055///
7056/// @return true iff @p is for a pointer, reference or qualified type
7057/// to anonymous class or struct.
7058static bool
7059pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7060{
7061 if (!die_is_pointer_array_or_reference_type(die)
7062 && !die_is_qualified_type(die))
7063 return false;
7064
7065 Dwarf_Die underlying_type_die;
7066 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7067 return false;
7068
7069 if (!die_is_class_type(&underlying_type_die))
7070 return false;
7071
7072 string name = die_name(&underlying_type_die);
7073
7074 return name.empty();
7075}
7076
7077/// Test if a DIE represents a reference type.
7078///
7079/// @param die the die to consider.
7080///
7081/// @return true iff @p die represents a reference type.
7082static bool
7083die_is_reference_type(const Dwarf_Die* die)
7084{
7085 if (!die)
7086 return false;
7087
7088 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7089 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7090 return true;
7091
7092 return false;
7093}
7094
7095/// Test if a DIE represents an array type.
7096///
7097/// @param die the die to consider.
7098///
7099/// @return true iff @p die represents an array type.
7100static bool
7101die_is_array_type(const Dwarf_Die* die)
7102{
7103 if (!die)
7104 return false;
7105
7106 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7107 if (tag == DW_TAG_array_type)
7108 return true;
7109
7110 return false;
7111}
7112
7113/// Test if a DIE represents a pointer, reference or array type.
7114///
7115/// @param die the die to consider.
7116///
7117/// @return true iff @p die represents a pointer or reference type.
7118static bool
7119die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7120{return (die_is_pointer_type(die)
7121 || die_is_reference_type(die)
7122 || die_is_array_type(die));}
7123
7124/// Test if a DIE represents a pointer or a reference type.
7125///
7126/// @param die the die to consider.
7127///
7128/// @return true iff @p die represents a pointer or reference type.
7129static bool
7130die_is_pointer_or_reference_type(const Dwarf_Die* die)
7131{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7132
7133/// Test if a DIE represents a pointer, a reference or a typedef type.
7134///
7135/// @param die the die to consider.
7136///
7137/// @return true iff @p die represents a pointer, a reference or a
7138/// typedef type.
7139static bool
7140die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7141{return (die_is_pointer_array_or_reference_type(die)
7142 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7143
7144/// Test if a DIE represents a class type.
7145///
7146/// @param die the die to consider.
7147///
7148/// @return true iff @p die represents a class type.
7149static bool
7150die_is_class_type(const Dwarf_Die* die)
7151{
7152 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7153
7154 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7155 return true;
7156
7157 return false;
7158}
7159
7160/// Test if a DIE is for a qualified type.
7161///
7162/// @param die the DIE to consider.
7163///
7164/// @return true iff @p die is for a qualified type.
7165static bool
7166die_is_qualified_type(const Dwarf_Die* die)
7167{
7168 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7169 if (tag == DW_TAG_const_type
7170 || tag == DW_TAG_volatile_type
7171 || tag == DW_TAG_restrict_type)
7172 return true;
7173
7174 return false;
7175}
7176
7177/// Test if a DIE is for a function type.
7178///
7179/// @param die the DIE to consider.
7180///
7181/// @return true iff @p die is for a function type.
7182static bool
7183die_is_function_type(const Dwarf_Die *die)
7184{
7185 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7186 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7187 return true;
7188
7189 return false;
7190}
7191
7192/// Test if a DIE for a function pointer or member function has an
7193/// DW_AT_object_pointer attribute.
7194///
7195/// @param die the DIE to consider.
7196///
7197/// @param object_pointer out parameter. It's set to the DIE for the
7198/// object pointer iff the function returns true.
7199///
7200/// @return true iff the DIE @p die has an object pointer. In that
7201/// case, the parameter @p object_pointer is set to the DIE of that
7202/// object pointer.
7203static bool
7204die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7205{
7206 if (!die)
7207 return false;
7208
7209 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7210 return true;
7211
7212 return false;
7213}
7214
7215/// Test if a DIE has children DIEs.
7216///
7217/// @param die the DIE to consider.
7218///
7219/// @return true iff @p DIE has at least one child node.
7220static bool
7221die_has_children(const Dwarf_Die* die)
7222{
7223 if (!die)
7224 return false;
7225
7226 Dwarf_Die child;
7227 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7228 return true;
7229
7230 return false;
7231}
7232
7233/// When given the object pointer DIE of a function type or member
7234/// function DIE, this function returns the "this" pointer that points
7235/// to the associated class.
7236///
7237/// @param die the DIE of the object pointer of the function or member
7238/// function to consider.
7239///
7240/// @param this_pointer_die out parameter. This is set to the DIE of
7241/// the "this" pointer iff the function returns true.
7242///
7243/// @return true iff the function found the "this" pointer from the
7244/// object pointer DIE @p die. In that case, the parameter @p
7245/// this_pointer_die is set to the DIE of that "this" pointer.
7246static bool
7247die_this_pointer_from_object_pointer(Dwarf_Die* die,
7248 Dwarf_Die& this_pointer_die)
7249{
7250 ABG_ASSERT(die);
7251 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7252
7253 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7254 return true;
7255
7256 return false;
7257}
7258
7259/// Test if a given "this" pointer that points to a particular class
7260/// type is for a const class or not. If it's for a const class, then
7261/// it means the function type or the member function associated to
7262/// that "this" pointer is const.
7263///
7264/// @param die the DIE of the "this" pointer to consider.
7265///
7266/// @return true iff @p die points to a const class type.
7267static bool
7268die_this_pointer_is_const(Dwarf_Die* die)
7269{
7270 ABG_ASSERT(die);
7271
7272 if (dwarf_tag(die) == DW_TAG_pointer_type)
7273 {
7274 Dwarf_Die pointed_to_type_die;
7275 if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7276 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7277 return true;
7278 }
7279
7280 return false;
7281}
7282
7283/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7284/// attribute) points to a const implicit class and so is for a const
7285/// method or or a const member function type.
7286///
7287/// @param die the DIE of the object pointer to consider.
7288///
7289/// @return true iff the object pointer represented by @p die is for a
7290/// a const method or const member function type.
7291static bool
7292die_object_pointer_is_for_const_method(Dwarf_Die* die)
7293{
7294 ABG_ASSERT(die);
7295 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7296
7297 Dwarf_Die this_pointer_die;
7298 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7299 if (die_this_pointer_is_const(&this_pointer_die))
7300 return true;
7301
7302 return false;
7303}
7304
7305/// Test if a DIE represents an entity that is at class scope.
7306///
7307/// @param rdr the DWARF reader to use.
7308///
7309/// @param die the DIE to consider.
7310///
7311/// @param where_offset where we are logically at in the DIE stream.
7312///
7313/// @param class_scope_die out parameter. Set to the DIE of the
7314/// containing class iff @p die happens to be at class scope; that is,
7315/// iff the function returns true.
7316///
7317/// @return true iff @p die is at class scope. In that case, @p
7318/// class_scope_die is set to the DIE of the class that contains @p
7319/// die.
7320static bool
7321die_is_at_class_scope(const reader& rdr,
7322 const Dwarf_Die* die,
7323 size_t where_offset,
7324 Dwarf_Die& class_scope_die)
7325{
7326 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7327 return false;
7328
7329 int tag = dwarf_tag(&class_scope_die);
7330
7331 return (tag == DW_TAG_structure_type
7332 || tag == DW_TAG_class_type
7333 || tag == DW_TAG_union_type);
7334}
7335
7336/// Return the leaf object under a pointer, reference or qualified
7337/// type DIE.
7338///
7339/// @param die the DIE of the type to consider.
7340///
7341/// @param peeled_die out parameter. Set to the DIE of the leaf
7342/// object iff the function actually peeled anything.
7343///
7344/// @return true upon successful completion.
7345static bool
7346die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7347{
7348 if (!die)
7349 return false;
7350
7351 int tag = dwarf_tag(die);
7352
7353 if (tag == DW_TAG_const_type
7354 || tag == DW_TAG_volatile_type
7355 || tag == DW_TAG_restrict_type
7356 || tag == DW_TAG_pointer_type
7357 || tag == DW_TAG_reference_type
7358 || tag == DW_TAG_rvalue_reference_type)
7359 {
7360 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7361 return false;
7362 }
7363 else
7364 return false;
7365
7366 memcpy(&peeled_die, die, sizeof(peeled_die));
7367
7368 while (tag == DW_TAG_const_type
7369 || tag == DW_TAG_volatile_type
7370 || tag == DW_TAG_restrict_type
7371 || tag == DW_TAG_pointer_type
7372 || tag == DW_TAG_reference_type
7373 || tag == DW_TAG_rvalue_reference_type)
7374 {
7375 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7376 break;
7377 tag = dwarf_tag(&peeled_die);
7378 }
7379
7380 return true;
7381}
7382
7383/// Return the leaf object under a qualified type DIE.
7384///
7385/// @param die the DIE of the type to consider.
7386///
7387/// @param peeled_die out parameter. Set to the DIE of the leaf
7388/// object iff the function actually peeled anything.
7389///
7390/// @return true upon successful completion.
7391static bool
7392die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7393{
7394 if (!die)
7395 return false;
7396
7397 memcpy(&peeled_die, die, sizeof(peeled_die));
7398
7399 int tag = dwarf_tag(&peeled_die);
7400
7401 bool result = false;
7402 while (tag == DW_TAG_const_type
7403 || tag == DW_TAG_volatile_type
7404 || tag == DW_TAG_restrict_type)
7405 {
7406 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7407 break;
7408 tag = dwarf_tag(&peeled_die);
7409 result = true;
7410 }
7411
7412 return result;
7413}
7414
7415/// Return the leaf object under a typedef type DIE.
7416///
7417/// @param die the DIE of the type to consider.
7418///
7419/// @param peeled_die out parameter. Set to the DIE of the leaf
7420/// object iff the function actually peeled anything.
7421///
7422/// @return true upon successful completion.
7423static bool
7424die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7425{
7426 if (!die)
7427 return false;
7428
7429 int tag = dwarf_tag(die);
7430
7431 memcpy(&peeled_die, die, sizeof(peeled_die));
7432
7433 if (tag == DW_TAG_typedef)
7434 {
7435 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7436 return false;
7437 }
7438 else
7439 return false;
7440
7441 while (tag == DW_TAG_typedef)
7442 {
7443 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7444 break;
7445 tag = dwarf_tag(&peeled_die);
7446 }
7447
7448 return true;
7449
7450}
7451
7452/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7453///
7454/// @param die the DIE to consider.
7455///
7456/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7457/// iff the function returned true.
7458///
7459/// @return true iff the function could peel @p die.
7460static bool
7461die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7462{
7463 if (!die)
7464 return false;
7465
7466 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7467
7468 if (tag == DW_TAG_pointer_type
7469 || tag == DW_TAG_reference_type
7470 || tag == DW_TAG_rvalue_reference_type
7471 || tag == DW_TAG_typedef)
7472 {
7473 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7474 return false;
7475 }
7476 else
7477 return false;
7478
7479 while (tag == DW_TAG_pointer_type
7480 || tag == DW_TAG_reference_type
7481 || tag == DW_TAG_rvalue_reference_type
7482 || tag == DW_TAG_typedef)
7483 {
7484 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7485 break;
7486 tag = dwarf_tag(&peeled_die);
7487 }
7488 return true;
7489}
7490
7491/// Test if a DIE for a function type represents a method type.
7492///
7493/// @param rdr the DWARF reader.
7494///
7495/// @param die the DIE to consider.
7496///
7497/// @param where_offset where we logically are in the stream of DIEs.
7498///
7499/// @param object_pointer_die out parameter. This is set by the
7500/// function to the DIE that refers to the formal function parameter
7501/// which holds the implicit "this" pointer of the method. That die
7502/// is called the object pointer DIE. This is set iff the function
7503///
7504/// @param class_die out parameter. This is set by the function to
7505/// the DIE that represents the class of the method type. This is set
7506/// iff the function returns true.
7507///
7508/// @param is_static out parameter. This is set to true by the
7509/// function if @p die is a static method. This is set iff the
7510/// function returns true.
7511///
7512/// @return true iff @p die is a DIE for a method type.
7513static bool
7514die_function_type_is_method_type(const reader& rdr,
7515 const Dwarf_Die *die,
7516 size_t where_offset,
7517 Dwarf_Die& object_pointer_die,
7518 Dwarf_Die& class_die,
7519 bool& is_static)
7520{
7521 if (!die)
7522 return false;
7523
7524 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7525 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7526
7527 bool has_object_pointer = false;
7528 is_static = false;
7529 if (tag == DW_TAG_subprogram)
7530 {
7531 Dwarf_Die spec_or_origin_die;
7532 if (die_die_attribute(die, DW_AT_specification,
7533 spec_or_origin_die)
7534 || die_die_attribute(die, DW_AT_abstract_origin,
7535 spec_or_origin_die))
7536 {
7537 if (die_has_object_pointer(&spec_or_origin_die,
7538 object_pointer_die))
7539 has_object_pointer = true;
7540 else
7541 {
7542 if (die_is_at_class_scope(rdr, &spec_or_origin_die,
7543 where_offset, class_die))
7544 is_static = true;
7545 else
7546 return false;
7547 }
7548 }
7549 else
7550 {
7551 if (die_has_object_pointer(die, object_pointer_die))
7552 has_object_pointer = true;
7553 else
7554 {
7555 if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7556 is_static = true;
7557 else
7558 return false;
7559 }
7560 }
7561 }
7562 else
7563 {
7564 if (die_has_object_pointer(die, object_pointer_die))
7565 has_object_pointer = true;
7566 else
7567 return false;
7568 }
7569
7570 if (!is_static)
7571 {
7572 ABG_ASSERT(has_object_pointer);
7573 // The object pointer die points to a DW_TAG_formal_parameter which
7574 // is the "this" parameter. The type of the "this" parameter is a
7575 // pointer. Let's get that pointer type.
7576 Dwarf_Die this_type_die;
7577 if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7578 return false;
7579
7580 // So the class type is the type pointed to by the type of the "this"
7581 // parameter.
7582 if (!die_peel_qual_ptr(&this_type_die, class_die))
7583 return false;
7584
7585 // And make we return a class type, rather than a typedef to a
7586 // class.
7587 die_peel_typedef(&class_die, class_die);
7588 }
7589
7590 return true;
7591}
7592
7593enum virtuality
7594{
7595 VIRTUALITY_NOT_VIRTUAL,
7596 VIRTUALITY_VIRTUAL,
7597 VIRTUALITY_PURE_VIRTUAL
7598};
7599
7600/// Get the virtual-ness of a given DIE, that is, the value of the
7601/// DW_AT_virtuality attribute.
7602///
7603/// @param die the DIE to read from.
7604///
7605/// @param virt the resulting virtuality attribute. This is set iff
7606/// the function returns true.
7607///
7608/// @return true if the virtual-ness could be determined.
7609static bool
7610die_virtuality(const Dwarf_Die* die, virtuality& virt)
7611{
7612 if (!die)
7613 return false;
7614
7615 uint64_t v = 0;
7616 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7617
7618 if (v == DW_VIRTUALITY_virtual)
7619 virt = VIRTUALITY_VIRTUAL;
7620 else if (v == DW_VIRTUALITY_pure_virtual)
7621 virt = VIRTUALITY_PURE_VIRTUAL;
7622 else
7623 virt = VIRTUALITY_NOT_VIRTUAL;
7624
7625 return true;
7626}
7627
7628/// Test whether the DIE represent either a virtual base or function.
7629///
7630/// @param die the DIE to consider.
7631///
7632/// @return bool if the DIE represents a virtual base or function,
7633/// false othersise.
7634static bool
7635die_is_virtual(const Dwarf_Die* die)
7636{
7637 virtuality v;
7638 if (!die_virtuality(die, v))
7639 return false;
7640
7641 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7642}
7643
7644/// Test if the DIE represents an entity that was declared inlined.
7645///
7646/// @param die the DIE to test for.
7647///
7648/// @return true if the DIE represents an entity that was declared
7649/// inlined.
7650static bool
7651die_is_declared_inline(Dwarf_Die* die)
7652{
7653 uint64_t inline_value = 0;
7654 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7655 return false;
7656 return inline_value == DW_INL_declared_inlined;
7657}
7658
7659/// Compare two DWARF strings using the most accurate (and slowest)
7660/// method possible.
7661///
7662/// @param l the DIE that carries the first string to consider, as an
7663/// attribute value.
7664///
7665/// @param attr_name the name of the attribute which value is the
7666/// string to compare.
7667///
7668/// @return true iff the string carried by @p l equals the one carried
7669/// by @p r.
7670static bool
7671slowly_compare_strings(const Dwarf_Die *l,
7672 const Dwarf_Die *r,
7673 unsigned attr_name)
7674{
7675 const char *l_str = die_char_str_attribute(l, attr_name),
7676 *r_str = die_char_str_attribute(r, attr_name);
7677 if (!l_str && !r_str)
7678 return true;
7679 return l_str && r_str && !strcmp(l_str, r_str);
7680}
7681
7682/// This function is a fast routine (optimization) to compare the
7683/// values of two string attributes of two DIEs.
7684///
7685/// @param l the first DIE to consider.
7686///
7687/// @param r the second DIE to consider.
7688///
7689/// @param attr_name the name of the attribute to compare, on the two
7690/// DIEs above.
7691///
7692/// @param result out parameter. This is set to the result of the
7693/// comparison. If the value of attribute @p attr_name on DIE @p l
7694/// equals the value of attribute @p attr_name on DIE @p r, then the
7695/// the argument of this parameter is set to true. Otherwise, it's
7696/// set to false. Note that the argument of this parameter is set iff
7697/// the function returned true.
7698///
7699/// @return true iff the comparison could be performed. There are
7700/// cases in which the comparison cannot be performed. For instance,
7701/// if one of the DIEs does not have the attribute @p attr_name. In
7702/// any case, if this function returns true, then the parameter @p
7703/// result is set to the result of the comparison.
7704static bool
7705compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7706 unsigned attr_name,
7707 bool &result)
7708{
7709 Dwarf_Attribute l_attr, r_attr;
7710 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7711 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7712 return false;
7713
7714 ABG_ASSERT(l_attr.form == DW_FORM_strp
7715 || l_attr.form == DW_FORM_string
7716 || l_attr.form == DW_FORM_GNU_strp_alt
7717 || form_is_DW_FORM_strx(l_attr.form)
7718 || form_is_DW_FORM_line_strp(l_attr.form));
7719
7720 ABG_ASSERT(r_attr.form == DW_FORM_strp
7721 || r_attr.form == DW_FORM_string
7722 || r_attr.form == DW_FORM_GNU_strp_alt
7723 || form_is_DW_FORM_strx(r_attr.form)
7724 || form_is_DW_FORM_line_strp(r_attr.form));
7725
7726 if ((l_attr.form == DW_FORM_strp
7727 && r_attr.form == DW_FORM_strp)
7728 || (l_attr.form == DW_FORM_GNU_strp_alt
7729 && r_attr.form == DW_FORM_GNU_strp_alt)
7730 || (form_is_DW_FORM_strx(l_attr.form)
7731 && form_is_DW_FORM_strx(r_attr.form))
7732 || (form_is_DW_FORM_line_strp(l_attr.form)
7733 && form_is_DW_FORM_line_strp(r_attr.form)))
7734 {
7735 // So these string attributes are actually pointers into a
7736 // string table. The string table is most likely de-duplicated
7737 // so comparing the *values* of the pointers should be enough.
7738 //
7739 // This is the fast path.
7740 if (l_attr.valp == r_attr.valp)
7741 {
7742#if WITH_DEBUG_TYPE_CANONICALIZATION
7743 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
7744#endif
7745 result = true;
7746 return true;
7747 }
7748 }
7749
7750 // If we reached this point it means we couldn't use the fast path
7751 // because the string atttributes are strings that are "inline" in
7752 // the debug info section. Let's just compare them the slow and
7753 // obvious way.
7754 result = slowly_compare_strings(l, r, attr_name);
7755 return true;
7756}
7757
7758/// Compare the file path of the compilation units (aka CUs)
7759/// associated to two DIEs.
7760///
7761/// If the DIEs are for pointers or typedefs, this function also
7762/// compares the file paths of the CUs of the leaf DIEs (underlying
7763/// DIEs of the pointer or the typedef).
7764///
7765/// @param l the first type DIE to consider.
7766///
7767/// @param r the second type DIE to consider.
7768///
7769/// @return true iff the file paths of the DIEs of the two types are
7770/// equal.
7771static bool
7772compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7773{
7774 Dwarf_Die l_cu, r_cu;
7775 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7776 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7777 return false;
7778
7779 bool compared =
7780 compare_dies_string_attribute_value(&l_cu, &r_cu,
7781 DW_AT_name,
7782 result);
7783 if (compared && result)
7784 {
7785 Dwarf_Die peeled_l, peeled_r;
7786 if (die_is_pointer_reference_or_typedef_type(l)
7787 && die_is_pointer_reference_or_typedef_type(r)
7788 && die_peel_pointer_and_typedef(l, peeled_l)
7789 && die_peel_pointer_and_typedef(r, peeled_r))
7790 {
7791 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7792 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7793 return false;
7794 compared =
7795 compare_dies_string_attribute_value(&l_cu, &r_cu,
7796 DW_AT_name,
7797 result);
7798 }
7799 }
7800
7801 return compared;
7802}
7803
7804// -----------------------------------
7805// <location expression evaluation>
7806// -----------------------------------
7807
7808/// Get the value of a given DIE attribute, knowing that it must be a
7809/// location expression.
7810///
7811/// @param die the DIE to read the attribute from.
7812///
7813/// @param attr_name the name of the attribute to read the value for.
7814///
7815/// @param expr the pointer to allocate and fill with the resulting
7816/// array of operators + operands forming a dwarf expression. This is
7817/// set iff the function returns true.
7818///
7819/// @param expr_len the length of the resulting dwarf expression.
7820/// This is set iff the function returns true.
7821///
7822/// @return true if the attribute exists and has a non-empty dwarf expression
7823/// as value. In that case the expr and expr_len arguments are set to the
7824/// resulting dwarf expression.
7825static bool
7826die_location_expr(const Dwarf_Die* die,
7827 unsigned attr_name,
7828 Dwarf_Op** expr,
7829 size_t* expr_len)
7830{
7831 if (!die)
7832 return false;
7833
7834 Dwarf_Attribute attr;
7835 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7836 return false;
7837
7838 size_t len = 0;
7839 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7840
7841 // Ignore location expressions where reading them succeeded but
7842 // their length is 0.
7843 result &= len > 0;
7844
7845 if (result)
7846 *expr_len = len;
7847
7848 return result;
7849}
7850
7851/// If the current operation in the dwarf expression represents a push
7852/// of a constant value onto the dwarf expr virtual machine (aka
7853/// DEVM), perform the operation and update the DEVM.
7854///
7855/// If the result of the operation is a constant, update the DEVM
7856/// accumulator with its value. Otherwise, the DEVM accumulator is
7857/// left with its previous value.
7858///
7859/// @param ops the array of the dwarf expression operations to consider.
7860///
7861/// @param ops_len the lengths of @p ops array above.
7862///
7863/// @param index the index of the operation to interpret, in @p ops.
7864///
7865/// @param next_index the index of the operation to interpret at the
7866/// next step, after this function completed and returned. This is
7867/// set an output parameter that is set iff the function returns true.
7868///
7869/// @param ctxt the DEVM evaluation context.
7870///
7871/// @return true if the current operation actually pushes a constant
7872/// value onto the DEVM stack, false otherwise.
7873static bool
7874op_pushes_constant_value(Dwarf_Op* ops,
7875 size_t ops_len,
7876 size_t index,
7877 size_t& next_index,
7878 dwarf_expr_eval_context& ctxt)
7879{
7880 ABG_ASSERT(index < ops_len);
7881
7882 Dwarf_Op& op = ops[index];
7883 int64_t value = 0;
7884
7885 switch (op.atom)
7886 {
7887 case DW_OP_addr:
7888 value = ops[index].number;
7889 break;
7890
7891 case DW_OP_const1u:
7892 case DW_OP_const1s:
7893 case DW_OP_const2u:
7894 case DW_OP_const2s:
7895 case DW_OP_const4u:
7896 case DW_OP_const4s:
7897 case DW_OP_const8u:
7898 case DW_OP_const8s:
7899 case DW_OP_constu:
7900 case DW_OP_consts:
7901 value = ops[index].number;
7902 break;
7903
7904 case DW_OP_lit0:
7905 value = 0;
7906 break;
7907 case DW_OP_lit1:
7908 value = 1;
7909 break;
7910 case DW_OP_lit2:
7911 value = 2;
7912 break;
7913 case DW_OP_lit3:
7914 value = 3;
7915 break;
7916 case DW_OP_lit4:
7917 value = 4;
7918 break;
7919 case DW_OP_lit5:
7920 value = 5;
7921 break;
7922 case DW_OP_lit6:
7923 value = 6;
7924 break;
7925 case DW_OP_lit7:
7926 value = 7;
7927 break;
7928 case DW_OP_lit8:
7929 value = 8;
7930 break;
7931 case DW_OP_lit9:
7932 value = 9;
7933 break;
7934 case DW_OP_lit10:
7935 value = 10;
7936 break;
7937 case DW_OP_lit11:
7938 value = 11;
7939 break;
7940 case DW_OP_lit12:
7941 value = 12;
7942 break;
7943 case DW_OP_lit13:
7944 value = 13;
7945 break;
7946 case DW_OP_lit14:
7947 value = 14;
7948 break;
7949 case DW_OP_lit15:
7950 value = 15;
7951 break;
7952 case DW_OP_lit16:
7953 value = 16;
7954 break;
7955 case DW_OP_lit17:
7956 value = 17;
7957 break;
7958 case DW_OP_lit18:
7959 value = 18;
7960 break;
7961 case DW_OP_lit19:
7962 value = 19;
7963 break;
7964 case DW_OP_lit20:
7965 value = 20;
7966 break;
7967 case DW_OP_lit21:
7968 value = 21;
7969 break;
7970 case DW_OP_lit22:
7971 value = 22;
7972 break;
7973 case DW_OP_lit23:
7974 value = 23;
7975 break;
7976 case DW_OP_lit24:
7977 value = 24;
7978 break;
7979 case DW_OP_lit25:
7980 value = 25;
7981 break;
7982 case DW_OP_lit26:
7983 value = 26;
7984 break;
7985 case DW_OP_lit27:
7986 value = 27;
7987 break;
7988 case DW_OP_lit28:
7989 value = 28;
7990 break;
7991 case DW_OP_lit29:
7992 value = 29;
7993 break;
7994 case DW_OP_lit30:
7995 value = 30;
7996 break;
7997 case DW_OP_lit31:
7998 value = 31;
7999 break;
8000
8001 default:
8002 return false;
8003 }
8004
8005 expr_result r(value);
8006 ctxt.push(r);
8007 ctxt.accum = r;
8008 next_index = index + 1;
8009
8010 return true;
8011}
8012
8013/// If the current operation in the dwarf expression represents a push
8014/// of a non-constant value onto the dwarf expr virtual machine (aka
8015/// DEVM), perform the operation and update the DEVM. A non-constant
8016/// is namely a quantity for which we need inferior (a running program
8017/// image) state to know the exact value.
8018///
8019/// Upon successful completion, as the result of the operation is a
8020/// non-constant the DEVM accumulator value is left to its state as of
8021/// before the invocation of this function.
8022///
8023/// @param ops the array of the dwarf expression operations to consider.
8024///
8025/// @param ops_len the lengths of @p ops array above.
8026///
8027/// @param index the index of the operation to interpret, in @p ops.
8028///
8029/// @param next_index the index of the operation to interpret at the
8030/// next step, after this function completed and returned. This is
8031/// set an output parameter that is set iff the function returns true.
8032///
8033/// @param ctxt the DEVM evaluation context.
8034///
8035/// @return true if the current operation actually pushes a
8036/// non-constant value onto the DEVM stack, false otherwise.
8037static bool
8038op_pushes_non_constant_value(Dwarf_Op* ops,
8039 size_t ops_len,
8040 size_t index,
8041 size_t& next_index,
8042 dwarf_expr_eval_context& ctxt)
8043{
8044 ABG_ASSERT(index < ops_len);
8045 Dwarf_Op& op = ops[index];
8046
8047 switch (op.atom)
8048 {
8049 case DW_OP_reg0:
8050 case DW_OP_reg1:
8051 case DW_OP_reg2:
8052 case DW_OP_reg3:
8053 case DW_OP_reg4:
8054 case DW_OP_reg5:
8055 case DW_OP_reg6:
8056 case DW_OP_reg7:
8057 case DW_OP_reg8:
8058 case DW_OP_reg9:
8059 case DW_OP_reg10:
8060 case DW_OP_reg11:
8061 case DW_OP_reg12:
8062 case DW_OP_reg13:
8063 case DW_OP_reg14:
8064 case DW_OP_reg15:
8065 case DW_OP_reg16:
8066 case DW_OP_reg17:
8067 case DW_OP_reg18:
8068 case DW_OP_reg19:
8069 case DW_OP_reg20:
8070 case DW_OP_reg21:
8071 case DW_OP_reg22:
8072 case DW_OP_reg23:
8073 case DW_OP_reg24:
8074 case DW_OP_reg25:
8075 case DW_OP_reg26:
8076 case DW_OP_reg27:
8077 case DW_OP_reg28:
8078 case DW_OP_reg29:
8079 case DW_OP_reg30:
8080 case DW_OP_reg31:
8081 next_index = index + 1;
8082 break;
8083
8084 case DW_OP_breg0:
8085 case DW_OP_breg1:
8086 case DW_OP_breg2:
8087 case DW_OP_breg3:
8088 case DW_OP_breg4:
8089 case DW_OP_breg5:
8090 case DW_OP_breg6:
8091 case DW_OP_breg7:
8092 case DW_OP_breg8:
8093 case DW_OP_breg9:
8094 case DW_OP_breg10:
8095 case DW_OP_breg11:
8096 case DW_OP_breg12:
8097 case DW_OP_breg13:
8098 case DW_OP_breg14:
8099 case DW_OP_breg15:
8100 case DW_OP_breg16:
8101 case DW_OP_breg17:
8102 case DW_OP_breg18:
8103 case DW_OP_breg19:
8104 case DW_OP_breg20:
8105 case DW_OP_breg21:
8106 case DW_OP_breg22:
8107 case DW_OP_breg23:
8108 case DW_OP_breg24:
8109 case DW_OP_breg25:
8110 case DW_OP_breg26:
8111 case DW_OP_breg27:
8112 case DW_OP_breg28:
8113 case DW_OP_breg29:
8114 case DW_OP_breg30:
8115 case DW_OP_breg31:
8116 next_index = index + 1;
8117 break;
8118
8119 case DW_OP_regx:
8120 next_index = index + 2;
8121 break;
8122
8123 case DW_OP_fbreg:
8124 next_index = index + 1;
8125 break;
8126
8127 case DW_OP_bregx:
8128 next_index = index + 1;
8129 break;
8130
8131 case DW_OP_GNU_variable_value:
8132 next_index = index + 1;
8133 break;
8134
8135 default:
8136 return false;
8137 }
8138
8139 expr_result r(false);
8140 ctxt.push(r);
8141
8142 return true;
8143}
8144
8145/// If the current operation in the dwarf expression represents a
8146/// manipulation of the stack of the DWARF Expression Virtual Machine
8147/// (aka DEVM), this function performs the operation and updates the
8148/// state of the DEVM. If the result of the operation represents a
8149/// constant value, then the accumulator of the DEVM is set to that
8150/// result's value, Otherwise, the DEVM accumulator is left with its
8151/// previous value.
8152///
8153/// @param expr the array of the dwarf expression operations to consider.
8154///
8155/// @param expr_len the lengths of @p ops array above.
8156///
8157/// @param index the index of the operation to interpret, in @p ops.
8158///
8159/// @param next_index the index of the operation to interpret at the
8160/// next step, after this function completed and returned. This is
8161/// set an output parameter that is set iff the function returns true.
8162///
8163/// @param ctxt the DEVM evaluation context.
8164///
8165/// @return true if the current operation actually manipulates the
8166/// DEVM stack, false otherwise.
8167static bool
8168op_manipulates_stack(Dwarf_Op* expr,
8169 size_t expr_len,
8170 size_t index,
8171 size_t& next_index,
8172 dwarf_expr_eval_context& ctxt)
8173{
8174 Dwarf_Op& op = expr[index];
8175 expr_result v;
8176
8177 switch (op.atom)
8178 {
8179 case DW_OP_dup:
8180 v = ctxt.stack.front();
8181 ctxt.push(v);
8182 break;
8183
8184 case DW_OP_drop:
8185 v = ctxt.stack.front();
8186 ctxt.pop();
8187 break;
8188
8189 case DW_OP_over:
8190 ABG_ASSERT(ctxt.stack.size() > 1);
8191 v = ctxt.stack[1];
8192 ctxt.push(v);
8193 break;
8194
8195 case DW_OP_pick:
8196 ABG_ASSERT(index + 1 < expr_len);
8197 v = op.number;
8198 ctxt.push(v);
8199 break;
8200
8201 case DW_OP_swap:
8202 ABG_ASSERT(ctxt.stack.size() > 1);
8203 v = ctxt.stack[1];
8204 ctxt.stack.erase(ctxt.stack.begin() + 1);
8205 ctxt.push(v);
8206 break;
8207
8208 case DW_OP_rot:
8209 ABG_ASSERT(ctxt.stack.size() > 2);
8210 v = ctxt.stack[2];
8211 ctxt.stack.erase(ctxt.stack.begin() + 2);
8212 ctxt.push(v);
8213 break;
8214
8215 case DW_OP_deref:
8216 case DW_OP_deref_size:
8217 ABG_ASSERT(ctxt.stack.size() > 0);
8218 ctxt.pop();
8219 v.is_const(false);
8220 ctxt.push(v);
8221 break;
8222
8223 case DW_OP_xderef:
8224 case DW_OP_xderef_size:
8225 ABG_ASSERT(ctxt.stack.size() > 1);
8226 ctxt.pop();
8227 ctxt.pop();
8228 v.is_const(false);
8229 ctxt.push(v);
8230 break;
8231
8232 case DW_OP_push_object_address:
8233 v.is_const(false);
8234 ctxt.push(v);
8235 break;
8236
8237 case DW_OP_form_tls_address:
8238 case DW_OP_GNU_push_tls_address:
8239 ABG_ASSERT(ctxt.stack.size() > 0);
8240 v = ctxt.pop();
8241 if (op.atom == DW_OP_form_tls_address)
8242 v.is_const(false);
8243 ctxt.push(v);
8244 break;
8245
8246 case DW_OP_call_frame_cfa:
8247 v.is_const(false);
8248 ctxt.push(v);
8249 break;
8250
8251 default:
8252 return false;
8253 }
8254
8255 if (v.is_const())
8256 ctxt.accum = v;
8257
8258 if (op.atom == DW_OP_form_tls_address
8259 || op.atom == DW_OP_GNU_push_tls_address)
8260 ctxt.set_tls_address(true);
8261 else
8262 ctxt.set_tls_address(false);
8263
8264 next_index = index + 1;
8265
8266 return true;
8267}
8268
8269/// If the current operation in the dwarf expression represents a push
8270/// of an arithmetic or logic operation onto the dwarf expr virtual
8271/// machine (aka DEVM), perform the operation and update the DEVM.
8272///
8273/// If the result of the operation is a constant, update the DEVM
8274/// accumulator with its value. Otherwise, the DEVM accumulator is
8275/// left with its previous value.
8276///
8277/// @param expr the array of the dwarf expression operations to consider.
8278///
8279/// @param expr_len the lengths of @p expr array above.
8280///
8281/// @param index the index of the operation to interpret, in @p expr.
8282///
8283/// @param next_index the index of the operation to interpret at the
8284/// next step, after this function completed and returned. This is
8285/// set an output parameter that is set iff the function returns true.
8286///
8287/// @param ctxt the DEVM evaluation context.
8288///
8289/// @return true if the current operation actually represent an
8290/// arithmetic or logic operation.
8291static bool
8292op_is_arith_logic(Dwarf_Op* expr,
8293 size_t expr_len,
8294 size_t index,
8295 size_t& next_index,
8296 dwarf_expr_eval_context& ctxt)
8297{
8298 ABG_ASSERT(index < expr_len);
8299
8300 Dwarf_Op& op = expr[index];
8301 expr_result val1, val2;
8302 bool result = false;
8303
8304 switch (op.atom)
8305 {
8306 case DW_OP_abs:
8307 ABG_ASSERT(ctxt.stack.size() > 0);
8308 val1 = ctxt.pop();
8309 val1 = val1.abs();
8310 ctxt.push(val1);
8311 result = true;
8312 break;
8313
8314 case DW_OP_and:
8315 ABG_ASSERT(ctxt.stack.size() > 1);
8316 val1 = ctxt.pop();
8317 val2 = ctxt.pop();
8318 ctxt.push(val1 & val2);
8319 break;
8320
8321 case DW_OP_div:
8322 ABG_ASSERT(ctxt.stack.size() > 1);
8323 val1 = ctxt.pop();
8324 val2 = ctxt.pop();
8325 if (!val1.is_const())
8326 val1 = 1;
8327 ctxt.push(val2 / val1);
8328 result = true;
8329 break;
8330
8331 case DW_OP_minus:
8332 ABG_ASSERT(ctxt.stack.size() > 1);
8333 val1 = ctxt.pop();
8334 val2 = ctxt.pop();
8335 ctxt.push(val2 - val1);
8336 result = true;
8337 break;
8338
8339 case DW_OP_mod:
8340 ABG_ASSERT(ctxt.stack.size() > 1);
8341 val1 = ctxt.pop();
8342 val2 = ctxt.pop();
8343 ctxt.push(val2 % val1);
8344 result = true;
8345 break;
8346
8347 case DW_OP_mul:
8348 ABG_ASSERT(ctxt.stack.size() > 1);
8349 val1 = ctxt.pop();
8350 val2 = ctxt.pop();
8351 ctxt.push(val2 * val1);
8352 result = true;
8353 break;
8354
8355 case DW_OP_neg:
8356 ABG_ASSERT(ctxt.stack.size() > 0);
8357 val1 = ctxt.pop();
8358 ctxt.push(-val1);
8359 result = true;
8360 break;
8361
8362 case DW_OP_not:
8363 ABG_ASSERT(ctxt.stack.size() > 0);
8364 val1 = ctxt.pop();
8365 ctxt.push(~val1);
8366 result = true;
8367 break;
8368
8369 case DW_OP_or:
8370 ABG_ASSERT(ctxt.stack.size() > 1);
8371 val1 = ctxt.pop();
8372 val2 = ctxt.pop();
8373 ctxt.push(val1 | val2);
8374 result = true;
8375 break;
8376
8377 case DW_OP_plus:
8378 ABG_ASSERT(ctxt.stack.size() > 1);
8379 val1 = ctxt.pop();
8380 val2 = ctxt.pop();
8381 ctxt.push(val2 + val1);
8382 result = true;
8383 break;
8384
8385 case DW_OP_plus_uconst:
8386 ABG_ASSERT(ctxt.stack.size() > 0);
8387 val1 = ctxt.pop();
8388 val1 += op.number;
8389 ctxt.push(val1);
8390 result = true;
8391 break;
8392
8393 case DW_OP_shl:
8394 ABG_ASSERT(ctxt.stack.size() > 1);
8395 val1 = ctxt.pop();
8396 val2 = ctxt.pop();
8397 ctxt.push(val2 << val1);
8398 result = true;
8399 break;
8400
8401 case DW_OP_shr:
8402 case DW_OP_shra:
8403 ABG_ASSERT(ctxt.stack.size() > 1);
8404 val1 = ctxt.pop();
8405 val2 = ctxt.pop();
8406 ctxt.push(val2 >> val1);
8407 result = true;
8408 break;
8409
8410 case DW_OP_xor:
8411 ABG_ASSERT(ctxt.stack.size() > 1);
8412 val1 = ctxt.pop();
8413 val2 = ctxt.pop();
8414 ctxt.push(val2 ^ val1);
8415 result = true;
8416 break;
8417
8418 default:
8419 break;
8420 }
8421
8422 if (result == true)
8423 {
8424 if (ctxt.stack.front().is_const())
8425 ctxt.accum = ctxt.stack.front();
8426
8427 next_index = index + 1;
8428 }
8429 return result;;
8430}
8431
8432/// If the current operation in the dwarf expression represents a push
8433/// of a control flow operation onto the dwarf expr virtual machine
8434/// (aka DEVM), perform the operation and update the DEVM.
8435///
8436/// If the result of the operation is a constant, update the DEVM
8437/// accumulator with its value. Otherwise, the DEVM accumulator is
8438/// left with its previous value.
8439///
8440/// @param expr the array of the dwarf expression operations to consider.
8441///
8442/// @param expr_len the lengths of @p expr array above.
8443///
8444/// @param index the index of the operation to interpret, in @p expr.
8445///
8446/// @param next_index the index of the operation to interpret at the
8447/// next step, after this function completed and returned. This is
8448/// set an output parameter that is set iff the function returns true.
8449///
8450/// @param ctxt the DEVM evaluation context.
8451///
8452/// @return true if the current operation actually represents a
8453/// control flow operation, false otherwise.
8454static bool
8455op_is_control_flow(Dwarf_Op* expr,
8456 size_t expr_len,
8457 size_t index,
8458 size_t& next_index,
8459 dwarf_expr_eval_context& ctxt)
8460{
8461 ABG_ASSERT(index < expr_len);
8462
8463 Dwarf_Op& op = expr[index];
8464 expr_result val1, val2;
8465
8466 switch (op.atom)
8467 {
8468 case DW_OP_eq:
8469 case DW_OP_ge:
8470 case DW_OP_gt:
8471 case DW_OP_le:
8472 case DW_OP_lt:
8473 case DW_OP_ne:
8474 {
8475 bool value = true;
8476 val1 = ctxt.pop();
8477 val2 = ctxt.pop();
8478 if (op.atom == DW_OP_eq)
8479 value = val2 == val1;
8480 else if (op.atom == DW_OP_ge)
8481 value = val2 >= val1;
8482 else if (op.atom == DW_OP_gt)
8483 value = val2 > val1;
8484 else if (op.atom == DW_OP_le)
8485 value = val2 <= val1;
8486 else if (op.atom == DW_OP_lt)
8487 value = val2 < val1;
8488 else if (op.atom == DW_OP_ne)
8489 value = val2 != val1;
8490
8491 val1 = value ? 1 : 0;
8492 ctxt.push(val1);
8493 }
8494 break;
8495
8496 case DW_OP_skip:
8497 if (op.number > 0)
8498 index += op.number - 1;
8499 break;
8500
8501 case DW_OP_bra:
8502 val1 = ctxt.pop();
8503 if (val1.const_value() != 0)
8504 index += val1.const_value() - 1;
8505 break;
8506
8507 case DW_OP_call2:
8508 case DW_OP_call4:
8509 case DW_OP_call_ref:
8510 case DW_OP_nop:
8511 break;
8512
8513 default:
8514 return false;
8515 }
8516
8517 if (ctxt.stack.front().is_const())
8518 ctxt.accum = ctxt.stack.front();
8519
8520 next_index = index + 1;
8521 return true;
8522}
8523
8524/// This function quickly evaluates a DWARF expression that is a
8525/// constant.
8526///
8527/// This is a "fast path" function that quickly evaluates a DWARF
8528/// expression that is only made of a DW_OP_plus_uconst operator.
8529///
8530/// This is a sub-routine of die_member_offset.
8531///
8532/// @param expr the DWARF expression to evaluate.
8533///
8534/// @param expr_len the length of the expression @p expr.
8535///
8536/// @param value out parameter. This is set to the result of the
8537/// evaluation of @p expr, iff this function returns true.
8538///
8539/// @return true iff the evaluation of @p expr went OK.
8540static bool
8541eval_quickly(Dwarf_Op* expr,
8542 uint64_t expr_len,
8543 int64_t& value)
8544{
8545 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8546 {
8547 value = expr[0].number;
8548 return true;
8549 }
8550 return false;
8551}
8552
8553/// Evaluate the value of the last sub-expression that is a constant,
8554/// inside a given DWARF expression.
8555///
8556/// @param expr the DWARF expression to consider.
8557///
8558/// @param expr_len the length of the expression to consider.
8559///
8560/// @param value the resulting value of the last constant
8561/// sub-expression of the DWARF expression. This is set iff the
8562/// function returns true.
8563///
8564/// @param is_tls_address out parameter. This is set to true iff
8565/// the resulting value of the evaluation is a TLS (thread local
8566/// storage) address.
8567///
8568/// @param eval_ctxt the evaluation context to (re)use. Note that
8569/// this function initializes this context before using it.
8570///
8571/// @return true if the function could find a constant sub-expression
8572/// to evaluate, false otherwise.
8573static bool
8574eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8575 size_t expr_len,
8576 int64_t& value,
8577 bool& is_tls_address,
8578 dwarf_expr_eval_context &eval_ctxt)
8579{
8580 // Reset the evaluation context before evaluating the constant sub
8581 // expression contained in the DWARF expression 'expr'.
8582 eval_ctxt.reset();
8583
8584 size_t index = 0, next_index = 0;
8585 do
8586 {
8587 if (op_is_arith_logic(expr, expr_len, index,
8588 next_index, eval_ctxt)
8589 || op_pushes_constant_value(expr, expr_len, index,
8590 next_index, eval_ctxt)
8591 || op_manipulates_stack(expr, expr_len, index,
8592 next_index, eval_ctxt)
8593 || op_pushes_non_constant_value(expr, expr_len, index,
8594 next_index, eval_ctxt)
8595 || op_is_control_flow(expr, expr_len, index,
8596 next_index, eval_ctxt))
8597 ;
8598 else
8599 next_index = index + 1;
8600
8601 ABG_ASSERT(next_index > index);
8602 index = next_index;
8603 } while (index < expr_len);
8604
8605 is_tls_address = eval_ctxt.set_tls_address();
8606 if (eval_ctxt.accum.is_const())
8607 {
8608 value = eval_ctxt.accum;
8609 return true;
8610 }
8611 return false;
8612}
8613
8614/// Evaluate the value of the last sub-expression that is a constant,
8615/// inside a given DWARF expression.
8616///
8617/// @param expr the DWARF expression to consider.
8618///
8619/// @param expr_len the length of the expression to consider.
8620///
8621/// @param value the resulting value of the last constant
8622/// sub-expression of the DWARF expression. This is set iff the
8623/// function returns true.
8624///
8625/// @return true if the function could find a constant sub-expression
8626/// to evaluate, false otherwise.
8627static bool
8628eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8629 size_t expr_len,
8630 int64_t& value,
8631 bool& is_tls_address)
8632{
8633 dwarf_expr_eval_context eval_ctxt;
8634 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8635 is_tls_address, eval_ctxt);
8636}
8637
8638// -----------------------------------
8639// </location expression evaluation>
8640// -----------------------------------
8641
8642/// Convert a DW_AT_bit_offset attribute value into the same value as
8643/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8644///
8645/// On big endian machines, the value of the DW_AT_bit_offset
8646/// attribute + 8 * the value of the DW_AT_data_member_location
8647/// attribute is the same as the value of the DW_AT_data_bit_offset
8648/// attribute.
8649///
8650/// On little endian machines however, the situation is different.
8651/// The DW_AT_bit_offset value for a bit field is the number of bits
8652/// to the left of the most significant bit of the bit field, within
8653/// the integer value at DW_AT_data_member_location.
8654///
8655/// The DW_AT_data_bit_offset offset value is the number of bits to
8656/// the right of the least significant bit of the bit field, again
8657/// relative to the containing integer value.
8658///
8659/// In other words, DW_AT_data_bit_offset is what everybody would
8660/// instinctively think of as being the "offset of the bit field". 8 *
8661/// DW_AT_data_member_location + DW_AT_bit_offset however is very
8662/// counter-intuitive on little endian machines.
8663///
8664/// This function thus reads the value of a DW_AT_bit_offset property
8665/// of a DIE and converts it into what the DW_AT_data_bit_offset would
8666/// have been if it was present, ignoring the contribution of
8667/// DW_AT_data_member_location.
8668///
8669/// Note that DW_AT_bit_offset has been made obsolete starting from
8670/// DWARF5 (for GCC; Clang still emits it).
8671///
8672/// If you like coffee and it's not too late, now might be a good time
8673/// to have a coffee break. Otherwise if it's late at night, you
8674/// might want to consider an herbal tea break. Then come back to
8675/// read this.
8676///
8677///
8678/// In what follows, the bit fields are all contained within the first
8679/// whole int of the struct, so DW_AT_data_member_location is 0.
8680///
8681/// Okay, to have a better idea of what DW_AT_bit_offset and
8682/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8683/// have bit fields data members defined as:
8684///
8685/// struct S
8686/// {
8687/// int j:5;
8688/// int k:6;
8689/// int m:5;
8690/// int n:8;
8691/// };
8692///
8693/// The below wonderful (at least!) ASCII art sketch describes the
8694/// layout of the bitfields of 'struct S' on a little endian machine.
8695/// You need to read the sketch from the bottom-up.
8696///
8697/// So please scroll down to its bottom. Note how the 32 bits integer
8698/// word containing the bit fields is laid out with its least
8699/// significant bit starting on the right hand side, at index 0.
8700///
8701/// Then slowly scroll up starting from there, and take the time to
8702/// read each line and see how the bit fields are laid out and what
8703/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8704/// the bit fields.
8705///
8706/// DW_AT_bit_offset(n)
8707/// < - - - - - - >
8708/// | | n |
8709/// ^ ^< - - - - >^
8710/// DW_AT_data_bit_offset(n)
8711/// < - - - - - - - - - - - - - - - >
8712/// | |
8713/// ^ ^
8714/// DW_AT_bit_offset(m)
8715/// <--------------------------------->
8716/// | | m |
8717/// ^ ^< - >^
8718/// DW_AT_data_bit_offset(m)
8719/// < - - - - - - - - - - >
8720/// | |
8721/// ^ ^
8722/// DW_AT_bit_offset(k)
8723/// <-------------------------------------------->
8724/// | | k |
8725/// ^ ^< - - >^
8726/// DW_AT_data_bit_offset(k)
8727/// < - - - - >
8728/// | |
8729/// ^ ^
8730/// DW_AT_bit_offset(j)
8731/// <-------------------------------------------------------->
8732/// | |
8733/// ^ ^
8734/// n m k j
8735/// < - - - - - - > < - - - > < - - - - > < - - - >
8736///
8737/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8738/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8739/// 31 27 23 16 15 11 10 6 5 4 0
8740///
8741/// So, the different bit fields all fit in one 32 bits word, assuming
8742/// the bit fields are tightly packed.
8743///
8744/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8745/// on this little endian machine and let's see how it relates to
8746/// DW_AT_data_bit_offset of j.
8747///
8748/// DW_AT_bit_offset(j) would be equal to the number of bits from the
8749/// left of the 32 bits word (i.e from bit number 31) to the most
8750/// significant bit of the j bit field (i.e, bit number 4). Thus:
8751///
8752/// DW_AT_bit_offset(j) =
8753/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8754///
8755/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8756/// 32 bits word (i.e, bit number 0) to the lest significant bit of
8757/// the 'j' bit field (ie, bit number 0). Thus:
8758///
8759/// DW_AT_data_bit_offset(j) = 0.
8760///
8761/// More generally, we can notice that:
8762///
8763/// sizeof_in_bits(int) =
8764/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8765///
8766/// It follows that:
8767///
8768/// DW_AT_data_bit_offset(j) =
8769/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8770///
8771/// Thus:
8772///
8773/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8774///
8775/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8776/// from the right hand side of the word. It is what we would
8777/// intuitively think it is. DW_AT_bit_offset however is super
8778/// counter-intuitive, pfff.
8779///
8780/// Anyway, this general equation holds true for all bit fields.
8781///
8782/// Similarly, it follows that:
8783///
8784/// DW_AT_bit_offset(k) =
8785/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8786///
8787/// Thus:
8788/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8789///
8790///
8791/// Likewise:
8792///
8793/// DW_AT_bit_offset(m) =
8794/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8795///
8796///
8797/// Thus:
8798/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8799///
8800/// And:
8801///
8802///
8803/// Lastly:
8804///
8805/// DW_AT_bit_offset(n) =
8806/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8807///
8808/// Thus:
8809/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8810///
8811/// Luckily, the body of the function is much smaller than this
8812/// comment. Enjoy!
8813///
8814/// @param die the DIE to consider.
8815///
8816/// @param is_big_endian this is true iff the machine we are looking at
8817/// is big endian.
8818///
8819/// @param offset this is the output parameter into which the value of
8820/// the DW_AT_bit_offset is put, converted as if it was the value of
8821/// the DW_AT_data_bit_offset parameter, less the contribution of
8822/// DW_AT_data_member_location. This parameter is set iff the
8823/// function returns true.
8824///
8825/// @return true if DW_AT_bit_offset was found on @p die.
8826static bool
8827read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8828 bool is_big_endian,
8829 uint64_t &offset)
8830{
8831 uint64_t off = 0;
8832 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8833 return false;
8834
8835 if (is_big_endian)
8836 {
8837 offset = off;
8838 return true;
8839 }
8840
8841 // Okay, we are looking at a little endian machine. We need to
8842 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8843 // have been. To understand this, you really need to read the
8844 // preliminary comment of this function.
8845 uint64_t containing_anonymous_object_size = 0;
8846 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8847 containing_anonymous_object_size));
8848 containing_anonymous_object_size *= 8;
8849
8850 uint64_t bitfield_size = 0;
8851 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8852 bitfield_size));
8853
8854 // As noted in the the preliminary comment of this function if we
8855 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8856 // its DW_AT_bit_offset value, the equation is:
8857 //
8858 // DW_AT_data_bit_offset(k) =
8859 // sizeof_in_bits(containing_anonymous_object_size)
8860 // - DW_AT_data_bit_offset(k)
8861 // - sizeof_in_bits(k)
8862 offset = containing_anonymous_object_size - off - bitfield_size;
8863
8864 return true;
8865}
8866
8867/// Get the value of the DW_AT_data_member_location of the given DIE
8868/// attribute as an constant.
8869///
8870/// @param die the DIE to read the attribute from.
8871///
8872/// @param offset the attribute as a constant value. This is set iff
8873/// the function returns true.
8874///
8875/// @return true if the attribute exists and has a constant value. In
8876/// that case the offset is set to the value.
8877static bool
8878die_constant_data_member_location(const Dwarf_Die *die,
8879 int64_t& offset)
8880{
8881 if (!die)
8882 return false;
8883
8884 Dwarf_Attribute attr;
8885 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8886 DW_AT_data_member_location,
8887 &attr))
8888 return false;
8889
8890 Dwarf_Word val;
8891 if (dwarf_formudata(&attr, &val) != 0)
8892 return false;
8893
8894 offset = val;
8895 return true;
8896}
8897
8898/// Get the offset of a struct/class member as represented by the
8899/// value of the DW_AT_data_member_location attribute.
8900///
8901/// There is a huge gotcha in here. The value of the
8902/// DW_AT_data_member_location is not necessarily a constant that one
8903/// would just read and be done with it. Rather, it can be a DWARF
8904/// expression that one has to interpret. In general, the offset can
8905/// be given by the DW_AT_data_bit_offset or by the
8906/// DW_AT_data_member_location attribute and optionally the
8907/// DW_AT_bit_offset attribute. The bit offset attributes are
8908/// always simple constants, but the DW_AT_data_member_location
8909/// attribute is a DWARF location expression.
8910///
8911/// When it's the DW_AT_data_member_location that is present,
8912/// there are three cases to possibly take into account:
8913///
8914/// 1/ The offset in the vtable where the offset of a virtual base
8915/// can be found, aka vptr offset. Given the address of a
8916/// given object O, the vptr offset for B is given by the
8917/// (DWARF) expression:
8918///
8919/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8920///
8921/// where VIRTUAL_OFFSET is a constant value; In this case,
8922/// this function returns the constant VIRTUAL_OFFSET, as this
8923/// is enough to detect changes in a given virtual base
8924/// relative to the other virtual bases.
8925///
8926/// 2/ The offset of a regular data member. Given the address of
8927/// a struct object named O, the memory location for a
8928/// particular data member is given by the (DWARF) expression:
8929///
8930/// address(O) + OFFSET
8931///
8932/// where OFFSET is a constant. In this case, this function
8933/// returns the OFFSET constant.
8934///
8935/// 3/ The offset of a virtual member function in the virtual
8936/// pointer. The DWARF expression is a constant that designates
8937/// the offset of the function in the vtable. In this case this
8938/// function returns that constant.
8939///
8940/// @param rdr the DWARF reader to consider.
8941///
8942/// @param die the DIE to read the information from.
8943///
8944/// @param offset the resulting constant offset, in bits. This
8945/// argument is set iff the function returns true.
8946static bool
8947die_member_offset(const reader& rdr,
8948 const Dwarf_Die* die,
8949 int64_t& offset)
8950{
8951 Dwarf_Op* expr = NULL;
8952 size_t expr_len = 0;
8953 uint64_t bit_offset = 0;
8954
8955 // First let's see if the DW_AT_data_bit_offset attribute is
8956 // present.
8957 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8958 {
8959 offset = bit_offset;
8960 return true;
8961 }
8962
8963 // First try to read DW_AT_data_member_location as a plain constant.
8964 // We do this because the generic method using die_location_expr
8965 // might hit a bug in elfutils libdw dwarf_location_expression only
8966 // fixed in elfutils 0.184+. The bug only triggers if the attribute
8967 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8968 // handle all constants here because that is more consistent (and
8969 // slightly faster in the general case where the attribute isn't a
8970 // full DWARF expression).
8971 if (!die_constant_data_member_location(die, offset))
8972 {
8973 // Otherwise, let's see if the DW_AT_data_member_location
8974 // attribute and, optionally, the DW_AT_bit_offset attributes
8975 // are present.
8976 if (!die_location_expr(die, DW_AT_data_member_location,
8977 &expr, &expr_len))
8978 return false;
8979
8980 // The DW_AT_data_member_location attribute is present. Let's
8981 // evaluate it and get its constant sub-expression and return
8982 // that one.
8983 if (!eval_quickly(expr, expr_len, offset))
8984 {
8985 bool is_tls_address = false;
8986 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8987 offset, is_tls_address,
8988 rdr.dwarf_expr_eval_ctxt()))
8989 return false;
8990 }
8991 }
8992 offset *= 8;
8993
8994 // On little endian machines, we need to convert the
8995 // DW_AT_bit_offset attribute into a relative offset to 8 *
8996 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8997 // would be if it were used instead.
8998 //
8999 // In other words, before adding it to 8 *
9000 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
9001 // converted into a human-understandable form that represents the
9002 // offset of the bitfield data member it describes. For details
9003 // about the conversion, please read the extensive comments of
9004 // read_and_convert_DW_at_bit_offset.
9005 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
9006 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
9007 offset += bit_offset;
9008
9009 return true;
9010}
9011
9012/// Read the value of the DW_AT_location attribute from a DIE,
9013/// evaluate the resulting DWARF expression and, if it's a constant
9014/// expression, return it.
9015///
9016/// @param die the DIE to consider.
9017///
9018/// @param address the resulting constant address. This is set iff
9019/// the function returns true.
9020///
9021/// @return true iff the whole sequence of action described above
9022/// could be completed normally.
9023static bool
9024die_location_address(Dwarf_Die* die,
9025 Dwarf_Addr& address,
9026 bool& is_tls_address)
9027{
9028 Dwarf_Op* expr = NULL;
9029 size_t expr_len = 0;
9030
9031 is_tls_address = false;
9032
9033 if (!die)
9034 return false;
9035
9036 Dwarf_Attribute attr;
9037 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
9038 return false;
9039
9040 if (dwarf_getlocation(&attr, &expr, &expr_len))
9041 return false;
9042 // Ignore location expressions where reading them succeeded but
9043 // their length is 0.
9044 if (expr_len == 0)
9045 return false;
9046
9047 Dwarf_Attribute result;
9048 if (!dwarf_getlocation_attr(&attr, expr, &result))
9049 // A location that has been interpreted as an address.
9050 return !dwarf_formaddr(&result, &address);
9051
9052 // Just get the address out of the number field.
9053 address = expr->number;
9054 return true;
9055}
9056
9057/// Return the index of a function in its virtual table. That is,
9058/// return the value of the DW_AT_vtable_elem_location attribute.
9059///
9060/// @param die the DIE of the function to consider.
9061///
9062/// @param vindex the resulting index. This is set iff the function
9063/// returns true.
9064///
9065/// @return true if the DIE has a DW_AT_vtable_elem_location
9066/// attribute.
9067static bool
9068die_virtual_function_index(Dwarf_Die* die,
9069 int64_t& vindex)
9070{
9071 if (!die)
9072 return false;
9073
9074 Dwarf_Op* expr = NULL;
9075 size_t expr_len = 0;
9076 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9077 &expr, &expr_len))
9078 return false;
9079
9080 int64_t i = 0;
9081 bool is_tls_addr = false;
9082 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9083 return false;
9084
9085 vindex = i;
9086 return true;
9087}
9088
9089/// Test if a given DIE represents an anonymous type.
9090///
9091/// Anonymous types we are interested in are classes, unions and
9092/// enumerations.
9093///
9094/// @param die the DIE to consider.
9095///
9096/// @return true iff @p die represents an anonymous type.
9097bool
9099{
9100 int tag = dwarf_tag(die);
9101
9102 if (tag == DW_TAG_class_type
9103 || tag == DW_TAG_structure_type
9104 || tag == DW_TAG_union_type
9105 || tag == DW_TAG_enumeration_type)
9106 return die_is_anonymous(die);
9107
9108 return false;
9109}
9110
9111/// Return the base of the internal name to represent an anonymous
9112/// type.
9113///
9114/// Typically, anonymous enums would be named
9115/// __anonymous_enum__<number>, anonymous struct or classes would be
9116/// named __anonymous_struct__<number> and anonymous unions would be
9117/// named __anonymous_union__<number>. The first part of these
9118/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9119/// the base name. This function returns that base name, depending on
9120/// the kind of type DIE we are looking at.
9121///
9122/// @param die the type DIE to look at. This function expects a type
9123/// DIE with an empty DW_AT_name property value (anonymous).
9124///
9125/// @return a string representing the base of the internal anonymous
9126/// name.
9127static string
9128get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9129{
9130 ABG_ASSERT(die_is_type(die));
9131 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9132
9133 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9134 string type_name;
9135 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9137 else if (tag == DW_TAG_union_type)
9139 else if (tag == DW_TAG_enumeration_type)
9141
9142 return type_name;
9143}
9144
9145/// Build a full internal anonymous type name.
9146///
9147/// @param base_name this is the base name as returned by the function
9148/// @ref get_internal_anonymous_die_prefix_name.
9149///
9150/// @param anonymous_type_index this is the index of the anonymous
9151/// type in its scope. That is, if there are more than one anonymous
9152/// types of a given kind in a scope, this index is what tells them
9153/// appart, starting from 0.
9154///
9155/// @return the built string, which is a concatenation of @p base_name
9156/// and @p anonymous_type_index.
9157static string
9158build_internal_anonymous_die_name(const string &base_name,
9159 size_t anonymous_type_index)
9160{
9161 string name = base_name;
9162 if (anonymous_type_index && !base_name.empty())
9163 {
9164 std::ostringstream o;
9165 o << base_name << anonymous_type_index;
9166 name = o.str();
9167 }
9168 return name;
9169}
9170
9171
9172/// Build a full internal anonymous type name.
9173///
9174/// @param die the DIE representing the anonymous type to consider.
9175///
9176/// @param anonymous_type_index the index of the anonymous type
9177/// represented by @p DIE, in its scope. That is, if there are
9178/// several different anonymous types of the same kind as @p die, this
9179/// index is what tells them appart.
9180///
9181/// @return the internal name of the anonymous type represented by @p
9182/// DIE.
9183static string
9184get_internal_anonymous_die_name(Dwarf_Die *die,
9185 size_t anonymous_type_index)
9186{
9187 string name = get_internal_anonymous_die_prefix_name(die);
9188 name = build_internal_anonymous_die_name(name, anonymous_type_index);
9189 return name;
9190}
9191
9192// ------------------------------------
9193// <DIE pretty printer>
9194// ------------------------------------
9195
9196/// Compute the qualified name of a DIE that represents a type.
9197///
9198/// For instance, if the DIE tag is DW_TAG_subprogram then this
9199/// function computes the name of the function *type*.
9200///
9201/// @param rdr the DWARF reader.
9202///
9203/// @param die the DIE to consider.
9204///
9205/// @param where_offset where in the are logically are in the DIE
9206/// stream.
9207///
9208/// @return a copy of the qualified name of the type.
9209static string
9210die_qualified_type_name(const reader& rdr,
9211 const Dwarf_Die* die,
9212 size_t where_offset)
9213{
9214 if (!die)
9215 return "";
9216
9217 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9218 if (tag == DW_TAG_compile_unit
9219 || tag == DW_TAG_partial_unit
9220 || tag == DW_TAG_type_unit)
9221 return "";
9222
9223 string name = die_name(die);
9224
9225 Dwarf_Die scope_die;
9226 if (!get_scope_die(rdr, die, where_offset, scope_die))
9227 return "";
9228
9229 string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
9230 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9231 string separator = colon_colon ? "::" : ".";
9232
9233 string repr;
9234
9235 switch (tag)
9236 {
9237 case DW_TAG_unspecified_type:
9238 break;
9239
9240 case DW_TAG_base_type:
9241 {
9243 if (parse_integral_type(name, int_type))
9244 repr = int_type;
9245 else
9246 repr = name;
9247 }
9248 break;
9249
9250 case DW_TAG_typedef:
9251 case DW_TAG_enumeration_type:
9252 case DW_TAG_structure_type:
9253 case DW_TAG_class_type:
9254 case DW_TAG_union_type:
9255 {
9256 if (name.empty())
9257 // TODO: handle cases where there are more than one
9258 // anonymous type of the same kind in the same scope. In
9259 // that case, their name must be built with the function
9260 // get_internal_anonymous_die_name or something of the same
9261 // kind.
9262 name = get_internal_anonymous_die_prefix_name(die);
9263
9264 ABG_ASSERT(!name.empty());
9265 repr = parent_name.empty() ? name : parent_name + separator + name;
9266 }
9267 break;
9268
9269 case DW_TAG_const_type:
9270 case DW_TAG_volatile_type:
9271 case DW_TAG_restrict_type:
9272 {
9273 Dwarf_Die underlying_type_die;
9274 bool has_underlying_type_die =
9275 die_die_attribute(die, DW_AT_type, underlying_type_die);
9276
9277 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9278 break;
9279
9280 if (tag == DW_TAG_const_type)
9281 {
9282 if (has_underlying_type_die
9283 && die_is_reference_type(&underlying_type_die))
9284 // A reference is always const. So, to lower false
9285 // positive reports in diff computations, we consider a
9286 // const reference just as a reference. But we need to
9287 // keep the qualified-ness of the type. So we introduce
9288 // a 'no-op' qualifier here. Please remember that this
9289 // has to be kept in sync with what is done in
9290 // get_name_of_qualified_type. So if you change this
9291 // here, you have to change that code there too.
9292 repr = "";
9293 else if (!has_underlying_type_die
9294 || die_is_void_type(&underlying_type_die))
9295 {
9296 repr = "void";
9297 break;
9298 }
9299 else
9300 repr = "const";
9301 }
9302 else if (tag == DW_TAG_volatile_type)
9303 repr = "volatile";
9304 else if (tag == DW_TAG_restrict_type)
9305 repr = "restrict";
9306 else
9308
9309 string underlying_type_repr;
9310 if (has_underlying_type_die)
9311 underlying_type_repr =
9312 die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9313 else
9314 underlying_type_repr = "void";
9315
9316 if (underlying_type_repr.empty())
9317 repr.clear();
9318 else
9319 {
9320 if (has_underlying_type_die)
9321 {
9322 Dwarf_Die peeled;
9323 die_peel_qualified(&underlying_type_die, peeled);
9324 if (die_is_pointer_or_reference_type(&peeled))
9325 repr = underlying_type_repr + " " + repr;
9326 else
9327 repr += " " + underlying_type_repr;
9328 }
9329 else
9330 repr += " " + underlying_type_repr;
9331 }
9332 }
9333 break;
9334
9335 case DW_TAG_pointer_type:
9336 case DW_TAG_reference_type:
9337 case DW_TAG_rvalue_reference_type:
9338 {
9339 Dwarf_Die pointed_to_type_die;
9340 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9341 {
9342 if (tag == DW_TAG_pointer_type)
9343 repr = "void*";
9344 break;
9345 }
9346
9347 if (die_is_unspecified(&pointed_to_type_die))
9348 break;
9349
9350 string pointed_type_repr =
9351 die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9352
9353 repr = pointed_type_repr;
9354 if (repr.empty())
9355 break;
9356
9357 if (tag == DW_TAG_pointer_type)
9358 repr += "*";
9359 else if (tag == DW_TAG_reference_type)
9360 repr += "&";
9361 else if (tag == DW_TAG_rvalue_reference_type)
9362 repr += "&&";
9363 else
9365 }
9366 break;
9367
9368 case DW_TAG_subrange_type:
9369 {
9370 // In Ada, this one can be generated on its own, that is, not
9371 // as a sub-type of an array. So we need to support it on its
9372 // own. Note that when it's emitted as the sub-type of an
9373 // array like in C and C++, this is handled differently, for
9374 // now. But we try to make this usable by other languages
9375 // that are not Ada, even if we modelled it after Ada.
9376
9377 // So we build a subrange type for the sole purpose of using
9378 // the ::as_string() method of that type. So we don't add
9379 // that type to the current type tree being built.
9381 build_subrange_type(const_cast<reader&>(rdr),
9382 die, where_offset,
9383 /*associate_die_to_type=*/false);
9384 repr += s->as_string();
9385 break;
9386 }
9387
9388 case DW_TAG_array_type:
9389 {
9390 Dwarf_Die element_type_die;
9391 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9392 break;
9393 string element_type_name =
9394 die_qualified_type_name(rdr, &element_type_die, where_offset);
9395 if (element_type_name.empty())
9396 break;
9397
9399 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9400 die, subranges, where_offset,
9401 /*associate_type_to_die=*/false);
9402
9403 repr = element_type_name;
9404 repr += array_type_def::subrange_type::vector_as_string(subranges);
9405 }
9406 break;
9407
9408 case DW_TAG_subroutine_type:
9409 case DW_TAG_subprogram:
9410 {
9411 string return_type_name;
9412 string class_name;
9413 vector<string> parm_names;
9414 bool is_const = false;
9415 bool is_static = false;
9416
9417 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9418 /*pretty_print=*/true,
9419 return_type_name, class_name,
9420 parm_names, is_const,
9421 is_static);
9422 if (return_type_name.empty())
9423 return_type_name = "void";
9424
9425 repr = return_type_name;
9426
9427 if (!class_name.empty())
9428 {
9429 // This is a method, so print the class name.
9430 repr += " (" + class_name + "::*)";
9431 }
9432
9433 // Now parameters.
9434 repr += " (";
9435 for (vector<string>::const_iterator i = parm_names.begin();
9436 i != parm_names.end();
9437 ++i)
9438 {
9439 if (i != parm_names.begin())
9440 repr += ", ";
9441 repr += *i;
9442 }
9443 repr += ")";
9444
9445 }
9446 break;
9447
9448 case DW_TAG_string_type:
9449 case DW_TAG_ptr_to_member_type:
9450 case DW_TAG_set_type:
9451 case DW_TAG_file_type:
9452 case DW_TAG_packed_type:
9453 case DW_TAG_thrown_type:
9454 case DW_TAG_interface_type:
9455 case DW_TAG_shared_type:
9456 break;
9457 }
9458
9459 return repr;
9460}
9461
9462/// Compute the qualified name of a decl represented by a given DIE.
9463///
9464/// For instance, for a DIE of tag DW_TAG_subprogram this function
9465/// computes the signature of the function *declaration*.
9466///
9467/// @param rdr the DWARF reader.
9468///
9469/// @param die the DIE to consider.
9470///
9471/// @param where_offset where we are logically at in the DIE stream.
9472///
9473/// @return a copy of the computed name.
9474static string
9475die_qualified_decl_name(const reader& rdr,
9476 const Dwarf_Die* die,
9477 size_t where_offset)
9478{
9479 if (!die || !die_is_decl(die))
9480 return "";
9481
9482 string name = die_name(die);
9483
9484 Dwarf_Die scope_die;
9485 if (!get_scope_die(rdr, die, where_offset, scope_die))
9486 return "";
9487
9488 string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9489 string separator = "::";
9490
9491 string repr;
9492
9493 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9494 switch (tag)
9495 {
9496 case DW_TAG_namespace:
9497 case DW_TAG_member:
9498 case DW_TAG_variable:
9499 repr = scope_name.empty() ? name : scope_name + separator + name;
9500 break;
9501 case DW_TAG_subprogram:
9502 repr = die_function_signature(rdr, die, where_offset);
9503 break;
9504
9505 case DW_TAG_unspecified_parameters:
9506 repr = "...";
9507 break;
9508
9509 case DW_TAG_formal_parameter:
9510 case DW_TAG_imported_declaration:
9511 case DW_TAG_GNU_template_template_param:
9512 case DW_TAG_GNU_template_parameter_pack:
9513 case DW_TAG_GNU_formal_parameter_pack:
9514 break;
9515 }
9516 return repr;
9517}
9518
9519/// Compute the qualified name of the artifact represented by a given
9520/// DIE.
9521///
9522/// If the DIE represents a type, then the function computes the name
9523/// of the type. Otherwise, if the DIE represents a decl then the
9524/// function computes the name of the decl. Note that a DIE of tag
9525/// DW_TAG_subprogram is going to be considered as a "type" -- just
9526/// like if it was a DW_TAG_subroutine_type.
9527///
9528/// @param rdr the DWARF reader.
9529///
9530/// @param die the DIE to consider.
9531///
9532/// @param where_offset where we are logically at in the DIE stream.
9533///
9534/// @return a copy of the computed name.
9535static string
9536die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9537{
9538 if (die_is_type(die))
9539 return die_qualified_type_name(rdr, die, where);
9540 else if (die_is_decl(die))
9541 return die_qualified_decl_name(rdr, die, where);
9542 return "";
9543}
9544
9545/// Test if the qualified name of a given type should be empty.
9546///
9547/// The reason why the name of a DIE with a given tag would be empty
9548/// is that libabigail's internal representation doesn't yet support
9549/// that tag; or if the DIE's qualified name is built from names of
9550/// sub-types DIEs whose tags are not yet supported.
9551///
9552/// @param rdr the DWARF reader.
9553///
9554/// @param die the DIE to consider.
9555///
9556/// @param where where we are logically at, in the DIE stream.
9557///
9558/// @param qualified_name the qualified name of the DIE. This is set
9559/// only iff the function returns false.
9560///
9561/// @return true if the qualified name of the DIE is empty.
9562static bool
9563die_qualified_type_name_empty(const reader& rdr,
9564 const Dwarf_Die* die,
9565 size_t where, string &qualified_name)
9566{
9567 if (!die)
9568 return true;
9569
9570 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9571
9572 string qname;
9573 if (tag == DW_TAG_typedef
9574 || tag == DW_TAG_pointer_type
9575 || tag == DW_TAG_reference_type
9576 || tag == DW_TAG_rvalue_reference_type
9577 || tag == DW_TAG_array_type
9578 || tag == DW_TAG_const_type
9579 || tag == DW_TAG_volatile_type
9580 || tag == DW_TAG_restrict_type)
9581 {
9582 Dwarf_Die underlying_type_die;
9583 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9584 {
9585 string name =
9586 die_qualified_type_name(rdr, &underlying_type_die, where);
9587 if (name.empty())
9588 return true;
9589 }
9590 }
9591 else
9592 {
9593 string name = die_qualified_type_name(rdr, die, where);
9594 if (name.empty())
9595 return true;
9596 }
9597
9598 qname = die_qualified_type_name(rdr, die, where);
9599 if (qname.empty())
9600 return true;
9601
9602 qualified_name = qname;
9603 return false;
9604}
9605
9606/// Given the DIE that represents a function type, compute the names
9607/// of the following properties the function's type:
9608///
9609/// - return type
9610/// - enclosing class (if the function is a member function)
9611/// - function parameter types
9612///
9613/// When the function we are looking at is a member function, it also
9614/// tells if it's const.
9615///
9616/// @param rdr the DWARF reader.
9617///
9618/// @param die the DIE of the function or function type we are looking
9619/// at.
9620///
9621/// @param where_offset where we are logically at in the DIE stream.
9622///
9623/// @param pretty_print if set to yes, the type names are going to be
9624/// pretty-printed names; otherwise, they are just qualified type
9625/// names.
9626///
9627/// @param return_type_name out parameter. This contains the name of
9628/// the return type of the function.
9629///
9630/// @param class_name out parameter. If the function is a member
9631/// function, this contains the name of the enclosing class.
9632///
9633/// @param parm_names out parameter. This vector is set to the names
9634/// of the types of the parameters of the function.
9635///
9636/// @param is_const out parameter. If the function is a member
9637/// function, this is set to true iff the member function is const.
9638///
9639/// @param is_static out parameter. If the function is a static
9640/// member function, then this is set to true.
9641static void
9642die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9643 const Dwarf_Die* die,
9644 size_t where_offset,
9645 bool pretty_print,
9646 string &return_type_name,
9647 string &class_name,
9648 vector<string>& parm_names,
9649 bool& is_const,
9650 bool& is_static)
9651{
9652 Dwarf_Die child;
9653 Dwarf_Die ret_type_die;
9654 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9655 return_type_name = "void";
9656 else
9657 return_type_name =
9658 pretty_print
9659 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9660 : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9661
9662 if (return_type_name.empty())
9663 return_type_name = "void";
9664
9665 Dwarf_Die object_pointer_die, class_die;
9666 bool is_method_type =
9667 die_function_type_is_method_type(rdr, die, where_offset,
9668 object_pointer_die,
9669 class_die, is_static);
9670
9671 is_const = false;
9672 if (is_method_type)
9673 {
9674 class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9675
9676 Dwarf_Die this_pointer_die;
9677 Dwarf_Die pointed_to_die;
9678 if (!is_static
9679 && die_die_attribute(&object_pointer_die, DW_AT_type,
9680 this_pointer_die))
9681 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9682 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9683 is_const = true;
9684
9685 string fn_name = die_name(die);
9686 string non_qualified_class_name = die_name(&class_die);
9687 bool is_ctor = fn_name == non_qualified_class_name;
9688 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9689
9690 if (is_ctor || is_dtor)
9691 return_type_name.clear();
9692 }
9693
9694 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9695 do
9696 {
9697 int child_tag = dwarf_tag(&child);
9698 if (child_tag == DW_TAG_formal_parameter)
9699 {
9700 Dwarf_Die parm_type_die;
9701 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9702 continue;
9703 string qualified_name =
9704 pretty_print
9705 ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
9706 : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
9707
9708 if (qualified_name.empty())
9709 continue;
9710 parm_names.push_back(qualified_name);
9711 }
9712 else if (child_tag == DW_TAG_unspecified_parameters)
9713 {
9714 // This is a variadic function parameter.
9715 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
9716 // After a DW_TAG_unspecified_parameters tag, we shouldn't
9717 // keep reading for parameters. The
9718 // unspecified_parameters TAG should be the last parameter
9719 // that we record. For instance, if there are multiple
9720 // DW_TAG_unspecified_parameters DIEs then we should care
9721 // only for the first one.
9722 break;
9723 }
9724 }
9725 while (dwarf_siblingof(&child, &child) == 0);
9726
9727 if (class_name.empty())
9728 {
9729 Dwarf_Die parent_die;
9730 if (get_parent_die(rdr, die, parent_die, where_offset))
9731 {
9732 if (die_is_class_type(&parent_die))
9733 class_name =
9734 rdr.get_die_qualified_type_name(&parent_die, where_offset);
9735 }
9736 }
9737}
9738
9739/// This computes the signature of the a function declaration
9740/// represented by a DIE.
9741///
9742/// @param rdr the DWARF reader.
9743///
9744/// @param fn_die the DIE of the function to consider.
9745///
9746/// @param where_offset where we are logically at in the stream of
9747/// DIEs.
9748///
9749/// @return a copy of the computed function signature string.
9750static string
9751die_function_signature(const reader& rdr,
9752 const Dwarf_Die *fn_die,
9753 size_t where_offset)
9754{
9755
9757 bool has_lang = false;
9758 if ((has_lang = rdr.get_die_language(fn_die, lang)))
9759 {
9760 // In a binary originating from the C language, it's OK to use
9761 // the linkage name of the function as a key for the map which
9762 // is meant to reduce the number of DIE comparisons involved
9763 // during DIE canonicalization computation.
9764 if (is_c_language(lang))
9765 {
9766 string fn_name = die_linkage_name(fn_die);
9767 if (fn_name.empty())
9768 fn_name = die_name(fn_die);
9769 return fn_name;
9770 }
9771 }
9772
9773 // TODO: When we can structurally compare DIEs originating from C++
9774 // as well, we can use the linkage name of functions in C++ too, to
9775 // reduce the number of comparisons involved during DIE
9776 // canonicalization.
9777
9778 string return_type_name;
9779 Dwarf_Die ret_type_die;
9780 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9781 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
9782 where_offset);
9783
9784 if (return_type_name.empty())
9785 return_type_name = "void";
9786
9787 Dwarf_Die scope_die;
9788 string scope_name;
9789 if (get_scope_die(rdr, fn_die, where_offset, scope_die))
9790 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
9791 string fn_name = die_name(fn_die);
9792 if (!scope_name.empty())
9793 fn_name = scope_name + "::" + fn_name;
9794
9795 string class_name;
9796 vector<string> parm_names;
9797 bool is_const = false;
9798 bool is_static = false;
9799
9800 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
9801 /*pretty_print=*/false,
9802 return_type_name, class_name,
9803 parm_names, is_const, is_static);
9804
9805 bool is_virtual = die_is_virtual(fn_die);
9806
9807 string repr = class_name.empty() ? "function" : "method";
9808 if (is_virtual)
9809 repr += " virtual";
9810
9811 if (!return_type_name.empty())
9812 repr += " " + return_type_name;
9813
9814 repr += " " + fn_name;
9815
9816 // Now parameters.
9817 repr += "(";
9818 bool some_parm_emitted = false;
9819 for (vector<string>::const_iterator i = parm_names.begin();
9820 i != parm_names.end();
9821 ++i)
9822 {
9823 if (i != parm_names.begin())
9824 {
9825 if (some_parm_emitted)
9826 repr += ", ";
9827 }
9828 else
9829 if (!is_static && !class_name.empty())
9830 // We are printing a non-static method name, skip the implicit "this"
9831 // parameter type.
9832 continue;
9833 repr += *i;
9834 some_parm_emitted = true;
9835 }
9836 repr += ")";
9837
9838 if (is_const)
9839 {
9840 ABG_ASSERT(!class_name.empty());
9841 repr += " const";
9842 }
9843
9844 return repr;
9845}
9846
9847/// Return a pretty string representation of a type, for internal purposes.
9848///
9849/// By internal purpose, we mean things like key-ing types for lookup
9850/// purposes and so on.
9851///
9852/// Note that this function is also used to pretty print functions.
9853/// For functions, it prints the *type* of the function.
9854///
9855/// @param rdr the context to use.
9856///
9857/// @param the DIE of the type to pretty print.
9858///
9859/// @param where_offset where we logically are placed when calling
9860/// this. It's useful to handle inclusion of DW_TAG_compile_unit
9861/// entries.
9862///
9863/// @return the resulting pretty representation.
9864static string
9865die_pretty_print_type(reader& rdr,
9866 const Dwarf_Die* die,
9867 size_t where_offset)
9868{
9869 if (!die
9870 || (!die_is_type(die)
9871 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9872 return "";
9873
9874 string repr;
9875
9876 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9877 switch (tag)
9878 {
9879 case DW_TAG_string_type:
9880 // For now, we won't try to go get the actual representation of
9881 // the string because this would make things more complicated;
9882 // for that we'd need to interpret some location expressions to
9883 // get the length of the string. And for dynamically allocated
9884 // strings, the result of the location expression evaluation
9885 // might not even be a constant. So at the moment I consider
9886 // this to be a lot of hassle for no great return. Until proven
9887 // otherwise, of course.
9888 repr = "string type";
9889
9890 case DW_TAG_unspecified_type:
9891 case DW_TAG_ptr_to_member_type:
9892 break;
9893
9894 case DW_TAG_namespace:
9895 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
9896 break;
9897
9898 case DW_TAG_base_type:
9899 repr = rdr.get_die_qualified_type_name(die, where_offset);
9900 break;
9901
9902 case DW_TAG_typedef:
9903 {
9904 string qualified_name;
9905 if (!die_qualified_type_name_empty(rdr, die,
9906 where_offset,
9907 qualified_name))
9908 repr = "typedef " + qualified_name;
9909 }
9910 break;
9911
9912 case DW_TAG_const_type:
9913 case DW_TAG_volatile_type:
9914 case DW_TAG_restrict_type:
9915 case DW_TAG_pointer_type:
9916 case DW_TAG_reference_type:
9917 case DW_TAG_rvalue_reference_type:
9918 repr = rdr.get_die_qualified_type_name(die, where_offset);
9919 break;
9920
9921 case DW_TAG_enumeration_type:
9922 {
9923 string qualified_name =
9924 rdr.get_die_qualified_type_name(die, where_offset);
9925 repr = "enum " + qualified_name;
9926 }
9927 break;
9928
9929 case DW_TAG_structure_type:
9930 case DW_TAG_class_type:
9931 {
9932 string qualified_name =
9933 rdr.get_die_qualified_type_name(die, where_offset);
9934 repr = "class " + qualified_name;
9935 }
9936 break;
9937
9938 case DW_TAG_union_type:
9939 {
9940 string qualified_name =
9941 rdr.get_die_qualified_type_name(die, where_offset);
9942 repr = "union " + qualified_name;
9943 }
9944 break;
9945
9946 case DW_TAG_array_type:
9947 {
9948 Dwarf_Die element_type_die;
9949 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9950 break;
9951 string element_type_name =
9952 rdr.get_die_qualified_type_name(&element_type_die, where_offset);
9953 if (element_type_name.empty())
9954 break;
9955
9957 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
9958 /*associate_type_to_die=*/false);
9959
9960 repr = element_type_name;
9961 repr += array_type_def::subrange_type::vector_as_string(subranges);
9962 }
9963 break;
9964
9965 case DW_TAG_subrange_type:
9966 {
9967 // So this can be generated by Ada, on its own; that is, not
9968 // as a subtype of an array. In that case we need to handle
9969 // it properly.
9970
9971 // For now, we consider that the pretty printed name of the
9972 // subrange type is its name. We might need something more
9973 // advance, should the needs of the users get more
9974 // complicated.
9975 repr += die_qualified_type_name(rdr, die, where_offset);
9976 }
9977 break;
9978
9979 case DW_TAG_subroutine_type:
9980 case DW_TAG_subprogram:
9981 {
9982 string return_type_name;
9983 string class_name;
9984 vector<string> parm_names;
9985 bool is_const = false;
9986 bool is_static = false;
9987
9988 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9989 /*pretty_print=*/true,
9990 return_type_name, class_name,
9991 parm_names, is_const,
9992 is_static);
9993 if (class_name.empty())
9994 repr = "function type";
9995 else
9996 repr = "method type";
9997 repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
9998 }
9999 break;
10000
10001 case DW_TAG_set_type:
10002 case DW_TAG_file_type:
10003 case DW_TAG_packed_type:
10004 case DW_TAG_thrown_type:
10005 case DW_TAG_interface_type:
10006 case DW_TAG_shared_type:
10008 }
10009
10010 return repr;
10011}
10012
10013/// Return a pretty string representation of a declaration, for
10014/// internal purposes.
10015///
10016/// By internal purpose, we mean things like key-ing declarations for
10017/// lookup purposes and so on.
10018///
10019/// Note that this function is also used to pretty print functions.
10020/// For functions, it prints the signature of the function.
10021///
10022/// @param rdr the context to use.
10023///
10024/// @param the DIE of the declaration to pretty print.
10025///
10026/// @param where_offset where we logically are placed when calling
10027/// this. It's useful to handle inclusion of DW_TAG_compile_unit
10028/// entries.
10029///
10030/// @return the resulting pretty representation.
10031static string
10032die_pretty_print_decl(reader& rdr,
10033 const Dwarf_Die* die,
10034 size_t where_offset)
10035{
10036 if (!die || !die_is_decl(die))
10037 return "";
10038
10039 string repr;
10040
10041 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10042 switch (tag)
10043 {
10044 case DW_TAG_namespace:
10045 repr = "namespace " + die_qualified_name(rdr, die, where_offset);
10046 break;
10047
10048 case DW_TAG_member:
10049 case DW_TAG_variable:
10050 {
10051 string type_repr = "void";
10052 Dwarf_Die type_die;
10053 if (die_die_attribute(die, DW_AT_type, type_die))
10054 type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
10055 repr = die_qualified_name(rdr, die, where_offset);
10056 if (!repr.empty())
10057 repr = type_repr + " " + repr;
10058 }
10059 break;
10060
10061 case DW_TAG_subprogram:
10062 repr = die_function_signature(rdr, die, where_offset);
10063 break;
10064
10065 default:
10066 break;
10067 }
10068 return repr;
10069}
10070
10071/// Compute the pretty printed representation of an artifact
10072/// represented by a DIE.
10073///
10074/// If the DIE is a type, compute the its pretty representation as a
10075/// type; otherwise, if it's a declaration, compute its pretty
10076/// representation as a declaration. Note for For instance, that a
10077/// DW_TAG_subprogram DIE is going to be represented as a function
10078/// *type*.
10079///
10080/// @param rdr the DWARF reader.
10081///
10082/// @param die the DIE to consider.
10083///
10084/// @param where_offset we in the DIE stream we are logically at.
10085///
10086/// @return a copy of the pretty printed artifact.
10087static string
10088die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
10089{
10090 if (die_is_type(die))
10091 return die_pretty_print_type(rdr, die, where_offset);
10092 else if (die_is_decl(die))
10093 return die_pretty_print_decl(rdr, die, where_offset);
10094 return "";
10095}
10096
10097// -----------------------------------
10098// </die pretty printer>
10099// -----------------------------------
10100
10101
10102// ----------------------------------
10103// <die comparison engine>
10104// ---------------------------------
10105
10106/// Compares two decls DIEs
10107///
10108/// This works only for DIEs emitted by the C language.
10109///
10110/// This implementation doesn't yet support namespaces.
10111///
10112/// This is a subroutine of compare_dies.
10113///
10114/// @return true iff @p l equals @p r.
10115static bool
10116compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10117{
10118 ABG_ASSERT(l && r);
10119
10120 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10121 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10122 if (l_tag != r_tag)
10123 return false;
10124
10125 bool result = false;
10126
10127 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
10128 {
10129 // Fast path for functions and global variables.
10130 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
10131 result)
10132 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
10133 result))
10134 {
10135 if (!result)
10136 return false;
10137 }
10138
10139 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10140 result))
10141 {
10142 if (!result)
10143 return false;
10144 }
10145 return true;
10146 }
10147
10148 // Fast path for types.
10149 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10150 result))
10151 return result;
10152 return true;
10153}
10154
10155/// Test if at least one of two ODR-relevant DIEs is decl-only.
10156///
10157/// @param rdr the DWARF reader to consider.
10158///
10159/// @param l the first type DIE to consider.
10160///
10161/// @param r the second type DIE to consider.
10162///
10163/// @return true iff either @p l or @p r is decl-only and both are
10164/// ODR-relevant.
10165static bool
10166at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
10167 const Dwarf_Die *l,
10168 const Dwarf_Die *r)
10169{
10170 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
10171 return false;
10172
10173 if ((die_is_declaration_only(l) && die_has_no_child(l))
10174 || (die_is_declaration_only(r) && die_has_no_child(r)))
10175 return true;
10176 return false;
10177}
10178
10179/// Compares two type DIEs
10180///
10181/// This is a subroutine of compare_dies.
10182///
10183/// Note that this function doesn't look at the name of the DIEs.
10184/// Naming is taken into account by the function compare_as_decl_dies.
10185///
10186/// If the two DIEs are from a translation unit that is subject to the
10187/// ONE Definition Rule, then the function considers that if one DIE
10188/// is a declaration, then it's equivalent to the second. In that
10189/// case, the sizes of the two DIEs are not compared. This is so that
10190/// a declaration of a type compares equal to the definition of the
10191/// type.
10192///
10193/// @param rdr the DWARF reader to consider.
10194///
10195/// @param l the left operand of the comparison operator.
10196///
10197/// @param r the right operand of the comparison operator.
10198///
10199/// @return true iff @p l equals @p r.
10200static bool
10201compare_as_type_dies(const reader& rdr,
10202 const Dwarf_Die *l,
10203 const Dwarf_Die *r)
10204{
10205 ABG_ASSERT(l && r);
10206 ABG_ASSERT(die_is_type(l));
10207 ABG_ASSERT(die_is_type(r));
10208
10209 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10210 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10211 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10212 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10213 // For now, we cannot compare DW_TAG_string_type because of its
10214 // string_length attribute that is a location descriptor that is
10215 // not necessarily a constant. So it's super hard to evaluate it
10216 // in a libabigail context. So for now, we just say that all
10217 // DW_TAG_string_type DIEs are different, by default.
10218 return false;
10219
10220 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10221 // A declaration of a type compares equal to the definition of the
10222 // type.
10223 return true;
10224
10225 uint64_t l_size = 0, r_size = 0;
10226 die_size_in_bits(l, l_size);
10227 die_size_in_bits(r, r_size);
10228
10229 return l_size == r_size;
10230}
10231
10232/// Compare two DIEs as decls (looking as their names etc) and as
10233/// types (looking at their size etc).
10234///
10235/// @param rdr the DWARF reader to consider.
10236///
10237/// @param l the first DIE to consider.
10238///
10239/// @param r the second DIE to consider.
10240///
10241/// @return TRUE iff @p l equals @p r as far as naming and size is
10242/// concerned.
10243static bool
10244compare_as_decl_and_type_dies(const reader &rdr,
10245 const Dwarf_Die *l,
10246 const Dwarf_Die *r)
10247{
10248 if (!compare_as_decl_dies(l, r)
10249 || !compare_as_type_dies(rdr, l, r))
10250 return false;
10251
10252 return true;
10253}
10254
10255/// Test if two DIEs representing function declarations have the same
10256/// linkage name, and thus are considered equal if they are C or C++,
10257/// because the two DIEs represent functions in the same binary.
10258///
10259/// If the DIEs don't have a linkage name, the function compares their
10260/// name. But in that case, the caller of the function must know that
10261/// in C++ for instance, that doesn't imply that the two functions are
10262/// equal.
10263///
10264/// @param rdr the @ref reader to consider.
10265///
10266/// @param l the first function DIE to consider.
10267///
10268/// @param r the second function DIE to consider.
10269///
10270/// @return true iff the function represented by @p l have the same
10271/// linkage name as the function represented by @p r.
10272static bool
10273fn_die_equal_by_linkage_name(const reader &rdr,
10274 const Dwarf_Die *l,
10275 const Dwarf_Die *r)
10276{
10277 if (!!l != !!r)
10278 return false;
10279
10280 if (!l)
10281 return false;
10282
10283 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10284 ABG_ASSERT(tag == DW_TAG_subprogram);
10285 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10286 ABG_ASSERT(tag == DW_TAG_subprogram);
10287
10288 string lname = die_name(l), rname = die_name(r);
10289 string llinkage_name = die_linkage_name(l),
10290 rlinkage_name = die_linkage_name(r);
10291
10292 if (rdr.die_is_in_c_or_cplusplus(l)
10293 && rdr.die_is_in_c_or_cplusplus(r))
10294 {
10295 if (!llinkage_name.empty() && !rlinkage_name.empty())
10296 return llinkage_name == rlinkage_name;
10297 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10298 return false;
10299 else
10300 return lname == rname;
10301 }
10302
10303 return (!llinkage_name.empty()
10304 && !rlinkage_name.empty()
10305 && llinkage_name == rlinkage_name);
10306}
10307
10308/// Compare two DIEs in the context of DIE canonicalization.
10309///
10310/// If DIE canonicalization is on, the function compares the DIEs
10311/// canonically and structurally. The two types of comparison should
10312/// be equal, of course.
10313///
10314/// @param rdr the DWARF reader.
10315///
10316/// @param l_offset the offset of the first canonical DIE to compare.
10317///
10318/// @param r_offset the offset of the second canonical DIE to compare.
10319///
10320/// @param l_die_source the source of the DIE denoted by the offset @p
10321/// l_offset.
10322///
10323/// @param r_die_source the source of the DIE denoted by the offset @p
10324/// r_offset.
10325///
10326/// @param l_has_canonical_die_offset output parameter. Is set to
10327/// true if @p l_offset has a canonical DIE.
10328///
10329/// @param r_has_canonical_die_offset output parameter. Is set to
10330/// true if @p r_offset has a canonical DIE.
10331///
10332/// @param l_canonical_die_offset output parameter. If @p
10333/// l_has_canonical_die_offset is set to true, then this parameter is
10334/// set to the offset of the canonical DIE of the DIE designated by @p
10335/// l_offset.
10336static bool
10337try_canonical_die_comparison(const reader& rdr,
10338 Dwarf_Off l_offset, Dwarf_Off r_offset,
10339 die_source l_die_source, die_source r_die_source,
10340 bool& l_has_canonical_die_offset,
10341 bool& r_has_canonical_die_offset,
10342 Dwarf_Off& l_canonical_die_offset,
10343 Dwarf_Off& r_canonical_die_offset,
10344 bool& result)
10345{
10346#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10347 if (rdr.debug_die_canonicalization_is_on_
10348 && !rdr.use_canonical_die_comparison_)
10349 return false;
10350#endif
10351
10352
10353 l_has_canonical_die_offset =
10354 (l_canonical_die_offset =
10355 rdr.get_canonical_die_offset(l_offset, l_die_source,
10356 /*die_as_type=*/true));
10357
10358 r_has_canonical_die_offset =
10359 (r_canonical_die_offset =
10360 rdr.get_canonical_die_offset(r_offset, r_die_source,
10361 /*die_as_type=*/true));
10362
10363 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10364 {
10365 result = (l_canonical_die_offset == r_canonical_die_offset);
10366 return true;
10367 }
10368
10369 return false;
10370}
10371
10372#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10373/// This function is called whenever a DIE comparison fails.
10374///
10375/// This function is intended for debugging purposes. The idea is for
10376/// hackers to set a breakpoint on this function so that they can
10377/// discover why exactly the comparison failed. They then can execute
10378/// the program from compare_dies_during_canonicalization, for
10379/// instance.
10380///
10381/// @param @l the left-hand side of the DIE comparison.
10382///
10383/// @param @r the right-hand side of the DIE comparison.
10384static void
10385notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10386{
10387}
10388
10389#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10390 notify_die_comparison_failed(l, r)
10391#else
10392#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10393#endif
10394
10395/// A macro used to return from DIE comparison routines.
10396///
10397/// If the return value is false, the macro invokes the
10398/// notify_die_comparison_failed signalling function before returning.
10399/// That way, hackers willing to learn more about why the comparison
10400/// routine returned "false" can just set a breakpoint on
10401/// notify_die_comparison_failed and execute the program from
10402/// compare_dies_during_canonicalization, for instance.
10403///
10404/// @param value the value to return from the DIE comparison routines.
10405#define ABG_RETURN(value) \
10406 do \
10407 { \
10408 if ((value) == COMPARISON_RESULT_DIFFERENT) \
10409 { \
10410 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10411 } \
10412 return return_comparison_result(l, r, dies_being_compared, \
10413 value, aggregates_being_compared, \
10414 update_canonical_dies_on_the_fly); \
10415 } \
10416 while(false)
10417
10418/// A macro used to return the "false" boolean from DIE comparison
10419/// routines.
10420///
10421/// As the return value is false, the macro invokes the
10422/// notify_die_comparison_failed signalling function before returning.
10423///
10424/// @param value the value to return from the DIE comparison routines.
10425#define ABG_RETURN_FALSE \
10426 do \
10427 { \
10428 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10429 return return_comparison_result(l, r, dies_being_compared, \
10430 COMPARISON_RESULT_DIFFERENT, \
10431 aggregates_being_compared, \
10432 update_canonical_dies_on_the_fly); \
10433 } while(false)
10434
10435/// A macro to set the 'result' variable to 'false'.
10436///
10437/// The macro invokes the notify_die_comparison_failed function so
10438/// that the hacker can set a debugging breakpoint on
10439/// notify_die_comparison_failed to know where a DIE comparison failed
10440/// during compare_dies_during_canonicalization for instance.
10441///
10442/// @param result the 'result' variable to set.
10443///
10444/// @param l the first DIE of the comparison operation.
10445///
10446/// @param r the second DIE of the comparison operation.
10447#define SET_RESULT_TO_FALSE(result, l , r) \
10448 do \
10449 { \
10450 result = COMPARISON_RESULT_DIFFERENT; \
10451 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10452 } while(false)
10453
10454/// A macro to set the 'result' variable to a given value.
10455///
10456/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10457/// invokes the notify_die_comparison_failed function so that the
10458/// hacker can set a debugging breakpoint on
10459/// notify_die_comparison_failed to know where a DIE comparison failed
10460/// during compare_dies_during_canonicalization for instance.
10461///
10462/// @param result the 'result' variable to set.
10463///
10464/// @param l the first DIE of the comparison operation.
10465///
10466/// @param r the second DIE of the comparison operation.
10467#define SET_RESULT_TO(result, value, l , r) \
10468 do \
10469 { \
10470 result = (value); \
10471 if (result == COMPARISON_RESULT_DIFFERENT) \
10472 { \
10473 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10474 } \
10475 } while(false)
10476
10477#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10478 do \
10479 { \
10480 if (aggregates_being_compared.contains(dies_being_compared)) \
10481 { \
10482 result = COMPARISON_RESULT_CYCLE_DETECTED; \
10483 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10484 ABG_RETURN(result); \
10485 } \
10486 } \
10487 while(false)
10488
10489/// Get the next member sibling of a given class or union member DIE.
10490///
10491/// @param die the DIE to consider.
10492///
10493/// @param member out parameter. This is set to the next member
10494/// sibling, iff the function returns TRUE.
10495///
10496/// @return TRUE iff the function set @p member to the next member
10497/// sibling DIE.
10498static bool
10499get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10500{
10501 if (!die)
10502 return false;
10503
10504 bool found_member = false;
10505 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10506 member) == 0);
10507 found_member;
10508 found_member = (dwarf_siblingof(member, member) == 0))
10509 {
10510 int tag = dwarf_tag(member);
10511 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10512 break;
10513 }
10514
10515 return found_member;
10516}
10517
10518/// Get the first child DIE of a class/struct/union DIE that is a
10519/// member DIE.
10520///
10521/// @param die the DIE to consider.
10522///
10523/// @param child out parameter. This is set to the first child DIE of
10524/// @p iff this function returns TRUE.
10525///
10526/// @return TRUE iff @p child is set to the first child DIE of @p die
10527/// that is a member DIE.
10528static bool
10529get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10530{
10531 if (!die)
10532 return false;
10533
10534 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10535 ABG_ASSERT(tag == DW_TAG_structure_type
10536 || tag == DW_TAG_union_type
10537 || tag == DW_TAG_class_type);
10538
10539 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10540 child) == 0);
10541
10542 if (!found_child)
10543 return false;
10544
10545 tag = dwarf_tag(child);
10546
10547 if (!(tag == DW_TAG_member
10548 || tag == DW_TAG_inheritance
10549 || tag == DW_TAG_subprogram))
10550 found_child = get_next_member_sibling_die(child, child);
10551
10552 return found_child;
10553}
10554
10555/// This is a sub-routine of return_comparison_result.
10556///
10557/// Propagate the canonical type of a the right-hand-side DIE to the
10558/// lef-hand-side DIE. This is a optimization that is done when the
10559/// two DIEs compare equal.
10560///
10561/// If the right-hand-side DIE is not canonicalized, the function
10562/// performs its canonicalization.
10563///
10564/// This optimization is performed only if
10565/// is_canon_type_to_be_propagated_tag returns true.
10566///
10567/// @param rdr the current context to consider.
10568///
10569/// @param l the left-hand-side DIE of the comparison. It's going to
10570/// receive the canonical type of the other DIE.
10571///
10572/// @param r the right-hand-side DIE of the comparison. Its canonical
10573/// type is propagated to @p l.
10574static void
10575maybe_propagate_canonical_type(const reader& rdr,
10576 const Dwarf_Die* l,
10577 const Dwarf_Die* r)
10578{
10579 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10580 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10581
10582 if (l_tag != r_tag)
10583 return;
10584
10585 if (is_canon_type_to_be_propagated_tag(l_tag))
10586 propagate_canonical_type(rdr, l, r);
10587}
10588
10589/// Propagate the canonical type of a the right-hand-side DIE to the
10590/// left-hand-side DIE. This is a optimization that is done when the
10591/// two DIEs compare equal.
10592///
10593/// If the right-hand-side DIE is not canonicalized, the function
10594/// performs its canonicalization.
10595///
10596/// @param rdr the current context to consider.
10597///
10598/// @param l the left-hand-side DIE of the comparison. It's going to
10599/// receive the canonical type of the other DIE.
10600///
10601/// @param r the right-hand-side DIE of the comparison. Its canonical
10602/// type is propagated to @p l.
10603static void
10604propagate_canonical_type(const reader& rdr,
10605 const Dwarf_Die* l,
10606 const Dwarf_Die* r)
10607{
10608 ABG_ASSERT(l && r);
10609
10610 // If 'l' has no canonical DIE and if 'r' has one, then propagage
10611 // the canonical DIE of 'r' to 'l'.
10612 //
10613 // In case 'r' has no canonical DIE, then compute it, and then
10614 // propagate that canonical DIE to 'r'.
10615 const die_source l_source = rdr.get_die_source(l);
10616 const die_source r_source = rdr.get_die_source(r);
10617
10618 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10619 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10620 bool l_has_canonical_die_offset = false;
10621 bool r_has_canonical_die_offset = false;
10622 Dwarf_Off l_canonical_die_offset = 0;
10623 Dwarf_Off r_canonical_die_offset = 0;
10624
10625 l_has_canonical_die_offset =
10626 (l_canonical_die_offset =
10627 rdr.get_canonical_die_offset(l_offset, l_source,
10628 /*die_as_type=*/true));
10629
10630 r_has_canonical_die_offset =
10631 (r_canonical_die_offset =
10632 rdr.get_canonical_die_offset(r_offset, r_source,
10633 /*die_as_type=*/true));
10634
10635
10636 if (!l_has_canonical_die_offset
10637 && r_has_canonical_die_offset
10638 // A DIE can be equivalent only to another DIE of the same
10639 // source.
10640 && l_source == r_source)
10641 {
10642 ABG_ASSERT(r_canonical_die_offset);
10643 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10644 /*die_as_type=*/true);
10645 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10646 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10647 rdr.canonical_propagated_count_++;
10648 }
10649}
10650
10651/// This function does the book keeping of comparison pairs necessary
10652/// to handle
10653///
10654/// * the detection of cycles during the comparison of aggregate
10655/// types, in conjuction with the macro
10656/// RETURN_IF_COMPARISON_CYCLE_DETECTED
10657///
10658/// * the handling of the canonical type propagation optimisation
10659/// to speed-up type canonicalization.
10660///
10661///
10662/// Note that this function is essentially a sub-routine of
10663/// compare_dies.
10664///
10665/// @param l the left-hand-side DIE being compared.
10666///
10667/// @param r the right-hand-side DIE being compared.
10668///
10669/// @param cur_dies the pair of die offsets of l and r. This is
10670/// redundant as it can been computed from @p l and @p r. However,
10671/// getting it as an argument is an optimization to avoid computing it
10672/// over and over again, given how often this function is invoked from
10673/// compare_dies.
10674///
10675/// @param return the result of comparing @p l against @p r.
10676///
10677/// @param comparison_stack the stack of pair of type DIEs being
10678/// compared.
10679///
10680/// @param do_propagate_canonical_type if true then the function
10681/// performs canonical DIEs propagation, meaning that if @p l equals
10682/// @p r and if @p r has a canonical type, then the canonical type of
10683/// @p l is set to the canonical type of @p r.
10684static comparison_result
10685return_comparison_result(const Dwarf_Die* l,
10686 const Dwarf_Die* r,
10687 const offset_pair_type& cur_dies,
10688 comparison_result result,
10689 offset_pairs_stack_type& comparison_stack,
10690 bool do_propagate_canonical_type = true)
10691{
10692 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10693
10694 if (result == COMPARISON_RESULT_EQUAL)
10695 {
10696 // The result comparing the two types is "true", basically. So
10697 // let's propagate the canonical type of r onto l, so that we
10698 // don't need to compute the canonical type of r.
10699 if (do_propagate_canonical_type)
10700 {
10701 // Propagate canonical type.
10702 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10703
10704 // TODO: do we need to confirm any tentative canonical
10705 // propagation?
10706 }
10707 }
10708 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10709 {
10710 // So upon detection of the comparison cycle, compare_dies
10711 // returned early with the comparison result
10712 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
10713 // carry on with the comparison of all the OTHER sub-types of
10714 // the redundant type. If they all compare equal, then it means
10715 // the redundant type pair compared equal. Otherwise, it
10716 // compared different.
10717 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
10718 // Let's fall through to let the end of this function set the
10719 // result to COMPARISON_RESULT_UNKNOWN;
10720 }
10721 else if (result == COMPARISON_RESULT_UNKNOWN)
10722 {
10723 // Here is an introductory comment describing what we are going
10724 // to do in this case where the result of the comparison of the
10725 // current pair of type is not "false", basically.
10726 //
10727 // This means that we don't yet know what the result of
10728 // comparing these two types is, because one of the sub-types of
10729 // the types being compared is "redundant", meaning it appears
10730 // more than once in the comparison stack, so if we were to
10731 // naively try to carry on with the comparison member-wise, we'd
10732 // end up with an endless loop, a.k.a "comparison cycle".
10733 //
10734 // If the current type pair is redundant then:
10735 //
10736 // * This is a redundant type that has just been fully
10737 // compared. In that case, all the types that depend on
10738 // this redundant type and that have been tentatively
10739 // canonical-type-propagated must see their canonical types
10740 // "confirmed". This means that this type is going to be
10741 // considered as not being redundant anymore, meaning all
10742 // the types that depend on it must be updated as not being
10743 // dependant on it anymore, and the type itsef must be
10744 // removed from the map of redundant types.
10745 //
10746 // After the type's canonical-type-propagation is confirmed,
10747 // the result of its comparison must also be changed into
10748 // COMPARISON_RESULT_EQUAL.
10749 //
10750 // After that, If the current type depends on a redundant type,
10751 // then propagate its canonical type AND track it as having its
10752 // type being canonical-type-propagated.
10753 //
10754 // If the current type is not redundant however, then it must be
10755 // dependant on a redundant type. If it's not dependant on a
10756 // redundant type, then it must be of those types which
10757 // comparisons are not tracked for cycle, probably because they
10758 // are not aggregates. Otherwise, ABORT to understand why. I
10759 // believe this should not happen. In any case, after that
10760 // safety check is passed, we just need to return at this point.
10761
10762 if (comparison_stack.is_redundant(cur_dies)
10763 && comparison_stack.vect_.back() == cur_dies)
10764 {
10765 // We are in the case described above of a redundant type
10766 // that has been fully compared.
10767 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10768 comparison_stack.confirm_canonical_propagated_type(cur_dies);
10769
10770 result = COMPARISON_RESULT_EQUAL;
10771 }
10772 else if (is_canon_type_to_be_propagated_tag(l_tag)
10773 && comparison_stack.vect_.back() == cur_dies)
10774 {
10775 // The current type is not redundant. So, as described in
10776 // the introductory comment above, it must be dependant on a
10777 // redundant type.
10778 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
10779 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10780 // Then pass through.
10781 }
10782 }
10783 else if (result == COMPARISON_RESULT_DIFFERENT)
10784 {
10785 // Here is an introductory comment describing what we are going
10786 // to do in this case where the result of the comparison of the
10787 // current pair of type is "false", basically.
10788 //
10789 // If the type pair {l,r} is redundant then cancel the
10790 // canonical-type-propagation of all the dependant pairs that
10791 // depends on this redundant {l, r}. This means walk the types
10792 // that depends on {l, r} and cancel their
10793 // canonical-propagate-type, that means remove their canonical
10794 // types and mark them as not being canonically-propagated.
10795 // Also, erase their cached comparison results that was likely
10796 // set to COMPARISON_RESULT_UNKNOWN.
10797 //
10798 // Also, update the cached result for this pair, that was likely
10799 // to be COMPARISON_RESULT_UNKNOWN.
10800 if (comparison_stack.is_redundant(cur_dies)
10801 && comparison_stack.vect_.back() == cur_dies)
10802 comparison_stack.cancel_canonical_propagated_type(cur_dies);
10803 }
10804 else
10805 {
10806 // We should never reach here.
10808 }
10809
10810 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10811 result = COMPARISON_RESULT_UNKNOWN;
10812 else if (is_canon_type_to_be_propagated_tag(l_tag)
10813 && !comparison_stack.vect_.empty()
10814 && comparison_stack.vect_.back() == cur_dies)
10815 //Finally pop the pair types being compared from comparison_stack
10816 //iff {l,r} is on the top of the stack. If it's not, then it means
10817 //we are looking at a type that was detected as a being redundant
10818 //and thus hasn't been pushed to the stack yet gain.
10819 comparison_stack.erase(cur_dies);
10820
10821 maybe_cache_type_comparison_result(comparison_stack.rdr_,
10822 l_tag, cur_dies, result);
10823
10824 return result;
10825}
10826
10827/// Compare two DIEs emitted by a C compiler.
10828///
10829/// @param rdr the DWARF reader used to load the DWARF information.
10830///
10831/// @param l the left-hand-side argument of this comparison operator.
10832///
10833/// @param r the righ-hand-side argument of this comparison operator.
10834///
10835/// @param aggregates_being_compared this holds the names of the set
10836/// of aggregates being compared. It's used by the comparison
10837/// function to avoid recursing infinitely when faced with types
10838/// referencing themselves through pointers or references. By
10839/// default, just pass an empty instance of @ref istring_set_type to
10840/// it.
10841///
10842/// @param update_canonical_dies_on_the_fly if true, when two
10843/// sub-types compare equal (during the comparison of @p l and @p r)
10844/// update their canonical type. That way, two types of the same name
10845/// are structurally compared to each other only once. So the
10846/// non-linear structural comparison of two types of the same name
10847/// only happen once.
10848///
10849/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
10850static comparison_result
10851compare_dies(const reader& rdr,
10852 const Dwarf_Die *l, const Dwarf_Die *r,
10853 offset_pairs_stack_type& aggregates_being_compared,
10854 bool update_canonical_dies_on_the_fly)
10855{
10856 ABG_ASSERT(l);
10857 ABG_ASSERT(r);
10858
10859 const die_source l_die_source = rdr.get_die_source(l);
10860 const die_source r_die_source = rdr.get_die_source(r);
10861
10862 offset_type l_offset =
10863 {
10864 l_die_source,
10865 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10866 };
10867
10868 offset_type r_offset =
10869 {
10870 r_die_source,
10871 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
10872 };
10873
10874 offset_pair_type dies_being_compared(l_offset, r_offset);
10875
10876 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10877 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10878
10879 if (l_tag != r_tag)
10881
10882 if (l_offset == r_offset)
10883 return COMPARISON_RESULT_EQUAL;
10884
10885 if (rdr.leverage_dwarf_factorization()
10886 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
10887 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
10888 if (l_offset != r_offset)
10889 return COMPARISON_RESULT_DIFFERENT;
10890
10891 comparison_result result = COMPARISON_RESULT_EQUAL;
10892 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
10893 dies_being_compared,
10894 result))
10895 return result;
10896
10897 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10898 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
10899
10900 // If 'l' and 'r' already have canonical DIEs, then just compare the
10901 // offsets of their canonical DIEs.
10902 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
10903 {
10904 bool canonical_compare_result = false;
10905 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
10906 l_die_source, r_die_source,
10907 l_has_canonical_die_offset,
10908 r_has_canonical_die_offset,
10909 l_canonical_die_offset,
10910 r_canonical_die_offset,
10911 canonical_compare_result))
10912 {
10913 comparison_result result;
10914 SET_RESULT_TO(result,
10915 (canonical_compare_result
10916 ? COMPARISON_RESULT_EQUAL
10917 : COMPARISON_RESULT_DIFFERENT),
10918 l, r);
10919 return result;
10920 }
10921 }
10922
10923
10924
10925 switch (l_tag)
10926 {
10927 case DW_TAG_base_type:
10928 case DW_TAG_string_type:
10929 case DW_TAG_unspecified_type:
10930 if (!compare_as_decl_and_type_dies(rdr, l, r))
10931 SET_RESULT_TO_FALSE(result, l, r);
10932 break;
10933
10934 case DW_TAG_typedef:
10935 case DW_TAG_pointer_type:
10936 case DW_TAG_reference_type:
10937 case DW_TAG_rvalue_reference_type:
10938 case DW_TAG_const_type:
10939 case DW_TAG_volatile_type:
10940 case DW_TAG_restrict_type:
10941 {
10942 if (!compare_as_type_dies(rdr, l, r))
10943 {
10944 SET_RESULT_TO_FALSE(result, l, r);
10945 break;
10946 }
10947
10948 bool from_the_same_tu = false;
10949 if (!pointer_or_qual_die_of_anonymous_class_type(l)
10950 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
10951 && from_the_same_tu)
10952 {
10953 // These two typedefs, pointer, reference, or qualified
10954 // types have the same name and are defined in the same TU.
10955 // They thus ought to be the same.
10956 //
10957 // Note that pointers, reference or qualified types to
10958 // anonymous types are not taking into account here because
10959 // those always need to be structurally compared.
10960 SET_RESULT_TO_FALSE(result, l, r);
10961 break;
10962 }
10963 }
10964
10965 {
10966 // No fancy optimization in this case. We need to
10967 // structurally compare the two DIEs.
10968 Dwarf_Die lu_type_die, ru_type_die;
10969 bool lu_is_void, ru_is_void;
10970
10971 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
10972 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
10973
10974 if (lu_is_void && ru_is_void)
10975 result = COMPARISON_RESULT_EQUAL;
10976 else if (lu_is_void != ru_is_void)
10977 SET_RESULT_TO_FALSE(result, l, r);
10978 else
10979 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
10980 aggregates_being_compared,
10981 update_canonical_dies_on_the_fly);
10982 }
10983 break;
10984
10985 case DW_TAG_enumeration_type:
10986 if (!compare_as_decl_and_type_dies(rdr, l, r))
10987 SET_RESULT_TO_FALSE(result, l, r);
10988 else
10989 {
10990 // Walk the enumerators.
10991 Dwarf_Die l_enumtor, r_enumtor;
10992 bool found_l_enumtor = true, found_r_enumtor = true;
10993
10994 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10995 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
10996 &l_enumtor) == 0,
10997 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
10998 &r_enumtor) == 0;
10999 found_l_enumtor && found_r_enumtor;
11000 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
11001 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
11002 {
11003 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
11004 if ( l_tag != r_tag)
11005 {
11006 SET_RESULT_TO_FALSE(result, l, r);
11007 break;
11008 }
11009
11010 if (l_tag != DW_TAG_enumerator)
11011 continue;
11012
11013 uint64_t l_val = 0, r_val = 0;
11014 die_unsigned_constant_attribute(&l_enumtor,
11015 DW_AT_const_value,
11016 l_val);
11017 die_unsigned_constant_attribute(&r_enumtor,
11018 DW_AT_const_value,
11019 r_val);
11020 if (l_val != r_val)
11021 {
11022 SET_RESULT_TO_FALSE(result, l, r);
11023 break;
11024 }
11025 }
11026 if (found_l_enumtor != found_r_enumtor )
11027 SET_RESULT_TO_FALSE(result, l, r);
11028 }
11029 break;
11030
11031 case DW_TAG_structure_type:
11032 case DW_TAG_union_type:
11033 case DW_TAG_class_type:
11034 {
11035 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11036
11037 rdr.compare_count_++;
11038
11039 if (!compare_as_decl_and_type_dies(rdr, l, r))
11040 SET_RESULT_TO_FALSE(result, l, r);
11041 else if (rdr.options().assume_odr_for_cplusplus
11042 && rdr.odr_is_relevant(l)
11043 && rdr.odr_is_relevant(r)
11044 && !die_is_anonymous(l)
11045 && !die_is_anonymous(r))
11046 result = COMPARISON_RESULT_EQUAL;
11047 else
11048 {
11049 aggregates_being_compared.add(dies_being_compared);
11050
11051 Dwarf_Die l_member, r_member;
11052 bool found_l_member = true, found_r_member = true;
11053
11054 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
11055 for (found_l_member = get_member_child_die(l, &l_member),
11056 found_r_member = get_member_child_die(r, &r_member);
11057 found_l_member && found_r_member;
11058 found_l_member = get_next_member_sibling_die(&l_member,
11059 &l_member),
11060 found_r_member = get_next_member_sibling_die(&r_member,
11061 &r_member))
11062 {
11063 int l_tag = dwarf_tag(&l_member),
11064 r_tag = dwarf_tag(&r_member);
11065
11066 if (l_tag != r_tag)
11067 {
11068 SET_RESULT_TO_FALSE(result, l, r);
11069 break;
11070 }
11071
11072 ABG_ASSERT(l_tag == DW_TAG_member
11073 || l_tag == DW_TAG_variable
11074 || l_tag == DW_TAG_inheritance
11075 || l_tag == DW_TAG_subprogram);
11076
11077 comparison_result local_result =
11078 compare_dies(rdr, &l_member, &r_member,
11079 aggregates_being_compared,
11080 update_canonical_dies_on_the_fly);
11081
11082 if (local_result == COMPARISON_RESULT_UNKNOWN)
11083 // Note that if the result of comparing any
11084 // sub-type is COMPARISON_RESULT_EQUAL, just
11085 // because we have at least one sub-type's
11086 // comparison being COMPARISON_RESULT_UNKNOWN
11087 // means that the comparison of this type will
11088 // return COMPARISON_RESULT_UNKNOWN to show
11089 // callers that this type (and all the types that
11090 // depend on it) depends on a redundant type
11091 result = local_result;
11092
11093 if (local_result == COMPARISON_RESULT_DIFFERENT)
11094 {
11095 SET_RESULT_TO_FALSE(result, l, r);
11096 break;
11097 }
11098 }
11099 if (found_l_member != found_r_member)
11100 {
11101 SET_RESULT_TO_FALSE(result, l, r);
11102 break;
11103 }
11104 }
11105 }
11106 break;
11107
11108 case DW_TAG_array_type:
11109 {
11110 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11111
11112 aggregates_being_compared.add(dies_being_compared);
11113
11114 rdr.compare_count_++;
11115
11116 Dwarf_Die l_child, r_child;
11117 bool found_l_child, found_r_child;
11118 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11119 &l_child) == 0,
11120 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11121 &r_child) == 0;
11122 found_l_child && found_r_child;
11123 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
11124 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
11125 {
11126 int l_child_tag = dwarf_tag(&l_child),
11127 r_child_tag = dwarf_tag(&r_child);
11128 if (l_child_tag == DW_TAG_subrange_type
11129 || r_child_tag == DW_TAG_subrange_type)
11130 {
11131 result = compare_dies(rdr, &l_child, &r_child,
11132 aggregates_being_compared,
11133 update_canonical_dies_on_the_fly);
11134 if (!result)
11135 {
11136 SET_RESULT_TO_FALSE(result, l, r);
11137 break;
11138 }
11139 }
11140 }
11141 if (found_l_child != found_r_child)
11142 SET_RESULT_TO_FALSE(result, l, r);
11143 // Compare the types of the elements of the array.
11144 Dwarf_Die ltype_die, rtype_die;
11145 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
11146 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
11147 ABG_ASSERT(found_ltype && found_rtype);
11148
11149 result = compare_dies(rdr, &ltype_die, &rtype_die,
11150 aggregates_being_compared,
11151 update_canonical_dies_on_the_fly);
11152 if (!result)
11154 }
11155 break;
11156
11157 case DW_TAG_subrange_type:
11158 {
11159 uint64_t l_lower_bound = 0, r_lower_bound = 0,
11160 l_upper_bound = 0, r_upper_bound = 0;
11161 bool l_lower_bound_set = false, r_lower_bound_set = false,
11162 l_upper_bound_set = false, r_upper_bound_set = false;
11163
11164 l_lower_bound_set =
11165 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
11166 r_lower_bound_set =
11167 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
11168
11169 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
11170 l_upper_bound))
11171 {
11172 uint64_t l_count = 0;
11173 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
11174 {
11175 l_upper_bound = l_lower_bound + l_count;
11176 l_upper_bound_set = true;
11177 if (l_upper_bound)
11178 --l_upper_bound;
11179 }
11180 }
11181 else
11182 l_upper_bound_set = true;
11183
11184 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
11185 r_upper_bound))
11186 {
11187 uint64_t r_count = 0;
11188 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
11189 {
11190 r_upper_bound = r_lower_bound + r_count;
11191 r_upper_bound_set = true;
11192 if (r_upper_bound)
11193 --r_upper_bound;
11194 }
11195 }
11196 else
11197 r_upper_bound_set = true;
11198
11199 if ((l_lower_bound_set != r_lower_bound_set)
11200 || (l_upper_bound_set != r_upper_bound_set)
11201 || (l_lower_bound != r_lower_bound)
11202 || (l_upper_bound != r_upper_bound))
11203 SET_RESULT_TO_FALSE(result, l, r);
11204 }
11205 break;
11206
11207 case DW_TAG_subroutine_type:
11208 case DW_TAG_subprogram:
11209 {
11210 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11211
11212 aggregates_being_compared.add(dies_being_compared);
11213
11214 rdr.compare_count_++;
11215
11216 if (l_tag == DW_TAG_subprogram
11217 && !fn_die_equal_by_linkage_name(rdr, l, r))
11218 {
11219 SET_RESULT_TO_FALSE(result, l, r);
11220 break;
11221 }
11222 else if (l_tag == DW_TAG_subprogram
11223 && rdr.die_is_in_c(l) && rdr.die_is_in_c(r)
11224 /*&& fn_die_equal_by_linkage_name(rdr, l, r)*/)
11225 {
11226 result = COMPARISON_RESULT_EQUAL;
11227 break;
11228 }
11229 else if (!rdr.die_is_in_c(l) && !rdr.die_is_in_c(r))
11230 {
11231 // In C, we cannot have two different functions with the
11232 // same linkage name in a given binary. But here we are
11233 // looking at DIEs that don't originate from C. So we
11234 // need to compare return types and parameter types.
11235 Dwarf_Die l_return_type, r_return_type;
11236 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
11237 l_return_type);
11238 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
11239 r_return_type);
11240 if (l_return_type_is_void != r_return_type_is_void
11241 || (!l_return_type_is_void
11242 && !compare_dies(rdr,
11243 &l_return_type, &r_return_type,
11244 aggregates_being_compared,
11245 update_canonical_dies_on_the_fly)))
11246 SET_RESULT_TO_FALSE(result, l, r);
11247 else
11248 {
11249 Dwarf_Die l_child, r_child;
11250 bool found_l_child, found_r_child;
11251 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11252 &l_child) == 0,
11253 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11254 &r_child) == 0;
11255 found_l_child && found_r_child;
11256 found_l_child = dwarf_siblingof(&l_child,
11257 &l_child) == 0,
11258 found_r_child = dwarf_siblingof(&r_child,
11259 &r_child)==0)
11260 {
11261 int l_child_tag = dwarf_tag(&l_child);
11262 int r_child_tag = dwarf_tag(&r_child);
11263 comparison_result local_result =
11264 COMPARISON_RESULT_EQUAL;
11265 if (l_child_tag != r_child_tag)
11266 local_result = COMPARISON_RESULT_DIFFERENT;
11267 if (l_child_tag == DW_TAG_formal_parameter)
11268 local_result =
11269 compare_dies(rdr, &l_child, &r_child,
11270 aggregates_being_compared,
11271 update_canonical_dies_on_the_fly);
11272 if (local_result == COMPARISON_RESULT_DIFFERENT)
11273 {
11274 result = local_result;
11275 SET_RESULT_TO_FALSE(result, l, r);
11276 break;
11277 }
11278 if (local_result == COMPARISON_RESULT_UNKNOWN)
11279 // Note that if the result of comparing any
11280 // sub-type is COMPARISON_RESULT_EQUAL, just
11281 // because we have at least one sub-type's
11282 // comparison being COMPARISON_RESULT_UNKNOWN
11283 // means that the comparison of this type will
11284 // return COMPARISON_RESULT_UNKNOWN to show
11285 // callers that this type (and all the types
11286 // that depend on it) depends on a redundant
11287 // type and so, can't be
11288 // canonical-type-propagated.
11289 result = local_result;
11290 }
11291 if (found_l_child != found_r_child)
11292 {
11293 SET_RESULT_TO_FALSE(result, l, r);
11294 break;
11295 }
11296 }
11297 }
11298 }
11299 break;
11300
11301 case DW_TAG_formal_parameter:
11302 {
11303 Dwarf_Die l_type, r_type;
11304 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
11305 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
11306 if (l_type_is_void != r_type_is_void)
11307 SET_RESULT_TO_FALSE(result, l, r);
11308 else if (!l_type_is_void)
11309 {
11310 comparison_result local_result =
11311 compare_dies(rdr, &l_type, &r_type,
11312 aggregates_being_compared,
11313 update_canonical_dies_on_the_fly);
11314 SET_RESULT_TO(result, local_result, l, r);
11315 }
11316 }
11317 break;
11318
11319 case DW_TAG_variable:
11320 case DW_TAG_member:
11321 if (compare_as_decl_dies(l, r))
11322 {
11323 // Compare the offsets of the data members
11324 if (l_tag == DW_TAG_member)
11325 {
11326 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11327 die_member_offset(rdr, l, l_offset_in_bits);
11328 die_member_offset(rdr, r, r_offset_in_bits);
11329 if (l_offset_in_bits != r_offset_in_bits)
11330 SET_RESULT_TO_FALSE(result, l, r);
11331 }
11332 if (result)
11333 {
11334 // Compare the types of the data members or variables.
11335 Dwarf_Die l_type, r_type;
11336 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11337 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11338 comparison_result local_result =
11339 compare_dies(rdr, &l_type, &r_type,
11340 aggregates_being_compared,
11341 update_canonical_dies_on_the_fly);
11342 SET_RESULT_TO(result, local_result, l, r);
11343 }
11344 }
11345 else
11346 SET_RESULT_TO_FALSE(result, l, r);
11347 break;
11348
11349 case DW_TAG_inheritance:
11350 {
11351 Dwarf_Die l_type, r_type;
11352 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11353 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11354 result = compare_dies(rdr, &l_type, &r_type,
11355 aggregates_being_compared,
11356 update_canonical_dies_on_the_fly);
11357 if (!result)
11358 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11359
11360 uint64_t l_a = 0, r_a = 0;
11361 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11362 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11363 if (l_a != r_a)
11364 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11365
11366 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11367 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11368 if (l_a != r_a)
11369 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11370
11371 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11372 die_member_offset(rdr, l, l_offset_in_bits);
11373 die_member_offset(rdr, r, r_offset_in_bits);
11374 if (l_offset_in_bits != r_offset_in_bits)
11375 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11376 }
11377 break;
11378
11379 case DW_TAG_ptr_to_member_type:
11380 {
11381 bool comp_result = false;
11382 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11383 if (!comp_result)
11384 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11385
11386 Dwarf_Die l_type, r_type;
11387 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11388 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11389 result = compare_dies(rdr, &l_type, &r_type,
11390 aggregates_being_compared,
11391 update_canonical_dies_on_the_fly);
11392 if (!result)
11393 ABG_RETURN(result);
11394
11395 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11396 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11397 result = compare_dies(rdr, &l_type, &r_type,
11398 aggregates_being_compared,
11399 update_canonical_dies_on_the_fly);
11400 if (!result)
11401 ABG_RETURN(result);
11402 }
11403 break;
11404
11405 case DW_TAG_enumerator:
11406 case DW_TAG_packed_type:
11407 case DW_TAG_set_type:
11408 case DW_TAG_file_type:
11409 case DW_TAG_thrown_type:
11410 case DW_TAG_interface_type:
11411 case DW_TAG_shared_type:
11412 case DW_TAG_compile_unit:
11413 case DW_TAG_namespace:
11414 case DW_TAG_module:
11415 case DW_TAG_constant:
11416 case DW_TAG_partial_unit:
11417 case DW_TAG_imported_unit:
11418 case DW_TAG_dwarf_procedure:
11419 case DW_TAG_imported_declaration:
11420 case DW_TAG_entry_point:
11421 case DW_TAG_label:
11422 case DW_TAG_lexical_block:
11423 case DW_TAG_unspecified_parameters:
11424 case DW_TAG_variant:
11425 case DW_TAG_common_block:
11426 case DW_TAG_common_inclusion:
11427 case DW_TAG_inlined_subroutine:
11428 case DW_TAG_with_stmt:
11429 case DW_TAG_access_declaration:
11430 case DW_TAG_catch_block:
11431 case DW_TAG_friend:
11432 case DW_TAG_namelist:
11433 case DW_TAG_namelist_item:
11434 case DW_TAG_template_type_parameter:
11435 case DW_TAG_template_value_parameter:
11436 case DW_TAG_try_block:
11437 case DW_TAG_variant_part:
11438 case DW_TAG_imported_module:
11439 case DW_TAG_condition:
11440 case DW_TAG_type_unit:
11441 case DW_TAG_template_alias:
11442 case DW_TAG_lo_user:
11443 case DW_TAG_MIPS_loop:
11444 case DW_TAG_format_label:
11445 case DW_TAG_function_template:
11446 case DW_TAG_class_template:
11447 case DW_TAG_GNU_BINCL:
11448 case DW_TAG_GNU_EINCL:
11449 case DW_TAG_GNU_template_template_param:
11450 case DW_TAG_GNU_template_parameter_pack:
11451 case DW_TAG_GNU_formal_parameter_pack:
11452 case DW_TAG_GNU_call_site:
11453 case DW_TAG_GNU_call_site_parameter:
11454 case DW_TAG_hi_user:
11455#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11456 if (rdr.debug_die_canonicalization_is_on_)
11458#endif
11460 break;
11461 }
11462
11463 ABG_RETURN(result);
11464}
11465
11466/// Compare two DIEs emitted by a C compiler.
11467///
11468/// @param rdr the DWARF reader used to load the DWARF information.
11469///
11470/// @param l the left-hand-side argument of this comparison operator.
11471///
11472/// @param r the righ-hand-side argument of this comparison operator.
11473///
11474/// @param update_canonical_dies_on_the_fly if yes, then this function
11475/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11476/// comparing l and r. This helps in making so that sub-type DIEs of
11477/// 'l' and 'r' are compared structurally only once. This is how we
11478/// turn this exponential comparison problem into a problem that is a
11479/// closer to a linear one.
11480///
11481/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11482static comparison_result
11483compare_dies(const reader& rdr,
11484 const Dwarf_Die *l,
11485 const Dwarf_Die *r,
11486 bool update_canonical_dies_on_the_fly)
11487{
11488 offset_pairs_stack_type aggregates_being_compared(rdr);
11489 return compare_dies(rdr, l, r, aggregates_being_compared,
11490 update_canonical_dies_on_the_fly);
11491}
11492
11493/// Compare two DIEs for the purpose of canonicalization.
11494///
11495/// This is a sub-routine of reader::get_canonical_die.
11496///
11497/// When DIE canonicalization debugging is on, this function performs
11498/// both structural and canonical comparison. It expects that both
11499/// comparison yield the same result.
11500///
11501/// @param rdr the DWARF reader.
11502///
11503/// @param l the left-hand-side comparison operand DIE.
11504///
11505/// @param r the right-hand-side comparison operand DIE.
11506///
11507/// @param update_canonical_dies_on_the_fly if true, then some
11508/// aggregate DIEs will see their canonical types propagated.
11509///
11510/// @return true iff @p l equals @p r.
11511static bool
11512compare_dies_during_canonicalization(reader& rdr,
11513 const Dwarf_Die *l,
11514 const Dwarf_Die *r,
11515 bool update_canonical_dies_on_the_fly)
11516{
11517#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11518 if (rdr.debug_die_canonicalization_is_on_)
11519 {
11520 bool canonical_equality = false, structural_equality = false;
11521 rdr.use_canonical_die_comparison_ = false;
11522 structural_equality = compare_dies(rdr, l, r,
11523 /*update_canonical_dies_on_the_fly=*/false);
11524 rdr.use_canonical_die_comparison_ = true;
11525 canonical_equality = compare_dies(rdr, l, r,
11526 update_canonical_dies_on_the_fly);
11527 if (canonical_equality != structural_equality)
11528 {
11529 std::cerr << "structural & canonical equality different for DIEs: "
11530 << std::hex
11531 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11532 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11533 << std::dec
11534 << ", repr: '"
11535 << rdr.get_die_pretty_type_representation(l, 0)
11536 << "'"
11537 << std::endl;
11539 }
11540 return structural_equality;
11541 }
11542#endif
11543 return compare_dies(rdr, l, r,
11544 update_canonical_dies_on_the_fly);
11545}
11546
11547// ----------------------------------
11548// </die comparison engine>
11549// ---------------------------------
11550
11551/// Get the point where a DW_AT_import DIE is used to import a given
11552/// (unit) DIE, between two DIEs.
11553///
11554/// @param rdr the dwarf reader to consider.
11555///
11556/// @param partial_unit_offset the imported unit for which we want to
11557/// know the insertion point. This is usually a partial unit (with
11558/// tag DW_TAG_partial_unit) but it does not necessarily have to be
11559/// so.
11560///
11561/// @param first_die_offset the offset of the DIE from which this
11562/// function starts looking for the import point of
11563/// @partial_unit_offset. Note that this offset is excluded from the
11564/// set of potential solutions.
11565///
11566/// @param first_die_cu_offset the offset of the (compilation) unit
11567/// that @p first_die_cu_offset belongs to.
11568///
11569/// @param source where the DIE of first_die_cu_offset unit comes
11570/// from.
11571///
11572/// @param last_die_offset the offset of the last DIE of the up to
11573/// which this function looks for the import point of @p
11574/// partial_unit_offset. Note that this offset is excluded from the
11575/// set of potential solutions.
11576///
11577/// @param imported_point_offset. The resulting
11578/// imported_point_offset. Note that if the imported DIE @p
11579/// partial_unit_offset is not found between @p first_die_offset and
11580/// @p last_die_offset, this parameter is left untouched by this
11581/// function.
11582///
11583/// @return true iff an imported unit is found between @p
11584/// first_die_offset and @p last_die_offset.
11585static bool
11586find_import_unit_point_between_dies(const reader& rdr,
11587 size_t partial_unit_offset,
11588 Dwarf_Off first_die_offset,
11589 Dwarf_Off first_die_cu_offset,
11590 die_source source,
11591 size_t last_die_offset,
11592 size_t& imported_point_offset)
11593{
11594 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11595 rdr.tu_die_imported_unit_points_map(source);
11596
11597 tu_die_imported_unit_points_map_type::const_iterator iter =
11598 tu_die_imported_unit_points_map.find(first_die_cu_offset);
11599
11600 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11601
11602 const imported_unit_points_type& imported_unit_points = iter->second;
11603 if (imported_unit_points.empty())
11604 return false;
11605
11606 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11607 imported_unit_points_type::const_iterator e = imported_unit_points.end();
11608
11609 find_lower_bound_in_imported_unit_points(imported_unit_points,
11610 first_die_offset,
11611 b);
11612
11613 if (last_die_offset != static_cast<size_t>(-1))
11614 find_lower_bound_in_imported_unit_points(imported_unit_points,
11615 last_die_offset,
11616 e);
11617
11618 if (e != imported_unit_points.end())
11619 {
11620 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11621 if (i->imported_unit_die_off == partial_unit_offset)
11622 {
11623 imported_point_offset = i->offset_of_import ;
11624 return true;
11625 }
11626
11627 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11628 {
11629 if (find_import_unit_point_between_dies(rdr,
11630 partial_unit_offset,
11631 i->imported_unit_child_off,
11632 i->imported_unit_cu_off,
11633 i->imported_unit_die_source,
11634 /*(Dwarf_Off)*/-1,
11635 imported_point_offset))
11636 return true;
11637 }
11638 }
11639 else
11640 {
11641 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11642 if (i->imported_unit_die_off == partial_unit_offset)
11643 {
11644 imported_point_offset = i->offset_of_import ;
11645 return true;
11646 }
11647
11648 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11649 {
11650 if (find_import_unit_point_between_dies(rdr,
11651 partial_unit_offset,
11652 i->imported_unit_child_off,
11653 i->imported_unit_cu_off,
11654 i->imported_unit_die_source,
11655 /*(Dwarf_Off)*/-1,
11656 imported_point_offset))
11657 return true;
11658 }
11659 }
11660
11661 return false;
11662}
11663
11664/// In the current translation unit, get the last point where a
11665/// DW_AT_import DIE is used to import a given (unit) DIE, before a
11666/// given DIE is found. That given DIE is called the limit DIE.
11667///
11668/// Said otherwise, this function returns the last import point of a
11669/// unit, before a limit.
11670///
11671/// @param rdr the dwarf reader to consider.
11672///
11673/// @param partial_unit_offset the imported unit for which we want to
11674/// know the insertion point of. This is usually a partial unit (with
11675/// tag DW_TAG_partial_unit) but it does not necessarily have to be
11676/// so.
11677///
11678/// @param where_offset the offset of the limit DIE.
11679///
11680/// @param imported_point_offset. The resulting imported_point_offset.
11681/// Note that if the imported DIE @p partial_unit_offset is not found
11682/// before @p die_offset, this is set to the last @p
11683/// partial_unit_offset found under @p parent_die.
11684///
11685/// @return true iff an imported unit is found before @p die_offset.
11686/// Note that if an imported unit is found after @p die_offset then @p
11687/// imported_point_offset is set and the function return false.
11688static bool
11689find_import_unit_point_before_die(const reader& rdr,
11690 size_t partial_unit_offset,
11691 size_t where_offset,
11692 size_t& imported_point_offset)
11693{
11694 size_t import_point_offset = 0;
11695 Dwarf_Die first_die_of_tu;
11696
11697 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11698 &first_die_of_tu) != 0)
11699 return false;
11700
11701 Dwarf_Die cu_die_memory;
11702 Dwarf_Die *cu_die;
11703
11704 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
11705 &cu_die_memory, 0, 0);
11706
11707 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
11708 dwarf_dieoffset(&first_die_of_tu),
11709 dwarf_dieoffset(cu_die),
11710 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
11711 where_offset,
11712 import_point_offset))
11713 {
11714 imported_point_offset = import_point_offset;
11715 return true;
11716 }
11717
11718 if (import_point_offset)
11719 {
11720 imported_point_offset = import_point_offset;
11721 return true;
11722 }
11723
11724 return false;
11725}
11726
11727/// Return the parent DIE for a given DIE.
11728///
11729/// Note that the function build_die_parent_map() must have been
11730/// called before this one can work. This function either succeeds or
11731/// aborts the current process.
11732///
11733/// @param rdr the DWARF reader to consider.
11734///
11735/// @param die the DIE for which we want the parent.
11736///
11737/// @param parent_die the output parameter set to the parent die of
11738/// @p die. Its memory must be allocated and handled by the caller.
11739///
11740/// @param where_offset the offset of the DIE where we are "logically"
11741/// positionned at, in the DIE tree. This is useful when @p die is
11742/// e.g, DW_TAG_partial_unit that can be included in several places in
11743/// the DIE tree.
11744///
11745/// @return true if the function could get a parent DIE, false
11746/// otherwise.
11747static bool
11748get_parent_die(const reader& rdr,
11749 const Dwarf_Die* die,
11750 Dwarf_Die& parent_die,
11751 size_t where_offset)
11752{
11753 ABG_ASSERT(rdr.dwarf_debug_info());
11754
11755 const die_source source = rdr.get_die_source(die);
11756
11757 const offset_offset_map_type& m = rdr.die_parent_map(source);
11758 offset_offset_map_type::const_iterator i =
11759 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
11760
11761 if (i == m.end())
11762 return false;
11763
11764 switch (source)
11765 {
11766 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
11767 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11768 i->second, &parent_die));
11769 break;
11770 case ALT_DEBUG_INFO_DIE_SOURCE:
11771 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
11772 i->second, &parent_die));
11773 break;
11774 case TYPE_UNIT_DIE_SOURCE:
11775 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11776 i->second, &parent_die));
11777 break;
11778 case NO_DEBUG_INFO_DIE_SOURCE:
11779 case NUMBER_OF_DIE_SOURCES:
11781 }
11782
11783 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
11784 {
11785 if (where_offset == 0)
11786 {
11787 parent_die = *rdr.cur_tu_die();
11788 return true;
11789 }
11790 size_t import_point_offset = 0;
11791 bool found =
11792 find_import_unit_point_before_die(rdr,
11793 dwarf_dieoffset(&parent_die),
11794 where_offset,
11795 import_point_offset);
11796 if (!found)
11797 // It looks like parent_die (which comes from the alternate
11798 // debug info file) hasn't been imported into this TU. So,
11799 // Let's assume its logical parent is the DIE of the current
11800 // TU.
11801 parent_die = *rdr.cur_tu_die();
11802 else
11803 {
11804 ABG_ASSERT(import_point_offset);
11805 Dwarf_Die import_point_die;
11806 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11807 import_point_offset,
11808 &import_point_die));
11809 return get_parent_die(rdr, &import_point_die,
11810 parent_die, where_offset);
11811 }
11812 }
11813
11814 return true;
11815}
11816
11817/// Get the DIE representing the scope of a given DIE.
11818///
11819/// Please note that when the DIE we are looking at has a
11820/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
11821/// DIE is the parent DIE of the DIE referred to by that attribute.
11822/// In other words, this function returns the scope of the origin DIE
11823/// of the current DIE.
11824///
11825/// So, the scope DIE can be different from the parent DIE of a given
11826/// DIE.
11827///
11828/// Also note that if the current translation unit is from C, then
11829/// this returns the global scope.
11830///
11831/// @param rdr the DWARF reader to use.
11832///
11833/// @param dye the DIE to consider.
11834///
11835/// @param where_offset where we are logically at in the DIE stream.
11836///
11837/// @param scope_die out parameter. This is set to the resulting
11838/// scope DIE iff the function returns true.
11839///
11840/// @return true iff the scope was found and returned in the @p
11841/// scope_die parameter.
11842static bool
11843get_scope_die(const reader& rdr,
11844 const Dwarf_Die* dye,
11845 size_t where_offset,
11846 Dwarf_Die& scope_die)
11847{
11848 Dwarf_Die origin_die_mem;
11849 Dwarf_Die *die = &origin_die_mem;
11850 if (!die_origin_die(dye, origin_die_mem))
11851 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
11852
11853 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11854 rdr.get_die_language(die, die_lang);
11855 if (is_c_language(die_lang)
11856 || rdr.die_parent_map(rdr.get_die_source(die)).empty())
11857 {
11858 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
11859 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
11860 }
11861
11862 if (!get_parent_die(rdr, die, scope_die, where_offset))
11863 return false;
11864
11865 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
11866 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
11867 || dwarf_tag(&scope_die) == DW_TAG_array_type)
11868 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
11869
11870 return true;
11871}
11872
11873/// Return the abigail IR node representing the scope of a given DIE.
11874///
11875/// Note that it is the logical scope that is returned. That is, if
11876/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
11877/// attribute, it's the scope of the referred-to DIE (via these
11878/// attributes) that is returned. In other words, its the scope of
11879/// the origin DIE that is returned.
11880///
11881/// Also note that if the current translation unit is from C, then
11882/// this returns the global scope.
11883///
11884/// @param rdr the dwarf reader to use.
11885///
11886/// @param dye the DIE to get the scope for.
11887///
11888/// @param called_from_public_decl is true if this function has been
11889/// initially called within the context of a public decl.
11890///
11891/// @param where_offset the offset of the DIE where we are "logically"
11892/// positionned at, in the DIE tree. This is useful when @p die is
11893/// e.g, DW_TAG_partial_unit that can be included in several places in
11894/// the DIE tree.
11895///
11896/// @return the resulting scope, or nil if could not be computed.
11897static scope_decl_sptr
11898get_scope_for_die(reader& rdr,
11899 Dwarf_Die* dye,
11900 bool called_for_public_decl,
11901 size_t where_offset)
11902{
11903 Dwarf_Die origin_die_mem;
11904 Dwarf_Die *die = &origin_die_mem;
11905
11906 if (!die_origin_die(dye, origin_die_mem))
11907 // There was no origin DIE found, so let's make the "die" pointer
11908 // above point to the content of the input "dye".
11909 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
11910
11911 const die_source source_of_die = rdr.get_die_source(die);
11912
11913 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11914 rdr.get_die_language(die, die_lang);
11915 if (is_c_language(die_lang)
11916 || rdr.die_parent_map(source_of_die).empty())
11917 {
11918 // In units for the C languages all decls belong to the global
11919 // namespace. This is generally the case if Libabigail
11920 // determined that no DIE -> parent map was needed.
11921 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11922 return rdr.global_scope();
11923 }
11924
11925 Dwarf_Die parent_die;
11926
11927 if (!get_parent_die(rdr, die, parent_die, where_offset))
11928 return rdr.nil_scope();
11929
11930 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
11931 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
11932 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11933 {
11934 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
11935 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11936 {
11937 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
11938 || source_of_die == TYPE_UNIT_DIE_SOURCE);
11939 return rdr.cur_transl_unit()->get_global_scope();
11940 }
11941
11942 // For top level DIEs like DW_TAG_compile_unit, we just want to
11943 // return the global scope for the corresponding translation
11944 // unit. This must have been set by
11945 // build_translation_unit_and_add_to_ir if we already started to
11946 // build the translation unit of parent_die. Otherwise, just
11947 // return the global scope of the current translation unit.
11948 die_tu_map_type::const_iterator i =
11949 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
11950 if (i != rdr.die_tu_map().end())
11951 return i->second->get_global_scope();
11952 return rdr.cur_transl_unit()->get_global_scope();
11953 }
11954
11957 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
11958 || dwarf_tag(&parent_die) == DW_TAG_array_type
11959 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
11960 // this is an entity defined in a scope that is a function.
11961 // Normally, I would say that this should be dropped. But I have
11962 // seen a case where a typedef DIE needed by a function parameter
11963 // was defined right before the parameter, under the scope of the
11964 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
11965 // the function parm too. So for that case, let's say that the
11966 // scope is the scope of the function itself. Note that this is
11967 // an error of the DWARF emitter. We should never see this DIE in
11968 // this context.
11969 {
11970 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
11971 called_for_public_decl,
11972 where_offset);
11973 if (is_anonymous_type_die(die))
11974 // For anonymous type that have nothing to do in a function or
11975 // array type context, let's put it in the containing
11976 // namespace. That is, do not let it be in a containing class
11977 // or union where it has nothing to do.
11978 while (is_class_or_union_type(s))
11979 {
11980 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
11981 return rdr.nil_scope();
11982 s = get_scope_for_die(rdr, &parent_die,
11983 called_for_public_decl,
11984 where_offset);
11985 }
11986 return s;
11987 }
11988 else
11989 d = build_ir_node_from_die(rdr, &parent_die,
11990 called_for_public_decl,
11991 where_offset);
11992 s = dynamic_pointer_cast<scope_decl>(d);
11993 if (!s)
11994 // this is an entity defined in someting that is not a scope.
11995 // Let's drop it.
11996 return rdr.nil_scope();
11997
11998 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
11999 if (cl && cl->get_is_declaration_only())
12000 {
12001 scope_decl_sptr scop =
12002 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
12003 if (scop)
12004 s = scop;
12005 else
12006 s = cl;
12007 }
12008 return s;
12009}
12010
12011/// Convert a DWARF constant representing the value of the
12012/// DW_AT_language property into the translation_unit::language
12013/// enumerator.
12014///
12015/// @param l the DWARF constant to convert.
12016///
12017/// @return the resulting translation_unit::language enumerator.
12019dwarf_language_to_tu_language(size_t l)
12020{
12021 switch (l)
12022 {
12023 case DW_LANG_C89:
12024 return translation_unit::LANG_C89;
12025 case DW_LANG_C:
12026 return translation_unit::LANG_C;
12027 case DW_LANG_Ada83:
12028 return translation_unit::LANG_Ada83;
12029 case DW_LANG_C_plus_plus:
12030 return translation_unit::LANG_C_plus_plus;
12031 case DW_LANG_Cobol74:
12032 return translation_unit::LANG_Cobol74;
12033 case DW_LANG_Cobol85:
12034 return translation_unit::LANG_Cobol85;
12035 case DW_LANG_Fortran77:
12036 return translation_unit::LANG_Fortran77;
12037 case DW_LANG_Fortran90:
12038 return translation_unit::LANG_Fortran90;
12039 case DW_LANG_Pascal83:
12040 return translation_unit::LANG_Pascal83;
12041 case DW_LANG_Modula2:
12042 return translation_unit::LANG_Modula2;
12043 case DW_LANG_Java:
12044 return translation_unit::LANG_Java;
12045 case DW_LANG_C99:
12046 return translation_unit::LANG_C99;
12047 case DW_LANG_Ada95:
12048 return translation_unit::LANG_Ada95;
12049 case DW_LANG_Fortran95:
12050 return translation_unit::LANG_Fortran95;
12051 case DW_LANG_PLI:
12052 return translation_unit::LANG_PLI;
12053 case DW_LANG_ObjC:
12054 return translation_unit::LANG_ObjC;
12055 case DW_LANG_ObjC_plus_plus:
12056 return translation_unit::LANG_ObjC_plus_plus;
12057
12058#ifdef HAVE_DW_LANG_Rust_enumerator
12059 case DW_LANG_Rust:
12060 return translation_unit::LANG_Rust;
12061#endif
12062
12063#ifdef HAVE_DW_LANG_UPC_enumerator
12064 case DW_LANG_UPC:
12065 return translation_unit::LANG_UPC;
12066#endif
12067
12068#ifdef HAVE_DW_LANG_D_enumerator
12069 case DW_LANG_D:
12070 return translation_unit::LANG_D;
12071#endif
12072
12073#ifdef HAVE_DW_LANG_Python_enumerator
12074 case DW_LANG_Python:
12075 return translation_unit::LANG_Python;
12076#endif
12077
12078#ifdef HAVE_DW_LANG_Go_enumerator
12079 case DW_LANG_Go:
12080 return translation_unit::LANG_Go;
12081#endif
12082
12083#ifdef HAVE_DW_LANG_C11_enumerator
12084 case DW_LANG_C11:
12085 return translation_unit::LANG_C11;
12086#endif
12087
12088#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
12089 case DW_LANG_C_plus_plus_03:
12090 return translation_unit::LANG_C_plus_plus_03;
12091#endif
12092
12093#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
12094 case DW_LANG_C_plus_plus_11:
12095 return translation_unit::LANG_C_plus_plus_11;
12096#endif
12097
12098#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
12099 case DW_LANG_C_plus_plus_14:
12100 return translation_unit::LANG_C_plus_plus_14;
12101#endif
12102
12103#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
12104 case DW_LANG_Mips_Assembler:
12105 return translation_unit::LANG_Mips_Assembler;
12106#endif
12107
12108 default:
12109 return translation_unit::LANG_UNKNOWN;
12110 }
12111}
12112
12113/// Get the default array lower bound value as defined by the DWARF
12114/// specification, version 4, depending on the language of the
12115/// translation unit.
12116///
12117/// @param l the language of the translation unit.
12118///
12119/// @return the default array lower bound value.
12120static uint64_t
12121get_default_array_lower_bound(translation_unit::language l)
12122{
12123 int value = 0;
12124 switch (l)
12125 {
12126 case translation_unit::LANG_UNKNOWN:
12127 value = 0;
12128 break;
12129 case translation_unit::LANG_Cobol74:
12130 case translation_unit::LANG_Cobol85:
12131 value = 1;
12132 break;
12133 case translation_unit::LANG_C89:
12134 case translation_unit::LANG_C99:
12135 case translation_unit::LANG_C11:
12136 case translation_unit::LANG_C:
12137 case translation_unit::LANG_C_plus_plus_03:
12138 case translation_unit::LANG_C_plus_plus_11:
12139 case translation_unit::LANG_C_plus_plus_14:
12140 case translation_unit::LANG_C_plus_plus:
12141 case translation_unit::LANG_ObjC:
12142 case translation_unit::LANG_ObjC_plus_plus:
12143 case translation_unit::LANG_Rust:
12144 value = 0;
12145 break;
12146 case translation_unit::LANG_Fortran77:
12147 case translation_unit::LANG_Fortran90:
12148 case translation_unit::LANG_Fortran95:
12149 case translation_unit::LANG_Ada83:
12150 case translation_unit::LANG_Ada95:
12151 case translation_unit::LANG_Pascal83:
12152 case translation_unit::LANG_Modula2:
12153 value = 1;
12154 break;
12155 case translation_unit::LANG_Java:
12156 value = 0;
12157 break;
12158 case translation_unit::LANG_PLI:
12159 value = 1;
12160 break;
12161 case translation_unit::LANG_UPC:
12162 case translation_unit::LANG_D:
12163 case translation_unit::LANG_Python:
12164 case translation_unit::LANG_Go:
12165 case translation_unit::LANG_Mips_Assembler:
12166 value = 0;
12167 break;
12168 }
12169
12170 return value;
12171}
12172
12173/// For a given offset, find the lower bound of a sorted vector of
12174/// imported unit point offset.
12175///
12176/// The lower bound is the smallest point (the point with the smallest
12177/// offset) which is the greater than a given offset.
12178///
12179/// @param imported_unit_points_type the sorted vector of imported
12180/// unit points.
12181///
12182/// @param val the offset to consider when looking for the lower
12183/// bound.
12184///
12185/// @param r an iterator to the lower bound found. This parameter is
12186/// set iff the function returns true.
12187///
12188/// @return true iff the lower bound has been found.
12189static bool
12190find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
12191 Dwarf_Off val,
12192 imported_unit_points_type::const_iterator& r)
12193{
12194 imported_unit_point v(val);
12195 imported_unit_points_type::const_iterator result =
12196 std::lower_bound(p.begin(), p.end(), v);
12197
12198 bool is_ok = result != p.end();
12199
12200 if (is_ok)
12201 r = result;
12202
12203 return is_ok;
12204}
12205
12206/// Given a DW_TAG_compile_unit, build and return the corresponding
12207/// abigail::translation_unit ir node. Note that this function
12208/// recursively reads the children dies of the current DIE and
12209/// populates the resulting translation unit.
12210///
12211/// @param rdr the DWARF reader to use.
12212///
12213/// @param die the DW_TAG_compile_unit DIE to consider.
12214///
12215/// @param address_size the size of the addresses expressed in this
12216/// translation unit in general.
12217///
12218/// @return a pointer to the resulting translation_unit.
12220build_translation_unit_and_add_to_ir(reader& rdr,
12221 Dwarf_Die* die,
12222 char address_size)
12223{
12224 translation_unit_sptr result;
12225
12226 if (!die)
12227 return result;
12228 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
12229
12230 // Clear the part of the context that is dependent on the translation
12231 // unit we are reading.
12232 rdr.clear_per_translation_unit_data();
12233
12234 rdr.cur_tu_die(die);
12235
12236 string path = die_string_attribute(die, DW_AT_name);
12237 if (path == "<artificial>")
12238 {
12239 // This is a file artificially generated by the compiler, so its
12240 // name is '<artificial>'. As we want all different translation
12241 // units to have unique path names, let's suffix this path name
12242 // with its die offset.
12243 std::ostringstream o;
12244 o << path << "-" << std::hex << dwarf_dieoffset(die);
12245 path = o.str();
12246 }
12247 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
12248
12249 // See if the same translation unit exits already in the current
12250 // corpus. Sometimes, the same translation unit can be present
12251 // several times in the same debug info. The content of the
12252 // different instances of the translation unit are different. So to
12253 // represent that, we are going to re-use the same translation
12254 // unit. That is, it's going to be the union of all the translation
12255 // units of the same path.
12256 {
12257 const string& abs_path =
12258 compilation_dir.empty() ? path : compilation_dir + "/" + path;
12259 result = rdr.corpus()->find_translation_unit(abs_path);
12260 }
12261
12262 if (!result)
12263 {
12264 result.reset(new translation_unit(rdr.env(),
12265 path,
12266 address_size));
12267 result->set_compilation_dir_path(compilation_dir);
12268 rdr.corpus()->add(result);
12269 uint64_t l = 0;
12270 die_unsigned_constant_attribute(die, DW_AT_language, l);
12271 result->set_language(dwarf_language_to_tu_language(l));
12272 }
12273
12274 rdr.cur_transl_unit(result);
12275 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
12276
12277 Dwarf_Die child;
12278 if (dwarf_child(die, &child) != 0)
12279 return result;
12280
12281 result->set_is_constructed(false);
12282
12283 do
12284 // Analyze all the DIEs we encounter unless we are asked to only
12285 // analyze exported interfaces and the types reachables from them.
12286 if (!rdr.env().analyze_exported_interfaces_only()
12287 || rdr.is_decl_die_with_exported_symbol(&child))
12288 build_ir_node_from_die(rdr, &child,
12289 die_is_public_decl(&child),
12290 dwarf_dieoffset(&child));
12291 while (dwarf_siblingof(&child, &child) == 0);
12292
12293 if (!rdr.var_decls_to_re_add_to_tree().empty())
12294 for (list<var_decl_sptr>::const_iterator v =
12295 rdr.var_decls_to_re_add_to_tree().begin();
12296 v != rdr.var_decls_to_re_add_to_tree().end();
12297 ++v)
12298 {
12299 if (is_member_decl(*v))
12300 continue;
12301
12302 ABG_ASSERT((*v)->get_scope());
12303 string demangled_name =
12304 demangle_cplus_mangled_name((*v)->get_linkage_name());
12305 if (!demangled_name.empty())
12306 {
12307 std::list<string> fqn_comps;
12308 fqn_to_components(demangled_name, fqn_comps);
12309 string mem_name = fqn_comps.back();
12310 fqn_comps.pop_back();
12311 class_decl_sptr class_type;
12312 string ty_name;
12313 if (!fqn_comps.empty())
12314 {
12315 ty_name = components_to_type_name(fqn_comps);
12316 class_type =
12317 lookup_class_type(ty_name, *rdr.cur_transl_unit());
12318 }
12319 if (class_type)
12320 {
12321 // So we are seeing a member variable for which there
12322 // is a global variable definition DIE not having a
12323 // reference attribute pointing back to the member
12324 // variable declaration DIE. Thus remove the global
12325 // variable definition from its current non-class
12326 // scope ...
12327 decl_base_sptr d;
12328 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12329 // This is the data member with the same name in cl.
12330 // We just need to flag it as static.
12331 ;
12332 else
12333 {
12334 // In this case there is no data member with the
12335 // same name in cl already. Let's add it there then
12336 // ...
12338 d = add_decl_to_scope(*v, class_type);
12339 }
12340
12341 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12342 // Let's flag the data member as static.
12343 set_member_is_static(d, true);
12344 }
12345 }
12346 }
12347 rdr.var_decls_to_re_add_to_tree().clear();
12348
12349 result->set_is_constructed(true);
12350
12351 return result;
12352}
12353
12354/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12355/// DW_TAG_module (for fortran) DIE.
12356///
12357/// Note that this function connects the DW_TAG_namespace to the IR
12358/// being currently created, reads the children of the DIE and
12359/// connects them to the IR as well.
12360///
12361/// @param rdr the DWARF reader to use.
12362///
12363/// @param die the DIE to read from. Must be either DW_TAG_namespace
12364/// or DW_TAG_module.
12365///
12366/// @param where_offset the offset of the DIE where we are "logically"
12367/// positionned at, in the DIE tree. This is useful when @p die is
12368/// e.g, DW_TAG_partial_unit that can be included in several places in
12369/// the DIE tree.
12370///
12371/// @return the resulting @ref abigail::namespace_decl or NULL if it
12372/// couldn't be created.
12374build_namespace_decl_and_add_to_ir(reader& rdr,
12375 Dwarf_Die* die,
12376 size_t where_offset)
12377{
12378 namespace_decl_sptr result;
12379
12380 if (!die)
12381 return result;
12382
12383 unsigned tag = dwarf_tag(die);
12384 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12385 return result;
12386
12387 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12388 /*called_for_public_decl=*/false,
12389 where_offset);
12390
12391 string name, linkage_name;
12392 location loc;
12393 die_loc_and_name(rdr, die, loc, name, linkage_name);
12394
12395 result.reset(new namespace_decl(rdr.env(), name, loc));
12396 add_decl_to_scope(result, scope.get());
12397 rdr.associate_die_to_decl(die, result, where_offset);
12398
12399 Dwarf_Die child;
12400 if (dwarf_child(die, &child) != 0)
12401 return result;
12402
12403 rdr.scope_stack().push(result.get());
12404 do
12405 build_ir_node_from_die(rdr, &child,
12406 // If this namespace DIE is private
12407 // (anonymous) then all its content is
12408 // considered private. Otherwise, its
12409 // public decls are considered public.
12410 /*called_from_public_decl=*/
12411 die_is_public_decl(die) && die_is_public_decl(&child),
12412 where_offset);
12413 while (dwarf_siblingof(&child, &child) == 0);
12414 rdr.scope_stack().pop();
12415
12416 return result;
12417}
12418
12419/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12420///
12421/// @param rdr the DWARF reader to use.
12422///
12423/// @param die the DW_TAG_base_type to consider.
12424///
12425/// @param where_offset where we are logically at in the DIE stream.
12426///
12427/// @return the resulting decl_base_sptr.
12428static type_decl_sptr
12429build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12430{
12431 type_decl_sptr result;
12432
12433 if (!die)
12434 return result;
12435 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12436
12437 uint64_t byte_size = 0, bit_size = 0;
12438 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12439 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12440 return result;
12441
12442 if (bit_size == 0 && byte_size != 0)
12443 // Update the bit size.
12444 bit_size = byte_size * 8;
12445
12446 string type_name, linkage_name;
12447 location loc;
12448 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12449
12450 if (byte_size == 0)
12451 {
12452 // The size of the type is zero, that must mean that we are
12453 // looking at the definition of the void type.
12454 if (type_name == "void")
12455 result = is_type_decl(build_ir_node_for_void_type(rdr));
12456 else
12457 // A type of size zero that is not void? Hmmh, I am not sure
12458 // what that means. Return nil for now.
12459 return result;
12460 }
12461
12462 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12463 {
12464 string normalized_type_name = type_name;
12465 integral_type int_type;
12466 if (parse_integral_type(type_name, int_type))
12467 normalized_type_name = int_type.to_string();
12468 result = lookup_basic_type(normalized_type_name, *corp);
12469 }
12470
12471 if (!result)
12472 if (corpus_sptr corp = rdr.corpus())
12473 result = lookup_basic_type(type_name, *corp);
12474 if (!result)
12475 result.reset(new type_decl(rdr.env(), type_name, bit_size,
12476 /*alignment=*/0, loc, linkage_name));
12477 rdr.associate_die_to_type(die, result, where_offset);
12478 return result;
12479}
12480
12481/// Construct the type that is to be used as the underlying type of an
12482/// enum.
12483///
12484/// @param rdr the DWARF reader to use.
12485///
12486/// @param enum_name the name of the enum that this type is going to
12487/// be the underlying type of.
12488///
12489/// @param enum_size the size of the enum.
12490///
12491/// @param is_anonymous whether the underlying type is anonymous or
12492/// not. By default, this should be set to true as before c++11 (and
12493/// in C), it's almost the case.
12494static type_decl_sptr
12495build_enum_underlying_type(reader& rdr,
12496 string enum_name,
12497 uint64_t enum_size,
12498 bool is_anonymous = true)
12499{
12500 string underlying_type_name =
12501 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12502 enum_size);
12503
12504 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12505 enum_size, enum_size, location()));
12506 result->set_is_anonymous(is_anonymous);
12507 result->set_is_artificial(true);
12508 translation_unit_sptr tu = rdr.cur_transl_unit();
12509 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12510 result = dynamic_pointer_cast<type_decl>(d);
12511 ABG_ASSERT(result);
12512 canonicalize(result);
12513 return result;
12514}
12515
12516/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12517///
12518/// @param rdr the DWARF reader to use.
12519///
12520/// @param die the DIE to read from.
12521///
12522/// @param scope the scope of the final enum. Note that this function
12523/// does *NOT* add the built type to this scope. The scope is just so
12524/// that the function knows how to name anonymous enums.
12525///
12526/// @param is_declaration_only is true if the DIE denoted by @p die is
12527/// a declaration-only DIE.
12528///
12529/// @return the built enum_type_decl or NULL if it could not be built.
12531build_enum_type(reader& rdr,
12532 Dwarf_Die* die,
12533 scope_decl* scope,
12534 size_t where_offset,
12535 bool is_declaration_only)
12536{
12537 enum_type_decl_sptr result;
12538 if (!die)
12539 return result;
12540
12541 unsigned tag = dwarf_tag(die);
12542 if (tag != DW_TAG_enumeration_type)
12543 return result;
12544
12545 string name, linkage_name;
12546 location loc;
12547 die_loc_and_name(rdr, die, loc, name, linkage_name);
12548
12549 bool is_anonymous = false;
12550 // If the enum is anonymous, let's give it a name.
12551 if (name.empty())
12552 {
12553 name = get_internal_anonymous_die_prefix_name(die);
12554 ABG_ASSERT(!name.empty());
12555 // But we remember that the type is anonymous.
12556 is_anonymous = true;
12557
12558 if (size_t s = scope->get_num_anonymous_member_enums())
12559 name = build_internal_anonymous_die_name(name, s);
12560 }
12561
12562 bool use_odr = rdr.odr_is_relevant(die);
12563 // If the type has location, then associate it to its
12564 // representation. This way, all occurences of types with the same
12565 // representation (name) and location can be later detected as being
12566 // for the same type.
12567
12568 if (!is_anonymous)
12569 {
12570 if (use_odr)
12571 {
12572 if (enum_type_decl_sptr pre_existing_enum =
12573 is_enum_type(rdr.lookup_artifact_from_die(die)))
12574 result = pre_existing_enum;
12575 }
12576 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12577 {
12578 if (loc)
12579 result = lookup_enum_type_per_location(loc.expand(), *corp);
12580 }
12581 else if (loc)
12582 {
12583 if (enum_type_decl_sptr pre_existing_enum =
12584 is_enum_type(rdr.lookup_artifact_from_die(die)))
12585 if (pre_existing_enum->get_location() == loc)
12586 result = pre_existing_enum;
12587 }
12588
12589 if (result)
12590 {
12591 rdr.associate_die_to_type(die, result, where_offset);
12592 return result;
12593 }
12594 }
12595 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12596 // we can look them up?
12597
12598 uint64_t size = 0;
12599 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12600 size *= 8;
12601 bool is_artificial = die_is_artificial(die);
12602
12603 // for now we consider that underlying types of enums are all anonymous
12604 bool enum_underlying_type_is_anonymous= true;
12605
12607 Dwarf_Die child;
12608 if (dwarf_child(die, &child) == 0)
12609 {
12610 do
12611 {
12612 if (dwarf_tag(&child) != DW_TAG_enumerator)
12613 continue;
12614
12615 string n, m;
12616 location l;
12617 die_loc_and_name(rdr, &child, l, n, m);
12618 uint64_t val = 0;
12619 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12620 enms.push_back(enum_type_decl::enumerator(n, val));
12621 }
12622 while (dwarf_siblingof(&child, &child) == 0);
12623 }
12624
12625 // DWARF up to version 4 (at least) doesn't seem to carry the
12626 // underlying type, so let's create an artificial one here, which
12627 // sole purpose is to be passed to the constructor of the
12628 // enum_type_decl type.
12629 type_decl_sptr t =
12630 build_enum_underlying_type(rdr, name, size,
12631 enum_underlying_type_is_anonymous);
12632 t->set_is_declaration_only(is_declaration_only);
12633
12634 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12635 result->set_is_anonymous(is_anonymous);
12636 result->set_is_declaration_only(is_declaration_only);
12637 result->set_is_artificial(is_artificial);
12638 rdr.associate_die_to_type(die, result, where_offset);
12639
12640 rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12641
12642 return result;
12643}
12644
12645/// Once a function_decl has been built and added to a class as a
12646/// member function, this function updates the information of the
12647/// function_decl concerning the properties of its relationship with
12648/// the member class. That is, it updates properties like
12649/// virtualness, access, constness, cdtorness, etc ...
12650///
12651/// @param die the DIE of the function_decl that has been just built.
12652///
12653/// @param f the function_decl that has just been built from @p die.
12654///
12655/// @param klass the @ref class_or_union that @p f belongs to.
12656///
12657/// @param rdr the context used to read the ELF/DWARF information.
12658static void
12659finish_member_function_reading(Dwarf_Die* die,
12660 const function_decl_sptr& f,
12661 const class_or_union_sptr klass,
12662 reader& rdr)
12663{
12664 ABG_ASSERT(klass);
12665
12666 method_decl_sptr m = is_method_decl(f);
12667 ABG_ASSERT(m);
12668
12669 method_type_sptr method_t = is_method_type(m->get_type());
12670 ABG_ASSERT(method_t);
12671
12672 bool is_ctor = (f->get_name() == klass->get_name());
12673 bool is_dtor = (!f->get_name().empty()
12674 && static_cast<string>(f->get_name())[0] == '~');
12675 bool is_virtual = die_is_virtual(die);
12676 int64_t vindex = -1;
12677 if (is_virtual)
12678 die_virtual_function_index(die, vindex);
12679 access_specifier access = public_access;
12680 if (class_decl_sptr c = is_class_type(klass))
12681 if (!c->is_struct())
12682 access = private_access;
12683 die_access_specifier(die, access);
12684
12685 bool is_static = false;
12686 {
12687 // Let's see if the first parameter is a pointer to an instance of
12688 // the same class type as the current class and has a
12689 // DW_AT_artificial attribute flag set. We are not looking at
12690 // DW_AT_object_pointer (for DWARF 3) because it wasn't being
12691 // emitted in GCC 4_4, which was already DWARF 3.
12693 if (!f->get_parameters().empty())
12694 first_parm = f->get_parameters()[0];
12695
12696 bool is_artificial = first_parm && first_parm->get_is_artificial();
12697 type_base_sptr this_ptr_type, other_klass;
12698
12699 if (is_artificial)
12700 this_ptr_type = first_parm->get_type();
12701
12702 // Sometimes, the type of the "this" pointer is "const class_type* const".
12703 //
12704 // Meaning that the "this pointer" itself is const qualified. So
12705 // let's get the underlying underlying non-qualified pointer.
12706 if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
12707 this_ptr_type = q->get_underlying_type();
12708
12709 // Now, get the pointed-to type.
12710 if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
12711 other_klass = p->get_pointed_to_type();
12712
12713 // Sometimes, other_klass can be qualified; e.g, volatile. In
12714 // that case, let's get the unqualified version of other_klass.
12715 if (qualified_type_def_sptr q = is_qualified_type(other_klass))
12716 other_klass = q->get_underlying_type();
12717
12718 if (other_klass
12719 && get_type_name(other_klass) == klass->get_qualified_name())
12720 ;
12721 else
12722 is_static = true;
12723
12724 if (is_static)
12725 {
12726 // If we are looking at a DWARF version that is high enough
12727 // for the DW_AT_object_pointer attribute to be present, let's
12728 // see if it's present. If it is, then the current member
12729 // function is not static.
12730 Dwarf_Die object_pointer_die;
12731 if (die_has_object_pointer(die, object_pointer_die))
12732 is_static = false;
12733 }
12734 }
12735 set_member_access_specifier(m, access);
12736 if (vindex != -1)
12738 if (is_virtual)
12739 set_member_function_is_virtual(m, is_virtual);
12740 set_member_is_static(m, is_static);
12741 set_member_function_is_ctor(m, is_ctor);
12742 set_member_function_is_dtor(m, is_dtor);
12743 set_member_function_is_const(m, method_t->get_is_const());
12744
12746
12747 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
12748 {
12749 // This is a virtual member function which has a linkage name
12750 // but has no underlying symbol set.
12751 //
12752 // The underlying elf symbol to set to this function can show up
12753 // later in the DWARF input or it can be that, because of some
12754 // compiler optimization, the relation between this function and
12755 // its underlying elf symbol is simply not emitted in the DWARF.
12756 //
12757 // Let's thus schedule this function for a later fixup pass
12758 // (performed by
12759 // reader::fixup_functions_with_no_symbols()) that will
12760 // set its underlying symbol.
12761 //
12762 // Note that if the underying symbol is encountered later in the
12763 // DWARF input, then the part of build_function_decl() that
12764 // updates the function to set its underlying symbol will
12765 // de-schedule this function wrt fixup pass.
12766 Dwarf_Off die_offset = dwarf_dieoffset(die);
12767 die_function_decl_map_type &fns_with_no_symbol =
12768 rdr.die_function_decl_with_no_symbol_map();
12769 die_function_decl_map_type::const_iterator i =
12770 fns_with_no_symbol.find(die_offset);
12771 if (i == fns_with_no_symbol.end())
12772 fns_with_no_symbol[die_offset] = f;
12773 }
12774
12775}
12776
12777/// If a function DIE has attributes which have not yet been read and
12778/// added to the internal representation that represents that function
12779/// then read those extra attributes and update the internal
12780/// representation.
12781///
12782/// @param rdr the DWARF reader to use.
12783///
12784/// @param die the function DIE to consider.
12785///
12786/// @param where_offset where we logical are, currently, in the stream
12787/// of DIEs. If you don't know what this is, you can just set it to zero.
12788///
12789/// @param existing_fn the representation of the function to update.
12790///
12791/// @return the updated function representation.
12792static function_decl_sptr
12793maybe_finish_function_decl_reading(reader& rdr,
12794 Dwarf_Die* die,
12795 size_t where_offset,
12796 const function_decl_sptr& existing_fn)
12797{
12798 function_decl_sptr result = build_function_decl(rdr, die,
12799 where_offset,
12800 existing_fn);
12801
12802 return result;
12803}
12804
12805/// Lookup a class or a typedef with a given qualified name in the
12806/// corpus that a given scope belongs to.
12807///
12808/// @param scope the scope to consider.
12809///
12810/// @param type_name the qualified name of the type to look for.
12811///
12812/// @return the typedef or class type found.
12813static type_base_sptr
12814lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
12815{
12816 string qname = build_qualified_name(scope, type_name);
12817 corpus* corp = scope->get_corpus();
12818 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
12819 return result;
12820}
12821
12822/// Lookup a class of typedef type from the current corpus being
12823/// constructed.
12824///
12825/// The type being looked for has the same name as a given DIE.
12826///
12827/// @param rdr the DWARF reader to use.
12828///
12829/// @param die the DIE which has the same name as the type we are
12830/// looking for.
12831///
12832/// @param called_for_public_decl whether this function is being
12833/// called from a a publicly defined declaration.
12834///
12835/// @param where_offset where we are logically at in the DIE stream.
12836///
12837/// @return the type found.
12838static type_base_sptr
12839lookup_class_or_typedef_from_corpus(reader& rdr,
12840 Dwarf_Die* die,
12841 bool called_for_public_decl,
12842 size_t where_offset)
12843{
12844 if (!die)
12845 return class_decl_sptr();
12846
12847 string class_name = die_string_attribute(die, DW_AT_name);
12848 if (class_name.empty())
12849 return class_decl_sptr();
12850
12851 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12852 called_for_public_decl,
12853 where_offset);
12854 if (scope)
12855 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
12856
12857 return type_base_sptr();
12858}
12859
12860/// Lookup a class, typedef or enum type with a given qualified name
12861/// in the corpus that a given scope belongs to.
12862///
12863/// @param scope the scope to consider.
12864///
12865/// @param type_name the qualified name of the type to look for.
12866///
12867/// @return the typedef, enum or class type found.
12868static type_base_sptr
12869lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
12870 const string& type_name)
12871{
12872 string qname = build_qualified_name(scope, type_name);
12873 corpus* corp = scope->get_corpus();
12874 type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
12875 return result;
12876}
12877
12878/// Lookup a class, typedef or enum type in a given scope, in the
12879/// corpus that scope belongs to.
12880///
12881/// @param die the DIE of the class, typedef or enum to lookup.
12882///
12883/// @param anonymous_member_type_idx if @p DIE represents an anonymous
12884/// type, this is the index of that anonymous type in its scope, in
12885/// case there are several anonymous types of the same kind in that
12886/// scope.
12887///
12888/// @param scope the scope in which to look the type for.
12889///
12890/// @return the typedef, enum or class type found.
12891static type_base_sptr
12892lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
12893 size_t anonymous_member_type_idx,
12894 scope_decl* scope)
12895{
12896 if (!die)
12897 return class_decl_sptr();
12898
12899 string type_name = die_string_attribute(die, DW_AT_name);
12900 if (is_anonymous_type_die(die))
12901 type_name =
12902 get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12903
12904 if (type_name.empty())
12905 return class_decl_sptr();
12906
12907 return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
12908}
12909
12910
12911/// Test if a DIE represents a function that is a member of a given
12912/// class type.
12913///
12914/// @param rdr the DWARF reader.
12915///
12916/// @param function_die the DIE of the function to consider.
12917///
12918/// @param class_type the class type to consider.
12919///
12920/// @param where_offset where we are logically at in the DIE stream.
12921///
12922/// @return the method declaration corresponding to the member
12923/// function of @p class_type, iff @p function_die is for a member
12924/// function of @p class_type.
12925static method_decl_sptr
12926is_function_for_die_a_member_of_class(reader& rdr,
12927 Dwarf_Die* function_die,
12928 const class_or_union_sptr& class_type)
12929{
12930 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
12931
12932 if (!artifact)
12933 return method_decl_sptr();
12934
12935 method_decl_sptr method = is_method_decl(artifact);
12936 method_type_sptr method_type;
12937
12938 if (method)
12939 method_type = method->get_type();
12940 else
12941 method_type = is_method_type(artifact);
12942 ABG_ASSERT(method_type);
12943
12944 class_or_union_sptr method_class = method_type->get_class_type();
12945 ABG_ASSERT(method_class);
12946
12947 string method_class_name = method_class->get_qualified_name(),
12948 class_type_name = class_type->get_qualified_name();
12949
12950 if (method_class_name == class_type_name)
12951 {
12952 //ABG_ASSERT(class_type.get() == method_class.get());
12953 return method;
12954 }
12955
12956 return method_decl_sptr();
12957}
12958
12959/// If a given function DIE represents an existing member function of
12960/// a given class, then update that member function with new
12961/// properties present in the DIE. Otherwise, if the DIE represents a
12962/// new member function that is not already present in the class then
12963/// add that new member function to the class.
12964///
12965/// @param rdr the DWARF reader.
12966///
12967/// @param function_die the DIE of the potential member function to
12968/// consider.
12969///
12970/// @param class_type the class type to consider.
12971///
12972/// @param called_from_public_decl is true iff this function was
12973/// called from a publicly defined and exported declaration.
12974///
12975/// @param where_offset where we are logically at in the DIE stream.
12976///
12977/// @return the method decl representing the member function.
12978static method_decl_sptr
12979add_or_update_member_function(reader& rdr,
12980 Dwarf_Die* function_die,
12981 const class_or_union_sptr& class_type,
12982 bool called_from_public_decl,
12983 size_t where_offset)
12984{
12985 method_decl_sptr method =
12986 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
12987
12988 if (!method)
12989 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
12990 class_type.get(),
12991 called_from_public_decl,
12992 where_offset));
12993 if (!method)
12994 return method_decl_sptr();
12995
12996 finish_member_function_reading(function_die,
12997 is_function_decl(method),
12998 class_type, rdr);
12999 return method;
13000}
13001
13002/// Build a an IR node for class type from a DW_TAG_structure_type or
13003/// DW_TAG_class_type DIE and add that node to the ABI corpus being
13004/// currently built.
13005///
13006/// If the represents class type that already exists, then update the
13007/// existing class type with the new properties found in the DIE.
13008///
13009/// It meanst that this function can also update an existing
13010/// class_decl node with data members, member functions and other
13011/// properties coming from the DIE.
13012///
13013/// @param rdr the DWARF reader to consider.
13014///
13015/// @param die the DIE to read information from. Must be either a
13016/// DW_TAG_structure_type or a DW_TAG_class_type.
13017///
13018/// @param scope a pointer to the scope_decl* under which this class
13019/// is to be added to.
13020///
13021/// @param is_struct whether the class was declared as a struct.
13022///
13023/// @param klass if non-null, this is a klass to append the members
13024/// to. Otherwise, this function just builds the class from scratch.
13025///
13026/// @param called_from_public_decl set to true if this class is being
13027/// called from a "Public declaration like vars or public symbols".
13028///
13029/// @param where_offset the offset of the DIE where we are "logically"
13030/// positionned at, in the DIE tree. This is useful when @p die is
13031/// e.g, DW_TAG_partial_unit that can be included in several places in
13032/// the DIE tree.
13033///
13034/// @param is_declaration_only is true if the DIE denoted by @p die is
13035/// a declaration-only DIE.
13036///
13037/// @return the resulting class_type.
13038static class_decl_sptr
13039add_or_update_class_type(reader& rdr,
13040 Dwarf_Die* die,
13041 scope_decl* scope,
13042 bool is_struct,
13043 class_decl_sptr klass,
13044 bool called_from_public_decl,
13045 size_t where_offset,
13046 bool is_declaration_only)
13047{
13048 class_decl_sptr result;
13049 if (!die)
13050 return result;
13051
13052 const die_source source = rdr.get_die_source(die);
13053
13054 unsigned tag = dwarf_tag(die);
13055
13056 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
13057 return result;
13058
13059 {
13060 die_class_or_union_map_type::const_iterator i =
13061 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13062 if (i != rdr.die_wip_classes_map(source).end())
13063 {
13064 class_decl_sptr class_type = is_class_type(i->second);
13065 ABG_ASSERT(class_type);
13066 return class_type;
13067 }
13068 }
13069
13070 string name, linkage_name;
13071 location loc;
13072 die_loc_and_name(rdr, die, loc, name, linkage_name);
13073
13074 bool is_anonymous = false;
13075 if (name.empty())
13076 {
13077 // So we are looking at an anonymous struct. Let's
13078 // give it a name.
13079 name = get_internal_anonymous_die_prefix_name(die);
13080 ABG_ASSERT(!name.empty());
13081 // But we remember that the type is anonymous.
13082 is_anonymous = true;
13083
13084 if (size_t s = scope->get_num_anonymous_member_classes())
13085 name = build_internal_anonymous_die_name(name, s);
13086 }
13087
13088 if (!is_anonymous)
13089 {
13090 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13091 {
13092 if (loc)
13093 // TODO: if there is only one class defined in the corpus
13094 // for this location, then re-use it. But if there are
13095 // more than one, then do not re-use it, for now.
13096 result = lookup_class_type_per_location(loc.expand(), *corp);
13097 else
13098 // TODO: if there is just one class for that name defined,
13099 // then re-use it. Otherwise, don't.
13100 result = lookup_class_type(name, *corp);
13101 if (result
13102 // If we are seeing a declaration of a definition we
13103 // already had, or if we are seing a type with the same
13104 // declaration-only-ness that we had before, then keep
13105 // the one we already had.
13106 && (result->get_is_declaration_only() == is_declaration_only
13107 || (!result->get_is_declaration_only()
13108 && is_declaration_only)))
13109 {
13110 rdr.associate_die_to_type(die, result, where_offset);
13111 return result;
13112 }
13113 else
13114 // We might be seeing the definition of a declaration we
13115 // already had. In that case, keep the definition and
13116 // drop the declaration.
13117 result.reset();
13118 }
13119 }
13120
13121 // If we've already seen the same class as 'die', then let's re-use
13122 // that one, unless it's an anonymous class. We can't really safely
13123 // re-use anonymous classes as they have no name, by construction.
13124 // What we can do, rather, is to reuse the typedef that name them,
13125 // when they do have a naming typedef.
13126 if (!is_anonymous)
13127 if (class_decl_sptr pre_existing_class =
13128 is_class_type(rdr.lookup_type_artifact_from_die(die)))
13129 klass = pre_existing_class;
13130
13131 uint64_t size = 0;
13132 die_size_in_bits(die, size);
13133 bool is_artificial = die_is_artificial(die);
13134
13135 Dwarf_Die child;
13136 bool has_child = (dwarf_child(die, &child) == 0);
13137
13138 decl_base_sptr res;
13139 if (klass)
13140 {
13141 res = result = klass;
13142 if (has_child && klass->get_is_declaration_only()
13143 && klass->get_definition_of_declaration())
13144 res = result = is_class_type(klass->get_definition_of_declaration());
13145 if (loc)
13146 result->set_location(loc);
13147 }
13148 else
13149 {
13150 result.reset(new class_decl(rdr.env(), name, size,
13151 /*alignment=*/0, is_struct, loc,
13152 decl_base::VISIBILITY_DEFAULT,
13153 is_anonymous));
13154
13155 result->set_is_declaration_only(is_declaration_only);
13156
13157 res = add_decl_to_scope(result, scope);
13158 result = dynamic_pointer_cast<class_decl>(res);
13159 ABG_ASSERT(result);
13160 }
13161
13162 if (!klass || klass->get_is_declaration_only())
13163 if (size != result->get_size_in_bits())
13164 result->set_size_in_bits(size);
13165
13166 if (klass)
13167 // We are amending a class that was built before. So let's check
13168 // if we need to amend its "declaration-only-ness" status.
13169 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
13170 // The size of the class doesn't match its
13171 // 'declaration-only-ness". We might have a non-zero sized
13172 // class which is declaration-only, or a zero sized class that
13173 // is not declaration-only. Let's set the declaration-only-ness
13174 // according to what we are instructed to.
13175 //
13176 // Note however that there are binaries out there emitted by
13177 // compilers (Clang, in C++) emit declarations-only classes that
13178 // have non-zero size. So we must honor these too. That is why
13179 // we are not forcing the declaration-only-ness to false when a
13180 // class has non-zero size. An example of such binary is
13181 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
13182 result->set_is_declaration_only(is_declaration_only);
13183
13184 // If a non-decl-only class has children node and is advertized as
13185 // having a non-zero size let's trust that.
13186 if (!result->get_is_declaration_only() && has_child)
13187 if (result->get_size_in_bits() == 0 && size != 0)
13188 result->set_size_in_bits(size);
13189
13190 result->set_is_artificial(is_artificial);
13191
13192 rdr.associate_die_to_type(die, result, where_offset);
13193
13194 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13195
13196 if (!has_child)
13197 // TODO: set the access specifier for the declaration-only class
13198 // here.
13199 return result;
13200
13201 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13202
13203 bool is_incomplete_type = false;
13204 if (is_declaration_only && size == 0 && has_child)
13205 // this is an incomplete DWARF type as defined by [5.7.1]
13206 //
13207 // An incomplete structure, union or class type is represented by
13208 // a structure, union or class entry that does not have a byte
13209 // size attribute and that has a DW_AT_declaration attribute.
13210 //
13211 // Let's consider that it's thus a decl-only class, likely
13212 // referred to by a pointer. If we later encounter a definition
13213 // for this decl-only class type, then this decl-only class will
13214 // be resolved to it by the code in
13215 // reader::resolve_declaration_only_classes.
13216 is_incomplete_type = true;
13217
13218 scope_decl_sptr scop =
13219 dynamic_pointer_cast<scope_decl>(res);
13220 ABG_ASSERT(scop);
13221 rdr.scope_stack().push(scop.get());
13222
13223 if (has_child && !is_incomplete_type)
13224 {
13225 int anonymous_member_class_index = -1;
13226 int anonymous_member_union_index = -1;
13227 int anonymous_member_enum_index = -1;
13228
13229 do
13230 {
13231 tag = dwarf_tag(&child);
13232
13233 // Handle base classes.
13234 if (tag == DW_TAG_inheritance)
13235 {
13236 result->set_is_declaration_only(false);
13237
13238 Dwarf_Die type_die;
13239 if (!die_die_attribute(&child, DW_AT_type, type_die))
13240 continue;
13241
13242 type_base_sptr base_type;
13243 if (!(base_type =
13244 lookup_class_or_typedef_from_corpus(rdr, &type_die,
13245 called_from_public_decl,
13246 where_offset)))
13247 {
13248 base_type =
13249 is_type(build_ir_node_from_die(rdr, &type_die,
13250 called_from_public_decl,
13251 where_offset));
13252 }
13253 // Sometimes base_type can be a typedef. Let's make
13254 // sure that typedef is compatible with a class type.
13256 if (!b)
13257 continue;
13258
13259 access_specifier access =
13260 is_struct
13261 ? public_access
13262 : private_access;
13263
13264 die_access_specifier(&child, access);
13265
13266 bool is_virt= die_is_virtual(&child);
13267 int64_t offset = 0;
13268 bool is_offset_present =
13269 die_member_offset(rdr, &child, offset);
13270
13271 class_decl::base_spec_sptr base(new class_decl::base_spec
13272 (b, access,
13273 is_offset_present ? offset : -1,
13274 is_virt));
13275 if (b->get_is_declaration_only()
13276 // Only non-anonymous decl-only classes are
13277 // scheduled for resolution to their definition.
13278 // Anonymous classes that are decl-only are likely
13279 // only artificially created by
13280 // get_opaque_version_of_type, from anonymous fully
13281 // defined classes. Those are never defined.
13282 && !b->get_qualified_name().empty())
13283 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
13284 if (result->find_base_class(b->get_qualified_name()))
13285 continue;
13286 result->add_base_specifier(base);
13287 }
13288 // Handle data members.
13289 else if (tag == DW_TAG_member
13290 || tag == DW_TAG_variable)
13291 {
13292 Dwarf_Die type_die;
13293 if (!die_die_attribute(&child, DW_AT_type, type_die))
13294 continue;
13295
13296 string n, m;
13297 location loc;
13298 die_loc_and_name(rdr, &child, loc, n, m);
13299 /// For now, we skip the hidden vtable pointer.
13300 /// Currently, we're looking for a member starting with
13301 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
13302 /// use as a name for the hidden vtable pointer.
13303 if (n.substr(0, 5) == "_vptr"
13304 && n.size() > 5
13305 && !std::isalnum(n.at(5))
13306 && n.at(5) != '_')
13307 continue;
13308
13309 // If the variable is already a member of this class,
13310 // move on. If it's an anonymous data member, we need
13311 // to handle it differently. We'll do that later below.
13312 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13313 continue;
13314
13315 int64_t offset_in_bits = 0;
13316 bool is_laid_out = die_member_offset(rdr, &child,
13317 offset_in_bits);
13318 // For now, is_static == !is_laid_out. When we have
13319 // templates, we'll try to be more specific. For now,
13320 // this approximation should do OK.
13321 bool is_static = !is_laid_out;
13322
13323 if (is_static && variable_is_suppressed(rdr,
13324 result.get(),
13325 &child))
13326 continue;
13327
13328 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
13329 called_from_public_decl,
13330 where_offset));
13331 type_base_sptr t = is_type(ty);
13332 if (!t)
13333 continue;
13334
13335 if (n.empty() && !die_is_anonymous_data_member(&child))
13336 {
13337 // We must be in a case where the data member has an
13338 // empty name because the DWARF emitter has a bug.
13339 // Let's generate an artificial name for that data
13340 // member.
13341 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
13342 ABG_ASSERT(!n.empty());
13343 }
13344
13345 // The call to build_ir_node_from_die above could have
13346 // triggered the adding of a data member named 'n' into
13347 // result. So let's check again if the variable is
13348 // already a member of this class. Here again, if it's
13349 // an anonymous data member, we need to handle it
13350 // differently. We'll do that later below.
13351 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13352 continue;
13353
13354 if (!is_static)
13355 // We have a non-static data member. So this class
13356 // cannot be a declaration-only class anymore, even if
13357 // some DWARF emitters might consider it otherwise.
13358 result->set_is_declaration_only(false);
13359 access_specifier access =
13360 is_struct
13361 ? public_access
13362 : private_access;
13363
13364 die_access_specifier(&child, access);
13365
13366 var_decl_sptr dm(new var_decl(n, t, loc, m));
13367 if (n.empty()
13369 // dm is an anonymous data member that was already
13370 // present in the current class so let's not add it.
13371 continue;
13372 result->add_data_member(dm, access, is_laid_out,
13373 is_static, offset_in_bits);
13374 ABG_ASSERT(has_scope(dm));
13375 rdr.associate_die_to_decl(&child, dm, where_offset,
13376 /*associate_by_repr=*/false);
13377 }
13378 // Handle member functions;
13379 else if (tag == DW_TAG_subprogram)
13380 {
13381 decl_base_sptr r =
13382 add_or_update_member_function(rdr, &child, result,
13383 called_from_public_decl,
13384 where_offset);
13386 rdr.associate_die_to_decl(&child, f, where_offset,
13387 /*associate_by_repr=*/true);
13388 }
13389 // Handle member types
13390 else if (die_is_type(&child))
13391 {
13392 // Track the anonymous type index in the current
13393 // scope. Look for what this means by reading the
13394 // comment of the function
13395 // build_internal_anonymous_die_name.
13396 int anonymous_member_type_index = 0;
13397 if (is_anonymous_type_die(&child))
13398 {
13399 // Update the anonymous type index.
13400 if (die_is_class_type(&child))
13401 anonymous_member_type_index =
13402 ++anonymous_member_class_index;
13403 else if (dwarf_tag(&child) == DW_TAG_union_type)
13404 anonymous_member_type_index =
13405 ++anonymous_member_union_index;
13406 else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13407 anonymous_member_type_index =
13408 ++anonymous_member_enum_index;
13409 }
13410 // if the type is not already a member of this class,
13411 // then add it to the class.
13412 if ((is_anonymous_type_die(&child)
13413 && !lookup_class_typedef_or_enum_type_from_corpus
13414 (&child, anonymous_member_type_index, result.get()))
13415 || !result->find_member_type(die_name(&child)))
13416 build_ir_node_from_die(rdr, &child, result.get(),
13417 called_from_public_decl,
13418 where_offset);
13419 }
13420 } while (dwarf_siblingof(&child, &child) == 0);
13421 }
13422
13423 rdr.scope_stack().pop();
13424
13425 {
13426 die_class_or_union_map_type::const_iterator i =
13427 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13428 if (i != rdr.die_wip_classes_map(source).end())
13429 {
13430 if (is_member_type(i->second))
13432 get_member_access_specifier(i->second));
13433 rdr.die_wip_classes_map(source).erase(i);
13434 }
13435 }
13436
13437 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13438 return result;
13439}
13440
13441/// Build an @ref union_decl from a DW_TAG_union_type DIE.
13442///
13443/// @param rdr the DWARF reader to use.
13444///
13445/// @param die the DIE to read from.
13446///
13447/// @param scope the scope the resulting @ref union_decl belongs to.
13448///
13449/// @param union_type if this parameter is non-nil, then this function
13450/// updates the @ref union_decl that it points to, rather than
13451/// creating a new @ref union_decl.
13452///
13453/// @param called_from_public_decl is true if this function has been
13454/// initially called within the context of a public decl.
13455///
13456/// @param where_offset the offset of the DIE where we are "logically"
13457/// positionned at, in the DIE tree. This is useful when @p die is
13458/// e.g, DW_TAG_partial_unit that can be included in several places in
13459/// the DIE tree.
13460///
13461/// @param is_declaration_only is true if the DIE denoted by @p die is
13462/// a declaration-only DIE.
13463///
13464/// @return the resulting @ref union_decl type.
13465static union_decl_sptr
13466add_or_update_union_type(reader& rdr,
13467 Dwarf_Die* die,
13468 scope_decl* scope,
13469 union_decl_sptr union_type,
13470 bool called_from_public_decl,
13471 size_t where_offset,
13472 bool is_declaration_only)
13473{
13474 union_decl_sptr result;
13475 if (!die)
13476 return result;
13477
13478 unsigned tag = dwarf_tag(die);
13479
13480 if (tag != DW_TAG_union_type)
13481 return result;
13482
13483 const die_source source = rdr.get_die_source(die);
13484 {
13485 die_class_or_union_map_type::const_iterator i =
13486 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13487 if (i != rdr.die_wip_classes_map(source).end())
13488 {
13489 union_decl_sptr u = is_union_type(i->second);
13490 ABG_ASSERT(u);
13491 return u;
13492 }
13493 }
13494
13495 string name, linkage_name;
13496 location loc;
13497 die_loc_and_name(rdr, die, loc, name, linkage_name);
13498
13499 bool is_anonymous = false;
13500 if (name.empty())
13501 {
13502 // So we are looking at an anonymous union. Let's give it a
13503 // name.
13504 name = get_internal_anonymous_die_prefix_name(die);
13505 ABG_ASSERT(!name.empty());
13506 // But we remember that the type is anonymous.
13507 is_anonymous = true;
13508
13509 if (size_t s = scope->get_num_anonymous_member_unions())
13510 name = build_internal_anonymous_die_name(name, s);
13511 }
13512
13513 // If the type has location, then associate it to its
13514 // representation. This way, all occurences of types with the same
13515 // representation (name) and location can be later detected as being
13516 // for the same type.
13517
13518 if (!is_anonymous)
13519 {
13520 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13521 {
13522 if (loc)
13523 result = lookup_union_type_per_location(loc.expand(), *corp);
13524 else
13525 result = lookup_union_type(name, *corp);
13526
13527 if (result)
13528 {
13529 rdr.associate_die_to_type(die, result, where_offset);
13530 return result;
13531 }
13532 }
13533 }
13534
13535 // if we've already seen a union with the same union as 'die' then
13536 // let's re-use that one. We can't really safely re-use anonymous
13537 // unions as they have no name, by construction. What we can do,
13538 // rather, is to reuse the typedef that name them, when they do have
13539 // a naming typedef.
13540 if (!is_anonymous)
13541 if (union_decl_sptr pre_existing_union =
13542 is_union_type(rdr.lookup_artifact_from_die(die)))
13543 union_type = pre_existing_union;
13544
13545 uint64_t size = 0;
13546 die_size_in_bits(die, size);
13547 bool is_artificial = die_is_artificial(die);
13548
13549 if (union_type)
13550 {
13551 result = union_type;
13552 result->set_location(loc);
13553 }
13554 else
13555 {
13556 result.reset(new union_decl(rdr.env(), name, size, loc,
13557 decl_base::VISIBILITY_DEFAULT,
13558 is_anonymous));
13559 if (is_declaration_only)
13560 result->set_is_declaration_only(true);
13561 result = is_union_type(add_decl_to_scope(result, scope));
13562 ABG_ASSERT(result);
13563 }
13564
13565 if (size)
13566 {
13567 result->set_size_in_bits(size);
13568 result->set_is_declaration_only(false);
13569 }
13570
13571 result->set_is_artificial(is_artificial);
13572
13573 rdr.associate_die_to_type(die, result, where_offset);
13574
13575 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13576
13577 Dwarf_Die child;
13578 bool has_child = (dwarf_child(die, &child) == 0);
13579 if (!has_child)
13580 return result;
13581
13582 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13583
13584 scope_decl_sptr scop =
13585 dynamic_pointer_cast<scope_decl>(result);
13586 ABG_ASSERT(scop);
13587 rdr.scope_stack().push(scop.get());
13588
13589 if (has_child)
13590 {
13591 do
13592 {
13593 tag = dwarf_tag(&child);
13594 // Handle data members.
13595 if (tag == DW_TAG_member || tag == DW_TAG_variable)
13596 {
13597 Dwarf_Die type_die;
13598 if (!die_die_attribute(&child, DW_AT_type, type_die))
13599 continue;
13600
13601 string n, m;
13602 location loc;
13603 die_loc_and_name(rdr, &child, loc, n, m);
13604
13605 // Because we can be updating an existing union, let's
13606 // make sure we don't already have a member of the same
13607 // name. Anonymous member are handled a bit later below
13608 // so let's not consider them here.
13609 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13610 continue;
13611
13612 ssize_t offset_in_bits = 0;
13613 decl_base_sptr ty =
13614 is_decl(build_ir_node_from_die(rdr, &type_die,
13615 called_from_public_decl,
13616 where_offset));
13617 type_base_sptr t = is_type(ty);
13618 if (!t)
13619 continue;
13620
13621 // We have a non-static data member. So this union
13622 // cannot be a declaration-only union anymore, even if
13623 // some DWARF emitters might consider it otherwise.
13624 result->set_is_declaration_only(false);
13625 access_specifier access = public_access;
13626
13627 die_access_specifier(&child, access);
13628
13629 var_decl_sptr dm(new var_decl(n, t, loc, m));
13630 // If dm is an anonymous data member, let's make sure
13631 // the current union doesn't already have it as a data
13632 // member.
13633 if (n.empty() && result->find_data_member(dm))
13634 continue;
13635
13636 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13637 continue;
13638
13639 result->add_data_member(dm, access, /*is_laid_out=*/true,
13640 /*is_static=*/false,
13641 offset_in_bits);
13642 ABG_ASSERT(has_scope(dm));
13643 rdr.associate_die_to_decl(&child, dm, where_offset,
13644 /*associate_by_repr=*/false);
13645 }
13646 // Handle member functions;
13647 else if (tag == DW_TAG_subprogram)
13648 {
13649 decl_base_sptr r =
13650 is_decl(build_ir_node_from_die(rdr, &child,
13651 result.get(),
13652 called_from_public_decl,
13653 where_offset));
13654 if (!r)
13655 continue;
13656
13657 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13658 ABG_ASSERT(f);
13659
13660 finish_member_function_reading(&child, f, result, rdr);
13661
13662 rdr.associate_die_to_decl(&child, f, where_offset,
13663 /*associate_by_repr=*/false);
13664 }
13665 // Handle member types
13666 else if (die_is_type(&child))
13667 decl_base_sptr td =
13668 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13669 called_from_public_decl,
13670 where_offset));
13671 } while (dwarf_siblingof(&child, &child) == 0);
13672 }
13673
13674 rdr.scope_stack().pop();
13675
13676 {
13677 die_class_or_union_map_type::const_iterator i =
13678 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13679 if (i != rdr.die_wip_classes_map(source).end())
13680 {
13681 if (is_member_type(i->second))
13683 get_member_access_specifier(i->second));
13684 rdr.die_wip_classes_map(source).erase(i);
13685 }
13686 }
13687
13688 return result;
13689}
13690
13691/// build a qualified type from a DW_TAG_const_type,
13692/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13693///
13694/// @param rdr the DWARF reader to consider.
13695///
13696/// @param die the input DIE to read from.
13697///
13698/// @param called_from_public_decl true if this function was called
13699/// from a context where either a public function or a public variable
13700/// is being built.
13701///
13702/// @param where_offset the offset of the DIE where we are "logically"
13703/// positionned at, in the DIE tree. This is useful when @p die is
13704/// e.g, DW_TAG_partial_unit that can be included in several places in
13705/// the DIE tree.
13706///
13707/// @return the resulting qualified_type_def.
13708static type_base_sptr
13709build_qualified_type(reader& rdr,
13710 Dwarf_Die* die,
13711 bool called_from_public_decl,
13712 size_t where_offset)
13713{
13714 type_base_sptr result;
13715 if (!die)
13716 return result;
13717
13718 unsigned tag = dwarf_tag(die);
13719
13720 if (tag != DW_TAG_const_type
13721 && tag != DW_TAG_volatile_type
13722 && tag != DW_TAG_restrict_type)
13723 return result;
13724
13725 Dwarf_Die underlying_type_die;
13726 decl_base_sptr utype_decl;
13727 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13728 // So, if no DW_AT_type is present, then this means (if we are
13729 // looking at a debug info emitted by GCC) that we are looking
13730 // at a qualified void type.
13731 utype_decl = build_ir_node_for_void_type(rdr);
13732
13733 if (!utype_decl)
13734 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
13735 called_from_public_decl,
13736 where_offset));
13737 if (!utype_decl)
13738 return result;
13739
13740 // The call to build_ir_node_from_die() could have triggered the
13741 // creation of the type for this DIE. In that case, just return it.
13742 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13743 {
13744 result = t;
13745 rdr.associate_die_to_type(die, result, where_offset);
13746 return result;
13747 }
13748
13749 type_base_sptr utype = is_type(utype_decl);
13750 ABG_ASSERT(utype);
13751
13752 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
13753 if (tag == DW_TAG_const_type)
13754 qual |= qualified_type_def::CV_CONST;
13755 else if (tag == DW_TAG_volatile_type)
13756 qual |= qualified_type_def::CV_VOLATILE;
13757 else if (tag == DW_TAG_restrict_type)
13758 qual |= qualified_type_def::CV_RESTRICT;
13759 else
13761
13762 if (!result)
13763 result.reset(new qualified_type_def(utype, qual, location()));
13764
13765 rdr.associate_die_to_type(die, result, where_offset);
13766
13767 return result;
13768}
13769
13770/// Walk a tree of typedef of qualified arrays and schedule all type
13771/// nodes for canonicalization.
13772///
13773/// This is to be used after an array tree has been cloned. In that
13774/// case, the newly cloned type nodes have to be scheduled for
13775/// canonicalization.
13776///
13777/// This is a subroutine of maybe_strip_qualification.
13778///
13779/// @param t the type node to be scheduled for canonicalization.
13780///
13781/// @param rdr the DWARF reader to use.
13782static void
13783schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
13784 reader &rdr)
13785{
13786 if (typedef_decl_sptr type = is_typedef(t))
13787 {
13788 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13789 rdr);
13790 rdr.schedule_type_for_late_canonicalization(t);
13791 }
13792 else if (qualified_type_def_sptr type = is_qualified_type(t))
13793 {
13794 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13795 rdr);
13796 rdr.schedule_type_for_late_canonicalization(t);
13797 }
13798 else if (array_type_def_sptr type = is_array_type(t))
13799 {
13800 for (vector<array_type_def::subrange_sptr>::const_iterator i =
13801 type->get_subranges().begin();
13802 i != type->get_subranges().end();
13803 ++i)
13804 {
13805 if (!(*i)->get_scope())
13806 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
13807 rdr.schedule_type_for_late_canonicalization(*i);
13808
13809 }
13810 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
13811 rdr);
13812 rdr.schedule_type_for_late_canonicalization(type);
13813 }
13814}
13815
13816/// Strip qualification from a qualified type, when it makes sense.
13817///
13818/// DWARF constructs "const reference". This is redundant because a
13819/// reference is always const. The issue is these redundant types then
13820/// leak into the IR and make for bad diagnostics.
13821///
13822/// This function thus strips the const qualifier from the type in
13823/// that case. It might contain code to strip other cases like this
13824/// in the future.
13825///
13826/// @param t the type to strip const qualification from.
13827///
13828/// @param rdr the @ref reader to use.
13829///
13830/// @return the stripped type or just return @p t.
13831static decl_base_sptr
13832maybe_strip_qualification(const qualified_type_def_sptr t,
13833 reader &rdr)
13834{
13835 if (!t)
13836 return t;
13837
13838 decl_base_sptr result = t;
13839 type_base_sptr u = t->get_underlying_type();
13840
13843 if (result.get() != t.get())
13844 return result;
13845
13847 {
13848 array_type_def_sptr array;
13849 scope_decl * scope = 0;
13850 if ((array = is_array_type(u)))
13851 {
13852 scope = array->get_scope();
13853 ABG_ASSERT(scope);
13854 array = is_array_type(clone_array_tree(array));
13855 schedule_array_tree_for_late_canonicalization(array, rdr);
13856 add_decl_to_scope(array, scope);
13857 t->set_underlying_type(array);
13858 u = t->get_underlying_type();
13859 }
13860 else if (is_typedef_of_array(u))
13861 {
13862 scope = is_decl(u)->get_scope();
13863 ABG_ASSERT(scope);
13864 typedef_decl_sptr typdef =
13866 schedule_array_tree_for_late_canonicalization(typdef, rdr);
13867 ABG_ASSERT(typdef);
13868 add_decl_to_scope(typdef, scope);
13869 t->set_underlying_type(typdef);
13870 u = t->get_underlying_type();
13871 array = is_typedef_of_array(u);
13872 }
13873 else
13875
13876 ABG_ASSERT(array);
13877 // We should not be editing types that are already canonicalized.
13878 ABG_ASSERT(!array->get_canonical_type());
13879 type_base_sptr element_type = array->get_element_type();
13880
13881 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
13882 {
13883 // We should not be editing types that are already canonicalized.
13884 ABG_ASSERT(!qualified->get_canonical_type());
13885 qualified_type_def::CV quals = qualified->get_cv_quals();
13886 quals |= t->get_cv_quals();
13887 qualified->set_cv_quals(quals);
13889 result = is_decl(u);
13890 }
13891 else
13892 {
13893 qualified_type_def_sptr qual_type
13894 (new qualified_type_def(element_type,
13895 t->get_cv_quals(),
13896 t->get_location()));
13898 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
13899 array->set_element_type(qual_type);
13900 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
13901 result = is_decl(u);
13902 }
13903 }
13904
13905 return result;
13906}
13907
13908/// Build a pointer type from a DW_TAG_pointer_type DIE.
13909///
13910/// @param rdr the DWARF reader to consider.
13911///
13912/// @param die the DIE to read information from.
13913///
13914/// @param called_from_public_decl true if this function was called
13915/// from a context where either a public function or a public variable
13916/// is being built.
13917///
13918/// @param where_offset the offset of the DIE where we are "logically"
13919/// positionned at, in the DIE tree. This is useful when @p die is
13920/// e.g, DW_TAG_partial_unit that can be included in several places in
13921/// the DIE tree.
13922///
13923/// @return the resulting pointer to pointer_type_def.
13925build_pointer_type_def(reader& rdr,
13926 Dwarf_Die* die,
13927 bool called_from_public_decl,
13928 size_t where_offset)
13929{
13930 pointer_type_def_sptr result;
13931
13932 if (!die)
13933 return result;
13934
13935 unsigned tag = dwarf_tag(die);
13936 if (tag != DW_TAG_pointer_type)
13937 return result;
13938
13939 type_or_decl_base_sptr utype_decl;
13940 Dwarf_Die underlying_type_die;
13941 bool has_underlying_type_die = false;
13942 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13943 // If the DW_AT_type attribute is missing, that means we are
13944 // looking at a pointer to "void".
13945 utype_decl = build_ir_node_for_void_type(rdr);
13946 else
13947 has_underlying_type_die = true;
13948
13949 if (!utype_decl && has_underlying_type_die)
13950 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
13951 called_from_public_decl,
13952 where_offset);
13953 if (!utype_decl)
13954 return result;
13955
13956 // The call to build_ir_node_from_die() could have triggered the
13957 // creation of the type for this DIE. In that case, just return it.
13958 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13959 {
13960 result = is_pointer_type(t);
13961 ABG_ASSERT(result);
13962 return result;
13963 }
13964
13965 type_base_sptr utype = is_type(utype_decl);
13966 ABG_ASSERT(utype);
13967
13968 // if the DIE for the pointer type doesn't have a byte_size
13969 // attribute then we assume the size of the pointer is the address
13970 // size of the current translation unit.
13971 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13972 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13973 // The size as expressed by DW_AT_byte_size is in byte, so let's
13974 // convert it to bits.
13975 size *= 8;
13976
13977 // And the size of the pointer must be the same as the address size
13978 // of the current translation unit.
13979 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13980
13981 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
13982 ABG_ASSERT(result->get_pointed_to_type());
13983
13984 if (is_void_pointer_type(result))
13985 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
13986
13987 rdr.associate_die_to_type(die, result, where_offset);
13988 return result;
13989}
13990
13991/// Build a reference type from either a DW_TAG_reference_type or
13992/// DW_TAG_rvalue_reference_type DIE.
13993///
13994/// @param rdr the DWARF reader to consider.
13995///
13996/// @param die the DIE to read from.
13997///
13998/// @param called_from_public_decl true if this function was called
13999/// from a context where either a public function or a public variable
14000/// is being built.
14001///
14002/// @param where_offset the offset of the DIE where we are "logically"
14003/// positionned at, in the DIE tree. This is useful when @p die is
14004/// e.g, DW_TAG_partial_unit that can be included in several places in
14005/// the DIE tree.
14006///
14007/// @return a pointer to the resulting reference_type_def.
14009build_reference_type(reader& rdr,
14010 Dwarf_Die* die,
14011 bool called_from_public_decl,
14012 size_t where_offset)
14013{
14015
14016 if (!die)
14017 return result;
14018
14019 unsigned tag = dwarf_tag(die);
14020 if (tag != DW_TAG_reference_type
14021 && tag != DW_TAG_rvalue_reference_type)
14022 return result;
14023
14024 Dwarf_Die underlying_type_die;
14025 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14026 return result;
14027
14028 type_or_decl_base_sptr utype_decl =
14029 build_ir_node_from_die(rdr, &underlying_type_die,
14030 called_from_public_decl,
14031 where_offset);
14032 if (!utype_decl)
14033 return result;
14034
14035 // The call to build_ir_node_from_die() could have triggered the
14036 // creation of the type for this DIE. In that case, just return it.
14037 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14038 {
14039 result = is_reference_type(t);
14040 ABG_ASSERT(result);
14041 return result;
14042 }
14043
14044 type_base_sptr utype = is_type(utype_decl);
14045 ABG_ASSERT(utype);
14046
14047 // if the DIE for the reference type doesn't have a byte_size
14048 // attribute then we assume the size of the reference is the address
14049 // size of the current translation unit.
14050 uint64_t size = rdr.cur_transl_unit()->get_address_size();
14051 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
14052 size *= 8;
14053
14054 // And the size of the pointer must be the same as the address size
14055 // of the current translation unit.
14056 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
14057
14058 bool is_lvalue = tag == DW_TAG_reference_type;
14059
14060 result.reset(new reference_type_def(utype, is_lvalue, size,
14061 /*alignment=*/0,
14062 location()));
14063 if (corpus_sptr corp = rdr.corpus())
14064 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
14065 result = t;
14066 rdr.associate_die_to_type(die, result, where_offset);
14067 return result;
14068}
14069
14070/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
14071/// DW_TAG_ptr_to_member_type.
14072///
14073/// @param the DWARF reader touse.
14074///
14075/// @param the DIE to consider. It must carry the tag
14076/// DW_TAG_ptr_to_member_type.
14077///
14078/// @param called_from_public_decl true if this function was called
14079/// from a context where either a public function or a public variable
14080/// is being built.
14081///
14082/// @param where_offset the offset of the DIE where we are "logically"
14083/// positionned at, in the DIE tree. This is useful when @p die is
14084/// e.g, DW_TAG_partial_unit that can be included in several places in
14085/// the DIE tree.
14086///
14087/// @return a pointer to the resulting @ref ptr_to_mbr_type.
14089build_ptr_to_mbr_type(reader& rdr,
14090 Dwarf_Die* die,
14091 bool called_from_public_decl,
14092 size_t where_offset)
14093{
14094 ptr_to_mbr_type_sptr result;
14095
14096 if (!die)
14097 return result;
14098
14099 unsigned tag = dwarf_tag(die);
14100 if (tag != DW_TAG_ptr_to_member_type)
14101 return result;
14102
14103 Dwarf_Die data_member_type_die, containing_type_die;
14104
14105 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
14106 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
14107 return result;
14108
14109 type_or_decl_base_sptr data_member_type =
14110 build_ir_node_from_die(rdr, &data_member_type_die,
14111 called_from_public_decl, where_offset);
14112 if (!data_member_type)
14113 return result;
14114
14115 type_or_decl_base_sptr containing_type =
14116 build_ir_node_from_die(rdr, &containing_type_die,
14117 called_from_public_decl, where_offset);
14118 if (!containing_type)
14119 return result;
14120
14122 (is_type(containing_type)))
14123 return result;
14124
14125 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14126 {
14127 result = is_ptr_to_mbr_type(t);
14128 ABG_ASSERT(result);
14129 return result;
14130 }
14131
14132 uint64_t size_in_bits = rdr.cur_transl_unit()->get_address_size();
14133
14134 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
14135 is_type(data_member_type),
14136 is_type(containing_type),
14137 size_in_bits,
14138 /*alignment=*/0,
14139 location()));
14140
14141 rdr.associate_die_to_type(die, result, where_offset);
14142 return result;
14143}
14144
14145/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
14146///
14147/// @param rdr the DWARF reader to consider.
14148///
14149/// @param die the DIE to read from.
14150///
14151/// @param is_method points to a class or union declaration iff we're
14152/// building the type for a method. This is the enclosing class or
14153/// union of the method.
14154///
14155/// @param where_offset the offset of the DIE where we are "logically"
14156/// positioned at, in the DIE tree. This is useful when @p die is
14157/// e.g, DW_TAG_partial_unit that can be included in several places in
14158/// the DIE tree.
14159///
14160/// @return a pointer to the resulting function_type_sptr.
14161static function_type_sptr
14162build_function_type(reader& rdr,
14163 Dwarf_Die* die,
14164 class_or_union_sptr is_method,
14165 size_t where_offset)
14166{
14167 function_type_sptr result;
14168
14169 if (!die)
14170 return result;
14171
14172 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
14173 || dwarf_tag(die) == DW_TAG_subprogram);
14174
14175 const die_source source = rdr.get_die_source(die);
14176
14177 {
14178 size_t off = dwarf_dieoffset(die);
14179 auto i = rdr.die_wip_function_types_map(source).find(off);
14180 if (i != rdr.die_wip_function_types_map(source).end())
14181 {
14182 function_type_sptr fn_type = is_function_type(i->second);
14183 ABG_ASSERT(fn_type);
14184 return fn_type;
14185 }
14186 }
14187
14188 decl_base_sptr type_decl;
14189
14190 translation_unit_sptr tu = rdr.cur_transl_unit();
14191 ABG_ASSERT(tu);
14192
14193 /// If, inside the current translation unit, we've already seen a
14194 /// function type with the same text representation, then reuse that
14195 /// one instead.
14196 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
14197 {
14198 result = is_function_type(t);
14199 ABG_ASSERT(result);
14200 rdr.associate_die_to_type(die, result, where_offset);
14201 return result;
14202 }
14203
14204 bool odr_is_relevant = rdr.odr_is_relevant(die);
14205 if (odr_is_relevant)
14206 {
14207 // So we can rely on the One Definition Rule to say that if
14208 // several different function types have the same name (or
14209 // rather, representation) across the entire binary, then they
14210 // ought to designate the same function type. So let's ensure
14211 // that if we've already seen a function type with the same
14212 // representation as the function type 'die', then it's the same
14213 // type as the one denoted by 'die'.
14214 if (function_type_sptr fn_type =
14215 is_function_type(rdr.lookup_type_artifact_from_die(die)))
14216 {
14217 rdr.associate_die_to_type(die, fn_type, where_offset);
14218 return fn_type;
14219 }
14220 }
14221
14222 // Let's look at the DIE to detect if it's the DIE for a method
14223 // (type). If it is, we can deduce the name of its enclosing class
14224 // and if it's a static or const.
14225 bool is_const = false;
14226 bool is_static = false;
14227 Dwarf_Die object_pointer_die;
14228 Dwarf_Die class_type_die;
14229 bool has_this_parm_die =
14230 die_function_type_is_method_type(rdr, die, where_offset,
14231 object_pointer_die,
14232 class_type_die,
14233 is_static);
14234 if (has_this_parm_die)
14235 {
14236 // The function (type) has a "this" parameter DIE. It means it's
14237 // a member function DIE.
14238 if (!is_static)
14239 if (die_object_pointer_is_for_const_method(&object_pointer_die))
14240 is_const = true;
14241
14242 if (!is_method)
14243 {
14244 // We were initially called as if the function represented
14245 // by DIE was *NOT* a member function. But now we know it's
14246 // a member function. Let's take that into account.
14247 class_or_union_sptr klass_type =
14248 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
14249 /*called_from_pub_decl=*/true,
14250 where_offset));
14251 ABG_ASSERT(klass_type);
14252 is_method = klass_type;
14253 }
14254 }
14255
14256 // Let's create the type early and record it as being for the DIE
14257 // 'die'. This way, when building the sub-type triggers the
14258 // creation of a type matching the same 'die', then we'll reuse this
14259 // one.
14260
14261 result.reset(is_method
14262 ? new method_type(is_method, is_const,
14263 tu->get_address_size(),
14264 /*alignment=*/0)
14265 : new function_type(rdr.env(), tu->get_address_size(),
14266 /*alignment=*/0));
14267 rdr.associate_die_to_type(die, result, where_offset);
14268 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
14269
14270 type_base_sptr return_type;
14271 Dwarf_Die ret_type_die;
14272 if (die_die_attribute(die, DW_AT_type, ret_type_die))
14273 return_type =
14274 is_type(build_ir_node_from_die(rdr, &ret_type_die,
14275 /*called_from_public_decl=*/true,
14276 where_offset));
14277 if (!return_type)
14278 return_type = is_type(build_ir_node_for_void_type(rdr));
14279 result->set_return_type(return_type);
14280
14281 Dwarf_Die child;
14282 function_decl::parameters function_parms;
14283
14284 if (dwarf_child(die, &child) == 0)
14285 do
14286 {
14287 int child_tag = dwarf_tag(&child);
14288 if (child_tag == DW_TAG_formal_parameter)
14289 {
14290 // This is a "normal" function parameter.
14291 string name, linkage_name;
14292 location loc;
14293 die_loc_and_name(rdr, &child, loc, name, linkage_name);
14295 // Sometimes, bogus compiler emit names that are
14296 // non-ascii garbage. Let's just ditch that for now.
14297 name.clear();
14298 bool is_artificial = die_is_artificial(&child);
14299 type_base_sptr parm_type;
14300 Dwarf_Die parm_type_die;
14301 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
14302 parm_type =
14303 is_type(build_ir_node_from_die(rdr, &parm_type_die,
14304 /*called_from_public_decl=*/true,
14305 where_offset));
14306 if (!parm_type)
14307 continue;
14309 (new function_decl::parameter(parm_type, name, loc,
14310 /*variadic_marker=*/false,
14311 is_artificial));
14312 function_parms.push_back(p);
14313 }
14314 else if (child_tag == DW_TAG_unspecified_parameters)
14315 {
14316 // This is a variadic function parameter.
14317 bool is_artificial = die_is_artificial(&child);
14318
14319 type_base_sptr parm_type =
14320 is_type(build_ir_node_for_variadic_parameter_type(rdr));
14322 (new function_decl::parameter(parm_type,
14323 /*name=*/"",
14324 location(),
14325 /*variadic_marker=*/true,
14326 is_artificial));
14327 function_parms.push_back(p);
14328 // After a DW_TAG_unspecified_parameters tag, we shouldn't
14329 // keep reading for parameters. The
14330 // unspecified_parameters TAG should be the last parameter
14331 // that we record. For instance, if there are multiple
14332 // DW_TAG_unspecified_parameters DIEs then we should care
14333 // only for the first one.
14334 break;
14335 }
14336 }
14337 while (dwarf_siblingof(&child, &child) == 0);
14338
14339 result->set_parameters(function_parms);
14340
14341 tu->bind_function_type_life_time(result);
14342
14343 result->set_is_artificial(true);
14344
14345 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
14346
14347 {
14348 die_function_type_map_type::const_iterator i =
14349 rdr.die_wip_function_types_map(source).
14350 find(dwarf_dieoffset(die));
14351 if (i != rdr.die_wip_function_types_map(source).end())
14352 rdr.die_wip_function_types_map(source).erase(i);
14353 }
14354
14355 maybe_canonicalize_type(result, rdr);
14356 return result;
14357}
14358
14359/// Build a subrange type from a DW_TAG_subrange_type.
14360///
14361/// @param rdr the DWARF reader to consider.
14362///
14363/// @param die the DIE to read from.
14364///
14365/// @param where_offset the offset of the DIE where we are "logically"
14366/// positionned at in the DIE tree. This is useful when @p die is
14367/// e,g, DW_TAG_partial_unit that can be included in several places in
14368/// the DIE tree.
14369///
14370/// @param associate_die_to_type if this is true then the resulting
14371/// type is associated to the @p die, so that next time when the
14372/// system looks up the type associated to it, the current resulting
14373/// type is returned. If false, then no association is done and the
14374/// resulting type can be destroyed right after. This can be useful
14375/// when the sole purpose of building the @ref
14376/// array_type_def::subrange_type is to use some of its method like,
14377/// e.g, its name pretty printing methods.
14378///
14379/// @return the newly built instance of @ref
14380/// array_type_def::subrange_type, or nil if no type could be built.
14382build_subrange_type(reader& rdr,
14383 const Dwarf_Die* die,
14384 size_t where_offset,
14385 bool associate_type_to_die)
14386{
14388
14389 if (!die)
14390 return result;
14391
14392 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
14393 if (tag != DW_TAG_subrange_type)
14394 return result;
14395
14396 string name = die_name(die);
14397
14398 // load the underlying type.
14399 Dwarf_Die underlying_type_die;
14400 type_base_sptr underlying_type;
14401 /* Unless there is an underlying type which says differently. */
14402 bool is_signed = false;
14403 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
14404 underlying_type =
14405 is_type(build_ir_node_from_die(rdr,
14406 &underlying_type_die,
14407 /*called_from_public_decl=*/true,
14408 where_offset));
14409
14410 if (underlying_type)
14411 {
14412 uint64_t ate;
14413 if (die_unsigned_constant_attribute (&underlying_type_die,
14414 DW_AT_encoding,
14415 ate))
14416 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
14417 }
14418
14419 translation_unit::language language = rdr.cur_transl_unit()->get_language();
14420 array_type_def::subrange_type::bound_value lower_bound =
14421 get_default_array_lower_bound(language);
14422 array_type_def::subrange_type::bound_value upper_bound;
14423 uint64_t count = 0;
14424 bool is_non_finite = false;
14425 bool non_zero_count_present = false;
14426
14427 // The DWARF 4 specifications says, in [5.11 Subrange
14428 // Type Entries]:
14429 //
14430 // The subrange entry may have the attributes
14431 // DW_AT_lower_bound and DW_AT_upper_bound to
14432 // specify, respectively, the lower and upper bound
14433 // values of the subrange.
14434 //
14435 // So let's look for DW_AT_lower_bound first.
14436 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14437
14438 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
14439 is_signed, upper_bound);
14440 if (!found_upper_bound)
14441 found_upper_bound = subrange_die_indirect_bound_value(die,
14442 DW_AT_upper_bound,
14443 upper_bound,
14444 is_signed);
14445 // Then, DW_AT_upper_bound.
14446 if (!found_upper_bound)
14447 {
14448 // The DWARF 4 spec says, in [5.11 Subrange Type
14449 // Entries]:
14450 //
14451 // The DW_AT_upper_bound attribute may be replaced
14452 // by a DW_AT_count attribute, whose value
14453 // describes the number of elements in the
14454 // subrange rather than the value of the last
14455 // element."
14456 //
14457 // So, as DW_AT_upper_bound is not present in this
14458 // case, let's see if there is a DW_AT_count.
14459 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
14460 {
14461 if (count)
14462 // DW_AT_count can be present and be set to zero. This is
14463 // for instance the case to model this gcc extension to
14464 // represent flexible arrays:
14465 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
14466 // For instance: int flex_array[0];
14467 non_zero_count_present = true;
14468
14469 // When the count is present and non-zero, we can deduce the
14470 // upper_bound from the lower_bound and the number of
14471 // elements of the array:
14472 int64_t u = lower_bound.get_signed_value() + count;
14473 if (u)
14474 upper_bound = u - 1;
14475 }
14476
14477 if (!non_zero_count_present)
14478 // No upper_bound nor count was present on the DIE, this means
14479 // the array is considered to have an infinite (or rather not
14480 // known) size.
14481 is_non_finite = true;
14482 }
14483
14484 if (UINT64_MAX == upper_bound.get_unsigned_value())
14485 // If the upper_bound size is the max of the integer value
14486 // then it most certainly means unknown size.
14487 is_non_finite = true;
14488
14489 result.reset
14490 (new array_type_def::subrange_type(rdr.env(),
14491 name,
14492 lower_bound,
14493 upper_bound,
14494 location()));
14495 result->is_non_finite(is_non_finite);
14496
14497 if (underlying_type)
14498 result->set_underlying_type(underlying_type);
14499
14500 // Let's ensure the resulting subrange looks metabolically healhty.
14501 ABG_ASSERT(result->is_non_finite()
14502 || (result->get_length() ==
14503 (uint64_t) (result->get_upper_bound()
14504 - result->get_lower_bound() + 1)));
14505
14506 if (associate_type_to_die)
14507 rdr.associate_die_to_type(die, result, where_offset);
14508
14509 return result;
14510}
14511
14512/// Build the sub-ranges of an array type.
14513///
14514/// This is a sub-routine of build_array_type().
14515///
14516/// @param rdr the context to read from.
14517///
14518/// @param die the DIE of tag DW_TAG_array_type which contains
14519/// children DIEs that represent the sub-ranges.
14520///
14521/// @param subranges out parameter. This is set to the sub-ranges
14522/// that are built from @p die.
14523///
14524/// @param where_offset the offset of the DIE where we are "logically"
14525/// positioned at, in the DIE tree. This is useful when @p die is
14526/// e.g, DW_TAG_partial_unit that can be included in several places in
14527/// the DIE tree.
14528static void
14529build_subranges_from_array_type_die(reader& rdr,
14530 const Dwarf_Die* die,
14532 size_t where_offset,
14533 bool associate_type_to_die)
14534{
14535 Dwarf_Die child;
14536
14537 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14538 {
14539 do
14540 {
14541 int child_tag = dwarf_tag(&child);
14542 if (child_tag == DW_TAG_subrange_type)
14543 {
14545 if (associate_type_to_die)
14546 {
14547 // We are being called to create the type, add it to
14548 // the current type graph and associate it to the
14549 // DIE it's been created from.
14551 build_ir_node_from_die(rdr, &child,
14552 /*called_from_public_decl=*/true,
14553 where_offset);
14554 s = is_subrange_type(t);
14555 }
14556 else
14557 // We are being called to create the type but *NOT*
14558 // add it to the current tyupe tree, *NOR* associate
14559 // it to the DIE it's been created from.
14560 s = build_subrange_type(rdr, &child,
14561 where_offset,
14562 /*associate_type_to_die=*/false);
14563 if (s)
14564 subranges.push_back(s);
14565 }
14566 }
14567 while (dwarf_siblingof(&child, &child) == 0);
14568 }
14569}
14570
14571/// Build an array type from a DW_TAG_array_type DIE.
14572///
14573/// @param rdr the DWARF reader to consider.
14574///
14575/// @param die the DIE to read from.
14576///
14577/// @param called_from_public_decl true if this function was called
14578/// from a context where either a public function or a public variable
14579/// is being built.
14580///
14581/// @param where_offset the offset of the DIE where we are "logically"
14582/// positioned at, in the DIE tree. This is useful when @p die is
14583/// e.g, DW_TAG_partial_unit that can be included in several places in
14584/// the DIE tree.
14585///
14586/// @return a pointer to the resulting array_type_def.
14588build_array_type(reader& rdr,
14589 Dwarf_Die* die,
14590 bool called_from_public_decl,
14591 size_t where_offset)
14592{
14593 array_type_def_sptr result;
14594
14595 if (!die)
14596 return result;
14597
14598 unsigned tag = dwarf_tag(die);
14599 if (tag != DW_TAG_array_type)
14600 return result;
14601
14602 decl_base_sptr type_decl;
14603 Dwarf_Die type_die;
14604
14605 if (die_die_attribute(die, DW_AT_type, type_die))
14606 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14607 called_from_public_decl,
14608 where_offset));
14609 if (!type_decl)
14610 return result;
14611
14612 // The call to build_ir_node_from_die() could have triggered the
14613 // creation of the type for this DIE. In that case, just return it.
14614 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14615 {
14616 result = is_array_type(t);
14617 ABG_ASSERT(result);
14618 return result;
14619 }
14620
14621 type_base_sptr type = is_type(type_decl);
14622 ABG_ASSERT(type);
14623
14625
14626 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14627
14628 result.reset(new array_type_def(type, subranges, location()));
14629
14630 return result;
14631}
14632
14633/// Create a typedef_decl from a DW_TAG_typedef DIE.
14634///
14635/// @param rdr the DWARF reader to consider.
14636///
14637/// @param die the DIE to read from.
14638///
14639/// @param called_from_public_decl true if this function was called
14640/// from a context where either a public function or a public variable
14641/// is being built.
14642///
14643/// @param where_offset the offset of the DIE where we are "logically"
14644/// positionned at, in the DIE tree. This is useful when @p die is
14645/// e.g, DW_TAG_partial_unit that can be included in several places in
14646/// the DIE tree.
14647///
14648/// @return the newly created typedef_decl.
14649static typedef_decl_sptr
14650build_typedef_type(reader& rdr,
14651 Dwarf_Die* die,
14652 bool called_from_public_decl,
14653 size_t where_offset)
14654{
14655 typedef_decl_sptr result;
14656
14657 if (!die)
14658 return result;
14659
14660 unsigned tag = dwarf_tag(die);
14661 if (tag != DW_TAG_typedef)
14662 return result;
14663
14664 string name, linkage_name;
14665 location loc;
14666 die_loc_and_name(rdr, die, loc, name, linkage_name);
14667
14668 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14669 if (loc)
14670 result = lookup_typedef_type_per_location(loc.expand(), *corp);
14671
14672 if (!result)
14673 {
14674 type_base_sptr utype;
14675 Dwarf_Die underlying_type_die;
14676 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14677 // A typedef DIE with no underlying type means a typedef to
14678 // void type.
14679 utype = rdr.env().get_void_type();
14680
14681 if (!utype)
14682 utype =
14683 is_type(build_ir_node_from_die(rdr,
14684 &underlying_type_die,
14685 called_from_public_decl,
14686 where_offset));
14687 if (!utype)
14688 return result;
14689
14690 ABG_ASSERT(utype);
14691 result.reset(new typedef_decl(name, utype, loc, linkage_name));
14692
14693 if ((is_class_or_union_type(utype) || is_enum_type(utype))
14694 && is_anonymous_type(utype))
14695 {
14696 // This is a naming typedef for an enum or a class. Let's
14697 // mark the underlying decl as such.
14698 decl_base_sptr decl = is_decl(utype);
14699 ABG_ASSERT(decl);
14700 decl->set_naming_typedef(result);
14701 if (is_class_or_union_type(utype))
14702 rdr.maybe_schedule_declaration_only_class_for_resolution
14703 (is_class_or_union_type(utype));
14704 else if (is_enum_type(utype))
14705 rdr.maybe_schedule_declaration_only_enum_for_resolution
14706 (is_enum_type(utype));
14707 }
14708 }
14709
14710 rdr.associate_die_to_type(die, result, where_offset);
14711
14712 return result;
14713}
14714
14715/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
14716/// denoted by the DIE is not suppressed by a suppression
14717/// specification associated to the current DWARF reader.
14718///
14719/// Note that if a member variable declaration with the same name as
14720/// the name of the DIE we are looking at exists, this function returns
14721/// that existing variable declaration.
14722///
14723/// @param rdr the DWARF reader to use.
14724///
14725/// @param die the DIE representing the variable we are looking at.
14726///
14727/// @param where_offset the offset of the DIE where we are "logically"
14728/// positionned at, in the DIE tree. This is useful when @p die is
14729/// e.g, DW_TAG_partial_unit that can be included in several places in
14730/// the DIE tree.
14731///
14732/// @param result if this is set to an existing var_decl, this means
14733/// that the function will append the new properties it sees on @p die
14734/// to that exising var_decl. Otherwise, if this parameter is NULL, a
14735/// new var_decl is going to be allocated and returned.
14736///
14737/// @param is_required_decl_spec this is true iff the variable to
14738/// build is referred to as being the specification of another
14739/// variable.
14740///
14741/// @return a pointer to the newly created var_decl. If the var_decl
14742/// could not be built, this function returns NULL.
14743static var_decl_sptr
14744build_or_get_var_decl_if_not_suppressed(reader& rdr,
14745 scope_decl *scope,
14746 Dwarf_Die *die,
14747 size_t where_offset,
14748 var_decl_sptr result,
14749 bool is_required_decl_spec)
14750{
14751 var_decl_sptr var;
14752 if (variable_is_suppressed(rdr, scope, die, is_required_decl_spec))
14753 return var;
14754
14755 if (class_decl* class_type = is_class_type(scope))
14756 {
14757 string var_name = die_name(die);
14758 if (!var_name.empty())
14759 if ((var = class_type->find_data_member(var_name)))
14760 return var;
14761 }
14762 var = build_var_decl(rdr, die, where_offset, result);
14763 return var;
14764}
14765
14766/// Build a @ref var_decl out of a DW_TAG_variable DIE.
14767///
14768/// @param rdr the DWARF reader to use.
14769///
14770/// @param die the DIE representing the variable we are looking at.
14771///
14772/// @param where_offset the offset of the DIE where we are "logically"
14773/// positionned at, in the DIE tree. This is useful when @p die is
14774/// e.g, DW_TAG_partial_unit that can be included in several places in
14775/// the DIE tree.
14776///
14777/// @param result if this is set to an existing var_decl, this means
14778/// that the function will append the new properties it sees on @p die
14779/// to that exising var_decl. Otherwise, if this parameter is NULL, a
14780/// new var_decl is going to be allocated and returned.
14781///
14782/// @return a pointer to the newly created var_decl. If the var_decl
14783/// could not be built, this function returns NULL.
14784static var_decl_sptr
14785build_var_decl(reader& rdr,
14786 Dwarf_Die *die,
14787 size_t where_offset,
14788 var_decl_sptr result)
14789{
14790 if (!die)
14791 return result;
14792
14793 int tag = dwarf_tag(die);
14794 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
14795
14796 if (!die_is_public_decl(die))
14797 return result;
14798
14799 type_base_sptr type;
14800 Dwarf_Die type_die;
14801 if (die_die_attribute(die, DW_AT_type, type_die))
14802 {
14803 decl_base_sptr ty =
14804 is_decl(build_ir_node_from_die(rdr, &type_die,
14805 /*called_from_public_decl=*/true,
14806 where_offset));
14807 if (!ty)
14808 return result;
14809 type = is_type(ty);
14810 ABG_ASSERT(type);
14811 }
14812
14813 if (!type && !result)
14814 return result;
14815
14816 string name, linkage_name;
14817 location loc;
14818 die_loc_and_name(rdr, die, loc, name, linkage_name);
14819
14820 if (!result)
14821 result.reset(new var_decl(name, type, loc, linkage_name));
14822 else
14823 {
14824 // We were called to append properties that might have been
14825 // missing from the first version of the variable. And usually
14826 // that missing property is the mangled name or the type.
14827 if (!linkage_name.empty())
14828 result->set_linkage_name(linkage_name);
14829
14830 if (type)
14831 result->set_type(type);
14832 }
14833
14834 // Check if a variable symbol with this name is exported by the elf
14835 // binary. If it is, then set the symbol of the variable, if it's
14836 // not set already.
14837 if (!result->get_symbol())
14838 {
14839 elf_symbol_sptr var_sym;
14840 Dwarf_Addr var_addr;
14841
14842 if (rdr.get_variable_address(die, var_addr))
14843 {
14844 rdr.symtab()->
14845 update_main_symbol(var_addr,
14846 result->get_linkage_name().empty()
14847 ? result->get_name()
14848 : result->get_linkage_name());
14849 var_sym = rdr.variable_symbol_is_exported(var_addr);
14850 }
14851
14852 if (var_sym)
14853 {
14854 result->set_symbol(var_sym);
14855 // If the linkage name is not set or is wrong, set it to
14856 // the name of the underlying symbol.
14857 string linkage_name = result->get_linkage_name();
14858 if (linkage_name.empty()
14859 || !var_sym->get_alias_from_name(linkage_name))
14860 result->set_linkage_name(var_sym->get_name());
14861 result->set_is_in_public_symbol_table(true);
14862 }
14863 }
14864
14865 return result;
14866}
14867
14868/// Test if a given function denoted by its DIE and its scope is
14869/// suppressed by any of the suppression specifications associated to
14870/// a given context of ELF/DWARF reading.
14871///
14872/// Note that a non-member function which symbol is not exported is
14873/// also suppressed.
14874///
14875/// @param rdr the ELF/DWARF reading content of interest.
14876///
14877/// @param scope of the scope of the function.
14878///
14879/// @param function_die the DIE representing the function.
14880///
14881/// @param is_declaration_only is true if the DIE denoted by @p die is
14882/// a declaration-only DIE.
14883///
14884/// @return true iff @p function_die is suppressed by at least one
14885/// suppression specification attached to the @p rdr.
14886static bool
14887function_is_suppressed(const reader& rdr,
14888 const scope_decl* scope,
14889 Dwarf_Die *function_die,
14890 bool is_declaration_only)
14891{
14892 if (function_die == 0
14893 || dwarf_tag(function_die) != DW_TAG_subprogram)
14894 return false;
14895
14896 string fname = die_string_attribute(function_die, DW_AT_name);
14897 string flinkage_name = die_linkage_name(function_die);
14898 if (flinkage_name.empty() && rdr.die_is_in_c(function_die))
14899 flinkage_name = fname;
14900 string qualified_name = build_qualified_name(scope, fname);
14901
14902 // A non-member non-static function which symbol is not exported is
14903 // suppressed.
14904 //
14905 // Note that if the non-member non-static function has an undefined
14906 // symbol, by default, it's not suppressed. Unless we are asked to
14907 // drop undefined symbols too.
14908 if (!is_class_type(scope)
14909 && (!is_declaration_only || rdr.drop_undefined_syms()))
14910 {
14911 Dwarf_Addr fn_addr;
14912 if (!rdr.get_function_address(function_die, fn_addr))
14913 return true;
14914
14915 elf_symbol_sptr symbol =
14916 rdr.function_symbol_is_exported(fn_addr);
14917 if (!symbol)
14918 return true;
14919 if (!symbol->is_suppressed())
14920 return false;
14921
14922 // Since there is only one symbol in DWARF associated with an elf_symbol,
14923 // we can assume this is the main symbol then. Otherwise the main hinting
14924 // did not work as expected.
14925 ABG_ASSERT(symbol->is_main_symbol());
14926 if (symbol->has_aliases())
14927 for (elf_symbol_sptr a = symbol->get_next_alias();
14928 !a->is_main_symbol(); a = a->get_next_alias())
14929 if (!a->is_suppressed())
14930 return false;
14931 }
14932
14933 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
14934 /*require_drop_property=*/true);
14935}
14936
14937/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
14938/// function denoted by the DIE is not suppressed by a suppression
14939/// specification associated to the current DWARF reader.
14940///
14941/// Note that if a member function declaration with the same signature
14942/// (pretty representation) as one of the DIE we are looking at
14943/// exists, this function returns that existing function declaration.
14944/// Similarly, if there is already a constructed member function with
14945/// the same linkage name as the one on the DIE, this function returns
14946/// that member function.
14947///
14948/// Also note that the function_decl IR returned by this function must
14949/// be passed to finish_member_function_reading because several
14950/// properties from the DIE are actually read by that function, and
14951/// the corresponding properties on the function_decl IR are updated
14952/// accordingly. This is done to support "updating" a function_decl
14953/// IR with properties scathered across several DIEs.
14954///
14955/// @param rdr the DWARF reader to use.
14956///
14957/// @param scope the scope of the function we are looking at.
14958///
14959/// @param fn_die the DIE representing the function we are looking at.
14960///
14961/// @param where_offset the offset of the DIE where we are "logically"
14962/// positionned at, in the DIE tree. This is useful when @p die is
14963/// e.g, DW_TAG_partial_unit that can be included in several places in
14964/// the DIE tree.
14965///
14966/// @param is_declaration_only is true if the DIE denoted by @p fn_die
14967/// is a declaration-only DIE.
14968///
14969/// @param result if this is set to an existing function_decl, this
14970/// means that the function will append the new properties it sees on
14971/// @p fn_die to that exising function_decl. Otherwise, if this
14972/// parameter is NULL, a new function_decl is going to be allocated
14973/// and returned.
14974///
14975/// @return a pointer to the newly created var_decl. If the var_decl
14976/// could not be built, this function returns NULL.
14977static function_decl_sptr
14978build_or_get_fn_decl_if_not_suppressed(reader& rdr,
14979 scope_decl *scope,
14980 Dwarf_Die *fn_die,
14981 size_t where_offset,
14982 bool is_declaration_only,
14983 function_decl_sptr result)
14984{
14986 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
14987 return fn;
14988
14989 string name = die_name(fn_die);
14990 string linkage_name = die_linkage_name(fn_die);
14991 bool is_dtor = !name.empty() && name[0]== '~';
14992 bool is_virtual = false;
14993 if (is_dtor)
14994 {
14995 Dwarf_Attribute attr;
14996 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
14997 DW_AT_vtable_elem_location,
14998 &attr))
14999 is_virtual = true;
15000 }
15001
15002
15003 // If we've already built an IR for a function with the same
15004 // signature (from another DIE), reuse it, unless that function is a
15005 // virtual C++ destructor. Several virtual C++ destructors with the
15006 // same signature can be implemented by several different ELF
15007 // symbols. So re-using C++ destructors like that can lead to us
15008 // missing some destructors.
15009 if (!result && (!(is_dtor && is_virtual)))
15010 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
15011 {
15012 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
15013 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
15014 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
15015 return fn;
15016 }
15017
15018 // If a member function with the same linkage name as the one
15019 // carried by the DIE already exists, then return it.
15020 if (class_decl* klass = is_class_type(scope))
15021 {
15022 string linkage_name = die_linkage_name(fn_die);
15023 fn = klass->find_member_function_sptr(linkage_name);
15024 if (fn)
15025 // We found a member function that has the same signature.
15026 // Let's mark it for update.
15027 result = fn;
15028 }
15029
15030 if (!fn || !fn->get_symbol())
15031 // We haven't yet been able to construct a function IR, or, we
15032 // have one 'partial' function IR that doesn't have any associated
15033 // symbol yet. Note that in the later case, a function IR without
15034 // any associated symbol will be dropped on the floor by
15035 // potential_member_fn_should_be_dropped. So let's build or a new
15036 // function IR or complete the existing partial IR.
15037 fn = build_function_decl(rdr, fn_die, where_offset, result);
15038
15039 return fn;
15040}
15041
15042/// Test if a given variable denoted by its DIE and its scope is
15043/// suppressed by any of the suppression specifications associated to
15044/// a given context of ELF/DWARF reading.
15045///
15046/// @param rdr the ELF/DWARF reading content of interest.
15047///
15048/// @param scope of the scope of the variable.
15049///
15050/// @param variable_die the DIE representing the variable.
15051///
15052/// @param is_required_decl_spec if true, means that the @p
15053/// variable_die being considered is for a variable decl that is a
15054/// specification for a concrete variable being built.
15055///
15056/// @return true iff @p variable_die is suppressed by at least one
15057/// suppression specification attached to the @p rdr.
15058static bool
15059variable_is_suppressed(const reader& rdr,
15060 const scope_decl* scope,
15061 Dwarf_Die *variable_die,
15062 bool is_required_decl_spec)
15063{
15064 if (variable_die == 0
15065 || (dwarf_tag(variable_die) != DW_TAG_variable
15066 && dwarf_tag(variable_die) != DW_TAG_member))
15067 return false;
15068
15069 string name = die_string_attribute(variable_die, DW_AT_name);
15070 string linkage_name = die_linkage_name(variable_die);
15071 if (linkage_name.empty() && rdr.die_is_in_c(variable_die))
15072 linkage_name = name;
15073 string qualified_name = build_qualified_name(scope, name);
15074
15075 // If a non member variable that is a declaration (has no defined
15076 // and exported symbol) and is not the specification of another
15077 // concrete variable, then it's suppressed. This is a size
15078 // optimization; it removes useless declaration-only variables from
15079 // the IR.
15080 if (!is_class_type(scope) && !is_required_decl_spec)
15081 {
15082 Dwarf_Addr var_addr = 0;
15083 if (!rdr.get_variable_address(variable_die, var_addr))
15084 return true;
15085
15086 elf_symbol_sptr symbol =
15087 rdr.variable_symbol_is_exported(var_addr);
15088 if (!symbol)
15089 return true;
15090 if (!symbol->is_suppressed())
15091 return false;
15092
15093 // Since there is only one symbol in DWARF associated with an elf_symbol,
15094 // we can assume this is the main symbol then. Otherwise the main hinting
15095 // did not work as expected.
15096 ABG_ASSERT(symbol->is_main_symbol());
15097 if (symbol->has_aliases())
15098 for (elf_symbol_sptr a = symbol->get_next_alias();
15099 !a->is_main_symbol(); a = a->get_next_alias())
15100 if (!a->is_suppressed())
15101 return false;
15102 }
15103
15105 qualified_name,
15106 linkage_name,
15107 /*require_drop_property=*/true);
15108}
15109
15110/// Test if a type (designated by a given DIE) in a given scope is
15111/// suppressed by the suppression specifications that are associated
15112/// to a given DWARF reader.
15113///
15114/// @param rdr the DWARF reader to consider.
15115///
15116/// @param scope of the scope of the type DIE to consider.
15117///
15118/// @param type_die the DIE that designates the type to consider.
15119///
15120/// @param type_is_private out parameter. If this function returns
15121/// true (the type @p type_die is suppressed) and if the type was
15122/// suppressed because it's private then this parameter is set to
15123/// true.
15124///
15125/// @return true iff the type designated by the DIE @p type_die, in
15126/// the scope @p scope is suppressed by at the suppression
15127/// specifications associated to the current DWARF reader.
15128static bool
15129type_is_suppressed(const reader& rdr,
15130 const scope_decl* scope,
15131 Dwarf_Die *type_die,
15132 bool &type_is_private)
15133{
15134 if (type_die == 0
15135 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
15136 && dwarf_tag(type_die) != DW_TAG_class_type
15137 && dwarf_tag(type_die) != DW_TAG_structure_type
15138 && dwarf_tag(type_die) != DW_TAG_union_type))
15139 return false;
15140
15141 string type_name, linkage_name;
15142 location type_location;
15143 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15144 string qualified_name = build_qualified_name(scope, type_name);
15145
15146 return suppr::is_type_suppressed(rdr,
15147 qualified_name,
15148 type_location,
15149 type_is_private,
15150 /*require_drop_property=*/true);
15151}
15152
15153/// Test if a type (designated by a given DIE) in a given scope is
15154/// suppressed by the suppression specifications that are associated
15155/// to a given DWARF reader.
15156///
15157/// @param rdr the DWARF reader to consider.
15158///
15159/// @param scope of the scope of the type DIE to consider.
15160///
15161/// @param type_die the DIE that designates the type to consider.
15162///
15163/// @return true iff the type designated by the DIE @p type_die, in
15164/// the scope @p scope is suppressed by at the suppression
15165/// specifications associated to the current DWARF reader.
15166static bool
15167type_is_suppressed(const reader& rdr,
15168 const scope_decl* scope,
15169 Dwarf_Die *type_die)
15170{
15171 bool type_is_private = false;
15172 return type_is_suppressed(rdr, scope, type_die, type_is_private);
15173}
15174
15175/// Get the opaque version of a type that was suppressed because it's
15176/// a private type.
15177///
15178/// The opaque version version of the type is just a declared-only
15179/// version of the type (class, union or enum type) denoted by @p
15180/// type_die.
15181///
15182/// @param rdr the DWARF reader in use.
15183///
15184/// @param scope the scope of the type die we are looking at.
15185///
15186/// @param type_die the type DIE we are looking at.
15187///
15188/// @param where_offset the offset of the DIE where we are "logically"
15189/// positionned at, in the DIE tree. This is useful when @p die is
15190/// e.g, DW_TAG_partial_unit that can be included in several places in
15191/// the DIE tree.
15192///
15193/// @return the opaque version of the type denoted by @p type_die or
15194/// nil if no opaque version was found.
15196get_opaque_version_of_type(reader &rdr,
15197 scope_decl *scope,
15198 Dwarf_Die *type_die,
15199 size_t where_offset)
15200{
15202
15203 if (type_die == 0)
15204 return result;
15205
15206 unsigned tag = dwarf_tag(type_die);
15207 if (tag != DW_TAG_class_type
15208 && tag != DW_TAG_structure_type
15209 && tag != DW_TAG_union_type
15210 && tag != DW_TAG_enumeration_type)
15211 return result;
15212
15213 string type_name, linkage_name;
15214 location type_location;
15215 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15216 if (!type_location)
15217 return result;
15218
15219 string qualified_name = build_qualified_name(scope, type_name);
15220
15221 //
15222 // TODO: also handle declaration-only unions. To do that, we mostly
15223 // need to adapt add_or_update_union_type to make it schedule
15224 // declaration-only unions for resolution too.
15225 //
15226 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
15227 {
15228 string_classes_or_unions_map::const_iterator i =
15229 rdr.declaration_only_classes().find(qualified_name);
15230 if (i != rdr.declaration_only_classes().end())
15231 result = i->second.back();
15232
15233 if (!result)
15234 {
15235 // So we didn't find any pre-existing forward-declared-only
15236 // class for the class definition that we could return as an
15237 // opaque type. So let's build one.
15238 //
15239 // TODO: we need to be able to do this for unions too!
15240 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
15241 /*alignment=*/0, /*size=*/0,
15242 tag == DW_TAG_structure_type,
15243 type_location,
15244 decl_base::VISIBILITY_DEFAULT));
15245 klass->set_is_declaration_only(true);
15246 klass->set_is_artificial(die_is_artificial(type_die));
15247 add_decl_to_scope(klass, scope);
15248 rdr.associate_die_to_type(type_die, klass, where_offset);
15249 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
15250 result = klass;
15251 }
15252 }
15253
15254 if (tag == DW_TAG_enumeration_type)
15255 {
15256 string_enums_map::const_iterator i =
15257 rdr.declaration_only_enums().find(qualified_name);
15258 if (i != rdr.declaration_only_enums().end())
15259 result = i->second.back();
15260
15261 if (!result)
15262 {
15263 uint64_t size = 0;
15264 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
15265 size *= 8;
15266 type_decl_sptr underlying_type =
15267 build_enum_underlying_type(rdr, type_name, size,
15268 /*anonymous=*/true);
15269 enum_type_decl::enumerators enumeratorz;
15270 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
15271 type_location,
15272 underlying_type,
15273 enumeratorz,
15274 linkage_name));
15275 enum_type->set_is_artificial(die_is_artificial(type_die));
15276 add_decl_to_scope(enum_type, scope);
15277 result = enum_type;
15278 }
15279 }
15280
15281 return result;
15282}
15283
15284/// Create a function symbol with a given name.
15285///
15286/// @param sym_name the name of the symbol to create.
15287///
15288/// @param env the environment to create the symbol in.
15289///
15290/// @return the newly created symbol.
15292create_default_fn_sym(const string& sym_name, const environment& env)
15293{
15295 elf_symbol_sptr result =
15297 /*symbol index=*/ 0,
15298 /*symbol size=*/ 0,
15299 sym_name,
15300 /*symbol type=*/ elf_symbol::FUNC_TYPE,
15301 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
15302 /*symbol is defined=*/ true,
15303 /*symbol is common=*/ false,
15304 /*symbol version=*/ ver,
15305 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
15306 return result;
15307}
15308
15309/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
15310///
15311/// @param rdr the DWARF reader to use
15312///
15313/// @param die the DW_TAG_subprogram DIE to read from.
15314///
15315/// @param where_offset the offset of the DIE where we are "logically"
15316/// positionned at, in the DIE tree. This is useful when @p die is
15317/// e.g, DW_TAG_partial_unit that can be included in several places in
15318/// the DIE tree.
15319///
15320/// @param called_for_public_decl this is set to true if the function
15321/// was called for a public (function) decl.
15322static function_decl_sptr
15323build_function_decl(reader& rdr,
15324 Dwarf_Die* die,
15325 size_t where_offset,
15327{
15328 function_decl_sptr result = fn;
15329 if (!die)
15330 return result;
15331 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
15332
15333 if (!die_is_public_decl(die))
15334 return result;
15335
15336 translation_unit_sptr tu = rdr.cur_transl_unit();
15337 ABG_ASSERT(tu);
15338
15339 string fname, flinkage_name;
15340 location floc;
15341 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
15342
15343 size_t is_inline = die_is_declared_inline(die);
15344 class_or_union_sptr is_method =
15345 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
15346
15347 if (result)
15348 {
15349 // Add the properties that might have been missing from the
15350 // first declaration of the function. For now, it usually is
15351 // the mangled name that goes missing in the first declarations.
15352 //
15353 // Also note that if 'fn' has just been cloned, the current
15354 // linkage name (of the current DIE) might be different from the
15355 // linkage name of 'fn'. In that case, update the linkage name
15356 // of 'fn' too.
15357 if (!flinkage_name.empty()
15358 && result->get_linkage_name() != flinkage_name)
15359 result->set_linkage_name(flinkage_name);
15360 if (floc)
15361 if (!result->get_location())
15362 result->set_location(floc);
15363 }
15364 else
15365 {
15366 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
15367 where_offset));
15368 if (!fn_type)
15369 return result;
15370
15371 maybe_canonicalize_type(fn_type, rdr);
15372
15373 result.reset(is_method
15374 ? new method_decl(fname, fn_type,
15375 is_inline, floc,
15376 flinkage_name)
15377 : new function_decl(fname, fn_type,
15378 is_inline, floc,
15379 flinkage_name));
15380 }
15381
15382 // Set the symbol of the function. If the linkage name is not set
15383 // or is wrong, set it to the name of the underlying symbol.
15384 if (!result->get_symbol())
15385 {
15386 elf_symbol_sptr fn_sym;
15387 Dwarf_Addr fn_addr;
15388 if (rdr.get_function_address(die, fn_addr))
15389 {
15390 rdr.symtab()->
15391 update_main_symbol(fn_addr,
15392 result->get_linkage_name().empty()
15393 ? result->get_name()
15394 : result->get_linkage_name());
15395 fn_sym = rdr.function_symbol_is_exported(fn_addr);
15396 }
15397
15398 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
15399 {
15400 result->set_symbol(fn_sym);
15401 string linkage_name = result->get_linkage_name();
15402 if (linkage_name.empty())
15403 result->set_linkage_name(fn_sym->get_name());
15404 result->set_is_in_public_symbol_table(true);
15405 }
15406 }
15407
15408 rdr.associate_die_to_type(die, result->get_type(), where_offset);
15409
15410 size_t die_offset = dwarf_dieoffset(die);
15411
15412 if (fn
15413 && is_member_function(fn)
15415 && !result->get_linkage_name().empty())
15416 // This function is a virtual member function which has its
15417 // linkage name *and* and has its underlying symbol correctly set.
15418 // It thus doesn't need any fixup related to elf symbol. So
15419 // remove it from the set of virtual member functions with linkage
15420 // names and no elf symbol that need to be fixed up.
15421 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
15422 return result;
15423}
15424
15425/// Canonicalize a type if it's suitable for early canonicalizing, or,
15426/// if it's not, schedule it for late canonicalization, after the
15427/// debug info of the current translation unit has been fully read.
15428///
15429/// A (composite) type is deemed suitable for early canonicalizing iff
15430/// all of its sub-types are canonicalized themselve. Non composite
15431/// types are always deemed suitable for early canonicalization.
15432///
15433/// Note that this function knows how to deal with anonymous classes,
15434/// structs and enums, unlike the overload below:
15435///
15436/// @param t the type DIE to consider for canonicalization.
15437///
15438/// @param rdr the @ref reader to use.
15439static void
15440maybe_canonicalize_type(const type_base_sptr& t,
15441 reader& rdr)
15442{
15443 if (!t)
15444 return;
15445
15446 type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15447 if (is_class_type(peeled_type)
15448 || is_union_type(peeled_type)
15449 || is_function_type(peeled_type)
15450 || is_array_type(peeled_type)
15451 || is_qualified_type(peeled_type)
15452 || is_enum_type(peeled_type)
15453 ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15454 // We delay canonicalization of classes/unions or typedef,
15455 // pointers, references and array to classes/unions. This is
15456 // because the (underlying) class might not be finished yet and we
15457 // might not be able to able detect it here (thinking about
15458 // classes that are work-in-progress, or classes that might be
15459 // later amended by some DWARF construct). So we err on the safe
15460 // side. We also delay canonicalization for array and qualified
15461 // types because they can be edited (in particular by
15462 // maybe_strip_qualification) after they are initially built.
15463 rdr.schedule_type_for_late_canonicalization(t);
15465 rdr.schedule_type_for_late_canonicalization(t);
15466 else
15467 canonicalize(t);
15468}
15469
15470/// If a given decl is a member type declaration, set its access
15471/// specifier from the DIE that represents it.
15472///
15473/// @param member_type_declaration the member type declaration to
15474/// consider.
15475static void
15476maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15477 Dwarf_Die* die)
15478{
15479 if (is_type(member_type_declaration)
15480 && is_member_decl(member_type_declaration))
15481 {
15482 class_or_union* scope =
15483 is_class_or_union_type(member_type_declaration->get_scope());
15484 ABG_ASSERT(scope);
15485
15486 access_specifier access = public_access;
15487 if (class_decl* cl = is_class_type(scope))
15488 if (!cl->is_struct())
15489 access = private_access;
15490
15491 die_access_specifier(die, access);
15492 set_member_access_specifier(member_type_declaration, access);
15493 }
15494}
15495
15496/// This function tests if a given function which might be intented to
15497/// be added to a class scope (to become a member function) should be
15498/// dropped on the floor instead and not be added to the class.
15499///
15500/// This is a subroutine of build_ir_node_from_die.
15501///
15502/// @param fn the function to consider.
15503///
15504/// @param scope the scope the function is intended to be added
15505/// to. This might be of class type or not.
15506///
15507/// @param fn_die the DWARF die of @p fn.
15508///
15509/// @return true iff @p fn should be dropped on the floor.
15510static bool
15511potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15512 Dwarf_Die *fn_die)
15513{
15514 if (!fn || fn->get_scope())
15515 return false;
15516
15517 if (// A function that is not virtual ...
15518 !die_is_virtual(fn_die)
15519 // ... has a linkage name ...
15520 && !fn->get_linkage_name().empty()
15521 // .. and yet has no ELF symbol associated ...
15522 && !fn->get_symbol())
15523 // Should not be added to its class scope.
15524 //
15525 // Why would it? It's not part of the ABI anyway, as it doesn't
15526 // have any ELF symbol associated and is not a virtual member
15527 // function. It just constitutes bloat in the IR and might even
15528 // induce spurious change reports down the road.
15529 return true;
15530
15531 return false;
15532}
15533
15534/// Build an IR node from a given DIE and add the node to the current
15535/// IR being build and held in the DWARF reader. Doing that is called
15536/// "emitting an IR node for the DIE".
15537///
15538/// @param rdr the DWARF reader.
15539///
15540/// @param die the DIE to consider.
15541///
15542/// @param scope the scope under which the resulting IR node has to be
15543/// added.
15544///
15545/// @param called_from_public_decl set to yes if this function is
15546/// called from the functions used to build a public decl (functions
15547/// and variables). In that case, this function accepts building IR
15548/// nodes representing types. Otherwise, this function only creates
15549/// IR nodes representing public decls (functions and variables).
15550/// This is done to avoid emitting IR nodes for types that are not
15551/// referenced by public functions or variables.
15552///
15553/// @param where_offset the offset of the DIE where we are "logically"
15554/// positionned at, in the DIE tree. This is useful when @p die is
15555/// e.g, DW_TAG_partial_unit that can be included in several places in
15556/// the DIE tree.
15557///
15558/// @param is_required_decl_spec if true, it means the ir node to
15559/// build is for a decl that is a specification for another decl that
15560/// is concrete. If you don't know what this is, set it to false.
15561///
15562/// @param is_declaration_only is true if the DIE denoted by @p die is
15563/// a declaration-only DIE.
15564///
15565/// @return the resulting IR node.
15567build_ir_node_from_die(reader& rdr,
15568 Dwarf_Die* die,
15569 scope_decl* scope,
15570 bool called_from_public_decl,
15571 size_t where_offset,
15572 bool is_declaration_only,
15573 bool is_required_decl_spec)
15574{
15576
15577 if (!die || !scope)
15578 return result;
15579
15580 int tag = dwarf_tag(die);
15581
15582 if (!called_from_public_decl)
15583 {
15584 if (rdr.load_all_types() && die_is_type(die))
15585 /* We were instructed to load debug info for all types,
15586 included those that are not reachable from a public
15587 declaration. So load the debug info for this type. */;
15588 else if (tag != DW_TAG_subprogram
15589 && tag != DW_TAG_variable
15590 && tag != DW_TAG_member
15591 && tag != DW_TAG_namespace)
15592 return result;
15593 }
15594
15595 const die_source source_of_die = rdr.get_die_source(die);
15596
15597 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15598 source_of_die)))
15599 {
15600 if (rdr.load_all_types())
15601 if (called_from_public_decl)
15602 if (type_base_sptr t = is_type(result))
15603 if (corpus *abi_corpus = scope->get_corpus())
15604 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15605
15606 return result;
15607 }
15608
15609 // This is *the* bit of code that ensures we have the right notion
15610 // of "declared" at any point in a DIE chain formed from
15611 // DW_AT_abstract_origin and DW_AT_specification links. There should
15612 // be no other callers of die_is_declaration_only.
15613 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15614
15615 switch (tag)
15616 {
15617 // Type DIEs we support.
15618 case DW_TAG_base_type:
15619 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15620 {
15621 result =
15622 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15623 canonicalize(t);
15624 }
15625 break;
15626
15627 case DW_TAG_typedef:
15628 {
15629 typedef_decl_sptr t = build_typedef_type(rdr, die,
15630 called_from_public_decl,
15631 where_offset);
15632
15633 result = add_decl_to_scope(t, scope);
15634 if (result)
15635 {
15636 maybe_set_member_type_access_specifier(is_decl(result), die);
15637 maybe_canonicalize_type(t, rdr);
15638 }
15639 }
15640 break;
15641
15642 case DW_TAG_pointer_type:
15643 {
15645 build_pointer_type_def(rdr, die,
15646 called_from_public_decl,
15647 where_offset);
15648 if (p)
15649 {
15650 result =
15651 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15652 ABG_ASSERT(result->get_translation_unit());
15653 maybe_canonicalize_type(p, rdr);
15654 }
15655 }
15656 break;
15657
15658 case DW_TAG_reference_type:
15659 case DW_TAG_rvalue_reference_type:
15660 {
15662 build_reference_type(rdr, die,
15663 called_from_public_decl,
15664 where_offset);
15665 if (r)
15666 {
15667 result =
15668 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
15669
15670 rdr.associate_die_to_type(die, r, where_offset);
15671 maybe_canonicalize_type(r, rdr);
15672 }
15673 }
15674 break;
15675
15676 case DW_TAG_ptr_to_member_type:
15677 {
15679 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
15680 where_offset);
15681 if (p)
15682 {
15683 result =
15684 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15685 maybe_canonicalize_type(p, rdr);
15686 }
15687 }
15688 break;
15689
15690 case DW_TAG_const_type:
15691 case DW_TAG_volatile_type:
15692 case DW_TAG_restrict_type:
15693 {
15694 type_base_sptr q =
15695 build_qualified_type(rdr, die,
15696 called_from_public_decl,
15697 where_offset);
15698 if (q)
15699 {
15700 // Strip some potentially redundant type qualifiers from
15701 // the qualified type we just built.
15702 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
15703 rdr);
15704 if (!d)
15705 d = get_type_declaration(q);
15706 ABG_ASSERT(d);
15707 type_base_sptr ty = is_type(d);
15708 // Associate the die to type ty again because 'ty'might be
15709 // different from 'q', because 'ty' is 'q' possibly
15710 // stripped from some redundant type qualifier.
15711 rdr.associate_die_to_type(die, ty, where_offset);
15712 result =
15713 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
15714 maybe_canonicalize_type(is_type(result), rdr);
15715 }
15716 }
15717 break;
15718
15719 case DW_TAG_enumeration_type:
15720 {
15721 bool type_is_private = false;
15722 bool type_suppressed =
15723 type_is_suppressed(rdr, scope, die, type_is_private);
15724 if (type_suppressed && type_is_private)
15725 {
15726 // The type is suppressed because it's private. If other
15727 // non-suppressed and declaration-only instances of this
15728 // type exist in the current corpus, then it means those
15729 // non-suppressed instances are opaque versions of the
15730 // suppressed private type. Lets return one of these opaque
15731 // types then.
15732 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15733 maybe_canonicalize_type(is_type(result), rdr);
15734 }
15735 else if (!type_suppressed)
15736 {
15737 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
15738 where_offset,
15739 is_declaration_only);
15740 result = add_decl_to_scope(e, scope);
15741 if (result)
15742 {
15743 maybe_set_member_type_access_specifier(is_decl(result), die);
15744 maybe_canonicalize_type(is_type(result), rdr);
15745 }
15746 }
15747 }
15748 break;
15749
15750 case DW_TAG_class_type:
15751 case DW_TAG_structure_type:
15752 {
15753 bool type_is_private = false;
15754 bool type_suppressed=
15755 type_is_suppressed(rdr, scope, die, type_is_private);
15756
15757 if (type_suppressed && type_is_private)
15758 {
15759 // The type is suppressed because it's private. If other
15760 // non-suppressed and declaration-only instances of this
15761 // type exist in the current corpus, then it means those
15762 // non-suppressed instances are opaque versions of the
15763 // suppressed private type. Lets return one of these opaque
15764 // types then.
15765 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15766 maybe_canonicalize_type(is_type(result), rdr);
15767 }
15768 else if (!type_suppressed)
15769 {
15770 Dwarf_Die spec_die;
15771 scope_decl_sptr scop;
15772 class_decl_sptr klass;
15773 if (die_die_attribute(die, DW_AT_specification, spec_die))
15774 {
15775 scope_decl_sptr skope =
15776 get_scope_for_die(rdr, &spec_die,
15777 called_from_public_decl,
15778 where_offset);
15779 ABG_ASSERT(skope);
15780 decl_base_sptr cl =
15781 is_decl(build_ir_node_from_die(rdr, &spec_die,
15782 skope.get(),
15783 called_from_public_decl,
15784 where_offset,
15785 is_declaration_only,
15786 /*is_required_decl_spec=*/false));
15787 ABG_ASSERT(cl);
15788 klass = dynamic_pointer_cast<class_decl>(cl);
15789 ABG_ASSERT(klass);
15790
15791 klass =
15792 add_or_update_class_type(rdr, die,
15793 skope.get(),
15794 tag == DW_TAG_structure_type,
15795 klass,
15796 called_from_public_decl,
15797 where_offset,
15798 is_declaration_only);
15799 }
15800 else
15801 klass =
15802 add_or_update_class_type(rdr, die, scope,
15803 tag == DW_TAG_structure_type,
15805 called_from_public_decl,
15806 where_offset,
15807 is_declaration_only);
15808 result = klass;
15809 if (klass)
15810 {
15811 maybe_set_member_type_access_specifier(klass, die);
15812 maybe_canonicalize_type(klass, rdr);
15813 }
15814 }
15815 }
15816 break;
15817 case DW_TAG_union_type:
15818 if (!type_is_suppressed(rdr, scope, die))
15819 {
15820 union_decl_sptr union_type =
15821 add_or_update_union_type(rdr, die, scope,
15822 union_decl_sptr(),
15823 called_from_public_decl,
15824 where_offset,
15825 is_declaration_only);
15826 if (union_type)
15827 {
15828 maybe_set_member_type_access_specifier(union_type, die);
15829 maybe_canonicalize_type(union_type, rdr);
15830 }
15831 result = union_type;
15832 }
15833 break;
15834 case DW_TAG_string_type:
15835 break;
15836 case DW_TAG_subroutine_type:
15837 {
15838 function_type_sptr f = build_function_type(rdr, die,
15840 where_offset);
15841 if (f)
15842 {
15843 result = f;
15844 result->set_is_artificial(false);
15845 maybe_canonicalize_type(f, rdr);
15846 }
15847 }
15848 break;
15849 case DW_TAG_array_type:
15850 {
15851 array_type_def_sptr a = build_array_type(rdr,
15852 die,
15853 called_from_public_decl,
15854 where_offset);
15855 if (a)
15856 {
15857 result =
15858 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
15859 rdr.associate_die_to_type(die, a, where_offset);
15860 maybe_canonicalize_type(a, rdr);
15861 }
15862 break;
15863 }
15864 case DW_TAG_subrange_type:
15865 {
15866 // If we got here, this means the subrange type is a "free
15867 // form" defined in the global namespace of the current
15868 // translation unit, like what is found in Ada.
15870 build_subrange_type(rdr, die, where_offset);
15871 if (s)
15872 {
15873 result =
15874 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
15875 rdr.associate_die_to_type(die, s, where_offset);
15876 maybe_canonicalize_type(s, rdr);
15877 }
15878 }
15879 break;
15880 case DW_TAG_packed_type:
15881 break;
15882 case DW_TAG_set_type:
15883 break;
15884 case DW_TAG_file_type:
15885 break;
15886 case DW_TAG_thrown_type:
15887 break;
15888 case DW_TAG_interface_type:
15889 break;
15890 case DW_TAG_unspecified_type:
15891 break;
15892 case DW_TAG_shared_type:
15893 break;
15894
15895 case DW_TAG_compile_unit:
15896 // We shouldn't reach this point b/c this should be handled by
15897 // build_translation_unit.
15899
15900 case DW_TAG_namespace:
15901 case DW_TAG_module:
15902 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
15903 break;
15904
15905 case DW_TAG_variable:
15906 case DW_TAG_member:
15907 {
15908 Dwarf_Die spec_die;
15909 bool var_is_cloned = false;
15910
15911 if (tag == DW_TAG_member)
15912 ABG_ASSERT(!rdr.die_is_in_c(die));
15913
15914 if (die_die_attribute(die, DW_AT_specification, spec_die, false)
15915 || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
15916 spec_die, false)))
15917 {
15918 scope_decl_sptr spec_scope =
15919 get_scope_for_die(rdr, &spec_die,
15920 /*called_from_public_decl=*/
15921 die_is_effectively_public_decl(rdr, die),
15922 where_offset);
15923 if (spec_scope)
15924 {
15925 decl_base_sptr d =
15926 is_decl(build_ir_node_from_die(rdr, &spec_die,
15927 spec_scope.get(),
15928 called_from_public_decl,
15929 where_offset,
15930 is_declaration_only,
15931 /*is_required_decl_spec=*/true));
15932 if (d)
15933 {
15934 var_decl_sptr m =
15935 dynamic_pointer_cast<var_decl>(d);
15936 if (var_is_cloned)
15937 m = m->clone();
15938 m = build_var_decl(rdr, die, where_offset, m);
15939 if (is_data_member(m))
15940 {
15941 set_member_is_static(m, true);
15942 rdr.associate_die_to_decl(die, m, where_offset,
15943 /*associate_by_repr=*/false);
15944 }
15945 else
15946 {
15948 rdr.var_decls_to_re_add_to_tree().push_back(m);
15949 }
15950 ABG_ASSERT(m->get_scope());
15951 rdr.maybe_add_var_to_exported_decls(m.get());
15952 result = m;
15953 }
15954 }
15955 }
15956 else if (var_decl_sptr v =
15957 build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
15958 where_offset,
15959 /*result=*/var_decl_sptr(),
15960 is_required_decl_spec))
15961 {
15962 result = add_decl_to_scope(v, scope);
15963 ABG_ASSERT(is_decl(result)->get_scope());
15964 v = dynamic_pointer_cast<var_decl>(result);
15965 ABG_ASSERT(v);
15966 ABG_ASSERT(v->get_scope());
15967 rdr.var_decls_to_re_add_to_tree().push_back(v);
15968 rdr.maybe_add_var_to_exported_decls(v.get());
15969 }
15970 }
15971 break;
15972
15973 case DW_TAG_subprogram:
15974 {
15975 if (die_is_artificial(die))
15976 break;
15977
15978 Dwarf_Die abstract_origin_die;
15979 bool has_abstract_origin = die_die_attribute(die, DW_AT_abstract_origin,
15980 abstract_origin_die,
15981 /*recursive=*/true);
15982
15983
15984 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
15985 where_offset);
15986 scope_decl* interface_scope = scope ? scope : s.get();
15987
15988 class_decl* class_scope = is_class_type(interface_scope);
15989 string linkage_name = die_linkage_name(die);
15990 string spec_linkage_name;
15991 function_decl_sptr existing_fn;
15992
15993 if (class_scope)
15994 {
15995 // The scope of the function DIE we are looking at is a
15996 // class. So we are looking at a member function.
15997 if (!linkage_name.empty())
15998 {
15999 if ((existing_fn =
16000 class_scope->find_member_function_sptr(linkage_name)))
16001 {
16002 // A function with the same linkage name has
16003 // already been created. Let's see if we are a
16004 // clone of it or not.
16005 spec_linkage_name = existing_fn->get_linkage_name();
16006 if (has_abstract_origin
16007 && !spec_linkage_name.empty()
16008 && linkage_name != spec_linkage_name)
16009 {
16010 // The current DIE has 'existing_fn' as
16011 // abstract orign, and has a linkage name that
16012 // is different from from the linkage name of
16013 // 'existing_fn'. That means, the current DIE
16014 // represents a clone of 'existing_fn'.
16015 existing_fn = existing_fn->clone();
16016 }
16017 }
16018 }
16019 }
16020
16021 rdr.scope_stack().push(interface_scope);
16022
16023 // Either we create a branch new IR for the current function
16024 // DIE we are looking at, or we complete an existing IR node
16025 // with the new completementary information carried by this
16026 // DIE for that IR node.
16027 result =
16028 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
16029 die, where_offset,
16030 is_declaration_only,
16031 existing_fn);
16032
16033 if (result && !existing_fn)
16034 {
16035 // We built a brand new IR for the function DIE. Now
16036 // there should be enough information on that IR to know
16037 // if we should drop it on the floor or keep it ...
16038 if (potential_member_fn_should_be_dropped(is_function_decl(result),
16039 die)
16040 && !is_required_decl_spec)
16041 {
16042 // So apparently we should drop that function IR on
16043 // the floor. Let's do so.
16044 result.reset();
16045 break;
16046 }
16047 // OK so we came to the conclusion that we need to keep
16048 // the function. So let's add it to its scope.
16049 result = add_decl_to_scope(is_decl(result), interface_scope);
16050 }
16051
16053 if (fn && is_member_function(fn))
16054 {
16055 class_decl_sptr klass(static_cast<class_decl*>(interface_scope),
16056 sptr_utils::noop_deleter());
16057 ABG_ASSERT(klass);
16058 finish_member_function_reading(die, fn, klass, rdr);
16059 }
16060
16061 if (fn)
16062 {
16063 if (!is_member_function(fn)
16065 // Virtual member functions are added to the set of
16066 // functions exported by the current ABI corpus *after*
16067 // the canonicalization of their parent type. So let's
16068 // not do it here.
16069 rdr.maybe_add_fn_to_exported_decls(fn.get());
16070 rdr.associate_die_to_decl(die, fn, where_offset,
16071 /*associate_by_repr=*/false);
16072 maybe_canonicalize_type(fn->get_type(), rdr);
16073 }
16074
16075 rdr.scope_stack().pop();
16076 }
16077 break;
16078
16079 case DW_TAG_formal_parameter:
16080 // We should not read this case as it should have been dealt
16081 // with by build_function_decl above.
16083
16084 case DW_TAG_constant:
16085 break;
16086 case DW_TAG_enumerator:
16087 break;
16088
16089 case DW_TAG_partial_unit:
16090 case DW_TAG_imported_unit:
16091 // For now, the DIEs under these are read lazily when they are
16092 // referenced by a public decl DIE that is under a
16093 // DW_TAG_compile_unit, so we shouldn't get here.
16095
16096 // Other declaration we don't really intend to support yet.
16097 case DW_TAG_dwarf_procedure:
16098 case DW_TAG_imported_declaration:
16099 case DW_TAG_entry_point:
16100 case DW_TAG_label:
16101 case DW_TAG_lexical_block:
16102 case DW_TAG_unspecified_parameters:
16103 case DW_TAG_variant:
16104 case DW_TAG_common_block:
16105 case DW_TAG_common_inclusion:
16106 case DW_TAG_inheritance:
16107 case DW_TAG_inlined_subroutine:
16108 case DW_TAG_with_stmt:
16109 case DW_TAG_access_declaration:
16110 case DW_TAG_catch_block:
16111 case DW_TAG_friend:
16112 case DW_TAG_namelist:
16113 case DW_TAG_namelist_item:
16114 case DW_TAG_template_type_parameter:
16115 case DW_TAG_template_value_parameter:
16116 case DW_TAG_try_block:
16117 case DW_TAG_variant_part:
16118 case DW_TAG_imported_module:
16119 case DW_TAG_condition:
16120 case DW_TAG_type_unit:
16121 case DW_TAG_template_alias:
16122 case DW_TAG_lo_user:
16123 case DW_TAG_MIPS_loop:
16124 case DW_TAG_format_label:
16125 case DW_TAG_function_template:
16126 case DW_TAG_class_template:
16127 case DW_TAG_GNU_BINCL:
16128 case DW_TAG_GNU_EINCL:
16129 case DW_TAG_GNU_template_template_param:
16130 case DW_TAG_GNU_template_parameter_pack:
16131 case DW_TAG_GNU_formal_parameter_pack:
16132 case DW_TAG_GNU_call_site:
16133 case DW_TAG_GNU_call_site_parameter:
16134 case DW_TAG_hi_user:
16135 default:
16136 break;
16137 }
16138
16139 if (result && tag != DW_TAG_subroutine_type)
16140 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
16141 /*associate_by_repr=*/false);
16142
16143 if (result)
16144 if (rdr.load_all_types())
16145 if (called_from_public_decl)
16146 if (type_base_sptr t = is_type(result))
16147 if (corpus *abi_corpus = scope->get_corpus())
16148 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
16149
16150 return result;
16151}
16152
16153/// Build the IR node for a void type.
16154///
16155/// @param rdr the DWARF reader to use.
16156///
16157/// @return the void type node.
16158static decl_base_sptr
16159build_ir_node_for_void_type(reader& rdr)
16160{
16161 const environment& env = rdr.env();
16162
16163 type_base_sptr t = env.get_void_type();
16164 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16165 decl_base_sptr type_declaration = get_type_declaration(t);
16166 canonicalize(t);
16167 return type_declaration;
16168}
16169
16170/// Build the IR node for a "pointer to void type".
16171///
16172/// That IR node is shared across the ABI corpus.
16173///
16174/// Note that this function just gets that IR node from the
16175/// environment and, if it's not added to any scope yet, adds it to
16176/// the global scope associated to the current translation unit.
16177///
16178/// @param rdr the DWARF reader to consider.
16179///
16180/// @return the IR node.
16182build_ir_node_for_void_pointer_type(reader& rdr)
16183{
16184 const environment& env = rdr.env();
16185
16186 type_base_sptr t = env.get_void_pointer_type();
16187 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16188 decl_base_sptr type_declaration = get_type_declaration(t);
16189 canonicalize(t);
16190 return type_declaration;
16191}
16192
16193/// Build the IR node for a variadic parameter type.
16194///
16195/// @param rdr the DWARF reader to use.
16196///
16197/// @return the variadic parameter type.
16198static decl_base_sptr
16199build_ir_node_for_variadic_parameter_type(reader &rdr)
16200{
16201
16202 const environment& env = rdr.env();
16203
16204 type_base_sptr t = env.get_variadic_parameter_type();
16205 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16206 decl_base_sptr type_declaration = get_type_declaration(t);
16207 canonicalize(t);
16208 return type_declaration;
16209}
16210
16211/// Build an IR node from a given DIE and add the node to the current
16212/// IR being build and held in the DWARF reader. Doing that is called
16213/// "emitting an IR node for the DIE".
16214///
16215/// @param rdr the DWARF reader.
16216///
16217/// @param die the DIE to consider.
16218///
16219/// @param called_from_public_decl set to yes if this function is
16220/// called from the functions used to build a public decl (functions
16221/// and variables). In that case, this function accepts building IR
16222/// nodes representing types. Otherwise, this function only creates
16223/// IR nodes representing public decls (functions and variables).
16224/// This is done to avoid emitting IR nodes for types that are not
16225/// referenced by public functions or variables.
16226///
16227/// @param where_offset the offset of the DIE where we are "logically"
16228/// positionned at, in the DIE tree. This is useful when @p die is
16229/// e.g, DW_TAG_partial_unit that can be included in several places in
16230/// the DIE tree.
16231///
16232/// @return the resulting IR node.
16234build_ir_node_from_die(reader& rdr,
16235 Dwarf_Die* die,
16236 bool called_from_public_decl,
16237 size_t where_offset)
16238{
16239 if (!die)
16240 return decl_base_sptr();
16241
16242 // Normaly, a decl that is meant to be external has a DW_AT_external
16243 // set. But then some compilers fail to always emit that flag. For
16244 // instance, for static data members, some compilers won't emit the
16245 // DW_AT_external. In that case, we assume that if the variable is
16246 // at global or named namespace scope, then we can assume it's
16247 // external. If the variable doesn't have any ELF symbol associated
16248 // to it, it'll be dropped on the floor anyway. Those variable
16249 // decls are considered as being "effectively public".
16250 bool consider_as_called_from_public_decl =
16251 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
16252 scope_decl_sptr scope = get_scope_for_die(rdr, die,
16253 consider_as_called_from_public_decl,
16254 where_offset);
16255 return build_ir_node_from_die(rdr, die, scope.get(),
16256 called_from_public_decl,
16257 where_offset, true);
16258}
16259
16260/// Create a dwarf::reader.
16261///
16262/// @param elf_path the path to the elf file the reader is to be used
16263/// for.
16264///
16265/// @param debug_info_root_paths a vector to the paths to the
16266/// directories under which the debug info is to be found for @p
16267/// elf_path. Pass an empty vector if the debug info is not in a
16268/// split file.
16269///
16270/// @param environment the environment used by the current context.
16271/// This environment contains resources needed by the DWARF reader and by
16272/// the types and declarations that are to be created later. Note
16273/// that ABI artifacts that are to be compared all need to be created
16274/// within the same environment.
16275///
16276/// Please also note that the life time of this environment object
16277/// must be greater than the life time of the resulting @ref
16278/// reader the context uses resources that are allocated in the
16279/// environment.
16280///
16281/// @param load_all_types if set to false only the types that are
16282/// reachable from publicly exported declarations (of functions and
16283/// variables) are read. If set to true then all types found in the
16284/// debug information are loaded.
16285///
16286/// @param linux_kernel_mode if set to true, then consider the special
16287/// linux kernel symbol tables when determining if a symbol is
16288/// exported or not.
16289///
16290/// @return a smart pointer to the resulting dwarf::reader.
16291elf_based_reader_sptr
16292create_reader(const std::string& elf_path,
16293 const vector<char**>& debug_info_root_paths,
16295 bool load_all_types,
16296 bool linux_kernel_mode)
16297{
16298
16299 reader_sptr r = reader::create(elf_path,
16300 debug_info_root_paths,
16302 load_all_types,
16303 linux_kernel_mode);
16304 return static_pointer_cast<elf_based_reader>(r);
16305}
16306
16307/// Re-initialize a reader so that it can re-used to read
16308/// another binary.
16309///
16310/// @param rdr the context to re-initialize.
16311///
16312/// @param elf_path the path to the elf file the context is to be used
16313/// for.
16314///
16315/// @param debug_info_root_path a pointer to the path to the root
16316/// directory under which the debug info is to be found for @p
16317/// elf_path. Leave this to NULL if the debug info is not in a split
16318/// file.
16319///
16320/// @param environment the environment used by the current context.
16321/// This environment contains resources needed by the DWARF reader and by
16322/// the types and declarations that are to be created later. Note
16323/// that ABI artifacts that are to be compared all need to be created
16324/// within the same environment.
16325///
16326/// Please also note that the life time of this environment object
16327/// must be greater than the life time of the resulting @ref
16328/// reader the context uses resources that are allocated in the
16329/// environment.
16330///
16331/// @param load_all_types if set to false only the types that are
16332/// reachable from publicly exported declarations (of functions and
16333/// variables) are read. If set to true then all types found in the
16334/// debug information are loaded.
16335///
16336/// @param linux_kernel_mode if set to true, then consider the special
16337/// linux kernel symbol tables when determining if a symbol is
16338/// exported or not.
16339///
16340/// @return a smart pointer to the resulting dwarf::reader.
16341void
16343 const std::string& elf_path,
16344 const vector<char**>&debug_info_root_path,
16345 bool read_all_types,
16346 bool linux_kernel_mode)
16347{
16348 reader& r = dynamic_cast<reader&>(rdr);
16349 r.initialize(elf_path, debug_info_root_path,
16350 read_all_types, linux_kernel_mode);
16351}
16352
16353/// Read all @ref abigail::translation_unit possible from the debug info
16354/// accessible from an elf file, stuff them into a libabigail ABI
16355/// Corpus and return it.
16356///
16357/// @param elf_path the path to the elf file.
16358///
16359/// @param debug_info_root_paths a vector of pointers to root paths
16360/// under which to look for the debug info of the elf files that are
16361/// later handled by the Dwfl. This for cases where the debug info is
16362/// split into a different file from the binary we want to inspect.
16363/// On Red Hat compatible systems, this root path is usually
16364/// /usr/lib/debug by default. If this argument is set to NULL, then
16365/// "./debug" and /usr/lib/debug will be searched for sub-directories
16366/// containing the debug info file.
16367///
16368/// @param environment the environment used by the current context.
16369/// This environment contains resources needed by the DWARF reader and by
16370/// the types and declarations that are to be created later. Note
16371/// that ABI artifacts that are to be compared all need to be created
16372/// within the same environment. Also, the lifetime of the
16373/// environment must be greater than the lifetime of the resulting
16374/// corpus because the corpus uses resources that are allocated in the
16375/// environment.
16376///
16377/// @param load_all_types if set to false only the types that are
16378/// reachable from publicly exported declarations (of functions and
16379/// variables) are read. If set to true then all types found in the
16380/// debug information are loaded.
16381///
16382/// @param resulting_corp a pointer to the resulting abigail::corpus.
16383///
16384/// @return the resulting status.
16385corpus_sptr
16386read_corpus_from_elf(const std::string& elf_path,
16387 const vector<char**>& debug_info_root_paths,
16389 bool load_all_types,
16390 fe_iface::status& status)
16391{
16392 elf_based_reader_sptr rdr =
16393 dwarf::reader::create(elf_path, debug_info_root_paths,
16394 environment, load_all_types,
16395 /*linux_kernel_mode=*/false);
16396
16397 return rdr->read_corpus(status);
16398}
16399
16400/// Look into the symbol tables of a given elf file and see if we find
16401/// a given symbol.
16402///
16403/// @param env the environment we are operating from.
16404///
16405/// @param elf_path the path to the elf file to consider.
16406///
16407/// @param symbol_name the name of the symbol to look for.
16408///
16409/// @param demangle if true, try to demangle the symbol name found in
16410/// the symbol table.
16411///
16412/// @param syms the vector of symbols found with the name @p symbol_name.
16413///
16414/// @return true iff the symbol was found among the publicly exported
16415/// symbols of the ELF file.
16416bool
16417lookup_symbol_from_elf(const environment& env,
16418 const string& elf_path,
16419 const string& symbol_name,
16420 bool demangle,
16421 vector<elf_symbol_sptr>& syms)
16422
16423{
16424 if (elf_version(EV_CURRENT) == EV_NONE)
16425 return false;
16426
16427 int fd = open(elf_path.c_str(), O_RDONLY);
16428 if (fd < 0)
16429 return false;
16430
16431 struct stat s;
16432 if (fstat(fd, &s))
16433 return false;
16434
16435 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16436 if (elf == 0)
16437 return false;
16438
16439 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
16440 demangle, syms);
16441 elf_end(elf);
16442 close(fd);
16443
16444 return value;
16445}
16446
16447/// Look into the symbol tables of an elf file to see if a public
16448/// function of a given name is found.
16449///
16450/// @param env the environment we are operating from.
16451///
16452/// @param elf_path the path to the elf file to consider.
16453///
16454/// @param symbol_name the name of the function to look for.
16455///
16456/// @param syms the vector of public function symbols found with the
16457/// name @p symname.
16458///
16459/// @return true iff a function with symbol name @p symbol_name is
16460/// found.
16461bool
16463 const string& path,
16464 const string& symname,
16465 vector<elf_symbol_sptr>& syms)
16466{
16467 if (elf_version(EV_CURRENT) == EV_NONE)
16468 return false;
16469
16470 int fd = open(path.c_str(), O_RDONLY);
16471 if (fd < 0)
16472 return false;
16473
16474 struct stat s;
16475 if (fstat(fd, &s))
16476 return false;
16477
16478 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16479 if (elf == 0)
16480 return false;
16481
16482 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16483 elf_end(elf);
16484 close(fd);
16485
16486 return value;
16487}
16488
16489}// end namespace dwarf
16490
16491}// 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:1695
This contains the private implementation of the suppression engine of libabigail.
Utilities to ease the wrapping of C types into std::shared_ptr.
This contains the private implementation of the suppression engine of libabigail.
This contains the declarations for the symtab reader.
#define ABG_ASSERT_NOT_REACHED
A macro that expands to aborting the program when executed.
Simplified implementation of std::optional just enough to be used as a replacement for our purposes a...
const string & elf_architecture() const
Get the value of the 'ARCHITECTURE' property of the current ELF file.
const Dwarf * dwarf_debug_info() const
Getter of the handle used to access DWARF information from the current ELF file.
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_exported(GElf_Addr symbol_address) const
Test if a given variable symbol has been exported.
elf_symbol_sptr function_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given function symbol has been exported.
const vector< string > & dt_needed() const
Get the value of the DT_NEEDED property of the current ELF file.
The common interface of readers based on ELF.
elf_based_reader(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &env)
Readers that implement this interface must provide a factory method to create a reader instance as th...
virtual void initialize(const std::string &elf_path, const vector< char ** > &debug_info_root_paths)
(re)Initialize) the resources used by the current reader.
status
The status of the fe_iface::read_corpus call.
Definition: abg-fe-iface.h:38
@ STATUS_DEBUG_INFO_NOT_FOUND
This status is for when the debug info could not be read.
Definition: abg-fe-iface.h:46
@ STATUS_ALT_DEBUG_INFO_NOT_FOUND
This status is for when the alternate debug info could not be found.
Definition: abg-fe-iface.h:50
@ STATUS_UNKNOWN
The status is in an unknown state.
Definition: abg-fe-iface.h:40
const options_type & options() const
Getter of the the options of the current Front End Interface.
Definition: abg-fe-iface.cc:92
corpus_sptr corpus()
Getter for the ABI corpus being built by the current front-end.
corpus_group_sptr & corpus_group()
Getter for the ABI corpus group being built by the current front-end.
const std::string & corpus_path() const
Getter of the path to the file which an ABI corpus is to be created for.
const string & dt_soname() const
Getter for the SONAME of the analyzed binary.
The abstraction of an interned string.
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition: abg-ir.h:2537
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition: abg-ir.h:2540
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition: abg-ir.h:4246
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition: abg-corpus.h:45
scope_decl * get_scope() const
Return the type containing the current decl, if any.
Definition: abg-ir.cc:5025
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1194
binding
The binding of a symbol.
Definition: abg-ir.h:940
type
The type of a symbol.
Definition: abg-ir.h:927
static elf_symbol_sptr create(const environment &e, size_t i, size_t s, const string &n, type t, binding b, bool d, bool c, const version &ve, visibility vi, bool is_in_ksymtab=false, const abg_compat::optional< uint32_t > &crc={}, const abg_compat::optional< std::string > &ns={}, bool is_suppressed=false)
Factory of instances of elf_symbol.
Definition: abg-ir.cc:1993
visibility
The visibility of the symbol.
Definition: abg-ir.h:949
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2766
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition: abg-ir.h:140
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition: abg-ir.h:3135
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3138
The internal representation of an integral type.
Definition: abg-ir-priv.h:46
string to_string(bool internal=false) const
Return the string representation of the current instance of integral_type.
Definition: abg-ir.cc:16343
The source location of a token.
Definition: abg-ir.h:299
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition: abg-ir.h:2245
language
The language of the translation unit.
Definition: abg-ir.h:699
visiting_kind operator~(visiting_kind l)
The overloaded 'bit inversion' operator for visiting_kind.
unordered_map< std::pair< offset_type, offset_type >, offset_pair_set_type, offset_pair_hash > offset_pair_set_map_type
A convenience typedef for an unordered_map that associates a pair of offset_type to a set of pairs of...
unordered_map< Dwarf_Off, translation_unit_sptr > die_tu_map_type
Convenience typedef for a map which key is the offset of a DW_TAG_compile_unit and the value is the c...
bool lookup_symbol_from_elf(const environment &env, const string &elf_path, const string &symbol_name, bool demangle, vector< elf_symbol_sptr > &syms)
Look into the symbol tables of a given elf file and see if we find a given symbol.
void reset_reader(elf_based_reader &rdr, const std::string &elf_path, const vector< char ** > &debug_info_root_path, bool read_all_types, bool linux_kernel_mode)
Re-initialize a reader so that it can re-used to read another binary.
unordered_map< string, classes_type > string_classes_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
std::pair< offset_type, offset_type > offset_pair_type
A convenience typedef for a pair of offset_type.
shared_ptr< addr_elf_symbol_sptr_map_type > addr_elf_symbol_sptr_map_sptr
Convenience typedef for a shared pointer to an addr_elf_symbol_sptr_map_type.
bool is_anonymous_type_die(Dwarf_Die *die)
Test if a given DIE represents an anonymous type.
unordered_map< Dwarf_Off, class_or_union_sptr > die_class_or_union_map_type
Convenience typedef for a map which key is the offset of a dwarf die, (given by dwarf_dieoffset()) an...
unordered_map< Dwarf_Off, function_type_sptr > die_function_type_map_type
Convenience typedef for a map which key is the offset of a dwarf die and which value is the correspon...
unordered_map< Dwarf_Off, interned_string > die_istring_map_type
Convenience typedef for a map which key is the offset of a DIE and the value is the corresponding qua...
unordered_map< Dwarf_Off, function_decl_sptr > die_function_decl_map_type
Convenience typedef for a map which key the offset of a dwarf die and which value is the correspondin...
bool lookup_public_function_symbol_from_elf(environment &env, const string &path, const string &symname, vector< elf_symbol_sptr > &syms)
Look into the symbol tables of an elf file to see if a public function of a given name is found.
unordered_set< offset_type, offset_hash > offset_set_type
A convenience typedef for an unordered set of DIE offsets.
unordered_map< Dwarf_Off, type_or_decl_base_sptr > die_artefact_map_type
Convenience typedef for a map which key is the offset of a dwarf die and which value is the correspon...
unordered_map< std::pair< offset_type, offset_type >, offset_pair_vector_type, offset_pair_hash > offset_pair_vect_map_type
A convenience typedef for an unordered map that associates a pair of offset_type to a vector of pairs...
unordered_map< interned_string, function_type_sptr, hash_interned_string > istring_fn_type_map_type
Convenience typedef for a map that associates an interned_string to a function_type_sptr.
unordered_map< Dwarf_Off, class_decl_sptr > die_class_map_type
Convenience typedef for a map which key is the offset of a dwarf die, (given by dwarf_dieoffset()) an...
unordered_map< string, classes_or_unions_type > string_classes_or_unions_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
elf_symbol_sptr create_default_fn_sym(const string &sym_name, const environment &env)
Create a function symbol with a given name.
unordered_map< Dwarf_Off, imported_unit_points_type > tu_die_imported_unit_points_map_type
Convenience typedef for a vector of imported_unit_point.
vector< Dwarf_Off > dwarf_offsets_type
A convenience typedef for a vector of Dwarf_Off.
unordered_map< Dwarf_Off, Dwarf_Off > offset_offset_map_type
Convenience typedef for a map which key is a dwarf offset. The value is also a dwarf offset.
unordered_set< std::pair< offset_type, offset_type >, offset_pair_hash > offset_pair_set_type
A convenience typedef for an unordered set of pairs of offset_type.
unordered_map< string, enums_type > string_enums_map
Convenience typedef for a map which key is a string and which value is a vector of smart pointer to a...
stack< scope_decl * > scope_stack_type
Convenience typedef for a stack containing the scopes up to the current point in the abigail Internal...
vector< std::pair< offset_type, offset_type > > offset_pair_vector_type
A convenience typedef for a vector of pairs of offset_type.
elf_based_reader_sptr create_reader(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &environment, bool load_all_types, bool linux_kernel_mode)
Create a dwarf::reader.
die_source
Where a DIE comes from. For instance, a DIE can come from the main debug info section,...
unordered_map< interned_string, dwarf_offsets_type, hash_interned_string > istring_dwarf_offsets_map_type
Convenience typedef for a map which is an interned_string and which value is a vector of offsets.
vector< imported_unit_point > imported_unit_points_type
Convenience typedef for a vector of imported_unit_point.
corpus_sptr read_corpus_from_elf(const std::string &elf_path, const vector< char ** > &debug_info_root_paths, environment &environment, bool load_all_types, fe_iface::status &status)
Read all abigail::translation_unit possible from the debug info accessible from an elf file,...
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition: abg-fwd.h:232
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:12103
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition: abg-fwd.h:218
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:11967
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition: abg-fwd.h:266
access_specifier
Access specifier for class members.
Definition: abg-ir.h:879
const type_base_wptrs_type * lookup_enum_types(const interned_string &qualified_name, const corpus &corp)
Look into a given corpus to find the enum type*s* that have a given qualified name.
Definition: abg-ir.cc:13684
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:13846
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6845
vector< type_base_wptr > type_base_wptrs_type
A convenience typedef for a vector of type_base_wptr.
Definition: abg-fwd.h:143
const type_base * is_void_pointer_type(const type_base *t)
Test if a type is a pointer to void type.
Definition: abg-ir.cc:11445
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10541
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5633
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:11873
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:888
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6777
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:10791
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8525
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:10155
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:11369
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:11993
comparison_result
The result of structural comparison of type ABI artifacts.
Definition: abg-ir-priv.h:33
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition: abg-ir.cc:10823
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:241
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition: abg-ir.cc:10592
type_base_sptr lookup_class_typedef_or_enum_type(const string &qualified_name, const corpus &corp)
Look into a corpus to find a class, typedef or enum type which has a given qualified name.
Definition: abg-ir.cc:13871
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:6280
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:13547
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6648
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:12434
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:11054
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition: abg-fwd.h:190
type_base_sptr peel_typedef_pointer_or_reference_type(const type_base_sptr type)
Return the leaf underlying or pointed-to type node of a typedef_decl, pointer_type_def,...
Definition: abg-ir.cc:7477
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:6704
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:7032
void canonicalize_types(const input_iterator &begin, const input_iterator &end, deref_lambda deref)
Compute the canonical type for all the IR types of the system.
Definition: abg-ir-priv.h:1326
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:10643
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:11516
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5778
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10701
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:13714
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition: abg-fwd.h:207
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition: abg-fwd.h:164
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:12143
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:11272
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:11309
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1742
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:16261
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:10773
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8593
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:253
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:236
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition: abg-fwd.h:261
shared_ptr< type_or_decl_base > type_or_decl_base_sptr
A convenience typedef for a shared_ptr to type_or_decl_base.
Definition: abg-fwd.h:121
shared_ptr< translation_unit > translation_unit_sptr
Convenience typedef for a shared pointer on a translation_unit type.
Definition: abg-fwd.h:137
const decl_base_sptr lookup_var_decl_in_scope(const string &fqn, const scope_decl_sptr &skope)
Lookup a var_decl in a scope.
Definition: abg-ir.cc:12633
type_base * type_has_non_canonicalized_subtype(type_base_sptr t)
Test if a type has sub-types that are non-canonicalized.
Definition: abg-ir.cc:27643
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1756
union_decl_sptr lookup_union_type(const interned_string &type_name, const translation_unit &tu)
Lookup a union type from a translation unit.
Definition: abg-ir.cc:12180
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:15752
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition: abg-fwd.h:223
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6534
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1728
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10481
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:24982
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5698
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:8501
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:28314
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5749
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition: abg-fwd.h:172
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition: abg-ir.cc:6807
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:11137
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:11576
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:11103
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:13502
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:11852
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5847
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:13451
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:10174
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:26106
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:11781
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition: abg-fwd.h:158
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:7155
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition: abg-fwd.h:281
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1765
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:15086
interned_string get_type_name(const type_base_sptr &t, bool qualified, bool internal)
Get the name of a given type and return a copy of it.
Definition: abg-ir.cc:8885
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:7736
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:13809
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10429
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:11546
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:11496
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:12213
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:8782
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:11606
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5651
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:6591
bool is_type_suppressed(const fe_iface &fe, const string &type_name, const location &type_location, bool &type_is_private, bool require_drop_property)
Test if a type is matched by at least one suppression specification associated with a given front-end...
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_variable_suppressed(const fe_iface &fe, const string &var_name, const string &var_linkage_name, bool require_drop_property)
Test if a variable is matched by at least one suppression specification associated with a given front...
bool base_name(string const &path, string &file_name)
Return the file name part of a file part.
void initialize()
This function needs to be called before any libabigail function.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool string_is_ascii_identifier(const string &str)
Test if a string is made of ascii characters which are identifiers acceptable in C or C++ programs.
Toplevel namespace for libabigail.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition: abg-ir.cc:152
fe_iface::status operator&(fe_iface::status l, fe_iface::status r)
The bitwise AND operator for the fe_iface::status type.
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition: abg-ir.cc:186
fe_iface::status operator|(fe_iface::status l, fe_iface::status r)
The bitwise OR operator for the fe_iface::status type.
std::ostream & operator<<(std::ostream &o, const interned_string &s)
Streaming operator.
Definition: abg-ir.cc:169
A functor to hash instances of interned_string.