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
527subrange_die_indirect_bound_value(const Dwarf_Die *die,
528 unsigned attr_name,
529 array_type_def::subrange_type::bound_value& v,
530 bool& is_signed);
531
532static bool
533subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
534 unsigned attr_name,
535 Dwarf_Die& referenced_subrange);
536static string
537get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
538
539static string
540build_internal_anonymous_die_name(const string &base_name,
541 size_t anonymous_type_index);
542
543static string
544get_internal_anonymous_die_name(Dwarf_Die *die,
545 size_t anonymous_type_index);
546
547static string
548die_qualified_type_name(const reader& rdr,
549 const Dwarf_Die* die,
550 size_t where);
551
552static string
553die_qualified_decl_name(const reader& rdr,
554 const Dwarf_Die* die,
555 size_t where);
556
557static string
558die_qualified_name(const reader& rdr,
559 const Dwarf_Die* die,
560 size_t where);
561
562static bool
563die_qualified_type_name_empty(const reader& rdr,
564 const Dwarf_Die* die, size_t where,
565 string &qualified_name);
566
567static void
568die_return_and_parm_names_from_fn_type_die(const reader& rdr,
569 const Dwarf_Die* die,
570 size_t where_offset,
571 bool pretty_print,
572 string &return_type_name,
573 string &class_name,
574 vector<string>& parm_names,
575 bool& is_const,
576 bool& is_static);
577
578static string
579die_function_signature(const reader& rdr,
580 const Dwarf_Die *die,
581 size_t where_offset);
582
583static bool
584die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
585
586static bool
587die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
588
589static bool
590die_function_type_is_method_type(const reader& rdr,
591 const Dwarf_Die *die,
592 size_t where_offset,
593 Dwarf_Die& object_pointer_die,
594 Dwarf_Die& class_die,
595 bool& is_static);
596
597static string
598die_pretty_print_type(reader& rdr,
599 const Dwarf_Die* die,
600 size_t where_offset);
601
602static string
603die_pretty_print_decl(reader& rdr,
604 const Dwarf_Die* die,
605 size_t where_offset);
606
607static string
608die_pretty_print(reader& rdr,
609 const Dwarf_Die* die,
610 size_t where_offset);
611
612static void
613maybe_canonicalize_type(const type_base_sptr& t,
614 reader& rdr);
615
616static uint64_t
617get_default_array_lower_bound(translation_unit::language l);
618
619static bool
620find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
621 Dwarf_Off,
622 imported_unit_points_type::const_iterator&);
623
625build_subrange_type(reader& rdr,
626 const Dwarf_Die* die,
627 size_t where_offset,
628 bool associate_type_to_die = true);
629
630static void
631build_subranges_from_array_type_die(reader& rdr,
632 const Dwarf_Die* die,
634 size_t where_offset,
635 bool associate_type_to_die = true);
636
638compare_dies(const reader& rdr,
639 const Dwarf_Die *l, const Dwarf_Die *r,
640 bool update_canonical_dies_on_the_fly);
641
642static bool
643compare_dies_during_canonicalization(reader& rdr,
644 const Dwarf_Die *l, const Dwarf_Die *r,
645 bool update_canonical_dies_on_the_fly);
646
647static bool
648get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
649
650/// Compare a symbol name against another name, possibly demangling
651/// the symbol_name before performing the comparison.
652///
653/// @param symbol_name the symbol_name to take in account.
654///
655/// @param name the second name to take in account.
656///
657/// @param demangle if true, demangle @p symbol_name and compare the
658/// result of the demangling with @p name.
659///
660/// @return true iff symbol_name equals name.
661static bool
662compare_symbol_name(const string& symbol_name,
663 const string& name,
664 bool demangle)
665{
666 if (demangle)
667 {
668 string m = demangle_cplus_mangled_name(symbol_name);
669 return m == name;
670 }
671 return symbol_name == name;
672}
673
674/// Lookup a symbol using the SysV ELF hash table.
675///
676/// Note that this function hasn't been tested. So it hasn't been
677/// debugged yet. IOW, it is not known to work. Or rather, it's
678/// almost like it's surely doesn't work ;-)
679///
680/// Use it at your own risks. :-)
681///
682///@parm env the environment we are operating from.
683///
684/// @param elf_handle the elf_handle to use.
685///
686/// @param sym_name the symbol name to look for.
687///
688/// @param ht_index the index (in the section headers table) of the
689/// hash table section to use.
690///
691/// @param sym_tab_index the index (in the section headers table) of
692/// the symbol table to use.
693///
694/// @param demangle if true, demangle @p sym_name before comparing it
695/// to names from the symbol table.
696///
697/// @param syms_found a vector of symbols found with the name @p
698/// sym_name. table.
699static bool
700lookup_symbol_from_sysv_hash_tab(const environment& env,
701 Elf* elf_handle,
702 const string& sym_name,
703 size_t ht_index,
704 size_t sym_tab_index,
705 bool demangle,
706 vector<elf_symbol_sptr>& syms_found)
707{
708 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
709 ABG_ASSERT(sym_tab_section);
710
711 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
712 ABG_ASSERT(sym_tab_data);
713
714 GElf_Shdr sheader_mem;
715 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
716 &sheader_mem);
717 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
718 ABG_ASSERT(hash_section);
719
720 // Poke at the different parts of the hash table and get them ready
721 // to be used.
722 unsigned long hash = elf_hash(sym_name.c_str());
723 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
724 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
725 size_t nb_buckets = ht_data[0];
726 size_t nb_chains = ht_data[1];
727
728 if (nb_buckets == 0)
729 // An empty hash table. Not sure if that is possible, but it
730 // would mean an empty table of exported symbols.
731 return false;
732
733 //size_t nb_chains = ht_data[1];
734 Elf32_Word* ht_buckets = &ht_data[2];
735 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
736
737 // Now do the real work.
738 size_t bucket = hash % nb_buckets;
739 size_t symbol_index = ht_buckets[bucket];
740
741 GElf_Sym symbol;
742 const char* sym_name_str;
743 size_t sym_size;
744 elf_symbol::type sym_type;
745 elf_symbol::binding sym_binding;
746 elf_symbol::visibility sym_visibility;
747 bool found = false;
748
749 do
750 {
751 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
752 sym_name_str = elf_strptr(elf_handle,
753 sym_tab_section_header->sh_link,
754 symbol.st_name);
755 if (sym_name_str
756 && compare_symbol_name(sym_name_str, sym_name, demangle))
757 {
758 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
759 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
760 sym_visibility =
761 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
762 sym_size = symbol.st_size;
763 elf_symbol::version ver;
764 if (get_version_for_symbol(elf_handle, symbol_index,
765 /*get_def_version=*/true, ver))
766 ABG_ASSERT(!ver.str().empty());
767 elf_symbol_sptr symbol_found =
769 symbol_index,
770 sym_size,
771 sym_name_str,
772 sym_type,
773 sym_binding,
774 symbol.st_shndx != SHN_UNDEF,
775 symbol.st_shndx == SHN_COMMON,
776 ver, sym_visibility);
777 syms_found.push_back(symbol_found);
778 found = true;
779 }
780 symbol_index = ht_chains[symbol_index];
781 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
782
783 return found;
784}
785
786/// Get the size of the elf class, in bytes.
787///
788/// @param elf_handle the elf handle to use.
789///
790/// @return the size computed.
791static char
792get_elf_class_size_in_bytes(Elf* elf_handle)
793{
794 char result = 0;
795 GElf_Ehdr hdr;
796
797 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
798 int c = hdr.e_ident[EI_CLASS];
799
800 switch (c)
801 {
802 case ELFCLASS32:
803 result = 4;
804 break;
805 case ELFCLASS64:
806 result = 8;
807 break;
808 default:
810 }
811
812 return result;
813}
814
815/// Get a given word of a bloom filter, referred to by the index of
816/// the word.
817///
818/// The bloom word size depends on the current elf class (32 bits for
819/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
820/// abstracts that nicely.
821///
822/// @param elf_handle the elf handle to use.
823///
824/// @param bloom_filter the bloom filter to consider.
825///
826/// @param index the index of the bloom filter to return.
827///
828/// @return a 64 bits work containing the bloom word found at index @p
829/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
830/// most significant bytes of the result are going to be zero.
831static Elf64_Xword
832bloom_word_at(Elf* elf_handle,
833 Elf32_Word* bloom_filter,
834 size_t index)
835{
836 Elf64_Xword result = 0;
837 GElf_Ehdr h;
838 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
839 int c;
840 c = h.e_ident[EI_CLASS];
841
842 switch(c)
843 {
844 case ELFCLASS32:
845 result = bloom_filter[index];
846 break ;
847 case ELFCLASS64:
848 {
849 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
850 result = f[index];
851 }
852 break;
853 default:
854 abort();
855 }
856
857 return result;
858}
859
860/// The abstraction of the gnu elf hash table.
861///
862/// The members of this struct are explained at
863/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
864/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
865struct gnu_ht
866{
867 size_t nb_buckets;
868 Elf32_Word* buckets;
869 Elf32_Word* chain;
870 size_t first_sym_index;
871 size_t bf_nwords;
872 size_t bf_size;
873 Elf32_Word* bloom_filter;
874 size_t shift;
875 size_t sym_count;
876 Elf_Scn* sym_tab_section;
877 GElf_Shdr sym_tab_section_header;
878
879 gnu_ht()
880 : nb_buckets(0),
881 buckets(0),
882 chain(0),
883 first_sym_index(0),
884 bf_nwords(0),
885 bf_size(0),
886 bloom_filter(0),
887 shift(0),
888 sym_count(0),
889 sym_tab_section(0)
890 {}
891}; // end struct gnu_ht
892
893/// Setup the members of the gnu hash table.
894///
895/// @param elf_handle a handle on the elf file to use.
896///
897/// @param ht_index the index (into the elf section headers table) of
898/// the hash table section to use.
899///
900/// @param sym_tab_index the index (into the elf section headers
901/// table) of the symbol table the gnu hash table is about.
902///
903/// @param ht the resulting hash table.
904///
905/// @return true iff the hash table @ ht could be setup.
906static bool
907setup_gnu_ht(Elf* elf_handle,
908 size_t ht_index,
909 size_t sym_tab_index,
910 gnu_ht& ht)
911{
912 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
913 ABG_ASSERT(ht.sym_tab_section);
914 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
915 ht.sym_count =
916 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
917 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
918 ABG_ASSERT(hash_section);
919
920 // Poke at the different parts of the hash table and get them ready
921 // to be used.
922 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
923 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
924
925 ht.nb_buckets = ht_data[0];
926 if (ht.nb_buckets == 0)
927 // An empty hash table. Not sure if that is possible, but it
928 // would mean an empty table of exported symbols.
929 return false;
930 ht.first_sym_index = ht_data[1];
931 // The number of words used by the bloom filter. A size of a word
932 // is ELFCLASS.
933 ht.bf_nwords = ht_data[2];
934 // The shift used by the bloom filter code.
935 ht.shift = ht_data[3];
936 // The data of the bloom filter proper.
937 ht.bloom_filter = &ht_data[4];
938 // The size of the bloom filter in 4 bytes word. This is going to
939 // be used to index the 'bloom_filter' above, which is of type
940 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
941 // words.
942 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
943 // The buckets of the hash table.
944 ht.buckets = ht.bloom_filter + ht.bf_size;
945 // The chain of the hash table.
946 ht.chain = ht.buckets + ht.nb_buckets;
947
948 return true;
949}
950
951/// Look into the symbol tables of the underlying elf file and find
952/// the symbol we are being asked.
953///
954/// This function uses the GNU hash table for the symbol lookup.
955///
956/// The reference of for the implementation of this function can be
957/// found at:
958/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
959/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
960///
961/// @param elf_handle the elf handle to use.
962///
963/// @param sym_name the name of the symbol to look for.
964///
965/// @param ht_index the index of the hash table header to use.
966///
967/// @param sym_tab_index the index of the symbol table header to use
968/// with this hash table.
969///
970/// @param demangle if true, demangle @p sym_name.
971///
972/// @param syms_found the vector of symbols found with the name @p
973/// sym_name.
974///
975/// @return true if a symbol was actually found.
976static bool
977lookup_symbol_from_gnu_hash_tab(const environment& env,
978 Elf* elf_handle,
979 const string& sym_name,
980 size_t ht_index,
981 size_t sym_tab_index,
982 bool demangle,
983 vector<elf_symbol_sptr>& syms_found)
984{
985 gnu_ht ht;
986 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
987 return false;
988
989 // Now do the real work.
990
991 // Compute bloom hashes (GNU hash and second bloom specific hashes).
992 size_t h1 = elf_gnu_hash(sym_name.c_str());
993 size_t h2 = h1 >> ht.shift;
994 // The size of one of the words used in the bloom
995 // filter, in bits.
996 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
997 int n = (h1 / c) % ht.bf_nwords;
998 // The bitmask of the bloom filter has a size of either 32-bits on
999 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
1000 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
1001 // type used here. When dealing with 32bits binaries, the upper
1002 // bits of the bitmask will be zero anyway.
1003 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
1004
1005 // Test if the symbol is *NOT* present in this ELF file.
1006 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
1007 return false;
1008
1009 size_t i = ht.buckets[h1 % ht.nb_buckets];
1010 if (i == STN_UNDEF)
1011 return false;
1012
1013 Elf32_Word stop_word, *stop_wordp;
1014 elf_symbol::version ver;
1015 GElf_Sym symbol;
1016 const char* sym_name_str;
1017 bool found = false;
1018
1019 elf_symbol::type sym_type;
1020 elf_symbol::binding sym_binding;
1021 elf_symbol::visibility sym_visibility;
1022
1023 // Let's walk the hash table and record the versions of all the
1024 // symbols which name equal sym_name.
1025 for (i = ht.buckets[h1 % ht.nb_buckets],
1026 stop_wordp = &ht.chain[i - ht.first_sym_index];
1027 i != STN_UNDEF
1028 && (stop_wordp
1029 < ht.chain + (ht.sym_count - ht.first_sym_index));
1030 ++i, ++stop_wordp)
1031 {
1032 stop_word = *stop_wordp;
1033 if ((stop_word & ~ 1)!= (h1 & ~1))
1034 // A given bucket can reference several hashes. Here we
1035 // stumbled across a hash value different from the one we are
1036 // looking for. Let's keep walking.
1037 continue;
1038
1039 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1040 i, &symbol));
1041 sym_name_str = elf_strptr(elf_handle,
1042 ht.sym_tab_section_header.sh_link,
1043 symbol.st_name);
1044 if (sym_name_str
1045 && compare_symbol_name(sym_name_str, sym_name, demangle))
1046 {
1047 // So we found a symbol (in the symbol table) that equals
1048 // sym_name. Now lets try to get its version and record it.
1049 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1050 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1051 sym_visibility =
1052 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1053
1054 if (get_version_for_symbol(elf_handle, i,
1055 /*get_def_version=*/true,
1056 ver))
1057 ABG_ASSERT(!ver.str().empty());
1058
1059 elf_symbol_sptr symbol_found =
1060 elf_symbol::create(env, i,
1061 symbol.st_size,
1062 sym_name_str,
1063 sym_type, sym_binding,
1064 symbol.st_shndx != SHN_UNDEF,
1065 symbol.st_shndx == SHN_COMMON,
1066 ver, sym_visibility);
1067 syms_found.push_back(symbol_found);
1068 found = true;
1069 }
1070
1071 if (stop_word & 1)
1072 // The last bit of the stop_word is 1. That means we need to
1073 // stop here. We reached the end of the chain of values
1074 // referenced by the hask bucket.
1075 break;
1076 }
1077 return found;
1078}
1079
1080/// Look into the symbol tables of the underlying elf file and find
1081/// the symbol we are being asked.
1082///
1083/// This function uses the elf hash table (be it the GNU hash table or
1084/// the sysv hash table) for the symbol lookup.
1085///
1086/// @param env the environment we are operating from.
1087///
1088/// @param elf_handle the elf handle to use.
1089///
1090/// @param ht_kind the kind of hash table to use. This is returned by
1091/// the function function find_hash_table_section_index.
1092///
1093/// @param ht_index the index (in the section headers table) of the
1094/// hash table section to use.
1095///
1096/// @param sym_tab_index the index (in section headers table) of the
1097/// symbol table index to use with this hash table.
1098///
1099/// @param symbol_name the name of the symbol to look for.
1100///
1101/// @param demangle if true, demangle @p sym_name.
1102///
1103/// @param syms_found the symbols that were actually found with the
1104/// name @p symbol_name.
1105///
1106/// @return true iff the function found the symbol from the elf hash
1107/// table.
1108static bool
1109lookup_symbol_from_elf_hash_tab(const environment& env,
1110 Elf* elf_handle,
1111 hash_table_kind ht_kind,
1112 size_t ht_index,
1113 size_t symtab_index,
1114 const string& symbol_name,
1115 bool demangle,
1116 vector<elf_symbol_sptr>& syms_found)
1117{
1118 if (elf_handle == 0 || symbol_name.empty())
1119 return false;
1120
1121 if (ht_kind == NO_HASH_TABLE_KIND)
1122 return false;
1123
1124 if (ht_kind == SYSV_HASH_TABLE_KIND)
1125 return lookup_symbol_from_sysv_hash_tab(env,
1126 elf_handle, symbol_name,
1127 ht_index,
1128 symtab_index,
1129 demangle,
1130 syms_found);
1131 else if (ht_kind == GNU_HASH_TABLE_KIND)
1132 return lookup_symbol_from_gnu_hash_tab(env,
1133 elf_handle, symbol_name,
1134 ht_index,
1135 symtab_index,
1136 demangle,
1137 syms_found);
1138 return false;
1139}
1140
1141/// Lookup a symbol from the symbol table directly.
1142///
1143///
1144/// @param env the environment we are operating from.
1145///
1146/// @param elf_handle the elf handle to use.
1147///
1148/// @param sym_name the name of the symbol to look up.
1149///
1150/// @param sym_tab_index the index (in the section headers table) of
1151/// the symbol table section.
1152///
1153/// @param demangle if true, demangle the names found in the symbol
1154/// table before comparing them with @p sym_name.
1155///
1156/// @param sym_name_found the actual name of the symbol found.
1157///
1158/// @param sym_type the type of the symbol found.
1159///
1160/// @param sym_binding the binding of the symbol found.
1161///
1162/// @param sym_versions the versions of the symbol found.
1163///
1164/// @return true iff the symbol was found.
1165static bool
1166lookup_symbol_from_symtab(const environment& env,
1167 Elf* elf_handle,
1168 const string& sym_name,
1169 size_t sym_tab_index,
1170 bool demangle,
1171 vector<elf_symbol_sptr>& syms_found)
1172{
1173 // TODO: read all of the symbol table, store it in memory in a data
1174 // structure that associates each symbol with its versions and in
1175 // which lookups of a given symbol is fast.
1176 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1177 ABG_ASSERT(sym_tab_section);
1178
1179 GElf_Shdr header_mem;
1180 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1181 &header_mem);
1182
1183 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1184 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1185 GElf_Sym* sym;
1186 char* name_str = 0;
1187 elf_symbol::version ver;
1188 bool found = false;
1189
1190 for (size_t i = 0; i < symcount; ++i)
1191 {
1192 GElf_Sym sym_mem;
1193 sym = gelf_getsym(symtab, i, &sym_mem);
1194 name_str = elf_strptr(elf_handle,
1195 sym_tab_header->sh_link,
1196 sym->st_name);
1197
1198 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1199 {
1200 elf_symbol::type sym_type =
1201 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1202 elf_symbol::binding sym_binding =
1203 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1204 elf_symbol::visibility sym_visibility =
1205 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1206 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1207 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1208
1209 if (get_version_for_symbol(elf_handle, i,
1210 /*get_def_version=*/sym_is_defined,
1211 ver))
1212 ABG_ASSERT(!ver.str().empty());
1213 elf_symbol_sptr symbol_found =
1214 elf_symbol::create(env, i, sym->st_size,
1215 name_str, sym_type,
1216 sym_binding, sym_is_defined,
1217 sym_is_common, ver, sym_visibility);
1218 syms_found.push_back(symbol_found);
1219 found = true;
1220 }
1221 }
1222
1223 if (found)
1224 return true;
1225
1226 return false;
1227}
1228
1229/// Look into the symbol tables of the underlying elf file and see
1230/// if we find a given symbol.
1231///
1232/// @param env the environment we are operating from.
1233///
1234/// @param symbol_name the name of the symbol to look for.
1235///
1236/// @param demangle if true, try to demangle the symbol name found in
1237/// the symbol table before comparing it to @p symbol_name.
1238///
1239/// @param syms_found the list of symbols found, with the name @p
1240/// symbol_name.
1241///
1242/// @param sym_type this is set to the type of the symbol found. This
1243/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1244/// STT_FUNC, STT_IFUNC, etc ...
1245///
1246/// Note that this parameter is set iff the function returns true.
1247///
1248/// @param sym_binding this is set to the binding of the symbol found.
1249/// This is a standard elf.h value of the symbol binding kind, that
1250/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1251///
1252/// @param symbol_versions the versions of the symbol @p symbol_name,
1253/// if it was found.
1254///
1255/// @return true iff a symbol with the name @p symbol_name was found.
1256static bool
1257lookup_symbol_from_elf(const environment& env,
1258 Elf* elf_handle,
1259 const string& symbol_name,
1260 bool demangle,
1261 vector<elf_symbol_sptr>& syms_found)
1262{
1263 size_t hash_table_index = 0, symbol_table_index = 0;
1264 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1265
1266 if (!demangle)
1267 ht_kind = find_hash_table_section_index(elf_handle,
1268 hash_table_index,
1269 symbol_table_index);
1270
1271 if (ht_kind == NO_HASH_TABLE_KIND)
1272 {
1273 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1274 return false;
1275
1276 return lookup_symbol_from_symtab(env,
1277 elf_handle,
1278 symbol_name,
1279 symbol_table_index,
1280 demangle,
1281 syms_found);
1282 }
1283
1284 return lookup_symbol_from_elf_hash_tab(env,
1285 elf_handle,
1286 ht_kind,
1287 hash_table_index,
1288 symbol_table_index,
1289 symbol_name,
1290 demangle,
1291 syms_found);
1292}
1293
1294/// Look into the symbol tables of the underlying elf file and see if
1295/// we find a given public (global or weak) symbol of function type.
1296///
1297/// @param env the environment we are operating from.
1298///
1299/// @param elf_handle the elf handle to use for the query.
1300///
1301/// @param symbol_name the function symbol to look for.
1302///
1303/// @param func_syms the vector of public functions symbols found, if
1304/// any.
1305///
1306/// @return true iff the symbol was found.
1307static bool
1308lookup_public_function_symbol_from_elf(environment& env,
1309 Elf* elf_handle,
1310 const string& symbol_name,
1311 vector<elf_symbol_sptr>& func_syms)
1312{
1313 vector<elf_symbol_sptr> syms_found;
1314 bool found = false;
1315
1316 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1317 /*demangle=*/false, syms_found))
1318 {
1319 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1320 i != syms_found.end();
1321 ++i)
1322 {
1323 elf_symbol::type type = (*i)->get_type();
1324 elf_symbol::binding binding = (*i)->get_binding();
1325
1326 if ((type == elf_symbol::FUNC_TYPE
1327 || type == elf_symbol::GNU_IFUNC_TYPE
1328 || type == elf_symbol::COMMON_TYPE)
1329 && (binding == elf_symbol::GLOBAL_BINDING
1330 || binding == elf_symbol::WEAK_BINDING))
1331 {
1332 func_syms.push_back(*i);
1333 found = true;
1334 }
1335 }
1336 }
1337
1338 return found;
1339}
1340
1341// ---------------------------------------
1342// <location expression evaluation types>
1343// ---------------------------------------
1344
1345/// An abstraction of a value representing the result of the
1346/// evaluation of a dwarf expression. This is abstraction represents
1347/// a partial view on the possible values because we are only
1348/// interested in extracting the latest and longuest constant
1349/// sub-expression of a given dwarf expression.
1350class expr_result
1351{
1352 bool is_const_;
1353 int64_t const_value_;
1354
1355public:
1356 expr_result()
1357 : is_const_(true),
1358 const_value_(0)
1359 {}
1360
1361 expr_result(bool is_const)
1362 : is_const_(is_const),
1363 const_value_(0)
1364 {}
1365
1366 explicit expr_result(int64_t v)
1367 :is_const_(true),
1368 const_value_(v)
1369 {}
1370
1371 /// @return true if the value is a constant. Otherwise, return
1372 /// false, meaning the value represents a quantity for which we need
1373 /// inferior (a running program) state to determine the value.
1374 bool
1375 is_const() const
1376 {return is_const_;}
1377
1378
1379 /// @param f a flag saying if the value is set to a constant or not.
1380 void
1381 is_const(bool f)
1382 {is_const_ = f;}
1383
1384 /// Get the current constant value iff this represents a
1385 /// constant.
1386 ///
1387 /// @param value the out parameter. Is set to the constant value of
1388 /// the @ref expr_result. This is set iff the function return true.
1389 ///
1390 ///@return true if this has a constant value, false otherwise.
1391 bool
1392 const_value(int64_t& value)
1393 {
1394 if (is_const())
1395 {
1396 value = const_value_;
1397 return true;
1398 }
1399 return false;
1400 }
1401
1402 /// Getter of the constant value of the current @ref expr_result.
1403 ///
1404 /// Note that the current @ref expr_result must be constant,
1405 /// otherwise the current process is aborted.
1406 ///
1407 /// @return the constant value of the current @ref expr_result.
1408 int64_t
1409 const_value() const
1410 {
1411 ABG_ASSERT(is_const());
1412 return const_value_;
1413 }
1414
1415 operator int64_t() const
1416 {return const_value();}
1417
1418 expr_result&
1419 operator=(const int64_t v)
1420 {
1421 const_value_ = v;
1422 return *this;
1423 }
1424
1425 bool
1426 operator==(const expr_result& o) const
1427 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1428
1429 bool
1430 operator>=(const expr_result& o) const
1431 {return const_value_ >= o.const_value_;}
1432
1433 bool
1434 operator<=(const expr_result& o) const
1435 {return const_value_ <= o.const_value_;}
1436
1437 bool
1438 operator>(const expr_result& o) const
1439 {return const_value_ > o.const_value_;}
1440
1441 bool
1442 operator<(const expr_result& o) const
1443 {return const_value_ < o.const_value_;}
1444
1445 expr_result
1446 operator+(const expr_result& v) const
1447 {
1448 expr_result r(*this);
1449 r.const_value_ += v.const_value_;
1450 r.is_const_ = r.is_const_ && v.is_const_;
1451 return r;
1452 }
1453
1454 expr_result&
1455 operator+=(int64_t v)
1456 {
1457 const_value_ += v;
1458 return *this;
1459 }
1460
1461 expr_result
1462 operator-(const expr_result& v) const
1463 {
1464 expr_result r(*this);
1465 r.const_value_ -= v.const_value_;
1466 r.is_const_ = r.is_const_ && v.is_const_;
1467 return r;
1468 }
1469
1470 expr_result
1471 operator%(const expr_result& v) const
1472 {
1473 expr_result r(*this);
1474 r.const_value_ %= v.const_value_;
1475 r.is_const_ = r.is_const_ && v.is_const();
1476 return r;
1477 }
1478
1479 expr_result
1480 operator*(const expr_result& v) const
1481 {
1482 expr_result r(*this);
1483 r.const_value_ *= v.const_value_;
1484 r.is_const_ = r.is_const_ && v.is_const();
1485 return r;
1486 }
1487
1488 expr_result
1489 operator|(const expr_result& v) const
1490 {
1491 expr_result r(*this);
1492 r.const_value_ |= v.const_value_;
1493 r.is_const_ = r.is_const_ && v.is_const_;
1494 return r;
1495 }
1496
1497 expr_result
1498 operator^(const expr_result& v) const
1499 {
1500 expr_result r(*this);
1501 r.const_value_ ^= v.const_value_;
1502 r.is_const_ = r.is_const_ && v.is_const_;
1503 return r;
1504 }
1505
1506 expr_result
1507 operator>>(const expr_result& v) const
1508 {
1509 expr_result r(*this);
1510 r.const_value_ = r.const_value_ >> v.const_value_;
1511 r.is_const_ = r.is_const_ && v.is_const_;
1512 return r;
1513 }
1514
1515 expr_result
1516 operator<<(const expr_result& v) const
1517 {
1518 expr_result r(*this);
1519 r.const_value_ = r.const_value_ << v.const_value_;
1520 r.is_const_ = r.is_const_ && v.is_const_;
1521 return r;
1522 }
1523
1524 expr_result
1525 operator~() const
1526 {
1527 expr_result r(*this);
1528 r.const_value_ = ~r.const_value_;
1529 return r;
1530 }
1531
1532 expr_result
1533 neg() const
1534 {
1535 expr_result r(*this);
1536 r.const_value_ = -r.const_value_;
1537 return r;
1538 }
1539
1540 expr_result
1541 abs() const
1542 {
1543 expr_result r = *this;
1544 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1545 return r;
1546 }
1547
1548 expr_result
1549 operator&(const expr_result& o)
1550 {
1551 expr_result r(*this);
1552 r.const_value_ &= o.const_value_;
1553 r.is_const_ = r.is_const_ && o.is_const_;
1554 return r;
1555 }
1556
1557 expr_result
1558 operator/(const expr_result& o)
1559 {
1560 expr_result r(*this);
1561 r.is_const_ = r.is_const_ && o.is_const_;
1562 return r.const_value() / o.const_value();
1563 }
1564};// class end expr_result;
1565
1566/// A class that implements a stack of @ref expr_result, to be used in
1567/// the engine evaluating DWARF expressions.
1568class expr_result_stack_type
1569{
1570 vector<expr_result> elems_;
1571
1572public:
1573
1574 expr_result_stack_type()
1575 {elems_.reserve(4);}
1576
1577 expr_result&
1578 operator[](unsigned i)
1579 {
1580 unsigned s = elems_.size();
1581 ABG_ASSERT(s > i);
1582 return elems_[s - 1 -i];
1583 }
1584
1585 const expr_result&
1586 operator[](unsigned i) const
1587 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1588
1589 unsigned
1590 size() const
1591 {return elems_.size();}
1592
1593 vector<expr_result>::reverse_iterator
1594 begin()
1595 {return elems_.rbegin();}
1596
1597 const vector<expr_result>::reverse_iterator
1598 begin() const
1599 {return const_cast<expr_result_stack_type*>(this)->begin();}
1600
1601 vector<expr_result>::reverse_iterator
1602 end()
1603 {return elems_.rend();}
1604
1605 const vector<expr_result>::reverse_iterator
1606 end() const
1607 {return const_cast<expr_result_stack_type*>(this)->end();}
1608
1609 expr_result&
1610 front()
1611 {return elems_.back();}
1612
1613 const expr_result&
1614 front() const
1615 {return const_cast<expr_result_stack_type*>(this)->front();}
1616
1617 void
1618 push_front(expr_result e)
1619 {elems_.push_back(e);}
1620
1621 expr_result
1622 pop_front()
1623 {
1624 expr_result r = front();
1625 elems_.pop_back();
1626 return r;
1627 }
1628
1629 void
1630 erase(vector<expr_result>::reverse_iterator i)
1631 {elems_.erase(--i.base());}
1632
1633 void
1634 clear()
1635 {elems_.clear();}
1636}; // end class expr_result_stack_type
1637
1638/// Abstraction of the evaluation context of a dwarf expression.
1639struct dwarf_expr_eval_context
1640{
1641 expr_result accum;
1642 expr_result_stack_type stack;
1643 // Is set to true if the result of the expression that got evaluated
1644 // is a TLS address.
1645 bool set_tls_addr;
1646
1647 dwarf_expr_eval_context()
1648 : accum(/*is_const=*/false),
1649 set_tls_addr(false)
1650 {
1651 stack.push_front(expr_result(true));
1652 }
1653
1654 void
1655 reset()
1656 {
1657 stack.clear();
1658 stack.push_front(expr_result(true));
1659 accum = expr_result(false);
1660 set_tls_addr = false;
1661 }
1662
1663 /// Set a flag to to tell that the result of the expression that got
1664 /// evaluated is a TLS address.
1665 ///
1666 /// @param f true iff the result of the expression that got
1667 /// evaluated is a TLS address, false otherwise.
1668 void
1669 set_tls_address(bool f)
1670 {set_tls_addr = f;}
1671
1672 /// Getter for the flag that tells if the result of the expression
1673 /// that got evaluated is a TLS address.
1674 ///
1675 /// @return true iff the result of the expression that got evaluated
1676 /// is a TLS address.
1677 bool
1678 set_tls_address() const
1679 {return set_tls_addr;}
1680
1681 expr_result
1682 pop()
1683 {
1684 expr_result r = stack.front();
1685 stack.pop_front();
1686 return r;
1687 }
1688
1689 void
1690 push(const expr_result& v)
1691 {stack.push_front(v);}
1692};//end class dwarf_expr_eval_context
1693
1694// ---------------------------------------
1695// </location expression evaluation types>
1696// ---------------------------------------
1697
1698class reader;
1699
1700typedef shared_ptr<reader> reader_sptr;
1701
1702/// The DWARF reader used to build the ABI corpus from debug info in
1703/// DWARF format.
1704///
1705/// This type is to be instanciated
1706/// abigail::dwarf::reader::create().
1707class reader : public elf_based_reader
1708{
1709public:
1710
1711 /// A set of containers that contains one container per kind of @ref
1712 /// die_source. This allows to associate DIEs to things, depending
1713 /// on the source of the DIE.
1714 template <typename ContainerType>
1715 class die_source_dependant_container_set
1716 {
1717 ContainerType primary_debug_info_container_;
1718 ContainerType alt_debug_info_container_;
1719 ContainerType type_unit_container_;
1720
1721 public:
1722
1723 /// Getter for the container associated to DIEs coming from a
1724 /// given @ref die_source.
1725 ///
1726 /// @param source the die_source for which we want the container.
1727 ///
1728 /// @return the container that associates DIEs coming from @p
1729 /// source to something.
1730 ContainerType&
1731 get_container(die_source source)
1732 {
1733 ContainerType *result = 0;
1734 switch (source)
1735 {
1736 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
1737 result = &primary_debug_info_container_;
1738 break;
1739 case ALT_DEBUG_INFO_DIE_SOURCE:
1740 result = &alt_debug_info_container_;
1741 break;
1742 case TYPE_UNIT_DIE_SOURCE:
1743 result = &type_unit_container_;
1744 break;
1745 case NO_DEBUG_INFO_DIE_SOURCE:
1746 case NUMBER_OF_DIE_SOURCES:
1748 }
1749 return *result;
1750 }
1751
1752 /// Getter for the container associated to DIEs coming from a
1753 /// given @ref die_source.
1754 ///
1755 /// @param source the die_source for which we want the container.
1756 ///
1757 /// @return the container that associates DIEs coming from @p
1758 /// source to something.
1759 const ContainerType&
1760 get_container(die_source source) const
1761 {
1762 return const_cast<die_source_dependant_container_set*>(this)->
1763 get_container(source);
1764 }
1765
1766 /// Getter for the container associated to DIEs coming from the
1767 /// same source as a given DIE.
1768 ///
1769 /// @param rdr the DWARF reader to consider.
1770 ///
1771 /// @param die the DIE which should have the same source as the
1772 /// source of the container we want.
1773 ///
1774 /// @return the container that associates DIEs coming from the
1775 /// same source as @p die.
1776 ContainerType&
1777 get_container(const reader& rdr, const Dwarf_Die *die)
1778 {
1779 const die_source source = rdr.get_die_source(die);
1780 return get_container(source);
1781 }
1782
1783 /// Getter for the container associated to DIEs coming from the
1784 /// same source as a given DIE.
1785 ///
1786 /// @param rdr the DWARF reader to consider.
1787 ///
1788 /// @param die the DIE which should have the same source as the
1789 /// source of the container we want.
1790 ///
1791 /// @return the container that associates DIEs coming from the
1792 /// same source as @p die.
1793 const ContainerType&
1794 get_container(const reader& rdr, const Dwarf_Die *die) const
1795 {
1796 return const_cast<die_source_dependant_container_set*>(this)->
1797 get_container(rdr, die);
1798 }
1799
1800 /// Clear the container set.
1801 void
1802 clear()
1803 {
1804 primary_debug_info_container_.clear();
1805 alt_debug_info_container_.clear();
1806 type_unit_container_.clear();
1807 }
1808 }; // end die_dependant_container_set
1809
1810 unsigned short dwarf_version_;
1811 Dwarf_Die* cur_tu_die_;
1812 mutable dwarf_expr_eval_context dwarf_expr_eval_context_;
1813 // A set of maps (one per kind of die source) that associates a decl
1814 // string representation with the DIEs (offsets) representing that
1815 // decl.
1816 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1817 decl_die_repr_die_offsets_maps_;
1818 // A set of maps (one per kind of die source) that associates a type
1819 // string representation with the DIEs (offsets) representing that
1820 // type.
1821 mutable die_source_dependant_container_set<istring_dwarf_offsets_map_type>
1822 type_die_repr_die_offsets_maps_;
1823 mutable die_source_dependant_container_set<die_istring_map_type>
1824 die_qualified_name_maps_;
1825 mutable die_source_dependant_container_set<die_istring_map_type>
1826 die_pretty_repr_maps_;
1827 mutable die_source_dependant_container_set<die_istring_map_type>
1828 die_pretty_type_repr_maps_;
1829 // A set of maps (one per kind of die source) that associates the
1830 // offset of a decl die to its corresponding decl artifact.
1831 mutable die_source_dependant_container_set<die_artefact_map_type>
1832 decl_die_artefact_maps_;
1833 // A set of maps (one per kind of die source) that associates the
1834 // offset of a type die to its corresponding type artifact.
1835 mutable die_source_dependant_container_set<die_artefact_map_type>
1836 type_die_artefact_maps_;
1837 /// A set of vectors (one per kind of die source) that associates
1838 /// the offset of a type DIE to the offset of its canonical DIE.
1839 mutable die_source_dependant_container_set<offset_offset_map_type>
1840 canonical_type_die_offsets_;
1841 /// A set of vectors (one per kind of die source) that associates
1842 /// the offset of a decl DIE to the offset of its canonical DIE.
1843 mutable die_source_dependant_container_set<offset_offset_map_type>
1844 canonical_decl_die_offsets_;
1845 /// A map that associates a function type representations to
1846 /// function types, inside a translation unit.
1847 mutable istring_fn_type_map_type per_tu_repr_to_fn_type_maps_;
1848 /// A map that associates a pair of DIE offsets to the result of the
1849 /// comparison of that pair.
1850 mutable std::unordered_map<std::pair<offset_type,offset_type>,
1852 dwarf_offset_pair_hash> die_comparison_results_;
1853 // The set of types pair that have been canonical-type-propagated.
1854 mutable offset_pair_set_type propagated_types_;
1855 die_class_or_union_map_type die_wip_classes_map_;
1856 die_class_or_union_map_type alternate_die_wip_classes_map_;
1857 die_class_or_union_map_type type_unit_die_wip_classes_map_;
1858 die_function_type_map_type die_wip_function_types_map_;
1859 die_function_type_map_type alternate_die_wip_function_types_map_;
1860 die_function_type_map_type type_unit_die_wip_function_types_map_;
1861 die_function_decl_map_type die_function_with_no_symbol_map_;
1862 vector<type_base_sptr> types_to_canonicalize_;
1863 string_classes_or_unions_map decl_only_classes_map_;
1864 string_enums_map decl_only_enums_map_;
1865 die_tu_map_type die_tu_map_;
1866 translation_unit_sptr cur_tu_;
1867 scope_decl_sptr nil_scope_;
1868 scope_stack_type scope_stack_;
1869 offset_offset_map_type primary_die_parent_map_;
1870 // A map that associates each tu die to a vector of unit import
1871 // points, in the main debug info
1872 tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
1873 // A map that associates each tu die to a vector of unit import
1874 // points, in the alternate debug info
1875 tu_die_imported_unit_points_map_type alt_tu_die_imported_unit_points_map_;
1876 tu_die_imported_unit_points_map_type type_units_tu_die_imported_unit_points_map_;
1877 // A DIE -> parent map for DIEs coming from the alternate debug info
1878 // file.
1879 offset_offset_map_type alternate_die_parent_map_;
1880 offset_offset_map_type type_section_die_parent_map_;
1881 list<var_decl_sptr> var_decls_to_add_;
1882#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1883 bool debug_die_canonicalization_is_on_;
1884 bool use_canonical_die_comparison_;
1885#endif
1886 mutable size_t compare_count_;
1887 mutable size_t canonical_propagated_count_;
1888 mutable size_t cancelled_propagation_count_;
1889 mutable optional<bool> leverage_dwarf_factorization_;
1890
1891protected:
1892
1893 reader() = delete;
1894
1895 /// Constructor of reader.
1896 ///
1897 /// @param elf_path the path to the elf file the context is to be
1898 /// used for.
1899 ///
1900 /// @param debug_info_root_paths a vector of pointers to the path to
1901 /// the root directory under which the debug info is to be found for
1902 /// @p elf_path. Leave this empty if the debug info is not in a
1903 /// split file.
1904 ///
1905 /// @param environment the environment used by the current context.
1906 /// This environment contains resources needed by the DWARF reader and by
1907 /// the types and declarations that are to be created later. Note
1908 /// that ABI artifacts that are to be compared all need to be
1909 /// created within the same environment.
1910 ///
1911 /// Please also note that the life time of this environment object
1912 /// must be greater than the life time of the resulting @ref
1913 /// reader the context uses resources that are allocated in
1914 /// the environment.
1915 ///
1916 /// @param load_all_types if set to false only the types that are
1917 /// reachable from publicly exported declarations (of functions and
1918 /// variables) are read. If set to true then all types found in the
1919 /// debug information are loaded.
1920 ///
1921 /// @param linux_kernel_mode if set to true, then consider the special
1922 /// linux kernel symbol tables when determining if a symbol is
1923 /// exported or not.
1924 reader(const string& elf_path,
1925 const vector<char**>& debug_info_root_paths,
1926 environment& environment,
1927 bool load_all_types,
1928 bool linux_kernel_mode)
1929 : elf_based_reader(elf_path,
1930 debug_info_root_paths,
1931 environment)
1932 {
1933 initialize(load_all_types, linux_kernel_mode);
1934 }
1935
1936public:
1937
1938 /// Initializer of reader.
1939 ///
1940 /// Resets the reader so that it can be re-used to read another binary.
1941 ///
1942 /// @param load_all_types if set to false only the types that are
1943 /// reachable from publicly exported declarations (of functions and
1944 /// variables) are read. If set to true then all types found in the
1945 /// debug information are loaded.
1946 ///
1947 /// @param linux_kernel_mode if set to true, then consider the
1948 /// special linux kernel symbol tables when determining if a symbol
1949 /// is exported or not.
1950 void
1951 initialize(bool load_all_types, bool linux_kernel_mode)
1952 {
1953 dwarf_version_ = 0;
1954 cur_tu_die_ = 0;
1955 decl_die_repr_die_offsets_maps_.clear();
1956 type_die_repr_die_offsets_maps_.clear();
1957 die_qualified_name_maps_.clear();
1958 die_pretty_repr_maps_.clear();
1959 die_pretty_type_repr_maps_.clear();
1960 decl_die_artefact_maps_.clear();
1961 type_die_artefact_maps_.clear();
1962 canonical_type_die_offsets_.clear();
1963 canonical_decl_die_offsets_.clear();
1964 die_wip_classes_map_.clear();
1965 alternate_die_wip_classes_map_.clear();
1966 type_unit_die_wip_classes_map_.clear();
1967 die_wip_function_types_map_.clear();
1968 alternate_die_wip_function_types_map_.clear();
1969 type_unit_die_wip_function_types_map_.clear();
1970 die_function_with_no_symbol_map_.clear();
1971 types_to_canonicalize_.clear();
1972 decl_only_classes_map_.clear();
1973 die_tu_map_.clear();
1974 corpus().reset();
1975 corpus_group().reset();
1976 cur_tu_.reset();
1977 primary_die_parent_map_.clear();
1978 tu_die_imported_unit_points_map_.clear();
1979 alt_tu_die_imported_unit_points_map_.clear();
1980 type_units_tu_die_imported_unit_points_map_.clear();
1981 alternate_die_parent_map_.clear();
1982 type_section_die_parent_map_.clear();
1983 var_decls_to_add_.clear();
1984 clear_per_translation_unit_data();
1985 options().load_in_linux_kernel_mode = linux_kernel_mode;
1986 options().load_all_types = load_all_types;
1987#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
1988 debug_die_canonicalization_is_on_ =
1989 env().debug_die_canonicalization_is_on();
1990 use_canonical_die_comparison_ = true;
1991#endif
1992 compare_count_ = 0;
1993 canonical_propagated_count_ = 0;
1994 cancelled_propagation_count_ = 0;
1995 load_in_linux_kernel_mode(linux_kernel_mode);
1996 }
1997
1998 /// Initializer of reader.
1999 ///
2000 /// Resets the reader so that it can be re-used to read another binary.
2001 ///
2002 /// @param elf_path the path to the new ELF file.
2003 ///
2004 /// @param debug_info_root_paths the vector of debug-info path to
2005 /// look for split debug info.
2006 ///
2007 /// @param load_all_types if set to false only the types that are
2008 /// reachable from publicly exported declarations (of functions and
2009 /// variables) are read. If set to true then all types found in the
2010 /// debug information are loaded.
2011 ///
2012 /// @param linux_kernel_mode if set to true, then consider the
2013 /// special linux kernel symbol tables when determining if a symbol
2014 /// is exported or not.
2015 void
2016 initialize(const string& elf_path,
2017 const vector<char**>& debug_info_root_paths,
2018 bool load_all_types,
2019 bool linux_kernel_mode)
2020 {
2021 elf_based_reader::initialize(elf_path, debug_info_root_paths);
2022 initialize(load_all_types, linux_kernel_mode);
2023 }
2024
2025 /// Create an instance of DWARF Reader.
2026 ///
2027 /// @param elf_path the path to the ELF file to read from.
2028 ///
2029 /// @param debug_info_root_paths a vector of paths where to look up
2030 /// split debug info files.
2031 ///
2032 /// @param environment the environment to be used by the reader.
2033 ///
2034 /// @param load_all_types if set to false only the types that are
2035 /// reachable from publicly exported declarations (of functions and
2036 /// variables) are read. If set to true then all types found in the
2037 /// debug information are loaded.
2038 ///
2039 /// @param linux_kernel_mode if set to true, then consider the
2040 /// special linux kernel symbol tables when determining if a symbol
2041 /// is exported or not.
2042 static dwarf::reader_sptr
2043 create(const std::string& elf_path,
2044 const vector<char**>& debug_info_root_paths,
2045 environment& environment,
2046 bool load_all_types,
2047 bool linux_kernel_mode)
2048 {
2049 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2050 environment, load_all_types,
2051 linux_kernel_mode));
2052 return result;
2053 }
2054
2055 /// Destructor of the @ref reader type.
2056 ~reader()
2057 {
2058 }
2059
2060 /// Read and analyze the ELF and DWARF information associated with
2061 /// the underlying ELF file and build an ABI corpus out of it.
2062 ///
2063 /// @param status output parameter. This is set to the status of
2064 /// the analysis of the debug info.
2065 ///
2066 /// @return the resulting ABI corpus.
2067 corpus_sptr
2068 read_corpus(status& status)
2069 {
2070 status = STATUS_UNKNOWN;
2071
2072 // Load the generic ELF parts of the corpus.
2074
2075 if ((status & STATUS_NO_SYMBOLS_FOUND)
2076 || !(status & STATUS_OK))
2077 // Either we couldn't find ELF symbols or something went badly
2078 // wrong. There is nothing we can do with this ELF file. Bail
2079 // out.
2080 return corpus_sptr();
2081
2082 // If we couldn't find debug info from the elf path, then say it.
2083 if (dwarf_debug_info() == nullptr)
2085
2086 {
2087 string alt_di_path;
2088 if (refers_to_alt_debug_info(alt_di_path)
2091 }
2092
2093 if (// If debug info was found but not the required alternate debug
2094 // info ...
2095 ((status & STATUS_ALT_DEBUG_INFO_NOT_FOUND)
2096 && !(status & STATUS_DEBUG_INFO_NOT_FOUND)))
2097 // ... then we cannot handle the binary.
2098 return corpus_sptr();
2099
2100 // Read the variable and function descriptions from the debug info
2101 // we have, through the dwfl handle.
2102 corpus_sptr corp = read_debug_info_into_corpus();
2103
2104 status |= STATUS_OK;
2105
2106 return corp;
2107 }
2108
2109 /// Read an analyze the DWARF information.
2110 ///
2111 /// Construct an ABI corpus from it.
2112 ///
2113 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2114 ///
2115 /// @return the resulting ABI corpus.
2116 corpus_sptr
2117 read_debug_info_into_corpus()
2118 {
2119 clear_per_corpus_data();
2120
2121 // First set some mundane properties of the corpus gathered from
2122 // ELF.
2123 corpus::origin origin = corpus()->get_origin();
2124 origin |= corpus::DWARF_ORIGIN;
2125 corpus()->set_origin(origin);
2126
2127 if (origin & corpus::LINUX_KERNEL_BINARY_ORIGIN
2128 && !env().user_set_analyze_exported_interfaces_only())
2129 // So we are looking at the Linux Kernel and the user has not set
2130 // any particular option regarding the amount of types to analyse.
2131 // In that case, we need to only analyze types that are reachable
2132 // from exported interfaces otherwise we get such a massive amount
2133 // of type DIEs to look at that things are just too slow down the
2134 // road.
2135 env().analyze_exported_interfaces_only(true);
2136
2137 corpus()->set_soname(dt_soname());
2138 corpus()->set_needed(dt_needed());
2139 corpus()->set_architecture_name(elf_architecture());
2140 // Set symbols information to the corpus.
2141 corpus()->set_symtab(symtab());
2142
2143 // Get out now if no debug info is found or if the symbol table is
2144 // empty.
2145 if (!dwarf_debug_info()
2146 || !corpus()->get_symtab()->has_symbols())
2147 return corpus();
2148
2149 uint8_t address_size = 0;
2150 size_t header_size = 0;
2151
2152#ifdef WITH_DEBUG_SELF_COMPARISON
2153 if (env().self_comparison_debug_is_on())
2154 env().set_self_comparison_debug_input(corpus());
2155#endif
2156
2157 // Walk all the DIEs of the debug info to build a DIE -> parent map
2158 // useful for get_die_parent() to work.
2159 {
2160 tools_utils::timer t;
2161 if (do_log())
2162 {
2163 cerr << "building die -> parent maps ...";
2164 t.start();
2165 }
2166
2167 build_die_parent_maps();
2168
2169 if (do_log())
2170 {
2171 t.stop();
2172 cerr << " DONE@" << corpus()->get_path()
2173 << ":"
2174 << t
2175 << "\n";
2176 }
2177 }
2178
2179 env().canonicalization_is_done(false);
2180
2181 {
2182 tools_utils::timer t;
2183 if (do_log())
2184 {
2185 cerr << "building the libabigail internal representation ...";
2186 t.start();
2187 }
2188 // And now walk all the DIEs again to build the libabigail IR.
2189 Dwarf_Half dwarf_vers = 0;
2190 for (Dwarf_Off offset = 0, next_offset = 0;
2191 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2192 offset, &next_offset, &header_size,
2193 &dwarf_vers, NULL, &address_size, NULL,
2194 NULL, NULL) == 0);
2195 offset = next_offset)
2196 {
2197 Dwarf_Off die_offset = offset + header_size;
2198 Dwarf_Die unit;
2199 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2200 die_offset, &unit)
2201 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2202 continue;
2203
2204 dwarf_version(dwarf_vers);
2205
2206 address_size *= 8;
2207
2208 // Build a translation_unit IR node from cu; note that cu must
2209 // be a DW_TAG_compile_unit die.
2210 translation_unit_sptr ir_node =
2211 build_translation_unit_and_add_to_ir(*this, &unit, address_size);
2212 ABG_ASSERT(ir_node);
2213 }
2214 if (do_log())
2215 {
2216 t.stop();
2217 cerr << " DONE@" << corpus()->get_path()
2218 << ":"
2219 << t
2220 << "\n";
2221
2222 cerr << "Number of aggregate types compared: "
2223 << compare_count_ << "\n"
2224 << "Number of canonical types propagated: "
2225 << canonical_propagated_count_ << "\n"
2226 << "Number of cancelled propagated canonical types:"
2227 << cancelled_propagation_count_ << "\n";
2228 }
2229 }
2230
2231 {
2232 tools_utils::timer t;
2233 if (do_log())
2234 {
2235 cerr << "resolving declaration only classes ...";
2236 t.start();
2237 }
2238 resolve_declaration_only_classes();
2239 if (do_log())
2240 {
2241 t.stop();
2242 cerr << " DONE@" << corpus()->get_path()
2243 << ":"
2244 << t
2245 <<"\n";
2246 }
2247 }
2248
2249 {
2250 tools_utils::timer t;
2251 if (do_log())
2252 {
2253 cerr << "resolving declaration only enums ...";
2254 t.start();
2255 }
2256 resolve_declaration_only_enums();
2257 if (do_log())
2258 {
2259 t.stop();
2260 cerr << " DONE@" << corpus()->get_path()
2261 << ":"
2262 << t
2263 <<"\n";
2264 }
2265 }
2266
2267 {
2268 tools_utils::timer t;
2269 if (do_log())
2270 {
2271 cerr << "fixing up functions with linkage name but "
2272 << "no advertised underlying symbols ....";
2273 t.start();
2274 }
2275 fixup_functions_with_no_symbols();
2276 if (do_log())
2277 {
2278 t.stop();
2279 cerr << " DONE@" << corpus()->get_path()
2280 <<":"
2281 << t
2282 <<"\n";
2283 }
2284 }
2285
2286 /// Now, look at the types that needs to be canonicalized after the
2287 /// translation has been constructed (which is just now) and
2288 /// canonicalize them.
2289 ///
2290 /// These types need to be constructed at the end of the translation
2291 /// unit reading phase because some types are modified by some DIEs
2292 /// even after the principal DIE describing the type has been read;
2293 /// this happens for clones of virtual destructors (for instance) or
2294 /// even for some static data members. We need to do that for types
2295 /// are in the alternate debug info section and for types that in
2296 /// the main debug info section.
2297 {
2298 tools_utils::timer t;
2299 if (do_log())
2300 {
2301 cerr << "perform late type canonicalizing ...\n";
2302 t.start();
2303 }
2304
2305 perform_late_type_canonicalizing();
2306 if (do_log())
2307 {
2308 t.stop();
2309 cerr << "late type canonicalizing DONE@"
2310 << corpus()->get_path()
2311 << ":"
2312 << t
2313 << "\n";
2314 }
2315 }
2316
2317 env().canonicalization_is_done(true);
2318
2319 {
2320 tools_utils::timer t;
2321 if (do_log())
2322 {
2323 cerr << "sort functions and variables ...";
2324 t.start();
2325 }
2326 corpus()->sort_functions();
2327 corpus()->sort_variables();
2328 if (do_log())
2329 {
2330 t.stop();
2331 cerr << " DONE@" << corpus()->get_path()
2332 << ":"
2333 << t
2334 <<" \n";
2335 }
2336 }
2337
2338 return corpus();
2339 }
2340
2341 /// Clear the data that is relevant only for the current translation
2342 /// unit being read. The rest of the data is relevant for the
2343 /// entire ABI corpus.
2344 void
2345 clear_per_translation_unit_data()
2346 {
2347 while (!scope_stack().empty())
2348 scope_stack().pop();
2349 var_decls_to_re_add_to_tree().clear();
2350 per_tu_repr_to_fn_type_maps().clear();
2351 }
2352
2353 /// Clear the data that is relevant for the current corpus being
2354 /// read.
2355 void
2356 clear_per_corpus_data()
2357 {
2358 die_qualified_name_maps_.clear();
2359 die_pretty_repr_maps_.clear();
2360 die_pretty_type_repr_maps_.clear();
2361 clear_types_to_canonicalize();
2362 }
2363
2364 /// Getter for the current environment.
2365 ///
2366 /// @return the current environment.
2367 environment&
2368 env()
2369 {return options().env;}
2370
2371 /// Getter for the current environment.
2372 ///
2373 /// @return the current environment.
2374 const environment&
2375 env() const
2376 {return const_cast<reader*>(this)->env();}
2377
2378 /// Getter for the flag that tells us if we are dropping functions
2379 /// and variables that have undefined symbols.
2380 ///
2381 /// @return true iff we are dropping functions and variables that have
2382 /// undefined symbols.
2383 bool
2384 drop_undefined_syms() const
2385 {return options().drop_undefined_syms;}
2386
2387 /// Setter for the flag that tells us if we are dropping functions
2388 /// and variables that have undefined symbols.
2389 ///
2390 /// @param f the new value of the flag.
2391 void
2392 drop_undefined_syms(bool f)
2393 {options().drop_undefined_syms = f;}
2394
2395 /// Getter of the DWARF version.
2396 unsigned short
2397 dwarf_version() const
2398 {return dwarf_version_;}
2399
2400 void
2401 dwarf_version(unsigned short v)
2402 {dwarf_version_ = v;}
2403
2404 /// Return the ELF descriptor used for DWARF access.
2405 ///
2406 /// This can be the same as reader::elf_handle() above, if the
2407 /// DWARF info is in the same ELF file as the one of the binary we
2408 /// are analizing. It is different if e.g, the debug info is split
2409 /// from the ELF file we are analizing.
2410 ///
2411 /// @return a pointer to the ELF descriptor used to access debug
2412 /// info.
2413 Elf*
2414 dwarf_elf_handle() const
2415 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2416
2417 /// Test if the debug information is in a separate ELF file wrt the
2418 /// main ELF file of the program (application or shared library) we
2419 /// are analizing.
2420 ///
2421 /// @return true if the debug information is in a separate ELF file
2422 /// compared to the main ELF file of the program (application or
2423 /// shared library) that we are looking at.
2424 bool
2425 dwarf_is_splitted() const
2426 {return dwarf_elf_handle() != elf_handle();}
2427
2428 /// Return the correct debug info, depending on the DIE source we
2429 /// are looking at.
2430 ///
2431 /// @param source the DIE source to consider.
2432 ///
2433 /// @return the right debug info, depending on @p source.
2434 const Dwarf*
2435 dwarf_per_die_source(die_source source) const
2436 {
2437 const Dwarf *result = 0;
2438 switch(source)
2439 {
2440 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2441 case TYPE_UNIT_DIE_SOURCE:
2442 result = dwarf_debug_info();
2443 break;
2444 case ALT_DEBUG_INFO_DIE_SOURCE:
2445 result = alternate_dwarf_debug_info();
2446 break;
2447 case NO_DEBUG_INFO_DIE_SOURCE:
2448 case NUMBER_OF_DIE_SOURCES:
2450 }
2451 return result;
2452 }
2453
2454 /// Return the path to the ELF path we are reading.
2455 ///
2456 /// @return the elf path.
2457 const string&
2458 elf_path() const
2459 {return corpus_path();}
2460
2461 const Dwarf_Die*
2462 cur_tu_die() const
2463 {return cur_tu_die_;}
2464
2465 void
2466 cur_tu_die(Dwarf_Die* cur_tu_die)
2467 {cur_tu_die_ = cur_tu_die;}
2468
2469 dwarf_expr_eval_context&
2470 dwarf_expr_eval_ctxt() const
2471 {return dwarf_expr_eval_context_;}
2472
2473 /// Getter of the maps set that associates a representation of a
2474 /// decl DIE to a vector of offsets of DIEs having that representation.
2475 ///
2476 /// @return the maps set that associates a representation of a decl
2477 /// DIE to a vector of offsets of DIEs having that representation.
2478 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2479 decl_die_repr_die_offsets_maps() const
2480 {return decl_die_repr_die_offsets_maps_;}
2481
2482 /// Getter of the maps set that associates a representation of a
2483 /// decl DIE to a vector of offsets of DIEs having that representation.
2484 ///
2485 /// @return the maps set that associates a representation of a decl
2486 /// DIE to a vector of offsets of DIEs having that representation.
2487 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2488 decl_die_repr_die_offsets_maps()
2489 {return decl_die_repr_die_offsets_maps_;}
2490
2491 /// Getter of the maps set that associate a representation of a type
2492 /// DIE to a vector of offsets of DIEs having that representation.
2493 ///
2494 /// @return the maps set that associate a representation of a type
2495 /// DIE to a vector of offsets of DIEs having that representation.
2496 const die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2497 type_die_repr_die_offsets_maps() const
2498 {return type_die_repr_die_offsets_maps_;}
2499
2500 /// Getter of the maps set that associate a representation of a type
2501 /// DIE to a vector of offsets of DIEs having that representation.
2502 ///
2503 /// @return the maps set that associate a representation of a type
2504 /// DIE to a vector of offsets of DIEs having that representation.
2505 die_source_dependant_container_set<istring_dwarf_offsets_map_type>&
2506 type_die_repr_die_offsets_maps()
2507 {return type_die_repr_die_offsets_maps_;}
2508
2509
2510 /// Compute the offset of the canonical DIE of a given DIE.
2511 ///
2512 /// @param die the DIE to consider.
2513 ///
2514 /// @param canonical_die_offset out parameter. This is set to the
2515 /// resulting canonical DIE that was computed.
2516 ///
2517 /// @param die_as_type if yes, it means @p die has to be considered
2518 /// as a type.
2519 void
2520 compute_canonical_die_offset(const Dwarf_Die *die,
2521 Dwarf_Off &canonical_die_offset,
2522 bool die_as_type) const
2523 {
2524 offset_offset_map_type &canonical_dies =
2525 die_as_type
2526 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2527 get_container(*this, die)
2528 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2529 get_container(*this, die);
2530
2531 Dwarf_Die canonical_die;
2532 compute_canonical_die(die, canonical_dies, canonical_die, die_as_type);
2533
2534 canonical_die_offset = dwarf_dieoffset(&canonical_die);
2535 }
2536
2537 /// Compute (find) the canonical DIE of a given DIE.
2538 ///
2539 /// @param die the DIE to consider.
2540 ///
2541 /// @param canonical_dies the vector in which the canonical dies ar
2542 /// stored. The index of each element is the offset of the DIE we
2543 /// want the canonical DIE for. And the value of the element at
2544 /// that index is the canonical DIE offset we are looking for.
2545 ///
2546 /// @param canonical_die_offset out parameter. This is set to the
2547 /// resulting canonical DIE that was computed.
2548 ///
2549 /// @param die_as_type if yes, it means @p die has to be considered
2550 /// as a type.
2551 void
2552 compute_canonical_die(const Dwarf_Die *die,
2553 offset_offset_map_type& canonical_dies,
2554 Dwarf_Die &canonical_die,
2555 bool die_as_type) const
2556 {
2557 const die_source source = get_die_source(die);
2558
2559 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2560
2561 compute_canonical_die(die_offset, source,
2562 canonical_dies,
2563 canonical_die, die_as_type);
2564 }
2565
2566 /// Compute (find) the canonical DIE of a given DIE.
2567 ///
2568 /// @param die_offset the offset of the DIE to consider.
2569 ///
2570 /// @param source the source of the DIE to consider.
2571 ///
2572 /// @param canonical_dies the vector in which the canonical dies ar
2573 /// stored. The index of each element is the offset of the DIE we
2574 /// want the canonical DIE for. And the value of the element at
2575 /// that index is the canonical DIE offset we are looking for.
2576 ///
2577 /// @param canonical_die_offset out parameter. This is set to the
2578 /// resulting canonical DIE that was computed.
2579 ///
2580 /// @param die_as_type if yes, it means @p die has to be considered
2581 /// as a type.
2582 void
2583 compute_canonical_die(Dwarf_Off die_offset,
2584 die_source source,
2585 offset_offset_map_type& canonical_dies,
2586 Dwarf_Die &canonical_die,
2587 bool die_as_type) const
2588 {
2589 // The map that associates the string representation of 'die'
2590 // with a vector of offsets of potentially equivalent DIEs.
2592 die_as_type
2593 ? (const_cast<reader*>(this)->
2594 type_die_repr_die_offsets_maps().get_container(source))
2595 : (const_cast<reader*>(this)->
2596 decl_die_repr_die_offsets_maps().get_container(source));
2597
2598 Dwarf_Die die;
2599 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2600 die_offset, &die));
2601
2602 // The variable repr is the the string representation of 'die'.
2603 //
2604 // Even if die_as_type is true -- which means that 'die' is said
2605 // to be considered as a type -- we always consider a
2606 // DW_TAG_subprogram DIE as a decl here, as far as its string
2607 // representation is concerned.
2608 interned_string name =
2609 (die_as_type)
2610 ? get_die_pretty_type_representation(&die, /*where=*/0)
2611 : get_die_pretty_representation(&die, /*where=*/0);
2612
2613 Dwarf_Off canonical_die_offset = 0;
2614 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2615 if (i == map.end())
2616 {
2617 dwarf_offsets_type offsets;
2618 offsets.push_back(die_offset);
2619 map[name] = offsets;
2620 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2621 get_die_from_offset(source, die_offset, &canonical_die);
2622 return;
2623 }
2624
2625 Dwarf_Off cur_die_offset;
2626 Dwarf_Die potential_canonical_die;
2627 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2628 o != i->second.end();
2629 ++o)
2630 {
2631 cur_die_offset = *o;
2632 get_die_from_offset(source, cur_die_offset, &potential_canonical_die);
2633 if (compare_dies(*this, &die, &potential_canonical_die,
2634 /*update_canonical_dies_on_the_fly=*/false))
2635 {
2636 canonical_die_offset = cur_die_offset;
2637 set_canonical_die_offset(canonical_dies, die_offset,
2638 canonical_die_offset);
2639 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2640 return;
2641 }
2642 }
2643
2644 canonical_die_offset = die_offset;
2645 i->second.push_back(die_offset);
2646 set_canonical_die_offset(canonical_dies, die_offset, die_offset);
2647 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2648 }
2649
2650 /// Getter of the canonical DIE of a given DIE.
2651 ///
2652 /// @param die the DIE to consider.
2653 ///
2654 /// @param canonical_die output parameter. Is set to the resulting
2655 /// canonical die, if this function returns true.
2656 ///
2657 /// @param where the offset of the logical DIE we are supposed to be
2658 /// calling this function from. If set to zero this means this is
2659 /// to be ignored.
2660 ///
2661 /// @param die_as_type if set to yes, it means @p die is to be
2662 /// considered as a type DIE.
2663 ///
2664 /// @return true iff a canonical DIE was found for @p die.
2665 bool
2666 get_canonical_die(const Dwarf_Die *die,
2667 Dwarf_Die &canonical_die,
2668 size_t where,
2669 bool die_as_type)
2670 {
2671 const die_source source = get_die_source(die);
2672
2673 offset_offset_map_type &canonical_dies =
2674 die_as_type
2675 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2676 get_container(source)
2677 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2678 get_container(source);
2679
2680 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2681 if (Dwarf_Off canonical_die_offset =
2682 get_canonical_die_offset(canonical_dies, die_offset))
2683 {
2684 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2685 return true;
2686 }
2687
2688 // The map that associates the string representation of 'die'
2689 // with a vector of offsets of potentially equivalent DIEs.
2691 die_as_type
2692 ? (const_cast<reader*>(this)->
2693 type_die_repr_die_offsets_maps().get_container(*this, die))
2694 : (const_cast<reader*>(this)->
2695 decl_die_repr_die_offsets_maps().get_container(*this, die));
2696
2697 // The variable repr is the the string representation of 'die'.
2698 //
2699 // Even if die_as_type is true -- which means that 'die' is said
2700 // to be considered as a type -- we always consider a
2701 // DW_TAG_subprogram DIE as a decl here, as far as its string
2702 // representation is concerned.
2703 interned_string name =
2704 (die_as_type /*&& dwarf_tag(die) != DW_TAG_subprogram*/)
2705 ? get_die_pretty_type_representation(die, where)
2706 : get_die_pretty_representation(die, where);
2707
2708 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2709 if (i == map.end())
2710 return false;
2711
2712 Dwarf_Off cur_die_offset;
2713 for (dwarf_offsets_type::const_iterator o = i->second.begin();
2714 o != i->second.end();
2715 ++o)
2716 {
2717 cur_die_offset = *o;
2718 get_die_from_offset(source, cur_die_offset, &canonical_die);
2719 // compare die and canonical_die.
2720 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2721 die, &canonical_die,
2722 /*update_canonical_dies_on_the_fly=*/true))
2723 {
2724 set_canonical_die_offset(canonical_dies,
2725 die_offset,
2726 cur_die_offset);
2727 return true;
2728 }
2729 }
2730
2731 return false;
2732 }
2733
2734 /// Retrieve the canonical DIE of a given DIE.
2735 ///
2736 /// The canonical DIE is a DIE that is structurally equivalent to
2737 /// this one.
2738 ///
2739 /// Note that this function caches the canonical DIE that was
2740 /// computed. Subsequent invocations of this function on the same
2741 /// DIE return the same cached DIE.
2742 ///
2743 /// @param die the DIE to get a canonical type for.
2744 ///
2745 /// @param canonical_die the resulting canonical DIE.
2746 ///
2747 /// @param where the offset of the logical DIE we are supposed to be
2748 /// calling this function from. If set to zero this means this is
2749 /// to be ignored.
2750 ///
2751 /// @param die_as_type if true, consider DIE is a type.
2752 ///
2753 /// @return true if an *existing* canonical DIE was found.
2754 /// Otherwise, @p die is considered as being a canonical DIE for
2755 /// itself. @p canonical_die is thus set to the canonical die in
2756 /// either cases.
2757 bool
2758 get_or_compute_canonical_die(const Dwarf_Die* die,
2759 Dwarf_Die& canonical_die,
2760 size_t where,
2761 bool die_as_type) const
2762 {
2763 const die_source source = get_die_source(die);
2764
2765 offset_offset_map_type &canonical_dies =
2766 die_as_type
2767 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
2768 get_container(source)
2769 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
2770 get_container(source);
2771
2772 Dwarf_Off initial_die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
2773
2774 if (Dwarf_Off canonical_die_offset =
2775 get_canonical_die_offset(canonical_dies,
2776 initial_die_offset))
2777 {
2778 get_die_from_offset(source, canonical_die_offset, &canonical_die);
2779 return true;
2780 }
2781
2782 if (!is_type_die_to_be_canonicalized(die))
2783 return false;
2784
2785 // The map that associates the string representation of 'die'
2786 // with a vector of offsets of potentially equivalent DIEs.
2788 die_as_type
2789 ? (const_cast<reader*>(this)->
2790 type_die_repr_die_offsets_maps().get_container(*this, die))
2791 : (const_cast<reader*>(this)->
2792 decl_die_repr_die_offsets_maps().get_container(*this, die));
2793
2794 // The variable repr is the the string representation of 'die'.
2795 //
2796 // Even if die_as_type is true -- which means that 'die' is said
2797 // to be considered as a type -- we always consider a
2798 // DW_TAG_subprogram DIE as a decl here, as far as its string
2799 // representation is concerned.
2800 interned_string name =
2801 (die_as_type)
2802 ? get_die_pretty_type_representation(die, where)
2803 : get_die_pretty_representation(die, where);
2804
2805 istring_dwarf_offsets_map_type::iterator i = map.find(name);
2806 if (i == map.end())
2807 {
2808 dwarf_offsets_type offsets;
2809 offsets.push_back(initial_die_offset);
2810 map[name] = offsets;
2811 get_die_from_offset(source, initial_die_offset, &canonical_die);
2812 set_canonical_die_offset(canonical_dies,
2813 initial_die_offset,
2814 initial_die_offset);
2815 return false;
2816 }
2817
2818 // walk i->second without any iterator (using a while loop rather
2819 // than a for loop) because compare_dies might add new content to
2820 // the end of the i->second vector during the walking.
2821 dwarf_offsets_type::size_type n = 0, s = i->second.size();
2822 while (n < s)
2823 {
2824 Dwarf_Off die_offset = i->second[n];
2825 get_die_from_offset(source, die_offset, &canonical_die);
2826 // compare die and canonical_die.
2827 if (compare_dies_during_canonicalization(const_cast<reader&>(*this),
2828 die, &canonical_die,
2829 /*update_canonical_dies_on_the_fly=*/true))
2830 {
2831 set_canonical_die_offset(canonical_dies,
2832 initial_die_offset,
2833 die_offset);
2834 return true;
2835 }
2836 ++n;
2837 }
2838
2839 // We didn't find a canonical DIE for 'die'. So let's consider
2840 // that it is its own canonical DIE.
2841 get_die_from_offset(source, initial_die_offset, &canonical_die);
2842 i->second.push_back(initial_die_offset);
2843 set_canonical_die_offset(canonical_dies,
2844 initial_die_offset,
2845 initial_die_offset);
2846
2847 return false;
2848 }
2849
2850 /// Get the source of the DIE.
2851 ///
2852 /// The function returns an enumerator value saying if the DIE comes
2853 /// from the .debug_info section of the primary debug info file, the
2854 /// .debug_info section of the alternate debug info file, or the
2855 /// .debug_types section.
2856 ///
2857 /// @param die the DIE to get the source of.
2858 ///
2859 /// @return the source of the DIE if it could be determined,
2860 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2862 get_die_source(const Dwarf_Die *die) const
2863 {
2864 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2865 ABG_ASSERT(die);
2866 ABG_ASSERT(get_die_source(*die, source));
2867 return source;
2868 }
2869
2870 /// Get the source of the DIE.
2871 ///
2872 /// The function returns an enumerator value saying if the DIE comes
2873 /// from the .debug_info section of the primary debug info file, the
2874 /// .debug_info section of the alternate debug info file, or the
2875 /// .debug_types section.
2876 ///
2877 /// @param die the DIE to get the source of.
2878 ///
2879 /// @param source out parameter. The function sets this parameter
2880 /// to the source of the DIE @p iff it returns true.
2881 ///
2882 /// @return true iff the source of the DIE could be determined and
2883 /// returned.
2884 bool
2885 get_die_source(const Dwarf_Die &die, die_source &source) const
2886 {
2887 Dwarf_Die cu_die;
2888 Dwarf_Die cu_kind;
2889 uint8_t address_size = 0, offset_size = 0;
2890 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2891 &cu_die, &address_size,
2892 &offset_size))
2893 return false;
2894
2895 Dwarf_Half version = 0;
2896 Dwarf_Off abbrev_offset = 0;
2897 uint64_t type_signature = 0;
2898 Dwarf_Off type_offset = 0;
2899 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2900 &version, &abbrev_offset,
2901 &address_size, &offset_size,
2902 &type_signature, &type_offset))
2903 return false;
2904
2905 int tag = dwarf_tag(&cu_kind);
2906
2907 if (tag == DW_TAG_compile_unit
2908 || tag == DW_TAG_partial_unit)
2909 {
2910 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
2911 if (dwarf_debug_info() == die_dwarf)
2912 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
2913 else if (alternate_dwarf_debug_info() == die_dwarf)
2914 source = ALT_DEBUG_INFO_DIE_SOURCE;
2915 else
2917 }
2918 else if (tag == DW_TAG_type_unit)
2919 source = TYPE_UNIT_DIE_SOURCE;
2920 else
2921 return false;
2922
2923 return true;
2924 }
2925
2926 /// Getter for the DIE designated by an offset.
2927 ///
2928 /// @param source the source of the DIE to get.
2929 ///
2930 /// @param offset the offset of the DIE to get.
2931 ///
2932 /// @param die the resulting DIE. The pointer has to point to an
2933 /// allocated memory region.
2934 void
2935 get_die_from_offset(die_source source, Dwarf_Off offset, Dwarf_Die *die) const
2936 {
2937 if (source == TYPE_UNIT_DIE_SOURCE)
2938 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2939 offset, die));
2940 else
2941 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
2942 offset, die));
2943 }
2944
2945public:
2946
2947 /// Add an entry to the relevant die->decl map.
2948 ///
2949 /// @param die the DIE to add the the map.
2950 ///
2951 /// @param decl the decl to consider.
2952 ///
2953 /// @param where_offset where in the DIE stream we logically are.
2954 ///
2955 /// @param do_associate_by_repr if true then this function
2956 /// associates the representation string of @p die with the
2957 /// declaration @p decl, in a corpus-wide manner. That is, in the
2958 /// entire current corpus, there is going to be just one declaration
2959 /// associated with a DIE of the string representation of @p die.
2960 ///
2961 /// @param do_associate_by_repr_per_tu if true, then this function
2962 /// associates the representation string of @p die with the
2963 /// declaration @p decl in a translation unit wide manner. That is,
2964 /// in the entire current translation unit, there is going to be
2965 /// just one declaration associated with a DIE of the string
2966 /// representation of @p die.
2967 void
2968 associate_die_to_decl(Dwarf_Die* die,
2969 decl_base_sptr decl,
2970 size_t where_offset,
2971 bool do_associate_by_repr = false)
2972 {
2973 const die_source source = get_die_source(die);
2974
2976 decl_die_artefact_maps().get_container(source);
2977
2978 size_t die_offset;
2979 if (do_associate_by_repr)
2980 {
2981 Dwarf_Die equiv_die;
2982 if (!get_or_compute_canonical_die(die, equiv_die, where_offset,
2983 /*die_as_type=*/false))
2984 return;
2985 die_offset = dwarf_dieoffset(&equiv_die);
2986 }
2987 else
2988 die_offset = dwarf_dieoffset(die);
2989
2990 m[die_offset] = decl;
2991 }
2992
2993 /// Lookup the decl for a given DIE.
2994 ///
2995 /// The returned decl is either the decl of the DIE that as the
2996 /// exact offset @p die_offset
2997 /// die_offset, or
2998 /// give
2999 ///
3000 /// @param die_offset the offset of the DIE to consider.
3001 ///
3002 /// @param source where the DIE represented by @p die_offset comes
3003 /// from.
3004 ///
3005 /// Note that "alternate debug info sections" is a GNU extension as
3006 /// of DWARF4 and is described at
3007 /// http://www.dwarfstd.org/ShowIssue.php?issue=120604.1
3008 ///
3009 /// @return the resulting decl, or null if no decl is associated to
3010 /// the DIE represented by @p die_offset.
3011 decl_base_sptr
3012 lookup_decl_from_die_offset(Dwarf_Off die_offset, die_source source)
3013 {
3014 decl_base_sptr result =
3015 is_decl(lookup_artifact_from_die_offset(die_offset, source,
3016 /*die_as_type=*/false));
3017
3018 return result;
3019 }
3020
3021 /// Get the qualified name of a given DIE.
3022 ///
3023 /// If the name of the DIE was already computed before just return
3024 /// that name from a cache. Otherwise, build the name, cache it and
3025 /// return it.
3026 ///
3027 /// @param die the DIE to consider.
3028 ///
3029 /// @param where_offset where in the DIE stream we logically are.
3030 ///
3031 /// @return the interned string representing the qualified name of
3032 /// @p die.
3033 interned_string
3034 get_die_qualified_name(Dwarf_Die *die, size_t where_offset)
3035 {
3036 ABG_ASSERT(die);
3038 die_qualified_name_maps_.get_container(*this, die);
3039
3040 size_t die_offset = dwarf_dieoffset(die);
3041 die_istring_map_type::const_iterator i = map.find(die_offset);
3042
3043 if (i == map.end())
3044 {
3045 reader& rdr = *const_cast<reader*>(this);
3046 string qualified_name = die_qualified_name(rdr, die, where_offset);
3047 interned_string istr = env().intern(qualified_name);
3048 map[die_offset] = istr;
3049 return istr;
3050 }
3051
3052 return i->second;
3053 }
3054
3055 /// Get the qualified name of a given DIE.
3056 ///
3057 /// If the name of the DIE was already computed before just return
3058 /// that name from a cache. Otherwise, build the name, cache it and
3059 /// return it.
3060 ///
3061 /// @param die the DIE to consider.
3062 ///
3063 /// @param where_offset where in the DIE stream we logically are.
3064 ///
3065 /// @return the interned string representing the qualified name of
3066 /// @p die.
3067 interned_string
3068 get_die_qualified_name(Dwarf_Die *die, size_t where_offset) const
3069 {
3070 return const_cast<reader*>(this)->
3071 get_die_qualified_name(die, where_offset);
3072 }
3073
3074 /// Get the qualified name of a given DIE which is considered to be
3075 /// the DIE for a type.
3076 ///
3077 /// For instance, for a DW_TAG_subprogram DIE, this function
3078 /// computes the name of the function *type* that corresponds to the
3079 /// function.
3080 ///
3081 /// If the name of the DIE was already computed before just return
3082 /// that name from a cache. Otherwise, build the name, cache it and
3083 /// return it.
3084 ///
3085 /// @param die the DIE to consider.
3086 ///
3087 /// @param where_offset where in the DIE stream we logically are.
3088 ///
3089 /// @return the interned string representing the qualified name of
3090 /// @p die.
3091 interned_string
3092 get_die_qualified_type_name(const Dwarf_Die *die, size_t where_offset) const
3093 {
3094 ABG_ASSERT(die);
3095
3096 // The name of the translation unit die is "".
3097 if (die == cur_tu_die())
3098 return env().intern("");
3099
3101 die_qualified_name_maps_.get_container(*const_cast<reader*>(this),
3102 die);
3103
3104 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3105 die_istring_map_type::const_iterator i =
3106 map.find(die_offset);
3107
3108 if (i == map.end())
3109 {
3110 reader& rdr = *const_cast<reader*>(this);
3111 string qualified_name;
3112 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3113 if ((tag == DW_TAG_structure_type
3114 || tag == DW_TAG_class_type
3115 || tag == DW_TAG_union_type)
3116 && die_is_anonymous(die))
3117 {
3118 location l = die_location(*this, die);
3119 qualified_name = l ? l.expand() : "noloc";
3120 qualified_name = "unnamed-at-" + qualified_name;
3121 }
3122 else
3123 qualified_name =
3124 die_qualified_type_name(rdr, die, where_offset);
3125
3126 interned_string istr = env().intern(qualified_name);
3127 map[die_offset] = istr;
3128 return istr;
3129 }
3130
3131 return i->second;
3132 }
3133
3134 /// Get the pretty representation of a DIE that represents a type.
3135 ///
3136 /// For instance, for the DW_TAG_subprogram, this function computes
3137 /// the pretty representation of the type of the function, not the
3138 /// pretty representation of the function declaration.
3139 ///
3140 /// Once the pretty representation is computed, it's stored in a
3141 /// cache. Subsequent invocations of this function on the same DIE
3142 /// will yield the cached name.
3143 ///
3144 /// @param die the DIE to consider.
3145 ///
3146 /// @param where_offset where in the DIE stream we logically are.
3147 ///
3148 /// @return the interned_string that represents the pretty
3149 /// representation.
3150 interned_string
3151 get_die_pretty_type_representation(const Dwarf_Die *die,
3152 size_t where_offset) const
3153 {
3154 ABG_ASSERT(die);
3156 die_pretty_type_repr_maps_.get_container(*const_cast<reader*>(this),
3157 die);
3158
3159 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3160 die_istring_map_type::const_iterator i = map.find(die_offset);
3161
3162 if (i == map.end())
3163 {
3164 reader& rdr = *const_cast<reader*>(this);
3165 string pretty_representation =
3166 die_pretty_print_type(rdr, die, where_offset);
3167 interned_string istr = env().intern(pretty_representation);
3168 map[die_offset] = istr;
3169 return istr;
3170 }
3171
3172 return i->second;
3173 }
3174
3175 /// Get the pretty representation of a DIE.
3176 ///
3177 /// Once the pretty representation is computed, it's stored in a
3178 /// cache. Subsequent invocations of this function on the same DIE
3179 /// will yield the cached name.
3180 ///
3181 /// @param die the DIE to consider.
3182 ///
3183 /// @param where_offset where in the DIE stream we logically are.
3184 ///
3185 /// @return the interned_string that represents the pretty
3186 /// representation.
3187 interned_string
3188 get_die_pretty_representation(const Dwarf_Die *die, size_t where_offset) const
3189 {
3190 ABG_ASSERT(die);
3191
3193 die_pretty_repr_maps_.get_container(*const_cast<reader*>(this),
3194 die);
3195
3196 size_t die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3197 die_istring_map_type::const_iterator i = map.find(die_offset);
3198
3199 if (i == map.end())
3200 {
3201 reader& rdr = *const_cast<reader*>(this);
3202 string pretty_representation =
3203 die_pretty_print(rdr, die, where_offset);
3204 interned_string istr = env().intern(pretty_representation);
3205 map[die_offset] = istr;
3206 return istr;
3207 }
3208
3209 return i->second;
3210 }
3211
3212 /// Lookup the artifact that was built to represent a type that has
3213 /// the same pretty representation as the type denoted by a given
3214 /// DIE.
3215 ///
3216 /// Note that the DIE must have previously been associated with the
3217 /// artifact using the functions associate_die_to_decl or
3218 /// associate_die_to_type.
3219 ///
3220 /// Also, note that the scope of the lookup is the current ABI
3221 /// corpus.
3222 ///
3223 /// @param die the DIE to consider.
3224 ///
3225 /// @param where_offset where in the DIE stream we logically are.
3226 ///
3227 /// @return the type artifact found.
3229 lookup_type_artifact_from_die(Dwarf_Die *die) const
3230 {
3231 type_or_decl_base_sptr artifact =
3232 lookup_artifact_from_die(die, /*type_as_die=*/true);
3233 if (function_decl_sptr fn = is_function_decl(artifact))
3234 return fn->get_type();
3235 return artifact;
3236 }
3237
3238 /// Lookup the artifact that was built to represent a type or a
3239 /// declaration that has the same pretty representation as the type
3240 /// denoted by a given DIE.
3241 ///
3242 /// Note that the DIE must have previously been associated with the
3243 /// artifact using the functions associate_die_to_decl or
3244 /// associate_die_to_type.
3245 ///
3246 /// Also, note that the scope of the lookup is the current ABI
3247 /// corpus.
3248 ///
3249 /// @param die the DIE to consider.
3250 ///
3251 /// @param where_offset where in the DIE stream we logically are.
3252 ///
3253 /// @param die_as_type if true, it means the DIE is to be considered
3254 /// as a type.
3255 ///
3256 /// @return the artifact found.
3258 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3259 {
3260 Dwarf_Die equiv_die;
3261 if (!get_or_compute_canonical_die(die, equiv_die, /*where=*/0, die_as_type))
3262 return type_or_decl_base_sptr();
3263
3264 const die_artefact_map_type& m =
3265 die_as_type
3266 ? type_die_artefact_maps().get_container(*this, &equiv_die)
3267 : decl_die_artefact_maps().get_container(*this, &equiv_die);
3268
3269 size_t die_offset = dwarf_dieoffset(&equiv_die);
3270 die_artefact_map_type::const_iterator i = m.find(die_offset);
3271
3272 if (i == m.end())
3273 return type_or_decl_base_sptr();
3274 return i->second;
3275 }
3276
3277 /// Lookup the artifact that was built to represent a type or a
3278 /// declaration that has the same pretty representation as the type
3279 /// denoted by the offset of a given DIE.
3280 ///
3281 /// Note that the DIE must have previously been associated with the
3282 /// artifact using either associate_die_to_decl or
3283 /// associate_die_to_type.
3284 ///
3285 /// Also, note that the scope of the lookup is the current ABI
3286 /// corpus.
3287 ///
3288 /// @param die the DIE to consider.
3289 ///
3290 /// @param where_offset where in the DIE stream we logically are.
3291 ///
3292 /// @param die_as_type if true, it means the DIE is to be considered
3293 /// as a type.
3294 ///
3295 /// @return the artifact found.
3297 lookup_artifact_from_die_offset(Dwarf_Off die_offset,
3298 die_source source,
3299 bool die_as_type = false) const
3300 {
3301 const die_artefact_map_type& m =
3302 die_as_type
3303 ? type_die_artefact_maps().get_container(source)
3304 : decl_die_artefact_maps().get_container(source);
3305
3306 die_artefact_map_type::const_iterator i = m.find(die_offset);
3307 if (i == m.end())
3308 return type_or_decl_base_sptr();
3309 return i->second;
3310 }
3311
3312 /// Get the language used to generate a given DIE.
3313 ///
3314 /// @param die the DIE to consider.
3315 ///
3316 /// @param lang the resulting language.
3317 ///
3318 /// @return true iff the language of the DIE was found.
3319 bool
3320 get_die_language(const Dwarf_Die *die, translation_unit::language &lang) const
3321 {
3322 Dwarf_Die cu_die;
3323 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
3324
3325 uint64_t l = 0;
3326 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
3327 return false;
3328
3329 lang = dwarf_language_to_tu_language(l);
3330 return true;
3331 }
3332
3333 /// Test if a given DIE originates from a program written in the C
3334 /// language.
3335 ///
3336 /// @param die the DIE to consider.
3337 ///
3338 /// @return true iff @p die originates from a program in the C
3339 /// language.
3340 bool
3341 die_is_in_c(const Dwarf_Die *die) const
3342 {
3343 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3344 if (!get_die_language(die, l))
3345 return false;
3346 return is_c_language(l);
3347 }
3348
3349 /// Test if a given DIE originates from a program written in the C++
3350 /// language.
3351 ///
3352 /// @param die the DIE to consider.
3353 ///
3354 /// @return true iff @p die originates from a program in the C++
3355 /// language.
3356 bool
3357 die_is_in_cplus_plus(const Dwarf_Die *die) const
3358 {
3359 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3360 if (!get_die_language(die, l))
3361 return false;
3362 return is_cplus_plus_language(l);
3363 }
3364
3365 /// Test if a given DIE originates from a program written either in
3366 /// C or C++.
3367 ///
3368 /// @param die the DIE to consider.
3369 ///
3370 /// @return true iff @p die originates from a program written either in
3371 /// C or C++.
3372 bool
3373 die_is_in_c_or_cplusplus(const Dwarf_Die *die) const
3374 {
3375 translation_unit::language l = translation_unit::LANG_UNKNOWN;
3376 if (!get_die_language(die, l))
3377 return false;
3378 return (is_cplus_plus_language(l) || is_c_language(l));
3379 }
3380
3381 /// Check if we can assume the One Definition Rule[1] to be relevant
3382 /// for the current translation unit.
3383 ///
3384 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3385 ///
3386 /// At the moment this returns true if the current translation unit
3387 /// is in C++ language. In that case, it's relevant to assume that
3388 /// we use optimizations based on the ODR.
3389 bool
3390 odr_is_relevant() const
3391 {return odr_is_relevant(cur_transl_unit()->get_language());}
3392
3393 /// Check if we can assume the One Definition Rule[1] to be relevant
3394 /// for a given language.
3395 ///
3396 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3397 ///
3398 /// At the moment this returns true if the language considered
3399 /// is C++, Java or Ada.
3400 bool
3402 {
3403 return (is_cplus_plus_language(l)
3404 || is_java_language(l)
3405 || is_ada_language(l));
3406 }
3407
3408 /// Check if we can assume the One Definition Rule to be relevant
3409 /// for a given DIE.
3410 ///
3411 /// @param die the DIE to consider.
3412 ///
3413 /// @return true if the ODR is relevant for @p die.
3414 bool
3415 odr_is_relevant(Dwarf_Off die_offset, die_source source) const
3416 {
3417 Dwarf_Die die;
3418 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(dwarf_per_die_source(source)),
3419 die_offset, &die));
3420 return odr_is_relevant(&die);
3421 }
3422
3423 /// Check if we can assume the One Definition Rule to be relevant
3424 /// for a given DIE.
3425 ///
3426 /// @param die the DIE to consider.
3427 ///
3428 /// @return true if the ODR is relevant for @p die.
3429 bool
3430 odr_is_relevant(const Dwarf_Die *die) const
3431 {
3433 if (!get_die_language(die, lang))
3434 return odr_is_relevant();
3435
3436 return odr_is_relevant(lang);
3437 }
3438
3439 /// Getter for the maps set that associates a decl DIE offset to an
3440 /// artifact.
3441 ///
3442 /// @return the maps set that associates a decl DIE offset to an
3443 /// artifact.
3444 die_source_dependant_container_set<die_artefact_map_type>&
3445 decl_die_artefact_maps()
3446 {return decl_die_artefact_maps_;}
3447
3448 /// Getter for the maps set that associates a decl DIE offset to an
3449 /// artifact.
3450 ///
3451 /// @return the maps set that associates a decl DIE offset to an
3452 /// artifact.
3453 const die_source_dependant_container_set<die_artefact_map_type>&
3454 decl_die_artefact_maps() const
3455 {return decl_die_artefact_maps_;}
3456
3457 /// Getter for the maps set that associates a type DIE offset to an
3458 /// artifact.
3459 ///
3460 /// @return the maps set that associates a type DIE offset to an
3461 /// artifact.
3462 die_source_dependant_container_set<die_artefact_map_type>&
3463 type_die_artefact_maps()
3464 {return type_die_artefact_maps_;}
3465
3466 /// Getter for the maps set that associates a type DIE offset to an
3467 /// artifact.
3468 ///
3469 /// @return the maps set that associates a type DIE offset to an
3470 /// artifact.
3471 const die_source_dependant_container_set<die_artefact_map_type>&
3472 type_die_artefact_maps() const
3473 {return type_die_artefact_maps_;}
3474
3475 /// Getter of the maps that associates function type representations
3476 /// to function types, inside a translation unit.
3477 ///
3478 /// @return the maps that associates function type representations
3479 /// to function types, inside a translation unit.
3481 per_tu_repr_to_fn_type_maps()
3482 {return per_tu_repr_to_fn_type_maps_;}
3483
3484 /// Getter of the maps that associates function type representations
3485 /// to function types, inside a translation unit.
3486 ///
3487 /// @return the maps that associates function type representations
3488 /// to function types, inside a translation unit.
3490 per_tu_repr_to_fn_type_maps() const
3491 {return per_tu_repr_to_fn_type_maps_;}
3492
3493 /// Associate the representation of a function type DIE to a given
3494 /// function type, inside the current translation unit.
3495 ///
3496 /// @param die the DIE to associate to the function type, using its
3497 /// representation.
3498 ///
3499 /// @param fn_type the function type to associate to @p die.
3500 void
3501 associate_die_repr_to_fn_type_per_tu(const Dwarf_Die *die,
3502 const function_type_sptr &fn_type)
3503 {
3504 if (!die_is_function_type(die))
3505 return;
3506
3507 interned_string repr =
3508 get_die_pretty_type_representation(die, /*where=*/0);
3509 ABG_ASSERT(!repr.empty());
3510
3511 per_tu_repr_to_fn_type_maps()[repr]= fn_type;
3512 }
3513
3514 /// Lookup the function type associated to a given function type
3515 /// DIE, in the current translation unit.
3516 ///
3517 /// @param die the DIE of function type to consider.
3518 ///
3519 /// @return the @ref function_type_sptr associated to @p die, or nil
3520 /// of no function_type is associated to @p die.
3522 lookup_fn_type_from_die_repr_per_tu(const Dwarf_Die *die)
3523 {
3524 if (!die_is_function_type(die))
3525 return function_type_sptr();
3526
3527 interned_string repr = die_name(die).empty() ?
3528 get_die_pretty_type_representation(die, /*where=*/0)
3529 : get_die_pretty_representation(die, /*where=*/0);
3530 ABG_ASSERT(!repr.empty());
3531
3532 istring_fn_type_map_type::const_iterator i =
3533 per_tu_repr_to_fn_type_maps().find(repr);
3534
3535 if (i == per_tu_repr_to_fn_type_maps().end())
3536 return function_type_sptr();
3537
3538 return i->second;
3539 }
3540
3541 /// Set the canonical DIE offset of a given DIE.
3542 ///
3543 /// @param canonical_dies the vector that holds canonical DIEs.
3544 ///
3545 /// @param die_offset the offset of the DIE to set the canonical DIE
3546 /// for.
3547 ///
3548 /// @param canonical_die_offset the canonical DIE offset to
3549 /// associate to @p die_offset.
3550 void
3551 set_canonical_die_offset(offset_offset_map_type &canonical_dies,
3552 Dwarf_Off die_offset,
3553 Dwarf_Off canonical_die_offset) const
3554 {
3555 canonical_dies[die_offset] = canonical_die_offset;}
3556
3557 /// Set the canonical DIE offset of a given DIE.
3558 ///
3559 ///
3560 /// @param die_offset the offset of the DIE to set the canonical DIE
3561 /// for.
3562 ///
3563 /// @param source the source of the DIE denoted by @p die_offset.
3564 ///
3565 /// @param canonical_die_offset the canonical DIE offset to
3566 /// associate to @p die_offset.
3567 ///
3568 /// @param die_as_type if true, it means that @p die_offset has to
3569 /// be considered as a type.
3570 void
3571 set_canonical_die_offset(Dwarf_Off die_offset,
3572 die_source source,
3573 Dwarf_Off canonical_die_offset,
3574 bool die_as_type) const
3575 {
3576 offset_offset_map_type &canonical_dies =
3577 die_as_type
3578 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3579 get_container(source)
3580 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3581 get_container(source);
3582
3583 set_canonical_die_offset(canonical_dies,
3584 die_offset,
3585 canonical_die_offset);
3586 }
3587
3588 /// Set the canonical DIE offset of a given DIE.
3589 ///
3590 ///
3591 /// @param die the DIE to set the canonical DIE for.
3592 ///
3593 /// @param canonical_die_offset the canonical DIE offset to
3594 /// associate to @p die_offset.
3595 ///
3596 /// @param die_as_type if true, it means that @p die has to be
3597 /// considered as a type.
3598 void
3599 set_canonical_die_offset(const Dwarf_Die *die,
3600 Dwarf_Off canonical_die_offset,
3601 bool die_as_type) const
3602 {
3603 const die_source source = get_die_source(die);
3604
3605 Dwarf_Off die_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(die));
3606
3607 set_canonical_die_offset(die_offset, source,
3608 canonical_die_offset,
3609 die_as_type);
3610 }
3611
3612 /// Get the canonical DIE offset of a given DIE.
3613 ///
3614 /// @param canonical_dies the vector that contains canonical DIES.
3615 ///
3616 /// @param die_offset the offset of the DIE to consider.
3617 ///
3618 /// @return the canonical of the DIE denoted by @p die_offset, or
3619 /// zero if no canonical DIE was found.
3620 Dwarf_Off
3621 get_canonical_die_offset(offset_offset_map_type &canonical_dies,
3622 Dwarf_Off die_offset) const
3623 {
3624 offset_offset_map_type::const_iterator it = canonical_dies.find(die_offset);
3625 if (it == canonical_dies.end())
3626 return 0;
3627 return it->second;
3628 }
3629
3630 /// Get the canonical DIE offset of a given DIE.
3631 ///
3632 /// @param die_offset the offset of the DIE to consider.
3633 ///
3634 /// @param source the source of the DIE denoted by @p die_offset.
3635 ///
3636 /// @param die_as_type if true, it means that @p is to be considered
3637 /// as a type DIE.
3638 ///
3639 /// @return the canonical of the DIE denoted by @p die_offset, or
3640 /// zero if no canonical DIE was found.
3641 Dwarf_Off
3642 get_canonical_die_offset(Dwarf_Off die_offset,
3643 die_source source,
3644 bool die_as_type) const
3645 {
3646 offset_offset_map_type &canonical_dies =
3647 die_as_type
3648 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3649 get_container(source)
3650 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3651 get_container(source);
3652
3653 return get_canonical_die_offset(canonical_dies, die_offset);
3654 }
3655
3656 /// Erase the canonical type of a given DIE.
3657 ///
3658 /// @param die_offset the offset of the DIE to consider.
3659 ///
3660 /// @param source the source of the canonical type.
3661 ///
3662 /// @param die_as_type if true, it means that @p is to be considered
3663 /// as a type DIE.
3664 ///
3665 /// @return the canonical of the DIE denoted by @p die_offset, or
3666 /// zero if no canonical DIE was found and erased..
3667 bool
3668 erase_canonical_die_offset(Dwarf_Off die_offset,
3669 die_source source,
3670 bool die_as_type) const
3671 {
3672 offset_offset_map_type &canonical_dies =
3673 die_as_type
3674 ? const_cast<reader*>(this)->canonical_type_die_offsets_.
3675 get_container(source)
3676 : const_cast<reader*>(this)->canonical_decl_die_offsets_.
3677 get_container(source);
3678
3679 return canonical_dies.erase(die_offset);
3680 }
3681
3682
3683 /// Associate a DIE (representing a type) to the type that it
3684 /// represents.
3685 ///
3686 /// @param die the DIE to consider.
3687 ///
3688 /// @param type the type to associate the DIE to.
3689 ///
3690 /// @param where_offset where in the DIE stream we logically are.
3691 void
3692 associate_die_to_type(const Dwarf_Die *die,
3693 type_base_sptr type,
3694 size_t where)
3695 {
3696 if (!type)
3697 return;
3698
3699 Dwarf_Die equiv_die;
3700 if (!get_or_compute_canonical_die(die, equiv_die, where,
3701 /*die_as_type=*/true))
3702 return;
3703
3705 type_die_artefact_maps().get_container(*this, &equiv_die);
3706
3707 size_t die_offset = dwarf_dieoffset(&equiv_die);
3708 m[die_offset] = type;
3709 }
3710
3711 /// Lookup the type associated to a given DIE.
3712 ///
3713 /// Note that the DIE must have been associated to type by a
3714 /// previous invocation of the function
3715 /// reader::associate_die_to_type().
3716 ///
3717 /// @param die the DIE to consider.
3718 ///
3719 /// @return the type associated to the DIE or NULL if no type is
3720 /// associated to the DIE.
3721 type_base_sptr
3722 lookup_type_from_die(const Dwarf_Die* die) const
3723 {
3724 type_or_decl_base_sptr artifact =
3725 lookup_artifact_from_die(die, /*die_as_type=*/true);
3726 if (function_decl_sptr fn = is_function_decl(artifact))
3727 return fn->get_type();
3728 return is_type(artifact);
3729 }
3730
3731 /// Lookup the type associated to a DIE at a given offset, from a
3732 /// given source.
3733 ///
3734 /// Note that the DIE must have been associated to type by a
3735 /// previous invocation of the function
3736 /// reader::associate_die_to_type().
3737 ///
3738 /// @param die_offset the offset of the DIE to consider.
3739 ///
3740 /// @param source the source of the DIE to consider.
3741 ///
3742 /// @return the type associated to the DIE or NULL if no type is
3743 /// associated to the DIE.
3744 type_base_sptr
3745 lookup_type_from_die_offset(size_t die_offset, die_source source) const
3746 {
3747 type_base_sptr result;
3748 const die_artefact_map_type& m =
3749 type_die_artefact_maps().get_container(source);
3750 die_artefact_map_type::const_iterator i = m.find(die_offset);
3751 if (i != m.end())
3752 {
3753 if (function_decl_sptr fn = is_function_decl(i->second))
3754 return fn->get_type();
3755 result = is_type(i->second);
3756 }
3757
3758 if (!result)
3759 {
3760 // Maybe we are looking for a class type being constructed?
3761 const die_class_or_union_map_type& m = die_wip_classes_map(source);
3762 die_class_or_union_map_type::const_iterator i = m.find(die_offset);
3763
3764 if (i != m.end())
3765 result = i->second;
3766 }
3767
3768 if (!result)
3769 {
3770 // Maybe we are looking for a function type being constructed?
3772 die_wip_function_types_map(source);
3773 die_function_type_map_type::const_iterator i = m.find(die_offset);
3774
3775 if (i != m.end())
3776 result = i->second;
3777 }
3778
3779 return result;
3780 }
3781
3782 /// Getter of a map that associates a die that represents a
3783 /// class/struct with the declaration of the class, while the class
3784 /// is being constructed.
3785 ///
3786 /// @param source where the DIE is from.
3787 ///
3788 /// @return the map that associates a DIE to the class that is being
3789 /// built.
3791 die_wip_classes_map(die_source source) const
3792 {return const_cast<reader*>(this)->die_wip_classes_map(source);}
3793
3794 /// Getter of a map that associates a die that represents a
3795 /// class/struct with the declaration of the class, while the class
3796 /// is being constructed.
3797 ///
3798 /// @param source where the DIE comes from.
3799 ///
3800 /// @return the map that associates a DIE to the class that is being
3801 /// built.
3803 die_wip_classes_map(die_source source)
3804 {
3805 switch (source)
3806 {
3807 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3808 break;
3809 case ALT_DEBUG_INFO_DIE_SOURCE:
3810 return alternate_die_wip_classes_map_;
3811 case TYPE_UNIT_DIE_SOURCE:
3812 return type_unit_die_wip_classes_map_;
3813 case NO_DEBUG_INFO_DIE_SOURCE:
3814 case NUMBER_OF_DIE_SOURCES:
3816 }
3817 return die_wip_classes_map_;
3818 }
3819
3820 /// Getter for a map that associates a die (that represents a
3821 /// function type) whith a function type, while the function type is
3822 /// being constructed (WIP == work in progress).
3823 ///
3824 /// @param source where the DIE comes from.n
3825 ///
3826 /// @return the map of wip function types.
3828 die_wip_function_types_map(die_source source) const
3829 {return const_cast<reader*>(this)->die_wip_function_types_map(source);}
3830
3831 /// Getter for a map that associates a die (that represents a
3832 /// function type) whith a function type, while the function type is
3833 /// being constructed (WIP == work in progress).
3834 ///
3835 /// @param source where DIEs of the map come from.
3836 ///
3837 /// @return the map of wip function types.
3839 die_wip_function_types_map(die_source source)
3840 {
3841 switch (source)
3842 {
3843 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
3844 break;
3845 case ALT_DEBUG_INFO_DIE_SOURCE:
3846 return alternate_die_wip_function_types_map_;
3847 case TYPE_UNIT_DIE_SOURCE:
3848 return type_unit_die_wip_function_types_map_;
3849 case NO_DEBUG_INFO_DIE_SOURCE:
3850 case NUMBER_OF_DIE_SOURCES:
3852 }
3853 return die_wip_function_types_map_;
3854 }
3855
3856 /// Getter for a map that associates a die with a function decl
3857 /// which has a linkage name but no elf symbol yet.
3858 ///
3859 /// This is to fixup function decls with linkage names, but with no
3860 /// link to their underlying elf symbol. There are some DIEs like
3861 /// that in DWARF sometimes, especially when the compiler optimizes
3862 /// stuff aggressively.
3864 die_function_decl_with_no_symbol_map()
3865 {return die_function_with_no_symbol_map_;}
3866
3867 /// Return true iff a given offset is for the DIE of a class that is
3868 /// being built, but that is not fully built yet. WIP == "work in
3869 /// progress".
3870 ///
3871 /// @param offset the DIE offset to consider.
3872 ///
3873 /// @param source where the DIE of the map come from.
3874 ///
3875 /// @return true iff @p offset is the offset of the DIE of a class
3876 /// that is being currently built.
3877 bool
3878 is_wip_class_die_offset(Dwarf_Off offset, die_source source) const
3879 {
3880 die_class_or_union_map_type::const_iterator i =
3881 die_wip_classes_map(source).find(offset);
3882 return (i != die_wip_classes_map(source).end());
3883 }
3884
3885 /// Return true iff a given offset is for the DIE of a function type
3886 /// that is being built at the moment, but is not fully built yet.
3887 /// WIP == work in progress.
3888 ///
3889 /// @param offset DIE offset to consider.
3890 ///
3891 /// @param source where the DIE comes from.
3892 ///
3893 /// @return true iff @p offset is the offset of the DIE of a
3894 /// function type that is being currently built.
3895 bool
3896 is_wip_function_type_die_offset(Dwarf_Off offset, die_source source) const
3897 {
3898 die_function_type_map_type::const_iterator i =
3899 die_wip_function_types_map(source).find(offset);
3900 return (i != die_wip_function_types_map(source).end());
3901 }
3902
3903 /// Sometimes, a data member die can erroneously have an empty name as
3904 /// a result of a bug of the DWARF emitter.
3905 ///
3906 /// This is what happens in
3907 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3908 ///
3909 /// In that case, this function constructs an artificial name for that
3910 /// data member. The pattern of the name is as follows:
3911 ///
3912 /// "unnamed-@-<location>".
3913 ///
3914 ///location is either the value of the data member location of the
3915 ///data member if it has one or concatenation of its source location
3916 ///if it has none. If no location can be calculated then the function
3917 ///returns the empty string.
3918 string
3919 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die)
3920 {
3921 string result;
3922 // Let's make sure we are looking at a data member with an empty
3923 // name ...
3924 if (!die
3925 || dwarf_tag(die) != DW_TAG_member
3926 || !die_name(die).empty())
3927 return result;
3928
3929 // ... and yet, it's not an anonymous data member (aka unnamed
3930 // field) as described in
3931 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3932 if (die_is_anonymous_data_member(die))
3933 return result;
3934
3935 // If we come this far, it means we are looking at a buggy data
3936 // member with no name. Let's build a name for it so that it can be
3937 // addressed.
3938 int64_t offset_in_bits = 0;
3939 bool has_offset = die_member_offset(*this, die, offset_in_bits);
3940 location loc;
3941 if (!has_offset)
3942 {
3943 loc = die_location(*this, die);
3944 if (!loc)
3945 return result;
3946 }
3947
3948 std::ostringstream o;
3949 o << "unnamed-dm-@-";
3950 if (has_offset)
3951 o << "offset-" << offset_in_bits << "bits";
3952 else
3953 o << "loc-" << loc.expand();
3954
3955 return o.str();
3956 }
3957
3958 /// Getter for the map of declaration-only classes that are to be
3959 /// resolved to their definition classes by the end of the corpus
3960 /// loading.
3961 ///
3962 /// @return a map of string -> vector of classes where the key is
3963 /// the fully qualified name of the class and the value is the
3964 /// vector of declaration-only class.
3966 declaration_only_classes() const
3967 {return decl_only_classes_map_;}
3968
3969 /// Getter for the map of declaration-only classes that are to be
3970 /// resolved to their definition classes by the end of the corpus
3971 /// loading.
3972 ///
3973 /// @return a map of string -> vector of classes where the key is
3974 /// the fully qualified name of the class and the value is the
3975 /// vector of declaration-only class.
3977 declaration_only_classes()
3978 {return decl_only_classes_map_;}
3979
3980 /// If a given class is a declaration-only class then stash it on
3981 /// the side so that at the end of the corpus reading we can resolve
3982 /// it to its definition.
3983 ///
3984 /// @param klass the class to consider.
3985 void
3986 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
3987 {
3988 if (cou->get_is_declaration_only()
3989 && cou->get_definition_of_declaration() == 0)
3990 {
3991 string qn = cou->get_qualified_name();
3992 string_classes_or_unions_map::iterator record =
3993 declaration_only_classes().find(qn);
3994 if (record == declaration_only_classes().end())
3995 declaration_only_classes()[qn].push_back(cou);
3996 else
3997 record->second.push_back(cou);
3998 }
3999 }
4000
4001 /// Test if a given declaration-only class has been scheduled for
4002 /// resolution to a defined class.
4003 ///
4004 /// @param klass the class to consider for the test.
4005 ///
4006 /// @return true iff @p klass is a declaration-only class and if
4007 /// it's been scheduled for resolution to a defined class.
4008 bool
4009 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
4010 {
4011 if (cou->get_is_declaration_only())
4012 return (declaration_only_classes().find(cou->get_qualified_name())
4013 != declaration_only_classes().end());
4014
4015 return false;
4016 }
4017
4018 /// Compare two ABI artifacts in a context which canonicalization
4019 /// has not be done yet.
4020 ///
4021 /// @param l the left-hand-side operand of the comparison
4022 ///
4023 /// @param r the right-hand-side operand of the comparison.
4024 ///
4025 /// @return true if @p l equals @p r.
4026 bool
4027 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
4028 const type_or_decl_base_sptr &r)
4029 {
4030 if (!l || !r)
4031 return !!l == !!r;
4032
4033 const environment& e = l->get_environment();
4034 ABG_ASSERT(!e.canonicalization_is_done());
4035
4036 e.priv_->allow_type_comparison_results_caching(true);
4037 bool s0 = e.decl_only_class_equals_definition();
4038 e.decl_only_class_equals_definition(true);
4039 bool equal = l == r;
4040 e.decl_only_class_equals_definition(s0);
4041 e.priv_->clear_type_comparison_results_cache();
4042 e.priv_->allow_type_comparison_results_caching(false);
4043 return equal;
4044 }
4045
4046 /// Walk the declaration-only classes that have been found during
4047 /// the building of the corpus and resolve them to their definitions.
4048 void
4049 resolve_declaration_only_classes()
4050 {
4051 vector<string> resolved_classes;
4052
4053 for (string_classes_or_unions_map::iterator i =
4054 declaration_only_classes().begin();
4055 i != declaration_only_classes().end();
4056 ++i)
4057 {
4058 bool to_resolve = false;
4059 for (classes_or_unions_type::iterator j = i->second.begin();
4060 j != i->second.end();
4061 ++j)
4062 if ((*j)->get_is_declaration_only()
4063 && ((*j)->get_definition_of_declaration() == 0))
4064 to_resolve = true;
4065
4066 if (!to_resolve)
4067 {
4068 resolved_classes.push_back(i->first);
4069 continue;
4070 }
4071
4072 // Now, for each decl-only class that have the current name
4073 // 'i->first', let's try to poke at the fully defined class
4074 // that is defined in the same translation unit as the
4075 // declaration.
4076 //
4077 // If we find one class (defined in the TU of the declaration)
4078 // that defines the declaration, then the declaration can be
4079 // resolved to that class.
4080 //
4081 // If no defining class is found in the TU of the declaration,
4082 // then there are possibly three cases to consider:
4083 //
4084 // 1/ There is exactly one class that defines the
4085 // declaration and that class is defined in another TU. In
4086 // this case, the declaration is resolved to that
4087 // definition.
4088 //
4089 // 2/ There are more than one class that define that
4090 // declaration and none of them is defined in the TU of the
4091 // declaration. If those classes are all different, then
4092 // the declaration is left unresolved.
4093 //
4094 // 3/ No class defines the declaration. In this case, the
4095 // declaration is left unresoved.
4096
4097 // So get the classes that might define the current
4098 // declarations which name is i->first.
4099 const type_base_wptrs_type *classes =
4100 lookup_class_types(i->first, *corpus());
4101 if (!classes)
4102 classes = lookup_union_types(i->first, *corpus());
4103
4104 if (!classes)
4105 continue;
4106
4107 // This is a map that associates the translation unit path to
4108 // the class (that potentially defines the declarations that
4109 // we consider) that are defined in that translation unit. It
4110 // should stay ordered by using the TU path as key to ensure
4111 // stability of the order of classe definitions in ABIXML
4112 // output.
4113 map<string, class_or_union_sptr> per_tu_class_map;
4114 for (type_base_wptrs_type::const_iterator c = classes->begin();
4115 c != classes->end();
4116 ++c)
4117 {
4118 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4119 ABG_ASSERT(klass);
4120
4122 if (klass->get_is_declaration_only())
4123 continue;
4124
4125 string tu_path = klass->get_translation_unit()->get_absolute_path();
4126 if (tu_path.empty())
4127 continue;
4128
4129 // Build a map that associates the translation unit path
4130 // to the class (that potentially defines the declarations
4131 // that we consider) that are defined in that translation unit.
4132 per_tu_class_map[tu_path] = klass;
4133 }
4134
4135 if (!per_tu_class_map.empty())
4136 {
4137 // Walk the declarations to resolve and resolve them
4138 // either to the definitions that are in the same TU as
4139 // the declaration, or to the definition found elsewhere,
4140 // if there is only one such definition.
4141 for (classes_or_unions_type::iterator j = i->second.begin();
4142 j != i->second.end();
4143 ++j)
4144 {
4145 if ((*j)->get_is_declaration_only()
4146 && ((*j)->get_definition_of_declaration() == 0))
4147 {
4148 string tu_path =
4149 (*j)->get_translation_unit()->get_absolute_path();
4150 map<string, class_or_union_sptr>::const_iterator e =
4151 per_tu_class_map.find(tu_path);
4152 if (e != per_tu_class_map.end())
4153 (*j)->set_definition_of_declaration(e->second);
4154 else if (per_tu_class_map.size() == 1)
4155 (*j)->set_definition_of_declaration
4156 (per_tu_class_map.begin()->second);
4157 else
4158 {
4159 // We are in case where there are more than
4160 // one definition for the declaration. Let's
4161 // see if they are all equal. If they are,
4162 // then the declaration resolves to the
4163 // definition. Otherwise, we are in the case
4164 // 3/ described above.
4165 map<string,
4166 class_or_union_sptr>::const_iterator it;
4167 class_or_union_sptr first_class =
4168 per_tu_class_map.begin()->second;
4169 bool all_class_definitions_are_equal = true;
4170 for (it = per_tu_class_map.begin();
4171 it != per_tu_class_map.end();
4172 ++it)
4173 {
4174 if (it == per_tu_class_map.begin())
4175 continue;
4176 else
4177 {
4178 if (!compare_before_canonicalisation(it->second,
4179 first_class))
4180 {
4181 all_class_definitions_are_equal = false;
4182 break;
4183 }
4184 }
4185 }
4186 if (all_class_definitions_are_equal)
4187 (*j)->set_definition_of_declaration(first_class);
4188 }
4189 }
4190 }
4191 resolved_classes.push_back(i->first);
4192 }
4193 }
4194
4195 size_t num_decl_only_classes = declaration_only_classes().size(),
4196 num_resolved = resolved_classes.size();
4197 if (show_stats())
4198 cerr << "resolved " << num_resolved
4199 << " class declarations out of "
4200 << num_decl_only_classes
4201 << "\n";
4202
4203 for (vector<string>::const_iterator i = resolved_classes.begin();
4204 i != resolved_classes.end();
4205 ++i)
4206 declaration_only_classes().erase(*i);
4207
4208 if (show_stats() && !declaration_only_classes().empty())
4209 {
4210 cerr << "Here are the "
4211 << num_decl_only_classes - num_resolved
4212 << " unresolved class declarations:\n";
4213 for (string_classes_or_unions_map::iterator i =
4214 declaration_only_classes().begin();
4215 i != declaration_only_classes().end();
4216 ++i)
4217 cerr << " " << i->first << "\n";
4218 }
4219 }
4220
4221 /// Getter for the map of declaration-only enums that are to be
4222 /// resolved to their definition enums by the end of the corpus
4223 /// loading.
4224 ///
4225 /// @return a map of string -> vector of enums where the key is
4226 /// the fully qualified name of the enum and the value is the
4227 /// vector of declaration-only enum.
4228 const string_enums_map&
4229 declaration_only_enums() const
4230 {return decl_only_enums_map_;}
4231
4232 /// Getter for the map of declaration-only enums that are to be
4233 /// resolved to their definition enums by the end of the corpus
4234 /// loading.
4235 ///
4236 /// @return a map of string -> vector of enums where the key is
4237 /// the fully qualified name of the enum and the value is the
4238 /// vector of declaration-only enum.
4240 declaration_only_enums()
4241 {return decl_only_enums_map_;}
4242
4243 /// If a given enum is a declaration-only enum then stash it on
4244 /// the side so that at the end of the corpus reading we can resolve
4245 /// it to its definition.
4246 ///
4247 /// @param enom the enum to consider.
4248 void
4249 maybe_schedule_declaration_only_enum_for_resolution(enum_type_decl_sptr& enom)
4250 {
4251 if (enom->get_is_declaration_only()
4252 && enom->get_definition_of_declaration() == 0)
4253 {
4254 string qn = enom->get_qualified_name();
4255 string_enums_map::iterator record =
4256 declaration_only_enums().find(qn);
4257 if (record == declaration_only_enums().end())
4258 declaration_only_enums()[qn].push_back(enom);
4259 else
4260 record->second.push_back(enom);
4261 }
4262 }
4263
4264 /// Test if a given declaration-only enum has been scheduled for
4265 /// resolution to a defined enum.
4266 ///
4267 /// @param enom the enum to consider for the test.
4268 ///
4269 /// @return true iff @p enom is a declaration-only enum and if
4270 /// it's been scheduled for resolution to a defined enum.
4271 bool
4272 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4273 {
4274 if (enom->get_is_declaration_only())
4275 return (declaration_only_enums().find(enom->get_qualified_name())
4276 != declaration_only_enums().end());
4277
4278 return false;
4279 }
4280
4281 /// Walk the declaration-only enums that have been found during
4282 /// the building of the corpus and resolve them to their definitions.
4283 ///
4284 /// TODO: Do away with this function by factorizing it with
4285 /// resolve_declaration_only_classes. All declaration-only decls
4286 /// could be handled the same way as declaration-only-ness is a
4287 /// property of abigail::ir::decl_base now.
4288 void
4289 resolve_declaration_only_enums()
4290 {
4291 vector<string> resolved_enums;
4292
4293 for (string_enums_map::iterator i =
4294 declaration_only_enums().begin();
4295 i != declaration_only_enums().end();
4296 ++i)
4297 {
4298 bool to_resolve = false;
4299 for (enums_type::iterator j = i->second.begin();
4300 j != i->second.end();
4301 ++j)
4302 if ((*j)->get_is_declaration_only()
4303 && ((*j)->get_definition_of_declaration() == 0))
4304 to_resolve = true;
4305
4306 if (!to_resolve)
4307 {
4308 resolved_enums.push_back(i->first);
4309 continue;
4310 }
4311
4312 // Now, for each decl-only enum that have the current name
4313 // 'i->first', let's try to poke at the fully defined enum
4314 // that is defined in the same translation unit as the
4315 // declaration.
4316 //
4317 // If we find one enum (defined in the TU of the declaration)
4318 // that defines the declaration, then the declaration can be
4319 // resolved to that enum.
4320 //
4321 // If no defining enum is found in the TU of the declaration,
4322 // then there are possibly three cases to consider:
4323 //
4324 // 1/ There is exactly one enum that defines the
4325 // declaration and that enum is defined in another TU. In
4326 // this case, the declaration is resolved to that
4327 // definition.
4328 //
4329 // 2/ There are more than one enum that define that
4330 // declaration and none of them is defined in the TU of the
4331 // declaration. In this case, the declaration is left
4332 // unresolved.
4333 //
4334 // 3/ No enum defines the declaration. In this case, the
4335 // declaration is left unresoved.
4336
4337 // So get the enums that might define the current
4338 // declarations which name is i->first.
4339 const type_base_wptrs_type *enums =
4340 lookup_enum_types(i->first, *corpus());
4341 if (!enums)
4342 continue;
4343
4344 // This is a map that associates the translation unit path to
4345 // the enum (that potentially defines the declarations that
4346 // we consider) that are defined in that translation unit. It
4347 // should stay ordered by using the TU path as key to ensure
4348 // stability of the order of enum definitions in ABIXML
4349 // output.
4350 map<string, enum_type_decl_sptr> per_tu_enum_map;
4351 for (type_base_wptrs_type::const_iterator c = enums->begin();
4352 c != enums->end();
4353 ++c)
4354 {
4355 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4356 ABG_ASSERT(enom);
4357
4359 if (enom->get_is_declaration_only())
4360 continue;
4361
4362 string tu_path = enom->get_translation_unit()->get_absolute_path();
4363 if (tu_path.empty())
4364 continue;
4365
4366 // Build a map that associates the translation unit path
4367 // to the enum (that potentially defines the declarations
4368 // that we consider) that are defined in that translation unit.
4369 per_tu_enum_map[tu_path] = enom;
4370 }
4371
4372 if (!per_tu_enum_map.empty())
4373 {
4374 // Walk the declarations to resolve and resolve them
4375 // either to the definitions that are in the same TU as
4376 // the declaration, or to the definition found elsewhere,
4377 // if there is only one such definition.
4378 for (enums_type::iterator j = i->second.begin();
4379 j != i->second.end();
4380 ++j)
4381 {
4382 if ((*j)->get_is_declaration_only()
4383 && ((*j)->get_definition_of_declaration() == 0))
4384 {
4385 string tu_path =
4386 (*j)->get_translation_unit()->get_absolute_path();
4387 map<string, enum_type_decl_sptr>::const_iterator e =
4388 per_tu_enum_map.find(tu_path);
4389 if (e != per_tu_enum_map.end())
4390 (*j)->set_definition_of_declaration(e->second);
4391 else if (per_tu_enum_map.size() == 1)
4392 (*j)->set_definition_of_declaration
4393 (per_tu_enum_map.begin()->second);
4394 else
4395 {
4396 // We are in case where there are more than
4397 // one definition for the declaration. Let's
4398 // see if they are all equal. If they are,
4399 // then the declaration resolves to the
4400 // definition. Otherwise, we are in the case
4401 // 3/ described above.
4402 map<string,
4403 enum_type_decl_sptr>::const_iterator it;
4404 enum_type_decl_sptr first_enum =
4405 per_tu_enum_map.begin()->second;
4406 bool all_enum_definitions_are_equal = true;
4407 for (it = per_tu_enum_map.begin();
4408 it != per_tu_enum_map.end();
4409 ++it)
4410 {
4411 if (it == per_tu_enum_map.begin())
4412 continue;
4413 else
4414 {
4415 if (!compare_before_canonicalisation(it->second,
4416 first_enum))
4417 {
4418 all_enum_definitions_are_equal = false;
4419 break;
4420 }
4421 }
4422 }
4423 if (all_enum_definitions_are_equal)
4424 (*j)->set_definition_of_declaration(first_enum);
4425 }
4426 }
4427 }
4428 resolved_enums.push_back(i->first);
4429 }
4430 }
4431
4432 size_t num_decl_only_enums = declaration_only_enums().size(),
4433 num_resolved = resolved_enums.size();
4434 if (show_stats())
4435 cerr << "resolved " << num_resolved
4436 << " enum declarations out of "
4437 << num_decl_only_enums
4438 << "\n";
4439
4440 for (vector<string>::const_iterator i = resolved_enums.begin();
4441 i != resolved_enums.end();
4442 ++i)
4443 declaration_only_enums().erase(*i);
4444
4445 if (show_stats() && !declaration_only_enums().empty())
4446 {
4447 cerr << "Here are the "
4448 << num_decl_only_enums - num_resolved
4449 << " unresolved enum declarations:\n";
4450 for (string_enums_map::iterator i = declaration_only_enums().begin();
4451 i != declaration_only_enums().end();
4452 ++i)
4453 cerr << " " << i->first << "\n";
4454 }
4455 }
4456
4457 /// Test if a symbol belongs to a function of the current ABI
4458 /// corpus.
4459 ///
4460 /// This is a sub-routine of fixup_functions_with_no_symbols.
4461 ///
4462 /// @param fn the function symbol to consider.
4463 ///
4464 /// @returnt true if @p fn belongs to a function of the current ABI
4465 /// corpus.
4466 bool
4467 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4468 {
4469 corpus_sptr corp = corpus();
4470 if (!corp)
4471 return false;
4472
4473 string id = fn->get_id_string();
4474
4475 const std::unordered_set<function_decl*> *fns = corp->lookup_functions(id);
4476 if (!fns)
4477 return false;
4478
4479 for (auto f : *fns)
4480 if (f->get_symbol())
4481 return true;
4482
4483 return false;
4484 }
4485
4486 /// Some functions described by DWARF may have their linkage name
4487 /// set, but no link to their actual underlying elf symbol. When
4488 /// these are virtual member functions, comparing the enclosing type
4489 /// against another one which has its underlying symbol properly set
4490 /// might lead to spurious type changes.
4491 ///
4492 /// If the corpus contains a symbol with the same name as the
4493 /// linkage name of the function, then set up the link between the
4494 /// function and its underlying symbol.
4495 ///
4496 /// Note that for the moment, only virtual member functions are
4497 /// fixed up like this. This is because they really are the only
4498 /// fuctions of functions that can affect types (in spurious ways).
4499 void
4500 fixup_functions_with_no_symbols()
4501 {
4502 corpus_sptr corp = corpus();
4503 if (!corp)
4504 return;
4505
4506 die_function_decl_map_type &fns_with_no_symbol =
4507 die_function_decl_with_no_symbol_map();
4508
4509 if (do_log())
4510 cerr << fns_with_no_symbol.size()
4511 << " functions to fixup, potentially\n";
4512
4513 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4514 i != fns_with_no_symbol.end();
4515 ++i)
4516 if (elf_symbol_sptr sym =
4517 corp->lookup_function_symbol(i->second->get_linkage_name()))
4518 {
4519 // So i->second is a virtual member function that was
4520 // previously scheduled to be set a function symbol.
4521 //
4522 // But if it appears that it now has a symbol already set,
4523 // then do not set a symbol to it again.
4524 //
4525 // Or if it appears that another virtual member function
4526 // from the current ABI Corpus, with the same linkage
4527 // (mangled) name has already been set a symbol, then do not
4528 // set a symbol to this function either. Otherwise, there
4529 // will be two virtual member functions with the same symbol
4530 // in the class and that leads to spurious hard-to-debug
4531 // change reports later down the road.
4532 if (i->second->get_symbol()
4533 || symbol_already_belongs_to_a_function(sym))
4534 continue;
4535
4536 ABG_ASSERT(is_member_function(i->second));
4538 i->second->set_symbol(sym);
4539 // The function_decl now has an associated (public) ELF symbol so
4540 // it ought to be advertised as being public.
4541 i->second->set_is_in_public_symbol_table(true);
4542 // Add the function to the set of exported decls of the
4543 // current corpus.
4544 maybe_add_fn_to_exported_decls(i->second.get());
4545 if (do_log())
4546 cerr << "fixed up '"
4547 << i->second->get_pretty_representation()
4548 << "' with symbol '"
4549 << sym->get_id_string()
4550 << "'\n";
4551 }
4552
4553 fns_with_no_symbol.clear();
4554 }
4555
4556 /// Return a reference to the vector containing the types created
4557 /// during the binary analysis but that are not tied to a given
4558 /// DWARF DIE.
4559 ///
4560 /// @return reference to the vector containing the types created
4561 /// during the binary analysis but that are not tied to a given
4562 /// DWARF DIE.
4563 const vector<type_base_sptr>&
4564 types_to_canonicalize() const
4565 {return types_to_canonicalize_;}
4566
4567 /// Clear the containers holding types to canonicalize.
4568 void
4569 clear_types_to_canonicalize()
4570 {
4571 types_to_canonicalize_.clear();
4572 }
4573
4574 /// Types that were created but not tied to a particular DIE, must
4575 /// be scheduled for late canonicalization using this method.
4576 ///
4577 /// @param t the type to schedule for late canonicalization.
4578 void
4579 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4580 {
4581 types_to_canonicalize_.push_back(t);
4582 }
4583
4584 /// Canonicalize types which DIE offsets are stored in vectors on
4585 /// the side. This is a sub-routine of
4586 /// reader::perform_late_type_canonicalizing().
4587 ///
4588 /// @param source where the DIE of the types to canonicalize are
4589 /// from.
4590 void
4591 canonicalize_types_scheduled()
4592 {
4593 tools_utils::timer cn_timer;
4594 if (do_log())
4595 {
4596 cerr << "going to canonicalize types";
4597 corpus_sptr c = corpus();
4598 if (c)
4599 cerr << " of corpus " << corpus()->get_path();
4600 cn_timer.start();
4601 }
4602
4603 if (!types_to_canonicalize().empty())
4604 canonicalize_types(types_to_canonicalize().begin(),
4605 types_to_canonicalize().end(),
4606 [](const vector<type_base_sptr>::const_iterator& i)
4607 {return *i;});
4608
4609 if (do_log())
4610 {
4611 cn_timer.stop();
4612 cerr << "finished canonicalizing types";
4613 corpus_sptr c = corpus();
4614 if (c)
4615 cerr << " of corpus " << corpus()->get_path();
4616 cerr << ": (" << cn_timer << ")\n";
4617 }
4618 }
4619
4620 /// Compute the number of canonicalized and missed types in the late
4621 /// canonicalization phase.
4622 ///
4623 /// @param source where the DIEs of the canonicalized types are
4624 /// from.
4625 ///
4626 /// @param canonicalized the number of types that got canonicalized
4627 /// is added to the value already present in this parameter.
4628 ///
4629 /// @param missed the number of types scheduled for late
4630 /// canonicalization and which couldn't be canonicalized (for a
4631 /// reason) is added to the value already present in this parameter.
4632 void
4633 add_late_canonicalized_types_stats(size_t& canonicalized,
4634 size_t& missed) const
4635 {
4636 for (auto t : types_to_canonicalize())
4637 {
4638 if (t->get_canonical_type())
4639 ++canonicalized;
4640 else
4641 ++missed;
4642 }
4643 }
4644
4645 // Look at the types that need to be canonicalized after the
4646 // translation unit has been constructed and canonicalize them.
4647 void
4648 perform_late_type_canonicalizing()
4649 {
4650 canonicalize_types_scheduled();
4651
4652 if (show_stats())
4653 {
4654 size_t num_canonicalized = 0, num_missed = 0, total = 0;
4655 add_late_canonicalized_types_stats(num_canonicalized,
4656 num_missed);
4657 total = num_canonicalized + num_missed;
4658 cerr << "binary: "
4659 << elf_path()
4660 << "\n";
4661 cerr << " # late canonicalized types: "
4662 << num_canonicalized;
4663 if (total)
4664 cerr << " (" << num_canonicalized * 100 / total << "%)";
4665 cerr << "\n"
4666 << " # missed canonicalization opportunities: "
4667 << num_missed;
4668 if (total)
4669 cerr << " (" << num_missed * 100 / total << "%)";
4670 cerr << "\n";
4671 }
4672
4673 }
4674
4675 const die_tu_map_type&
4676 die_tu_map() const
4677 {return die_tu_map_;}
4678
4680 die_tu_map()
4681 {return die_tu_map_;}
4682
4683 /// Getter for the map that associates a translation unit DIE to the
4684 /// vector of imported unit points that it contains.
4685 ///
4686 /// @param source where the DIEs are from.
4687 ///
4688 /// @return the map.
4690 tu_die_imported_unit_points_map(die_source source) const
4691 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map(source);}
4692
4693 /// Getter for the map that associates a translation unit DIE to the
4694 /// vector of imported unit points that it contains.
4695 ///
4696 /// @param source where the DIEs are from.
4697 ///
4698 /// @return the map.
4700 tu_die_imported_unit_points_map(die_source source)
4701 {
4702 switch (source)
4703 {
4704 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4705 break;
4706 case ALT_DEBUG_INFO_DIE_SOURCE:
4707 return alt_tu_die_imported_unit_points_map_;
4708 case TYPE_UNIT_DIE_SOURCE:
4709 return type_units_tu_die_imported_unit_points_map_;
4710 case NO_DEBUG_INFO_DIE_SOURCE:
4711 case NUMBER_OF_DIE_SOURCES:
4712 // We cannot reach this point.
4714 }
4715 return tu_die_imported_unit_points_map_;
4716 }
4717
4718 /// Reset the current corpus being constructed.
4719 ///
4720 /// This actually deletes the current corpus being constructed.
4721 void
4722 reset_corpus()
4723 {corpus().reset();}
4724
4725 /// Get the map that associates each DIE to its parent DIE. This is
4726 /// for DIEs coming from the main debug info sections.
4727 ///
4728 /// @param source where the DIEs in the map come from.
4729 ///
4730 /// @return the DIE -> parent map.
4732 die_parent_map(die_source source) const
4733 {return const_cast<reader*>(this)->die_parent_map(source);}
4734
4735 /// Get the map that associates each DIE to its parent DIE. This is
4736 /// for DIEs coming from the main debug info sections.
4737 ///
4738 /// @param source where the DIEs in the map come from.
4739 ///
4740 /// @return the DIE -> parent map.
4742 die_parent_map(die_source source)
4743 {
4744 switch (source)
4745 {
4746 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
4747 break;
4748 case ALT_DEBUG_INFO_DIE_SOURCE:
4749 return alternate_die_parent_map_;
4750 case TYPE_UNIT_DIE_SOURCE:
4751 return type_section_die_parent_map();
4752 case NO_DEBUG_INFO_DIE_SOURCE:
4753 case NUMBER_OF_DIE_SOURCES:
4755 }
4756 return primary_die_parent_map_;
4757 }
4758
4760 type_section_die_parent_map() const
4761 {return type_section_die_parent_map_;}
4762
4764 type_section_die_parent_map()
4765 {return type_section_die_parent_map_;}
4766
4767 /// Getter of the current translation unit.
4768 ///
4769 /// @return the current translation unit being constructed.
4771 cur_transl_unit() const
4772 {return cur_tu_;}
4773
4774 /// Getter of the current translation unit.
4775 ///
4776 /// @return the current translation unit being constructed.
4778 cur_transl_unit()
4779 {return cur_tu_;}
4780
4781 /// Setter of the current translation unit.
4782 ///
4783 /// @param tu the current translation unit being constructed.
4784 void
4785 cur_transl_unit(translation_unit_sptr tu)
4786 {
4787 if (tu)
4788 cur_tu_ = tu;
4789 }
4790
4791 /// Return the global scope of the current translation unit.
4792 ///
4793 /// @return the global scope of the current translation unit.
4794 const scope_decl_sptr&
4795 global_scope() const
4796 {return cur_transl_unit()->get_global_scope();}
4797
4798 /// Return a scope that is nil.
4799 ///
4800 /// @return a scope that is nil.
4801 const scope_decl_sptr&
4802 nil_scope() const
4803 {return nil_scope_;}
4804
4805 const scope_stack_type&
4806 scope_stack() const
4807 {return scope_stack_;}
4808
4810 scope_stack()
4811 {return scope_stack_;}
4812
4813 scope_decl*
4814 current_scope()
4815 {
4816 if (scope_stack().empty())
4817 {
4818 if (cur_transl_unit())
4819 scope_stack().push(cur_transl_unit()->get_global_scope().get());
4820 }
4821 return scope_stack().top();
4822 }
4823
4824 list<var_decl_sptr>&
4825 var_decls_to_re_add_to_tree()
4826 {return var_decls_to_add_;}
4827
4828 /// Test if a DIE represents a decl (function or variable) that has
4829 /// a symbol that is exported, whatever that means. This is
4830 /// supposed to work for Linux Kernel binaries as well.
4831 ///
4832 /// This is useful to limit the amount of DIEs taken into account to
4833 /// the strict limit of what an ABI actually means. Limiting the
4834 /// volume of DIEs analyzed this way is an important optimization to
4835 /// keep big binaries "manageable" by libabigail.
4836 ///
4837 /// @param DIE the die to consider.
4838 bool
4839 is_decl_die_with_exported_symbol(const Dwarf_Die *die)
4840 {
4841 if (!die || !die_is_decl(die))
4842 return false;
4843
4844 bool result = false, address_found = false, symbol_is_exported = false;;
4845 Dwarf_Addr decl_symbol_address = 0;
4846
4847 if (die_is_variable_decl(die))
4848 {
4849 if ((address_found = get_variable_address(die, decl_symbol_address)))
4850 symbol_is_exported =
4851 !!variable_symbol_is_exported(decl_symbol_address);
4852 }
4853 else if (die_is_function_decl(die))
4854 {
4855 if ((address_found = get_function_address(die, decl_symbol_address)))
4856 symbol_is_exported =
4857 !!function_symbol_is_exported(decl_symbol_address);
4858 }
4859
4860 if (address_found)
4861 result = symbol_is_exported;
4862
4863 return result;
4864 }
4865
4866 /// This is a sub-routine of maybe_adjust_fn_sym_address and
4867 /// maybe_adjust_var_sym_address.
4868 ///
4869 /// Given an address that we got by looking at some debug
4870 /// information (e.g, a symbol's address referred to by a DWARF
4871 /// TAG), If the ELF file we are interested in is a shared library
4872 /// or an executable, then adjust the address to be coherent with
4873 /// where the executable (or shared library) is loaded. That way,
4874 /// the address can be used to look for symbols in the executable or
4875 /// shared library.
4876 ///
4877 /// @return the adjusted address, or the same address as @p addr if
4878 /// it didn't need any adjustment.
4879 Dwarf_Addr
4880 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4881 {
4882 if (addr == 0)
4883 return addr;
4884
4885 GElf_Ehdr eh_mem;
4886 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4887
4888 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4889 {
4890 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4891 ABG_ASSERT(get_binary_load_address(dwarf_elf_handle(),
4892 dwarf_elf_load_address));
4894 elf_load_address));
4895 if (dwarf_is_splitted()
4896 && (dwarf_elf_load_address != elf_load_address))
4897 // This means that in theory the DWARF and the executable are
4898 // not loaded at the same address. And addr is meaningful
4899 // only in the context of the DWARF.
4900 //
4901 // So let's transform addr into an offset relative to where
4902 // the DWARF is loaded, and let's add that relative offset
4903 // to the load address of the executable. That way, addr
4904 // becomes meaningful in the context of the executable and
4905 // can thus be used to compare against the address of
4906 // symbols of the executable, for instance.
4907 addr = addr - dwarf_elf_load_address + elf_load_address;
4908 }
4909
4910 return addr;
4911 }
4912
4913 /// For a relocatable (*.o) elf file, this function expects an
4914 /// absolute address, representing a function symbol. It then
4915 /// extracts the address of the .text section from the symbol
4916 /// absolute address to get the relative address of the function
4917 /// from the beginning of the .text section.
4918 ///
4919 /// For executable or shared library, this function expects an
4920 /// address of a function symbol that was retrieved by looking at a
4921 /// DWARF "file". The function thus adjusts the address to make it
4922 /// be meaningful in the context of the ELF file.
4923 ///
4924 /// In both cases, the address can then be compared against the
4925 /// st_value field of a function symbol from the ELF file.
4926 ///
4927 /// @param addr an adress for a function symbol that was retrieved
4928 /// from a DWARF file.
4929 ///
4930 /// @return the (possibly) adjusted address, or just @p addr if no
4931 /// adjustment took place.
4932 Dwarf_Addr
4933 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4934 {
4935 if (addr == 0)
4936 return addr;
4937
4938 Elf* elf = elf_handle();
4939 GElf_Ehdr eh_mem;
4940 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4941
4942 if (elf_header->e_type == ET_REL)
4943 // We are looking at a relocatable file. In this case, we don't
4944 // do anything because:
4945 //
4946 // 1/ the addresses from DWARF are absolute (relative to the
4947 // beginning of the relocatable file)
4948 //
4949 // 2/ The ELF symbol addresses that we store in our lookup
4950 // tables are translated from section-related to absolute as
4951 // well. So we don't have anything to do at this point for
4952 // ET_REL files.
4953 ;
4954 else
4955 addr = maybe_adjust_address_for_exec_or_dyn(addr);
4956
4957 return addr;
4958 }
4959
4960 /// For a relocatable (*.o) elf file, this function expects an
4961 /// absolute address, representing a global variable symbol. It
4962 /// then extracts the address of the {.data,.data1,.rodata,.bss}
4963 /// section from the symbol absolute address to get the relative
4964 /// address of the variable from the beginning of the data section.
4965 ///
4966 /// For executable or shared library, this function expects an
4967 /// address of a variable symbol that was retrieved by looking at a
4968 /// DWARF "file". The function thus adjusts the address to make it
4969 /// be meaningful in the context of the ELF file.
4970 ///
4971 /// In both cases, the address can then be compared against the
4972 /// st_value field of a function symbol from the ELF file.
4973 ///
4974 /// @param addr an address for a global variable symbol that was
4975 /// retrieved from a DWARF file.
4976 ///
4977 /// @return the (possibly) adjusted address, or just @p addr if no
4978 /// adjustment took place.
4979 Dwarf_Addr
4980 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
4981 {
4982 Elf* elf = elf_handle();
4983 GElf_Ehdr eh_mem;
4984 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4985
4986 if (elf_header->e_type == ET_REL)
4987 // We are looking at a relocatable file. In this case, we don't
4988 // do anything because:
4989 //
4990 // 1/ the addresses from DWARF are absolute (relative to the
4991 // beginning of the relocatable file)
4992 //
4993 // 2/ The ELF symbol addresses that we store in our lookup
4994 // tables are translated from section-related to absolute as
4995 // well. So we don't have anything to do at this point for
4996 // ET_REL files.
4997 ;
4998 else
4999 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5000
5001 return addr;
5002 }
5003
5004 /// Get the first exported function address in the set of addresses
5005 /// referred to by the DW_AT_ranges attribute of a given DIE.
5006 ///
5007 /// @param die the DIE we are considering.
5008 ///
5009 /// @param address output parameter. This is set to the first
5010 /// address found in the sequence pointed to by the DW_AT_ranges
5011 /// attribute found on the DIE @p die, iff the function returns
5012 /// true. Otherwise, no value is set into this output parameter.
5013 ///
5014 /// @return true iff the DIE @p die does have a DW_AT_ranges
5015 /// attribute and an address of an exported function was found in
5016 /// its sequence value.
5017 bool
5018 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5019 Dwarf_Addr& address) const
5020 {
5021 Dwarf_Addr base;
5022 Dwarf_Addr end_addr;
5023 ptrdiff_t offset = 0;
5024
5025 do
5026 {
5027 Dwarf_Addr addr = 0, fn_addr = 0;
5028 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5029 {
5030 fn_addr = maybe_adjust_fn_sym_address(addr);
5031 if (function_symbol_is_exported(fn_addr))
5032 {
5033 address = fn_addr;
5034 return true;
5035 }
5036 }
5037 } while (offset > 0);
5038 return false;
5039 }
5040
5041 /// Get the address of the function.
5042 ///
5043 /// The address of the function is considered to be the value of the
5044 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5045 /// only) to not point to an absolute address anymore, but rather to
5046 /// the address of the function inside the .text segment.
5047 ///
5048 /// @param function_die the die of the function to consider.
5049 ///
5050 /// @param address the resulting address iff the function returns
5051 /// true.
5052 ///
5053 /// @return true if the function address was found.
5054 bool
5055 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5056 {
5057 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5058 DW_AT_low_pc, address))
5059 // So no DW_AT_low_pc was found. Let's see if the function DIE
5060 // has got a DW_AT_ranges attribute instead. If it does, the
5061 // first address of the set of addresses represented by the
5062 // value of that DW_AT_ranges represents the function (symbol)
5063 // address we are looking for.
5064 if (!get_first_exported_fn_address_from_DW_AT_ranges
5065 (const_cast<Dwarf_Die*>(function_die),
5066 address))
5067 return false;
5068
5069 address = maybe_adjust_fn_sym_address(address);
5070 return true;
5071 }
5072
5073 /// Get the address of the global variable.
5074 ///
5075 /// The address of the global variable is considered to be the value
5076 /// of the DW_AT_location attribute, possibly adjusted (in
5077 /// relocatable files only) to not point to an absolute address
5078 /// anymore, but rather to the address of the global variable inside
5079 /// the data segment.
5080 ///
5081 /// @param variable_die the die of the function to consider.
5082 ///
5083 /// @param address the resulting address iff this function returns
5084 /// true.
5085 ///
5086 /// @return true if the variable address was found.
5087 bool
5088 get_variable_address(const Dwarf_Die* variable_die,
5089 Dwarf_Addr& address) const
5090 {
5091 bool is_tls_address = false;
5092 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5093 address, is_tls_address))
5094 return false;
5095 if (!is_tls_address)
5096 address = maybe_adjust_var_sym_address(address);
5097 return true;
5098 }
5099
5100 /// Getter of the exported decls builder object.
5101 ///
5102 /// @return the exported decls builder.
5103 corpus::exported_decls_builder*
5104 exported_decls_builder()
5105 {return corpus()->get_exported_decls_builder().get();}
5106
5107 /// Getter of the "load_all_types" flag. This flag tells if all the
5108 /// types (including those not reachable by public declarations) are
5109 /// to be read and represented in the final ABI corpus.
5110 ///
5111 /// @return the load_all_types flag.
5112 bool
5113 load_all_types() const
5114 {return options().load_all_types;}
5115
5116 /// Setter of the "load_all_types" flag. This flag tells if all the
5117 /// types (including those not reachable by public declarations) are
5118 /// to be read and represented in the final ABI corpus.
5119 ///
5120 /// @param f the new load_all_types flag.
5121 void
5122 load_all_types(bool f)
5123 {options().load_all_types = f;}
5124
5125 bool
5126 load_in_linux_kernel_mode() const
5127 {return options().load_in_linux_kernel_mode;}
5128
5129 void
5130 load_in_linux_kernel_mode(bool f)
5131 {options().load_in_linux_kernel_mode = f;}
5132
5133 /// Test if it's allowed to assume that the DWARF debug info has
5134 /// been factorized (for instance, with the DWZ tool) so that if two
5135 /// type DIEs originating from the .gnu_debugaltlink section have
5136 /// different offsets, they represent different types.
5137 ///
5138 /// @return true iff we can assume that the DWARF debug info has
5139 /// been factorized.
5140 bool
5141 leverage_dwarf_factorization() const
5142 {
5143 if (!leverage_dwarf_factorization_.has_value())
5144 {
5145 if (options().leverage_dwarf_factorization
5146 && elf_helpers::find_section_by_name(elf_handle(),
5147 ".gnu_debugaltlink"))
5148 leverage_dwarf_factorization_ = true;
5149 else
5150 leverage_dwarf_factorization_ = false;
5151 }
5152 ABG_ASSERT(leverage_dwarf_factorization_.has_value());
5153
5154 return *leverage_dwarf_factorization_;
5155 }
5156 /// Getter of the "show_stats" flag.
5157 ///
5158 /// This flag tells if we should emit statistics about various
5159 /// internal stuff.
5160 ///
5161 /// @return the value of the flag.
5162 bool
5163 show_stats() const
5164 {return options().show_stats;}
5165
5166 /// Setter of the "show_stats" flag.
5167 ///
5168 /// This flag tells if we should emit statistics about various
5169 /// internal stuff.
5170 ///
5171 /// @param f the value of the flag.
5172 void
5173 show_stats(bool f)
5174 {options().show_stats = f;}
5175
5176 /// Getter of the "do_log" flag.
5177 ///
5178 /// This flag tells if we should log about various internal
5179 /// details.
5180 ///
5181 /// return the "do_log" flag.
5182 bool
5183 do_log() const
5184 {return options().do_log;}
5185
5186 /// Setter of the "do_log" flag.
5187 ///
5188 /// This flag tells if we should log about various internal details.
5189 ///
5190 /// @param f the new value of the flag.
5191 void
5192 do_log(bool f)
5193 {options().do_log = f;}
5194
5195 /// Walk the DIEs under a given die and for each child, populate the
5196 /// die -> parent map to record the child -> parent relationship
5197 /// that
5198 /// exists between the child and the given die.
5199 ///
5200 /// The function also builds the vector of places where units are
5201 /// imported.
5202 ///
5203 /// This is done recursively as for each child DIE, this function
5204 /// walks its children as well.
5205 ///
5206 /// @param die the DIE whose children to walk recursively.
5207 ///
5208 /// @param source where the DIE @p die comes from.
5209 ///
5210 /// @param imported_units a vector containing all the offsets of the
5211 /// points where unit have been imported, under @p die.
5212 void
5213 build_die_parent_relations_under(Dwarf_Die* die,
5214 die_source source,
5215 imported_unit_points_type & imported_units)
5216 {
5217 if (!die)
5218 return;
5219
5220 offset_offset_map_type& parent_of = die_parent_map(source);
5221
5222 Dwarf_Die child;
5223 if (dwarf_child(die, &child) != 0)
5224 return;
5225
5226 do
5227 {
5228 parent_of[dwarf_dieoffset(&child)] = dwarf_dieoffset(die);
5229 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5230 {
5231 Dwarf_Die imported_unit;
5232 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5233 // If the imported_unit has a sub-tree, let's record
5234 // this point at which the sub-tree is imported into
5235 // the current debug info.
5236 //
5237 // Otherwise, if the imported_unit has no sub-tree,
5238 // there is no point in recording where a non-existent
5239 // sub-tree is being imported.
5240 //
5241 // Note that the imported_unit_points_type type below
5242 // expects the imported_unit to have a sub-tree.
5243 && die_has_children(&imported_unit))
5244 {
5245 die_source imported_unit_die_source = NO_DEBUG_INFO_DIE_SOURCE;
5246 ABG_ASSERT(get_die_source(imported_unit, imported_unit_die_source));
5247 imported_units.push_back
5248 (imported_unit_point(dwarf_dieoffset(&child),
5249 imported_unit,
5250 imported_unit_die_source));
5251 }
5252 }
5253 build_die_parent_relations_under(&child, source, imported_units);
5254 }
5255 while (dwarf_siblingof(&child, &child) == 0);
5256
5257 }
5258
5259 /// Determine if we do have to build a DIE -> parent map, depending
5260 /// on a given language.
5261 ///
5262 /// Some languages like C++, Ada etc, do have the concept of
5263 /// namespace and yet, the DIE data structure doesn't provide us
5264 /// with a way to get the parent namespace of a given DIE. So for
5265 /// those languages, we need to build a DIE -> parent map so that we
5266 /// can get the namespace DIE (or more generally the scope DIE) of a given
5267 /// DIE as we need it.
5268 ///
5269 /// But then some more basic languages like C or assembly don't have
5270 /// that need.
5271 ///
5272 /// This function, depending on the language, tells us if we need to
5273 /// build the DIE -> parent map or not.
5274 ///
5275 /// @param lang the language to consider.
5276 ///
5277 /// @return true iff we need to build the DIE -> parent map for this
5278 /// language.
5279 bool
5280 do_we_build_die_parent_maps(translation_unit::language lang)
5281 {
5282 if (is_c_language(lang))
5283 return false;
5284
5285 switch (lang)
5286 {
5287 case translation_unit::LANG_UNKNOWN:
5288#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5289 case translation_unit::LANG_Mips_Assembler:
5290#endif
5291 return false;
5292 default:
5293 break;
5294 }
5295 return true;
5296 }
5297
5298 /// Walk all the DIEs accessible in the debug info (and in the
5299 /// alternate debug info as well) and build maps representing the
5300 /// relationship DIE -> parent. That is, make it so that we can get
5301 /// the parent for a given DIE.
5302 ///
5303 /// Note that the goal of this map is to be able to get the parent
5304 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5305 /// when we get a DIE of a type, and we want to build an internal
5306 /// representation for it, we need to get its fully qualified name.
5307 /// For that, we need to know what is the parent DIE of that type
5308 /// DIE, so that we can know what the namespace of that type is.
5309 ///
5310 /// Note that as the C language doesn't have namespaces (all types
5311 /// are defined in the same global namespace), this function doesn't
5312 /// build the DIE -> parent map if the current translation unit
5313 /// comes from C. This saves time on big C ELF files with a lot of
5314 /// DIEs.
5315 void
5316 build_die_parent_maps()
5317 {
5318 bool we_do_have_to_build_die_parent_map = false;
5319 uint8_t address_size = 0;
5320 size_t header_size = 0;
5321 // Get the DIE of the current translation unit, look at it to get
5322 // its language. If that language is in C, then all types are in
5323 // the global namespace so we don't need to build the DIE ->
5324 // parent map. So we dont build it in that case.
5325 for (Dwarf_Off offset = 0, next_offset = 0;
5326 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5327 offset, &next_offset, &header_size,
5328 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5329 offset = next_offset)
5330 {
5331 Dwarf_Off die_offset = offset + header_size;
5332 Dwarf_Die cu;
5333 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5334 die_offset, &cu))
5335 continue;
5336
5337 uint64_t l = 0;
5338 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5339 translation_unit::language lang = dwarf_language_to_tu_language(l);
5340 if (do_we_build_die_parent_maps(lang))
5341 we_do_have_to_build_die_parent_map = true;
5342 }
5343
5344 if (!we_do_have_to_build_die_parent_map)
5345 return;
5346
5347 // Build the DIE -> parent relation for DIEs coming from the
5348 // .debug_info section in the alternate debug info file.
5349 die_source source = ALT_DEBUG_INFO_DIE_SOURCE;
5350 for (Dwarf_Off offset = 0, next_offset = 0;
5351 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5352 offset, &next_offset, &header_size,
5353 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5354 offset = next_offset)
5355 {
5356 Dwarf_Off die_offset = offset + header_size;
5357 Dwarf_Die cu;
5358 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5359 die_offset, &cu))
5360 continue;
5361 cur_tu_die(&cu);
5362
5363 imported_unit_points_type& imported_units =
5364 tu_die_imported_unit_points_map(source)[die_offset] =
5366 build_die_parent_relations_under(&cu, source, imported_units);
5367 }
5368
5369 // Build the DIE -> parent relation for DIEs coming from the
5370 // .debug_info section of the main debug info file.
5371 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
5372 address_size = 0;
5373 header_size = 0;
5374 for (Dwarf_Off offset = 0, next_offset = 0;
5375 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5376 offset, &next_offset, &header_size,
5377 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5378 offset = next_offset)
5379 {
5380 Dwarf_Off die_offset = offset + header_size;
5381 Dwarf_Die cu;
5382 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5383 die_offset, &cu))
5384 continue;
5385 cur_tu_die(&cu);
5386 imported_unit_points_type& imported_units =
5387 tu_die_imported_unit_points_map(source)[die_offset] =
5389 build_die_parent_relations_under(&cu, source, imported_units);
5390 }
5391
5392 // Build the DIE -> parent relation for DIEs coming from the
5393 // .debug_types section.
5394 source = TYPE_UNIT_DIE_SOURCE;
5395 address_size = 0;
5396 header_size = 0;
5397 uint64_t type_signature = 0;
5398 Dwarf_Off type_offset;
5399 for (Dwarf_Off offset = 0, next_offset = 0;
5400 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5401 offset, &next_offset, &header_size,
5402 NULL, NULL, &address_size, NULL,
5403 &type_signature, &type_offset) == 0);
5404 offset = next_offset)
5405 {
5406 Dwarf_Off die_offset = offset + header_size;
5407 Dwarf_Die cu;
5408
5409 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5410 die_offset, &cu))
5411 continue;
5412 cur_tu_die(&cu);
5413 imported_unit_points_type& imported_units =
5414 tu_die_imported_unit_points_map(source)[die_offset] =
5416 build_die_parent_relations_under(&cu, source, imported_units);
5417 }
5418 }
5419};// end class reader.
5420
5421/// The type of the aggregates being compared during a DIE comparison.
5422///
5423/// This encapsulates the stack of aggregates being compared at any
5424/// single point.
5425///
5426/// This is useful to detect "comparison cycles" and thus avoid the
5427/// resulting infinite loops.
5428///
5429/// This is also useful for implementing a very important optimization
5430/// that takes place during the canonicalization
5431struct offset_pairs_stack_type
5432{
5433 // The DWARF DWARF reader that is useful for so many things.
5434 const reader& rdr_;
5435 // The set of types that are being compared. This is to speed up
5436 // searches.
5438 // The stack of types that are being compared. The top of the
5439 // stack is the back of the vector.
5441 // A map that associates a redundant type pair to the vector of
5442 // types that depends on it.
5443 offset_pair_vect_map_type redundant_types_;
5444 // A map that associates a dependant type to the vector of redundant
5445 // types it depends on.
5446 offset_pair_vect_map_type dependant_types_;
5447
5448 offset_pairs_stack_type(const reader& rdr)
5449 : rdr_ (rdr)
5450 {}
5451
5452 /// Add a pair of types being compared to the stack of aggregates
5453 /// being compared.
5454 ///
5455 /// @param p the pair of offsets of the type DIEs to consider.
5456 void
5457 add(const offset_pair_type& p)
5458 {
5459 set_.insert(p);
5460 vect_.push_back(p);
5461 }
5462
5463 /// Erase a pair of types being compared from the stack of
5464 /// aggregates being compared.
5465 ///
5466 /// @param p the pair of offsets of the type DIEs to consider.
5467 ///
5468 /// @return true iff @p was found and erased from the stack.
5469 bool
5470 erase(const offset_pair_type& p)
5471 {
5472 if (set_.erase(p))
5473 {
5474 offset_pair_vector_type::iterator i;
5475
5476 for (i = vect_.begin();i < vect_.end(); ++i)
5477 if (*i == p)
5478 break;
5479
5480 if (i != vect_.end())
5481 vect_.erase(i);
5482
5483 return true;
5484 }
5485
5486 return false;
5487 }
5488
5489 /// Test if a pair of type DIEs is part of the stack of type DIEs
5490 /// being compared.
5491 ///
5492 /// @param p the pair of offsets of the type DIEs to consider.
5493 ///
5494 /// @return true iff @p was found in the stack of types being
5495 /// compared.
5496 bool
5497 contains(const offset_pair_type &p) const
5498 {
5499 if (set_.find(p) == set_.end())
5500 return false;
5501 return true;
5502 }
5503
5504 /// Get the set of comparison pair that depends on a given
5505 /// comparison pair.
5506 ///
5507 /// A comparison pair T{t1,t2} depends on a comparison pair P{p1,p2}
5508 /// if p1 is a subtype of t1 and p2 is a subtype of t2. In other
5509 /// words, the pair T appears in the comparison stack BEFORE the
5510 /// pair P.
5511 ///
5512 /// So, this function returns the vector of comparison pairs that
5513 /// appear in the comparison stack AFTER a given comparison pair.
5514 ///
5515 /// @param p the comparison pair to consider.
5516 ///
5517 /// @param pairs out parameter. This is filled with the comparison
5518 /// pairs that depend on @p, iff the function returns true.
5519 ///
5520 /// @return true iff comparison pairs depending on @p have been
5521 /// found and collected in @pairs.
5522 bool
5523 get_pairs_that_depend_on(const offset_pair_type& p,
5524 offset_pair_vector_type& pairs) const
5525 {
5526 bool result = false;
5527 if (!contains(p))
5528 return result;
5529
5530 // First, get an iterator on the position of 'p'.
5531 offset_pair_vector_type::const_iterator i;
5532 for (i = vect_.begin(); i != vect_.end(); ++i)
5533 if (*i == p)
5534 break;
5535
5536 if (i == vect_.end())
5537 return result;
5538
5539 // Then, harvest all the comparison pairs that come after the
5540 // position of 'p'.
5541 for (++i; i != vect_.end(); ++i)
5542 {
5543 pairs.push_back(*i);
5544 result = true;
5545 }
5546
5547 return result;
5548 }
5549
5550 /// Record the fact that a set of comparison pairs depends on a
5551 /// given comparison pair.
5552 ///
5553 /// Set a map that associates each dependant comparison pair to the
5554 /// pair it depends on.
5555 ///
5556 /// @param p the comparison pair that the set depends on.
5557 ///
5558 /// @param dependant_types the set of types that depends on @p.
5559 void
5560 record_dependant_types(const offset_pair_type& p,
5561 const offset_pair_vector_type& dependant_types)
5562 {
5563 for (auto type_pair : dependant_types)
5564 dependant_types_[type_pair].push_back(p);
5565 }
5566
5567 /// Record a comparison pair as being redundant.
5568 ///
5569 ///
5570 /// @param p the comparison pair to record as redundant.
5571 void
5572 record_redundant_type_die_pair(const offset_pair_type& p)
5573 {
5574 offset_pair_vector_type dependant_types;
5575 get_pairs_that_depend_on(p, dependant_types);
5576
5577 // First, record the relationship "p -> [pairs that depend on p]".
5578 auto it = redundant_types_.find(p);
5579 if (it == redundant_types_.end())
5580 {
5581 auto entry = std::make_pair(p, dependant_types);
5582 redundant_types_.insert(entry);
5583 }
5584 else
5585 it->second.insert(it->second.end(),
5586 dependant_types.begin(),
5587 dependant_types.end());
5588
5589 // For each dependant type pair, record the association:
5590 // dependant_pair --> [vect of redundant types]
5591 record_dependant_types(p, dependant_types);
5592 }
5593
5594 /// Test if a given pair has been detected as redundant.
5595 ///
5596 /// @param p the pair of DIEs to consider.
5597 ///
5598 /// @return iff @p is redundant.
5599 bool
5600 is_redundant(const offset_pair_type& p)
5601 {
5602 auto i = redundant_types_.find(p);
5603 if (i != redundant_types_.end())
5604 return true;
5605 return false;
5606 }
5607
5608 /// Test if a given pair is dependant on at least a redundant type.
5609 ///
5610 /// @param p the pair to consider.
5611 ///
5612 /// @return true iff @p depends on a redundant type.
5613 bool
5614 depends_on_redundant_types(const offset_pair_type& p)
5615 {
5616 auto i = dependant_types_.find(p);
5617 if (i == dependant_types_.end())
5618 return false;
5619 return true;
5620 }
5621
5622 /// Remove a redundant pair from the system.
5623 ///
5624 /// This needs updating the system to also remove the dependant
5625 /// types that depend on the redundant pair (if they depend only on
5626 /// that redundant pair).
5627 ///
5628 /// @param p the pair to consider.
5629 ///
5630 /// @param erase_canonical_die_offset if true then erase the cached
5631 /// comparison results for the redundant pair and its dependant
5632 /// types.
5633 void
5634 erase_redundant_type_pair_entry(const offset_pair_type& p,
5635 bool erase_cached_results = false)
5636 {
5637 // First, update the dependant types that depend on the redundant
5638 // type pair
5639 auto redundant_type = redundant_types_.find(p);
5640 if (redundant_type != redundant_types_.end())
5641 {
5642 for (auto dependant_type : redundant_type->second)
5643 {
5644 // Each dependant_type depends on the redundant type 'p',
5645 // among others.
5646 auto dependant_types_it = dependant_types_.find(dependant_type);
5647 ABG_ASSERT(dependant_types_it != dependant_types_.end());
5648 // Erase the redundant type 'p' from the redundant types
5649 // that dependant_type depends on.
5650 {
5651 auto i = dependant_types_it->second.begin();
5652 for (; i!= dependant_types_it->second.end();++i)
5653 if (*i == p)
5654 break;
5655 if (i != dependant_types_it->second.end())
5656 dependant_types_it->second.erase(i);
5657 }
5658 // If the dependant type itself doesn't depend on ANY
5659 // redundant type anymore, then remove the depend type
5660 // from the map of the dependant types.
5661 if (dependant_types_it->second.empty())
5662 {
5663 if (erase_cached_results)
5664 rdr_.die_comparison_results_.erase(dependant_type);
5665 dependant_types_.erase(dependant_types_it);
5666 }
5667 }
5668 }
5669 if (erase_cached_results)
5670 rdr_.die_comparison_results_.erase(p);
5671 redundant_types_.erase(p);
5672 }
5673
5674 /// If a comparison pair has been detected as redundant, stop
5675 /// tracking it as well as its dependant pairs. That will
5676 /// essentially make it impossible to reset/cancel the canonical
5677 /// propagated types for those depdant pairs, but will also save
5678 /// ressources.
5679 ///
5680 /// @param p the comparison pair to consider.
5681 void
5682 confirm_canonical_propagated_type(const offset_pair_type& p)
5683 {erase_redundant_type_pair_entry(p, /*erase_cached_results=*/true);}
5684
5685 /// Walk the types that depend on a comparison pair and cancel their
5686 /// canonical-propagate-type, that means remove their canonical
5687 /// types and mark them as not being canonically-propagated. Also,
5688 /// erase their cached comparison results that was likely set to
5689 /// COMPARISON_RESULT_UNKNOWN.
5690 ///
5691 /// @param p the pair to consider.
5692 void
5693 cancel_canonical_propagated_type(const offset_pair_type& p)
5694 {
5695 offset_pair_set_type dependant_types;
5696 get_dependant_types(p, dependant_types, /*transitive_closure=*/true);
5697 for (auto dependant_type : dependant_types)
5698 {
5699 // If this dependant type was canonical-type-propagated then
5700 // erase that canonical type.
5701 if (rdr_.propagated_types_.find(dependant_type)
5702 != rdr_.propagated_types_.end())
5703 {
5704 rdr_.erase_canonical_die_offset(dependant_type.first.offset_,
5705 dependant_type.first.source_,
5706 /*die_as_type=*/true);
5707 rdr_.propagated_types_.erase(dependant_type);
5708 rdr_.cancelled_propagation_count_++;
5709 }
5710 // Update the cached result. We know the comparison result
5711 // must now be different.
5712 auto comp_result_it = rdr_.die_comparison_results_.find(dependant_type);
5713 if (comp_result_it != rdr_.die_comparison_results_.end())
5714 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5715 }
5716
5717 // Update the cached result of the root type to cancel too.
5718 auto comp_result_it = rdr_.die_comparison_results_.find(p);
5719 if (comp_result_it != rdr_.die_comparison_results_.end())
5720 {
5721 // At this point, the result of p is either
5722 // COMPARISON_RESULT_UNKNOWN (if we cache comparison
5723 // results of that kind) or COMPARISON_RESULT_DIFFERENT.
5724 // Make sure it's the cached result is now
5725 // COMPARISON_RESULT_DIFFERENT.
5726 if (comp_result_it->second == COMPARISON_RESULT_UNKNOWN)
5727 comp_result_it->second= COMPARISON_RESULT_DIFFERENT;
5728 ABG_ASSERT(comp_result_it->second == COMPARISON_RESULT_DIFFERENT);
5729 }
5730
5731 if (rdr_.propagated_types_.find(p) != rdr_.propagated_types_.end())
5732 {
5733 rdr_.erase_canonical_die_offset(p.first.offset_,
5734 p.first.source_,
5735 /*die_as_type=*/true);
5736 rdr_.propagated_types_.erase(p);
5737 rdr_.cancelled_propagation_count_++;
5738 }
5739 }
5740
5741 /// Get the set of comparison pairs that depend on a given pair.
5742 ///
5743 /// @param p the pair to consider.
5744 ///
5745 /// @param result this is set to the pairs that depend on @p, iff
5746 /// the function returned true.
5747 ///
5748 /// @param transitive_closure if set to true, the transitive closure
5749 /// of the @result is set to it.
5750 ///
5751 /// @return true iff @result could be filled with the dependant
5752 /// types.
5753 bool
5754 get_dependant_types(const offset_pair_type& p,
5755 offset_pair_set_type& result,
5756 bool transitive_closure = false)
5757 {
5758 auto i = redundant_types_.find(p);
5759 if (i != redundant_types_.end())
5760 {
5761 for (auto dependant_type : i->second)
5762 if (result.find(dependant_type) == result.end())
5763 {
5764 result.insert(dependant_type);
5765 if (transitive_closure)
5766 get_dependant_types(p, result, /*transitive_closure=*/true);
5767 }
5768 return true;
5769 }
5770 return false;
5771 }
5772}; // end struct offset_pairs_stack_type
5773
5775build_ir_node_from_die(reader& rdr,
5776 Dwarf_Die* die,
5777 scope_decl* scope,
5778 bool called_from_public_decl,
5779 size_t where_offset,
5780 bool is_declaration_only = true,
5781 bool is_required_decl_spec = false);
5782
5784build_ir_node_from_die(reader& rdr,
5785 Dwarf_Die* die,
5786 bool called_from_public_decl,
5787 size_t where_offset);
5788
5789static decl_base_sptr
5790build_ir_node_for_void_type(reader& rdr);
5791
5793build_ir_node_for_void_pointer_type(reader& rdr);
5794
5795static class_decl_sptr
5796add_or_update_class_type(reader& rdr,
5797 Dwarf_Die* die,
5798 scope_decl* scope,
5799 bool is_struct,
5800 class_decl_sptr klass,
5801 bool called_from_public_decl,
5802 size_t where_offset,
5803 bool is_declaration_only);
5804
5805static union_decl_sptr
5806add_or_update_union_type(reader& rdr,
5807 Dwarf_Die* die,
5808 scope_decl* scope,
5809 union_decl_sptr union_type,
5810 bool called_from_public_decl,
5811 size_t where_offset,
5812 bool is_declaration_only);
5813
5814static decl_base_sptr
5815build_ir_node_for_void_type(reader& rdr);
5816
5817static decl_base_sptr
5818build_ir_node_for_variadic_parameter_type(reader &rdr);
5819
5820static function_decl_sptr
5821build_function_decl(reader& rdr,
5822 Dwarf_Die* die,
5823 size_t where_offset,
5825
5826static bool
5827function_is_suppressed(const reader& rdr,
5828 const scope_decl* scope,
5829 Dwarf_Die *function_die,
5830 bool is_declaration_only);
5831
5832static function_decl_sptr
5833build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5834 scope_decl *scope,
5835 Dwarf_Die *die,
5836 size_t where_offset,
5837 bool is_declaration_only,
5839
5840static var_decl_sptr
5841build_var_decl(reader& rdr,
5842 Dwarf_Die *die,
5843 size_t where_offset,
5844 var_decl_sptr result = var_decl_sptr());
5845
5846static var_decl_sptr
5847build_or_get_var_decl_if_not_suppressed(reader& rdr,
5848 scope_decl *scope,
5849 Dwarf_Die *die,
5850 size_t where_offset,
5852 bool is_required_decl_spec = false);
5853static bool
5854variable_is_suppressed(const reader& rdr,
5855 const scope_decl* scope,
5856 Dwarf_Die *variable_die,
5857 bool is_required_decl_spec = false);
5858
5859static void
5860finish_member_function_reading(Dwarf_Die* die,
5861 const function_decl_sptr& f,
5862 const class_or_union_sptr klass,
5863 reader& rdr);
5864
5865/// Test if a given DIE is anonymous
5866///
5867/// @param die the DIE to consider.
5868///
5869/// @return true iff @p die is anonymous.
5870static bool
5871die_is_anonymous(const Dwarf_Die* die)
5872{
5873 Dwarf_Attribute attr;
5874 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5875 return true;
5876 return false;
5877}
5878
5879/// Test if a DIE is an anonymous data member, aka, "unnamed field".
5880///
5881/// Unnamed fields are specified at
5882/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
5883///
5884/// @param die the DIE to consider.
5885///
5886/// @return true iff @p die is an anonymous data member.
5887static bool
5888die_is_anonymous_data_member(const Dwarf_Die* die)
5889{
5890 if (!die
5891 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
5892 || !die_name(die).empty())
5893 return false;
5894
5895 Dwarf_Die type_die;
5896 if (!die_die_attribute(die, DW_AT_type, type_die))
5897 return false;
5898
5899 if (dwarf_tag(&type_die) != DW_TAG_structure_type
5900 && dwarf_tag(&type_die) != DW_TAG_union_type)
5901 return false;
5902
5903 return true;
5904}
5905
5906/// Get the value of an attribute that is supposed to be a string, or
5907/// an empty string if the attribute could not be found.
5908///
5909/// @param die the DIE to get the attribute value from.
5910///
5911/// @param attr_name the attribute name. Must come from dwarf.h and
5912/// be an enumerator representing an attribute like, e.g, DW_AT_name.
5913///
5914/// @return the string representing the value of the attribute, or an
5915/// empty string if no string attribute could be found.
5916static string
5917die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5918{
5919 if (!die)
5920 return "";
5921
5922 Dwarf_Attribute attr;
5923 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5924 return "";
5925
5926 const char* str = dwarf_formstring(&attr);
5927 return str ? str : "";
5928}
5929
5930/// Get the value of an attribute that is supposed to be a string, or
5931/// an empty string if the attribute could not be found.
5932///
5933/// @param die the DIE to get the attribute value from.
5934///
5935/// @param attr_name the attribute name. Must come from dwarf.h and
5936/// be an enumerator representing an attribute like, e.g, DW_AT_name.
5937///
5938/// @return the char* representing the value of the attribute, or an
5939/// empty string if no string attribute could be found.
5940static const char*
5941die_char_str_attribute(const Dwarf_Die* die, unsigned attr_name)
5942{
5943 if (!die)
5944 return nullptr;
5945
5946 Dwarf_Attribute attr;
5947 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5948 return nullptr;
5949
5950 const char* str = dwarf_formstring(&attr);
5951 return str;
5952}
5953
5954/// Get the value of an attribute that is supposed to be an unsigned
5955/// constant.
5956///
5957/// @param die the DIE to read the information from.
5958///
5959/// @param attr_name the DW_AT_* name of the attribute. Must come
5960/// from dwarf.h and be an enumerator representing an attribute like,
5961/// e.g, DW_AT_decl_line.
5962///
5963///@param cst the output parameter that is set to the value of the
5964/// attribute @p attr_name. This parameter is set iff the function
5965/// return true.
5966///
5967/// @return true if there was an attribute of the name @p attr_name
5968/// and with a value that is a constant, false otherwise.
5969static bool
5970die_unsigned_constant_attribute(const Dwarf_Die* die,
5971 unsigned attr_name,
5972 uint64_t& cst)
5973{
5974 if (!die)
5975 return false;
5976
5977 Dwarf_Attribute attr;
5978 Dwarf_Word result = 0;
5979 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5980 || dwarf_formudata(&attr, &result))
5981 return false;
5982
5983 cst = result;
5984 return true;
5985}
5986
5987/// Read a signed constant value from a given attribute.
5988///
5989/// The signed constant expected must be of constant form.
5990///
5991/// @param die the DIE to get the attribute from.
5992///
5993/// @param attr_name the attribute name.
5994///
5995/// @param cst the resulting signed constant read.
5996///
5997/// @return true iff a signed constant attribute of the name @p
5998/// attr_name was found on the DIE @p die.
5999static bool
6000die_signed_constant_attribute(const Dwarf_Die *die,
6001 unsigned attr_name,
6002 int64_t& cst)
6003{
6004 if (!die)
6005 return false;
6006
6007 Dwarf_Attribute attr;
6008 Dwarf_Sword result = 0;
6009 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6010 || dwarf_formsdata(&attr, &result))
6011 return false;
6012
6013 cst = result;
6014 return true;
6015}
6016
6017/// Read the value of a constant attribute that is either signed or
6018/// unsigned into a array_type_def::subrange_type::bound_value value.
6019///
6020/// The bound_value instance will capture the actual signedness of the
6021/// read attribute.
6022///
6023/// @param die the DIE from which to read the value of the attribute.
6024///
6025/// @param attr_name the attribute name to consider.
6026///
6027/// @param is_signed true if the attribute value has to read as
6028/// signed.
6029///
6030/// @param value the resulting value read from attribute @p attr_name
6031/// on DIE @p die.
6032///
6033/// @return true iff DIE @p die has an attribute named @p attr_name
6034/// with a constant value.
6035static bool
6036die_constant_attribute(const Dwarf_Die *die,
6037 unsigned attr_name,
6038 bool is_signed,
6039 array_type_def::subrange_type::bound_value &value)
6040{
6041 if (!is_signed)
6042 {
6043 uint64_t l = 0;
6044 if (!die_unsigned_constant_attribute(die, attr_name, l))
6045 return false;
6046 value.set_unsigned(l);
6047 }
6048 else
6049 {
6050 int64_t l = 0;
6051 if (!die_signed_constant_attribute(die, attr_name, l))
6052 return false;
6053 value.set_signed(l);
6054 }
6055 return true;
6056}
6057
6058/// Test if a given DWARF form is DW_FORM_strx{1,4}.
6059///
6060/// Unfortunaly, the DW_FORM_strx{1,4} are enumerators of an untagged
6061/// enum in dwarf.h so we have to use an unsigned int for the form,
6062/// grrr.
6063///
6064/// @param form the form to consider.
6065///
6066/// @return true iff @p form is DW_FORM_strx{1,4}.
6067static bool
6068form_is_DW_FORM_strx(unsigned form)
6069{
6070 if (form)
6071 {
6072#if defined HAVE_DW_FORM_strx1 \
6073 && defined HAVE_DW_FORM_strx2 \
6074 && defined HAVE_DW_FORM_strx3 \
6075 && defined HAVE_DW_FORM_strx4
6076 if (form == DW_FORM_strx1
6077 || form == DW_FORM_strx2
6078 || form == DW_FORM_strx3
6079 ||form == DW_FORM_strx4)
6080 return true;
6081#endif
6082 }
6083 return false;
6084}
6085
6086/// Test if a given DWARF form is DW_FORM_line_strp.
6087///
6088/// Unfortunaly, the DW_FORM_line_strp is an enumerator of an untagged
6089/// enum in dwarf.h so we have to use an unsigned int for the form,
6090/// grrr.
6091///
6092/// @param form the form to consider.
6093///
6094/// @return true iff @p form is DW_FORM_line_strp.
6095static bool
6096form_is_DW_FORM_line_strp(unsigned form)
6097{
6098 if (form)
6099 {
6100#if defined HAVE_DW_FORM_line_strp
6101 if (form == DW_FORM_line_strp)
6102 return true;
6103#endif
6104 }
6105 return false;
6106}
6107
6108/// Get the value of a DIE attribute; that value is meant to be a
6109/// flag.
6110///
6111/// @param die the DIE to get the attribute from.
6112///
6113/// @param attr_name the DW_AT_* name of the attribute. Must come
6114/// from dwarf.h and be an enumerator representing an attribute like,
6115/// e.g, DW_AT_external.
6116///
6117/// @param flag the output parameter to store the flag value into.
6118/// This is set iff the function returns true.
6119///
6120/// @param recursively if true, the function looks through the
6121/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6122/// all the way down to the initial DIE that is cloned and look on
6123/// that DIE to see if it has the @p attr_name attribute.
6124///
6125/// @return true if the DIE has a flag attribute named @p attr_name,
6126/// false otherwise.
6127static bool
6128die_flag_attribute(const Dwarf_Die* die,
6129 unsigned attr_name,
6130 bool& flag,
6131 bool recursively = true)
6132{
6133 Dwarf_Attribute attr;
6134 if (recursively
6135 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6136 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6137 return false;
6138
6139 bool f = false;
6140 if (dwarf_formflag(&attr, &f))
6141 return false;
6142
6143 flag = f;
6144 return true;
6145}
6146
6147/// Get the mangled name from a given DIE.
6148///
6149/// @param die the DIE to read the mangled name from.
6150///
6151/// @return the mangled name if it's present in the DIE, or just an
6152/// empty string if it's not.
6153static string
6154die_linkage_name(const Dwarf_Die* die)
6155{
6156 if (!die)
6157 return "";
6158
6159 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6160 if (linkage_name.empty())
6161 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6162 return linkage_name;
6163}
6164
6165/// Get the file path that is the value of the DW_AT_decl_file
6166/// attribute on a given DIE, if the DIE is a decl DIE having that
6167/// attribute.
6168///
6169/// @param die the DIE to consider.
6170///
6171/// @return a string containing the file path that is the logical
6172/// value of the DW_AT_decl_file attribute. If the DIE @p die
6173/// doesn't have a DW_AT_decl_file attribute, then the return value is
6174/// just an empty string.
6175static string
6176die_decl_file_attribute(const Dwarf_Die* die)
6177{
6178 if (!die)
6179 return "";
6180
6181 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6182
6183 return str ? str : "";
6184}
6185
6186/// Get the value of an attribute which value is supposed to be a
6187/// reference to a DIE.
6188///
6189/// @param die the DIE to read the value from.
6190///
6191/// @param attr_name the DW_AT_* attribute name to read.
6192///
6193/// @param result the DIE resulting from reading the attribute value.
6194/// This is set iff the function returns true.
6195///
6196/// @param recursively if true, the function looks through the
6197/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6198/// all the way down to the initial DIE that is cloned and look on
6199/// that DIE to see if it has the @p attr_name attribute.
6200///
6201/// @return true if the DIE @p die contains an attribute named @p
6202/// attr_name that is a DIE reference, false otherwise.
6203static bool
6204die_die_attribute(const Dwarf_Die* die,
6205 unsigned attr_name,
6206 Dwarf_Die& result,
6207 bool recursively)
6208{
6209 Dwarf_Attribute attr;
6210 if (recursively
6211 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6212 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6213 return false;
6214
6215 return dwarf_formref_die(&attr, &result);
6216}
6217
6218/// Test if a subrange DIE indirectly references another subrange DIE
6219/// through a given attribute.
6220///
6221/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6222/// attribute be a reference to either a data member or a variable
6223/// which type is itself a DW_TAG_subrange_type. This latter subrange
6224/// DIE is said to be "indirectly referenced" by the former subrange
6225/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6226/// the value we want for the DW_AT_upper_bound of the former.
6227///
6228/// This function tests if the former subrange DIE does indirectly
6229/// reference another subrange DIE through a given attribute (not
6230/// necessarily DW_AT_upper_bound).
6231///
6232/// @param die the DIE to consider. Note that It must be a
6233/// DW_TAG_subrange_type.
6234///
6235/// @param attr_name the name of the attribute to look through for the
6236/// indirectly referenced subrange DIE.
6237///
6238/// @param referenced_subrange if the function returns true, then the
6239/// argument of this parameter is set to the indirectly referenced
6240/// DW_TAG_subrange_type DIE.
6241///
6242/// @return true iff @p DIE indirectly references a subrange DIE
6243/// through the attribute @p attr_name.
6244static bool
6245subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6246 unsigned attr_name,
6247 Dwarf_Die& referenced_subrange)
6248{
6249 bool result = false;
6250
6251 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6252 return result;
6253
6254 Dwarf_Die referenced_die;
6255 if (die_die_attribute(die, attr_name, referenced_die))
6256 {
6257 unsigned tag = dwarf_tag(&referenced_die);
6258 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6259 {
6260 Dwarf_Die type_die;
6261 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6262 {
6263 tag = dwarf_tag(&type_die);
6264 if (tag == DW_TAG_subrange_type)
6265 {
6266 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6267 result = true;
6268 }
6269 }
6270 }
6271 }
6272 return result;
6273}
6274
6275/// Return the bound value of subrange die by looking at an indirectly
6276/// referenced subrange DIE.
6277///
6278/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6279/// attribute be a reference to either a data member or a variable
6280/// which type is itself a DW_TAG_subrange_type. This latter subrange
6281/// DIE is said to be "indirectly referenced" by the former subrange
6282/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6283/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6284///
6285/// This function gets the DW_AT_{lower,upper}_bound value of a
6286/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6287/// the indirectly referenced subrange type, if it exists.
6288///
6289/// @param die the subrange DIE to consider.
6290///
6291/// @param attr_name the name of the attribute to consider, typically,
6292/// DW_AT_{lower,upper}_bound.
6293///
6294/// @param v the found value, iff this function returned true.
6295///
6296/// @param is_signed, this is set to true if @p v is signed. This
6297/// parameter is set at all only if the function returns true.
6298///
6299/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6300/// indirectly referenced subrange type.
6301static bool
6302subrange_die_indirect_bound_value(const Dwarf_Die *die,
6303 unsigned attr_name,
6304 array_type_def::subrange_type::bound_value& v,
6305 bool& is_signed)
6306{
6307 bool result = false;
6308
6309 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6310 return result;
6311
6312 Dwarf_Die subrange_die;
6313 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6314 subrange_die))
6315 {
6316 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6317 result = true;
6318 }
6319 return result;
6320}
6321
6322/// Read and return an addresss class attribute from a given DIE.
6323///
6324/// @param die the DIE to consider.
6325///
6326/// @param attr_name the name of the address class attribute to read
6327/// the value from.
6328///
6329/// @param the resulting address.
6330///
6331/// @return true iff the attribute could be read, was of the expected
6332/// address class and could thus be translated into the @p result.
6333static bool
6334die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6335{
6336 Dwarf_Attribute attr;
6337 if (!dwarf_attr_integrate(die, attr_name, &attr))
6338 return false;
6339 return dwarf_formaddr(&attr, &result) == 0;
6340}
6341
6342/// Returns the source location associated with a decl DIE.
6343///
6344/// @param rdr the @ref reader to use.
6345///
6346/// @param die the DIE the read the source location from.
6347///
6348/// @return the location associated with @p die.
6349static location
6350die_location(const reader& rdr, const Dwarf_Die* die)
6351{
6352 if (!die)
6353 return location();
6354
6355 string file = die_decl_file_attribute(die);
6356 uint64_t line = 0;
6357 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6358
6359 if (!file.empty() && line != 0)
6360 {
6361 translation_unit_sptr tu = rdr.cur_transl_unit();
6362 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6363 return l;
6364 }
6365 return location();
6366}
6367
6368/// Return a copy of the name of a DIE.
6369///
6370/// @param die the DIE to consider.
6371///
6372/// @return a copy of the name of the DIE.
6373static string
6374die_name(const Dwarf_Die* die)
6375{
6376 string name = die_string_attribute(die, DW_AT_name);
6377 return name;
6378}
6379
6380/// Return the location, the name and the mangled name of a given DIE.
6381///
6382/// @param rdr the DWARF reader to use.
6383///
6384/// @param die the DIE to read location and names from.
6385///
6386/// @param loc the location output parameter to set.
6387///
6388/// @param name the name output parameter to set.
6389///
6390/// @param linkage_name the linkage_name output parameter to set.
6391static void
6392die_loc_and_name(const reader& rdr,
6393 Dwarf_Die* die,
6394 location& loc,
6395 string& name,
6396 string& linkage_name)
6397{
6398 loc = die_location(rdr, die);
6399 name = die_name(die);
6400 linkage_name = die_linkage_name(die);
6401}
6402
6403/// Get the size of a (type) DIE as the value for the parameter
6404/// DW_AT_byte_size or DW_AT_bit_size.
6405///
6406/// @param die the DIE to read the information from.
6407///
6408/// @param size the resulting size in bits. This is set iff the
6409/// function return true.
6410///
6411/// @return true if the size attribute was found.
6412static bool
6413die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6414{
6415 if (!die)
6416 return false;
6417
6418 uint64_t byte_size = 0, bit_size = 0;
6419
6420 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6421 {
6422 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6423 return false;
6424 }
6425 else
6426 bit_size = byte_size * 8;
6427
6428 size = bit_size;
6429
6430 return true;
6431}
6432
6433/// Get the access specifier (from the DW_AT_accessibility attribute
6434/// value) of a given DIE.
6435///
6436/// @param die the DIE to consider.
6437///
6438/// @param access the resulting access. This is set iff the function
6439/// returns true.
6440///
6441/// @return bool if the DIE contains the DW_AT_accessibility die.
6442static bool
6443die_access_specifier(Dwarf_Die * die, access_specifier& access)
6444{
6445 if (!die)
6446 return false;
6447
6448 uint64_t a = 0;
6449 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6450 return false;
6451
6452 access_specifier result = private_access;
6453
6454 switch (a)
6455 {
6456 case private_access:
6457 result = private_access;
6458 break;
6459
6460 case protected_access:
6461 result = protected_access;
6462 break;
6463
6464 case public_access:
6465 result = public_access;
6466 break;
6467
6468 default:
6469 break;
6470 }
6471
6472 access = result;
6473 return true;
6474}
6475
6476/// Test whether a given DIE represents a decl that is public. That
6477/// is, one with the DW_AT_external attribute set.
6478///
6479/// @param die the DIE to consider for testing.
6480///
6481/// @return true if a DW_AT_external attribute is present and its
6482/// value is set to the true; return false otherwise.
6483static bool
6484die_is_public_decl(const Dwarf_Die* die)
6485{
6486 if (!die)
6487 return false;
6488 bool is_public = false;
6489
6490 // If this is a DW_TAG_subprogram DIE, look for the
6491 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6492 // then it's public. In all other cases, this should return false.
6493
6494 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6495 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6496 die_flag_attribute(die, DW_AT_external, is_public);
6497 else if (tag == DW_TAG_namespace)
6498 {
6499 string name = die_name(die);
6500 is_public = !name.empty();
6501 }
6502
6503 return is_public;
6504}
6505
6506/// Test if a DIE is effectively public.
6507///
6508/// This is meant to return true when either the DIE is public or when
6509/// it's a variable DIE that is at (global) namespace level.
6510///
6511/// @return true iff either the DIE is public or is a variable DIE
6512/// that is at (global) namespace level.
6513static bool
6514die_is_effectively_public_decl(const reader& rdr,
6515 const Dwarf_Die* die)
6516{
6517 if (die_is_public_decl(die))
6518 return true;
6519
6520 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6521 if (tag == DW_TAG_variable || tag == DW_TAG_member)
6522 {
6523 // The DIE is a variable.
6524 Dwarf_Die parent_die;
6525 size_t where_offset = 0;
6526 if (!get_parent_die(rdr, die, parent_die, where_offset))
6527 return false;
6528
6529 tag = dwarf_tag(&parent_die);
6530 if (tag == DW_TAG_compile_unit
6531 || tag == DW_TAG_partial_unit
6532 || tag == DW_TAG_type_unit)
6533 // The DIE is at global scope.
6534 return true;
6535
6536 if (tag == DW_TAG_namespace)
6537 {
6538 string name = die_name(&parent_die);
6539 if (name.empty())
6540 // The DIE at unnamed namespace scope, so it's not public.
6541 return false;
6542 // The DIE is at namespace scope.
6543 return true;
6544 }
6545 }
6546 return false;
6547}
6548
6549/// Test whether a given DIE represents a declaration-only DIE.
6550///
6551/// That is, if the DIE has the DW_AT_declaration flag set.
6552///
6553/// @param die the DIE to consider.
6554//
6555/// @return true if a DW_AT_declaration is present, false otherwise.
6556static bool
6557die_is_declaration_only(Dwarf_Die* die)
6558{
6559 bool is_declaration = false;
6560 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6561 if (is_declaration && !die_has_size_attribute(die))
6562 return true;
6563 return false;
6564}
6565
6566/// Test if a DIE is for a function decl.
6567///
6568/// @param die the DIE to consider.
6569///
6570/// @return true iff @p die represents a function decl.
6571static bool
6572die_is_function_decl(const Dwarf_Die *die)
6573{
6574 if (!die)
6575 return false;
6576
6577 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6578 if (tag == DW_TAG_subprogram)
6579 return true;
6580 return false;
6581}
6582
6583/// Test if a DIE is for a variable decl.
6584///
6585/// @param die the DIE to consider.
6586///
6587/// @return true iff @p die represents a variable decl.
6588static bool
6589die_is_variable_decl(const Dwarf_Die *die)
6590{
6591 if (!die)
6592 return false;
6593
6594 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6595 if (tag == DW_TAG_variable)
6596 return true;
6597 return false;
6598}
6599
6600/// Test if a DIE has size attribute.
6601///
6602/// @param die the DIE to consider.
6603///
6604/// @return true if the DIE has a size attribute.
6605static bool
6606die_has_size_attribute(const Dwarf_Die *die)
6607{
6608 uint64_t s;
6609 if (die_size_in_bits(die, s))
6610 return true;
6611 return false;
6612}
6613
6614/// Test that a DIE has no child DIE.
6615///
6616/// @param die the DIE to consider.
6617///
6618/// @return true iff @p die has no child DIE.
6619static bool
6620die_has_no_child(const Dwarf_Die *die)
6621{
6622 if (!die)
6623 return true;
6624
6625 Dwarf_Die child;
6626 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6627 return false;
6628 return true;
6629}
6630
6631/// Test whether a given DIE represents a declaration-only DIE.
6632///
6633/// That is, if the DIE has the DW_AT_declaration flag set.
6634///
6635/// @param die the DIE to consider.
6636//
6637/// @return true if a DW_AT_declaration is present, false otherwise.
6638static bool
6639die_is_declaration_only(const Dwarf_Die* die)
6640{return die_is_declaration_only(const_cast<Dwarf_Die*>(die));}
6641
6642/// Tests whether a given DIE is artificial.
6643///
6644/// @param die the test to test for.
6645///
6646/// @return true if the DIE is artificial, false otherwise.
6647static bool
6648die_is_artificial(Dwarf_Die* die)
6649{
6650 bool is_artificial;
6651 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6652}
6653
6654///@return true if a tag represents a type, false otherwise.
6655///
6656///@param tag the tag to consider.
6657static bool
6658is_type_tag(unsigned tag)
6659{
6660 bool result = false;
6661
6662 switch (tag)
6663 {
6664 case DW_TAG_array_type:
6665 case DW_TAG_class_type:
6666 case DW_TAG_enumeration_type:
6667 case DW_TAG_pointer_type:
6668 case DW_TAG_reference_type:
6669 case DW_TAG_string_type:
6670 case DW_TAG_structure_type:
6671 case DW_TAG_subroutine_type:
6672 case DW_TAG_typedef:
6673 case DW_TAG_union_type:
6674 case DW_TAG_ptr_to_member_type:
6675 case DW_TAG_set_type:
6676 case DW_TAG_subrange_type:
6677 case DW_TAG_base_type:
6678 case DW_TAG_const_type:
6679 case DW_TAG_file_type:
6680 case DW_TAG_packed_type:
6681 case DW_TAG_thrown_type:
6682 case DW_TAG_volatile_type:
6683 case DW_TAG_restrict_type:
6684 case DW_TAG_interface_type:
6685 case DW_TAG_unspecified_type:
6686 case DW_TAG_shared_type:
6687 case DW_TAG_rvalue_reference_type:
6688 case DW_TAG_coarray_type:
6689 case DW_TAG_atomic_type:
6690 case DW_TAG_immutable_type:
6691 result = true;
6692 break;
6693
6694 default:
6695 result = false;
6696 break;
6697 }
6698
6699 return result;
6700}
6701
6702/// Test if a given DIE is a type whose canonical type is to be
6703/// propagated during DIE canonicalization
6704///
6705/// This is a sub-routine of compare_dies.
6706///
6707/// @param tag the tag of the DIE to consider.
6708///
6709/// @return true iff the DIE of tag @p tag is can see its canonical
6710/// type be propagated during the type comparison that happens during
6711/// DIE canonicalization.
6712static bool
6713is_canon_type_to_be_propagated_tag(unsigned tag)
6714{
6715 bool result = false;
6716
6717 switch (tag)
6718 {
6719 case DW_TAG_class_type:
6720 case DW_TAG_structure_type:
6721 case DW_TAG_union_type:
6722 case DW_TAG_subroutine_type:
6723 case DW_TAG_subprogram:
6724 result = true;
6725 break;
6726
6727 default:
6728 result = false;
6729 break;
6730 }
6731
6732 return result;
6733}
6734
6735/// Test if a given kind of DIE ought to have its comparison result
6736/// cached by compare_dies, so that subsequent invocations of
6737/// compare_dies can be faster.
6738///
6739/// @param tag the tag of the DIE to consider.
6740///
6741/// @return true iff DIEs of the tag @p tag ought to have its
6742/// comparison results cached.
6743static bool
6744type_comparison_result_to_be_cached(unsigned tag)
6745{
6746 bool r = false;
6747 switch (tag)
6748 {
6749 case DW_TAG_class_type:
6750 case DW_TAG_structure_type:
6751 case DW_TAG_union_type:
6752 case DW_TAG_subroutine_type:
6753 case DW_TAG_subprogram:
6754 r = true;
6755 break;
6756
6757 default:
6758 r = false;
6759 break;
6760 }
6761 return r;
6762}
6763
6764/// Cache the result of comparing to type DIEs.
6765///
6766/// @param rdr the context to consider.
6767///
6768/// @param tag the tag of the DIEs to consider.
6769///
6770/// @param p the offsets of the pair of DIEs being compared.
6771///
6772/// @param result the comparison result to be cached.
6773static bool
6774maybe_cache_type_comparison_result(const reader& rdr,
6775 int tag,
6776 const offset_pair_type& p,
6777 comparison_result result)
6778{
6779 if (!type_comparison_result_to_be_cached(tag)
6780 || (result != COMPARISON_RESULT_EQUAL
6781 && result != COMPARISON_RESULT_DIFFERENT))
6782 return false;
6783
6784 rdr.die_comparison_results_[p] = result;
6785
6786 return true;
6787
6788}
6789
6790/// Get the cached result of the comparison of a pair of DIEs.
6791///
6792/// @param rdr the context to consider.
6793///
6794/// @param tag the tag of the pair of DIEs to consider.
6795///
6796/// @param p the offsets of the pair of DIEs to consider.
6797///
6798/// @param result out parameter set to the cached result of the
6799/// comparison of @p p if it has been found.
6800///
6801/// @return true iff a cached result for the comparisonof @p has been
6802/// found and set into @p result.
6803static bool
6804get_cached_type_comparison_result(const reader& rdr,
6805 const offset_pair_type& p,
6806 comparison_result& result)
6807{
6808 auto i = rdr.die_comparison_results_.find(p);
6809 if (i != rdr.die_comparison_results_.end())
6810 {
6811 result = i->second;
6812 return true;
6813 }
6814 return false;
6815}
6816
6817/// Get the cached result of the comparison of a pair of DIEs, if the
6818/// kind of DIEs ought to have its comparison results cached.
6819///
6820/// @param rdr the context to consider.
6821///
6822/// @param tag the tag of the pair of DIEs to consider.
6823///
6824/// @param p the offsets of the pair of DIEs to consider.
6825///
6826/// @param result out parameter set to the cached result of the
6827/// comparison of @p p if it has been found.
6828///
6829/// @return true iff a cached result for the comparisonof @p has been
6830/// found and set into @p result.
6831static bool
6832maybe_get_cached_type_comparison_result(const reader& rdr,
6833 int tag,
6834 const offset_pair_type& p,
6835 comparison_result& result)
6836{
6837 if (type_comparison_result_to_be_cached(tag))
6838 {
6839 // Types of this kind might have their comparison result cached
6840 // when they are not canonicalized. So let's see if we have a
6841 // cached comparison result.
6842 if (get_cached_type_comparison_result(rdr, p, result))
6843 return true;
6844 }
6845 return false;
6846}
6847
6848/// Test if a given DIE is to be canonicalized.
6849///
6850/// @param die the DIE to consider.
6851///
6852/// @return true iff @p die is to be canonicalized.
6853static bool
6854is_type_die_to_be_canonicalized(const Dwarf_Die *die)
6855{
6856 bool result = false;
6857 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6858
6859 if (!is_type_tag(tag))
6860 return false;
6861
6862 switch (tag)
6863 {
6864 case DW_TAG_class_type:
6865 case DW_TAG_structure_type:
6866 case DW_TAG_union_type:
6867 result = !die_is_declaration_only(die);
6868 break;
6869
6870 case DW_TAG_subroutine_type:
6871 case DW_TAG_subprogram:
6872 case DW_TAG_array_type:
6873 result = true;
6874
6875 default:
6876 break;
6877 }
6878
6879 return result;
6880}
6881
6882/// Test if a DIE tag represents a declaration.
6883///
6884/// @param tag the DWARF tag to consider.
6885///
6886/// @return true iff @p tag is for a declaration.
6887static bool
6888is_decl_tag(unsigned tag)
6889{
6890 switch (tag)
6891 {
6892 case DW_TAG_formal_parameter:
6893 case DW_TAG_imported_declaration:
6894 case DW_TAG_member:
6895 case DW_TAG_unspecified_parameters:
6896 case DW_TAG_subprogram:
6897 case DW_TAG_variable:
6898 case DW_TAG_namespace:
6899 case DW_TAG_GNU_template_template_param:
6900 case DW_TAG_GNU_template_parameter_pack:
6901 case DW_TAG_GNU_formal_parameter_pack:
6902 return true;
6903 }
6904 return false;
6905}
6906
6907/// Test if a DIE represents a type DIE.
6908///
6909/// @param die the DIE to consider.
6910///
6911/// @return true if @p die represents a type, false otherwise.
6912static bool
6913die_is_type(const Dwarf_Die* die)
6914{
6915 if (!die)
6916 return false;
6917 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6918}
6919
6920/// Test if a DIE represents a declaration.
6921///
6922/// @param die the DIE to consider.
6923///
6924/// @return true if @p die represents a decl, false otherwise.
6925static bool
6926die_is_decl(const Dwarf_Die* die)
6927{
6928 if (!die)
6929 return false;
6930 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6931}
6932
6933/// Test if a DIE represents a namespace.
6934///
6935/// @param die the DIE to consider.
6936///
6937/// @return true if @p die represents a namespace, false otherwise.
6938static bool
6939die_is_namespace(const Dwarf_Die* die)
6940{
6941 if (!die)
6942 return false;
6943 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
6944}
6945
6946/// Test if a DIE has tag DW_TAG_unspecified_type.
6947///
6948/// @param die the DIE to consider.
6949///
6950/// @return true if @p die has tag DW_TAG_unspecified_type.
6951static bool
6952die_is_unspecified(Dwarf_Die* die)
6953{
6954 if (!die)
6955 return false;
6956 return (dwarf_tag(die) == DW_TAG_unspecified_type);
6957}
6958
6959/// Test if a DIE represents a void type.
6960///
6961/// @param die the DIE to consider.
6962///
6963/// @return true if @p die represents a void type, false otherwise.
6964static bool
6965die_is_void_type(Dwarf_Die* die)
6966{
6967 if (!die || dwarf_tag(die) != DW_TAG_base_type)
6968 return false;
6969
6970 string name = die_name(die);
6971 if (name == "void")
6972 return true;
6973
6974 return false;
6975}
6976
6977/// Test if a DIE represents a pointer type.
6978///
6979/// @param die the die to consider.
6980///
6981/// @return true iff @p die represents a pointer type.
6982static bool
6983die_is_pointer_type(const Dwarf_Die* die)
6984{
6985 if (!die)
6986 return false;
6987
6988 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6989 if (tag == DW_TAG_pointer_type)
6990 return true;
6991
6992 return false;
6993}
6994
6995/// Test if a DIE is for a pointer, reference or qualified type to
6996/// anonymous class or struct.
6997///
6998/// @param die the DIE to consider.
6999///
7000/// @return true iff @p is for a pointer, reference or qualified type
7001/// to anonymous class or struct.
7002static bool
7003pointer_or_qual_die_of_anonymous_class_type(const Dwarf_Die* die)
7004{
7005 if (!die_is_pointer_array_or_reference_type(die)
7006 && !die_is_qualified_type(die))
7007 return false;
7008
7009 Dwarf_Die underlying_type_die;
7010 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
7011 return false;
7012
7013 if (!die_is_class_type(&underlying_type_die))
7014 return false;
7015
7016 string name = die_name(&underlying_type_die);
7017
7018 return name.empty();
7019}
7020
7021/// Test if a DIE represents a reference type.
7022///
7023/// @param die the die to consider.
7024///
7025/// @return true iff @p die represents a reference type.
7026static bool
7027die_is_reference_type(const Dwarf_Die* die)
7028{
7029 if (!die)
7030 return false;
7031
7032 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7033 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
7034 return true;
7035
7036 return false;
7037}
7038
7039/// Test if a DIE represents an array type.
7040///
7041/// @param die the die to consider.
7042///
7043/// @return true iff @p die represents an array type.
7044static bool
7045die_is_array_type(const Dwarf_Die* die)
7046{
7047 if (!die)
7048 return false;
7049
7050 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7051 if (tag == DW_TAG_array_type)
7052 return true;
7053
7054 return false;
7055}
7056
7057/// Test if a DIE represents a pointer, reference or array type.
7058///
7059/// @param die the die to consider.
7060///
7061/// @return true iff @p die represents a pointer or reference type.
7062static bool
7063die_is_pointer_array_or_reference_type(const Dwarf_Die* die)
7064{return (die_is_pointer_type(die)
7065 || die_is_reference_type(die)
7066 || die_is_array_type(die));}
7067
7068/// Test if a DIE represents a pointer or a reference type.
7069///
7070/// @param die the die to consider.
7071///
7072/// @return true iff @p die represents a pointer or reference type.
7073static bool
7074die_is_pointer_or_reference_type(const Dwarf_Die* die)
7075{return (die_is_pointer_type(die) || die_is_reference_type(die));}
7076
7077/// Test if a DIE represents a pointer, a reference or a typedef type.
7078///
7079/// @param die the die to consider.
7080///
7081/// @return true iff @p die represents a pointer, a reference or a
7082/// typedef type.
7083static bool
7084die_is_pointer_reference_or_typedef_type(const Dwarf_Die* die)
7085{return (die_is_pointer_array_or_reference_type(die)
7086 || dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_typedef);}
7087
7088/// Test if a DIE represents a class type.
7089///
7090/// @param die the die to consider.
7091///
7092/// @return true iff @p die represents a class type.
7093static bool
7094die_is_class_type(const Dwarf_Die* die)
7095{
7096 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7097
7098 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7099 return true;
7100
7101 return false;
7102}
7103
7104/// Test if a DIE is for a qualified type.
7105///
7106/// @param die the DIE to consider.
7107///
7108/// @return true iff @p die is for a qualified type.
7109static bool
7110die_is_qualified_type(const Dwarf_Die* die)
7111{
7112 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7113 if (tag == DW_TAG_const_type
7114 || tag == DW_TAG_volatile_type
7115 || tag == DW_TAG_restrict_type)
7116 return true;
7117
7118 return false;
7119}
7120
7121/// Test if a DIE is for a function type.
7122///
7123/// @param die the DIE to consider.
7124///
7125/// @return true iff @p die is for a function type.
7126static bool
7127die_is_function_type(const Dwarf_Die *die)
7128{
7129 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7130 if (tag == DW_TAG_subprogram || tag == DW_TAG_subroutine_type)
7131 return true;
7132
7133 return false;
7134}
7135
7136/// Test if a DIE for a function pointer or member function has an
7137/// DW_AT_object_pointer attribute.
7138///
7139/// @param die the DIE to consider.
7140///
7141/// @param object_pointer out parameter. It's set to the DIE for the
7142/// object pointer iff the function returns true.
7143///
7144/// @return true iff the DIE @p die has an object pointer. In that
7145/// case, the parameter @p object_pointer is set to the DIE of that
7146/// object pointer.
7147static bool
7148die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
7149{
7150 if (!die)
7151 return false;
7152
7153 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
7154 return true;
7155
7156 return false;
7157}
7158
7159/// Test if a DIE has children DIEs.
7160///
7161/// @param die the DIE to consider.
7162///
7163/// @return true iff @p DIE has at least one child node.
7164static bool
7165die_has_children(const Dwarf_Die* die)
7166{
7167 if (!die)
7168 return false;
7169
7170 Dwarf_Die child;
7171 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
7172 return true;
7173
7174 return false;
7175}
7176
7177/// When given the object pointer DIE of a function type or member
7178/// function DIE, this function returns the "this" pointer that points
7179/// to the associated class.
7180///
7181/// @param die the DIE of the object pointer of the function or member
7182/// function to consider.
7183///
7184/// @param this_pointer_die out parameter. This is set to the DIE of
7185/// the "this" pointer iff the function returns true.
7186///
7187/// @return true iff the function found the "this" pointer from the
7188/// object pointer DIE @p die. In that case, the parameter @p
7189/// this_pointer_die is set to the DIE of that "this" pointer.
7190static bool
7191die_this_pointer_from_object_pointer(Dwarf_Die* die,
7192 Dwarf_Die& this_pointer_die)
7193{
7194 ABG_ASSERT(die);
7195 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7196
7197 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7198 return true;
7199
7200 return false;
7201}
7202
7203/// Test if a given "this" pointer that points to a particular class
7204/// type is for a const class or not. If it's for a const class, then
7205/// it means the function type or the member function associated to
7206/// that "this" pointer is const.
7207///
7208/// @param die the DIE of the "this" pointer to consider.
7209///
7210/// @return true iff @p die points to a const class type.
7211static bool
7212die_this_pointer_is_const(Dwarf_Die* die)
7213{
7214 ABG_ASSERT(die);
7215
7216 if (dwarf_tag(die) == DW_TAG_pointer_type)
7217 {
7218 Dwarf_Die pointed_to_type_die;
7219 if (die_die_attribute(die, DW_AT_type, pointed_to_type_die))
7220 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7221 return true;
7222 }
7223
7224 return false;
7225}
7226
7227/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7228/// attribute) points to a const implicit class and so is for a const
7229/// method or or a const member function type.
7230///
7231/// @param die the DIE of the object pointer to consider.
7232///
7233/// @return true iff the object pointer represented by @p die is for a
7234/// a const method or const member function type.
7235static bool
7236die_object_pointer_is_for_const_method(Dwarf_Die* die)
7237{
7238 ABG_ASSERT(die);
7239 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7240
7241 Dwarf_Die this_pointer_die;
7242 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7243 if (die_this_pointer_is_const(&this_pointer_die))
7244 return true;
7245
7246 return false;
7247}
7248
7249/// Test if a DIE represents an entity that is at class scope.
7250///
7251/// @param rdr the DWARF reader to use.
7252///
7253/// @param die the DIE to consider.
7254///
7255/// @param where_offset where we are logically at in the DIE stream.
7256///
7257/// @param class_scope_die out parameter. Set to the DIE of the
7258/// containing class iff @p die happens to be at class scope; that is,
7259/// iff the function returns true.
7260///
7261/// @return true iff @p die is at class scope. In that case, @p
7262/// class_scope_die is set to the DIE of the class that contains @p
7263/// die.
7264static bool
7265die_is_at_class_scope(const reader& rdr,
7266 const Dwarf_Die* die,
7267 size_t where_offset,
7268 Dwarf_Die& class_scope_die)
7269{
7270 if (!get_scope_die(rdr, die, where_offset, class_scope_die))
7271 return false;
7272
7273 int tag = dwarf_tag(&class_scope_die);
7274
7275 return (tag == DW_TAG_structure_type
7276 || tag == DW_TAG_class_type
7277 || tag == DW_TAG_union_type);
7278}
7279
7280/// Return the leaf object under a pointer, reference or qualified
7281/// type DIE.
7282///
7283/// @param die the DIE of the type to consider.
7284///
7285/// @param peeled_die out parameter. Set to the DIE of the leaf
7286/// object iff the function actually peeled anything.
7287///
7288/// @return true upon successful completion.
7289static bool
7290die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7291{
7292 if (!die)
7293 return false;
7294
7295 int tag = dwarf_tag(die);
7296
7297 if (tag == DW_TAG_const_type
7298 || tag == DW_TAG_volatile_type
7299 || tag == DW_TAG_restrict_type
7300 || tag == DW_TAG_pointer_type
7301 || tag == DW_TAG_reference_type
7302 || tag == DW_TAG_rvalue_reference_type)
7303 {
7304 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7305 return false;
7306 }
7307 else
7308 return false;
7309
7310 memcpy(&peeled_die, die, sizeof(peeled_die));
7311
7312 while (tag == DW_TAG_const_type
7313 || tag == DW_TAG_volatile_type
7314 || tag == DW_TAG_restrict_type
7315 || tag == DW_TAG_pointer_type
7316 || tag == DW_TAG_reference_type
7317 || tag == DW_TAG_rvalue_reference_type)
7318 {
7319 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7320 break;
7321 tag = dwarf_tag(&peeled_die);
7322 }
7323
7324 return true;
7325}
7326
7327/// Return the leaf object under a qualified type DIE.
7328///
7329/// @param die the DIE of the type to consider.
7330///
7331/// @param peeled_die out parameter. Set to the DIE of the leaf
7332/// object iff the function actually peeled anything.
7333///
7334/// @return true upon successful completion.
7335static bool
7336die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7337{
7338 if (!die)
7339 return false;
7340
7341 memcpy(&peeled_die, die, sizeof(peeled_die));
7342
7343 int tag = dwarf_tag(&peeled_die);
7344
7345 bool result = false;
7346 while (tag == DW_TAG_const_type
7347 || tag == DW_TAG_volatile_type
7348 || tag == DW_TAG_restrict_type)
7349 {
7350 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7351 break;
7352 tag = dwarf_tag(&peeled_die);
7353 result = true;
7354 }
7355
7356 return result;
7357}
7358
7359/// Return the leaf object under a typedef type DIE.
7360///
7361/// @param die the DIE of the type to consider.
7362///
7363/// @param peeled_die out parameter. Set to the DIE of the leaf
7364/// object iff the function actually peeled anything.
7365///
7366/// @return true upon successful completion.
7367static bool
7368die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7369{
7370 if (!die)
7371 return false;
7372
7373 int tag = dwarf_tag(die);
7374
7375 memcpy(&peeled_die, die, sizeof(peeled_die));
7376
7377 if (tag == DW_TAG_typedef)
7378 {
7379 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7380 return false;
7381 }
7382 else
7383 return false;
7384
7385 while (tag == DW_TAG_typedef)
7386 {
7387 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7388 break;
7389 tag = dwarf_tag(&peeled_die);
7390 }
7391
7392 return true;
7393
7394}
7395
7396/// Return the leaf DIE under a pointer, a reference or a typedef DIE.
7397///
7398/// @param die the DIE to consider.
7399///
7400/// @param peeled_die the resulting peeled (or leaf) DIE. This is set
7401/// iff the function returned true.
7402///
7403/// @return true iff the function could peel @p die.
7404static bool
7405die_peel_pointer_and_typedef(const Dwarf_Die *die, Dwarf_Die& peeled_die)
7406{
7407 if (!die)
7408 return false;
7409
7410 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7411
7412 if (tag == DW_TAG_pointer_type
7413 || tag == DW_TAG_reference_type
7414 || tag == DW_TAG_rvalue_reference_type
7415 || tag == DW_TAG_typedef)
7416 {
7417 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7418 return false;
7419 }
7420 else
7421 return false;
7422
7423 while (tag == DW_TAG_pointer_type
7424 || tag == DW_TAG_reference_type
7425 || tag == DW_TAG_rvalue_reference_type
7426 || tag == DW_TAG_typedef)
7427 {
7428 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7429 break;
7430 tag = dwarf_tag(&peeled_die);
7431 }
7432 return true;
7433}
7434
7435/// Test if a DIE for a function type represents a method type.
7436///
7437/// @param rdr the DWARF reader.
7438///
7439/// @param die the DIE to consider.
7440///
7441/// @param where_offset where we logically are in the stream of DIEs.
7442///
7443/// @param object_pointer_die out parameter. This is set by the
7444/// function to the DIE that refers to the formal function parameter
7445/// which holds the implicit "this" pointer of the method. That die
7446/// is called the object pointer DIE. This is set iff the function
7447///
7448/// @param class_die out parameter. This is set by the function to
7449/// the DIE that represents the class of the method type. This is set
7450/// iff the function returns true.
7451///
7452/// @param is_static out parameter. This is set to true by the
7453/// function if @p die is a static method. This is set iff the
7454/// function returns true.
7455///
7456/// @return true iff @p die is a DIE for a method type.
7457static bool
7458die_function_type_is_method_type(const reader& rdr,
7459 const Dwarf_Die *die,
7460 size_t where_offset,
7461 Dwarf_Die& object_pointer_die,
7462 Dwarf_Die& class_die,
7463 bool& is_static)
7464{
7465 if (!die)
7466 return false;
7467
7468 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7469 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7470
7471 bool has_object_pointer = false;
7472 is_static = false;
7473 if (tag == DW_TAG_subprogram)
7474 {
7475 Dwarf_Die spec_or_origin_die;
7476 if (die_die_attribute(die, DW_AT_specification,
7477 spec_or_origin_die)
7478 || die_die_attribute(die, DW_AT_abstract_origin,
7479 spec_or_origin_die))
7480 {
7481 if (die_has_object_pointer(&spec_or_origin_die,
7482 object_pointer_die))
7483 has_object_pointer = true;
7484 else
7485 {
7486 if (die_is_at_class_scope(rdr, &spec_or_origin_die,
7487 where_offset, class_die))
7488 is_static = true;
7489 else
7490 return false;
7491 }
7492 }
7493 else
7494 {
7495 if (die_has_object_pointer(die, object_pointer_die))
7496 has_object_pointer = true;
7497 else
7498 {
7499 if (die_is_at_class_scope(rdr, die, where_offset, class_die))
7500 is_static = true;
7501 else
7502 return false;
7503 }
7504 }
7505 }
7506 else
7507 {
7508 if (die_has_object_pointer(die, object_pointer_die))
7509 has_object_pointer = true;
7510 else
7511 return false;
7512 }
7513
7514 if (!is_static)
7515 {
7516 ABG_ASSERT(has_object_pointer);
7517 // The object pointer die points to a DW_TAG_formal_parameter which
7518 // is the "this" parameter. The type of the "this" parameter is a
7519 // pointer. Let's get that pointer type.
7520 Dwarf_Die this_type_die;
7521 if (!die_die_attribute(&object_pointer_die, DW_AT_type, this_type_die))
7522 return false;
7523
7524 // So the class type is the type pointed to by the type of the "this"
7525 // parameter.
7526 if (!die_peel_qual_ptr(&this_type_die, class_die))
7527 return false;
7528
7529 // And make we return a class type, rather than a typedef to a
7530 // class.
7531 die_peel_typedef(&class_die, class_die);
7532 }
7533
7534 return true;
7535}
7536
7537enum virtuality
7538{
7539 VIRTUALITY_NOT_VIRTUAL,
7540 VIRTUALITY_VIRTUAL,
7541 VIRTUALITY_PURE_VIRTUAL
7542};
7543
7544/// Get the virtual-ness of a given DIE, that is, the value of the
7545/// DW_AT_virtuality attribute.
7546///
7547/// @param die the DIE to read from.
7548///
7549/// @param virt the resulting virtuality attribute. This is set iff
7550/// the function returns true.
7551///
7552/// @return true if the virtual-ness could be determined.
7553static bool
7554die_virtuality(const Dwarf_Die* die, virtuality& virt)
7555{
7556 if (!die)
7557 return false;
7558
7559 uint64_t v = 0;
7560 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7561
7562 if (v == DW_VIRTUALITY_virtual)
7563 virt = VIRTUALITY_VIRTUAL;
7564 else if (v == DW_VIRTUALITY_pure_virtual)
7565 virt = VIRTUALITY_PURE_VIRTUAL;
7566 else
7567 virt = VIRTUALITY_NOT_VIRTUAL;
7568
7569 return true;
7570}
7571
7572/// Test whether the DIE represent either a virtual base or function.
7573///
7574/// @param die the DIE to consider.
7575///
7576/// @return bool if the DIE represents a virtual base or function,
7577/// false othersise.
7578static bool
7579die_is_virtual(const Dwarf_Die* die)
7580{
7581 virtuality v;
7582 if (!die_virtuality(die, v))
7583 return false;
7584
7585 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7586}
7587
7588/// Test if the DIE represents an entity that was declared inlined.
7589///
7590/// @param die the DIE to test for.
7591///
7592/// @return true if the DIE represents an entity that was declared
7593/// inlined.
7594static bool
7595die_is_declared_inline(Dwarf_Die* die)
7596{
7597 uint64_t inline_value = 0;
7598 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7599 return false;
7600 return inline_value == DW_INL_declared_inlined;
7601}
7602
7603/// Compare two DWARF strings using the most accurate (and slowest)
7604/// method possible.
7605///
7606/// @param l the DIE that carries the first string to consider, as an
7607/// attribute value.
7608///
7609/// @param attr_name the name of the attribute which value is the
7610/// string to compare.
7611///
7612/// @return true iff the string carried by @p l equals the one carried
7613/// by @p r.
7614static bool
7615slowly_compare_strings(const Dwarf_Die *l,
7616 const Dwarf_Die *r,
7617 unsigned attr_name)
7618{
7619 const char *l_str = die_char_str_attribute(l, attr_name),
7620 *r_str = die_char_str_attribute(r, attr_name);
7621 if (!l_str && !r_str)
7622 return true;
7623 return l_str && r_str && !strcmp(l_str, r_str);
7624}
7625
7626/// This function is a fast routine (optimization) to compare the
7627/// values of two string attributes of two DIEs.
7628///
7629/// @param l the first DIE to consider.
7630///
7631/// @param r the second DIE to consider.
7632///
7633/// @param attr_name the name of the attribute to compare, on the two
7634/// DIEs above.
7635///
7636/// @param result out parameter. This is set to the result of the
7637/// comparison. If the value of attribute @p attr_name on DIE @p l
7638/// equals the value of attribute @p attr_name on DIE @p r, then the
7639/// the argument of this parameter is set to true. Otherwise, it's
7640/// set to false. Note that the argument of this parameter is set iff
7641/// the function returned true.
7642///
7643/// @return true iff the comparison could be performed. There are
7644/// cases in which the comparison cannot be performed. For instance,
7645/// if one of the DIEs does not have the attribute @p attr_name. In
7646/// any case, if this function returns true, then the parameter @p
7647/// result is set to the result of the comparison.
7648static bool
7649compare_dies_string_attribute_value(const Dwarf_Die *l, const Dwarf_Die *r,
7650 unsigned attr_name,
7651 bool &result)
7652{
7653 Dwarf_Attribute l_attr, r_attr;
7654 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(l), attr_name, &l_attr)
7655 || !dwarf_attr_integrate(const_cast<Dwarf_Die*>(r), attr_name, &r_attr))
7656 return false;
7657
7658 ABG_ASSERT(l_attr.form == DW_FORM_strp
7659 || l_attr.form == DW_FORM_string
7660 || l_attr.form == DW_FORM_GNU_strp_alt
7661 || form_is_DW_FORM_strx(l_attr.form)
7662 || form_is_DW_FORM_line_strp(l_attr.form));
7663
7664 ABG_ASSERT(r_attr.form == DW_FORM_strp
7665 || r_attr.form == DW_FORM_string
7666 || r_attr.form == DW_FORM_GNU_strp_alt
7667 || form_is_DW_FORM_strx(r_attr.form)
7668 || form_is_DW_FORM_line_strp(r_attr.form));
7669
7670 if ((l_attr.form == DW_FORM_strp
7671 && r_attr.form == DW_FORM_strp)
7672 || (l_attr.form == DW_FORM_GNU_strp_alt
7673 && r_attr.form == DW_FORM_GNU_strp_alt)
7674 || (form_is_DW_FORM_strx(l_attr.form)
7675 && form_is_DW_FORM_strx(r_attr.form))
7676 || (form_is_DW_FORM_line_strp(l_attr.form)
7677 && form_is_DW_FORM_line_strp(r_attr.form)))
7678 {
7679 // So these string attributes are actually pointers into a
7680 // string table. The string table is most likely de-duplicated
7681 // so comparing the *values* of the pointers should be enough.
7682 //
7683 // This is the fast path.
7684 if (l_attr.valp == r_attr.valp)
7685 {
7686#if WITH_DEBUG_TYPE_CANONICALIZATION
7687 ABG_ASSERT(slowly_compare_strings(l, r, attr_name));
7688#endif
7689 result = true;
7690 return true;
7691 }
7692 }
7693
7694 // If we reached this point it means we couldn't use the fast path
7695 // because the string atttributes are strings that are "inline" in
7696 // the debug info section. Let's just compare them the slow and
7697 // obvious way.
7698 result = slowly_compare_strings(l, r, attr_name);
7699 return true;
7700}
7701
7702/// Compare the file path of the compilation units (aka CUs)
7703/// associated to two DIEs.
7704///
7705/// If the DIEs are for pointers or typedefs, this function also
7706/// compares the file paths of the CUs of the leaf DIEs (underlying
7707/// DIEs of the pointer or the typedef).
7708///
7709/// @param l the first type DIE to consider.
7710///
7711/// @param r the second type DIE to consider.
7712///
7713/// @return true iff the file paths of the DIEs of the two types are
7714/// equal.
7715static bool
7716compare_dies_cu_decl_file(const Dwarf_Die* l, const Dwarf_Die *r, bool &result)
7717{
7718 Dwarf_Die l_cu, r_cu;
7719 if (!dwarf_diecu(const_cast<Dwarf_Die*>(l), &l_cu, 0, 0)
7720 ||!dwarf_diecu(const_cast<Dwarf_Die*>(r), &r_cu, 0, 0))
7721 return false;
7722
7723 bool compared =
7724 compare_dies_string_attribute_value(&l_cu, &r_cu,
7725 DW_AT_name,
7726 result);
7727 if (compared && result)
7728 {
7729 Dwarf_Die peeled_l, peeled_r;
7730 if (die_is_pointer_reference_or_typedef_type(l)
7731 && die_is_pointer_reference_or_typedef_type(r)
7732 && die_peel_pointer_and_typedef(l, peeled_l)
7733 && die_peel_pointer_and_typedef(r, peeled_r))
7734 {
7735 if (!dwarf_diecu(&peeled_l, &l_cu, 0, 0)
7736 ||!dwarf_diecu(&peeled_r, &r_cu, 0, 0))
7737 return false;
7738 compared =
7739 compare_dies_string_attribute_value(&l_cu, &r_cu,
7740 DW_AT_name,
7741 result);
7742 }
7743 }
7744
7745 return compared;
7746}
7747
7748// -----------------------------------
7749// <location expression evaluation>
7750// -----------------------------------
7751
7752/// Get the value of a given DIE attribute, knowing that it must be a
7753/// location expression.
7754///
7755/// @param die the DIE to read the attribute from.
7756///
7757/// @param attr_name the name of the attribute to read the value for.
7758///
7759/// @param expr the pointer to allocate and fill with the resulting
7760/// array of operators + operands forming a dwarf expression. This is
7761/// set iff the function returns true.
7762///
7763/// @param expr_len the length of the resulting dwarf expression.
7764/// This is set iff the function returns true.
7765///
7766/// @return true if the attribute exists and has a non-empty dwarf expression
7767/// as value. In that case the expr and expr_len arguments are set to the
7768/// resulting dwarf expression.
7769static bool
7770die_location_expr(const Dwarf_Die* die,
7771 unsigned attr_name,
7772 Dwarf_Op** expr,
7773 size_t* expr_len)
7774{
7775 if (!die)
7776 return false;
7777
7778 Dwarf_Attribute attr;
7779 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7780 return false;
7781
7782 size_t len = 0;
7783 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7784
7785 // Ignore location expressions where reading them succeeded but
7786 // their length is 0.
7787 result &= len > 0;
7788
7789 if (result)
7790 *expr_len = len;
7791
7792 return result;
7793}
7794
7795/// If the current operation in the dwarf expression represents a push
7796/// of a constant value onto the dwarf expr virtual machine (aka
7797/// DEVM), perform the operation and update the DEVM.
7798///
7799/// If the result of the operation is a constant, update the DEVM
7800/// accumulator with its value. Otherwise, the DEVM accumulator is
7801/// left with its previous value.
7802///
7803/// @param ops the array of the dwarf expression operations to consider.
7804///
7805/// @param ops_len the lengths of @p ops array above.
7806///
7807/// @param index the index of the operation to interpret, in @p ops.
7808///
7809/// @param next_index the index of the operation to interpret at the
7810/// next step, after this function completed and returned. This is
7811/// set an output parameter that is set iff the function returns true.
7812///
7813/// @param ctxt the DEVM evaluation context.
7814///
7815/// @return true if the current operation actually pushes a constant
7816/// value onto the DEVM stack, false otherwise.
7817static bool
7818op_pushes_constant_value(Dwarf_Op* ops,
7819 size_t ops_len,
7820 size_t index,
7821 size_t& next_index,
7822 dwarf_expr_eval_context& ctxt)
7823{
7824 ABG_ASSERT(index < ops_len);
7825
7826 Dwarf_Op& op = ops[index];
7827 int64_t value = 0;
7828
7829 switch (op.atom)
7830 {
7831 case DW_OP_addr:
7832 value = ops[index].number;
7833 break;
7834
7835 case DW_OP_const1u:
7836 case DW_OP_const1s:
7837 case DW_OP_const2u:
7838 case DW_OP_const2s:
7839 case DW_OP_const4u:
7840 case DW_OP_const4s:
7841 case DW_OP_const8u:
7842 case DW_OP_const8s:
7843 case DW_OP_constu:
7844 case DW_OP_consts:
7845 value = ops[index].number;
7846 break;
7847
7848 case DW_OP_lit0:
7849 value = 0;
7850 break;
7851 case DW_OP_lit1:
7852 value = 1;
7853 break;
7854 case DW_OP_lit2:
7855 value = 2;
7856 break;
7857 case DW_OP_lit3:
7858 value = 3;
7859 break;
7860 case DW_OP_lit4:
7861 value = 4;
7862 break;
7863 case DW_OP_lit5:
7864 value = 5;
7865 break;
7866 case DW_OP_lit6:
7867 value = 6;
7868 break;
7869 case DW_OP_lit7:
7870 value = 7;
7871 break;
7872 case DW_OP_lit8:
7873 value = 8;
7874 break;
7875 case DW_OP_lit9:
7876 value = 9;
7877 break;
7878 case DW_OP_lit10:
7879 value = 10;
7880 break;
7881 case DW_OP_lit11:
7882 value = 11;
7883 break;
7884 case DW_OP_lit12:
7885 value = 12;
7886 break;
7887 case DW_OP_lit13:
7888 value = 13;
7889 break;
7890 case DW_OP_lit14:
7891 value = 14;
7892 break;
7893 case DW_OP_lit15:
7894 value = 15;
7895 break;
7896 case DW_OP_lit16:
7897 value = 16;
7898 break;
7899 case DW_OP_lit17:
7900 value = 17;
7901 break;
7902 case DW_OP_lit18:
7903 value = 18;
7904 break;
7905 case DW_OP_lit19:
7906 value = 19;
7907 break;
7908 case DW_OP_lit20:
7909 value = 20;
7910 break;
7911 case DW_OP_lit21:
7912 value = 21;
7913 break;
7914 case DW_OP_lit22:
7915 value = 22;
7916 break;
7917 case DW_OP_lit23:
7918 value = 23;
7919 break;
7920 case DW_OP_lit24:
7921 value = 24;
7922 break;
7923 case DW_OP_lit25:
7924 value = 25;
7925 break;
7926 case DW_OP_lit26:
7927 value = 26;
7928 break;
7929 case DW_OP_lit27:
7930 value = 27;
7931 break;
7932 case DW_OP_lit28:
7933 value = 28;
7934 break;
7935 case DW_OP_lit29:
7936 value = 29;
7937 break;
7938 case DW_OP_lit30:
7939 value = 30;
7940 break;
7941 case DW_OP_lit31:
7942 value = 31;
7943 break;
7944
7945 default:
7946 return false;
7947 }
7948
7949 expr_result r(value);
7950 ctxt.push(r);
7951 ctxt.accum = r;
7952 next_index = index + 1;
7953
7954 return true;
7955}
7956
7957/// If the current operation in the dwarf expression represents a push
7958/// of a non-constant value onto the dwarf expr virtual machine (aka
7959/// DEVM), perform the operation and update the DEVM. A non-constant
7960/// is namely a quantity for which we need inferior (a running program
7961/// image) state to know the exact value.
7962///
7963/// Upon successful completion, as the result of the operation is a
7964/// non-constant the DEVM accumulator value is left to its state as of
7965/// before the invocation of this function.
7966///
7967/// @param ops the array of the dwarf expression operations to consider.
7968///
7969/// @param ops_len the lengths of @p ops array above.
7970///
7971/// @param index the index of the operation to interpret, in @p ops.
7972///
7973/// @param next_index the index of the operation to interpret at the
7974/// next step, after this function completed and returned. This is
7975/// set an output parameter that is set iff the function returns true.
7976///
7977/// @param ctxt the DEVM evaluation context.
7978///
7979/// @return true if the current operation actually pushes a
7980/// non-constant value onto the DEVM stack, false otherwise.
7981static bool
7982op_pushes_non_constant_value(Dwarf_Op* ops,
7983 size_t ops_len,
7984 size_t index,
7985 size_t& next_index,
7986 dwarf_expr_eval_context& ctxt)
7987{
7988 ABG_ASSERT(index < ops_len);
7989 Dwarf_Op& op = ops[index];
7990
7991 switch (op.atom)
7992 {
7993 case DW_OP_reg0:
7994 case DW_OP_reg1:
7995 case DW_OP_reg2:
7996 case DW_OP_reg3:
7997 case DW_OP_reg4:
7998 case DW_OP_reg5:
7999 case DW_OP_reg6:
8000 case DW_OP_reg7:
8001 case DW_OP_reg8:
8002 case DW_OP_reg9:
8003 case DW_OP_reg10:
8004 case DW_OP_reg11:
8005 case DW_OP_reg12:
8006 case DW_OP_reg13:
8007 case DW_OP_reg14:
8008 case DW_OP_reg15:
8009 case DW_OP_reg16:
8010 case DW_OP_reg17:
8011 case DW_OP_reg18:
8012 case DW_OP_reg19:
8013 case DW_OP_reg20:
8014 case DW_OP_reg21:
8015 case DW_OP_reg22:
8016 case DW_OP_reg23:
8017 case DW_OP_reg24:
8018 case DW_OP_reg25:
8019 case DW_OP_reg26:
8020 case DW_OP_reg27:
8021 case DW_OP_reg28:
8022 case DW_OP_reg29:
8023 case DW_OP_reg30:
8024 case DW_OP_reg31:
8025 next_index = index + 1;
8026 break;
8027
8028 case DW_OP_breg0:
8029 case DW_OP_breg1:
8030 case DW_OP_breg2:
8031 case DW_OP_breg3:
8032 case DW_OP_breg4:
8033 case DW_OP_breg5:
8034 case DW_OP_breg6:
8035 case DW_OP_breg7:
8036 case DW_OP_breg8:
8037 case DW_OP_breg9:
8038 case DW_OP_breg10:
8039 case DW_OP_breg11:
8040 case DW_OP_breg12:
8041 case DW_OP_breg13:
8042 case DW_OP_breg14:
8043 case DW_OP_breg15:
8044 case DW_OP_breg16:
8045 case DW_OP_breg17:
8046 case DW_OP_breg18:
8047 case DW_OP_breg19:
8048 case DW_OP_breg20:
8049 case DW_OP_breg21:
8050 case DW_OP_breg22:
8051 case DW_OP_breg23:
8052 case DW_OP_breg24:
8053 case DW_OP_breg25:
8054 case DW_OP_breg26:
8055 case DW_OP_breg27:
8056 case DW_OP_breg28:
8057 case DW_OP_breg29:
8058 case DW_OP_breg30:
8059 case DW_OP_breg31:
8060 next_index = index + 1;
8061 break;
8062
8063 case DW_OP_regx:
8064 next_index = index + 2;
8065 break;
8066
8067 case DW_OP_fbreg:
8068 next_index = index + 1;
8069 break;
8070
8071 case DW_OP_bregx:
8072 next_index = index + 1;
8073 break;
8074
8075 case DW_OP_GNU_variable_value:
8076 next_index = index + 1;
8077 break;
8078
8079 default:
8080 return false;
8081 }
8082
8083 expr_result r(false);
8084 ctxt.push(r);
8085
8086 return true;
8087}
8088
8089/// If the current operation in the dwarf expression represents a
8090/// manipulation of the stack of the DWARF Expression Virtual Machine
8091/// (aka DEVM), this function performs the operation and updates the
8092/// state of the DEVM. If the result of the operation represents a
8093/// constant value, then the accumulator of the DEVM is set to that
8094/// result's value, Otherwise, the DEVM accumulator is left with its
8095/// previous value.
8096///
8097/// @param expr the array of the dwarf expression operations to consider.
8098///
8099/// @param expr_len the lengths of @p ops array above.
8100///
8101/// @param index the index of the operation to interpret, in @p ops.
8102///
8103/// @param next_index the index of the operation to interpret at the
8104/// next step, after this function completed and returned. This is
8105/// set an output parameter that is set iff the function returns true.
8106///
8107/// @param ctxt the DEVM evaluation context.
8108///
8109/// @return true if the current operation actually manipulates the
8110/// DEVM stack, false otherwise.
8111static bool
8112op_manipulates_stack(Dwarf_Op* expr,
8113 size_t expr_len,
8114 size_t index,
8115 size_t& next_index,
8116 dwarf_expr_eval_context& ctxt)
8117{
8118 Dwarf_Op& op = expr[index];
8119 expr_result v;
8120
8121 switch (op.atom)
8122 {
8123 case DW_OP_dup:
8124 v = ctxt.stack.front();
8125 ctxt.push(v);
8126 break;
8127
8128 case DW_OP_drop:
8129 v = ctxt.stack.front();
8130 ctxt.pop();
8131 break;
8132
8133 case DW_OP_over:
8134 ABG_ASSERT(ctxt.stack.size() > 1);
8135 v = ctxt.stack[1];
8136 ctxt.push(v);
8137 break;
8138
8139 case DW_OP_pick:
8140 ABG_ASSERT(index + 1 < expr_len);
8141 v = op.number;
8142 ctxt.push(v);
8143 break;
8144
8145 case DW_OP_swap:
8146 ABG_ASSERT(ctxt.stack.size() > 1);
8147 v = ctxt.stack[1];
8148 ctxt.stack.erase(ctxt.stack.begin() + 1);
8149 ctxt.push(v);
8150 break;
8151
8152 case DW_OP_rot:
8153 ABG_ASSERT(ctxt.stack.size() > 2);
8154 v = ctxt.stack[2];
8155 ctxt.stack.erase(ctxt.stack.begin() + 2);
8156 ctxt.push(v);
8157 break;
8158
8159 case DW_OP_deref:
8160 case DW_OP_deref_size:
8161 ABG_ASSERT(ctxt.stack.size() > 0);
8162 ctxt.pop();
8163 v.is_const(false);
8164 ctxt.push(v);
8165 break;
8166
8167 case DW_OP_xderef:
8168 case DW_OP_xderef_size:
8169 ABG_ASSERT(ctxt.stack.size() > 1);
8170 ctxt.pop();
8171 ctxt.pop();
8172 v.is_const(false);
8173 ctxt.push(v);
8174 break;
8175
8176 case DW_OP_push_object_address:
8177 v.is_const(false);
8178 ctxt.push(v);
8179 break;
8180
8181 case DW_OP_form_tls_address:
8182 case DW_OP_GNU_push_tls_address:
8183 ABG_ASSERT(ctxt.stack.size() > 0);
8184 v = ctxt.pop();
8185 if (op.atom == DW_OP_form_tls_address)
8186 v.is_const(false);
8187 ctxt.push(v);
8188 break;
8189
8190 case DW_OP_call_frame_cfa:
8191 v.is_const(false);
8192 ctxt.push(v);
8193 break;
8194
8195 default:
8196 return false;
8197 }
8198
8199 if (v.is_const())
8200 ctxt.accum = v;
8201
8202 if (op.atom == DW_OP_form_tls_address
8203 || op.atom == DW_OP_GNU_push_tls_address)
8204 ctxt.set_tls_address(true);
8205 else
8206 ctxt.set_tls_address(false);
8207
8208 next_index = index + 1;
8209
8210 return true;
8211}
8212
8213/// If the current operation in the dwarf expression represents a push
8214/// of an arithmetic or logic operation onto the dwarf expr virtual
8215/// machine (aka DEVM), perform the operation and update the DEVM.
8216///
8217/// If the result of the operation is a constant, update the DEVM
8218/// accumulator with its value. Otherwise, the DEVM accumulator is
8219/// left with its previous value.
8220///
8221/// @param expr the array of the dwarf expression operations to consider.
8222///
8223/// @param expr_len the lengths of @p expr array above.
8224///
8225/// @param index the index of the operation to interpret, in @p expr.
8226///
8227/// @param next_index the index of the operation to interpret at the
8228/// next step, after this function completed and returned. This is
8229/// set an output parameter that is set iff the function returns true.
8230///
8231/// @param ctxt the DEVM evaluation context.
8232///
8233/// @return true if the current operation actually represent an
8234/// arithmetic or logic operation.
8235static bool
8236op_is_arith_logic(Dwarf_Op* expr,
8237 size_t expr_len,
8238 size_t index,
8239 size_t& next_index,
8240 dwarf_expr_eval_context& ctxt)
8241{
8242 ABG_ASSERT(index < expr_len);
8243
8244 Dwarf_Op& op = expr[index];
8245 expr_result val1, val2;
8246 bool result = false;
8247
8248 switch (op.atom)
8249 {
8250 case DW_OP_abs:
8251 ABG_ASSERT(ctxt.stack.size() > 0);
8252 val1 = ctxt.pop();
8253 val1 = val1.abs();
8254 ctxt.push(val1);
8255 result = true;
8256 break;
8257
8258 case DW_OP_and:
8259 ABG_ASSERT(ctxt.stack.size() > 1);
8260 val1 = ctxt.pop();
8261 val2 = ctxt.pop();
8262 ctxt.push(val1 & val2);
8263 break;
8264
8265 case DW_OP_div:
8266 ABG_ASSERT(ctxt.stack.size() > 1);
8267 val1 = ctxt.pop();
8268 val2 = ctxt.pop();
8269 if (!val1.is_const())
8270 val1 = 1;
8271 ctxt.push(val2 / val1);
8272 result = true;
8273 break;
8274
8275 case DW_OP_minus:
8276 ABG_ASSERT(ctxt.stack.size() > 1);
8277 val1 = ctxt.pop();
8278 val2 = ctxt.pop();
8279 ctxt.push(val2 - val1);
8280 result = true;
8281 break;
8282
8283 case DW_OP_mod:
8284 ABG_ASSERT(ctxt.stack.size() > 1);
8285 val1 = ctxt.pop();
8286 val2 = ctxt.pop();
8287 ctxt.push(val2 % val1);
8288 result = true;
8289 break;
8290
8291 case DW_OP_mul:
8292 ABG_ASSERT(ctxt.stack.size() > 1);
8293 val1 = ctxt.pop();
8294 val2 = ctxt.pop();
8295 ctxt.push(val2 * val1);
8296 result = true;
8297 break;
8298
8299 case DW_OP_neg:
8300 ABG_ASSERT(ctxt.stack.size() > 0);
8301 val1 = ctxt.pop();
8302 ctxt.push(-val1);
8303 result = true;
8304 break;
8305
8306 case DW_OP_not:
8307 ABG_ASSERT(ctxt.stack.size() > 0);
8308 val1 = ctxt.pop();
8309 ctxt.push(~val1);
8310 result = true;
8311 break;
8312
8313 case DW_OP_or:
8314 ABG_ASSERT(ctxt.stack.size() > 1);
8315 val1 = ctxt.pop();
8316 val2 = ctxt.pop();
8317 ctxt.push(val1 | val2);
8318 result = true;
8319 break;
8320
8321 case DW_OP_plus:
8322 ABG_ASSERT(ctxt.stack.size() > 1);
8323 val1 = ctxt.pop();
8324 val2 = ctxt.pop();
8325 ctxt.push(val2 + val1);
8326 result = true;
8327 break;
8328
8329 case DW_OP_plus_uconst:
8330 ABG_ASSERT(ctxt.stack.size() > 0);
8331 val1 = ctxt.pop();
8332 val1 += op.number;
8333 ctxt.push(val1);
8334 result = true;
8335 break;
8336
8337 case DW_OP_shl:
8338 ABG_ASSERT(ctxt.stack.size() > 1);
8339 val1 = ctxt.pop();
8340 val2 = ctxt.pop();
8341 ctxt.push(val2 << val1);
8342 result = true;
8343 break;
8344
8345 case DW_OP_shr:
8346 case DW_OP_shra:
8347 ABG_ASSERT(ctxt.stack.size() > 1);
8348 val1 = ctxt.pop();
8349 val2 = ctxt.pop();
8350 ctxt.push(val2 >> val1);
8351 result = true;
8352 break;
8353
8354 case DW_OP_xor:
8355 ABG_ASSERT(ctxt.stack.size() > 1);
8356 val1 = ctxt.pop();
8357 val2 = ctxt.pop();
8358 ctxt.push(val2 ^ val1);
8359 result = true;
8360 break;
8361
8362 default:
8363 break;
8364 }
8365
8366 if (result == true)
8367 {
8368 if (ctxt.stack.front().is_const())
8369 ctxt.accum = ctxt.stack.front();
8370
8371 next_index = index + 1;
8372 }
8373 return result;;
8374}
8375
8376/// If the current operation in the dwarf expression represents a push
8377/// of a control flow operation onto the dwarf expr virtual machine
8378/// (aka DEVM), perform the operation and update the DEVM.
8379///
8380/// If the result of the operation is a constant, update the DEVM
8381/// accumulator with its value. Otherwise, the DEVM accumulator is
8382/// left with its previous value.
8383///
8384/// @param expr the array of the dwarf expression operations to consider.
8385///
8386/// @param expr_len the lengths of @p expr array above.
8387///
8388/// @param index the index of the operation to interpret, in @p expr.
8389///
8390/// @param next_index the index of the operation to interpret at the
8391/// next step, after this function completed and returned. This is
8392/// set an output parameter that is set iff the function returns true.
8393///
8394/// @param ctxt the DEVM evaluation context.
8395///
8396/// @return true if the current operation actually represents a
8397/// control flow operation, false otherwise.
8398static bool
8399op_is_control_flow(Dwarf_Op* expr,
8400 size_t expr_len,
8401 size_t index,
8402 size_t& next_index,
8403 dwarf_expr_eval_context& ctxt)
8404{
8405 ABG_ASSERT(index < expr_len);
8406
8407 Dwarf_Op& op = expr[index];
8408 expr_result val1, val2;
8409
8410 switch (op.atom)
8411 {
8412 case DW_OP_eq:
8413 case DW_OP_ge:
8414 case DW_OP_gt:
8415 case DW_OP_le:
8416 case DW_OP_lt:
8417 case DW_OP_ne:
8418 {
8419 bool value = true;
8420 val1 = ctxt.pop();
8421 val2 = ctxt.pop();
8422 if (op.atom == DW_OP_eq)
8423 value = val2 == val1;
8424 else if (op.atom == DW_OP_ge)
8425 value = val2 >= val1;
8426 else if (op.atom == DW_OP_gt)
8427 value = val2 > val1;
8428 else if (op.atom == DW_OP_le)
8429 value = val2 <= val1;
8430 else if (op.atom == DW_OP_lt)
8431 value = val2 < val1;
8432 else if (op.atom == DW_OP_ne)
8433 value = val2 != val1;
8434
8435 val1 = value ? 1 : 0;
8436 ctxt.push(val1);
8437 }
8438 break;
8439
8440 case DW_OP_skip:
8441 if (op.number > 0)
8442 index += op.number - 1;
8443 break;
8444
8445 case DW_OP_bra:
8446 val1 = ctxt.pop();
8447 if (val1.const_value() != 0)
8448 index += val1.const_value() - 1;
8449 break;
8450
8451 case DW_OP_call2:
8452 case DW_OP_call4:
8453 case DW_OP_call_ref:
8454 case DW_OP_nop:
8455 break;
8456
8457 default:
8458 return false;
8459 }
8460
8461 if (ctxt.stack.front().is_const())
8462 ctxt.accum = ctxt.stack.front();
8463
8464 next_index = index + 1;
8465 return true;
8466}
8467
8468/// This function quickly evaluates a DWARF expression that is a
8469/// constant.
8470///
8471/// This is a "fast path" function that quickly evaluates a DWARF
8472/// expression that is only made of a DW_OP_plus_uconst operator.
8473///
8474/// This is a sub-routine of die_member_offset.
8475///
8476/// @param expr the DWARF expression to evaluate.
8477///
8478/// @param expr_len the length of the expression @p expr.
8479///
8480/// @param value out parameter. This is set to the result of the
8481/// evaluation of @p expr, iff this function returns true.
8482///
8483/// @return true iff the evaluation of @p expr went OK.
8484static bool
8485eval_quickly(Dwarf_Op* expr,
8486 uint64_t expr_len,
8487 int64_t& value)
8488{
8489 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8490 {
8491 value = expr[0].number;
8492 return true;
8493 }
8494 return false;
8495}
8496
8497/// Evaluate the value of the last sub-expression that is a constant,
8498/// inside a given DWARF expression.
8499///
8500/// @param expr the DWARF expression to consider.
8501///
8502/// @param expr_len the length of the expression to consider.
8503///
8504/// @param value the resulting value of the last constant
8505/// sub-expression of the DWARF expression. This is set iff the
8506/// function returns true.
8507///
8508/// @param is_tls_address out parameter. This is set to true iff
8509/// the resulting value of the evaluation is a TLS (thread local
8510/// storage) address.
8511///
8512/// @param eval_ctxt the evaluation context to (re)use. Note that
8513/// this function initializes this context before using it.
8514///
8515/// @return true if the function could find a constant sub-expression
8516/// to evaluate, false otherwise.
8517static bool
8518eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8519 size_t expr_len,
8520 int64_t& value,
8521 bool& is_tls_address,
8522 dwarf_expr_eval_context &eval_ctxt)
8523{
8524 // Reset the evaluation context before evaluating the constant sub
8525 // expression contained in the DWARF expression 'expr'.
8526 eval_ctxt.reset();
8527
8528 size_t index = 0, next_index = 0;
8529 do
8530 {
8531 if (op_is_arith_logic(expr, expr_len, index,
8532 next_index, eval_ctxt)
8533 || op_pushes_constant_value(expr, expr_len, index,
8534 next_index, eval_ctxt)
8535 || op_manipulates_stack(expr, expr_len, index,
8536 next_index, eval_ctxt)
8537 || op_pushes_non_constant_value(expr, expr_len, index,
8538 next_index, eval_ctxt)
8539 || op_is_control_flow(expr, expr_len, index,
8540 next_index, eval_ctxt))
8541 ;
8542 else
8543 next_index = index + 1;
8544
8545 ABG_ASSERT(next_index > index);
8546 index = next_index;
8547 } while (index < expr_len);
8548
8549 is_tls_address = eval_ctxt.set_tls_address();
8550 if (eval_ctxt.accum.is_const())
8551 {
8552 value = eval_ctxt.accum;
8553 return true;
8554 }
8555 return false;
8556}
8557
8558/// Evaluate the value of the last sub-expression that is a constant,
8559/// inside a given DWARF expression.
8560///
8561/// @param expr the DWARF expression to consider.
8562///
8563/// @param expr_len the length of the expression to consider.
8564///
8565/// @param value the resulting value of the last constant
8566/// sub-expression of the DWARF expression. This is set iff the
8567/// function returns true.
8568///
8569/// @return true if the function could find a constant sub-expression
8570/// to evaluate, false otherwise.
8571static bool
8572eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8573 size_t expr_len,
8574 int64_t& value,
8575 bool& is_tls_address)
8576{
8577 dwarf_expr_eval_context eval_ctxt;
8578 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8579 is_tls_address, eval_ctxt);
8580}
8581
8582// -----------------------------------
8583// </location expression evaluation>
8584// -----------------------------------
8585
8586/// Convert a DW_AT_bit_offset attribute value into the same value as
8587/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8588///
8589/// On big endian machines, the value of the DW_AT_bit_offset
8590/// attribute + 8 * the value of the DW_AT_data_member_location
8591/// attribute is the same as the value of the DW_AT_data_bit_offset
8592/// attribute.
8593///
8594/// On little endian machines however, the situation is different.
8595/// The DW_AT_bit_offset value for a bit field is the number of bits
8596/// to the left of the most significant bit of the bit field, within
8597/// the integer value at DW_AT_data_member_location.
8598///
8599/// The DW_AT_data_bit_offset offset value is the number of bits to
8600/// the right of the least significant bit of the bit field, again
8601/// relative to the containing integer value.
8602///
8603/// In other words, DW_AT_data_bit_offset is what everybody would
8604/// instinctively think of as being the "offset of the bit field". 8 *
8605/// DW_AT_data_member_location + DW_AT_bit_offset however is very
8606/// counter-intuitive on little endian machines.
8607///
8608/// This function thus reads the value of a DW_AT_bit_offset property
8609/// of a DIE and converts it into what the DW_AT_data_bit_offset would
8610/// have been if it was present, ignoring the contribution of
8611/// DW_AT_data_member_location.
8612///
8613/// Note that DW_AT_bit_offset has been made obsolete starting from
8614/// DWARF5 (for GCC; Clang still emits it).
8615///
8616/// If you like coffee and it's not too late, now might be a good time
8617/// to have a coffee break. Otherwise if it's late at night, you
8618/// might want to consider an herbal tea break. Then come back to
8619/// read this.
8620///
8621///
8622/// In what follows, the bit fields are all contained within the first
8623/// whole int of the struct, so DW_AT_data_member_location is 0.
8624///
8625/// Okay, to have a better idea of what DW_AT_bit_offset and
8626/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8627/// have bit fields data members defined as:
8628///
8629/// struct S
8630/// {
8631/// int j:5;
8632/// int k:6;
8633/// int m:5;
8634/// int n:8;
8635/// };
8636///
8637/// The below wonderful (at least!) ASCII art sketch describes the
8638/// layout of the bitfields of 'struct S' on a little endian machine.
8639/// You need to read the sketch from the bottom-up.
8640///
8641/// So please scroll down to its bottom. Note how the 32 bits integer
8642/// word containing the bit fields is laid out with its least
8643/// significant bit starting on the right hand side, at index 0.
8644///
8645/// Then slowly scroll up starting from there, and take the time to
8646/// read each line and see how the bit fields are laid out and what
8647/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8648/// the bit fields.
8649///
8650/// DW_AT_bit_offset(n)
8651/// < - - - - - - >
8652/// | | n |
8653/// ^ ^< - - - - >^
8654/// DW_AT_data_bit_offset(n)
8655/// < - - - - - - - - - - - - - - - >
8656/// | |
8657/// ^ ^
8658/// DW_AT_bit_offset(m)
8659/// <--------------------------------->
8660/// | | m |
8661/// ^ ^< - >^
8662/// DW_AT_data_bit_offset(m)
8663/// < - - - - - - - - - - >
8664/// | |
8665/// ^ ^
8666/// DW_AT_bit_offset(k)
8667/// <-------------------------------------------->
8668/// | | k |
8669/// ^ ^< - - >^
8670/// DW_AT_data_bit_offset(k)
8671/// < - - - - >
8672/// | |
8673/// ^ ^
8674/// DW_AT_bit_offset(j)
8675/// <-------------------------------------------------------->
8676/// | |
8677/// ^ ^
8678/// n m k j
8679/// < - - - - - - > < - - - > < - - - - > < - - - >
8680///
8681/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8682/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8683/// 31 27 23 16 15 11 10 6 5 4 0
8684///
8685/// So, the different bit fields all fit in one 32 bits word, assuming
8686/// the bit fields are tightly packed.
8687///
8688/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8689/// on this little endian machine and let's see how it relates to
8690/// DW_AT_data_bit_offset of j.
8691///
8692/// DW_AT_bit_offset(j) would be equal to the number of bits from the
8693/// left of the 32 bits word (i.e from bit number 31) to the most
8694/// significant bit of the j bit field (i.e, bit number 4). Thus:
8695///
8696/// DW_AT_bit_offset(j) =
8697/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8698///
8699/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8700/// 32 bits word (i.e, bit number 0) to the lest significant bit of
8701/// the 'j' bit field (ie, bit number 0). Thus:
8702///
8703/// DW_AT_data_bit_offset(j) = 0.
8704///
8705/// More generally, we can notice that:
8706///
8707/// sizeof_in_bits(int) =
8708/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8709///
8710/// It follows that:
8711///
8712/// DW_AT_data_bit_offset(j) =
8713/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8714///
8715/// Thus:
8716///
8717/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8718///
8719/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8720/// from the right hand side of the word. It is what we would
8721/// intuitively think it is. DW_AT_bit_offset however is super
8722/// counter-intuitive, pfff.
8723///
8724/// Anyway, this general equation holds true for all bit fields.
8725///
8726/// Similarly, it follows that:
8727///
8728/// DW_AT_bit_offset(k) =
8729/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8730///
8731/// Thus:
8732/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8733///
8734///
8735/// Likewise:
8736///
8737/// DW_AT_bit_offset(m) =
8738/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8739///
8740///
8741/// Thus:
8742/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8743///
8744/// And:
8745///
8746///
8747/// Lastly:
8748///
8749/// DW_AT_bit_offset(n) =
8750/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8751///
8752/// Thus:
8753/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8754///
8755/// Luckily, the body of the function is much smaller than this
8756/// comment. Enjoy!
8757///
8758/// @param die the DIE to consider.
8759///
8760/// @param is_big_endian this is true iff the machine we are looking at
8761/// is big endian.
8762///
8763/// @param offset this is the output parameter into which the value of
8764/// the DW_AT_bit_offset is put, converted as if it was the value of
8765/// the DW_AT_data_bit_offset parameter, less the contribution of
8766/// DW_AT_data_member_location. This parameter is set iff the
8767/// function returns true.
8768///
8769/// @return true if DW_AT_bit_offset was found on @p die.
8770static bool
8771read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8772 bool is_big_endian,
8773 uint64_t &offset)
8774{
8775 uint64_t off = 0;
8776 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8777 return false;
8778
8779 if (is_big_endian)
8780 {
8781 offset = off;
8782 return true;
8783 }
8784
8785 // Okay, we are looking at a little endian machine. We need to
8786 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8787 // have been. To understand this, you really need to read the
8788 // preliminary comment of this function.
8789 uint64_t containing_anonymous_object_size = 0;
8790 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8791 containing_anonymous_object_size));
8792 containing_anonymous_object_size *= 8;
8793
8794 uint64_t bitfield_size = 0;
8795 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8796 bitfield_size));
8797
8798 // As noted in the the preliminary comment of this function if we
8799 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8800 // its DW_AT_bit_offset value, the equation is:
8801 //
8802 // DW_AT_data_bit_offset(k) =
8803 // sizeof_in_bits(containing_anonymous_object_size)
8804 // - DW_AT_data_bit_offset(k)
8805 // - sizeof_in_bits(k)
8806 offset = containing_anonymous_object_size - off - bitfield_size;
8807
8808 return true;
8809}
8810
8811/// Get the value of the DW_AT_data_member_location of the given DIE
8812/// attribute as an constant.
8813///
8814/// @param die the DIE to read the attribute from.
8815///
8816/// @param offset the attribute as a constant value. This is set iff
8817/// the function returns true.
8818///
8819/// @return true if the attribute exists and has a constant value. In
8820/// that case the offset is set to the value.
8821static bool
8822die_constant_data_member_location(const Dwarf_Die *die,
8823 int64_t& offset)
8824{
8825 if (!die)
8826 return false;
8827
8828 Dwarf_Attribute attr;
8829 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8830 DW_AT_data_member_location,
8831 &attr))
8832 return false;
8833
8834 Dwarf_Word val;
8835 if (dwarf_formudata(&attr, &val) != 0)
8836 return false;
8837
8838 offset = val;
8839 return true;
8840}
8841
8842/// Get the offset of a struct/class member as represented by the
8843/// value of the DW_AT_data_member_location attribute.
8844///
8845/// There is a huge gotcha in here. The value of the
8846/// DW_AT_data_member_location is not necessarily a constant that one
8847/// would just read and be done with it. Rather, it can be a DWARF
8848/// expression that one has to interpret. In general, the offset can
8849/// be given by the DW_AT_data_bit_offset or by the
8850/// DW_AT_data_member_location attribute and optionally the
8851/// DW_AT_bit_offset attribute. The bit offset attributes are
8852/// always simple constants, but the DW_AT_data_member_location
8853/// attribute is a DWARF location expression.
8854///
8855/// When it's the DW_AT_data_member_location that is present,
8856/// there are three cases to possibly take into account:
8857///
8858/// 1/ The offset in the vtable where the offset of a virtual base
8859/// can be found, aka vptr offset. Given the address of a
8860/// given object O, the vptr offset for B is given by the
8861/// (DWARF) expression:
8862///
8863/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8864///
8865/// where VIRTUAL_OFFSET is a constant value; In this case,
8866/// this function returns the constant VIRTUAL_OFFSET, as this
8867/// is enough to detect changes in a given virtual base
8868/// relative to the other virtual bases.
8869///
8870/// 2/ The offset of a regular data member. Given the address of
8871/// a struct object named O, the memory location for a
8872/// particular data member is given by the (DWARF) expression:
8873///
8874/// address(O) + OFFSET
8875///
8876/// where OFFSET is a constant. In this case, this function
8877/// returns the OFFSET constant.
8878///
8879/// 3/ The offset of a virtual member function in the virtual
8880/// pointer. The DWARF expression is a constant that designates
8881/// the offset of the function in the vtable. In this case this
8882/// function returns that constant.
8883///
8884/// @param rdr the DWARF reader to consider.
8885///
8886/// @param die the DIE to read the information from.
8887///
8888/// @param offset the resulting constant offset, in bits. This
8889/// argument is set iff the function returns true.
8890static bool
8891die_member_offset(const reader& rdr,
8892 const Dwarf_Die* die,
8893 int64_t& offset)
8894{
8895 Dwarf_Op* expr = NULL;
8896 size_t expr_len = 0;
8897 uint64_t bit_offset = 0;
8898
8899 // First let's see if the DW_AT_data_bit_offset attribute is
8900 // present.
8901 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8902 {
8903 offset = bit_offset;
8904 return true;
8905 }
8906
8907 // First try to read DW_AT_data_member_location as a plain constant.
8908 // We do this because the generic method using die_location_expr
8909 // might hit a bug in elfutils libdw dwarf_location_expression only
8910 // fixed in elfutils 0.184+. The bug only triggers if the attribute
8911 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8912 // handle all constants here because that is more consistent (and
8913 // slightly faster in the general case where the attribute isn't a
8914 // full DWARF expression).
8915 if (!die_constant_data_member_location(die, offset))
8916 {
8917 // Otherwise, let's see if the DW_AT_data_member_location
8918 // attribute and, optionally, the DW_AT_bit_offset attributes
8919 // are present.
8920 if (!die_location_expr(die, DW_AT_data_member_location,
8921 &expr, &expr_len))
8922 return false;
8923
8924 // The DW_AT_data_member_location attribute is present. Let's
8925 // evaluate it and get its constant sub-expression and return
8926 // that one.
8927 if (!eval_quickly(expr, expr_len, offset))
8928 {
8929 bool is_tls_address = false;
8930 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8931 offset, is_tls_address,
8932 rdr.dwarf_expr_eval_ctxt()))
8933 return false;
8934 }
8935 }
8936 offset *= 8;
8937
8938 // On little endian machines, we need to convert the
8939 // DW_AT_bit_offset attribute into a relative offset to 8 *
8940 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8941 // would be if it were used instead.
8942 //
8943 // In other words, before adding it to 8 *
8944 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
8945 // converted into a human-understandable form that represents the
8946 // offset of the bitfield data member it describes. For details
8947 // about the conversion, please read the extensive comments of
8948 // read_and_convert_DW_at_bit_offset.
8949 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
8950 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
8951 offset += bit_offset;
8952
8953 return true;
8954}
8955
8956/// Read the value of the DW_AT_location attribute from a DIE,
8957/// evaluate the resulting DWARF expression and, if it's a constant
8958/// expression, return it.
8959///
8960/// @param die the DIE to consider.
8961///
8962/// @param address the resulting constant address. This is set iff
8963/// the function returns true.
8964///
8965/// @return true iff the whole sequence of action described above
8966/// could be completed normally.
8967static bool
8968die_location_address(Dwarf_Die* die,
8969 Dwarf_Addr& address,
8970 bool& is_tls_address)
8971{
8972 Dwarf_Op* expr = NULL;
8973 size_t expr_len = 0;
8974
8975 is_tls_address = false;
8976
8977 if (!die)
8978 return false;
8979
8980 Dwarf_Attribute attr;
8981 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
8982 return false;
8983
8984 if (dwarf_getlocation(&attr, &expr, &expr_len))
8985 return false;
8986 // Ignore location expressions where reading them succeeded but
8987 // their length is 0.
8988 if (expr_len == 0)
8989 return false;
8990
8991 Dwarf_Attribute result;
8992 if (!dwarf_getlocation_attr(&attr, expr, &result))
8993 // A location that has been interpreted as an address.
8994 return !dwarf_formaddr(&result, &address);
8995
8996 // Just get the address out of the number field.
8997 address = expr->number;
8998 return true;
8999}
9000
9001/// Return the index of a function in its virtual table. That is,
9002/// return the value of the DW_AT_vtable_elem_location attribute.
9003///
9004/// @param die the DIE of the function to consider.
9005///
9006/// @param vindex the resulting index. This is set iff the function
9007/// returns true.
9008///
9009/// @return true if the DIE has a DW_AT_vtable_elem_location
9010/// attribute.
9011static bool
9012die_virtual_function_index(Dwarf_Die* die,
9013 int64_t& vindex)
9014{
9015 if (!die)
9016 return false;
9017
9018 Dwarf_Op* expr = NULL;
9019 size_t expr_len = 0;
9020 if (!die_location_expr(die, DW_AT_vtable_elem_location,
9021 &expr, &expr_len))
9022 return false;
9023
9024 int64_t i = 0;
9025 bool is_tls_addr = false;
9026 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
9027 return false;
9028
9029 vindex = i;
9030 return true;
9031}
9032
9033/// Test if a given DIE represents an anonymous type.
9034///
9035/// Anonymous types we are interested in are classes, unions and
9036/// enumerations.
9037///
9038/// @param die the DIE to consider.
9039///
9040/// @return true iff @p die represents an anonymous type.
9041bool
9043{
9044 int tag = dwarf_tag(die);
9045
9046 if (tag == DW_TAG_class_type
9047 || tag == DW_TAG_structure_type
9048 || tag == DW_TAG_union_type
9049 || tag == DW_TAG_enumeration_type)
9050 return die_is_anonymous(die);
9051
9052 return false;
9053}
9054
9055/// Return the base of the internal name to represent an anonymous
9056/// type.
9057///
9058/// Typically, anonymous enums would be named
9059/// __anonymous_enum__<number>, anonymous struct or classes would be
9060/// named __anonymous_struct__<number> and anonymous unions would be
9061/// named __anonymous_union__<number>. The first part of these
9062/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
9063/// the base name. This function returns that base name, depending on
9064/// the kind of type DIE we are looking at.
9065///
9066/// @param die the type DIE to look at. This function expects a type
9067/// DIE with an empty DW_AT_name property value (anonymous).
9068///
9069/// @return a string representing the base of the internal anonymous
9070/// name.
9071static string
9072get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
9073{
9074 ABG_ASSERT(die_is_type(die));
9075 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
9076
9077 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9078 string type_name;
9079 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
9081 else if (tag == DW_TAG_union_type)
9083 else if (tag == DW_TAG_enumeration_type)
9085
9086 return type_name;
9087}
9088
9089/// Build a full internal anonymous type name.
9090///
9091/// @param base_name this is the base name as returned by the function
9092/// @ref get_internal_anonymous_die_prefix_name.
9093///
9094/// @param anonymous_type_index this is the index of the anonymous
9095/// type in its scope. That is, if there are more than one anonymous
9096/// types of a given kind in a scope, this index is what tells them
9097/// appart, starting from 0.
9098///
9099/// @return the built string, which is a concatenation of @p base_name
9100/// and @p anonymous_type_index.
9101static string
9102build_internal_anonymous_die_name(const string &base_name,
9103 size_t anonymous_type_index)
9104{
9105 string name = base_name;
9106 if (anonymous_type_index && !base_name.empty())
9107 {
9108 std::ostringstream o;
9109 o << base_name << anonymous_type_index;
9110 name = o.str();
9111 }
9112 return name;
9113}
9114
9115
9116/// Build a full internal anonymous type name.
9117///
9118/// @param die the DIE representing the anonymous type to consider.
9119///
9120/// @param anonymous_type_index the index of the anonymous type
9121/// represented by @p DIE, in its scope. That is, if there are
9122/// several different anonymous types of the same kind as @p die, this
9123/// index is what tells them appart.
9124///
9125/// @return the internal name of the anonymous type represented by @p
9126/// DIE.
9127static string
9128get_internal_anonymous_die_name(Dwarf_Die *die,
9129 size_t anonymous_type_index)
9130{
9131 string name = get_internal_anonymous_die_prefix_name(die);
9132 name = build_internal_anonymous_die_name(name, anonymous_type_index);
9133 return name;
9134}
9135
9136// ------------------------------------
9137// <DIE pretty printer>
9138// ------------------------------------
9139
9140/// Compute the qualified name of a DIE that represents a type.
9141///
9142/// For instance, if the DIE tag is DW_TAG_subprogram then this
9143/// function computes the name of the function *type*.
9144///
9145/// @param rdr the DWARF reader.
9146///
9147/// @param die the DIE to consider.
9148///
9149/// @param where_offset where in the are logically are in the DIE
9150/// stream.
9151///
9152/// @return a copy of the qualified name of the type.
9153static string
9154die_qualified_type_name(const reader& rdr,
9155 const Dwarf_Die* die,
9156 size_t where_offset)
9157{
9158 if (!die)
9159 return "";
9160
9161 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9162 if (tag == DW_TAG_compile_unit
9163 || tag == DW_TAG_partial_unit
9164 || tag == DW_TAG_type_unit)
9165 return "";
9166
9167 string name = die_name(die);
9168
9169 Dwarf_Die scope_die;
9170 if (!get_scope_die(rdr, die, where_offset, scope_die))
9171 return "";
9172
9173 string parent_name = die_qualified_name(rdr, &scope_die, where_offset);
9174 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9175 string separator = colon_colon ? "::" : ".";
9176
9177 string repr;
9178
9179 switch (tag)
9180 {
9181 case DW_TAG_unspecified_type:
9182 break;
9183
9184 case DW_TAG_base_type:
9185 {
9187 if (parse_integral_type(name, int_type))
9188 repr = int_type;
9189 else
9190 repr = name;
9191 }
9192 break;
9193
9194 case DW_TAG_typedef:
9195 case DW_TAG_enumeration_type:
9196 case DW_TAG_structure_type:
9197 case DW_TAG_class_type:
9198 case DW_TAG_union_type:
9199 {
9200 if (name.empty())
9201 // TODO: handle cases where there are more than one
9202 // anonymous type of the same kind in the same scope. In
9203 // that case, their name must be built with the function
9204 // get_internal_anonymous_die_name or something of the same
9205 // kind.
9206 name = get_internal_anonymous_die_prefix_name(die);
9207
9208 ABG_ASSERT(!name.empty());
9209 repr = parent_name.empty() ? name : parent_name + separator + name;
9210 }
9211 break;
9212
9213 case DW_TAG_const_type:
9214 case DW_TAG_volatile_type:
9215 case DW_TAG_restrict_type:
9216 {
9217 Dwarf_Die underlying_type_die;
9218 bool has_underlying_type_die =
9219 die_die_attribute(die, DW_AT_type, underlying_type_die);
9220
9221 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9222 break;
9223
9224 if (tag == DW_TAG_const_type)
9225 {
9226 if (has_underlying_type_die
9227 && die_is_reference_type(&underlying_type_die))
9228 // A reference is always const. So, to lower false
9229 // positive reports in diff computations, we consider a
9230 // const reference just as a reference. But we need to
9231 // keep the qualified-ness of the type. So we introduce
9232 // a 'no-op' qualifier here. Please remember that this
9233 // has to be kept in sync with what is done in
9234 // get_name_of_qualified_type. So if you change this
9235 // here, you have to change that code there too.
9236 repr = "";
9237 else if (!has_underlying_type_die
9238 || die_is_void_type(&underlying_type_die))
9239 {
9240 repr = "void";
9241 break;
9242 }
9243 else
9244 repr = "const";
9245 }
9246 else if (tag == DW_TAG_volatile_type)
9247 repr = "volatile";
9248 else if (tag == DW_TAG_restrict_type)
9249 repr = "restrict";
9250 else
9252
9253 string underlying_type_repr;
9254 if (has_underlying_type_die)
9255 underlying_type_repr =
9256 die_qualified_type_name(rdr, &underlying_type_die, where_offset);
9257 else
9258 underlying_type_repr = "void";
9259
9260 if (underlying_type_repr.empty())
9261 repr.clear();
9262 else
9263 {
9264 if (has_underlying_type_die)
9265 {
9266 Dwarf_Die peeled;
9267 die_peel_qualified(&underlying_type_die, peeled);
9268 if (die_is_pointer_or_reference_type(&peeled))
9269 repr = underlying_type_repr + " " + repr;
9270 else
9271 repr += " " + underlying_type_repr;
9272 }
9273 else
9274 repr += " " + underlying_type_repr;
9275 }
9276 }
9277 break;
9278
9279 case DW_TAG_pointer_type:
9280 case DW_TAG_reference_type:
9281 case DW_TAG_rvalue_reference_type:
9282 {
9283 Dwarf_Die pointed_to_type_die;
9284 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
9285 {
9286 if (tag == DW_TAG_pointer_type)
9287 repr = "void*";
9288 break;
9289 }
9290
9291 if (die_is_unspecified(&pointed_to_type_die))
9292 break;
9293
9294 string pointed_type_repr =
9295 die_qualified_type_name(rdr, &pointed_to_type_die, where_offset);
9296
9297 repr = pointed_type_repr;
9298 if (repr.empty())
9299 break;
9300
9301 if (tag == DW_TAG_pointer_type)
9302 repr += "*";
9303 else if (tag == DW_TAG_reference_type)
9304 repr += "&";
9305 else if (tag == DW_TAG_rvalue_reference_type)
9306 repr += "&&";
9307 else
9309 }
9310 break;
9311
9312 case DW_TAG_subrange_type:
9313 {
9314 // In Ada, this one can be generated on its own, that is, not
9315 // as a sub-type of an array. So we need to support it on its
9316 // own. Note that when it's emitted as the sub-type of an
9317 // array like in C and C++, this is handled differently, for
9318 // now. But we try to make this usable by other languages
9319 // that are not Ada, even if we modelled it after Ada.
9320
9321 // So we build a subrange type for the sole purpose of using
9322 // the ::as_string() method of that type. So we don't add
9323 // that type to the current type tree being built.
9325 build_subrange_type(const_cast<reader&>(rdr),
9326 die, where_offset,
9327 /*associate_die_to_type=*/false);
9328 repr += s->as_string();
9329 break;
9330 }
9331
9332 case DW_TAG_array_type:
9333 {
9334 Dwarf_Die element_type_die;
9335 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9336 break;
9337 string element_type_name =
9338 die_qualified_type_name(rdr, &element_type_die, where_offset);
9339 if (element_type_name.empty())
9340 break;
9341
9343 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9344 die, subranges, where_offset,
9345 /*associate_type_to_die=*/false);
9346
9347 repr = element_type_name;
9348 repr += array_type_def::subrange_type::vector_as_string(subranges);
9349 }
9350 break;
9351
9352 case DW_TAG_subroutine_type:
9353 case DW_TAG_subprogram:
9354 {
9355 string return_type_name;
9356 string class_name;
9357 vector<string> parm_names;
9358 bool is_const = false;
9359 bool is_static = false;
9360
9361 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9362 /*pretty_print=*/true,
9363 return_type_name, class_name,
9364 parm_names, is_const,
9365 is_static);
9366 if (return_type_name.empty())
9367 return_type_name = "void";
9368
9369 repr = return_type_name;
9370
9371 if (!class_name.empty())
9372 {
9373 // This is a method, so print the class name.
9374 repr += " (" + class_name + "::*)";
9375 }
9376
9377 // Now parameters.
9378 repr += " (";
9379 for (vector<string>::const_iterator i = parm_names.begin();
9380 i != parm_names.end();
9381 ++i)
9382 {
9383 if (i != parm_names.begin())
9384 repr += ", ";
9385 repr += *i;
9386 }
9387 repr += ")";
9388
9389 }
9390 break;
9391
9392 case DW_TAG_string_type:
9393 case DW_TAG_ptr_to_member_type:
9394 case DW_TAG_set_type:
9395 case DW_TAG_file_type:
9396 case DW_TAG_packed_type:
9397 case DW_TAG_thrown_type:
9398 case DW_TAG_interface_type:
9399 case DW_TAG_shared_type:
9400 break;
9401 }
9402
9403 return repr;
9404}
9405
9406/// Compute the qualified name of a decl represented by a given DIE.
9407///
9408/// For instance, for a DIE of tag DW_TAG_subprogram this function
9409/// computes the signature of the function *declaration*.
9410///
9411/// @param rdr the DWARF reader.
9412///
9413/// @param die the DIE to consider.
9414///
9415/// @param where_offset where we are logically at in the DIE stream.
9416///
9417/// @return a copy of the computed name.
9418static string
9419die_qualified_decl_name(const reader& rdr,
9420 const Dwarf_Die* die,
9421 size_t where_offset)
9422{
9423 if (!die || !die_is_decl(die))
9424 return "";
9425
9426 string name = die_name(die);
9427
9428 Dwarf_Die scope_die;
9429 if (!get_scope_die(rdr, die, where_offset, scope_die))
9430 return "";
9431
9432 string scope_name = die_qualified_name(rdr, &scope_die, where_offset);
9433 string separator = "::";
9434
9435 string repr;
9436
9437 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9438 switch (tag)
9439 {
9440 case DW_TAG_namespace:
9441 case DW_TAG_member:
9442 case DW_TAG_variable:
9443 repr = scope_name.empty() ? name : scope_name + separator + name;
9444 break;
9445 case DW_TAG_subprogram:
9446 repr = die_function_signature(rdr, die, where_offset);
9447 break;
9448
9449 case DW_TAG_unspecified_parameters:
9450 repr = "...";
9451 break;
9452
9453 case DW_TAG_formal_parameter:
9454 case DW_TAG_imported_declaration:
9455 case DW_TAG_GNU_template_template_param:
9456 case DW_TAG_GNU_template_parameter_pack:
9457 case DW_TAG_GNU_formal_parameter_pack:
9458 break;
9459 }
9460 return repr;
9461}
9462
9463/// Compute the qualified name of the artifact represented by a given
9464/// DIE.
9465///
9466/// If the DIE represents a type, then the function computes the name
9467/// of the type. Otherwise, if the DIE represents a decl then the
9468/// function computes the name of the decl. Note that a DIE of tag
9469/// DW_TAG_subprogram is going to be considered as a "type" -- just
9470/// like if it was a DW_TAG_subroutine_type.
9471///
9472/// @param rdr the DWARF reader.
9473///
9474/// @param die the DIE to consider.
9475///
9476/// @param where_offset where we are logically at in the DIE stream.
9477///
9478/// @return a copy of the computed name.
9479static string
9480die_qualified_name(const reader& rdr, const Dwarf_Die* die, size_t where)
9481{
9482 if (die_is_type(die))
9483 return die_qualified_type_name(rdr, die, where);
9484 else if (die_is_decl(die))
9485 return die_qualified_decl_name(rdr, die, where);
9486 return "";
9487}
9488
9489/// Test if the qualified name of a given type should be empty.
9490///
9491/// The reason why the name of a DIE with a given tag would be empty
9492/// is that libabigail's internal representation doesn't yet support
9493/// that tag; or if the DIE's qualified name is built from names of
9494/// sub-types DIEs whose tags are not yet supported.
9495///
9496/// @param rdr the DWARF reader.
9497///
9498/// @param die the DIE to consider.
9499///
9500/// @param where where we are logically at, in the DIE stream.
9501///
9502/// @param qualified_name the qualified name of the DIE. This is set
9503/// only iff the function returns false.
9504///
9505/// @return true if the qualified name of the DIE is empty.
9506static bool
9507die_qualified_type_name_empty(const reader& rdr,
9508 const Dwarf_Die* die,
9509 size_t where, string &qualified_name)
9510{
9511 if (!die)
9512 return true;
9513
9514 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9515
9516 string qname;
9517 if (tag == DW_TAG_typedef
9518 || tag == DW_TAG_pointer_type
9519 || tag == DW_TAG_reference_type
9520 || tag == DW_TAG_rvalue_reference_type
9521 || tag == DW_TAG_array_type
9522 || tag == DW_TAG_const_type
9523 || tag == DW_TAG_volatile_type
9524 || tag == DW_TAG_restrict_type)
9525 {
9526 Dwarf_Die underlying_type_die;
9527 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9528 {
9529 string name =
9530 die_qualified_type_name(rdr, &underlying_type_die, where);
9531 if (name.empty())
9532 return true;
9533 }
9534 }
9535 else
9536 {
9537 string name = die_qualified_type_name(rdr, die, where);
9538 if (name.empty())
9539 return true;
9540 }
9541
9542 qname = die_qualified_type_name(rdr, die, where);
9543 if (qname.empty())
9544 return true;
9545
9546 qualified_name = qname;
9547 return false;
9548}
9549
9550/// Given the DIE that represents a function type, compute the names
9551/// of the following properties the function's type:
9552///
9553/// - return type
9554/// - enclosing class (if the function is a member function)
9555/// - function parameter types
9556///
9557/// When the function we are looking at is a member function, it also
9558/// tells if it's const.
9559///
9560/// @param rdr the DWARF reader.
9561///
9562/// @param die the DIE of the function or function type we are looking
9563/// at.
9564///
9565/// @param where_offset where we are logically at in the DIE stream.
9566///
9567/// @param pretty_print if set to yes, the type names are going to be
9568/// pretty-printed names; otherwise, they are just qualified type
9569/// names.
9570///
9571/// @param return_type_name out parameter. This contains the name of
9572/// the return type of the function.
9573///
9574/// @param class_name out parameter. If the function is a member
9575/// function, this contains the name of the enclosing class.
9576///
9577/// @param parm_names out parameter. This vector is set to the names
9578/// of the types of the parameters of the function.
9579///
9580/// @param is_const out parameter. If the function is a member
9581/// function, this is set to true iff the member function is const.
9582///
9583/// @param is_static out parameter. If the function is a static
9584/// member function, then this is set to true.
9585static void
9586die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9587 const Dwarf_Die* die,
9588 size_t where_offset,
9589 bool pretty_print,
9590 string &return_type_name,
9591 string &class_name,
9592 vector<string>& parm_names,
9593 bool& is_const,
9594 bool& is_static)
9595{
9596 Dwarf_Die child;
9597 Dwarf_Die ret_type_die;
9598 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9599 return_type_name = "void";
9600 else
9601 return_type_name =
9602 pretty_print
9603 ? rdr.get_die_pretty_representation(&ret_type_die, where_offset)
9604 : rdr.get_die_qualified_type_name(&ret_type_die, where_offset);
9605
9606 if (return_type_name.empty())
9607 return_type_name = "void";
9608
9609 Dwarf_Die object_pointer_die, class_die;
9610 bool is_method_type =
9611 die_function_type_is_method_type(rdr, die, where_offset,
9612 object_pointer_die,
9613 class_die, is_static);
9614
9615 is_const = false;
9616 if (is_method_type)
9617 {
9618 class_name = rdr.get_die_qualified_type_name(&class_die, where_offset);
9619
9620 Dwarf_Die this_pointer_die;
9621 Dwarf_Die pointed_to_die;
9622 if (!is_static
9623 && die_die_attribute(&object_pointer_die, DW_AT_type,
9624 this_pointer_die))
9625 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9626 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9627 is_const = true;
9628
9629 string fn_name = die_name(die);
9630 string non_qualified_class_name = die_name(&class_die);
9631 bool is_ctor = fn_name == non_qualified_class_name;
9632 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9633
9634 if (is_ctor || is_dtor)
9635 return_type_name.clear();
9636 }
9637
9638 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9639 do
9640 {
9641 int child_tag = dwarf_tag(&child);
9642 if (child_tag == DW_TAG_formal_parameter)
9643 {
9644 Dwarf_Die parm_type_die;
9645 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9646 continue;
9647 string qualified_name =
9648 pretty_print
9649 ? rdr.get_die_pretty_representation(&parm_type_die, where_offset)
9650 : rdr.get_die_qualified_type_name(&parm_type_die, where_offset);
9651
9652 if (qualified_name.empty())
9653 continue;
9654 parm_names.push_back(qualified_name);
9655 }
9656 else if (child_tag == DW_TAG_unspecified_parameters)
9657 {
9658 // This is a variadic function parameter.
9659 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
9660 // After a DW_TAG_unspecified_parameters tag, we shouldn't
9661 // keep reading for parameters. The
9662 // unspecified_parameters TAG should be the last parameter
9663 // that we record. For instance, if there are multiple
9664 // DW_TAG_unspecified_parameters DIEs then we should care
9665 // only for the first one.
9666 break;
9667 }
9668 }
9669 while (dwarf_siblingof(&child, &child) == 0);
9670
9671 if (class_name.empty())
9672 {
9673 Dwarf_Die parent_die;
9674 if (get_parent_die(rdr, die, parent_die, where_offset))
9675 {
9676 if (die_is_class_type(&parent_die))
9677 class_name =
9678 rdr.get_die_qualified_type_name(&parent_die, where_offset);
9679 }
9680 }
9681}
9682
9683/// This computes the signature of the a function declaration
9684/// represented by a DIE.
9685///
9686/// @param rdr the DWARF reader.
9687///
9688/// @param fn_die the DIE of the function to consider.
9689///
9690/// @param where_offset where we are logically at in the stream of
9691/// DIEs.
9692///
9693/// @return a copy of the computed function signature string.
9694static string
9695die_function_signature(const reader& rdr,
9696 const Dwarf_Die *fn_die,
9697 size_t where_offset)
9698{
9699
9701 bool has_lang = false;
9702 if ((has_lang = rdr.get_die_language(fn_die, lang)))
9703 {
9704 // In a binary originating from the C language, it's OK to use
9705 // the linkage name of the function as a key for the map which
9706 // is meant to reduce the number of DIE comparisons involved
9707 // during DIE canonicalization computation.
9708 if (is_c_language(lang))
9709 {
9710 string fn_name = die_linkage_name(fn_die);
9711 if (fn_name.empty())
9712 fn_name = die_name(fn_die);
9713 return fn_name;
9714 }
9715 }
9716
9717 // TODO: When we can structurally compare DIEs originating from C++
9718 // as well, we can use the linkage name of functions in C++ too, to
9719 // reduce the number of comparisons involved during DIE
9720 // canonicalization.
9721
9722 string return_type_name;
9723 Dwarf_Die ret_type_die;
9724 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9725 return_type_name = rdr.get_die_qualified_type_name(&ret_type_die,
9726 where_offset);
9727
9728 if (return_type_name.empty())
9729 return_type_name = "void";
9730
9731 Dwarf_Die scope_die;
9732 string scope_name;
9733 if (get_scope_die(rdr, fn_die, where_offset, scope_die))
9734 scope_name = rdr.get_die_qualified_name(&scope_die, where_offset);
9735 string fn_name = die_name(fn_die);
9736 if (!scope_name.empty())
9737 fn_name = scope_name + "::" + fn_name;
9738
9739 string class_name;
9740 vector<string> parm_names;
9741 bool is_const = false;
9742 bool is_static = false;
9743
9744 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_offset,
9745 /*pretty_print=*/false,
9746 return_type_name, class_name,
9747 parm_names, is_const, is_static);
9748
9749 bool is_virtual = die_is_virtual(fn_die);
9750
9751 string repr = class_name.empty() ? "function" : "method";
9752 if (is_virtual)
9753 repr += " virtual";
9754
9755 if (!return_type_name.empty())
9756 repr += " " + return_type_name;
9757
9758 repr += " " + fn_name;
9759
9760 // Now parameters.
9761 repr += "(";
9762 bool some_parm_emitted = false;
9763 for (vector<string>::const_iterator i = parm_names.begin();
9764 i != parm_names.end();
9765 ++i)
9766 {
9767 if (i != parm_names.begin())
9768 {
9769 if (some_parm_emitted)
9770 repr += ", ";
9771 }
9772 else
9773 if (!is_static && !class_name.empty())
9774 // We are printing a non-static method name, skip the implicit "this"
9775 // parameter type.
9776 continue;
9777 repr += *i;
9778 some_parm_emitted = true;
9779 }
9780 repr += ")";
9781
9782 if (is_const)
9783 {
9784 ABG_ASSERT(!class_name.empty());
9785 repr += " const";
9786 }
9787
9788 return repr;
9789}
9790
9791/// Return a pretty string representation of a type, for internal purposes.
9792///
9793/// By internal purpose, we mean things like key-ing types for lookup
9794/// purposes and so on.
9795///
9796/// Note that this function is also used to pretty print functions.
9797/// For functions, it prints the *type* of the function.
9798///
9799/// @param rdr the context to use.
9800///
9801/// @param the DIE of the type to pretty print.
9802///
9803/// @param where_offset where we logically are placed when calling
9804/// this. It's useful to handle inclusion of DW_TAG_compile_unit
9805/// entries.
9806///
9807/// @return the resulting pretty representation.
9808static string
9809die_pretty_print_type(reader& rdr,
9810 const Dwarf_Die* die,
9811 size_t where_offset)
9812{
9813 if (!die
9814 || (!die_is_type(die)
9815 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
9816 return "";
9817
9818 string repr;
9819
9820 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9821 switch (tag)
9822 {
9823 case DW_TAG_string_type:
9824 // For now, we won't try to go get the actual representation of
9825 // the string because this would make things more complicated;
9826 // for that we'd need to interpret some location expressions to
9827 // get the length of the string. And for dynamically allocated
9828 // strings, the result of the location expression evaluation
9829 // might not even be a constant. So at the moment I consider
9830 // this to be a lot of hassle for no great return. Until proven
9831 // otherwise, of course.
9832 repr = "string type";
9833
9834 case DW_TAG_unspecified_type:
9835 case DW_TAG_ptr_to_member_type:
9836 break;
9837
9838 case DW_TAG_namespace:
9839 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_offset);
9840 break;
9841
9842 case DW_TAG_base_type:
9843 repr = rdr.get_die_qualified_type_name(die, where_offset);
9844 break;
9845
9846 case DW_TAG_typedef:
9847 {
9848 string qualified_name;
9849 if (!die_qualified_type_name_empty(rdr, die,
9850 where_offset,
9851 qualified_name))
9852 repr = "typedef " + qualified_name;
9853 }
9854 break;
9855
9856 case DW_TAG_const_type:
9857 case DW_TAG_volatile_type:
9858 case DW_TAG_restrict_type:
9859 case DW_TAG_pointer_type:
9860 case DW_TAG_reference_type:
9861 case DW_TAG_rvalue_reference_type:
9862 repr = rdr.get_die_qualified_type_name(die, where_offset);
9863 break;
9864
9865 case DW_TAG_enumeration_type:
9866 {
9867 string qualified_name =
9868 rdr.get_die_qualified_type_name(die, where_offset);
9869 repr = "enum " + qualified_name;
9870 }
9871 break;
9872
9873 case DW_TAG_structure_type:
9874 case DW_TAG_class_type:
9875 {
9876 string qualified_name =
9877 rdr.get_die_qualified_type_name(die, where_offset);
9878 repr = "class " + qualified_name;
9879 }
9880 break;
9881
9882 case DW_TAG_union_type:
9883 {
9884 string qualified_name =
9885 rdr.get_die_qualified_type_name(die, where_offset);
9886 repr = "union " + qualified_name;
9887 }
9888 break;
9889
9890 case DW_TAG_array_type:
9891 {
9892 Dwarf_Die element_type_die;
9893 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9894 break;
9895 string element_type_name =
9896 rdr.get_die_qualified_type_name(&element_type_die, where_offset);
9897 if (element_type_name.empty())
9898 break;
9899
9901 build_subranges_from_array_type_die(rdr, die, subranges, where_offset,
9902 /*associate_type_to_die=*/false);
9903
9904 repr = element_type_name;
9905 repr += array_type_def::subrange_type::vector_as_string(subranges);
9906 }
9907 break;
9908
9909 case DW_TAG_subrange_type:
9910 {
9911 // So this can be generated by Ada, on its own; that is, not
9912 // as a subtype of an array. In that case we need to handle
9913 // it properly.
9914
9915 // For now, we consider that the pretty printed name of the
9916 // subrange type is its name. We might need something more
9917 // advance, should the needs of the users get more
9918 // complicated.
9919 repr += die_qualified_type_name(rdr, die, where_offset);
9920 }
9921 break;
9922
9923 case DW_TAG_subroutine_type:
9924 case DW_TAG_subprogram:
9925 {
9926 string return_type_name;
9927 string class_name;
9928 vector<string> parm_names;
9929 bool is_const = false;
9930 bool is_static = false;
9931
9932 die_return_and_parm_names_from_fn_type_die(rdr, die, where_offset,
9933 /*pretty_print=*/true,
9934 return_type_name, class_name,
9935 parm_names, is_const,
9936 is_static);
9937 if (class_name.empty())
9938 repr = "function type";
9939 else
9940 repr = "method type";
9941 repr += " " + rdr.get_die_qualified_type_name(die, where_offset);
9942 }
9943 break;
9944
9945 case DW_TAG_set_type:
9946 case DW_TAG_file_type:
9947 case DW_TAG_packed_type:
9948 case DW_TAG_thrown_type:
9949 case DW_TAG_interface_type:
9950 case DW_TAG_shared_type:
9952 }
9953
9954 return repr;
9955}
9956
9957/// Return a pretty string representation of a declaration, for
9958/// internal purposes.
9959///
9960/// By internal purpose, we mean things like key-ing declarations for
9961/// lookup purposes and so on.
9962///
9963/// Note that this function is also used to pretty print functions.
9964/// For functions, it prints the signature of the function.
9965///
9966/// @param rdr the context to use.
9967///
9968/// @param the DIE of the declaration to pretty print.
9969///
9970/// @param where_offset where we logically are placed when calling
9971/// this. It's useful to handle inclusion of DW_TAG_compile_unit
9972/// entries.
9973///
9974/// @return the resulting pretty representation.
9975static string
9976die_pretty_print_decl(reader& rdr,
9977 const Dwarf_Die* die,
9978 size_t where_offset)
9979{
9980 if (!die || !die_is_decl(die))
9981 return "";
9982
9983 string repr;
9984
9985 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9986 switch (tag)
9987 {
9988 case DW_TAG_namespace:
9989 repr = "namespace " + die_qualified_name(rdr, die, where_offset);
9990 break;
9991
9992 case DW_TAG_member:
9993 case DW_TAG_variable:
9994 {
9995 string type_repr = "void";
9996 Dwarf_Die type_die;
9997 if (die_die_attribute(die, DW_AT_type, type_die))
9998 type_repr = die_qualified_type_name(rdr, &type_die, where_offset);
9999 repr = die_qualified_name(rdr, die, where_offset);
10000 if (!repr.empty())
10001 repr = type_repr + " " + repr;
10002 }
10003 break;
10004
10005 case DW_TAG_subprogram:
10006 repr = die_function_signature(rdr, die, where_offset);
10007 break;
10008
10009 default:
10010 break;
10011 }
10012 return repr;
10013}
10014
10015/// Compute the pretty printed representation of an artifact
10016/// represented by a DIE.
10017///
10018/// If the DIE is a type, compute the its pretty representation as a
10019/// type; otherwise, if it's a declaration, compute its pretty
10020/// representation as a declaration. Note for For instance, that a
10021/// DW_TAG_subprogram DIE is going to be represented as a function
10022/// *type*.
10023///
10024/// @param rdr the DWARF reader.
10025///
10026/// @param die the DIE to consider.
10027///
10028/// @param where_offset we in the DIE stream we are logically at.
10029///
10030/// @return a copy of the pretty printed artifact.
10031static string
10032die_pretty_print(reader& rdr, const Dwarf_Die* die, size_t where_offset)
10033{
10034 if (die_is_type(die))
10035 return die_pretty_print_type(rdr, die, where_offset);
10036 else if (die_is_decl(die))
10037 return die_pretty_print_decl(rdr, die, where_offset);
10038 return "";
10039}
10040
10041// -----------------------------------
10042// </die pretty printer>
10043// -----------------------------------
10044
10045
10046// ----------------------------------
10047// <die comparison engine>
10048// ---------------------------------
10049
10050/// Compares two decls DIEs
10051///
10052/// This works only for DIEs emitted by the C language.
10053///
10054/// This implementation doesn't yet support namespaces.
10055///
10056/// This is a subroutine of compare_dies.
10057///
10058/// @return true iff @p l equals @p r.
10059static bool
10060compare_as_decl_dies(const Dwarf_Die *l, const Dwarf_Die *r)
10061{
10062 ABG_ASSERT(l && r);
10063
10064 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10065 int r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10066 if (l_tag != r_tag)
10067 return false;
10068
10069 bool result = false;
10070
10071 if (l_tag == DW_TAG_subprogram || l_tag == DW_TAG_variable)
10072 {
10073 // Fast path for functions and global variables.
10074 if (compare_dies_string_attribute_value(l, r, DW_AT_linkage_name,
10075 result)
10076 || compare_dies_string_attribute_value(l, r, DW_AT_MIPS_linkage_name,
10077 result))
10078 {
10079 if (!result)
10080 return false;
10081 }
10082
10083 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10084 result))
10085 {
10086 if (!result)
10087 return false;
10088 }
10089 return true;
10090 }
10091
10092 // Fast path for types.
10093 if (compare_dies_string_attribute_value(l, r, DW_AT_name,
10094 result))
10095 return result;
10096 return true;
10097}
10098
10099/// Test if at least one of two ODR-relevant DIEs is decl-only.
10100///
10101/// @param rdr the DWARF reader to consider.
10102///
10103/// @param l the first type DIE to consider.
10104///
10105/// @param r the second type DIE to consider.
10106///
10107/// @return true iff either @p l or @p r is decl-only and both are
10108/// ODR-relevant.
10109static bool
10110at_least_one_decl_only_among_odr_relevant_dies(const reader &rdr,
10111 const Dwarf_Die *l,
10112 const Dwarf_Die *r)
10113{
10114 if (!(rdr.odr_is_relevant(l) && rdr.odr_is_relevant(r)))
10115 return false;
10116
10117 if ((die_is_declaration_only(l) && die_has_no_child(l))
10118 || (die_is_declaration_only(r) && die_has_no_child(r)))
10119 return true;
10120 return false;
10121}
10122
10123/// Compares two type DIEs
10124///
10125/// This is a subroutine of compare_dies.
10126///
10127/// Note that this function doesn't look at the name of the DIEs.
10128/// Naming is taken into account by the function compare_as_decl_dies.
10129///
10130/// If the two DIEs are from a translation unit that is subject to the
10131/// ONE Definition Rule, then the function considers that if one DIE
10132/// is a declaration, then it's equivalent to the second. In that
10133/// case, the sizes of the two DIEs are not compared. This is so that
10134/// a declaration of a type compares equal to the definition of the
10135/// type.
10136///
10137/// @param rdr the DWARF reader to consider.
10138///
10139/// @param l the left operand of the comparison operator.
10140///
10141/// @param r the right operand of the comparison operator.
10142///
10143/// @return true iff @p l equals @p r.
10144static bool
10145compare_as_type_dies(const reader& rdr,
10146 const Dwarf_Die *l,
10147 const Dwarf_Die *r)
10148{
10149 ABG_ASSERT(l && r);
10150 ABG_ASSERT(die_is_type(l));
10151 ABG_ASSERT(die_is_type(r));
10152
10153 if (dwarf_tag(const_cast<Dwarf_Die*>(l)) == DW_TAG_string_type
10154 && dwarf_tag(const_cast<Dwarf_Die*>(r)) == DW_TAG_string_type
10155 && (dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10156 != dwarf_dieoffset(const_cast<Dwarf_Die*>(r))))
10157 // For now, we cannot compare DW_TAG_string_type because of its
10158 // string_length attribute that is a location descriptor that is
10159 // not necessarily a constant. So it's super hard to evaluate it
10160 // in a libabigail context. So for now, we just say that all
10161 // DW_TAG_string_type DIEs are different, by default.
10162 return false;
10163
10164 if (at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10165 // A declaration of a type compares equal to the definition of the
10166 // type.
10167 return true;
10168
10169 uint64_t l_size = 0, r_size = 0;
10170 die_size_in_bits(l, l_size);
10171 die_size_in_bits(r, r_size);
10172
10173 return l_size == r_size;
10174}
10175
10176/// Compare two DIEs as decls (looking as their names etc) and as
10177/// types (looking at their size etc).
10178///
10179/// @param rdr the DWARF reader to consider.
10180///
10181/// @param l the first DIE to consider.
10182///
10183/// @param r the second DIE to consider.
10184///
10185/// @return TRUE iff @p l equals @p r as far as naming and size is
10186/// concerned.
10187static bool
10188compare_as_decl_and_type_dies(const reader &rdr,
10189 const Dwarf_Die *l,
10190 const Dwarf_Die *r)
10191{
10192 if (!compare_as_decl_dies(l, r)
10193 || !compare_as_type_dies(rdr, l, r))
10194 return false;
10195
10196 return true;
10197}
10198
10199/// Test if two DIEs representing function declarations have the same
10200/// linkage name, and thus are considered equal if they are C or C++,
10201/// because the two DIEs represent functions in the same binary.
10202///
10203/// If the DIEs don't have a linkage name, the function compares their
10204/// name. But in that case, the caller of the function must know that
10205/// in C++ for instance, that doesn't imply that the two functions are
10206/// equal.
10207///
10208/// @param rdr the @ref reader to consider.
10209///
10210/// @param l the first function DIE to consider.
10211///
10212/// @param r the second function DIE to consider.
10213///
10214/// @return true iff the function represented by @p l have the same
10215/// linkage name as the function represented by @p r.
10216static bool
10217fn_die_equal_by_linkage_name(const reader &rdr,
10218 const Dwarf_Die *l,
10219 const Dwarf_Die *r)
10220{
10221 if (!!l != !!r)
10222 return false;
10223
10224 if (!l)
10225 return false;
10226
10227 int tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10228 ABG_ASSERT(tag == DW_TAG_subprogram);
10229 tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10230 ABG_ASSERT(tag == DW_TAG_subprogram);
10231
10232 string lname = die_name(l), rname = die_name(r);
10233 string llinkage_name = die_linkage_name(l),
10234 rlinkage_name = die_linkage_name(r);
10235
10236 if (rdr.die_is_in_c_or_cplusplus(l)
10237 && rdr.die_is_in_c_or_cplusplus(r))
10238 {
10239 if (!llinkage_name.empty() && !rlinkage_name.empty())
10240 return llinkage_name == rlinkage_name;
10241 else if (!!llinkage_name.empty() != !!rlinkage_name.empty())
10242 return false;
10243 else
10244 return lname == rname;
10245 }
10246
10247 return (!llinkage_name.empty()
10248 && !rlinkage_name.empty()
10249 && llinkage_name == rlinkage_name);
10250}
10251
10252/// Compare two DIEs in the context of DIE canonicalization.
10253///
10254/// If DIE canonicalization is on, the function compares the DIEs
10255/// canonically and structurally. The two types of comparison should
10256/// be equal, of course.
10257///
10258/// @param rdr the DWARF reader.
10259///
10260/// @param l_offset the offset of the first canonical DIE to compare.
10261///
10262/// @param r_offset the offset of the second canonical DIE to compare.
10263///
10264/// @param l_die_source the source of the DIE denoted by the offset @p
10265/// l_offset.
10266///
10267/// @param r_die_source the source of the DIE denoted by the offset @p
10268/// r_offset.
10269///
10270/// @param l_has_canonical_die_offset output parameter. Is set to
10271/// true if @p l_offset has a canonical DIE.
10272///
10273/// @param r_has_canonical_die_offset output parameter. Is set to
10274/// true if @p r_offset has a canonical DIE.
10275///
10276/// @param l_canonical_die_offset output parameter. If @p
10277/// l_has_canonical_die_offset is set to true, then this parameter is
10278/// set to the offset of the canonical DIE of the DIE designated by @p
10279/// l_offset.
10280static bool
10281try_canonical_die_comparison(const reader& rdr,
10282 Dwarf_Off l_offset, Dwarf_Off r_offset,
10283 die_source l_die_source, die_source r_die_source,
10284 bool& l_has_canonical_die_offset,
10285 bool& r_has_canonical_die_offset,
10286 Dwarf_Off& l_canonical_die_offset,
10287 Dwarf_Off& r_canonical_die_offset,
10288 bool& result)
10289{
10290#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10291 if (rdr.debug_die_canonicalization_is_on_
10292 && !rdr.use_canonical_die_comparison_)
10293 return false;
10294#endif
10295
10296
10297 l_has_canonical_die_offset =
10298 (l_canonical_die_offset =
10299 rdr.get_canonical_die_offset(l_offset, l_die_source,
10300 /*die_as_type=*/true));
10301
10302 r_has_canonical_die_offset =
10303 (r_canonical_die_offset =
10304 rdr.get_canonical_die_offset(r_offset, r_die_source,
10305 /*die_as_type=*/true));
10306
10307 if (l_has_canonical_die_offset && r_has_canonical_die_offset)
10308 {
10309 result = (l_canonical_die_offset == r_canonical_die_offset);
10310 return true;
10311 }
10312
10313 return false;
10314}
10315
10316#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
10317/// This function is called whenever a DIE comparison fails.
10318///
10319/// This function is intended for debugging purposes. The idea is for
10320/// hackers to set a breakpoint on this function so that they can
10321/// discover why exactly the comparison failed. They then can execute
10322/// the program from compare_dies_during_canonicalization, for
10323/// instance.
10324///
10325/// @param @l the left-hand side of the DIE comparison.
10326///
10327/// @param @r the right-hand side of the DIE comparison.
10328static void
10329notify_die_comparison_failed(const Dwarf_Die* /*l*/, const Dwarf_Die* /*r*/)
10330{
10331}
10332
10333#define NOTIFY_DIE_COMPARISON_FAILED(l, r) \
10334 notify_die_comparison_failed(l, r)
10335#else
10336#define NOTIFY_DIE_COMPARISON_FAILED(l, r)
10337#endif
10338
10339/// A macro used to return from DIE comparison routines.
10340///
10341/// If the return value is false, the macro invokes the
10342/// notify_die_comparison_failed signalling function before returning.
10343/// That way, hackers willing to learn more about why the comparison
10344/// routine returned "false" can just set a breakpoint on
10345/// notify_die_comparison_failed and execute the program from
10346/// compare_dies_during_canonicalization, for instance.
10347///
10348/// @param value the value to return from the DIE comparison routines.
10349#define ABG_RETURN(value) \
10350 do \
10351 { \
10352 if ((value) == COMPARISON_RESULT_DIFFERENT) \
10353 { \
10354 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10355 } \
10356 return return_comparison_result(l, r, dies_being_compared, \
10357 value, aggregates_being_compared, \
10358 update_canonical_dies_on_the_fly); \
10359 } \
10360 while(false)
10361
10362/// A macro used to return the "false" boolean from DIE comparison
10363/// routines.
10364///
10365/// As the return value is false, the macro invokes the
10366/// notify_die_comparison_failed signalling function before returning.
10367///
10368/// @param value the value to return from the DIE comparison routines.
10369#define ABG_RETURN_FALSE \
10370 do \
10371 { \
10372 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10373 return return_comparison_result(l, r, dies_being_compared, \
10374 COMPARISON_RESULT_DIFFERENT, \
10375 aggregates_being_compared, \
10376 update_canonical_dies_on_the_fly); \
10377 } while(false)
10378
10379/// A macro to set the 'result' variable to 'false'.
10380///
10381/// The macro invokes the notify_die_comparison_failed function so
10382/// that the hacker can set a debugging breakpoint on
10383/// notify_die_comparison_failed to know where a DIE comparison failed
10384/// during compare_dies_during_canonicalization for instance.
10385///
10386/// @param result the 'result' variable to set.
10387///
10388/// @param l the first DIE of the comparison operation.
10389///
10390/// @param r the second DIE of the comparison operation.
10391#define SET_RESULT_TO_FALSE(result, l , r) \
10392 do \
10393 { \
10394 result = COMPARISON_RESULT_DIFFERENT; \
10395 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10396 } while(false)
10397
10398/// A macro to set the 'result' variable to a given value.
10399///
10400/// If the value equals to COMPARISON_RESULT_DIFFERENT, then the macro
10401/// invokes the notify_die_comparison_failed function so that the
10402/// hacker can set a debugging breakpoint on
10403/// notify_die_comparison_failed to know where a DIE comparison failed
10404/// during compare_dies_during_canonicalization for instance.
10405///
10406/// @param result the 'result' variable to set.
10407///
10408/// @param l the first DIE of the comparison operation.
10409///
10410/// @param r the second DIE of the comparison operation.
10411#define SET_RESULT_TO(result, value, l , r) \
10412 do \
10413 { \
10414 result = (value); \
10415 if (result == COMPARISON_RESULT_DIFFERENT) \
10416 { \
10417 NOTIFY_DIE_COMPARISON_FAILED(l, r); \
10418 } \
10419 } while(false)
10420
10421#define RETURN_IF_COMPARISON_CYCLE_DETECTED \
10422 do \
10423 { \
10424 if (aggregates_being_compared.contains(dies_being_compared)) \
10425 { \
10426 result = COMPARISON_RESULT_CYCLE_DETECTED; \
10427 aggregates_being_compared.record_redundant_type_die_pair(dies_being_compared); \
10428 ABG_RETURN(result); \
10429 } \
10430 } \
10431 while(false)
10432
10433/// Get the next member sibling of a given class or union member DIE.
10434///
10435/// @param die the DIE to consider.
10436///
10437/// @param member out parameter. This is set to the next member
10438/// sibling, iff the function returns TRUE.
10439///
10440/// @return TRUE iff the function set @p member to the next member
10441/// sibling DIE.
10442static bool
10443get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10444{
10445 if (!die)
10446 return false;
10447
10448 bool found_member = false;
10449 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10450 member) == 0);
10451 found_member;
10452 found_member = (dwarf_siblingof(member, member) == 0))
10453 {
10454 int tag = dwarf_tag(member);
10455 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10456 break;
10457 }
10458
10459 return found_member;
10460}
10461
10462/// Get the first child DIE of a class/struct/union DIE that is a
10463/// member DIE.
10464///
10465/// @param die the DIE to consider.
10466///
10467/// @param child out parameter. This is set to the first child DIE of
10468/// @p iff this function returns TRUE.
10469///
10470/// @return TRUE iff @p child is set to the first child DIE of @p die
10471/// that is a member DIE.
10472static bool
10473get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10474{
10475 if (!die)
10476 return false;
10477
10478 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10479 ABG_ASSERT(tag == DW_TAG_structure_type
10480 || tag == DW_TAG_union_type
10481 || tag == DW_TAG_class_type);
10482
10483 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die),
10484 child) == 0);
10485
10486 if (!found_child)
10487 return false;
10488
10489 tag = dwarf_tag(child);
10490
10491 if (!(tag == DW_TAG_member
10492 || tag == DW_TAG_inheritance
10493 || tag == DW_TAG_subprogram))
10494 found_child = get_next_member_sibling_die(child, child);
10495
10496 return found_child;
10497}
10498
10499/// This is a sub-routine of return_comparison_result.
10500///
10501/// Propagate the canonical type of a the right-hand-side DIE to the
10502/// lef-hand-side DIE. This is a optimization that is done when the
10503/// two DIEs compare equal.
10504///
10505/// If the right-hand-side DIE is not canonicalized, the function
10506/// performs its canonicalization.
10507///
10508/// This optimization is performed only if
10509/// is_canon_type_to_be_propagated_tag returns true.
10510///
10511/// @param rdr the current context to consider.
10512///
10513/// @param l the left-hand-side DIE of the comparison. It's going to
10514/// receive the canonical type of the other DIE.
10515///
10516/// @param r the right-hand-side DIE of the comparison. Its canonical
10517/// type is propagated to @p l.
10518static void
10519maybe_propagate_canonical_type(const reader& rdr,
10520 const Dwarf_Die* l,
10521 const Dwarf_Die* r)
10522{
10523 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10524 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10525
10526 if (l_tag != r_tag)
10527 return;
10528
10529 if (is_canon_type_to_be_propagated_tag(l_tag))
10530 propagate_canonical_type(rdr, l, r);
10531}
10532
10533/// Propagate the canonical type of a the right-hand-side DIE to the
10534/// left-hand-side DIE. This is a optimization that is done when the
10535/// two DIEs compare equal.
10536///
10537/// If the right-hand-side DIE is not canonicalized, the function
10538/// performs its canonicalization.
10539///
10540/// @param rdr the current context to consider.
10541///
10542/// @param l the left-hand-side DIE of the comparison. It's going to
10543/// receive the canonical type of the other DIE.
10544///
10545/// @param r the right-hand-side DIE of the comparison. Its canonical
10546/// type is propagated to @p l.
10547static void
10548propagate_canonical_type(const reader& rdr,
10549 const Dwarf_Die* l,
10550 const Dwarf_Die* r)
10551{
10552 ABG_ASSERT(l && r);
10553
10554 // If 'l' has no canonical DIE and if 'r' has one, then propagage
10555 // the canonical DIE of 'r' to 'l'.
10556 //
10557 // In case 'r' has no canonical DIE, then compute it, and then
10558 // propagate that canonical DIE to 'r'.
10559 const die_source l_source = rdr.get_die_source(l);
10560 const die_source r_source = rdr.get_die_source(r);
10561
10562 Dwarf_Off l_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(l));
10563 Dwarf_Off r_offset = dwarf_dieoffset(const_cast<Dwarf_Die*>(r));
10564 bool l_has_canonical_die_offset = false;
10565 bool r_has_canonical_die_offset = false;
10566 Dwarf_Off l_canonical_die_offset = 0;
10567 Dwarf_Off r_canonical_die_offset = 0;
10568
10569 l_has_canonical_die_offset =
10570 (l_canonical_die_offset =
10571 rdr.get_canonical_die_offset(l_offset, l_source,
10572 /*die_as_type=*/true));
10573
10574 r_has_canonical_die_offset =
10575 (r_canonical_die_offset =
10576 rdr.get_canonical_die_offset(r_offset, r_source,
10577 /*die_as_type=*/true));
10578
10579
10580 if (!l_has_canonical_die_offset
10581 && r_has_canonical_die_offset
10582 // A DIE can be equivalent only to another DIE of the same
10583 // source.
10584 && l_source == r_source)
10585 {
10586 ABG_ASSERT(r_canonical_die_offset);
10587 rdr.set_canonical_die_offset(l, r_canonical_die_offset,
10588 /*die_as_type=*/true);
10589 offset_type l_off = {l_source, l_offset}, r_off = {r_source, r_offset};
10590 rdr.propagated_types_.insert(std::make_pair(l_off,r_off));
10591 rdr.canonical_propagated_count_++;
10592 }
10593}
10594
10595/// This function does the book keeping of comparison pairs necessary
10596/// to handle
10597///
10598/// * the detection of cycles during the comparison of aggregate
10599/// types, in conjuction with the macro
10600/// RETURN_IF_COMPARISON_CYCLE_DETECTED
10601///
10602/// * the handling of the canonical type propagation optimisation
10603/// to speed-up type canonicalization.
10604///
10605///
10606/// Note that this function is essentially a sub-routine of
10607/// compare_dies.
10608///
10609/// @param l the left-hand-side DIE being compared.
10610///
10611/// @param r the right-hand-side DIE being compared.
10612///
10613/// @param cur_dies the pair of die offsets of l and r. This is
10614/// redundant as it can been computed from @p l and @p r. However,
10615/// getting it as an argument is an optimization to avoid computing it
10616/// over and over again, given how often this function is invoked from
10617/// compare_dies.
10618///
10619/// @param return the result of comparing @p l against @p r.
10620///
10621/// @param comparison_stack the stack of pair of type DIEs being
10622/// compared.
10623///
10624/// @param do_propagate_canonical_type if true then the function
10625/// performs canonical DIEs propagation, meaning that if @p l equals
10626/// @p r and if @p r has a canonical type, then the canonical type of
10627/// @p l is set to the canonical type of @p r.
10628static comparison_result
10629return_comparison_result(const Dwarf_Die* l,
10630 const Dwarf_Die* r,
10631 const offset_pair_type& cur_dies,
10632 comparison_result result,
10633 offset_pairs_stack_type& comparison_stack,
10634 bool do_propagate_canonical_type = true)
10635{
10636 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l));
10637
10638 if (result == COMPARISON_RESULT_EQUAL)
10639 {
10640 // The result comparing the two types is "true", basically. So
10641 // let's propagate the canonical type of r onto l, so that we
10642 // don't need to compute the canonical type of r.
10643 if (do_propagate_canonical_type)
10644 {
10645 // Propagate canonical type.
10646 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10647
10648 // TODO: do we need to confirm any tentative canonical
10649 // propagation?
10650 }
10651 }
10652 else if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10653 {
10654 // So upon detection of the comparison cycle, compare_dies
10655 // returned early with the comparison result
10656 // COMPARISON_RESULT_CYCLE_DETECTED, signalling us that we must
10657 // carry on with the comparison of all the OTHER sub-types of
10658 // the redundant type. If they all compare equal, then it means
10659 // the redundant type pair compared equal. Otherwise, it
10660 // compared different.
10661 //ABG_ASSERT(comparison_stack.contains(l_offset, r_offset));
10662 // Let's fall through to let the end of this function set the
10663 // result to COMPARISON_RESULT_UNKNOWN;
10664 }
10665 else if (result == COMPARISON_RESULT_UNKNOWN)
10666 {
10667 // Here is an introductory comment describing what we are going
10668 // to do in this case where the result of the comparison of the
10669 // current pair of type is not "false", basically.
10670 //
10671 // This means that we don't yet know what the result of
10672 // comparing these two types is, because one of the sub-types of
10673 // the types being compared is "redundant", meaning it appears
10674 // more than once in the comparison stack, so if we were to
10675 // naively try to carry on with the comparison member-wise, we'd
10676 // end up with an endless loop, a.k.a "comparison cycle".
10677 //
10678 // If the current type pair is redundant then:
10679 //
10680 // * This is a redundant type that has just been fully
10681 // compared. In that case, all the types that depend on
10682 // this redundant type and that have been tentatively
10683 // canonical-type-propagated must see their canonical types
10684 // "confirmed". This means that this type is going to be
10685 // considered as not being redundant anymore, meaning all
10686 // the types that depend on it must be updated as not being
10687 // dependant on it anymore, and the type itsef must be
10688 // removed from the map of redundant types.
10689 //
10690 // After the type's canonical-type-propagation is confirmed,
10691 // the result of its comparison must also be changed into
10692 // COMPARISON_RESULT_EQUAL.
10693 //
10694 // After that, If the current type depends on a redundant type,
10695 // then propagate its canonical type AND track it as having its
10696 // type being canonical-type-propagated.
10697 //
10698 // If the current type is not redundant however, then it must be
10699 // dependant on a redundant type. If it's not dependant on a
10700 // redundant type, then it must be of those types which
10701 // comparisons are not tracked for cycle, probably because they
10702 // are not aggregates. Otherwise, ABORT to understand why. I
10703 // believe this should not happen. In any case, after that
10704 // safety check is passed, we just need to return at this point.
10705
10706 if (comparison_stack.is_redundant(cur_dies)
10707 && comparison_stack.vect_.back() == cur_dies)
10708 {
10709 // We are in the case described above of a redundant type
10710 // that has been fully compared.
10711 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10712 comparison_stack.confirm_canonical_propagated_type(cur_dies);
10713
10714 result = COMPARISON_RESULT_EQUAL;
10715 }
10716 else if (is_canon_type_to_be_propagated_tag(l_tag)
10717 && comparison_stack.vect_.back() == cur_dies)
10718 {
10719 // The current type is not redundant. So, as described in
10720 // the introductory comment above, it must be dependant on a
10721 // redundant type.
10722 ABG_ASSERT(comparison_stack.depends_on_redundant_types(cur_dies));
10723 maybe_propagate_canonical_type(comparison_stack.rdr_, l, r);
10724 // Then pass through.
10725 }
10726 }
10727 else if (result == COMPARISON_RESULT_DIFFERENT)
10728 {
10729 // Here is an introductory comment describing what we are going
10730 // to do in this case where the result of the comparison of the
10731 // current pair of type is "false", basically.
10732 //
10733 // If the type pair {l,r} is redundant then cancel the
10734 // canonical-type-propagation of all the dependant pairs that
10735 // depends on this redundant {l, r}. This means walk the types
10736 // that depends on {l, r} and cancel their
10737 // canonical-propagate-type, that means remove their canonical
10738 // types and mark them as not being canonically-propagated.
10739 // Also, erase their cached comparison results that was likely
10740 // set to COMPARISON_RESULT_UNKNOWN.
10741 //
10742 // Also, update the cached result for this pair, that was likely
10743 // to be COMPARISON_RESULT_UNKNOWN.
10744 if (comparison_stack.is_redundant(cur_dies)
10745 && comparison_stack.vect_.back() == cur_dies)
10746 comparison_stack.cancel_canonical_propagated_type(cur_dies);
10747 }
10748 else
10749 {
10750 // We should never reach here.
10752 }
10753
10754 if (result == COMPARISON_RESULT_CYCLE_DETECTED)
10755 result = COMPARISON_RESULT_UNKNOWN;
10756 else if (is_canon_type_to_be_propagated_tag(l_tag)
10757 && !comparison_stack.vect_.empty()
10758 && comparison_stack.vect_.back() == cur_dies)
10759 //Finally pop the pair types being compared from comparison_stack
10760 //iff {l,r} is on the top of the stack. If it's not, then it means
10761 //we are looking at a type that was detected as a being redundant
10762 //and thus hasn't been pushed to the stack yet gain.
10763 comparison_stack.erase(cur_dies);
10764
10765 maybe_cache_type_comparison_result(comparison_stack.rdr_,
10766 l_tag, cur_dies, result);
10767
10768 return result;
10769}
10770
10771/// Compare two DIEs emitted by a C compiler.
10772///
10773/// @param rdr the DWARF reader used to load the DWARF information.
10774///
10775/// @param l the left-hand-side argument of this comparison operator.
10776///
10777/// @param r the righ-hand-side argument of this comparison operator.
10778///
10779/// @param aggregates_being_compared this holds the names of the set
10780/// of aggregates being compared. It's used by the comparison
10781/// function to avoid recursing infinitely when faced with types
10782/// referencing themselves through pointers or references. By
10783/// default, just pass an empty instance of @ref istring_set_type to
10784/// it.
10785///
10786/// @param update_canonical_dies_on_the_fly if true, when two
10787/// sub-types compare equal (during the comparison of @p l and @p r)
10788/// update their canonical type. That way, two types of the same name
10789/// are structurally compared to each other only once. So the
10790/// non-linear structural comparison of two types of the same name
10791/// only happen once.
10792///
10793/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
10794static comparison_result
10795compare_dies(const reader& rdr,
10796 const Dwarf_Die *l, const Dwarf_Die *r,
10797 offset_pairs_stack_type& aggregates_being_compared,
10798 bool update_canonical_dies_on_the_fly)
10799{
10800 ABG_ASSERT(l);
10801 ABG_ASSERT(r);
10802
10803 const die_source l_die_source = rdr.get_die_source(l);
10804 const die_source r_die_source = rdr.get_die_source(r);
10805
10806 offset_type l_offset =
10807 {
10808 l_die_source,
10809 dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
10810 };
10811
10812 offset_type r_offset =
10813 {
10814 r_die_source,
10815 dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
10816 };
10817
10818 offset_pair_type dies_being_compared(l_offset, r_offset);
10819
10820 int l_tag = dwarf_tag(const_cast<Dwarf_Die*>(l)),
10821 r_tag = dwarf_tag(const_cast<Dwarf_Die*>(r));
10822
10823 if (l_tag != r_tag)
10825
10826 if (l_offset == r_offset)
10827 return COMPARISON_RESULT_EQUAL;
10828
10829 if (rdr.leverage_dwarf_factorization()
10830 && (l_die_source == ALT_DEBUG_INFO_DIE_SOURCE
10831 && r_die_source == ALT_DEBUG_INFO_DIE_SOURCE))
10832 if (l_offset != r_offset)
10833 return COMPARISON_RESULT_DIFFERENT;
10834
10835 comparison_result result = COMPARISON_RESULT_EQUAL;
10836 if (maybe_get_cached_type_comparison_result(rdr, l_tag,
10837 dies_being_compared,
10838 result))
10839 return result;
10840
10841 Dwarf_Off l_canonical_die_offset = 0, r_canonical_die_offset = 0;
10842 bool l_has_canonical_die_offset = false, r_has_canonical_die_offset = false;
10843
10844 // If 'l' and 'r' already have canonical DIEs, then just compare the
10845 // offsets of their canonical DIEs.
10846 if (is_type_die_to_be_canonicalized(l) && is_type_die_to_be_canonicalized(r))
10847 {
10848 bool canonical_compare_result = false;
10849 if (try_canonical_die_comparison(rdr, l_offset, r_offset,
10850 l_die_source, r_die_source,
10851 l_has_canonical_die_offset,
10852 r_has_canonical_die_offset,
10853 l_canonical_die_offset,
10854 r_canonical_die_offset,
10855 canonical_compare_result))
10856 {
10857 comparison_result result;
10858 SET_RESULT_TO(result,
10859 (canonical_compare_result
10860 ? COMPARISON_RESULT_EQUAL
10861 : COMPARISON_RESULT_DIFFERENT),
10862 l, r);
10863 return result;
10864 }
10865 }
10866
10867
10868
10869 switch (l_tag)
10870 {
10871 case DW_TAG_base_type:
10872 case DW_TAG_string_type:
10873 case DW_TAG_unspecified_type:
10874 if (!compare_as_decl_and_type_dies(rdr, l, r))
10875 SET_RESULT_TO_FALSE(result, l, r);
10876 break;
10877
10878 case DW_TAG_typedef:
10879 case DW_TAG_pointer_type:
10880 case DW_TAG_reference_type:
10881 case DW_TAG_rvalue_reference_type:
10882 case DW_TAG_const_type:
10883 case DW_TAG_volatile_type:
10884 case DW_TAG_restrict_type:
10885 {
10886 if (!compare_as_type_dies(rdr, l, r))
10887 {
10888 SET_RESULT_TO_FALSE(result, l, r);
10889 break;
10890 }
10891
10892 bool from_the_same_tu = false;
10893 if (!pointer_or_qual_die_of_anonymous_class_type(l)
10894 && compare_dies_cu_decl_file(l, r, from_the_same_tu)
10895 && from_the_same_tu)
10896 {
10897 // These two typedefs, pointer, reference, or qualified
10898 // types have the same name and are defined in the same TU.
10899 // They thus ought to be the same.
10900 //
10901 // Note that pointers, reference or qualified types to
10902 // anonymous types are not taking into account here because
10903 // those always need to be structurally compared.
10904 SET_RESULT_TO_FALSE(result, l, r);
10905 break;
10906 }
10907 }
10908
10909 {
10910 // No fancy optimization in this case. We need to
10911 // structurally compare the two DIEs.
10912 Dwarf_Die lu_type_die, ru_type_die;
10913 bool lu_is_void, ru_is_void;
10914
10915 lu_is_void = !die_die_attribute(l, DW_AT_type, lu_type_die);
10916 ru_is_void = !die_die_attribute(r, DW_AT_type, ru_type_die);
10917
10918 if (lu_is_void && ru_is_void)
10919 result = COMPARISON_RESULT_EQUAL;
10920 else if (lu_is_void != ru_is_void)
10921 SET_RESULT_TO_FALSE(result, l, r);
10922 else
10923 result = compare_dies(rdr, &lu_type_die, &ru_type_die,
10924 aggregates_being_compared,
10925 update_canonical_dies_on_the_fly);
10926 }
10927 break;
10928
10929 case DW_TAG_enumeration_type:
10930 if (!compare_as_decl_and_type_dies(rdr, l, r))
10931 SET_RESULT_TO_FALSE(result, l, r);
10932 else
10933 {
10934 // Walk the enumerators.
10935 Dwarf_Die l_enumtor, r_enumtor;
10936 bool found_l_enumtor = true, found_r_enumtor = true;
10937
10938 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10939 for (found_l_enumtor = dwarf_child(const_cast<Dwarf_Die*>(l),
10940 &l_enumtor) == 0,
10941 found_r_enumtor = dwarf_child(const_cast<Dwarf_Die*>(r),
10942 &r_enumtor) == 0;
10943 found_l_enumtor && found_r_enumtor;
10944 found_l_enumtor = dwarf_siblingof(&l_enumtor, &l_enumtor) == 0,
10945 found_r_enumtor = dwarf_siblingof(&r_enumtor, &r_enumtor) == 0)
10946 {
10947 int l_tag = dwarf_tag(&l_enumtor), r_tag = dwarf_tag(&r_enumtor);
10948 if ( l_tag != r_tag)
10949 {
10950 SET_RESULT_TO_FALSE(result, l, r);
10951 break;
10952 }
10953
10954 if (l_tag != DW_TAG_enumerator)
10955 continue;
10956
10957 uint64_t l_val = 0, r_val = 0;
10958 die_unsigned_constant_attribute(&l_enumtor,
10959 DW_AT_const_value,
10960 l_val);
10961 die_unsigned_constant_attribute(&r_enumtor,
10962 DW_AT_const_value,
10963 r_val);
10964 if (l_val != r_val)
10965 {
10966 SET_RESULT_TO_FALSE(result, l, r);
10967 break;
10968 }
10969 }
10970 if (found_l_enumtor != found_r_enumtor )
10971 SET_RESULT_TO_FALSE(result, l, r);
10972 }
10973 break;
10974
10975 case DW_TAG_structure_type:
10976 case DW_TAG_union_type:
10977 case DW_TAG_class_type:
10978 {
10979 RETURN_IF_COMPARISON_CYCLE_DETECTED;
10980
10981 rdr.compare_count_++;
10982
10983 if (!compare_as_decl_and_type_dies(rdr, l, r))
10984 SET_RESULT_TO_FALSE(result, l, r);
10985 else if (rdr.options().assume_odr_for_cplusplus
10986 && rdr.odr_is_relevant(l)
10987 && rdr.odr_is_relevant(r)
10988 && !die_is_anonymous(l)
10989 && !die_is_anonymous(r))
10990 result = COMPARISON_RESULT_EQUAL;
10991 else
10992 {
10993 aggregates_being_compared.add(dies_being_compared);
10994
10995 Dwarf_Die l_member, r_member;
10996 bool found_l_member = true, found_r_member = true;
10997
10998 if (!at_least_one_decl_only_among_odr_relevant_dies(rdr, l, r))
10999 for (found_l_member = get_member_child_die(l, &l_member),
11000 found_r_member = get_member_child_die(r, &r_member);
11001 found_l_member && found_r_member;
11002 found_l_member = get_next_member_sibling_die(&l_member,
11003 &l_member),
11004 found_r_member = get_next_member_sibling_die(&r_member,
11005 &r_member))
11006 {
11007 int l_tag = dwarf_tag(&l_member),
11008 r_tag = dwarf_tag(&r_member);
11009
11010 if (l_tag != r_tag)
11011 {
11012 SET_RESULT_TO_FALSE(result, l, r);
11013 break;
11014 }
11015
11016 ABG_ASSERT(l_tag == DW_TAG_member
11017 || l_tag == DW_TAG_variable
11018 || l_tag == DW_TAG_inheritance
11019 || l_tag == DW_TAG_subprogram);
11020
11021 comparison_result local_result =
11022 compare_dies(rdr, &l_member, &r_member,
11023 aggregates_being_compared,
11024 update_canonical_dies_on_the_fly);
11025
11026 if (local_result == COMPARISON_RESULT_UNKNOWN)
11027 // Note that if the result of comparing any
11028 // sub-type is COMPARISON_RESULT_EQUAL, just
11029 // because we have at least one sub-type's
11030 // comparison being COMPARISON_RESULT_UNKNOWN
11031 // means that the comparison of this type will
11032 // return COMPARISON_RESULT_UNKNOWN to show
11033 // callers that this type (and all the types that
11034 // depend on it) depends on a redundant type
11035 result = local_result;
11036
11037 if (local_result == COMPARISON_RESULT_DIFFERENT)
11038 {
11039 SET_RESULT_TO_FALSE(result, l, r);
11040 break;
11041 }
11042 }
11043 if (found_l_member != found_r_member)
11044 {
11045 SET_RESULT_TO_FALSE(result, l, r);
11046 break;
11047 }
11048 }
11049 }
11050 break;
11051
11052 case DW_TAG_array_type:
11053 {
11054 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11055
11056 aggregates_being_compared.add(dies_being_compared);
11057
11058 rdr.compare_count_++;
11059
11060 Dwarf_Die l_child, r_child;
11061 bool found_l_child, found_r_child;
11062 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11063 &l_child) == 0,
11064 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11065 &r_child) == 0;
11066 found_l_child && found_r_child;
11067 found_l_child = dwarf_siblingof(&l_child, &l_child) == 0,
11068 found_r_child = dwarf_siblingof(&r_child, &r_child) == 0)
11069 {
11070 int l_child_tag = dwarf_tag(&l_child),
11071 r_child_tag = dwarf_tag(&r_child);
11072 if (l_child_tag == DW_TAG_subrange_type
11073 || r_child_tag == DW_TAG_subrange_type)
11074 {
11075 result = compare_dies(rdr, &l_child, &r_child,
11076 aggregates_being_compared,
11077 update_canonical_dies_on_the_fly);
11078 if (!result)
11079 {
11080 SET_RESULT_TO_FALSE(result, l, r);
11081 break;
11082 }
11083 }
11084 }
11085 if (found_l_child != found_r_child)
11086 SET_RESULT_TO_FALSE(result, l, r);
11087 // Compare the types of the elements of the array.
11088 Dwarf_Die ltype_die, rtype_die;
11089 bool found_ltype = die_die_attribute(l, DW_AT_type, ltype_die);
11090 bool found_rtype = die_die_attribute(r, DW_AT_type, rtype_die);
11091 ABG_ASSERT(found_ltype && found_rtype);
11092
11093 result = compare_dies(rdr, &ltype_die, &rtype_die,
11094 aggregates_being_compared,
11095 update_canonical_dies_on_the_fly);
11096 if (!result)
11098 }
11099 break;
11100
11101 case DW_TAG_subrange_type:
11102 {
11103 uint64_t l_lower_bound = 0, r_lower_bound = 0,
11104 l_upper_bound = 0, r_upper_bound = 0;
11105 bool l_lower_bound_set = false, r_lower_bound_set = false,
11106 l_upper_bound_set = false, r_upper_bound_set = false;
11107
11108 l_lower_bound_set =
11109 die_unsigned_constant_attribute(l, DW_AT_lower_bound, l_lower_bound);
11110 r_lower_bound_set =
11111 die_unsigned_constant_attribute(r, DW_AT_lower_bound, r_lower_bound);
11112
11113 if (!die_unsigned_constant_attribute(l, DW_AT_upper_bound,
11114 l_upper_bound))
11115 {
11116 uint64_t l_count = 0;
11117 if (die_unsigned_constant_attribute(l, DW_AT_count, l_count))
11118 {
11119 l_upper_bound = l_lower_bound + l_count;
11120 l_upper_bound_set = true;
11121 if (l_upper_bound)
11122 --l_upper_bound;
11123 }
11124 }
11125 else
11126 l_upper_bound_set = true;
11127
11128 if (!die_unsigned_constant_attribute(r, DW_AT_upper_bound,
11129 r_upper_bound))
11130 {
11131 uint64_t r_count = 0;
11132 if (die_unsigned_constant_attribute(l, DW_AT_count, r_count))
11133 {
11134 r_upper_bound = r_lower_bound + r_count;
11135 r_upper_bound_set = true;
11136 if (r_upper_bound)
11137 --r_upper_bound;
11138 }
11139 }
11140 else
11141 r_upper_bound_set = true;
11142
11143 if ((l_lower_bound_set != r_lower_bound_set)
11144 || (l_upper_bound_set != r_upper_bound_set)
11145 || (l_lower_bound != r_lower_bound)
11146 || (l_upper_bound != r_upper_bound))
11147 SET_RESULT_TO_FALSE(result, l, r);
11148 }
11149 break;
11150
11151 case DW_TAG_subroutine_type:
11152 case DW_TAG_subprogram:
11153 {
11154 RETURN_IF_COMPARISON_CYCLE_DETECTED;
11155
11156 aggregates_being_compared.add(dies_being_compared);
11157
11158 rdr.compare_count_++;
11159
11160 if (l_tag == DW_TAG_subprogram
11161 && !fn_die_equal_by_linkage_name(rdr, l, r))
11162 {
11163 SET_RESULT_TO_FALSE(result, l, r);
11164 break;
11165 }
11166 else if (l_tag == DW_TAG_subprogram
11167 && rdr.die_is_in_c(l) && rdr.die_is_in_c(r)
11168 /*&& fn_die_equal_by_linkage_name(rdr, l, r)*/)
11169 {
11170 result = COMPARISON_RESULT_EQUAL;
11171 break;
11172 }
11173 else if (!rdr.die_is_in_c(l) && !rdr.die_is_in_c(r))
11174 {
11175 // In C, we cannot have two different functions with the
11176 // same linkage name in a given binary. But here we are
11177 // looking at DIEs that don't originate from C. So we
11178 // need to compare return types and parameter types.
11179 Dwarf_Die l_return_type, r_return_type;
11180 bool l_return_type_is_void = !die_die_attribute(l, DW_AT_type,
11181 l_return_type);
11182 bool r_return_type_is_void = !die_die_attribute(r, DW_AT_type,
11183 r_return_type);
11184 if (l_return_type_is_void != r_return_type_is_void
11185 || (!l_return_type_is_void
11186 && !compare_dies(rdr,
11187 &l_return_type, &r_return_type,
11188 aggregates_being_compared,
11189 update_canonical_dies_on_the_fly)))
11190 SET_RESULT_TO_FALSE(result, l, r);
11191 else
11192 {
11193 Dwarf_Die l_child, r_child;
11194 bool found_l_child, found_r_child;
11195 for (found_l_child = dwarf_child(const_cast<Dwarf_Die*>(l),
11196 &l_child) == 0,
11197 found_r_child = dwarf_child(const_cast<Dwarf_Die*>(r),
11198 &r_child) == 0;
11199 found_l_child && found_r_child;
11200 found_l_child = dwarf_siblingof(&l_child,
11201 &l_child) == 0,
11202 found_r_child = dwarf_siblingof(&r_child,
11203 &r_child)==0)
11204 {
11205 int l_child_tag = dwarf_tag(&l_child);
11206 int r_child_tag = dwarf_tag(&r_child);
11207 comparison_result local_result =
11208 COMPARISON_RESULT_EQUAL;
11209 if (l_child_tag != r_child_tag)
11210 local_result = COMPARISON_RESULT_DIFFERENT;
11211 if (l_child_tag == DW_TAG_formal_parameter)
11212 local_result =
11213 compare_dies(rdr, &l_child, &r_child,
11214 aggregates_being_compared,
11215 update_canonical_dies_on_the_fly);
11216 if (local_result == COMPARISON_RESULT_DIFFERENT)
11217 {
11218 result = local_result;
11219 SET_RESULT_TO_FALSE(result, l, r);
11220 break;
11221 }
11222 if (local_result == COMPARISON_RESULT_UNKNOWN)
11223 // Note that if the result of comparing any
11224 // sub-type is COMPARISON_RESULT_EQUAL, just
11225 // because we have at least one sub-type's
11226 // comparison being COMPARISON_RESULT_UNKNOWN
11227 // means that the comparison of this type will
11228 // return COMPARISON_RESULT_UNKNOWN to show
11229 // callers that this type (and all the types
11230 // that depend on it) depends on a redundant
11231 // type and so, can't be
11232 // canonical-type-propagated.
11233 result = local_result;
11234 }
11235 if (found_l_child != found_r_child)
11236 {
11237 SET_RESULT_TO_FALSE(result, l, r);
11238 break;
11239 }
11240 }
11241 }
11242 }
11243 break;
11244
11245 case DW_TAG_formal_parameter:
11246 {
11247 Dwarf_Die l_type, r_type;
11248 bool l_type_is_void = !die_die_attribute(l, DW_AT_type, l_type);
11249 bool r_type_is_void = !die_die_attribute(r, DW_AT_type, r_type);
11250 if (l_type_is_void != r_type_is_void)
11251 SET_RESULT_TO_FALSE(result, l, r);
11252 else if (!l_type_is_void)
11253 {
11254 comparison_result local_result =
11255 compare_dies(rdr, &l_type, &r_type,
11256 aggregates_being_compared,
11257 update_canonical_dies_on_the_fly);
11258 SET_RESULT_TO(result, local_result, l, r);
11259 }
11260 }
11261 break;
11262
11263 case DW_TAG_variable:
11264 case DW_TAG_member:
11265 if (compare_as_decl_dies(l, r))
11266 {
11267 // Compare the offsets of the data members
11268 if (l_tag == DW_TAG_member)
11269 {
11270 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11271 die_member_offset(rdr, l, l_offset_in_bits);
11272 die_member_offset(rdr, r, r_offset_in_bits);
11273 if (l_offset_in_bits != r_offset_in_bits)
11274 SET_RESULT_TO_FALSE(result, l, r);
11275 }
11276 if (result)
11277 {
11278 // Compare the types of the data members or variables.
11279 Dwarf_Die l_type, r_type;
11280 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11281 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11282 comparison_result local_result =
11283 compare_dies(rdr, &l_type, &r_type,
11284 aggregates_being_compared,
11285 update_canonical_dies_on_the_fly);
11286 SET_RESULT_TO(result, local_result, l, r);
11287 }
11288 }
11289 else
11290 SET_RESULT_TO_FALSE(result, l, r);
11291 break;
11292
11293 case DW_TAG_inheritance:
11294 {
11295 Dwarf_Die l_type, r_type;
11296 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11297 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11298 result = compare_dies(rdr, &l_type, &r_type,
11299 aggregates_being_compared,
11300 update_canonical_dies_on_the_fly);
11301 if (!result)
11302 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11303
11304 uint64_t l_a = 0, r_a = 0;
11305 die_unsigned_constant_attribute(l, DW_AT_accessibility, l_a);
11306 die_unsigned_constant_attribute(r, DW_AT_accessibility, r_a);
11307 if (l_a != r_a)
11308 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11309
11310 die_unsigned_constant_attribute(l, DW_AT_virtuality, l_a);
11311 die_unsigned_constant_attribute(r, DW_AT_virtuality, r_a);
11312 if (l_a != r_a)
11313 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11314
11315 int64_t l_offset_in_bits = 0, r_offset_in_bits = 0;
11316 die_member_offset(rdr, l, l_offset_in_bits);
11317 die_member_offset(rdr, r, r_offset_in_bits);
11318 if (l_offset_in_bits != r_offset_in_bits)
11319 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11320 }
11321 break;
11322
11323 case DW_TAG_ptr_to_member_type:
11324 {
11325 bool comp_result = false;
11326 if (compare_dies_string_attribute_value(l, r, DW_AT_name, comp_result))
11327 if (!comp_result)
11328 ABG_RETURN(COMPARISON_RESULT_DIFFERENT);
11329
11330 Dwarf_Die l_type, r_type;
11331 ABG_ASSERT(die_die_attribute(l, DW_AT_type, l_type));
11332 ABG_ASSERT(die_die_attribute(r, DW_AT_type, r_type));
11333 result = compare_dies(rdr, &l_type, &r_type,
11334 aggregates_being_compared,
11335 update_canonical_dies_on_the_fly);
11336 if (!result)
11337 ABG_RETURN(result);
11338
11339 ABG_ASSERT(die_die_attribute(l, DW_AT_containing_type, l_type));
11340 ABG_ASSERT(die_die_attribute(r, DW_AT_containing_type, r_type));
11341 result = compare_dies(rdr, &l_type, &r_type,
11342 aggregates_being_compared,
11343 update_canonical_dies_on_the_fly);
11344 if (!result)
11345 ABG_RETURN(result);
11346 }
11347 break;
11348
11349 case DW_TAG_enumerator:
11350 case DW_TAG_packed_type:
11351 case DW_TAG_set_type:
11352 case DW_TAG_file_type:
11353 case DW_TAG_thrown_type:
11354 case DW_TAG_interface_type:
11355 case DW_TAG_shared_type:
11356 case DW_TAG_compile_unit:
11357 case DW_TAG_namespace:
11358 case DW_TAG_module:
11359 case DW_TAG_constant:
11360 case DW_TAG_partial_unit:
11361 case DW_TAG_imported_unit:
11362 case DW_TAG_dwarf_procedure:
11363 case DW_TAG_imported_declaration:
11364 case DW_TAG_entry_point:
11365 case DW_TAG_label:
11366 case DW_TAG_lexical_block:
11367 case DW_TAG_unspecified_parameters:
11368 case DW_TAG_variant:
11369 case DW_TAG_common_block:
11370 case DW_TAG_common_inclusion:
11371 case DW_TAG_inlined_subroutine:
11372 case DW_TAG_with_stmt:
11373 case DW_TAG_access_declaration:
11374 case DW_TAG_catch_block:
11375 case DW_TAG_friend:
11376 case DW_TAG_namelist:
11377 case DW_TAG_namelist_item:
11378 case DW_TAG_template_type_parameter:
11379 case DW_TAG_template_value_parameter:
11380 case DW_TAG_try_block:
11381 case DW_TAG_variant_part:
11382 case DW_TAG_imported_module:
11383 case DW_TAG_condition:
11384 case DW_TAG_type_unit:
11385 case DW_TAG_template_alias:
11386 case DW_TAG_lo_user:
11387 case DW_TAG_MIPS_loop:
11388 case DW_TAG_format_label:
11389 case DW_TAG_function_template:
11390 case DW_TAG_class_template:
11391 case DW_TAG_GNU_BINCL:
11392 case DW_TAG_GNU_EINCL:
11393 case DW_TAG_GNU_template_template_param:
11394 case DW_TAG_GNU_template_parameter_pack:
11395 case DW_TAG_GNU_formal_parameter_pack:
11396 case DW_TAG_GNU_call_site:
11397 case DW_TAG_GNU_call_site_parameter:
11398 case DW_TAG_hi_user:
11399#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11400 if (rdr.debug_die_canonicalization_is_on_)
11402#endif
11404 break;
11405 }
11406
11407 ABG_RETURN(result);
11408}
11409
11410/// Compare two DIEs emitted by a C compiler.
11411///
11412/// @param rdr the DWARF reader used to load the DWARF information.
11413///
11414/// @param l the left-hand-side argument of this comparison operator.
11415///
11416/// @param r the righ-hand-side argument of this comparison operator.
11417///
11418/// @param update_canonical_dies_on_the_fly if yes, then this function
11419/// updates the canonical DIEs of sub-type DIEs of 'l' and 'r', while
11420/// comparing l and r. This helps in making so that sub-type DIEs of
11421/// 'l' and 'r' are compared structurally only once. This is how we
11422/// turn this exponential comparison problem into a problem that is a
11423/// closer to a linear one.
11424///
11425/// @return COMPARISON_RESULT_EQUAL iff @p l equals @p r.
11426static comparison_result
11427compare_dies(const reader& rdr,
11428 const Dwarf_Die *l,
11429 const Dwarf_Die *r,
11430 bool update_canonical_dies_on_the_fly)
11431{
11432 offset_pairs_stack_type aggregates_being_compared(rdr);
11433 return compare_dies(rdr, l, r, aggregates_being_compared,
11434 update_canonical_dies_on_the_fly);
11435}
11436
11437/// Compare two DIEs for the purpose of canonicalization.
11438///
11439/// This is a sub-routine of reader::get_canonical_die.
11440///
11441/// When DIE canonicalization debugging is on, this function performs
11442/// both structural and canonical comparison. It expects that both
11443/// comparison yield the same result.
11444///
11445/// @param rdr the DWARF reader.
11446///
11447/// @param l the left-hand-side comparison operand DIE.
11448///
11449/// @param r the right-hand-side comparison operand DIE.
11450///
11451/// @param update_canonical_dies_on_the_fly if true, then some
11452/// aggregate DIEs will see their canonical types propagated.
11453///
11454/// @return true iff @p l equals @p r.
11455static bool
11456compare_dies_during_canonicalization(reader& rdr,
11457 const Dwarf_Die *l,
11458 const Dwarf_Die *r,
11459 bool update_canonical_dies_on_the_fly)
11460{
11461#ifdef WITH_DEBUG_TYPE_CANONICALIZATION
11462 if (rdr.debug_die_canonicalization_is_on_)
11463 {
11464 bool canonical_equality = false, structural_equality = false;
11465 rdr.use_canonical_die_comparison_ = false;
11466 structural_equality = compare_dies(rdr, l, r,
11467 /*update_canonical_dies_on_the_fly=*/false);
11468 rdr.use_canonical_die_comparison_ = true;
11469 canonical_equality = compare_dies(rdr, l, r,
11470 update_canonical_dies_on_the_fly);
11471 if (canonical_equality != structural_equality)
11472 {
11473 std::cerr << "structural & canonical equality different for DIEs: "
11474 << std::hex
11475 << "l: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(l))
11476 << ", r: " << dwarf_dieoffset(const_cast<Dwarf_Die*>(r))
11477 << std::dec
11478 << ", repr: '"
11479 << rdr.get_die_pretty_type_representation(l, 0)
11480 << "'"
11481 << std::endl;
11483 }
11484 return structural_equality;
11485 }
11486#endif
11487 return compare_dies(rdr, l, r,
11488 update_canonical_dies_on_the_fly);
11489}
11490
11491// ----------------------------------
11492// </die comparison engine>
11493// ---------------------------------
11494
11495/// Get the point where a DW_AT_import DIE is used to import a given
11496/// (unit) DIE, between two DIEs.
11497///
11498/// @param rdr the dwarf reader to consider.
11499///
11500/// @param partial_unit_offset the imported unit for which we want to
11501/// know the insertion point. This is usually a partial unit (with
11502/// tag DW_TAG_partial_unit) but it does not necessarily have to be
11503/// so.
11504///
11505/// @param first_die_offset the offset of the DIE from which this
11506/// function starts looking for the import point of
11507/// @partial_unit_offset. Note that this offset is excluded from the
11508/// set of potential solutions.
11509///
11510/// @param first_die_cu_offset the offset of the (compilation) unit
11511/// that @p first_die_cu_offset belongs to.
11512///
11513/// @param source where the DIE of first_die_cu_offset unit comes
11514/// from.
11515///
11516/// @param last_die_offset the offset of the last DIE of the up to
11517/// which this function looks for the import point of @p
11518/// partial_unit_offset. Note that this offset is excluded from the
11519/// set of potential solutions.
11520///
11521/// @param imported_point_offset. The resulting
11522/// imported_point_offset. Note that if the imported DIE @p
11523/// partial_unit_offset is not found between @p first_die_offset and
11524/// @p last_die_offset, this parameter is left untouched by this
11525/// function.
11526///
11527/// @return true iff an imported unit is found between @p
11528/// first_die_offset and @p last_die_offset.
11529static bool
11530find_import_unit_point_between_dies(const reader& rdr,
11531 size_t partial_unit_offset,
11532 Dwarf_Off first_die_offset,
11533 Dwarf_Off first_die_cu_offset,
11534 die_source source,
11535 size_t last_die_offset,
11536 size_t& imported_point_offset)
11537{
11538 const tu_die_imported_unit_points_map_type& tu_die_imported_unit_points_map =
11539 rdr.tu_die_imported_unit_points_map(source);
11540
11541 tu_die_imported_unit_points_map_type::const_iterator iter =
11542 tu_die_imported_unit_points_map.find(first_die_cu_offset);
11543
11544 ABG_ASSERT(iter != tu_die_imported_unit_points_map.end());
11545
11546 const imported_unit_points_type& imported_unit_points = iter->second;
11547 if (imported_unit_points.empty())
11548 return false;
11549
11550 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
11551 imported_unit_points_type::const_iterator e = imported_unit_points.end();
11552
11553 find_lower_bound_in_imported_unit_points(imported_unit_points,
11554 first_die_offset,
11555 b);
11556
11557 if (last_die_offset != static_cast<size_t>(-1))
11558 find_lower_bound_in_imported_unit_points(imported_unit_points,
11559 last_die_offset,
11560 e);
11561
11562 if (e != imported_unit_points.end())
11563 {
11564 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11565 if (i->imported_unit_die_off == partial_unit_offset)
11566 {
11567 imported_point_offset = i->offset_of_import ;
11568 return true;
11569 }
11570
11571 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
11572 {
11573 if (find_import_unit_point_between_dies(rdr,
11574 partial_unit_offset,
11575 i->imported_unit_child_off,
11576 i->imported_unit_cu_off,
11577 i->imported_unit_die_source,
11578 /*(Dwarf_Off)*/-1,
11579 imported_point_offset))
11580 return true;
11581 }
11582 }
11583 else
11584 {
11585 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11586 if (i->imported_unit_die_off == partial_unit_offset)
11587 {
11588 imported_point_offset = i->offset_of_import ;
11589 return true;
11590 }
11591
11592 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
11593 {
11594 if (find_import_unit_point_between_dies(rdr,
11595 partial_unit_offset,
11596 i->imported_unit_child_off,
11597 i->imported_unit_cu_off,
11598 i->imported_unit_die_source,
11599 /*(Dwarf_Off)*/-1,
11600 imported_point_offset))
11601 return true;
11602 }
11603 }
11604
11605 return false;
11606}
11607
11608/// In the current translation unit, get the last point where a
11609/// DW_AT_import DIE is used to import a given (unit) DIE, before a
11610/// given DIE is found. That given DIE is called the limit DIE.
11611///
11612/// Said otherwise, this function returns the last import point of a
11613/// unit, before a limit.
11614///
11615/// @param rdr the dwarf reader to consider.
11616///
11617/// @param partial_unit_offset the imported unit for which we want to
11618/// know the insertion point of. This is usually a partial unit (with
11619/// tag DW_TAG_partial_unit) but it does not necessarily have to be
11620/// so.
11621///
11622/// @param where_offset the offset of the limit DIE.
11623///
11624/// @param imported_point_offset. The resulting imported_point_offset.
11625/// Note that if the imported DIE @p partial_unit_offset is not found
11626/// before @p die_offset, this is set to the last @p
11627/// partial_unit_offset found under @p parent_die.
11628///
11629/// @return true iff an imported unit is found before @p die_offset.
11630/// Note that if an imported unit is found after @p die_offset then @p
11631/// imported_point_offset is set and the function return false.
11632static bool
11633find_import_unit_point_before_die(const reader& rdr,
11634 size_t partial_unit_offset,
11635 size_t where_offset,
11636 size_t& imported_point_offset)
11637{
11638 size_t import_point_offset = 0;
11639 Dwarf_Die first_die_of_tu;
11640
11641 if (dwarf_child(const_cast<Dwarf_Die*>(rdr.cur_tu_die()),
11642 &first_die_of_tu) != 0)
11643 return false;
11644
11645 Dwarf_Die cu_die_memory;
11646 Dwarf_Die *cu_die;
11647
11648 cu_die = dwarf_diecu(const_cast<Dwarf_Die*>(&first_die_of_tu),
11649 &cu_die_memory, 0, 0);
11650
11651 if (find_import_unit_point_between_dies(rdr, partial_unit_offset,
11652 dwarf_dieoffset(&first_die_of_tu),
11653 dwarf_dieoffset(cu_die),
11654 /*source=*/PRIMARY_DEBUG_INFO_DIE_SOURCE,
11655 where_offset,
11656 import_point_offset))
11657 {
11658 imported_point_offset = import_point_offset;
11659 return true;
11660 }
11661
11662 if (import_point_offset)
11663 {
11664 imported_point_offset = import_point_offset;
11665 return true;
11666 }
11667
11668 return false;
11669}
11670
11671/// Return the parent DIE for a given DIE.
11672///
11673/// Note that the function build_die_parent_map() must have been
11674/// called before this one can work. This function either succeeds or
11675/// aborts the current process.
11676///
11677/// @param rdr the DWARF reader to consider.
11678///
11679/// @param die the DIE for which we want the parent.
11680///
11681/// @param parent_die the output parameter set to the parent die of
11682/// @p die. Its memory must be allocated and handled by the caller.
11683///
11684/// @param where_offset the offset of the DIE where we are "logically"
11685/// positionned at, in the DIE tree. This is useful when @p die is
11686/// e.g, DW_TAG_partial_unit that can be included in several places in
11687/// the DIE tree.
11688///
11689/// @return true if the function could get a parent DIE, false
11690/// otherwise.
11691static bool
11692get_parent_die(const reader& rdr,
11693 const Dwarf_Die* die,
11694 Dwarf_Die& parent_die,
11695 size_t where_offset)
11696{
11697 ABG_ASSERT(rdr.dwarf_debug_info());
11698
11699 const die_source source = rdr.get_die_source(die);
11700
11701 const offset_offset_map_type& m = rdr.die_parent_map(source);
11702 offset_offset_map_type::const_iterator i =
11703 m.find(dwarf_dieoffset(const_cast<Dwarf_Die*>(die)));
11704
11705 if (i == m.end())
11706 return false;
11707
11708 switch (source)
11709 {
11710 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
11711 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11712 i->second, &parent_die));
11713 break;
11714 case ALT_DEBUG_INFO_DIE_SOURCE:
11715 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.alternate_dwarf_debug_info()),
11716 i->second, &parent_die));
11717 break;
11718 case TYPE_UNIT_DIE_SOURCE:
11719 ABG_ASSERT(dwarf_offdie_types(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11720 i->second, &parent_die));
11721 break;
11722 case NO_DEBUG_INFO_DIE_SOURCE:
11723 case NUMBER_OF_DIE_SOURCES:
11725 }
11726
11727 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
11728 {
11729 if (where_offset == 0)
11730 {
11731 parent_die = *rdr.cur_tu_die();
11732 return true;
11733 }
11734 size_t import_point_offset = 0;
11735 bool found =
11736 find_import_unit_point_before_die(rdr,
11737 dwarf_dieoffset(&parent_die),
11738 where_offset,
11739 import_point_offset);
11740 if (!found)
11741 // It looks like parent_die (which comes from the alternate
11742 // debug info file) hasn't been imported into this TU. So,
11743 // Let's assume its logical parent is the DIE of the current
11744 // TU.
11745 parent_die = *rdr.cur_tu_die();
11746 else
11747 {
11748 ABG_ASSERT(import_point_offset);
11749 Dwarf_Die import_point_die;
11750 ABG_ASSERT(dwarf_offdie(const_cast<Dwarf*>(rdr.dwarf_debug_info()),
11751 import_point_offset,
11752 &import_point_die));
11753 return get_parent_die(rdr, &import_point_die,
11754 parent_die, where_offset);
11755 }
11756 }
11757
11758 return true;
11759}
11760
11761/// Get the DIE representing the scope of a given DIE.
11762///
11763/// Please note that when the DIE we are looking at has a
11764/// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
11765/// DIE is the parent DIE of the DIE referred to by that attribute.
11766/// This is the only case where a scope DIE is different from the
11767/// parent DIE of a given DIE.
11768///
11769/// Also note that if the current translation unit is from C, then
11770/// this returns the global scope.
11771///
11772/// @param rdr the DWARF reader to use.
11773///
11774/// @param die the DIE to consider.
11775///
11776/// @param where_offset where we are logically at in the DIE stream.
11777///
11778/// @param scope_die out parameter. This is set to the resulting
11779/// scope DIE iff the function returns true.
11780static bool
11781get_scope_die(const reader& rdr,
11782 const Dwarf_Die* die,
11783 size_t where_offset,
11784 Dwarf_Die& scope_die)
11785{
11786 if (is_c_language(rdr.cur_transl_unit()->get_language()))
11787 {
11788 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
11789 return dwarf_diecu(const_cast<Dwarf_Die*>(die), &scope_die, 0, 0);
11790 }
11791
11792 Dwarf_Die logical_parent_die;
11793 if (die_die_attribute(die, DW_AT_specification,
11794 logical_parent_die, false)
11795 || die_die_attribute(die, DW_AT_abstract_origin,
11796 logical_parent_die, false))
11797 return get_scope_die(rdr, &logical_parent_die, where_offset, scope_die);
11798
11799 if (!get_parent_die(rdr, die, scope_die, where_offset))
11800 return false;
11801
11802 if (dwarf_tag(&scope_die) == DW_TAG_subprogram
11803 || dwarf_tag(&scope_die) == DW_TAG_subroutine_type
11804 || dwarf_tag(&scope_die) == DW_TAG_array_type)
11805 return get_scope_die(rdr, &scope_die, where_offset, scope_die);
11806
11807 return true;
11808}
11809
11810/// Return the abigail IR node representing the scope of a given DIE.
11811///
11812/// Note that it is the logical scope that is returned. That is, if
11813/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
11814/// attribute, it's the scope of the referred-to DIE (via these
11815/// attributes) that is returned.
11816///
11817/// Also note that if the current translation unit is from C, then
11818/// this returns the global scope.
11819///
11820/// @param rdr the dwarf reader to use.
11821///
11822/// @param die the DIE to get the scope for.
11823///
11824/// @param called_from_public_decl is true if this function has been
11825/// initially called within the context of a public decl.
11826///
11827/// @param where_offset the offset of the DIE where we are "logically"
11828/// positionned at, in the DIE tree. This is useful when @p die is
11829/// e.g, DW_TAG_partial_unit that can be included in several places in
11830/// the DIE tree.
11831static scope_decl_sptr
11832get_scope_for_die(reader& rdr,
11833 Dwarf_Die* die,
11834 bool called_for_public_decl,
11835 size_t where_offset)
11836{
11837 const die_source source_of_die = rdr.get_die_source(die);
11838
11839 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
11840 rdr.get_die_language(die, die_lang);
11841 if (is_c_language(die_lang)
11842 || rdr.die_parent_map(source_of_die).empty())
11843 {
11844 // In units for the C languages all decls belong to the global
11845 // namespace. This is generally the case if Libabigail
11846 // determined that no DIE -> parent map was needed.
11847 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
11848 return rdr.global_scope();
11849 }
11850
11851 Dwarf_Die cloned_die;
11852 if (die_die_attribute(die, DW_AT_specification, cloned_die, false)
11853 || die_die_attribute(die, DW_AT_abstract_origin, cloned_die, false))
11854 return get_scope_for_die(rdr, &cloned_die,
11855 called_for_public_decl,
11856 where_offset);
11857
11858 Dwarf_Die parent_die;
11859
11860 if (!get_parent_die(rdr, die, parent_die, where_offset))
11861 return rdr.nil_scope();
11862
11863 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
11864 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
11865 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11866 {
11867 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
11868 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
11869 {
11870 ABG_ASSERT(source_of_die == ALT_DEBUG_INFO_DIE_SOURCE
11871 || source_of_die == TYPE_UNIT_DIE_SOURCE);
11872 return rdr.cur_transl_unit()->get_global_scope();
11873 }
11874
11875 // For top level DIEs like DW_TAG_compile_unit, we just want to
11876 // return the global scope for the corresponding translation
11877 // unit. This must have been set by
11878 // build_translation_unit_and_add_to_ir if we already started to
11879 // build the translation unit of parent_die. Otherwise, just
11880 // return the global scope of the current translation unit.
11881 die_tu_map_type::const_iterator i =
11882 rdr.die_tu_map().find(dwarf_dieoffset(&parent_die));
11883 if (i != rdr.die_tu_map().end())
11884 return i->second->get_global_scope();
11885 return rdr.cur_transl_unit()->get_global_scope();
11886 }
11887
11890 if (dwarf_tag(&parent_die) == DW_TAG_subprogram
11891 || dwarf_tag(&parent_die) == DW_TAG_array_type
11892 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
11893 // this is an entity defined in a scope that is a function.
11894 // Normally, I would say that this should be dropped. But I have
11895 // seen a case where a typedef DIE needed by a function parameter
11896 // was defined right before the parameter, under the scope of the
11897 // function. Yeah, weird. So if I drop the typedef DIE, I'd drop
11898 // the function parm too. So for that case, let's say that the
11899 // scope is the scope of the function itself. Note that this is
11900 // an error of the DWARF emitter. We should never see this DIE in
11901 // this context.
11902 {
11903 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
11904 called_for_public_decl,
11905 where_offset);
11906 if (is_anonymous_type_die(die))
11907 // For anonymous type that have nothing to do in a function or
11908 // array type context, let's put it in the containing
11909 // namespace. That is, do not let it be in a containing class
11910 // or union where it has nothing to do.
11911 while (is_class_or_union_type(s))
11912 {
11913 if (!get_parent_die(rdr, &parent_die, parent_die, where_offset))
11914 return rdr.nil_scope();
11915 s = get_scope_for_die(rdr, &parent_die,
11916 called_for_public_decl,
11917 where_offset);
11918 }
11919 return s;
11920 }
11921 else
11922 d = build_ir_node_from_die(rdr, &parent_die,
11923 called_for_public_decl,
11924 where_offset);
11925 s = dynamic_pointer_cast<scope_decl>(d);
11926 if (!s)
11927 // this is an entity defined in someting that is not a scope.
11928 // Let's drop it.
11929 return rdr.nil_scope();
11930
11931 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
11932 if (cl && cl->get_is_declaration_only())
11933 {
11934 scope_decl_sptr scop =
11935 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
11936 if (scop)
11937 s = scop;
11938 else
11939 s = cl;
11940 }
11941 return s;
11942}
11943
11944/// Convert a DWARF constant representing the value of the
11945/// DW_AT_language property into the translation_unit::language
11946/// enumerator.
11947///
11948/// @param l the DWARF constant to convert.
11949///
11950/// @return the resulting translation_unit::language enumerator.
11952dwarf_language_to_tu_language(size_t l)
11953{
11954 switch (l)
11955 {
11956 case DW_LANG_C89:
11957 return translation_unit::LANG_C89;
11958 case DW_LANG_C:
11959 return translation_unit::LANG_C;
11960 case DW_LANG_Ada83:
11961 return translation_unit::LANG_Ada83;
11962 case DW_LANG_C_plus_plus:
11963 return translation_unit::LANG_C_plus_plus;
11964 case DW_LANG_Cobol74:
11965 return translation_unit::LANG_Cobol74;
11966 case DW_LANG_Cobol85:
11967 return translation_unit::LANG_Cobol85;
11968 case DW_LANG_Fortran77:
11969 return translation_unit::LANG_Fortran77;
11970 case DW_LANG_Fortran90:
11971 return translation_unit::LANG_Fortran90;
11972 case DW_LANG_Pascal83:
11973 return translation_unit::LANG_Pascal83;
11974 case DW_LANG_Modula2:
11975 return translation_unit::LANG_Modula2;
11976 case DW_LANG_Java:
11977 return translation_unit::LANG_Java;
11978 case DW_LANG_C99:
11979 return translation_unit::LANG_C99;
11980 case DW_LANG_Ada95:
11981 return translation_unit::LANG_Ada95;
11982 case DW_LANG_Fortran95:
11983 return translation_unit::LANG_Fortran95;
11984 case DW_LANG_PLI:
11985 return translation_unit::LANG_PLI;
11986 case DW_LANG_ObjC:
11987 return translation_unit::LANG_ObjC;
11988 case DW_LANG_ObjC_plus_plus:
11989 return translation_unit::LANG_ObjC_plus_plus;
11990
11991#ifdef HAVE_DW_LANG_Rust_enumerator
11992 case DW_LANG_Rust:
11993 return translation_unit::LANG_Rust;
11994#endif
11995
11996#ifdef HAVE_DW_LANG_UPC_enumerator
11997 case DW_LANG_UPC:
11998 return translation_unit::LANG_UPC;
11999#endif
12000
12001#ifdef HAVE_DW_LANG_D_enumerator
12002 case DW_LANG_D:
12003 return translation_unit::LANG_D;
12004#endif
12005
12006#ifdef HAVE_DW_LANG_Python_enumerator
12007 case DW_LANG_Python:
12008 return translation_unit::LANG_Python;
12009#endif
12010
12011#ifdef HAVE_DW_LANG_Go_enumerator
12012 case DW_LANG_Go:
12013 return translation_unit::LANG_Go;
12014#endif
12015
12016#ifdef HAVE_DW_LANG_C11_enumerator
12017 case DW_LANG_C11:
12018 return translation_unit::LANG_C11;
12019#endif
12020
12021#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
12022 case DW_LANG_C_plus_plus_03:
12023 return translation_unit::LANG_C_plus_plus_03;
12024#endif
12025
12026#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
12027 case DW_LANG_C_plus_plus_11:
12028 return translation_unit::LANG_C_plus_plus_11;
12029#endif
12030
12031#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
12032 case DW_LANG_C_plus_plus_14:
12033 return translation_unit::LANG_C_plus_plus_14;
12034#endif
12035
12036#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
12037 case DW_LANG_Mips_Assembler:
12038 return translation_unit::LANG_Mips_Assembler;
12039#endif
12040
12041 default:
12042 return translation_unit::LANG_UNKNOWN;
12043 }
12044}
12045
12046/// Get the default array lower bound value as defined by the DWARF
12047/// specification, version 4, depending on the language of the
12048/// translation unit.
12049///
12050/// @param l the language of the translation unit.
12051///
12052/// @return the default array lower bound value.
12053static uint64_t
12054get_default_array_lower_bound(translation_unit::language l)
12055{
12056 int value = 0;
12057 switch (l)
12058 {
12059 case translation_unit::LANG_UNKNOWN:
12060 value = 0;
12061 break;
12062 case translation_unit::LANG_Cobol74:
12063 case translation_unit::LANG_Cobol85:
12064 value = 1;
12065 break;
12066 case translation_unit::LANG_C89:
12067 case translation_unit::LANG_C99:
12068 case translation_unit::LANG_C11:
12069 case translation_unit::LANG_C:
12070 case translation_unit::LANG_C_plus_plus_03:
12071 case translation_unit::LANG_C_plus_plus_11:
12072 case translation_unit::LANG_C_plus_plus_14:
12073 case translation_unit::LANG_C_plus_plus:
12074 case translation_unit::LANG_ObjC:
12075 case translation_unit::LANG_ObjC_plus_plus:
12076 case translation_unit::LANG_Rust:
12077 value = 0;
12078 break;
12079 case translation_unit::LANG_Fortran77:
12080 case translation_unit::LANG_Fortran90:
12081 case translation_unit::LANG_Fortran95:
12082 case translation_unit::LANG_Ada83:
12083 case translation_unit::LANG_Ada95:
12084 case translation_unit::LANG_Pascal83:
12085 case translation_unit::LANG_Modula2:
12086 value = 1;
12087 break;
12088 case translation_unit::LANG_Java:
12089 value = 0;
12090 break;
12091 case translation_unit::LANG_PLI:
12092 value = 1;
12093 break;
12094 case translation_unit::LANG_UPC:
12095 case translation_unit::LANG_D:
12096 case translation_unit::LANG_Python:
12097 case translation_unit::LANG_Go:
12098 case translation_unit::LANG_Mips_Assembler:
12099 value = 0;
12100 break;
12101 }
12102
12103 return value;
12104}
12105
12106/// For a given offset, find the lower bound of a sorted vector of
12107/// imported unit point offset.
12108///
12109/// The lower bound is the smallest point (the point with the smallest
12110/// offset) which is the greater than a given offset.
12111///
12112/// @param imported_unit_points_type the sorted vector of imported
12113/// unit points.
12114///
12115/// @param val the offset to consider when looking for the lower
12116/// bound.
12117///
12118/// @param r an iterator to the lower bound found. This parameter is
12119/// set iff the function returns true.
12120///
12121/// @return true iff the lower bound has been found.
12122static bool
12123find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
12124 Dwarf_Off val,
12125 imported_unit_points_type::const_iterator& r)
12126{
12127 imported_unit_point v(val);
12128 imported_unit_points_type::const_iterator result =
12129 std::lower_bound(p.begin(), p.end(), v);
12130
12131 bool is_ok = result != p.end();
12132
12133 if (is_ok)
12134 r = result;
12135
12136 return is_ok;
12137}
12138
12139/// Given a DW_TAG_compile_unit, build and return the corresponding
12140/// abigail::translation_unit ir node. Note that this function
12141/// recursively reads the children dies of the current DIE and
12142/// populates the resulting translation unit.
12143///
12144/// @param rdr the DWARF reader to use.
12145///
12146/// @param die the DW_TAG_compile_unit DIE to consider.
12147///
12148/// @param address_size the size of the addresses expressed in this
12149/// translation unit in general.
12150///
12151/// @return a pointer to the resulting translation_unit.
12153build_translation_unit_and_add_to_ir(reader& rdr,
12154 Dwarf_Die* die,
12155 char address_size)
12156{
12157 translation_unit_sptr result;
12158
12159 if (!die)
12160 return result;
12161 ABG_ASSERT(dwarf_tag(die) == DW_TAG_compile_unit);
12162
12163 // Clear the part of the context that is dependent on the translation
12164 // unit we are reading.
12165 rdr.clear_per_translation_unit_data();
12166
12167 rdr.cur_tu_die(die);
12168
12169 string path = die_string_attribute(die, DW_AT_name);
12170 if (path == "<artificial>")
12171 {
12172 // This is a file artificially generated by the compiler, so its
12173 // name is '<artificial>'. As we want all different translation
12174 // units to have unique path names, let's suffix this path name
12175 // with its die offset.
12176 std::ostringstream o;
12177 o << path << "-" << std::hex << dwarf_dieoffset(die);
12178 path = o.str();
12179 }
12180 string compilation_dir = die_string_attribute(die, DW_AT_comp_dir);
12181
12182 // See if the same translation unit exits already in the current
12183 // corpus. Sometimes, the same translation unit can be present
12184 // several times in the same debug info. The content of the
12185 // different instances of the translation unit are different. So to
12186 // represent that, we are going to re-use the same translation
12187 // unit. That is, it's going to be the union of all the translation
12188 // units of the same path.
12189 {
12190 const string& abs_path =
12191 compilation_dir.empty() ? path : compilation_dir + "/" + path;
12192 result = rdr.corpus()->find_translation_unit(abs_path);
12193 }
12194
12195 if (!result)
12196 {
12197 result.reset(new translation_unit(rdr.env(),
12198 path,
12199 address_size));
12200 result->set_compilation_dir_path(compilation_dir);
12201 rdr.corpus()->add(result);
12202 uint64_t l = 0;
12203 die_unsigned_constant_attribute(die, DW_AT_language, l);
12204 result->set_language(dwarf_language_to_tu_language(l));
12205 }
12206
12207 rdr.cur_transl_unit(result);
12208 rdr.die_tu_map()[dwarf_dieoffset(die)] = result;
12209
12210 Dwarf_Die child;
12211 if (dwarf_child(die, &child) != 0)
12212 return result;
12213
12214 result->set_is_constructed(false);
12215
12216 do
12217 // Analyze all the DIEs we encounter unless we are asked to only
12218 // analyze exported interfaces and the types reachables from them.
12219 if (!rdr.env().analyze_exported_interfaces_only()
12220 || rdr.is_decl_die_with_exported_symbol(&child))
12221 build_ir_node_from_die(rdr, &child,
12222 die_is_public_decl(&child),
12223 dwarf_dieoffset(&child));
12224 while (dwarf_siblingof(&child, &child) == 0);
12225
12226 if (!rdr.var_decls_to_re_add_to_tree().empty())
12227 for (list<var_decl_sptr>::const_iterator v =
12228 rdr.var_decls_to_re_add_to_tree().begin();
12229 v != rdr.var_decls_to_re_add_to_tree().end();
12230 ++v)
12231 {
12232 if (is_member_decl(*v))
12233 continue;
12234
12235 ABG_ASSERT((*v)->get_scope());
12236 string demangled_name =
12237 demangle_cplus_mangled_name((*v)->get_linkage_name());
12238 if (!demangled_name.empty())
12239 {
12240 std::list<string> fqn_comps;
12241 fqn_to_components(demangled_name, fqn_comps);
12242 string mem_name = fqn_comps.back();
12243 fqn_comps.pop_back();
12244 class_decl_sptr class_type;
12245 string ty_name;
12246 if (!fqn_comps.empty())
12247 {
12248 ty_name = components_to_type_name(fqn_comps);
12249 class_type =
12250 lookup_class_type(ty_name, *rdr.cur_transl_unit());
12251 }
12252 if (class_type)
12253 {
12254 // So we are seeing a member variable for which there
12255 // is a global variable definition DIE not having a
12256 // reference attribute pointing back to the member
12257 // variable declaration DIE. Thus remove the global
12258 // variable definition from its current non-class
12259 // scope ...
12260 decl_base_sptr d;
12261 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
12262 // This is the data member with the same name in cl.
12263 // We just need to flag it as static.
12264 ;
12265 else
12266 {
12267 // In this case there is no data member with the
12268 // same name in cl already. Let's add it there then
12269 // ...
12271 d = add_decl_to_scope(*v, class_type);
12272 }
12273
12274 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
12275 // Let's flag the data member as static.
12276 set_member_is_static(d, true);
12277 }
12278 }
12279 }
12280 rdr.var_decls_to_re_add_to_tree().clear();
12281
12282 result->set_is_constructed(true);
12283
12284 return result;
12285}
12286
12287/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
12288/// DW_TAG_module (for fortran) DIE.
12289///
12290/// Note that this function connects the DW_TAG_namespace to the IR
12291/// being currently created, reads the children of the DIE and
12292/// connects them to the IR as well.
12293///
12294/// @param rdr the DWARF reader to use.
12295///
12296/// @param die the DIE to read from. Must be either DW_TAG_namespace
12297/// or DW_TAG_module.
12298///
12299/// @param where_offset the offset of the DIE where we are "logically"
12300/// positionned at, in the DIE tree. This is useful when @p die is
12301/// e.g, DW_TAG_partial_unit that can be included in several places in
12302/// the DIE tree.
12303///
12304/// @return the resulting @ref abigail::namespace_decl or NULL if it
12305/// couldn't be created.
12307build_namespace_decl_and_add_to_ir(reader& rdr,
12308 Dwarf_Die* die,
12309 size_t where_offset)
12310{
12311 namespace_decl_sptr result;
12312
12313 if (!die)
12314 return result;
12315
12316 unsigned tag = dwarf_tag(die);
12317 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
12318 return result;
12319
12320 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12321 /*called_for_public_decl=*/false,
12322 where_offset);
12323
12324 string name, linkage_name;
12325 location loc;
12326 die_loc_and_name(rdr, die, loc, name, linkage_name);
12327
12328 result.reset(new namespace_decl(rdr.env(), name, loc));
12329 add_decl_to_scope(result, scope.get());
12330 rdr.associate_die_to_decl(die, result, where_offset);
12331
12332 Dwarf_Die child;
12333 if (dwarf_child(die, &child) != 0)
12334 return result;
12335
12336 rdr.scope_stack().push(result.get());
12337 do
12338 build_ir_node_from_die(rdr, &child,
12339 // If this namespace DIE is private
12340 // (anonymous) then all its content is
12341 // considered private. Otherwise, its
12342 // public decls are considered public.
12343 /*called_from_public_decl=*/
12344 die_is_public_decl(die) && die_is_public_decl(&child),
12345 where_offset);
12346 while (dwarf_siblingof(&child, &child) == 0);
12347 rdr.scope_stack().pop();
12348
12349 return result;
12350}
12351
12352/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
12353///
12354/// @param rdr the DWARF reader to use.
12355///
12356/// @param die the DW_TAG_base_type to consider.
12357///
12358/// @param where_offset where we are logically at in the DIE stream.
12359///
12360/// @return the resulting decl_base_sptr.
12361static type_decl_sptr
12362build_type_decl(reader& rdr, Dwarf_Die* die, size_t where_offset)
12363{
12364 type_decl_sptr result;
12365
12366 if (!die)
12367 return result;
12368 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
12369
12370 uint64_t byte_size = 0, bit_size = 0;
12371 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
12372 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
12373 return result;
12374
12375 if (bit_size == 0 && byte_size != 0)
12376 // Update the bit size.
12377 bit_size = byte_size * 8;
12378
12379 string type_name, linkage_name;
12380 location loc;
12381 die_loc_and_name(rdr, die, loc, type_name, linkage_name);
12382
12383 if (byte_size == 0)
12384 {
12385 // The size of the type is zero, that must mean that we are
12386 // looking at the definition of the void type.
12387 if (type_name == "void")
12388 result = is_type_decl(build_ir_node_for_void_type(rdr));
12389 else
12390 // A type of size zero that is not void? Hmmh, I am not sure
12391 // what that means. Return nil for now.
12392 return result;
12393 }
12394
12395 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12396 {
12397 string normalized_type_name = type_name;
12398 integral_type int_type;
12399 if (parse_integral_type(type_name, int_type))
12400 normalized_type_name = int_type.to_string();
12401 result = lookup_basic_type(normalized_type_name, *corp);
12402 }
12403
12404 if (!result)
12405 if (corpus_sptr corp = rdr.corpus())
12406 result = lookup_basic_type(type_name, *corp);
12407 if (!result)
12408 result.reset(new type_decl(rdr.env(), type_name, bit_size,
12409 /*alignment=*/0, loc, linkage_name));
12410 rdr.associate_die_to_type(die, result, where_offset);
12411 return result;
12412}
12413
12414/// Construct the type that is to be used as the underlying type of an
12415/// enum.
12416///
12417/// @param rdr the DWARF reader to use.
12418///
12419/// @param enum_name the name of the enum that this type is going to
12420/// be the underlying type of.
12421///
12422/// @param enum_size the size of the enum.
12423///
12424/// @param is_anonymous whether the underlying type is anonymous or
12425/// not. By default, this should be set to true as before c++11 (and
12426/// in C), it's almost the case.
12427static type_decl_sptr
12428build_enum_underlying_type(reader& rdr,
12429 string enum_name,
12430 uint64_t enum_size,
12431 bool is_anonymous = true)
12432{
12433 string underlying_type_name =
12434 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
12435 enum_size);
12436
12437 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
12438 enum_size, enum_size, location()));
12439 result->set_is_anonymous(is_anonymous);
12440 result->set_is_artificial(true);
12441 translation_unit_sptr tu = rdr.cur_transl_unit();
12442 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope().get());
12443 result = dynamic_pointer_cast<type_decl>(d);
12444 ABG_ASSERT(result);
12445 canonicalize(result);
12446 return result;
12447}
12448
12449/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
12450///
12451/// @param rdr the DWARF reader to use.
12452///
12453/// @param die the DIE to read from.
12454///
12455/// @param scope the scope of the final enum. Note that this function
12456/// does *NOT* add the built type to this scope. The scope is just so
12457/// that the function knows how to name anonymous enums.
12458///
12459/// @param is_declaration_only is true if the DIE denoted by @p die is
12460/// a declaration-only DIE.
12461///
12462/// @return the built enum_type_decl or NULL if it could not be built.
12464build_enum_type(reader& rdr,
12465 Dwarf_Die* die,
12466 scope_decl* scope,
12467 size_t where_offset,
12468 bool is_declaration_only)
12469{
12470 enum_type_decl_sptr result;
12471 if (!die)
12472 return result;
12473
12474 unsigned tag = dwarf_tag(die);
12475 if (tag != DW_TAG_enumeration_type)
12476 return result;
12477
12478 string name, linkage_name;
12479 location loc;
12480 die_loc_and_name(rdr, die, loc, name, linkage_name);
12481
12482 bool is_anonymous = false;
12483 // If the enum is anonymous, let's give it a name.
12484 if (name.empty())
12485 {
12486 name = get_internal_anonymous_die_prefix_name(die);
12487 ABG_ASSERT(!name.empty());
12488 // But we remember that the type is anonymous.
12489 is_anonymous = true;
12490
12491 if (size_t s = scope->get_num_anonymous_member_enums())
12492 name = build_internal_anonymous_die_name(name, s);
12493 }
12494
12495 bool use_odr = rdr.odr_is_relevant(die);
12496 // If the type has location, then associate it to its
12497 // representation. This way, all occurences of types with the same
12498 // representation (name) and location can be later detected as being
12499 // for the same type.
12500
12501 if (!is_anonymous)
12502 {
12503 if (use_odr)
12504 {
12505 if (enum_type_decl_sptr pre_existing_enum =
12506 is_enum_type(rdr.lookup_artifact_from_die(die)))
12507 result = pre_existing_enum;
12508 }
12509 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12510 {
12511 if (loc)
12512 result = lookup_enum_type_per_location(loc.expand(), *corp);
12513 }
12514 else if (loc)
12515 {
12516 if (enum_type_decl_sptr pre_existing_enum =
12517 is_enum_type(rdr.lookup_artifact_from_die(die)))
12518 if (pre_existing_enum->get_location() == loc)
12519 result = pre_existing_enum;
12520 }
12521
12522 if (result)
12523 {
12524 rdr.associate_die_to_type(die, result, where_offset);
12525 return result;
12526 }
12527 }
12528 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
12529 // we can look them up?
12530
12531 uint64_t size = 0;
12532 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12533 size *= 8;
12534 bool is_artificial = die_is_artificial(die);
12535
12536 // for now we consider that underlying types of enums are all anonymous
12537 bool enum_underlying_type_is_anonymous= true;
12538
12540 Dwarf_Die child;
12541 if (dwarf_child(die, &child) == 0)
12542 {
12543 do
12544 {
12545 if (dwarf_tag(&child) != DW_TAG_enumerator)
12546 continue;
12547
12548 string n, m;
12549 location l;
12550 die_loc_and_name(rdr, &child, l, n, m);
12551 uint64_t val = 0;
12552 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
12553 enms.push_back(enum_type_decl::enumerator(n, val));
12554 }
12555 while (dwarf_siblingof(&child, &child) == 0);
12556 }
12557
12558 // DWARF up to version 4 (at least) doesn't seem to carry the
12559 // underlying type, so let's create an artificial one here, which
12560 // sole purpose is to be passed to the constructor of the
12561 // enum_type_decl type.
12562 type_decl_sptr t =
12563 build_enum_underlying_type(rdr, name, size,
12564 enum_underlying_type_is_anonymous);
12565 t->set_is_declaration_only(is_declaration_only);
12566
12567 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
12568 result->set_is_anonymous(is_anonymous);
12569 result->set_is_declaration_only(is_declaration_only);
12570 result->set_is_artificial(is_artificial);
12571 rdr.associate_die_to_type(die, result, where_offset);
12572
12573 rdr.maybe_schedule_declaration_only_enum_for_resolution(result);
12574
12575 return result;
12576}
12577
12578/// Once a function_decl has been built and added to a class as a
12579/// member function, this function updates the information of the
12580/// function_decl concerning the properties of its relationship with
12581/// the member class. That is, it updates properties like
12582/// virtualness, access, constness, cdtorness, etc ...
12583///
12584/// @param die the DIE of the function_decl that has been just built.
12585///
12586/// @param f the function_decl that has just been built from @p die.
12587///
12588/// @param klass the @ref class_or_union that @p f belongs to.
12589///
12590/// @param rdr the context used to read the ELF/DWARF information.
12591static void
12592finish_member_function_reading(Dwarf_Die* die,
12593 const function_decl_sptr& f,
12594 const class_or_union_sptr klass,
12595 reader& rdr)
12596{
12597 ABG_ASSERT(klass);
12598
12599 method_decl_sptr m = is_method_decl(f);
12600 ABG_ASSERT(m);
12601
12602 method_type_sptr method_t = is_method_type(m->get_type());
12603 ABG_ASSERT(method_t);
12604
12605 bool is_ctor = (f->get_name() == klass->get_name());
12606 bool is_dtor = (!f->get_name().empty()
12607 && static_cast<string>(f->get_name())[0] == '~');
12608 bool is_virtual = die_is_virtual(die);
12609 int64_t vindex = -1;
12610 if (is_virtual)
12611 die_virtual_function_index(die, vindex);
12612 access_specifier access = public_access;
12613 if (class_decl_sptr c = is_class_type(klass))
12614 if (!c->is_struct())
12615 access = private_access;
12616 die_access_specifier(die, access);
12617
12618 bool is_static = false;
12619 {
12620 // Let's see if the first parameter is a pointer to an instance of
12621 // the same class type as the current class and has a
12622 // DW_AT_artificial attribute flag set. We are not looking at
12623 // DW_AT_object_pointer (for DWARF 3) because it wasn't being
12624 // emitted in GCC 4_4, which was already DWARF 3.
12626 if (!f->get_parameters().empty())
12627 first_parm = f->get_parameters()[0];
12628
12629 bool is_artificial = first_parm && first_parm->get_is_artificial();
12630 type_base_sptr this_ptr_type, other_klass;
12631
12632 if (is_artificial)
12633 this_ptr_type = first_parm->get_type();
12634
12635 // Sometimes, the type of the "this" pointer is "const class_type* const".
12636 //
12637 // Meaning that the "this pointer" itself is const qualified. So
12638 // let's get the underlying underlying non-qualified pointer.
12639 if (qualified_type_def_sptr q = is_qualified_type(this_ptr_type))
12640 this_ptr_type = q->get_underlying_type();
12641
12642 // Now, get the pointed-to type.
12643 if (pointer_type_def_sptr p = is_pointer_type(this_ptr_type))
12644 other_klass = p->get_pointed_to_type();
12645
12646 // Sometimes, other_klass can be qualified; e.g, volatile. In
12647 // that case, let's get the unqualified version of other_klass.
12648 if (qualified_type_def_sptr q = is_qualified_type(other_klass))
12649 other_klass = q->get_underlying_type();
12650
12651 if (other_klass
12652 && get_type_name(other_klass) == klass->get_qualified_name())
12653 ;
12654 else
12655 is_static = true;
12656
12657 if (is_static)
12658 {
12659 // If we are looking at a DWARF version that is high enough
12660 // for the DW_AT_object_pointer attribute to be present, let's
12661 // see if it's present. If it is, then the current member
12662 // function is not static.
12663 Dwarf_Die object_pointer_die;
12664 if (die_has_object_pointer(die, object_pointer_die))
12665 is_static = false;
12666 }
12667 }
12668 set_member_access_specifier(m, access);
12669 if (vindex != -1)
12671 if (is_virtual)
12672 set_member_function_is_virtual(m, is_virtual);
12673 set_member_is_static(m, is_static);
12674 set_member_function_is_ctor(m, is_ctor);
12675 set_member_function_is_dtor(m, is_dtor);
12676 set_member_function_is_const(m, method_t->get_is_const());
12677
12679
12680 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol())
12681 {
12682 // This is a virtual member function which has a linkage name
12683 // but has no underlying symbol set.
12684 //
12685 // The underlying elf symbol to set to this function can show up
12686 // later in the DWARF input or it can be that, because of some
12687 // compiler optimization, the relation between this function and
12688 // its underlying elf symbol is simply not emitted in the DWARF.
12689 //
12690 // Let's thus schedule this function for a later fixup pass
12691 // (performed by
12692 // reader::fixup_functions_with_no_symbols()) that will
12693 // set its underlying symbol.
12694 //
12695 // Note that if the underying symbol is encountered later in the
12696 // DWARF input, then the part of build_function_decl() that
12697 // updates the function to set its underlying symbol will
12698 // de-schedule this function wrt fixup pass.
12699 Dwarf_Off die_offset = dwarf_dieoffset(die);
12700 die_function_decl_map_type &fns_with_no_symbol =
12701 rdr.die_function_decl_with_no_symbol_map();
12702 die_function_decl_map_type::const_iterator i =
12703 fns_with_no_symbol.find(die_offset);
12704 if (i == fns_with_no_symbol.end())
12705 fns_with_no_symbol[die_offset] = f;
12706 }
12707
12708}
12709
12710/// If a function DIE has attributes which have not yet been read and
12711/// added to the internal representation that represents that function
12712/// then read those extra attributes and update the internal
12713/// representation.
12714///
12715/// @param rdr the DWARF reader to use.
12716///
12717/// @param die the function DIE to consider.
12718///
12719/// @param where_offset where we logical are, currently, in the stream
12720/// of DIEs. If you don't know what this is, you can just set it to zero.
12721///
12722/// @param existing_fn the representation of the function to update.
12723///
12724/// @return the updated function representation.
12725static function_decl_sptr
12726maybe_finish_function_decl_reading(reader& rdr,
12727 Dwarf_Die* die,
12728 size_t where_offset,
12729 const function_decl_sptr& existing_fn)
12730{
12731 function_decl_sptr result = build_function_decl(rdr, die,
12732 where_offset,
12733 existing_fn);
12734
12735 return result;
12736}
12737
12738/// Lookup a class or a typedef with a given qualified name in the
12739/// corpus that a given scope belongs to.
12740///
12741/// @param scope the scope to consider.
12742///
12743/// @param type_name the qualified name of the type to look for.
12744///
12745/// @return the typedef or class type found.
12746static type_base_sptr
12747lookup_class_or_typedef_from_corpus(scope_decl* scope, const string& type_name)
12748{
12749 string qname = build_qualified_name(scope, type_name);
12750 corpus* corp = scope->get_corpus();
12751 type_base_sptr result = lookup_class_or_typedef_type(qname, *corp);
12752 return result;
12753}
12754
12755/// Lookup a class of typedef type from the current corpus being
12756/// constructed.
12757///
12758/// The type being looked for has the same name as a given DIE.
12759///
12760/// @param rdr the DWARF reader to use.
12761///
12762/// @param die the DIE which has the same name as the type we are
12763/// looking for.
12764///
12765/// @param called_for_public_decl whether this function is being
12766/// called from a a publicly defined declaration.
12767///
12768/// @param where_offset where we are logically at in the DIE stream.
12769///
12770/// @return the type found.
12771static type_base_sptr
12772lookup_class_or_typedef_from_corpus(reader& rdr,
12773 Dwarf_Die* die,
12774 bool called_for_public_decl,
12775 size_t where_offset)
12776{
12777 if (!die)
12778 return class_decl_sptr();
12779
12780 string class_name = die_string_attribute(die, DW_AT_name);
12781 if (class_name.empty())
12782 return class_decl_sptr();
12783
12784 scope_decl_sptr scope = get_scope_for_die(rdr, die,
12785 called_for_public_decl,
12786 where_offset);
12787 if (scope)
12788 return lookup_class_or_typedef_from_corpus(scope.get(), class_name);
12789
12790 return type_base_sptr();
12791}
12792
12793/// Lookup a class, typedef or enum type with a given qualified name
12794/// in the corpus that a given scope belongs to.
12795///
12796/// @param scope the scope to consider.
12797///
12798/// @param type_name the qualified name of the type to look for.
12799///
12800/// @return the typedef, enum or class type found.
12801static type_base_sptr
12802lookup_class_typedef_or_enum_type_from_corpus(scope_decl* scope,
12803 const string& type_name)
12804{
12805 string qname = build_qualified_name(scope, type_name);
12806 corpus* corp = scope->get_corpus();
12807 type_base_sptr result = lookup_class_typedef_or_enum_type(qname, *corp);
12808 return result;
12809}
12810
12811/// Lookup a class, typedef or enum type in a given scope, in the
12812/// corpus that scope belongs to.
12813///
12814/// @param die the DIE of the class, typedef or enum to lookup.
12815///
12816/// @param anonymous_member_type_idx if @p DIE represents an anonymous
12817/// type, this is the index of that anonymous type in its scope, in
12818/// case there are several anonymous types of the same kind in that
12819/// scope.
12820///
12821/// @param scope the scope in which to look the type for.
12822///
12823/// @return the typedef, enum or class type found.
12824static type_base_sptr
12825lookup_class_typedef_or_enum_type_from_corpus(Dwarf_Die* die,
12826 size_t anonymous_member_type_idx,
12827 scope_decl* scope)
12828{
12829 if (!die)
12830 return class_decl_sptr();
12831
12832 string type_name = die_string_attribute(die, DW_AT_name);
12833 if (is_anonymous_type_die(die))
12834 type_name =
12835 get_internal_anonymous_die_name(die, anonymous_member_type_idx);
12836
12837 if (type_name.empty())
12838 return class_decl_sptr();
12839
12840 return lookup_class_typedef_or_enum_type_from_corpus(scope, type_name);
12841}
12842
12843
12844/// Test if a DIE represents a function that is a member of a given
12845/// class type.
12846///
12847/// @param rdr the DWARF reader.
12848///
12849/// @param function_die the DIE of the function to consider.
12850///
12851/// @param class_type the class type to consider.
12852///
12853/// @param where_offset where we are logically at in the DIE stream.
12854///
12855/// @return the method declaration corresponding to the member
12856/// function of @p class_type, iff @p function_die is for a member
12857/// function of @p class_type.
12858static method_decl_sptr
12859is_function_for_die_a_member_of_class(reader& rdr,
12860 Dwarf_Die* function_die,
12861 const class_or_union_sptr& class_type)
12862{
12863 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
12864
12865 if (!artifact)
12866 return method_decl_sptr();
12867
12868 method_decl_sptr method = is_method_decl(artifact);
12869 method_type_sptr method_type;
12870
12871 if (method)
12872 method_type = method->get_type();
12873 else
12874 method_type = is_method_type(artifact);
12875 ABG_ASSERT(method_type);
12876
12877 class_or_union_sptr method_class = method_type->get_class_type();
12878 ABG_ASSERT(method_class);
12879
12880 string method_class_name = method_class->get_qualified_name(),
12881 class_type_name = class_type->get_qualified_name();
12882
12883 if (method_class_name == class_type_name)
12884 {
12885 //ABG_ASSERT(class_type.get() == method_class.get());
12886 return method;
12887 }
12888
12889 return method_decl_sptr();
12890}
12891
12892/// If a given function DIE represents an existing member function of
12893/// a given class, then update that member function with new
12894/// properties present in the DIE. Otherwise, if the DIE represents a
12895/// new member function that is not already present in the class then
12896/// add that new member function to the class.
12897///
12898/// @param rdr the DWARF reader.
12899///
12900/// @param function_die the DIE of the potential member function to
12901/// consider.
12902///
12903/// @param class_type the class type to consider.
12904///
12905/// @param called_from_public_decl is true iff this function was
12906/// called from a publicly defined and exported declaration.
12907///
12908/// @param where_offset where we are logically at in the DIE stream.
12909///
12910/// @return the method decl representing the member function.
12911static method_decl_sptr
12912add_or_update_member_function(reader& rdr,
12913 Dwarf_Die* function_die,
12914 const class_or_union_sptr& class_type,
12915 bool called_from_public_decl,
12916 size_t where_offset)
12917{
12918 method_decl_sptr method =
12919 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
12920
12921 if (!method)
12922 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
12923 class_type.get(),
12924 called_from_public_decl,
12925 where_offset));
12926 if (!method)
12927 return method_decl_sptr();
12928
12929 finish_member_function_reading(function_die,
12930 is_function_decl(method),
12931 class_type, rdr);
12932 return method;
12933}
12934
12935/// Build a an IR node for class type from a DW_TAG_structure_type or
12936/// DW_TAG_class_type DIE and add that node to the ABI corpus being
12937/// currently built.
12938///
12939/// If the represents class type that already exists, then update the
12940/// existing class type with the new properties found in the DIE.
12941///
12942/// It meanst that this function can also update an existing
12943/// class_decl node with data members, member functions and other
12944/// properties coming from the DIE.
12945///
12946/// @param rdr the DWARF reader to consider.
12947///
12948/// @param die the DIE to read information from. Must be either a
12949/// DW_TAG_structure_type or a DW_TAG_class_type.
12950///
12951/// @param scope a pointer to the scope_decl* under which this class
12952/// is to be added to.
12953///
12954/// @param is_struct whether the class was declared as a struct.
12955///
12956/// @param klass if non-null, this is a klass to append the members
12957/// to. Otherwise, this function just builds the class from scratch.
12958///
12959/// @param called_from_public_decl set to true if this class is being
12960/// called from a "Public declaration like vars or public symbols".
12961///
12962/// @param where_offset the offset of the DIE where we are "logically"
12963/// positionned at, in the DIE tree. This is useful when @p die is
12964/// e.g, DW_TAG_partial_unit that can be included in several places in
12965/// the DIE tree.
12966///
12967/// @param is_declaration_only is true if the DIE denoted by @p die is
12968/// a declaration-only DIE.
12969///
12970/// @return the resulting class_type.
12971static class_decl_sptr
12972add_or_update_class_type(reader& rdr,
12973 Dwarf_Die* die,
12974 scope_decl* scope,
12975 bool is_struct,
12976 class_decl_sptr klass,
12977 bool called_from_public_decl,
12978 size_t where_offset,
12979 bool is_declaration_only)
12980{
12981 class_decl_sptr result;
12982 if (!die)
12983 return result;
12984
12985 const die_source source = rdr.get_die_source(die);
12986
12987 unsigned tag = dwarf_tag(die);
12988
12989 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
12990 return result;
12991
12992 {
12993 die_class_or_union_map_type::const_iterator i =
12994 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
12995 if (i != rdr.die_wip_classes_map(source).end())
12996 {
12997 class_decl_sptr class_type = is_class_type(i->second);
12998 ABG_ASSERT(class_type);
12999 return class_type;
13000 }
13001 }
13002
13003 string name, linkage_name;
13004 location loc;
13005 die_loc_and_name(rdr, die, loc, name, linkage_name);
13006
13007 bool is_anonymous = false;
13008 if (name.empty())
13009 {
13010 // So we are looking at an anonymous struct. Let's
13011 // give it a name.
13012 name = get_internal_anonymous_die_prefix_name(die);
13013 ABG_ASSERT(!name.empty());
13014 // But we remember that the type is anonymous.
13015 is_anonymous = true;
13016
13017 if (size_t s = scope->get_num_anonymous_member_classes())
13018 name = build_internal_anonymous_die_name(name, s);
13019 }
13020
13021 if (!is_anonymous)
13022 {
13023 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13024 {
13025 if (loc)
13026 // TODO: if there is only one class defined in the corpus
13027 // for this location, then re-use it. But if there are
13028 // more than one, then do not re-use it, for now.
13029 result = lookup_class_type_per_location(loc.expand(), *corp);
13030 else
13031 // TODO: if there is just one class for that name defined,
13032 // then re-use it. Otherwise, don't.
13033 result = lookup_class_type(name, *corp);
13034 if (result
13035 // If we are seeing a declaration of a definition we
13036 // already had, or if we are seing a type with the same
13037 // declaration-only-ness that we had before, then keep
13038 // the one we already had.
13039 && (result->get_is_declaration_only() == is_declaration_only
13040 || (!result->get_is_declaration_only()
13041 && is_declaration_only)))
13042 {
13043 rdr.associate_die_to_type(die, result, where_offset);
13044 return result;
13045 }
13046 else
13047 // We might be seeing the definition of a declaration we
13048 // already had. In that case, keep the definition and
13049 // drop the declaration.
13050 result.reset();
13051 }
13052 }
13053
13054 // If we've already seen the same class as 'die', then let's re-use
13055 // that one, unless it's an anonymous class. We can't really safely
13056 // re-use anonymous classes as they have no name, by construction.
13057 // What we can do, rather, is to reuse the typedef that name them,
13058 // when they do have a naming typedef.
13059 if (!is_anonymous)
13060 if (class_decl_sptr pre_existing_class =
13061 is_class_type(rdr.lookup_type_artifact_from_die(die)))
13062 klass = pre_existing_class;
13063
13064 uint64_t size = 0;
13065 die_size_in_bits(die, size);
13066 bool is_artificial = die_is_artificial(die);
13067
13068 Dwarf_Die child;
13069 bool has_child = (dwarf_child(die, &child) == 0);
13070
13071 decl_base_sptr res;
13072 if (klass)
13073 {
13074 res = result = klass;
13075 if (has_child && klass->get_is_declaration_only()
13076 && klass->get_definition_of_declaration())
13077 res = result = is_class_type(klass->get_definition_of_declaration());
13078 if (loc)
13079 result->set_location(loc);
13080 }
13081 else
13082 {
13083 result.reset(new class_decl(rdr.env(), name, size,
13084 /*alignment=*/0, is_struct, loc,
13085 decl_base::VISIBILITY_DEFAULT,
13086 is_anonymous));
13087
13088 result->set_is_declaration_only(is_declaration_only);
13089
13090 res = add_decl_to_scope(result, scope);
13091 result = dynamic_pointer_cast<class_decl>(res);
13092 ABG_ASSERT(result);
13093 }
13094
13095 if (!klass || klass->get_is_declaration_only())
13096 if (size != result->get_size_in_bits())
13097 result->set_size_in_bits(size);
13098
13099 if (klass)
13100 // We are amending a class that was built before. So let's check
13101 // if we need to amend its "declaration-only-ness" status.
13102 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
13103 // The size of the class doesn't match its
13104 // 'declaration-only-ness". We might have a non-zero sized
13105 // class which is declaration-only, or a zero sized class that
13106 // is not declaration-only. Let's set the declaration-only-ness
13107 // according to what we are instructed to.
13108 //
13109 // Note however that there are binaries out there emitted by
13110 // compilers (Clang, in C++) emit declarations-only classes that
13111 // have non-zero size. So we must honor these too. That is why
13112 // we are not forcing the declaration-only-ness to false when a
13113 // class has non-zero size. An example of such binary is
13114 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
13115 result->set_is_declaration_only(is_declaration_only);
13116
13117 // If a non-decl-only class has children node and is advertized as
13118 // having a non-zero size let's trust that.
13119 if (!result->get_is_declaration_only() && has_child)
13120 if (result->get_size_in_bits() == 0 && size != 0)
13121 result->set_size_in_bits(size);
13122
13123 result->set_is_artificial(is_artificial);
13124
13125 rdr.associate_die_to_type(die, result, where_offset);
13126
13127 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13128
13129 if (!has_child)
13130 // TODO: set the access specifier for the declaration-only class
13131 // here.
13132 return result;
13133
13134 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13135
13136 bool is_incomplete_type = false;
13137 if (is_declaration_only && size == 0 && has_child)
13138 // this is an incomplete DWARF type as defined by [5.7.1]
13139 //
13140 // An incomplete structure, union or class type is represented by
13141 // a structure, union or class entry that does not have a byte
13142 // size attribute and that has a DW_AT_declaration attribute.
13143 //
13144 // Let's consider that it's thus a decl-only class, likely
13145 // referred to by a pointer. If we later encounter a definition
13146 // for this decl-only class type, then this decl-only class will
13147 // be resolved to it by the code in
13148 // reader::resolve_declaration_only_classes.
13149 is_incomplete_type = true;
13150
13151 scope_decl_sptr scop =
13152 dynamic_pointer_cast<scope_decl>(res);
13153 ABG_ASSERT(scop);
13154 rdr.scope_stack().push(scop.get());
13155
13156 if (has_child && !is_incomplete_type)
13157 {
13158 int anonymous_member_class_index = -1;
13159 int anonymous_member_union_index = -1;
13160 int anonymous_member_enum_index = -1;
13161
13162 do
13163 {
13164 tag = dwarf_tag(&child);
13165
13166 // Handle base classes.
13167 if (tag == DW_TAG_inheritance)
13168 {
13169 result->set_is_declaration_only(false);
13170
13171 Dwarf_Die type_die;
13172 if (!die_die_attribute(&child, DW_AT_type, type_die))
13173 continue;
13174
13175 type_base_sptr base_type;
13176 if (!(base_type =
13177 lookup_class_or_typedef_from_corpus(rdr, &type_die,
13178 called_from_public_decl,
13179 where_offset)))
13180 {
13181 base_type =
13182 is_type(build_ir_node_from_die(rdr, &type_die,
13183 called_from_public_decl,
13184 where_offset));
13185 }
13186 // Sometimes base_type can be a typedef. Let's make
13187 // sure that typedef is compatible with a class type.
13189 if (!b)
13190 continue;
13191
13192 access_specifier access =
13193 is_struct
13194 ? public_access
13195 : private_access;
13196
13197 die_access_specifier(&child, access);
13198
13199 bool is_virt= die_is_virtual(&child);
13200 int64_t offset = 0;
13201 bool is_offset_present =
13202 die_member_offset(rdr, &child, offset);
13203
13204 class_decl::base_spec_sptr base(new class_decl::base_spec
13205 (b, access,
13206 is_offset_present ? offset : -1,
13207 is_virt));
13208 if (b->get_is_declaration_only())
13209 ABG_ASSERT(rdr.is_decl_only_class_scheduled_for_resolution(b));
13210 if (result->find_base_class(b->get_qualified_name()))
13211 continue;
13212 result->add_base_specifier(base);
13213 }
13214 // Handle data members.
13215 else if (tag == DW_TAG_member
13216 || tag == DW_TAG_variable)
13217 {
13218 Dwarf_Die type_die;
13219 if (!die_die_attribute(&child, DW_AT_type, type_die))
13220 continue;
13221
13222 string n, m;
13223 location loc;
13224 die_loc_and_name(rdr, &child, loc, n, m);
13225 /// For now, we skip the hidden vtable pointer.
13226 /// Currently, we're looking for a member starting with
13227 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
13228 /// use as a name for the hidden vtable pointer.
13229 if (n.substr(0, 5) == "_vptr"
13230 && n.size() > 5
13231 && !std::isalnum(n.at(5))
13232 && n.at(5) != '_')
13233 continue;
13234
13235 // If the variable is already a member of this class,
13236 // move on. If it's an anonymous data member, we need
13237 // to handle it differently. We'll do that later below.
13238 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13239 continue;
13240
13241 int64_t offset_in_bits = 0;
13242 bool is_laid_out = die_member_offset(rdr, &child,
13243 offset_in_bits);
13244 // For now, is_static == !is_laid_out. When we have
13245 // templates, we'll try to be more specific. For now,
13246 // this approximation should do OK.
13247 bool is_static = !is_laid_out;
13248
13249 if (is_static && variable_is_suppressed(rdr,
13250 result.get(),
13251 &child))
13252 continue;
13253
13254 decl_base_sptr ty = is_decl(build_ir_node_from_die(rdr, &type_die,
13255 called_from_public_decl,
13256 where_offset));
13257 type_base_sptr t = is_type(ty);
13258 if (!t)
13259 continue;
13260
13261 if (n.empty() && !die_is_anonymous_data_member(&child))
13262 {
13263 // We must be in a case where the data member has an
13264 // empty name because the DWARF emitter has a bug.
13265 // Let's generate an artificial name for that data
13266 // member.
13267 n = rdr.build_name_for_buggy_anonymous_data_member(&child);
13268 ABG_ASSERT(!n.empty());
13269 }
13270
13271 // The call to build_ir_node_from_die above could have
13272 // triggered the adding of a data member named 'n' into
13273 // result. So let's check again if the variable is
13274 // already a member of this class. Here again, if it's
13275 // an anonymous data member, we need to handle it
13276 // differently. We'll do that later below.
13277 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13278 continue;
13279
13280 if (!is_static)
13281 // We have a non-static data member. So this class
13282 // cannot be a declaration-only class anymore, even if
13283 // some DWARF emitters might consider it otherwise.
13284 result->set_is_declaration_only(false);
13285 access_specifier access =
13286 is_struct
13287 ? public_access
13288 : private_access;
13289
13290 die_access_specifier(&child, access);
13291
13292 var_decl_sptr dm(new var_decl(n, t, loc, m));
13293 if (n.empty()
13295 // dm is an anonymous data member that was already
13296 // present in the current class so let's not add it.
13297 continue;
13298 result->add_data_member(dm, access, is_laid_out,
13299 is_static, offset_in_bits);
13300 ABG_ASSERT(has_scope(dm));
13301 rdr.associate_die_to_decl(&child, dm, where_offset,
13302 /*associate_by_repr=*/false);
13303 }
13304 // Handle member functions;
13305 else if (tag == DW_TAG_subprogram)
13306 {
13307 decl_base_sptr r =
13308 add_or_update_member_function(rdr, &child, result,
13309 called_from_public_decl,
13310 where_offset);
13312 rdr.associate_die_to_decl(&child, f, where_offset,
13313 /*associate_by_repr=*/true);
13314 }
13315 // Handle member types
13316 else if (die_is_type(&child))
13317 {
13318 // Track the anonymous type index in the current
13319 // scope. Look for what this means by reading the
13320 // comment of the function
13321 // build_internal_anonymous_die_name.
13322 int anonymous_member_type_index = 0;
13323 if (is_anonymous_type_die(&child))
13324 {
13325 // Update the anonymous type index.
13326 if (die_is_class_type(&child))
13327 anonymous_member_type_index =
13328 ++anonymous_member_class_index;
13329 else if (dwarf_tag(&child) == DW_TAG_union_type)
13330 anonymous_member_type_index =
13331 ++anonymous_member_union_index;
13332 else if (dwarf_tag(&child) == DW_TAG_enumeration_type)
13333 anonymous_member_type_index =
13334 ++anonymous_member_enum_index;
13335 }
13336 // if the type is not already a member of this class,
13337 // then add it to the class.
13338 if ((is_anonymous_type_die(&child)
13339 && !lookup_class_typedef_or_enum_type_from_corpus
13340 (&child, anonymous_member_type_index, result.get()))
13341 || !result->find_member_type(die_name(&child)))
13342 build_ir_node_from_die(rdr, &child, result.get(),
13343 called_from_public_decl,
13344 where_offset);
13345 }
13346 } while (dwarf_siblingof(&child, &child) == 0);
13347 }
13348
13349 rdr.scope_stack().pop();
13350
13351 {
13352 die_class_or_union_map_type::const_iterator i =
13353 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13354 if (i != rdr.die_wip_classes_map(source).end())
13355 {
13356 if (is_member_type(i->second))
13358 get_member_access_specifier(i->second));
13359 rdr.die_wip_classes_map(source).erase(i);
13360 }
13361 }
13362
13363 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13364 return result;
13365}
13366
13367/// Build an @ref union_decl from a DW_TAG_union_type DIE.
13368///
13369/// @param rdr the DWARF reader to use.
13370///
13371/// @param die the DIE to read from.
13372///
13373/// @param scope the scope the resulting @ref union_decl belongs to.
13374///
13375/// @param union_type if this parameter is non-nil, then this function
13376/// updates the @ref union_decl that it points to, rather than
13377/// creating a new @ref union_decl.
13378///
13379/// @param called_from_public_decl is true if this function has been
13380/// initially called within the context of a public decl.
13381///
13382/// @param where_offset the offset of the DIE where we are "logically"
13383/// positionned at, in the DIE tree. This is useful when @p die is
13384/// e.g, DW_TAG_partial_unit that can be included in several places in
13385/// the DIE tree.
13386///
13387/// @param is_declaration_only is true if the DIE denoted by @p die is
13388/// a declaration-only DIE.
13389///
13390/// @return the resulting @ref union_decl type.
13391static union_decl_sptr
13392add_or_update_union_type(reader& rdr,
13393 Dwarf_Die* die,
13394 scope_decl* scope,
13395 union_decl_sptr union_type,
13396 bool called_from_public_decl,
13397 size_t where_offset,
13398 bool is_declaration_only)
13399{
13400 union_decl_sptr result;
13401 if (!die)
13402 return result;
13403
13404 unsigned tag = dwarf_tag(die);
13405
13406 if (tag != DW_TAG_union_type)
13407 return result;
13408
13409 const die_source source = rdr.get_die_source(die);
13410 {
13411 die_class_or_union_map_type::const_iterator i =
13412 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13413 if (i != rdr.die_wip_classes_map(source).end())
13414 {
13415 union_decl_sptr u = is_union_type(i->second);
13416 ABG_ASSERT(u);
13417 return u;
13418 }
13419 }
13420
13421 string name, linkage_name;
13422 location loc;
13423 die_loc_and_name(rdr, die, loc, name, linkage_name);
13424
13425 bool is_anonymous = false;
13426 if (name.empty())
13427 {
13428 // So we are looking at an anonymous union. Let's give it a
13429 // name.
13430 name = get_internal_anonymous_die_prefix_name(die);
13431 ABG_ASSERT(!name.empty());
13432 // But we remember that the type is anonymous.
13433 is_anonymous = true;
13434
13435 if (size_t s = scope->get_num_anonymous_member_unions())
13436 name = build_internal_anonymous_die_name(name, s);
13437 }
13438
13439 // If the type has location, then associate it to its
13440 // representation. This way, all occurences of types with the same
13441 // representation (name) and location can be later detected as being
13442 // for the same type.
13443
13444 if (!is_anonymous)
13445 {
13446 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13447 {
13448 if (loc)
13449 result = lookup_union_type_per_location(loc.expand(), *corp);
13450 else
13451 result = lookup_union_type(name, *corp);
13452
13453 if (result)
13454 {
13455 rdr.associate_die_to_type(die, result, where_offset);
13456 return result;
13457 }
13458 }
13459 }
13460
13461 // if we've already seen a union with the same union as 'die' then
13462 // let's re-use that one. We can't really safely re-use anonymous
13463 // unions as they have no name, by construction. What we can do,
13464 // rather, is to reuse the typedef that name them, when they do have
13465 // a naming typedef.
13466 if (!is_anonymous)
13467 if (union_decl_sptr pre_existing_union =
13468 is_union_type(rdr.lookup_artifact_from_die(die)))
13469 union_type = pre_existing_union;
13470
13471 uint64_t size = 0;
13472 die_size_in_bits(die, size);
13473 bool is_artificial = die_is_artificial(die);
13474
13475 if (union_type)
13476 {
13477 result = union_type;
13478 result->set_location(loc);
13479 }
13480 else
13481 {
13482 result.reset(new union_decl(rdr.env(), name, size, loc,
13483 decl_base::VISIBILITY_DEFAULT,
13484 is_anonymous));
13485 if (is_declaration_only)
13486 result->set_is_declaration_only(true);
13487 result = is_union_type(add_decl_to_scope(result, scope));
13488 ABG_ASSERT(result);
13489 }
13490
13491 if (size)
13492 {
13493 result->set_size_in_bits(size);
13494 result->set_is_declaration_only(false);
13495 }
13496
13497 result->set_is_artificial(is_artificial);
13498
13499 rdr.associate_die_to_type(die, result, where_offset);
13500
13501 rdr.maybe_schedule_declaration_only_class_for_resolution(result);
13502
13503 Dwarf_Die child;
13504 bool has_child = (dwarf_child(die, &child) == 0);
13505 if (!has_child)
13506 return result;
13507
13508 rdr.die_wip_classes_map(source)[dwarf_dieoffset(die)] = result;
13509
13510 scope_decl_sptr scop =
13511 dynamic_pointer_cast<scope_decl>(result);
13512 ABG_ASSERT(scop);
13513 rdr.scope_stack().push(scop.get());
13514
13515 if (has_child)
13516 {
13517 do
13518 {
13519 tag = dwarf_tag(&child);
13520 // Handle data members.
13521 if (tag == DW_TAG_member || tag == DW_TAG_variable)
13522 {
13523 Dwarf_Die type_die;
13524 if (!die_die_attribute(&child, DW_AT_type, type_die))
13525 continue;
13526
13527 string n, m;
13528 location loc;
13529 die_loc_and_name(rdr, &child, loc, n, m);
13530
13531 // Because we can be updating an existing union, let's
13532 // make sure we don't already have a member of the same
13533 // name. Anonymous member are handled a bit later below
13534 // so let's not consider them here.
13535 if (!n.empty() && lookup_var_decl_in_scope(n, result))
13536 continue;
13537
13538 ssize_t offset_in_bits = 0;
13539 decl_base_sptr ty =
13540 is_decl(build_ir_node_from_die(rdr, &type_die,
13541 called_from_public_decl,
13542 where_offset));
13543 type_base_sptr t = is_type(ty);
13544 if (!t)
13545 continue;
13546
13547 // We have a non-static data member. So this union
13548 // cannot be a declaration-only union anymore, even if
13549 // some DWARF emitters might consider it otherwise.
13550 result->set_is_declaration_only(false);
13551 access_specifier access = public_access;
13552
13553 die_access_specifier(&child, access);
13554
13555 var_decl_sptr dm(new var_decl(n, t, loc, m));
13556 // If dm is an anonymous data member, let's make sure
13557 // the current union doesn't already have it as a data
13558 // member.
13559 if (n.empty() && result->find_data_member(dm))
13560 continue;
13561
13562 result->add_data_member(dm, access, /*is_laid_out=*/true,
13563 /*is_static=*/false,
13564 offset_in_bits);
13565 ABG_ASSERT(has_scope(dm));
13566 rdr.associate_die_to_decl(&child, dm, where_offset,
13567 /*associate_by_repr=*/false);
13568 }
13569 // Handle member functions;
13570 else if (tag == DW_TAG_subprogram)
13571 {
13572 decl_base_sptr r =
13573 is_decl(build_ir_node_from_die(rdr, &child,
13574 result.get(),
13575 called_from_public_decl,
13576 where_offset));
13577 if (!r)
13578 continue;
13579
13580 function_decl_sptr f = dynamic_pointer_cast<function_decl>(r);
13581 ABG_ASSERT(f);
13582
13583 finish_member_function_reading(&child, f, result, rdr);
13584
13585 rdr.associate_die_to_decl(&child, f, where_offset,
13586 /*associate_by_repr=*/false);
13587 }
13588 // Handle member types
13589 else if (die_is_type(&child))
13590 decl_base_sptr td =
13591 is_decl(build_ir_node_from_die(rdr, &child, result.get(),
13592 called_from_public_decl,
13593 where_offset));
13594 } while (dwarf_siblingof(&child, &child) == 0);
13595 }
13596
13597 rdr.scope_stack().pop();
13598
13599 {
13600 die_class_or_union_map_type::const_iterator i =
13601 rdr.die_wip_classes_map(source).find(dwarf_dieoffset(die));
13602 if (i != rdr.die_wip_classes_map(source).end())
13603 {
13604 if (is_member_type(i->second))
13606 get_member_access_specifier(i->second));
13607 rdr.die_wip_classes_map(source).erase(i);
13608 }
13609 }
13610
13611 return result;
13612}
13613
13614/// build a qualified type from a DW_TAG_const_type,
13615/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
13616///
13617/// @param rdr the DWARF reader to consider.
13618///
13619/// @param die the input DIE to read from.
13620///
13621/// @param called_from_public_decl true if this function was called
13622/// from a context where either a public function or a public variable
13623/// is being built.
13624///
13625/// @param where_offset the offset of the DIE where we are "logically"
13626/// positionned at, in the DIE tree. This is useful when @p die is
13627/// e.g, DW_TAG_partial_unit that can be included in several places in
13628/// the DIE tree.
13629///
13630/// @return the resulting qualified_type_def.
13631static type_base_sptr
13632build_qualified_type(reader& rdr,
13633 Dwarf_Die* die,
13634 bool called_from_public_decl,
13635 size_t where_offset)
13636{
13637 type_base_sptr result;
13638 if (!die)
13639 return result;
13640
13641 unsigned tag = dwarf_tag(die);
13642
13643 if (tag != DW_TAG_const_type
13644 && tag != DW_TAG_volatile_type
13645 && tag != DW_TAG_restrict_type)
13646 return result;
13647
13648 Dwarf_Die underlying_type_die;
13649 decl_base_sptr utype_decl;
13650 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13651 // So, if no DW_AT_type is present, then this means (if we are
13652 // looking at a debug info emitted by GCC) that we are looking
13653 // at a qualified void type.
13654 utype_decl = build_ir_node_for_void_type(rdr);
13655
13656 if (!utype_decl)
13657 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
13658 called_from_public_decl,
13659 where_offset));
13660 if (!utype_decl)
13661 return result;
13662
13663 // The call to build_ir_node_from_die() could have triggered the
13664 // creation of the type for this DIE. In that case, just return it.
13665 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13666 {
13667 result = t;
13668 rdr.associate_die_to_type(die, result, where_offset);
13669 return result;
13670 }
13671
13672 type_base_sptr utype = is_type(utype_decl);
13673 ABG_ASSERT(utype);
13674
13675 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
13676 if (tag == DW_TAG_const_type)
13677 qual |= qualified_type_def::CV_CONST;
13678 else if (tag == DW_TAG_volatile_type)
13679 qual |= qualified_type_def::CV_VOLATILE;
13680 else if (tag == DW_TAG_restrict_type)
13681 qual |= qualified_type_def::CV_RESTRICT;
13682 else
13684
13685 if (!result)
13686 result.reset(new qualified_type_def(utype, qual, location()));
13687
13688 rdr.associate_die_to_type(die, result, where_offset);
13689
13690 return result;
13691}
13692
13693/// Walk a tree of typedef of qualified arrays and schedule all type
13694/// nodes for canonicalization.
13695///
13696/// This is to be used after an array tree has been cloned. In that
13697/// case, the newly cloned type nodes have to be scheduled for
13698/// canonicalization.
13699///
13700/// This is a subroutine of maybe_strip_qualification.
13701///
13702/// @param t the type node to be scheduled for canonicalization.
13703///
13704/// @param rdr the DWARF reader to use.
13705static void
13706schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
13707 reader &rdr)
13708{
13709 if (typedef_decl_sptr type = is_typedef(t))
13710 {
13711 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13712 rdr);
13713 rdr.schedule_type_for_late_canonicalization(t);
13714 }
13715 else if (qualified_type_def_sptr type = is_qualified_type(t))
13716 {
13717 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
13718 rdr);
13719 rdr.schedule_type_for_late_canonicalization(t);
13720 }
13721 else if (array_type_def_sptr type = is_array_type(t))
13722 {
13723 for (vector<array_type_def::subrange_sptr>::const_iterator i =
13724 type->get_subranges().begin();
13725 i != type->get_subranges().end();
13726 ++i)
13727 {
13728 if (!(*i)->get_scope())
13729 add_decl_to_scope(*i, rdr.cur_transl_unit()->get_global_scope());
13730 rdr.schedule_type_for_late_canonicalization(*i);
13731
13732 }
13733 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
13734 rdr);
13735 rdr.schedule_type_for_late_canonicalization(type);
13736 }
13737}
13738
13739/// Strip qualification from a qualified type, when it makes sense.
13740///
13741/// DWARF constructs "const reference". This is redundant because a
13742/// reference is always const. The issue is these redundant types then
13743/// leak into the IR and make for bad diagnostics.
13744///
13745/// This function thus strips the const qualifier from the type in
13746/// that case. It might contain code to strip other cases like this
13747/// in the future.
13748///
13749/// @param t the type to strip const qualification from.
13750///
13751/// @param rdr the @ref reader to use.
13752///
13753/// @return the stripped type or just return @p t.
13754static decl_base_sptr
13755maybe_strip_qualification(const qualified_type_def_sptr t,
13756 reader &rdr)
13757{
13758 if (!t)
13759 return t;
13760
13761 decl_base_sptr result = t;
13762 type_base_sptr u = t->get_underlying_type();
13763
13766 if (result.get() != t.get())
13767 return result;
13768
13770 {
13771 array_type_def_sptr array;
13772 scope_decl * scope = 0;
13773 if ((array = is_array_type(u)))
13774 {
13775 scope = array->get_scope();
13776 ABG_ASSERT(scope);
13777 array = is_array_type(clone_array_tree(array));
13778 schedule_array_tree_for_late_canonicalization(array, rdr);
13779 add_decl_to_scope(array, scope);
13780 t->set_underlying_type(array);
13781 u = t->get_underlying_type();
13782 }
13783 else if (is_typedef_of_array(u))
13784 {
13785 scope = is_decl(u)->get_scope();
13786 ABG_ASSERT(scope);
13787 typedef_decl_sptr typdef =
13789 schedule_array_tree_for_late_canonicalization(typdef, rdr);
13790 ABG_ASSERT(typdef);
13791 add_decl_to_scope(typdef, scope);
13792 t->set_underlying_type(typdef);
13793 u = t->get_underlying_type();
13794 array = is_typedef_of_array(u);
13795 }
13796 else
13798
13799 ABG_ASSERT(array);
13800 // We should not be editing types that are already canonicalized.
13801 ABG_ASSERT(!array->get_canonical_type());
13802 type_base_sptr element_type = array->get_element_type();
13803
13804 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
13805 {
13806 // We should not be editing types that are already canonicalized.
13807 ABG_ASSERT(!qualified->get_canonical_type());
13808 qualified_type_def::CV quals = qualified->get_cv_quals();
13809 quals |= t->get_cv_quals();
13810 qualified->set_cv_quals(quals);
13812 result = is_decl(u);
13813 }
13814 else
13815 {
13816 qualified_type_def_sptr qual_type
13817 (new qualified_type_def(element_type,
13818 t->get_cv_quals(),
13819 t->get_location()));
13821 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
13822 array->set_element_type(qual_type);
13823 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
13824 result = is_decl(u);
13825 }
13826 }
13827
13828 return result;
13829}
13830
13831/// Build a pointer type from a DW_TAG_pointer_type DIE.
13832///
13833/// @param rdr the DWARF reader to consider.
13834///
13835/// @param die the DIE to read information from.
13836///
13837/// @param called_from_public_decl true if this function was called
13838/// from a context where either a public function or a public variable
13839/// is being built.
13840///
13841/// @param where_offset the offset of the DIE where we are "logically"
13842/// positionned at, in the DIE tree. This is useful when @p die is
13843/// e.g, DW_TAG_partial_unit that can be included in several places in
13844/// the DIE tree.
13845///
13846/// @return the resulting pointer to pointer_type_def.
13848build_pointer_type_def(reader& rdr,
13849 Dwarf_Die* die,
13850 bool called_from_public_decl,
13851 size_t where_offset)
13852{
13853 pointer_type_def_sptr result;
13854
13855 if (!die)
13856 return result;
13857
13858 unsigned tag = dwarf_tag(die);
13859 if (tag != DW_TAG_pointer_type)
13860 return result;
13861
13862 type_or_decl_base_sptr utype_decl;
13863 Dwarf_Die underlying_type_die;
13864 bool has_underlying_type_die = false;
13865 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13866 // If the DW_AT_type attribute is missing, that means we are
13867 // looking at a pointer to "void".
13868 utype_decl = build_ir_node_for_void_type(rdr);
13869 else
13870 has_underlying_type_die = true;
13871
13872 if (!utype_decl && has_underlying_type_die)
13873 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
13874 called_from_public_decl,
13875 where_offset);
13876 if (!utype_decl)
13877 return result;
13878
13879 // The call to build_ir_node_from_die() could have triggered the
13880 // creation of the type for this DIE. In that case, just return it.
13881 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13882 {
13883 result = is_pointer_type(t);
13884 ABG_ASSERT(result);
13885 return result;
13886 }
13887
13888 type_base_sptr utype = is_type(utype_decl);
13889 ABG_ASSERT(utype);
13890
13891 // if the DIE for the pointer type doesn't have a byte_size
13892 // attribute then we assume the size of the pointer is the address
13893 // size of the current translation unit.
13894 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13895 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
13896 // The size as expressed by DW_AT_byte_size is in byte, so let's
13897 // convert it to bits.
13898 size *= 8;
13899
13900 // And the size of the pointer must be the same as the address size
13901 // of the current translation unit.
13902 ABG_ASSERT((size_t) rdr.cur_transl_unit()->get_address_size() == size);
13903
13904 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
13905 ABG_ASSERT(result->get_pointed_to_type());
13906
13907 if (is_void_pointer_type(result))
13908 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr));
13909
13910 rdr.associate_die_to_type(die, result, where_offset);
13911 return result;
13912}
13913
13914/// Build a reference type from either a DW_TAG_reference_type or
13915/// DW_TAG_rvalue_reference_type DIE.
13916///
13917/// @param rdr the DWARF reader to consider.
13918///
13919/// @param die the DIE to read from.
13920///
13921/// @param called_from_public_decl true if this function was called
13922/// from a context where either a public function or a public variable
13923/// is being built.
13924///
13925/// @param where_offset the offset of the DIE where we are "logically"
13926/// positionned at, in the DIE tree. This is useful when @p die is
13927/// e.g, DW_TAG_partial_unit that can be included in several places in
13928/// the DIE tree.
13929///
13930/// @return a pointer to the resulting reference_type_def.
13932build_reference_type(reader& rdr,
13933 Dwarf_Die* die,
13934 bool called_from_public_decl,
13935 size_t where_offset)
13936{
13938
13939 if (!die)
13940 return result;
13941
13942 unsigned tag = dwarf_tag(die);
13943 if (tag != DW_TAG_reference_type
13944 && tag != DW_TAG_rvalue_reference_type)
13945 return result;
13946
13947 Dwarf_Die underlying_type_die;
13948 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13949 return result;
13950
13951 type_or_decl_base_sptr utype_decl =
13952 build_ir_node_from_die(rdr, &underlying_type_die,
13953 called_from_public_decl,
13954 where_offset);
13955 if (!utype_decl)
13956 return result;
13957
13958 // The call to build_ir_node_from_die() could have triggered the
13959 // creation of the type for this DIE. In that case, just return it.
13960 if (type_base_sptr t = rdr.lookup_type_from_die(die))
13961 {
13962 result = is_reference_type(t);
13963 ABG_ASSERT(result);
13964 return result;
13965 }
13966
13967 type_base_sptr utype = is_type(utype_decl);
13968 ABG_ASSERT(utype);
13969
13970 // if the DIE for the reference type doesn't have a byte_size
13971 // attribute then we assume the size of the reference is the address
13972 // size of the current translation unit.
13973 uint64_t size = rdr.cur_transl_unit()->get_address_size();
13974 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
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 bool is_lvalue = tag == DW_TAG_reference_type;
13982
13983 result.reset(new reference_type_def(utype, is_lvalue, size,
13984 /*alignment=*/0,
13985 location()));
13986 if (corpus_sptr corp = rdr.corpus())
13987 if (reference_type_def_sptr t = lookup_reference_type(*result, *corp))
13988 result = t;
13989 rdr.associate_die_to_type(die, result, where_offset);
13990 return result;
13991}
13992
13993/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
13994///
13995/// @param rdr the DWARF reader to consider.
13996///
13997/// @param die the DIE to read from.
13998///
13999/// @param is_method points to a class or union declaration iff we're
14000/// building the type for a method. This is the enclosing class or
14001/// union of the method.
14002///
14003/// @param where_offset the offset of the DIE where we are "logically"
14004/// positioned at, in the DIE tree. This is useful when @p die is
14005/// e.g, DW_TAG_partial_unit that can be included in several places in
14006/// the DIE tree.
14007///
14008/// @return a pointer to the resulting function_type_sptr.
14009static function_type_sptr
14010build_function_type(reader& rdr,
14011 Dwarf_Die* die,
14012 class_or_union_sptr is_method,
14013 size_t where_offset)
14014{
14015 function_type_sptr result;
14016
14017 if (!die)
14018 return result;
14019
14020 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
14021 || dwarf_tag(die) == DW_TAG_subprogram);
14022
14023 const die_source source = rdr.get_die_source(die);
14024
14025 {
14026 size_t off = dwarf_dieoffset(die);
14027 auto i = rdr.die_wip_function_types_map(source).find(off);
14028 if (i != rdr.die_wip_function_types_map(source).end())
14029 {
14030 function_type_sptr fn_type = is_function_type(i->second);
14031 ABG_ASSERT(fn_type);
14032 return fn_type;
14033 }
14034 }
14035
14036 decl_base_sptr type_decl;
14037
14038 translation_unit_sptr tu = rdr.cur_transl_unit();
14039 ABG_ASSERT(tu);
14040
14041 /// If, inside the current translation unit, we've already seen a
14042 /// function type with the same text representation, then reuse that
14043 /// one instead.
14044 if (type_base_sptr t = rdr.lookup_fn_type_from_die_repr_per_tu(die))
14045 {
14046 result = is_function_type(t);
14047 ABG_ASSERT(result);
14048 rdr.associate_die_to_type(die, result, where_offset);
14049 return result;
14050 }
14051
14052 bool odr_is_relevant = rdr.odr_is_relevant(die);
14053 if (odr_is_relevant)
14054 {
14055 // So we can rely on the One Definition Rule to say that if
14056 // several different function types have the same name (or
14057 // rather, representation) across the entire binary, then they
14058 // ought to designate the same function type. So let's ensure
14059 // that if we've already seen a function type with the same
14060 // representation as the function type 'die', then it's the same
14061 // type as the one denoted by 'die'.
14062 if (function_type_sptr fn_type =
14063 is_function_type(rdr.lookup_type_artifact_from_die(die)))
14064 {
14065 rdr.associate_die_to_type(die, fn_type, where_offset);
14066 return fn_type;
14067 }
14068 }
14069
14070 // Let's look at the DIE to detect if it's the DIE for a method
14071 // (type). If it is, we can deduce the name of its enclosing class
14072 // and if it's a static or const.
14073 bool is_const = false;
14074 bool is_static = false;
14075 Dwarf_Die object_pointer_die;
14076 Dwarf_Die class_type_die;
14077 bool has_this_parm_die =
14078 die_function_type_is_method_type(rdr, die, where_offset,
14079 object_pointer_die,
14080 class_type_die,
14081 is_static);
14082 if (has_this_parm_die)
14083 {
14084 // The function (type) has a "this" parameter DIE. It means it's
14085 // a member function DIE.
14086 if (!is_static)
14087 if (die_object_pointer_is_for_const_method(&object_pointer_die))
14088 is_const = true;
14089
14090 if (!is_method)
14091 {
14092 // We were initially called as if the function represented
14093 // by DIE was *NOT* a member function. But now we know it's
14094 // a member function. Let's take that into account.
14095 class_or_union_sptr klass_type =
14096 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
14097 /*called_from_pub_decl=*/true,
14098 where_offset));
14099 ABG_ASSERT(klass_type);
14100 is_method = klass_type;
14101 }
14102 }
14103
14104 // Let's create the type early and record it as being for the DIE
14105 // 'die'. This way, when building the sub-type triggers the
14106 // creation of a type matching the same 'die', then we'll reuse this
14107 // one.
14108
14109 result.reset(is_method
14110 ? new method_type(is_method, is_const,
14111 tu->get_address_size(),
14112 /*alignment=*/0)
14113 : new function_type(rdr.env(), tu->get_address_size(),
14114 /*alignment=*/0));
14115 rdr.associate_die_to_type(die, result, where_offset);
14116 rdr.die_wip_function_types_map(source)[dwarf_dieoffset(die)] = result;
14117
14118 type_base_sptr return_type;
14119 Dwarf_Die ret_type_die;
14120 if (die_die_attribute(die, DW_AT_type, ret_type_die))
14121 return_type =
14122 is_type(build_ir_node_from_die(rdr, &ret_type_die,
14123 /*called_from_public_decl=*/true,
14124 where_offset));
14125 if (!return_type)
14126 return_type = is_type(build_ir_node_for_void_type(rdr));
14127 result->set_return_type(return_type);
14128
14129 Dwarf_Die child;
14130 function_decl::parameters function_parms;
14131
14132 if (dwarf_child(die, &child) == 0)
14133 do
14134 {
14135 int child_tag = dwarf_tag(&child);
14136 if (child_tag == DW_TAG_formal_parameter)
14137 {
14138 // This is a "normal" function parameter.
14139 string name, linkage_name;
14140 location loc;
14141 die_loc_and_name(rdr, &child, loc, name, linkage_name);
14143 // Sometimes, bogus compiler emit names that are
14144 // non-ascii garbage. Let's just ditch that for now.
14145 name.clear();
14146 bool is_artificial = die_is_artificial(&child);
14147 type_base_sptr parm_type;
14148 Dwarf_Die parm_type_die;
14149 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
14150 parm_type =
14151 is_type(build_ir_node_from_die(rdr, &parm_type_die,
14152 /*called_from_public_decl=*/true,
14153 where_offset));
14154 if (!parm_type)
14155 continue;
14157 (new function_decl::parameter(parm_type, name, loc,
14158 /*variadic_marker=*/false,
14159 is_artificial));
14160 function_parms.push_back(p);
14161 }
14162 else if (child_tag == DW_TAG_unspecified_parameters)
14163 {
14164 // This is a variadic function parameter.
14165 bool is_artificial = die_is_artificial(&child);
14166
14167 type_base_sptr parm_type =
14168 is_type(build_ir_node_for_variadic_parameter_type(rdr));
14170 (new function_decl::parameter(parm_type,
14171 /*name=*/"",
14172 location(),
14173 /*variadic_marker=*/true,
14174 is_artificial));
14175 function_parms.push_back(p);
14176 // After a DW_TAG_unspecified_parameters tag, we shouldn't
14177 // keep reading for parameters. The
14178 // unspecified_parameters TAG should be the last parameter
14179 // that we record. For instance, if there are multiple
14180 // DW_TAG_unspecified_parameters DIEs then we should care
14181 // only for the first one.
14182 break;
14183 }
14184 }
14185 while (dwarf_siblingof(&child, &child) == 0);
14186
14187 result->set_parameters(function_parms);
14188
14189 tu->bind_function_type_life_time(result);
14190
14191 result->set_is_artificial(true);
14192
14193 rdr.associate_die_repr_to_fn_type_per_tu(die, result);
14194
14195 {
14196 die_function_type_map_type::const_iterator i =
14197 rdr.die_wip_function_types_map(source).
14198 find(dwarf_dieoffset(die));
14199 if (i != rdr.die_wip_function_types_map(source).end())
14200 rdr.die_wip_function_types_map(source).erase(i);
14201 }
14202
14203 maybe_canonicalize_type(result, rdr);
14204 return result;
14205}
14206
14207/// Build a subrange type from a DW_TAG_subrange_type.
14208///
14209/// @param rdr the DWARF reader to consider.
14210///
14211/// @param die the DIE to read from.
14212///
14213/// @param where_offset the offset of the DIE where we are "logically"
14214/// positionned at in the DIE tree. This is useful when @p die is
14215/// e,g, DW_TAG_partial_unit that can be included in several places in
14216/// the DIE tree.
14217///
14218/// @param associate_die_to_type if this is true then the resulting
14219/// type is associated to the @p die, so that next time when the
14220/// system looks up the type associated to it, the current resulting
14221/// type is returned. If false, then no association is done and the
14222/// resulting type can be destroyed right after. This can be useful
14223/// when the sole purpose of building the @ref
14224/// array_type_def::subrange_type is to use some of its method like,
14225/// e.g, its name pretty printing methods.
14226///
14227/// @return the newly built instance of @ref
14228/// array_type_def::subrange_type, or nil if no type could be built.
14230build_subrange_type(reader& rdr,
14231 const Dwarf_Die* die,
14232 size_t where_offset,
14233 bool associate_type_to_die)
14234{
14236
14237 if (!die)
14238 return result;
14239
14240 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
14241 if (tag != DW_TAG_subrange_type)
14242 return result;
14243
14244 string name = die_name(die);
14245
14246 // load the underlying type.
14247 Dwarf_Die underlying_type_die;
14248 type_base_sptr underlying_type;
14249 /* Unless there is an underlying type which says differently. */
14250 bool is_signed = false;
14251 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
14252 underlying_type =
14253 is_type(build_ir_node_from_die(rdr,
14254 &underlying_type_die,
14255 /*called_from_public_decl=*/true,
14256 where_offset));
14257
14258 if (underlying_type)
14259 {
14260 uint64_t ate;
14261 if (die_unsigned_constant_attribute (&underlying_type_die,
14262 DW_AT_encoding,
14263 ate))
14264 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
14265 }
14266
14267 translation_unit::language language = rdr.cur_transl_unit()->get_language();
14268 array_type_def::subrange_type::bound_value lower_bound =
14269 get_default_array_lower_bound(language);
14270 array_type_def::subrange_type::bound_value upper_bound;
14271 uint64_t count = 0;
14272 bool is_infinite = false;
14273 bool count_present = false;
14274
14275 // The DWARF 4 specifications says, in [5.11 Subrange
14276 // Type Entries]:
14277 //
14278 // The subrange entry may have the attributes
14279 // DW_AT_lower_bound and DW_AT_upper_bound to
14280 // specify, respectively, the lower and upper bound
14281 // values of the subrange.
14282 //
14283 // So let's look for DW_AT_lower_bound first.
14284 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
14285
14286 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
14287 is_signed, upper_bound);
14288 if (!found_upper_bound)
14289 found_upper_bound = subrange_die_indirect_bound_value(die,
14290 DW_AT_upper_bound,
14291 upper_bound,
14292 is_signed);
14293 // Then, DW_AT_upper_bound.
14294 if (!found_upper_bound)
14295 {
14296 // The DWARF 4 spec says, in [5.11 Subrange Type
14297 // Entries]:
14298 //
14299 // The DW_AT_upper_bound attribute may be replaced
14300 // by a DW_AT_count attribute, whose value
14301 // describes the number of elements in the
14302 // subrange rather than the value of the last
14303 // element."
14304 //
14305 // So, as DW_AT_upper_bound is not present in this
14306 // case, let's see if there is a DW_AT_count.
14307 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
14308 {
14309 count_present = true;
14310 // We can deduce the upper_bound from the
14311 // lower_bound and the number of elements of the
14312 // array:
14313 int64_t u = lower_bound.get_signed_value() + count;
14314 upper_bound = u - 1;
14315 }
14316
14317 if (!count_present)
14318 // No upper_bound nor count was present on the DIE, this means
14319 // the array is considered to have an infinite (or rather not
14320 // known) size.
14321 is_infinite = true;
14322 }
14323
14324 if (UINT64_MAX == upper_bound.get_unsigned_value())
14325 // If the upper_bound size is the max of the integer value
14326 // then it most certainly means unknown size.
14327 is_infinite = true;
14328
14329 result.reset
14330 (new array_type_def::subrange_type(rdr.env(),
14331 name,
14332 lower_bound,
14333 upper_bound,
14334 location()));
14335 result->is_infinite(is_infinite);
14336
14337 if (underlying_type)
14338 result->set_underlying_type(underlying_type);
14339
14340 // Let's ensure the resulting subrange looks metabolically healhty.
14341 ABG_ASSERT(result->is_infinite()
14342 || (result->get_length() ==
14343 (uint64_t) (result->get_upper_bound()
14344 - result->get_lower_bound() + 1)));
14345
14346 if (associate_type_to_die)
14347 rdr.associate_die_to_type(die, result, where_offset);
14348
14349 return result;
14350}
14351
14352/// Build the sub-ranges of an array type.
14353///
14354/// This is a sub-routine of build_array_type().
14355///
14356/// @param rdr the context to read from.
14357///
14358/// @param die the DIE of tag DW_TAG_array_type which contains
14359/// children DIEs that represent the sub-ranges.
14360///
14361/// @param subranges out parameter. This is set to the sub-ranges
14362/// that are built from @p die.
14363///
14364/// @param where_offset the offset of the DIE where we are "logically"
14365/// positioned at, in the DIE tree. This is useful when @p die is
14366/// e.g, DW_TAG_partial_unit that can be included in several places in
14367/// the DIE tree.
14368static void
14369build_subranges_from_array_type_die(reader& rdr,
14370 const Dwarf_Die* die,
14372 size_t where_offset,
14373 bool associate_type_to_die)
14374{
14375 Dwarf_Die child;
14376
14377 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
14378 {
14379 do
14380 {
14381 int child_tag = dwarf_tag(&child);
14382 if (child_tag == DW_TAG_subrange_type)
14383 {
14385 if (associate_type_to_die)
14386 {
14387 // We are being called to create the type, add it to
14388 // the current type graph and associate it to the
14389 // DIE it's been created from.
14391 build_ir_node_from_die(rdr, &child,
14392 /*called_from_public_decl=*/true,
14393 where_offset);
14394 s = is_subrange_type(t);
14395 }
14396 else
14397 // We are being called to create the type but *NOT*
14398 // add it to the current tyupe tree, *NOR* associate
14399 // it to the DIE it's been created from.
14400 s = build_subrange_type(rdr, &child,
14401 where_offset,
14402 /*associate_type_to_die=*/false);
14403 if (s)
14404 subranges.push_back(s);
14405 }
14406 }
14407 while (dwarf_siblingof(&child, &child) == 0);
14408 }
14409}
14410
14411/// Build an array type from a DW_TAG_array_type DIE.
14412///
14413/// @param rdr the DWARF reader to consider.
14414///
14415/// @param die the DIE to read from.
14416///
14417/// @param called_from_public_decl true if this function was called
14418/// from a context where either a public function or a public variable
14419/// is being built.
14420///
14421/// @param where_offset the offset of the DIE where we are "logically"
14422/// positioned at, in the DIE tree. This is useful when @p die is
14423/// e.g, DW_TAG_partial_unit that can be included in several places in
14424/// the DIE tree.
14425///
14426/// @return a pointer to the resulting array_type_def.
14428build_array_type(reader& rdr,
14429 Dwarf_Die* die,
14430 bool called_from_public_decl,
14431 size_t where_offset)
14432{
14433 array_type_def_sptr result;
14434
14435 if (!die)
14436 return result;
14437
14438 unsigned tag = dwarf_tag(die);
14439 if (tag != DW_TAG_array_type)
14440 return result;
14441
14442 decl_base_sptr type_decl;
14443 Dwarf_Die type_die;
14444
14445 if (die_die_attribute(die, DW_AT_type, type_die))
14446 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
14447 called_from_public_decl,
14448 where_offset));
14449 if (!type_decl)
14450 return result;
14451
14452 // The call to build_ir_node_from_die() could have triggered the
14453 // creation of the type for this DIE. In that case, just return it.
14454 if (type_base_sptr t = rdr.lookup_type_from_die(die))
14455 {
14456 result = is_array_type(t);
14457 ABG_ASSERT(result);
14458 return result;
14459 }
14460
14461 type_base_sptr type = is_type(type_decl);
14462 ABG_ASSERT(type);
14463
14465
14466 build_subranges_from_array_type_die(rdr, die, subranges, where_offset);
14467
14468 result.reset(new array_type_def(type, subranges, location()));
14469
14470 return result;
14471}
14472
14473/// Create a typedef_decl from a DW_TAG_typedef DIE.
14474///
14475/// @param rdr the DWARF reader to consider.
14476///
14477/// @param die the DIE to read from.
14478///
14479/// @param called_from_public_decl true if this function was called
14480/// from a context where either a public function or a public variable
14481/// is being built.
14482///
14483/// @param where_offset the offset of the DIE where we are "logically"
14484/// positionned at, in the DIE tree. This is useful when @p die is
14485/// e.g, DW_TAG_partial_unit that can be included in several places in
14486/// the DIE tree.
14487///
14488/// @return the newly created typedef_decl.
14489static typedef_decl_sptr
14490build_typedef_type(reader& rdr,
14491 Dwarf_Die* die,
14492 bool called_from_public_decl,
14493 size_t where_offset)
14494{
14495 typedef_decl_sptr result;
14496
14497 if (!die)
14498 return result;
14499
14500 unsigned tag = dwarf_tag(die);
14501 if (tag != DW_TAG_typedef)
14502 return result;
14503
14504 string name, linkage_name;
14505 location loc;
14506 die_loc_and_name(rdr, die, loc, name, linkage_name);
14507
14508 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
14509 if (loc)
14510 result = lookup_typedef_type_per_location(loc.expand(), *corp);
14511
14512 if (!result)
14513 {
14514 type_base_sptr utype;
14515 Dwarf_Die underlying_type_die;
14516 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
14517 // A typedef DIE with no underlying type means a typedef to
14518 // void type.
14519 utype = rdr.env().get_void_type();
14520
14521 if (!utype)
14522 utype =
14523 is_type(build_ir_node_from_die(rdr,
14524 &underlying_type_die,
14525 called_from_public_decl,
14526 where_offset));
14527 if (!utype)
14528 return result;
14529
14530 ABG_ASSERT(utype);
14531 result.reset(new typedef_decl(name, utype, loc, linkage_name));
14532
14533 if ((is_class_or_union_type(utype) || is_enum_type(utype))
14534 && is_anonymous_type(utype))
14535 {
14536 // This is a naming typedef for an enum or a class. Let's
14537 // mark the underlying decl as such.
14538 decl_base_sptr decl = is_decl(utype);
14539 ABG_ASSERT(decl);
14540 decl->set_naming_typedef(result);
14541 }
14542 }
14543
14544 rdr.associate_die_to_type(die, result, where_offset);
14545
14546 return result;
14547}
14548
14549/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
14550/// denoted by the DIE is not suppressed by a suppression
14551/// specification associated to the current DWARF reader.
14552///
14553/// Note that if a member variable declaration with the same name as
14554/// the name of the DIE we are looking at exists, this function returns
14555/// that existing variable declaration.
14556///
14557/// @param rdr the DWARF reader to use.
14558///
14559/// @param die the DIE representing the variable we are looking at.
14560///
14561/// @param where_offset the offset of the DIE where we are "logically"
14562/// positionned at, in the DIE tree. This is useful when @p die is
14563/// e.g, DW_TAG_partial_unit that can be included in several places in
14564/// the DIE tree.
14565///
14566/// @param result if this is set to an existing var_decl, this means
14567/// that the function will append the new properties it sees on @p die
14568/// to that exising var_decl. Otherwise, if this parameter is NULL, a
14569/// new var_decl is going to be allocated and returned.
14570///
14571/// @param is_required_decl_spec this is true iff the variable to
14572/// build is referred to as being the specification of another
14573/// variable.
14574///
14575/// @return a pointer to the newly created var_decl. If the var_decl
14576/// could not be built, this function returns NULL.
14577static var_decl_sptr
14578build_or_get_var_decl_if_not_suppressed(reader& rdr,
14579 scope_decl *scope,
14580 Dwarf_Die *die,
14581 size_t where_offset,
14582 var_decl_sptr result,
14583 bool is_required_decl_spec)
14584{
14585 var_decl_sptr var;
14586 if (variable_is_suppressed(rdr, scope, die, is_required_decl_spec))
14587 return var;
14588
14589 if (class_decl* class_type = is_class_type(scope))
14590 {
14591 string var_name = die_name(die);
14592 if (!var_name.empty())
14593 if ((var = class_type->find_data_member(var_name)))
14594 return var;
14595 }
14596 var = build_var_decl(rdr, die, where_offset, result);
14597 return var;
14598}
14599
14600/// Build a @ref var_decl out of a DW_TAG_variable DIE.
14601///
14602/// @param rdr the DWARF reader to use.
14603///
14604/// @param die the DIE representing the variable we are looking at.
14605///
14606/// @param where_offset the offset of the DIE where we are "logically"
14607/// positionned at, in the DIE tree. This is useful when @p die is
14608/// e.g, DW_TAG_partial_unit that can be included in several places in
14609/// the DIE tree.
14610///
14611/// @param result if this is set to an existing var_decl, this means
14612/// that the function will append the new properties it sees on @p die
14613/// to that exising var_decl. Otherwise, if this parameter is NULL, a
14614/// new var_decl is going to be allocated and returned.
14615///
14616/// @return a pointer to the newly created var_decl. If the var_decl
14617/// could not be built, this function returns NULL.
14618static var_decl_sptr
14619build_var_decl(reader& rdr,
14620 Dwarf_Die *die,
14621 size_t where_offset,
14622 var_decl_sptr result)
14623{
14624 if (!die)
14625 return result;
14626
14627 int tag = dwarf_tag(die);
14628 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
14629
14630 if (!die_is_public_decl(die))
14631 return result;
14632
14633 type_base_sptr type;
14634 Dwarf_Die type_die;
14635 if (die_die_attribute(die, DW_AT_type, type_die))
14636 {
14637 decl_base_sptr ty =
14638 is_decl(build_ir_node_from_die(rdr, &type_die,
14639 /*called_from_public_decl=*/true,
14640 where_offset));
14641 if (!ty)
14642 return result;
14643 type = is_type(ty);
14644 ABG_ASSERT(type);
14645 }
14646
14647 if (!type && !result)
14648 return result;
14649
14650 string name, linkage_name;
14651 location loc;
14652 die_loc_and_name(rdr, die, loc, name, linkage_name);
14653
14654 if (!result)
14655 result.reset(new var_decl(name, type, loc, linkage_name));
14656 else
14657 {
14658 // We were called to append properties that might have been
14659 // missing from the first version of the variable. And usually
14660 // that missing property is the mangled name or the type.
14661 if (!linkage_name.empty())
14662 result->set_linkage_name(linkage_name);
14663
14664 if (type)
14665 result->set_type(type);
14666 }
14667
14668 // Check if a variable symbol with this name is exported by the elf
14669 // binary. If it is, then set the symbol of the variable, if it's
14670 // not set already.
14671 if (!result->get_symbol())
14672 {
14673 elf_symbol_sptr var_sym;
14674 Dwarf_Addr var_addr;
14675
14676 if (rdr.get_variable_address(die, var_addr))
14677 {
14678 rdr.symtab()->
14679 update_main_symbol(var_addr,
14680 result->get_linkage_name().empty()
14681 ? result->get_name()
14682 : result->get_linkage_name());
14683 var_sym = rdr.variable_symbol_is_exported(var_addr);
14684 }
14685
14686 if (var_sym)
14687 {
14688 result->set_symbol(var_sym);
14689 // If the linkage name is not set or is wrong, set it to
14690 // the name of the underlying symbol.
14691 string linkage_name = result->get_linkage_name();
14692 if (linkage_name.empty()
14693 || !var_sym->get_alias_from_name(linkage_name))
14694 result->set_linkage_name(var_sym->get_name());
14695 result->set_is_in_public_symbol_table(true);
14696 }
14697 }
14698
14699 return result;
14700}
14701
14702/// Test if a given function denoted by its DIE and its scope is
14703/// suppressed by any of the suppression specifications associated to
14704/// a given context of ELF/DWARF reading.
14705///
14706/// Note that a non-member function which symbol is not exported is
14707/// also suppressed.
14708///
14709/// @param rdr the ELF/DWARF reading content of interest.
14710///
14711/// @param scope of the scope of the function.
14712///
14713/// @param function_die the DIE representing the function.
14714///
14715/// @param is_declaration_only is true if the DIE denoted by @p die is
14716/// a declaration-only DIE.
14717///
14718/// @return true iff @p function_die is suppressed by at least one
14719/// suppression specification attached to the @p rdr.
14720static bool
14721function_is_suppressed(const reader& rdr,
14722 const scope_decl* scope,
14723 Dwarf_Die *function_die,
14724 bool is_declaration_only)
14725{
14726 if (function_die == 0
14727 || dwarf_tag(function_die) != DW_TAG_subprogram)
14728 return false;
14729
14730 string fname = die_string_attribute(function_die, DW_AT_name);
14731 string flinkage_name = die_linkage_name(function_die);
14732 if (flinkage_name.empty() && rdr.die_is_in_c(function_die))
14733 flinkage_name = fname;
14734 string qualified_name = build_qualified_name(scope, fname);
14735
14736 // A non-member non-static function which symbol is not exported is
14737 // suppressed.
14738 //
14739 // Note that if the non-member non-static function has an undefined
14740 // symbol, by default, it's not suppressed. Unless we are asked to
14741 // drop undefined symbols too.
14742 if (!is_class_type(scope)
14743 && (!is_declaration_only || rdr.drop_undefined_syms()))
14744 {
14745 Dwarf_Addr fn_addr;
14746 if (!rdr.get_function_address(function_die, fn_addr))
14747 return true;
14748
14749 elf_symbol_sptr symbol =
14750 rdr.function_symbol_is_exported(fn_addr);
14751 if (!symbol)
14752 return true;
14753 if (!symbol->is_suppressed())
14754 return false;
14755
14756 // Since there is only one symbol in DWARF associated with an elf_symbol,
14757 // we can assume this is the main symbol then. Otherwise the main hinting
14758 // did not work as expected.
14759 ABG_ASSERT(symbol->is_main_symbol());
14760 if (symbol->has_aliases())
14761 for (elf_symbol_sptr a = symbol->get_next_alias();
14762 !a->is_main_symbol(); a = a->get_next_alias())
14763 if (!a->is_suppressed())
14764 return false;
14765 }
14766
14767 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
14768 /*require_drop_property=*/true);
14769}
14770
14771/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
14772/// function denoted by the DIE is not suppressed by a suppression
14773/// specification associated to the current DWARF reader.
14774///
14775/// Note that if a member function declaration with the same signature
14776/// (pretty representation) as one of the DIE we are looking at
14777/// exists, this function returns that existing function declaration.
14778/// Similarly, if there is already a constructed member function with
14779/// the same linkage name as the one on the DIE, this function returns
14780/// that member function.
14781///
14782/// Also note that the function_decl IR returned by this function must
14783/// be passed to finish_member_function_reading because several
14784/// properties from the DIE are actually read by that function, and
14785/// the corresponding properties on the function_decl IR are updated
14786/// accordingly. This is done to support "updating" a function_decl
14787/// IR with properties scathered across several DIEs.
14788///
14789/// @param rdr the DWARF reader to use.
14790///
14791/// @param scope the scope of the function we are looking at.
14792///
14793/// @param fn_die the DIE representing the function we are looking at.
14794///
14795/// @param where_offset the offset of the DIE where we are "logically"
14796/// positionned at, in the DIE tree. This is useful when @p die is
14797/// e.g, DW_TAG_partial_unit that can be included in several places in
14798/// the DIE tree.
14799///
14800/// @param is_declaration_only is true if the DIE denoted by @p fn_die
14801/// is a declaration-only DIE.
14802///
14803/// @param result if this is set to an existing function_decl, this
14804/// means that the function will append the new properties it sees on
14805/// @p fn_die to that exising function_decl. Otherwise, if this
14806/// parameter is NULL, a new function_decl is going to be allocated
14807/// and returned.
14808///
14809/// @return a pointer to the newly created var_decl. If the var_decl
14810/// could not be built, this function returns NULL.
14811static function_decl_sptr
14812build_or_get_fn_decl_if_not_suppressed(reader& rdr,
14813 scope_decl *scope,
14814 Dwarf_Die *fn_die,
14815 size_t where_offset,
14816 bool is_declaration_only,
14817 function_decl_sptr result)
14818{
14820 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
14821 return fn;
14822
14823 string name = die_name(fn_die);
14824 string linkage_name = die_linkage_name(fn_die);
14825 bool is_dtor = !name.empty() && name[0]== '~';
14826 bool is_virtual = false;
14827 if (is_dtor)
14828 {
14829 Dwarf_Attribute attr;
14830 if (dwarf_attr_integrate(const_cast<Dwarf_Die*>(fn_die),
14831 DW_AT_vtable_elem_location,
14832 &attr))
14833 is_virtual = true;
14834 }
14835
14836
14837 // If we've already built an IR for a function with the same
14838 // signature (from another DIE), reuse it, unless that function is a
14839 // virtual C++ destructor. Several virtual C++ destructors with the
14840 // same signature can be implemented by several different ELF
14841 // symbols. So re-using C++ destructors like that can lead to us
14842 // missing some destructors.
14843 if (!result && (!(is_dtor && is_virtual)))
14844 if ((fn = is_function_decl(rdr.lookup_artifact_from_die(fn_die))))
14845 {
14846 fn = maybe_finish_function_decl_reading(rdr, fn_die, where_offset, fn);
14847 rdr.associate_die_to_decl(fn_die, fn, /*do_associate_by_repr=*/true);
14848 rdr.associate_die_to_type(fn_die, fn->get_type(), where_offset);
14849 return fn;
14850 }
14851
14852 // If a member function with the same linkage name as the one
14853 // carried by the DIE already exists, then return it.
14854 if (class_decl* klass = is_class_type(scope))
14855 {
14856 string linkage_name = die_linkage_name(fn_die);
14857 fn = klass->find_member_function_sptr(linkage_name);
14858 if (fn)
14859 // We found a member function that has the same signature.
14860 // Let's mark it for update.
14861 result = fn;
14862 }
14863
14864 if (!fn || !fn->get_symbol())
14865 // We haven't yet been able to construct a function IR, or, we
14866 // have one 'partial' function IR that doesn't have any associated
14867 // symbol yet. Note that in the later case, a function IR without
14868 // any associated symbol will be dropped on the floor by
14869 // potential_member_fn_should_be_dropped. So let's build or a new
14870 // function IR or complete the existing partial IR.
14871 fn = build_function_decl(rdr, fn_die, where_offset, result);
14872
14873 return fn;
14874}
14875
14876/// Test if a given variable denoted by its DIE and its scope is
14877/// suppressed by any of the suppression specifications associated to
14878/// a given context of ELF/DWARF reading.
14879///
14880/// @param rdr the ELF/DWARF reading content of interest.
14881///
14882/// @param scope of the scope of the variable.
14883///
14884/// @param variable_die the DIE representing the variable.
14885///
14886/// @param is_required_decl_spec if true, means that the @p
14887/// variable_die being considered is for a variable decl that is a
14888/// specification for a concrete variable being built.
14889///
14890/// @return true iff @p variable_die is suppressed by at least one
14891/// suppression specification attached to the @p rdr.
14892static bool
14893variable_is_suppressed(const reader& rdr,
14894 const scope_decl* scope,
14895 Dwarf_Die *variable_die,
14896 bool is_required_decl_spec)
14897{
14898 if (variable_die == 0
14899 || (dwarf_tag(variable_die) != DW_TAG_variable
14900 && dwarf_tag(variable_die) != DW_TAG_member))
14901 return false;
14902
14903 string name = die_string_attribute(variable_die, DW_AT_name);
14904 string linkage_name = die_linkage_name(variable_die);
14905 if (linkage_name.empty() && rdr.die_is_in_c(variable_die))
14906 linkage_name = name;
14907 string qualified_name = build_qualified_name(scope, name);
14908
14909 // If a non member variable that is a declaration (has no defined
14910 // and exported symbol) and is not the specification of another
14911 // concrete variable, then it's suppressed. This is a size
14912 // optimization; it removes useless declaration-only variables from
14913 // the IR.
14914 if (!is_class_type(scope) && !is_required_decl_spec)
14915 {
14916 Dwarf_Addr var_addr = 0;
14917 if (!rdr.get_variable_address(variable_die, var_addr))
14918 return true;
14919
14920 elf_symbol_sptr symbol =
14921 rdr.variable_symbol_is_exported(var_addr);
14922 if (!symbol)
14923 return true;
14924 if (!symbol->is_suppressed())
14925 return false;
14926
14927 // Since there is only one symbol in DWARF associated with an elf_symbol,
14928 // we can assume this is the main symbol then. Otherwise the main hinting
14929 // did not work as expected.
14930 ABG_ASSERT(symbol->is_main_symbol());
14931 if (symbol->has_aliases())
14932 for (elf_symbol_sptr a = symbol->get_next_alias();
14933 !a->is_main_symbol(); a = a->get_next_alias())
14934 if (!a->is_suppressed())
14935 return false;
14936 }
14937
14939 qualified_name,
14940 linkage_name,
14941 /*require_drop_property=*/true);
14942}
14943
14944/// Test if a type (designated by a given DIE) in a given scope is
14945/// suppressed by the suppression specifications that are associated
14946/// to a given DWARF reader.
14947///
14948/// @param rdr the DWARF reader to consider.
14949///
14950/// @param scope of the scope of the type DIE to consider.
14951///
14952/// @param type_die the DIE that designates the type to consider.
14953///
14954/// @param type_is_private out parameter. If this function returns
14955/// true (the type @p type_die is suppressed) and if the type was
14956/// suppressed because it's private then this parameter is set to
14957/// true.
14958///
14959/// @return true iff the type designated by the DIE @p type_die, in
14960/// the scope @p scope is suppressed by at the suppression
14961/// specifications associated to the current DWARF reader.
14962static bool
14963type_is_suppressed(const reader& rdr,
14964 const scope_decl* scope,
14965 Dwarf_Die *type_die,
14966 bool &type_is_private)
14967{
14968 if (type_die == 0
14969 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
14970 && dwarf_tag(type_die) != DW_TAG_class_type
14971 && dwarf_tag(type_die) != DW_TAG_structure_type
14972 && dwarf_tag(type_die) != DW_TAG_union_type))
14973 return false;
14974
14975 string type_name, linkage_name;
14976 location type_location;
14977 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
14978 string qualified_name = build_qualified_name(scope, type_name);
14979
14980 return suppr::is_type_suppressed(rdr,
14981 qualified_name,
14982 type_location,
14983 type_is_private,
14984 /*require_drop_property=*/true);
14985}
14986
14987/// Test if a type (designated by a given DIE) in a given scope is
14988/// suppressed by the suppression specifications that are associated
14989/// to a given DWARF reader.
14990///
14991/// @param rdr the DWARF reader to consider.
14992///
14993/// @param scope of the scope of the type DIE to consider.
14994///
14995/// @param type_die the DIE that designates the type to consider.
14996///
14997/// @return true iff the type designated by the DIE @p type_die, in
14998/// the scope @p scope is suppressed by at the suppression
14999/// specifications associated to the current DWARF reader.
15000static bool
15001type_is_suppressed(const reader& rdr,
15002 const scope_decl* scope,
15003 Dwarf_Die *type_die)
15004{
15005 bool type_is_private = false;
15006 return type_is_suppressed(rdr, scope, type_die, type_is_private);
15007}
15008
15009/// Get the opaque version of a type that was suppressed because it's
15010/// a private type.
15011///
15012/// The opaque version version of the type is just a declared-only
15013/// version of the type (class, union or enum type) denoted by @p
15014/// type_die.
15015///
15016/// @param rdr the DWARF reader in use.
15017///
15018/// @param scope the scope of the type die we are looking at.
15019///
15020/// @param type_die the type DIE we are looking at.
15021///
15022/// @param where_offset the offset of the DIE where we are "logically"
15023/// positionned at, in the DIE tree. This is useful when @p die is
15024/// e.g, DW_TAG_partial_unit that can be included in several places in
15025/// the DIE tree.
15026///
15027/// @return the opaque version of the type denoted by @p type_die or
15028/// nil if no opaque version was found.
15030get_opaque_version_of_type(reader &rdr,
15031 scope_decl *scope,
15032 Dwarf_Die *type_die,
15033 size_t where_offset)
15034{
15036
15037 if (type_die == 0)
15038 return result;
15039
15040 unsigned tag = dwarf_tag(type_die);
15041 if (tag != DW_TAG_class_type
15042 && tag != DW_TAG_structure_type
15043 && tag != DW_TAG_union_type
15044 && tag != DW_TAG_enumeration_type)
15045 return result;
15046
15047 string type_name, linkage_name;
15048 location type_location;
15049 die_loc_and_name(rdr, type_die, type_location, type_name, linkage_name);
15050 if (!type_location)
15051 return result;
15052
15053 string qualified_name = build_qualified_name(scope, type_name);
15054
15055 //
15056 // TODO: also handle declaration-only unions. To do that, we mostly
15057 // need to adapt add_or_update_union_type to make it schedule
15058 // declaration-only unions for resolution too.
15059 //
15060 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
15061 {
15062 string_classes_or_unions_map::const_iterator i =
15063 rdr.declaration_only_classes().find(qualified_name);
15064 if (i != rdr.declaration_only_classes().end())
15065 result = i->second.back();
15066
15067 if (!result)
15068 {
15069 // So we didn't find any pre-existing forward-declared-only
15070 // class for the class definition that we could return as an
15071 // opaque type. So let's build one.
15072 //
15073 // TODO: we need to be able to do this for unions too!
15074 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
15075 /*alignment=*/0, /*size=*/0,
15076 tag == DW_TAG_structure_type,
15077 type_location,
15078 decl_base::VISIBILITY_DEFAULT));
15079 klass->set_is_declaration_only(true);
15080 klass->set_is_artificial(die_is_artificial(type_die));
15081 add_decl_to_scope(klass, scope);
15082 rdr.associate_die_to_type(type_die, klass, where_offset);
15083 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
15084 result = klass;
15085 }
15086 }
15087
15088 if (tag == DW_TAG_enumeration_type)
15089 {
15090 string_enums_map::const_iterator i =
15091 rdr.declaration_only_enums().find(qualified_name);
15092 if (i != rdr.declaration_only_enums().end())
15093 result = i->second.back();
15094
15095 if (!result)
15096 {
15097 uint64_t size = 0;
15098 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
15099 size *= 8;
15100 type_decl_sptr underlying_type =
15101 build_enum_underlying_type(rdr, type_name, size,
15102 /*anonymous=*/true);
15103 enum_type_decl::enumerators enumeratorz;
15104 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
15105 type_location,
15106 underlying_type,
15107 enumeratorz,
15108 linkage_name));
15109 enum_type->set_is_artificial(die_is_artificial(type_die));
15110 add_decl_to_scope(enum_type, scope);
15111 result = enum_type;
15112 }
15113 }
15114
15115 return result;
15116}
15117
15118/// Create a function symbol with a given name.
15119///
15120/// @param sym_name the name of the symbol to create.
15121///
15122/// @param env the environment to create the symbol in.
15123///
15124/// @return the newly created symbol.
15126create_default_fn_sym(const string& sym_name, const environment& env)
15127{
15129 elf_symbol_sptr result =
15131 /*symbol index=*/ 0,
15132 /*symbol size=*/ 0,
15133 sym_name,
15134 /*symbol type=*/ elf_symbol::FUNC_TYPE,
15135 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
15136 /*symbol is defined=*/ true,
15137 /*symbol is common=*/ false,
15138 /*symbol version=*/ ver,
15139 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
15140 return result;
15141}
15142
15143/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
15144///
15145/// @param rdr the DWARF reader to use
15146///
15147/// @param die the DW_TAG_subprogram DIE to read from.
15148///
15149/// @param where_offset the offset of the DIE where we are "logically"
15150/// positionned at, in the DIE tree. This is useful when @p die is
15151/// e.g, DW_TAG_partial_unit that can be included in several places in
15152/// the DIE tree.
15153///
15154/// @param called_for_public_decl this is set to true if the function
15155/// was called for a public (function) decl.
15156static function_decl_sptr
15157build_function_decl(reader& rdr,
15158 Dwarf_Die* die,
15159 size_t where_offset,
15161{
15162 function_decl_sptr result = fn;
15163 if (!die)
15164 return result;
15165 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subprogram);
15166
15167 if (!die_is_public_decl(die))
15168 return result;
15169
15170 translation_unit_sptr tu = rdr.cur_transl_unit();
15171 ABG_ASSERT(tu);
15172
15173 string fname, flinkage_name;
15174 location floc;
15175 die_loc_and_name(rdr, die, floc, fname, flinkage_name);
15176
15177 size_t is_inline = die_is_declared_inline(die);
15178 class_or_union_sptr is_method =
15179 is_class_or_union_type(get_scope_for_die(rdr, die, true, where_offset));
15180
15181 if (result)
15182 {
15183 // Add the properties that might have been missing from the
15184 // first declaration of the function. For now, it usually is
15185 // the mangled name that goes missing in the first declarations.
15186 //
15187 // Also note that if 'fn' has just been cloned, the current
15188 // linkage name (of the current DIE) might be different from the
15189 // linkage name of 'fn'. In that case, update the linkage name
15190 // of 'fn' too.
15191 if (!flinkage_name.empty()
15192 && result->get_linkage_name() != flinkage_name)
15193 result->set_linkage_name(flinkage_name);
15194 if (floc)
15195 if (!result->get_location())
15196 result->set_location(floc);
15197 }
15198 else
15199 {
15200 function_type_sptr fn_type(build_function_type(rdr, die, is_method,
15201 where_offset));
15202 if (!fn_type)
15203 return result;
15204
15205 maybe_canonicalize_type(fn_type, rdr);
15206
15207 result.reset(is_method
15208 ? new method_decl(fname, fn_type,
15209 is_inline, floc,
15210 flinkage_name)
15211 : new function_decl(fname, fn_type,
15212 is_inline, floc,
15213 flinkage_name));
15214 }
15215
15216 // Set the symbol of the function. If the linkage name is not set
15217 // or is wrong, set it to the name of the underlying symbol.
15218 if (!result->get_symbol())
15219 {
15220 elf_symbol_sptr fn_sym;
15221 Dwarf_Addr fn_addr;
15222 if (rdr.get_function_address(die, fn_addr))
15223 {
15224 rdr.symtab()->
15225 update_main_symbol(fn_addr,
15226 result->get_linkage_name().empty()
15227 ? result->get_name()
15228 : result->get_linkage_name());
15229 fn_sym = rdr.function_symbol_is_exported(fn_addr);
15230 }
15231
15232 if (fn_sym && !rdr.symbol_already_belongs_to_a_function(fn_sym))
15233 {
15234 result->set_symbol(fn_sym);
15235 string linkage_name = result->get_linkage_name();
15236 if (linkage_name.empty())
15237 result->set_linkage_name(fn_sym->get_name());
15238 result->set_is_in_public_symbol_table(true);
15239 }
15240 }
15241
15242 rdr.associate_die_to_type(die, result->get_type(), where_offset);
15243
15244 size_t die_offset = dwarf_dieoffset(die);
15245
15246 if (fn
15247 && is_member_function(fn)
15249 && !result->get_linkage_name().empty())
15250 // This function is a virtual member function which has its
15251 // linkage name *and* and has its underlying symbol correctly set.
15252 // It thus doesn't need any fixup related to elf symbol. So
15253 // remove it from the set of virtual member functions with linkage
15254 // names and no elf symbol that need to be fixed up.
15255 rdr.die_function_decl_with_no_symbol_map().erase(die_offset);
15256 return result;
15257}
15258
15259/// Canonicalize a type if it's suitable for early canonicalizing, or,
15260/// if it's not, schedule it for late canonicalization, after the
15261/// debug info of the current translation unit has been fully read.
15262///
15263/// A (composite) type is deemed suitable for early canonicalizing iff
15264/// all of its sub-types are canonicalized themselve. Non composite
15265/// types are always deemed suitable for early canonicalization.
15266///
15267/// Note that this function knows how to deal with anonymous classes,
15268/// structs and enums, unlike the overload below:
15269///
15270/// @param t the type DIE to consider for canonicalization.
15271///
15272/// @param rdr the @ref reader to use.
15273static void
15274maybe_canonicalize_type(const type_base_sptr& t,
15275 reader& rdr)
15276{
15277 if (!t)
15278 return;
15279
15280 type_base_sptr peeled_type = peel_typedef_pointer_or_reference_type(t);
15281 if (is_class_type(peeled_type)
15282 || is_union_type(peeled_type)
15283 || is_function_type(peeled_type)
15284 || is_array_type(peeled_type)
15285 || is_qualified_type(peeled_type)
15286 || is_enum_type(peeled_type)
15287 ||(is_decl(peeled_type) && is_decl(peeled_type)->get_is_anonymous()))
15288 // We delay canonicalization of classes/unions or typedef,
15289 // pointers, references and array to classes/unions. This is
15290 // because the (underlying) class might not be finished yet and we
15291 // might not be able to able detect it here (thinking about
15292 // classes that are work-in-progress, or classes that might be
15293 // later amended by some DWARF construct). So we err on the safe
15294 // side. We also delay canonicalization for array and qualified
15295 // types because they can be edited (in particular by
15296 // maybe_strip_qualification) after they are initially built.
15297 rdr.schedule_type_for_late_canonicalization(t);
15299 rdr.schedule_type_for_late_canonicalization(t);
15300 else
15301 canonicalize(t);
15302}
15303
15304/// If a given decl is a member type declaration, set its access
15305/// specifier from the DIE that represents it.
15306///
15307/// @param member_type_declaration the member type declaration to
15308/// consider.
15309static void
15310maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
15311 Dwarf_Die* die)
15312{
15313 if (is_type(member_type_declaration)
15314 && is_member_decl(member_type_declaration))
15315 {
15316 class_or_union* scope =
15317 is_class_or_union_type(member_type_declaration->get_scope());
15318 ABG_ASSERT(scope);
15319
15320 access_specifier access = public_access;
15321 if (class_decl* cl = is_class_type(scope))
15322 if (!cl->is_struct())
15323 access = private_access;
15324
15325 die_access_specifier(die, access);
15326 set_member_access_specifier(member_type_declaration, access);
15327 }
15328}
15329
15330/// This function tests if a given function which might be intented to
15331/// be added to a class scope (to become a member function) should be
15332/// dropped on the floor instead and not be added to the class.
15333///
15334/// This is a subroutine of build_ir_node_from_die.
15335///
15336/// @param fn the function to consider.
15337///
15338/// @param scope the scope the function is intended to be added
15339/// to. This might be of class type or not.
15340///
15341/// @param fn_die the DWARF die of @p fn.
15342///
15343/// @return true iff @p fn should be dropped on the floor.
15344static bool
15345potential_member_fn_should_be_dropped(const function_decl_sptr& fn,
15346 Dwarf_Die *fn_die)
15347{
15348 if (!fn || fn->get_scope())
15349 return false;
15350
15351 if (// A function that is not virtual ...
15352 !die_is_virtual(fn_die)
15353 // ... has a linkage name ...
15354 && !fn->get_linkage_name().empty()
15355 // .. and yet has no ELF symbol associated ...
15356 && !fn->get_symbol())
15357 // Should not be added to its class scope.
15358 //
15359 // Why would it? It's not part of the ABI anyway, as it doesn't
15360 // have any ELF symbol associated and is not a virtual member
15361 // function. It just constitutes bloat in the IR and might even
15362 // induce spurious change reports down the road.
15363 return true;
15364
15365 return false;
15366}
15367
15368/// Build an IR node from a given DIE and add the node to the current
15369/// IR being build and held in the DWARF reader. Doing that is called
15370/// "emitting an IR node for the DIE".
15371///
15372/// @param rdr the DWARF reader.
15373///
15374/// @param die the DIE to consider.
15375///
15376/// @param scope the scope under which the resulting IR node has to be
15377/// added.
15378///
15379/// @param called_from_public_decl set to yes if this function is
15380/// called from the functions used to build a public decl (functions
15381/// and variables). In that case, this function accepts building IR
15382/// nodes representing types. Otherwise, this function only creates
15383/// IR nodes representing public decls (functions and variables).
15384/// This is done to avoid emitting IR nodes for types that are not
15385/// referenced by public functions or variables.
15386///
15387/// @param where_offset the offset of the DIE where we are "logically"
15388/// positionned at, in the DIE tree. This is useful when @p die is
15389/// e.g, DW_TAG_partial_unit that can be included in several places in
15390/// the DIE tree.
15391///
15392/// @param is_required_decl_spec if true, it means the ir node to
15393/// build is for a decl that is a specification for another decl that
15394/// is concrete. If you don't know what this is, set it to false.
15395///
15396/// @param is_declaration_only is true if the DIE denoted by @p die is
15397/// a declaration-only DIE.
15398///
15399/// @return the resulting IR node.
15401build_ir_node_from_die(reader& rdr,
15402 Dwarf_Die* die,
15403 scope_decl* scope,
15404 bool called_from_public_decl,
15405 size_t where_offset,
15406 bool is_declaration_only,
15407 bool is_required_decl_spec)
15408{
15410
15411 if (!die || !scope)
15412 return result;
15413
15414 int tag = dwarf_tag(die);
15415
15416 if (!called_from_public_decl)
15417 {
15418 if (rdr.load_all_types() && die_is_type(die))
15419 /* We were instructed to load debug info for all types,
15420 included those that are not reachable from a public
15421 declaration. So load the debug info for this type. */;
15422 else if (tag != DW_TAG_subprogram
15423 && tag != DW_TAG_variable
15424 && tag != DW_TAG_member
15425 && tag != DW_TAG_namespace)
15426 return result;
15427 }
15428
15429 const die_source source_of_die = rdr.get_die_source(die);
15430
15431 if ((result = rdr.lookup_decl_from_die_offset(dwarf_dieoffset(die),
15432 source_of_die)))
15433 {
15434 if (rdr.load_all_types())
15435 if (called_from_public_decl)
15436 if (type_base_sptr t = is_type(result))
15437 if (corpus *abi_corpus = scope->get_corpus())
15438 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15439
15440 return result;
15441 }
15442
15443 // This is *the* bit of code that ensures we have the right notion
15444 // of "declared" at any point in a DIE chain formed from
15445 // DW_AT_abstract_origin and DW_AT_specification links. There should
15446 // be no other callers of die_is_declaration_only.
15447 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
15448
15449 switch (tag)
15450 {
15451 // Type DIEs we support.
15452 case DW_TAG_base_type:
15453 if (type_decl_sptr t = build_type_decl(rdr, die, where_offset))
15454 {
15455 result =
15456 add_decl_to_scope(t, rdr.cur_transl_unit()->get_global_scope());
15457 canonicalize(t);
15458 }
15459 break;
15460
15461 case DW_TAG_typedef:
15462 {
15464 t = is_typedef(scope->find_member_type(die_name(die)));
15465
15466 if (!t)
15467 t = build_typedef_type(rdr, die,
15468 called_from_public_decl,
15469 where_offset);
15470
15471 result = add_decl_to_scope(t, scope);
15472 if (result)
15473 {
15474 maybe_set_member_type_access_specifier(is_decl(result), die);
15475 maybe_canonicalize_type(t, rdr);
15476 }
15477 }
15478 break;
15479
15480 case DW_TAG_pointer_type:
15481 {
15483 build_pointer_type_def(rdr, die,
15484 called_from_public_decl,
15485 where_offset);
15486 if (p)
15487 {
15488 result =
15489 add_decl_to_scope(p, rdr.cur_transl_unit()->get_global_scope());
15490 ABG_ASSERT(result->get_translation_unit());
15491 maybe_canonicalize_type(p, rdr);
15492 }
15493 }
15494 break;
15495
15496 case DW_TAG_reference_type:
15497 case DW_TAG_rvalue_reference_type:
15498 {
15500 build_reference_type(rdr, die,
15501 called_from_public_decl,
15502 where_offset);
15503 if (r)
15504 {
15505 result =
15506 add_decl_to_scope(r, rdr.cur_transl_unit()->get_global_scope());
15507
15508 rdr.associate_die_to_type(die, r, where_offset);
15509 maybe_canonicalize_type(r, rdr);
15510 }
15511 }
15512 break;
15513
15514 case DW_TAG_const_type:
15515 case DW_TAG_volatile_type:
15516 case DW_TAG_restrict_type:
15517 {
15518 type_base_sptr q =
15519 build_qualified_type(rdr, die,
15520 called_from_public_decl,
15521 where_offset);
15522 if (q)
15523 {
15524 // Strip some potentially redundant type qualifiers from
15525 // the qualified type we just built.
15526 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
15527 rdr);
15528 if (!d)
15529 d = get_type_declaration(q);
15530 ABG_ASSERT(d);
15531 type_base_sptr ty = is_type(d);
15532 // Associate the die to type ty again because 'ty'might be
15533 // different from 'q', because 'ty' is 'q' possibly
15534 // stripped from some redundant type qualifier.
15535 rdr.associate_die_to_type(die, ty, where_offset);
15536 result =
15537 add_decl_to_scope(d, rdr.cur_transl_unit()->get_global_scope());
15538 maybe_canonicalize_type(is_type(result), rdr);
15539 }
15540 }
15541 break;
15542
15543 case DW_TAG_enumeration_type:
15544 {
15545 bool type_is_private = false;
15546 bool type_suppressed =
15547 type_is_suppressed(rdr, scope, die, type_is_private);
15548 if (type_suppressed && type_is_private)
15549 {
15550 // The type is suppressed because it's private. If other
15551 // non-suppressed and declaration-only instances of this
15552 // type exist in the current corpus, then it means those
15553 // non-suppressed instances are opaque versions of the
15554 // suppressed private type. Lets return one of these opaque
15555 // types then.
15556 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15557 maybe_canonicalize_type(is_type(result), rdr);
15558 }
15559 else if (!type_suppressed)
15560 {
15561 enum_type_decl_sptr e = build_enum_type(rdr, die, scope,
15562 where_offset,
15563 is_declaration_only);
15564 result = add_decl_to_scope(e, scope);
15565 if (result)
15566 {
15567 maybe_set_member_type_access_specifier(is_decl(result), die);
15568 maybe_canonicalize_type(is_type(result), rdr);
15569 }
15570 }
15571 }
15572 break;
15573
15574 case DW_TAG_class_type:
15575 case DW_TAG_structure_type:
15576 {
15577 bool type_is_private = false;
15578 bool type_suppressed=
15579 type_is_suppressed(rdr, scope, die, type_is_private);
15580
15581 if (type_suppressed && type_is_private)
15582 {
15583 // The type is suppressed because it's private. If other
15584 // non-suppressed and declaration-only instances of this
15585 // type exist in the current corpus, then it means those
15586 // non-suppressed instances are opaque versions of the
15587 // suppressed private type. Lets return one of these opaque
15588 // types then.
15589 result = get_opaque_version_of_type(rdr, scope, die, where_offset);
15590 maybe_canonicalize_type(is_type(result), rdr);
15591 }
15592 else if (!type_suppressed)
15593 {
15594 Dwarf_Die spec_die;
15595 scope_decl_sptr scop;
15596 class_decl_sptr klass;
15597 if (die_die_attribute(die, DW_AT_specification, spec_die))
15598 {
15599 scope_decl_sptr skope =
15600 get_scope_for_die(rdr, &spec_die,
15601 called_from_public_decl,
15602 where_offset);
15603 ABG_ASSERT(skope);
15604 decl_base_sptr cl =
15605 is_decl(build_ir_node_from_die(rdr, &spec_die,
15606 skope.get(),
15607 called_from_public_decl,
15608 where_offset,
15609 is_declaration_only,
15610 /*is_required_decl_spec=*/false));
15611 ABG_ASSERT(cl);
15612 klass = dynamic_pointer_cast<class_decl>(cl);
15613 ABG_ASSERT(klass);
15614
15615 klass =
15616 add_or_update_class_type(rdr, die,
15617 skope.get(),
15618 tag == DW_TAG_structure_type,
15619 klass,
15620 called_from_public_decl,
15621 where_offset,
15622 is_declaration_only);
15623 }
15624 else
15625 klass =
15626 add_or_update_class_type(rdr, die, scope,
15627 tag == DW_TAG_structure_type,
15629 called_from_public_decl,
15630 where_offset,
15631 is_declaration_only);
15632 result = klass;
15633 if (klass)
15634 {
15635 maybe_set_member_type_access_specifier(klass, die);
15636 maybe_canonicalize_type(klass, rdr);
15637 }
15638 }
15639 }
15640 break;
15641 case DW_TAG_union_type:
15642 if (!type_is_suppressed(rdr, scope, die))
15643 {
15644 union_decl_sptr union_type =
15645 add_or_update_union_type(rdr, die, scope,
15646 union_decl_sptr(),
15647 called_from_public_decl,
15648 where_offset,
15649 is_declaration_only);
15650 if (union_type)
15651 {
15652 maybe_set_member_type_access_specifier(union_type, die);
15653 maybe_canonicalize_type(union_type, rdr);
15654 }
15655 result = union_type;
15656 }
15657 break;
15658 case DW_TAG_string_type:
15659 break;
15660 case DW_TAG_subroutine_type:
15661 {
15662 function_type_sptr f = build_function_type(rdr, die,
15664 where_offset);
15665 if (f)
15666 {
15667 result = f;
15668 result->set_is_artificial(false);
15669 maybe_canonicalize_type(f, rdr);
15670 }
15671 }
15672 break;
15673 case DW_TAG_array_type:
15674 {
15675 array_type_def_sptr a = build_array_type(rdr,
15676 die,
15677 called_from_public_decl,
15678 where_offset);
15679 if (a)
15680 {
15681 result =
15682 add_decl_to_scope(a, rdr.cur_transl_unit()->get_global_scope());
15683 rdr.associate_die_to_type(die, a, where_offset);
15684 maybe_canonicalize_type(a, rdr);
15685 }
15686 break;
15687 }
15688 case DW_TAG_subrange_type:
15689 {
15690 // If we got here, this means the subrange type is a "free
15691 // form" defined in the global namespace of the current
15692 // translation unit, like what is found in Ada.
15694 build_subrange_type(rdr, die, where_offset);
15695 if (s)
15696 {
15697 result =
15698 add_decl_to_scope(s, rdr.cur_transl_unit()->get_global_scope());
15699 rdr.associate_die_to_type(die, s, where_offset);
15700 maybe_canonicalize_type(s, rdr);
15701 }
15702 }
15703 break;
15704 case DW_TAG_packed_type:
15705 break;
15706 case DW_TAG_set_type:
15707 break;
15708 case DW_TAG_file_type:
15709 break;
15710 case DW_TAG_ptr_to_member_type:
15711 break;
15712 case DW_TAG_thrown_type:
15713 break;
15714 case DW_TAG_interface_type:
15715 break;
15716 case DW_TAG_unspecified_type:
15717 break;
15718 case DW_TAG_shared_type:
15719 break;
15720
15721 case DW_TAG_compile_unit:
15722 // We shouldn't reach this point b/c this should be handled by
15723 // build_translation_unit.
15725
15726 case DW_TAG_namespace:
15727 case DW_TAG_module:
15728 result = build_namespace_decl_and_add_to_ir(rdr, die, where_offset);
15729 break;
15730
15731 case DW_TAG_variable:
15732 case DW_TAG_member:
15733 {
15734 Dwarf_Die spec_die;
15735 bool var_is_cloned = false;
15736
15737 if (tag == DW_TAG_member)
15738 ABG_ASSERT(!is_c_language(rdr.cur_transl_unit()->get_language()));
15739
15740 if (die_die_attribute(die, DW_AT_specification, spec_die, false)
15741 || (var_is_cloned = die_die_attribute(die, DW_AT_abstract_origin,
15742 spec_die, false)))
15743 {
15744 scope_decl_sptr spec_scope =
15745 get_scope_for_die(rdr, &spec_die,
15746 /*called_from_public_decl=*/
15747 die_is_effectively_public_decl(rdr, die),
15748 where_offset);
15749 if (spec_scope)
15750 {
15751 decl_base_sptr d =
15752 is_decl(build_ir_node_from_die(rdr, &spec_die,
15753 spec_scope.get(),
15754 called_from_public_decl,
15755 where_offset,
15756 is_declaration_only,
15757 /*is_required_decl_spec=*/true));
15758 if (d)
15759 {
15760 var_decl_sptr m =
15761 dynamic_pointer_cast<var_decl>(d);
15762 if (var_is_cloned)
15763 m = m->clone();
15764 m = build_var_decl(rdr, die, where_offset, m);
15765 if (is_data_member(m))
15766 {
15767 set_member_is_static(m, true);
15768 rdr.associate_die_to_decl(die, m, where_offset,
15769 /*associate_by_repr=*/false);
15770 }
15771 else
15772 {
15774 rdr.var_decls_to_re_add_to_tree().push_back(m);
15775 }
15776 ABG_ASSERT(m->get_scope());
15777 rdr.maybe_add_var_to_exported_decls(m.get());
15778 result = m;
15779 }
15780 }
15781 }
15782 else if (var_decl_sptr v =
15783 build_or_get_var_decl_if_not_suppressed(rdr, scope, die,
15784 where_offset,
15785 /*result=*/var_decl_sptr(),
15786 is_required_decl_spec))
15787 {
15788 result = add_decl_to_scope(v, scope);
15789 ABG_ASSERT(is_decl(result)->get_scope());
15790 v = dynamic_pointer_cast<var_decl>(result);
15791 ABG_ASSERT(v);
15792 ABG_ASSERT(v->get_scope());
15793 rdr.var_decls_to_re_add_to_tree().push_back(v);
15794 rdr.maybe_add_var_to_exported_decls(v.get());
15795 }
15796 }
15797 break;
15798
15799 case DW_TAG_subprogram:
15800 {
15801 Dwarf_Die spec_die;
15802 Dwarf_Die abstract_origin_die;
15803 Dwarf_Die *interface_die = 0, *origin_die = 0;
15804 scope_decl_sptr interface_scope;
15805 if (die_is_artificial(die))
15806 break;
15807
15809 bool has_spec = die_die_attribute(die, DW_AT_specification,
15810 spec_die, true);
15811 bool has_abstract_origin =
15812 die_die_attribute(die, DW_AT_abstract_origin,
15813 abstract_origin_die, true);
15814 if (has_spec || has_abstract_origin)
15815 {
15816 interface_die =
15817 has_spec
15818 ? &spec_die
15819 : &abstract_origin_die;
15820 origin_die =
15821 has_abstract_origin
15822 ? &abstract_origin_die
15823 : &spec_die;
15824
15825 string linkage_name = die_linkage_name(die);
15826 string spec_linkage_name = die_linkage_name(interface_die);
15827
15828 interface_scope = get_scope_for_die(rdr, interface_die,
15829 called_from_public_decl,
15830 where_offset);
15831 if (interface_scope)
15832 {
15833 decl_base_sptr d;
15834 class_decl_sptr c = is_class_type(interface_scope);
15835 if (c && !linkage_name.empty())
15836 d = c->find_member_function_sptr(linkage_name);
15837
15838 if (!d)
15839 d = is_decl(build_ir_node_from_die(rdr,
15840 origin_die,
15841 interface_scope.get(),
15842 called_from_public_decl,
15843 where_offset,
15844 is_declaration_only,
15845 /*is_required_decl_spec=*/true));
15846 if (d)
15847 {
15848 fn = dynamic_pointer_cast<function_decl>(d);
15849 if (has_abstract_origin
15850 && (linkage_name != spec_linkage_name)
15851 && !c->find_member_function_sptr(linkage_name))
15852 // The current DIE has 'd' as abstract orign,
15853 // and has a linkage name that is different
15854 // from from the linkage name of 'd'. That
15855 // means, the current DIE represents a clone
15856 // of 'd'.
15857 fn = fn->clone();
15858 }
15859 }
15860 }
15861 rdr.scope_stack().push(scope);
15862
15863 scope_decl* logical_scope =
15864 interface_scope
15865 ? interface_scope.get()
15866 : scope;
15867
15868 result = build_or_get_fn_decl_if_not_suppressed(rdr, logical_scope,
15869 die, where_offset,
15870 is_declaration_only,
15871 fn);
15872
15873 if (result && !fn)
15874 {
15875 if (potential_member_fn_should_be_dropped(is_function_decl(result),
15876 die)
15877 && !is_required_decl_spec)
15878 {
15879 result.reset();
15880 break;
15881 }
15882 result = add_decl_to_scope(is_decl(result), logical_scope);
15883 }
15884
15885 fn = is_function_decl(result);
15886 if (fn && is_member_function(fn))
15887 {
15888 class_decl_sptr klass(static_cast<class_decl*>(logical_scope),
15889 sptr_utils::noop_deleter());
15890 ABG_ASSERT(klass);
15891 finish_member_function_reading(die, fn, klass, rdr);
15892 }
15893
15894 if (fn)
15895 {
15896 rdr.maybe_add_fn_to_exported_decls(fn.get());
15897 rdr.associate_die_to_decl(die, fn, where_offset,
15898 /*associate_by_repr=*/false);
15899 maybe_canonicalize_type(fn->get_type(), rdr);
15900 }
15901
15902 rdr.scope_stack().pop();
15903 }
15904 break;
15905
15906 case DW_TAG_formal_parameter:
15907 // We should not read this case as it should have been dealt
15908 // with by build_function_decl above.
15910
15911 case DW_TAG_constant:
15912 break;
15913 case DW_TAG_enumerator:
15914 break;
15915
15916 case DW_TAG_partial_unit:
15917 case DW_TAG_imported_unit:
15918 // For now, the DIEs under these are read lazily when they are
15919 // referenced by a public decl DIE that is under a
15920 // DW_TAG_compile_unit, so we shouldn't get here.
15922
15923 // Other declaration we don't really intend to support yet.
15924 case DW_TAG_dwarf_procedure:
15925 case DW_TAG_imported_declaration:
15926 case DW_TAG_entry_point:
15927 case DW_TAG_label:
15928 case DW_TAG_lexical_block:
15929 case DW_TAG_unspecified_parameters:
15930 case DW_TAG_variant:
15931 case DW_TAG_common_block:
15932 case DW_TAG_common_inclusion:
15933 case DW_TAG_inheritance:
15934 case DW_TAG_inlined_subroutine:
15935 case DW_TAG_with_stmt:
15936 case DW_TAG_access_declaration:
15937 case DW_TAG_catch_block:
15938 case DW_TAG_friend:
15939 case DW_TAG_namelist:
15940 case DW_TAG_namelist_item:
15941 case DW_TAG_template_type_parameter:
15942 case DW_TAG_template_value_parameter:
15943 case DW_TAG_try_block:
15944 case DW_TAG_variant_part:
15945 case DW_TAG_imported_module:
15946 case DW_TAG_condition:
15947 case DW_TAG_type_unit:
15948 case DW_TAG_template_alias:
15949 case DW_TAG_lo_user:
15950 case DW_TAG_MIPS_loop:
15951 case DW_TAG_format_label:
15952 case DW_TAG_function_template:
15953 case DW_TAG_class_template:
15954 case DW_TAG_GNU_BINCL:
15955 case DW_TAG_GNU_EINCL:
15956 case DW_TAG_GNU_template_template_param:
15957 case DW_TAG_GNU_template_parameter_pack:
15958 case DW_TAG_GNU_formal_parameter_pack:
15959 case DW_TAG_GNU_call_site:
15960 case DW_TAG_GNU_call_site_parameter:
15961 case DW_TAG_hi_user:
15962 default:
15963 break;
15964 }
15965
15966 if (result && tag != DW_TAG_subroutine_type)
15967 rdr.associate_die_to_decl(die, is_decl(result), where_offset,
15968 /*associate_by_repr=*/false);
15969
15970 if (result)
15971 if (rdr.load_all_types())
15972 if (called_from_public_decl)
15973 if (type_base_sptr t = is_type(result))
15974 if (corpus *abi_corpus = scope->get_corpus())
15975 abi_corpus->record_type_as_reachable_from_public_interfaces(*t);
15976
15977 return result;
15978}
15979
15980/// Build the IR node for a void type.
15981///
15982/// @param rdr the DWARF reader to use.
15983///
15984/// @return the void type node.
15985static decl_base_sptr
15986build_ir_node_for_void_type(reader& rdr)
15987{
15988 const environment& env = rdr.env();
15989
15990 type_base_sptr t = env.get_void_type();
15991 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
15992 decl_base_sptr type_declaration = get_type_declaration(t);
15993 canonicalize(t);
15994 return type_declaration;
15995}
15996
15997/// Build the IR node for a "pointer to void type".
15998///
15999/// That IR node is shared across the ABI corpus.
16000///
16001/// Note that this function just gets that IR node from the
16002/// environment and, if it's not added to any scope yet, adds it to
16003/// the global scope associated to the current translation unit.
16004///
16005/// @param rdr the DWARF reader to consider.
16006///
16007/// @return the IR node.
16009build_ir_node_for_void_pointer_type(reader& rdr)
16010{
16011 const environment& env = rdr.env();
16012
16013 type_base_sptr t = env.get_void_pointer_type();
16014 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16015 decl_base_sptr type_declaration = get_type_declaration(t);
16016 canonicalize(t);
16017 return type_declaration;
16018}
16019
16020/// Build the IR node for a variadic parameter type.
16021///
16022/// @param rdr the DWARF reader to use.
16023///
16024/// @return the variadic parameter type.
16025static decl_base_sptr
16026build_ir_node_for_variadic_parameter_type(reader &rdr)
16027{
16028
16029 const environment& env = rdr.env();
16030
16031 type_base_sptr t = env.get_variadic_parameter_type();
16032 add_decl_to_scope(is_decl(t), rdr.cur_transl_unit()->get_global_scope());
16033 decl_base_sptr type_declaration = get_type_declaration(t);
16034 canonicalize(t);
16035 return type_declaration;
16036}
16037
16038/// Build an IR node from a given DIE and add the node to the current
16039/// IR being build and held in the DWARF reader. Doing that is called
16040/// "emitting an IR node for the DIE".
16041///
16042/// @param rdr the DWARF reader.
16043///
16044/// @param die the DIE to consider.
16045///
16046/// @param called_from_public_decl set to yes if this function is
16047/// called from the functions used to build a public decl (functions
16048/// and variables). In that case, this function accepts building IR
16049/// nodes representing types. Otherwise, this function only creates
16050/// IR nodes representing public decls (functions and variables).
16051/// This is done to avoid emitting IR nodes for types that are not
16052/// referenced by public functions or variables.
16053///
16054/// @param where_offset the offset of the DIE where we are "logically"
16055/// positionned at, in the DIE tree. This is useful when @p die is
16056/// e.g, DW_TAG_partial_unit that can be included in several places in
16057/// the DIE tree.
16058///
16059/// @return the resulting IR node.
16061build_ir_node_from_die(reader& rdr,
16062 Dwarf_Die* die,
16063 bool called_from_public_decl,
16064 size_t where_offset)
16065{
16066 if (!die)
16067 return decl_base_sptr();
16068
16069 if (is_c_language(rdr.cur_transl_unit()->get_language()))
16070 {
16071 const scope_decl_sptr& scop = rdr.global_scope();
16072 return build_ir_node_from_die(rdr, die, scop.get(),
16073 called_from_public_decl,
16074 where_offset,
16075 true);
16076 }
16077
16078 // Normaly, a decl that is meant to be external has a DW_AT_external
16079 // set. But then some compilers fail to always emit that flag. For
16080 // instance, for static data members, some compilers won't emit the
16081 // DW_AT_external. In that case, we assume that if the variable is
16082 // at global or named namespace scope, then we can assume it's
16083 // external. If the variable doesn't have any ELF symbol associated
16084 // to it, it'll be dropped on the floor anyway. Those variable
16085 // decls are considered as being "effectively public".
16086 bool consider_as_called_from_public_decl =
16087 called_from_public_decl || die_is_effectively_public_decl(rdr, die);
16088 scope_decl_sptr scope = get_scope_for_die(rdr, die,
16089 consider_as_called_from_public_decl,
16090 where_offset);
16091 return build_ir_node_from_die(rdr, die, scope.get(),
16092 called_from_public_decl,
16093 where_offset,
16094 true);
16095}
16096
16097/// Create a dwarf::reader.
16098///
16099/// @param elf_path the path to the elf file the reader is to be used
16100/// for.
16101///
16102/// @param debug_info_root_paths a vector to the paths to the
16103/// directories under which the debug info is to be found for @p
16104/// elf_path. Pass an empty vector if the debug info is not in a
16105/// split file.
16106///
16107/// @param environment the environment used by the current context.
16108/// This environment contains resources needed by the DWARF reader and by
16109/// the types and declarations that are to be created later. Note
16110/// that ABI artifacts that are to be compared all need to be created
16111/// within the same environment.
16112///
16113/// Please also note that the life time of this environment object
16114/// must be greater than the life time of the resulting @ref
16115/// reader the context uses resources that are allocated in the
16116/// environment.
16117///
16118/// @param load_all_types if set to false only the types that are
16119/// reachable from publicly exported declarations (of functions and
16120/// variables) are read. If set to true then all types found in the
16121/// debug information are loaded.
16122///
16123/// @param linux_kernel_mode if set to true, then consider the special
16124/// linux kernel symbol tables when determining if a symbol is
16125/// exported or not.
16126///
16127/// @return a smart pointer to the resulting dwarf::reader.
16128elf_based_reader_sptr
16129create_reader(const std::string& elf_path,
16130 const vector<char**>& debug_info_root_paths,
16132 bool load_all_types,
16133 bool linux_kernel_mode)
16134{
16135
16136 reader_sptr r = reader::create(elf_path,
16137 debug_info_root_paths,
16139 load_all_types,
16140 linux_kernel_mode);
16141 return static_pointer_cast<elf_based_reader>(r);
16142}
16143
16144/// Re-initialize a reader so that it can re-used to read
16145/// another binary.
16146///
16147/// @param rdr the context to re-initialize.
16148///
16149/// @param elf_path the path to the elf file the context is to be used
16150/// for.
16151///
16152/// @param debug_info_root_path a pointer to the path to the root
16153/// directory under which the debug info is to be found for @p
16154/// elf_path. Leave this to NULL if the debug info is not in a split
16155/// file.
16156///
16157/// @param environment the environment used by the current context.
16158/// This environment contains resources needed by the DWARF reader and by
16159/// the types and declarations that are to be created later. Note
16160/// that ABI artifacts that are to be compared all need to be created
16161/// within the same environment.
16162///
16163/// Please also note that the life time of this environment object
16164/// must be greater than the life time of the resulting @ref
16165/// reader the context uses resources that are allocated in the
16166/// environment.
16167///
16168/// @param load_all_types if set to false only the types that are
16169/// reachable from publicly exported declarations (of functions and
16170/// variables) are read. If set to true then all types found in the
16171/// debug information are loaded.
16172///
16173/// @param linux_kernel_mode if set to true, then consider the special
16174/// linux kernel symbol tables when determining if a symbol is
16175/// exported or not.
16176///
16177/// @return a smart pointer to the resulting dwarf::reader.
16178void
16180 const std::string& elf_path,
16181 const vector<char**>&debug_info_root_path,
16182 bool read_all_types,
16183 bool linux_kernel_mode)
16184{
16185 reader& r = dynamic_cast<reader&>(rdr);
16186 r.initialize(elf_path, debug_info_root_path,
16187 read_all_types, linux_kernel_mode);
16188}
16189
16190/// Read all @ref abigail::translation_unit possible from the debug info
16191/// accessible from an elf file, stuff them into a libabigail ABI
16192/// Corpus and return it.
16193///
16194/// @param elf_path the path to the elf file.
16195///
16196/// @param debug_info_root_paths a vector of pointers to root paths
16197/// under which to look for the debug info of the elf files that are
16198/// later handled by the Dwfl. This for cases where the debug info is
16199/// split into a different file from the binary we want to inspect.
16200/// On Red Hat compatible systems, this root path is usually
16201/// /usr/lib/debug by default. If this argument is set to NULL, then
16202/// "./debug" and /usr/lib/debug will be searched for sub-directories
16203/// containing the debug info file.
16204///
16205/// @param environment the environment used by the current context.
16206/// This environment contains resources needed by the DWARF reader and by
16207/// the types and declarations that are to be created later. Note
16208/// that ABI artifacts that are to be compared all need to be created
16209/// within the same environment. Also, the lifetime of the
16210/// environment must be greater than the lifetime of the resulting
16211/// corpus because the corpus uses resources that are allocated in the
16212/// environment.
16213///
16214/// @param load_all_types if set to false only the types that are
16215/// reachable from publicly exported declarations (of functions and
16216/// variables) are read. If set to true then all types found in the
16217/// debug information are loaded.
16218///
16219/// @param resulting_corp a pointer to the resulting abigail::corpus.
16220///
16221/// @return the resulting status.
16222corpus_sptr
16223read_corpus_from_elf(const std::string& elf_path,
16224 const vector<char**>& debug_info_root_paths,
16226 bool load_all_types,
16227 fe_iface::status& status)
16228{
16229 elf_based_reader_sptr rdr =
16230 dwarf::reader::create(elf_path, debug_info_root_paths,
16231 environment, load_all_types,
16232 /*linux_kernel_mode=*/false);
16233
16234 return rdr->read_corpus(status);
16235}
16236
16237/// Look into the symbol tables of a given elf file and see if we find
16238/// a given symbol.
16239///
16240/// @param env the environment we are operating from.
16241///
16242/// @param elf_path the path to the elf file to consider.
16243///
16244/// @param symbol_name the name of the symbol to look for.
16245///
16246/// @param demangle if true, try to demangle the symbol name found in
16247/// the symbol table.
16248///
16249/// @param syms the vector of symbols found with the name @p symbol_name.
16250///
16251/// @return true iff the symbol was found among the publicly exported
16252/// symbols of the ELF file.
16253bool
16254lookup_symbol_from_elf(const environment& env,
16255 const string& elf_path,
16256 const string& symbol_name,
16257 bool demangle,
16258 vector<elf_symbol_sptr>& syms)
16259
16260{
16261 if (elf_version(EV_CURRENT) == EV_NONE)
16262 return false;
16263
16264 int fd = open(elf_path.c_str(), O_RDONLY);
16265 if (fd < 0)
16266 return false;
16267
16268 struct stat s;
16269 if (fstat(fd, &s))
16270 return false;
16271
16272 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16273 if (elf == 0)
16274 return false;
16275
16276 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
16277 demangle, syms);
16278 elf_end(elf);
16279 close(fd);
16280
16281 return value;
16282}
16283
16284/// Look into the symbol tables of an elf file to see if a public
16285/// function of a given name is found.
16286///
16287/// @param env the environment we are operating from.
16288///
16289/// @param elf_path the path to the elf file to consider.
16290///
16291/// @param symbol_name the name of the function to look for.
16292///
16293/// @param syms the vector of public function symbols found with the
16294/// name @p symname.
16295///
16296/// @return true iff a function with symbol name @p symbol_name is
16297/// found.
16298bool
16300 const string& path,
16301 const string& symname,
16302 vector<elf_symbol_sptr>& syms)
16303{
16304 if (elf_version(EV_CURRENT) == EV_NONE)
16305 return false;
16306
16307 int fd = open(path.c_str(), O_RDONLY);
16308 if (fd < 0)
16309 return false;
16310
16311 struct stat s;
16312 if (fstat(fd, &s))
16313 return false;
16314
16315 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
16316 if (elf == 0)
16317 return false;
16318
16319 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
16320 elf_end(elf);
16321 close(fd);
16322
16323 return value;
16324}
16325
16326}// end namespace dwarf
16327
16328}// 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:1612
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
void maybe_add_fn_to_exported_decls(const function_decl *fn)
Try and add the representation of the ABI of a function to the set of exported declarations of the cu...
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:2473
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition: abg-ir.h:2476
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition: abg-ir.h:4168
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:4934
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1180
binding
The binding of a symbol.
Definition: abg-ir.h:926
type
The type of a symbol.
Definition: abg-ir.h:913
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:1902
visibility
The visibility of the symbol.
Definition: abg-ir.h:935
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition: abg-ir.h:2702
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:3057
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition: abg-ir.h:3060
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:15609
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:2232
language
The language of the translation unit.
Definition: abg-ir.h:685
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:11456
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:11320
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:865
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:13037
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:13199
void set_member_function_is_virtual(function_decl &f, bool is_virtual)
Set the virtual-ness of a member function.
Definition: abg-ir.cc:6712
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:10812
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition: abg-ir.cc:10248
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition: abg-ir.cc:5541
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:11226
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition: abg-ir.h:874
void set_member_function_vtable_offset(function_decl &f, ssize_t s)
Set the vtable offset of a member function.
Definition: abg-ir.cc:6644
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:10480
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition: abg-ir.cc:8371
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:9862
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:11346
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:10512
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition: abg-fwd.h:237
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition: abg-ir.cc:10299
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:13224
array_type_def * is_array_type(const type_or_decl_base *type)
Test if a type is an array_type_def.
Definition: abg-ir.cc:11148
reference_type_def * is_reference_type(type_or_decl_base *t)
Test whether a type is a reference_type_def.
Definition: abg-ir.cc:10742
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:6147
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:12900
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition: abg-ir.cc:6515
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:11787
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:10586
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:7344
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:6571
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:6899
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:1316
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:10332
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:10883
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition: abg-ir.cc:5686
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition: abg-ir.cc:10390
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:13067
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:11496
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition: abg-ir.cc:1651
bool parse_integral_type(const string &type_name, integral_type &type)
Parse an integral type from a string.
Definition: abg-ir.cc:15527
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:10462
const global_scope * get_global_scope(const decl_base &decl)
return the global scope as seen by a given declaration.
Definition: abg-ir.cc:8440
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition: abg-fwd.h:249
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:11986
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:26489
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition: abg-ir.cc:1665
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:11533
type_base_sptr canonicalize(type_base_sptr t)
Compute the canonical type of a given type.
Definition: abg-ir.cc:15034
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
pointer_type_def * is_pointer_type(type_or_decl_base *t)
Test whether a type is a pointer_type_def.
Definition: abg-ir.cc:10635
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition: abg-ir.cc:6401
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition: abg-ir.cc:1637
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition: abg-ir.cc:10188
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:23829
bool is_member_type(const type_base_sptr &t)
Tests if a type is a class member.
Definition: abg-ir.cc:5606
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:8347
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:27128
access_specifier get_member_access_specifier(const decl_base &d)
Gets the access specifier for a class member.
Definition: abg-ir.cc:5657
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:6674
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:10943
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition: abg-ir.cc:10605
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:12855
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:11205
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition: abg-ir.cc:5755
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:12804
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition: abg-ir.cc:9881
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition: abg-ir.cc:24952
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:7022
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition: abg-fwd.h:285
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition: abg-ir.cc:1674
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition: abg-ir.cc:14400
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:8732
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:7603
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:13162
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition: abg-ir.cc:10136
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:10913
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:10863
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:11566
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:8629
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:10973
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition: abg-ir.cc:5559
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:6458
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.
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.