libabigail
Loading...
Searching...
No Matches
abg-dwarf-reader.cc
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2// -*- Mode: C++ -*-
3//
4// Copyright (C) 2013-2026 Red Hat, Inc.
5//
6//
7// Author: Dodji Seketeli
8
9/// @file
10///
11/// This file contains the definitions of the entry points to
12/// de-serialize an instance of @ref abigail::corpus from a file in
13/// elf format, containing dwarf information.
14
15#include "abg-internal.h"
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <libgen.h>
21#include <assert.h>
22#include <limits.h>
23#include <elfutils/libdwfl.h>
24#include <dwarf.h>
25#include <algorithm>
26#include <cmath>
27#include <cstring>
28#include <deque>
29#include <list>
30#include <memory>
31#include <ostream>
32#include <sstream>
33#include <stack>
34#include <unordered_map>
35#include <unordered_set>
36#include <map>
37#include <mutex>
38
39#include "abg-ir-priv.h"
41#include "abg-corpus-priv.h"
42#include "abg-symtab-reader.h"
43
44// <headers defining libabigail's API go under here>
45ABG_BEGIN_EXPORT_DECLARATIONS
46
47#include "abg-dwarf-reader.h"
49#include "abg-sptr-utils.h"
50#include "abg-tools-utils.h"
51#include "abg-elf-helpers.h"
52#include "abg-workers.h"
53
54ABG_END_EXPORT_DECLARATIONS
55// </headers defining libabigail's API>
56
57#include "parallel_hashmap/phmap.h"
58
59#ifndef UINT64_MAX
60#define UINT64_MAX 0xffffffffffffffff
61#endif
62
63namespace abigail
64{
65
66/// The namespace for the DWARF reader.
67namespace dwarf
68{
69using std::string;
70using std::cerr;
71using std::dynamic_pointer_cast;
72using std::static_pointer_cast;
73using std::unordered_map;
74using std::unordered_set;
75using std::stack;
76using std::deque;
77using std::list;
78using std::map;
79using std::mutex;
81
82using namespace elf_helpers; // TODO: avoid using namespace
83
84/// Where a DIE comes from. For instance, a DIE can come from the main
85/// debug info section, the alternate debug info section or from the
86/// type unit section.
88{
89 NO_DEBUG_INFO_DIE_SOURCE,
90 PRIMARY_DEBUG_INFO_DIE_SOURCE,
91 ALT_DEBUG_INFO_DIE_SOURCE,
92 TYPE_UNIT_DIE_SOURCE,
93 NUMBER_OF_DIE_SOURCES, // This one must always be the latest
94 // enumerator
95};
96
97/// A convenience typedef for a vector of void*, representing the
98/// address of a DIE.
99typedef vector<void*> dwarf_addrs_type;
100
101/// A convenience typedef for a std::pair of void*, , representing the
102/// address of a DIE.
103typedef std::pair<void*, void*> dwarf_addr_pair_type;
104
105/// Convenience typedef for a map which key is the address of a dwarf
106/// die and which value is the corresponding artefact.
107typedef unordered_map<void*, type_or_decl_base_sptr> die_artefact_map_type;
108
109/// Convenience typedef for a map which key is the address of a dwarf
110/// die and which value is the corresponding class_decl.
111typedef unordered_map<void*, class_decl_sptr> die_class_map_type;
112
113/// Convenience typedef for a map which key is the address of a dwarf
114/// die and which value is the corresponding class_or_union_sptr.
115typedef unordered_map<void*,
116 class_or_union_sptr> die_class_or_union_map_type;
117
118/// Convenience typedef for a map which key is the address of a dwarf
119/// die and which value is the corresponding function_decl.
120typedef unordered_map<void*,
122
123/// Convenience typedef for a map which key is the address of a dwarf
124/// die and which value is the corresponding function_type.
125typedef unordered_map<void*,
127
128/// Convenience typedef for a map which key is the address of a
129/// DW_TAG_compile_unit and the value is the corresponding @ref
130/// translation_unit_sptr.
131typedef unordered_map<void*, translation_unit_sptr> die_tu_map_type;
132
133/// Convenience typedef for a map which key is the address of a DIE and
134/// the value is the corresponding qualified name of the DIE.
135typedef unordered_map<void*, interned_string> die_istring_map_type;
136
137/// Convenience typedef for a map which is an interned_string and
138/// which value is a vector of DIE addresses.
139typedef unordered_map<interned_string,
143
144/// A hasher for a pair of void*. This is used as a hasher for
145/// the type @ref dwarf_addr_pair_set_type.
146struct dwarf_addr_pair_hash
147{
148 size_t
149 operator()(const dwarf_addr_pair_type& p) const
150 {
151 ABG_ASSERT(sizeof(void*) <= sizeof(uint64_t));
152 return *abigail::hashing::combine_hashes(hash_t(reinterpret_cast<uint64_t>(p.first)),
153 hash_t(reinterpret_cast<uint64_t>(p.second)));
154 }
155};// end struct dwarf_addr_pair_hash
156
157typedef unordered_set<dwarf_addr_pair_type,
158 dwarf_addr_pair_hash> dwarf_addr_pair_set_type;
159
160/// A convenience typedef for a vector of pairs of void*
161typedef vector<dwarf_addr_pair_type> dwarf_addr_pairs_type;
162
163/// A convenience typedef for an unordered map that associates a pair
164/// of dwarf_addr_pair_type to a vector of pairs void*.
165typedef unordered_map<dwarf_addr_pair_type,
167 dwarf_addr_pair_hash> dwarf_addr_pairs_map_type;
168
169/// A convenience typedef for an unordered_map that associates a pair
170/// of dwarf_addr_type to a set of pairs of void*.
171typedef unordered_map<dwarf_addr_pair_type,
172 dwarf_addr_pair_set_type,
173 dwarf_addr_pair_hash> dwarf_addr_pair_set_map_type;
174
175/// A convenience typedef for an unordered set of 'void*'.
176typedef unordered_set<void*> addr_set_type;
177
178class reader;
179
180static void
181finish_member_function_reading(Dwarf_Die* die,
182 const function_decl_sptr& f,
183 const class_or_union_sptr klass,
184 reader& rdr);
185
186static void
187build_translation_unit_and_add_to_ir(reader& rdr,
188 Dwarf_Die die,
190
191static void
192maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
193 Dwarf_Die* die);
194
195static void
196cleanup_decl_name(string&);
197
198using workers::task;
200
201/// A type alias for a function type to build a translation unit.
202///
203/// The function takes a DWARF Reader, a Dwarf_Die (representing a
204/// translation unit to build the IR for) and the resulting
205/// translation_unit IR to populate. The function returns void.
207 void (*)(reader&, Dwarf_Die, translation_unit_sptr);
208
209/// A type alias for a task to build a translation unit in its own
210/// separate thread. The task executes a function of type @ref
211/// tu_building_fn_type.
214 /*function return type=*/
215 void,
216 /*function arguments=*/
217 reader&, Dwarf_Die,
219
220///A typedef of a shared pointer of @ref tu_building_task_type.
221typedef shared_ptr<tu_building_task_type> tu_building_task_type_sptr;
222
223/// Convenience typedef for a shared pointer to an
224/// addr_elf_symbol_sptr_map_type.
225typedef shared_ptr<addr_elf_symbol_sptr_map_type> addr_elf_symbol_sptr_map_sptr;
226
227/// Convenience typedef for a map that associates an @ref
228/// interned_string to a @ref function_type_sptr.
229typedef unordered_map<interned_string,
232
233/// Convenience typedef for a stack containing the scopes up to the
234/// current point in the abigail Internal Representation (aka IR) tree
235/// that is being built.
236typedef stack<scope_decl_sptr> scope_stack_type;
237
238/// Convenience typedef for a flat hash which key is a dwarf DIE
239/// address. The value is also a dwarf address.
240typedef phmap::flat_hash_map<void*, void*> addr_addr_phmap_type;
241
242/// Convenience typedef for a map which key is a string and which
243/// value is a vector of smart pointer to a class_or_union_sptr.
244typedef unordered_map<string, classes_or_unions_type> string_classes_or_unions_map;
245
246/// Convenience typedef for a map which key is a string and which
247/// value is a vector of smart pointer to a class.
248typedef unordered_map<string, classes_type> string_classes_map;
249
250/// Convenience typedef for a map which key is a string and which
251/// value is a vector of smart pointer to a enum.
252typedef unordered_map<string, enums_type> string_enums_map;
253
254/// The abstraction of the place where a partial unit has been
255/// imported. This is what the DW_TAG_imported_unit DIE expresses.
256///
257/// This type thus contains:
258/// - the addr to which the partial unit is imported
259/// - the addr of the imported partial unit.
260/// - the addr of the imported partial unit tree.
261struct imported_unit_point
262{
263 void* addr_of_import = nullptr;
264 void* imported_unit_die_addr = nullptr;
265 void* imported_unit_child_addr = nullptr;
266
267 /// Constructor of @ref the type imported_unit_point.
268 ///
269 /// @param import_addr the address of the point at which the unit
270 /// has been imported.
271 imported_unit_point(const void* import_addr)
272 : addr_of_import(const_cast<void*>(import_addr))
273 {}
274
275 /// Constructor of @ref the type imported_unit_point.
276 ///
277 /// @param import_addr the addressof the point at which the unit has
278 /// been imported.
279 ///
280 /// @param from where the imported DIE comes from.
281 ///
282 /// @param imported_die the die of the unit that has been imported.
283 imported_unit_point(void* import_addr, const Dwarf_Die& imported_die)
284 : addr_of_import(import_addr),
285 imported_unit_die_addr(imported_die.addr)
286 {
287 Dwarf_Die imported_unit_child;
288
289 ABG_ASSERT(dwarf_child(const_cast<Dwarf_Die*>(&imported_die),
290 &imported_unit_child) == 0);
291
292 imported_unit_child_addr = imported_unit_child.addr;
293 }
294}; // struct imported_unit_point
295
296/// Convenience typedef for a vector of @ref imported_unit_point.
297typedef vector<imported_unit_point> imported_unit_points_type;
298
299/// Convenience typedef for a vector of @ref imported_unit_point.
300typedef unordered_map<void*, imported_unit_points_type>
302
303/// "Less than" operator for instances of @ref imported_unit_point
304/// type.
305///
306/// @param the left hand side operand of the "Less than" operator.
307///
308/// @param the right hand side operand of the "Less than" operator.
309///
310/// @return true iff @p l is less than @p r.
311static bool
312operator<(const imported_unit_point& l, const imported_unit_point& r)
313{return l.addr_of_import < r.addr_of_import;}
314
315
316static bool
317get_die_language(const Dwarf_Die *die, translation_unit::language &lang) ;
318
319static bool
320get_translation_unit_die_for_die(const Dwarf_Die* die,
321 Dwarf_Die& cu_die);
322
323static string
324get_path_of_translation_unit_die(const Dwarf_Die& tu_die,
325 bool absolute = false);
326
327static string
328get_comp_dir_of_translation_unit_die(const Dwarf_Die& tu_die);
329
330static bool
331die_is_in_c(const Dwarf_Die *die);
332
333static bool
334die_is_anonymous(const Dwarf_Die* die);
335
336static bool
337die_is_anonymous_data_member(const Dwarf_Die* die);
338
339static bool
340die_is_type(const Dwarf_Die* die);
341
342static bool
343die_is_decl(const Dwarf_Die* die);
344
345static bool
346die_is_declaration_only(Dwarf_Die* die);
347
348static bool
349die_is_variable_decl(const Dwarf_Die *die);
350
351static bool
352die_is_virtual(const Dwarf_Die* die);
353
354static bool
355die_is_function_decl(const Dwarf_Die *die);
356
357static bool
358die_is_destructor(const Dwarf_Die *die);
359
360static bool
361die_has_size_attribute(const Dwarf_Die *die);
362
363static bool
364die_is_namespace(const Dwarf_Die* die);
365
366static bool
367die_is_unspecified(Dwarf_Die* die);
368
369static bool
370die_is_void_type(Dwarf_Die* die);
371
372static bool
373die_is_pointer_type(const Dwarf_Die* die);
374
375static bool
376die_is_reference_type(const Dwarf_Die* die);
377
378static bool
379die_is_pointer_or_reference_type(const Dwarf_Die* die);
380
381static bool
382die_is_class_type(const Dwarf_Die* die);
383
384static bool
385die_has_object_pointer(const Dwarf_Die* die,
386 Dwarf_Die& object_pointer);
387
388static bool
389die_has_children(const Dwarf_Die* die);
390
391static string
392die_string_attribute(const Dwarf_Die* die, unsigned attr_name);
393
394static bool
395fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die);
396
397static bool
398get_member_fn_class_die_from_object_pointer(const Dwarf_Die* die,
399 Dwarf_Die& class_die,
400 Dwarf_Die& object_ptr_die);
401
402static bool
403die_this_pointer_from_object_pointer(Dwarf_Die* die,
404 Dwarf_Die& this_pointer);
405
406static bool
407die_this_pointer_is_const(Dwarf_Die* die);
408
409static bool
410die_object_pointer_is_for_const_method(Dwarf_Die* die);
411
412static bool
413eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
414 size_t expr_len,
415 int64_t& value,
416 bool& is_tls_address);
417
419dwarf_language_to_tu_language(size_t l);
420
421static bool
422die_unsigned_constant_attribute(const Dwarf_Die* die,
423 unsigned attr_name,
424 uint64_t& cst);
425
426static bool
427die_signed_constant_attribute(const Dwarf_Die*die,
428 unsigned attr_name,
429 int64_t& cst);
430
431static bool
432die_constant_attribute(const Dwarf_Die *die,
433 unsigned attr_name,
434 bool is_signed,
436
437static bool
438die_member_offset(const reader& rdr,
439 const Dwarf_Die* die,
440 int64_t& offset);
441
442static bool
443die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result);
444
445static string
446die_name(const Dwarf_Die* die);
447
448static void
449die_name_and_linkage_name(const Dwarf_Die* die,
450 string& name,
451 string& linkage_name);
452
453static bool
454die_location_address(Dwarf_Die* die,
455 Dwarf_Addr& address,
456 bool& is_tls_address);
457
458static bool
459die_die_attribute(const Dwarf_Die* die,
460 unsigned attr_name,
461 Dwarf_Die& result,
462 bool recursively = true);
463
464static bool
465die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die);
466
467static bool
468subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
469 unsigned attr_name,
470 Dwarf_Die& referenced_subrange);
471static string
472get_internal_anonymous_die_prefix_name(const Dwarf_Die *die);
473
474static bool
475die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die);
476
477static bool
478die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die);
479
480static bool
481die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die);
482
483static void
484maybe_canonicalize_type(const type_base_sptr& t,
485 reader& rdr);
486
487static uint64_t
488get_default_array_lower_bound(translation_unit::language l);
489
490static bool
491find_lower_bound_in_imported_unit_points(const imported_unit_points_type&,
492 const void*,
493 imported_unit_points_type::const_iterator&);
494
495static bool
496get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child);
497
498static bool
499get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member);
500
501/// Get the language used to generate a given DIE.
502///
503/// @param die the DIE to consider.
504///
505/// @param lang the resulting language.
506///
507/// @return true iff the language of the DIE was found.
508static bool
509get_die_language(const Dwarf_Die *die, translation_unit::language &lang)
510{
511 Dwarf_Die cu_die;
512 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
513
514 uint64_t l = 0;
515 if (!die_unsigned_constant_attribute(&cu_die, DW_AT_language, l))
516 return false;
517
518 lang = dwarf_language_to_tu_language(l);
519 return true;
520}
521
522/// The translation unit DIE that a given DIE belongs to.
523///
524/// @param die the DIE to consider.
525///
526/// @param cur_die the resulting translation unit DIE. This is
527/// populated iff the function returns true.
528///
529/// @return true iff the function could populate @p cu_die with the
530/// translation unit DIE for @p die.
531static bool
532get_translation_unit_die_for_die(const Dwarf_Die* die,
533 Dwarf_Die& cu_die)
534{
535 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
536
537 Dwarf_Die *testing_die = const_cast<Dwarf_Die*>(die);
538 if (dwarf_dieoffset(testing_die) == 0xf9)
539 {
540 if (dwarf_dieoffset(&cu_die) != 0xb)
541 {
542 std::cerr << "Got CU DIE offset "
543 << std::hex
544 <<dwarf_dieoffset(&cu_die)
545 << " in lieu of "
546 << 0xf9
547 << std::endl;
549 }
550 }
551 return true;
552}
553
554/// Get the path of a translation unit DIE, as given by the DW_AT_name
555/// attribute of the DIE.
556///
557/// @param tu_die the translation unit DIE to consider.
558///
559/// @param absolute if true, then construct the absolute path,
560/// otherwise, return the relative path.
561///
562/// @return the path of the TU
563static string
564get_path_of_translation_unit_die(const Dwarf_Die& tu_die, bool absolute)
565{
566 string path = die_string_attribute(&tu_die, DW_AT_name);
567 if (path == "<artificial>")
568 {
569 // This is a file artificially generated by the
570 // compiler, so its name is '<artificial>'. As we
571 // want all different translation units to have
572 // unique path names, let's suffix this path name
573 // with its die offset.
574 std::ostringstream o;
575 o << path
576 << "-"
577 << std::hex
578 << dwarf_dieoffset(const_cast<Dwarf_Die*>(&tu_die));
579 path = o.str();
580 }
581
582 if (absolute)
583 {
584 string compilation_dir =
585 get_comp_dir_of_translation_unit_die(tu_die);
586 const string& abs_path =
587 compilation_dir.empty() ? path : compilation_dir + "/" + path;
588 path = abs_path;
589 }
590
591 return path;
592}
593
594/// Get the compilation directory of a given translation unit DIE.
595///
596/// @param tu_die the translation unit DIE to consider.
597///
598/// @return the compilation directory.
599static string
600get_comp_dir_of_translation_unit_die(const Dwarf_Die& tu_die)
601{
602 string compilation_dir = die_string_attribute(&tu_die, DW_AT_comp_dir);
603 return compilation_dir;
604}
605
606/// Test if a given DIE originates from a program written in the C
607/// language.
608///
609/// @param die the DIE to consider.
610///
611/// @return true iff @p die originates from a program in the C
612/// language.
613static bool
614die_is_in_c(const Dwarf_Die *die)
615{
616 translation_unit::language l = translation_unit::LANG_UNKNOWN;
617 if (!get_die_language(die, l))
618 return false;
619 return is_c_language(l);
620}
621
622/// Compare a symbol name against another name, possibly demangling
623/// the symbol_name before performing the comparison.
624///
625/// @param symbol_name the symbol_name to take in account.
626///
627/// @param name the second name to take in account.
628///
629/// @param demangle if true, demangle @p symbol_name and compare the
630/// result of the demangling with @p name.
631///
632/// @return true iff symbol_name equals name.
633static bool
634compare_symbol_name(const string& symbol_name,
635 const string& name,
636 bool demangle)
637{
638 if (demangle)
639 {
640 string m = demangle_cplus_mangled_name(symbol_name);
641 return m == name;
642 }
643 return symbol_name == name;
644}
645
646/// Lookup a symbol using the SysV ELF hash table.
647///
648/// Note that this function hasn't been tested. So it hasn't been
649/// debugged yet. IOW, it is not known to work. Or rather, it's
650/// almost like it's surely doesn't work ;-)
651///
652/// Use it at your own risks. :-)
653///
654///@parm env the environment we are operating from.
655///
656/// @param elf_handle the elf_handle to use.
657///
658/// @param sym_name the symbol name to look for.
659///
660/// @param ht_index the index (in the section headers table) of the
661/// hash table section to use.
662///
663/// @param sym_tab_index the index (in the section headers table) of
664/// the symbol table to use.
665///
666/// @param demangle if true, demangle @p sym_name before comparing it
667/// to names from the symbol table.
668///
669/// @param syms_found a vector of symbols found with the name @p
670/// sym_name. table.
671static bool
672lookup_symbol_from_sysv_hash_tab(const environment& env,
673 Elf* elf_handle,
674 const string& sym_name,
675 size_t ht_index,
676 size_t sym_tab_index,
677 bool demangle,
678 vector<elf_symbol_sptr>& syms_found)
679{
680 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
681 ABG_ASSERT(sym_tab_section);
682
683 Elf_Data* sym_tab_data = elf_getdata(sym_tab_section, 0);
684 ABG_ASSERT(sym_tab_data);
685
686 GElf_Shdr sheader_mem;
687 GElf_Shdr* sym_tab_section_header = gelf_getshdr(sym_tab_section,
688 &sheader_mem);
689 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
690 ABG_ASSERT(hash_section);
691
692 // Poke at the different parts of the hash table and get them ready
693 // to be used.
694 unsigned long hash = elf_hash(sym_name.c_str());
695 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
696 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
697 size_t nb_buckets = ht_data[0];
698 size_t nb_chains = ht_data[1];
699
700 if (nb_buckets == 0)
701 // An empty hash table. Not sure if that is possible, but it
702 // would mean an empty table of exported symbols.
703 return false;
704
705 //size_t nb_chains = ht_data[1];
706 Elf32_Word* ht_buckets = &ht_data[2];
707 Elf32_Word* ht_chains = &ht_buckets[nb_buckets];
708
709 // Now do the real work.
710 size_t bucket = hash % nb_buckets;
711 size_t symbol_index = ht_buckets[bucket];
712
713 GElf_Sym symbol;
714 const char* sym_name_str;
715 size_t sym_size;
716 elf_symbol::type sym_type;
717 elf_symbol::binding sym_binding;
718 elf_symbol::visibility sym_visibility;
719 bool found = false;
720
721 do
722 {
723 ABG_ASSERT(gelf_getsym(sym_tab_data, symbol_index, &symbol));
724 sym_name_str = elf_strptr(elf_handle,
725 sym_tab_section_header->sh_link,
726 symbol.st_name);
727 if (sym_name_str
728 && compare_symbol_name(sym_name_str, sym_name, demangle))
729 {
730 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
731 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
732 sym_visibility =
733 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
734 sym_size = symbol.st_size;
735 elf_symbol::version ver;
736 if (get_version_for_symbol(elf_handle, symbol_index,
737 /*get_def_version=*/true, ver))
738 ABG_ASSERT(!ver.str().empty());
739 elf_symbol_sptr symbol_found =
741 symbol_index,
742 sym_size,
743 sym_name_str,
744 sym_type,
745 sym_binding,
746 symbol.st_shndx != SHN_UNDEF,
747 symbol.st_shndx == SHN_COMMON,
748 ver, sym_visibility);
749 syms_found.push_back(symbol_found);
750 found = true;
751 }
752 symbol_index = ht_chains[symbol_index];
753 } while (symbol_index != STN_UNDEF || symbol_index >= nb_chains);
754
755 return found;
756}
757
758/// Get the size of the elf class, in bytes.
759///
760/// @param elf_handle the elf handle to use.
761///
762/// @return the size computed.
763static char
764get_elf_class_size_in_bytes(Elf* elf_handle)
765{
766 char result = 0;
767 GElf_Ehdr hdr;
768
769 ABG_ASSERT(gelf_getehdr(elf_handle, &hdr));
770 int c = hdr.e_ident[EI_CLASS];
771
772 switch (c)
773 {
774 case ELFCLASS32:
775 result = 4;
776 break;
777 case ELFCLASS64:
778 result = 8;
779 break;
780 default:
782 }
783
784 return result;
785}
786
787/// Get a given word of a bloom filter, referred to by the index of
788/// the word.
789///
790/// The bloom word size depends on the current elf class (32 bits for
791/// an ELFCLASS32 or 64 bits for an ELFCLASS64 one) and this function
792/// abstracts that nicely.
793///
794/// @param elf_handle the elf handle to use.
795///
796/// @param bloom_filter the bloom filter to consider.
797///
798/// @param index the index of the bloom filter to return.
799///
800/// @return a 64 bits work containing the bloom word found at index @p
801/// index. Note that if we are looking at an ELFCLASS32 binary, the 4
802/// most significant bytes of the result are going to be zero.
803static Elf64_Xword
804bloom_word_at(Elf* elf_handle,
805 Elf32_Word* bloom_filter,
806 size_t index)
807{
808 Elf64_Xword result = 0;
809 GElf_Ehdr h;
810 ABG_ASSERT(gelf_getehdr(elf_handle, &h));
811 int c;
812 c = h.e_ident[EI_CLASS];
813
814 switch(c)
815 {
816 case ELFCLASS32:
817 result = bloom_filter[index];
818 break ;
819 case ELFCLASS64:
820 {
821 Elf64_Xword* f= reinterpret_cast<Elf64_Xword*>(bloom_filter);
822 result = f[index];
823 }
824 break;
825 default:
826 abort();
827 }
828
829 return result;
830}
831
832/// The abstraction of the gnu elf hash table.
833///
834/// The members of this struct are explained at
835/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
836/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
837struct gnu_ht
838{
839 size_t nb_buckets;
840 Elf32_Word* buckets;
841 Elf32_Word* chain;
842 size_t first_sym_index;
843 size_t bf_nwords;
844 size_t bf_size;
845 Elf32_Word* bloom_filter;
846 size_t shift;
847 size_t sym_count;
848 Elf_Scn* sym_tab_section;
849 GElf_Shdr sym_tab_section_header;
850
851 gnu_ht()
852 : nb_buckets(0),
853 buckets(0),
854 chain(0),
855 first_sym_index(0),
856 bf_nwords(0),
857 bf_size(0),
858 bloom_filter(0),
859 shift(0),
860 sym_count(0),
861 sym_tab_section(0)
862 {}
863}; // end struct gnu_ht
864
865/// Setup the members of the gnu hash table.
866///
867/// @param elf_handle a handle on the elf file to use.
868///
869/// @param ht_index the index (into the elf section headers table) of
870/// the hash table section to use.
871///
872/// @param sym_tab_index the index (into the elf section headers
873/// table) of the symbol table the gnu hash table is about.
874///
875/// @param ht the resulting hash table.
876///
877/// @return true iff the hash table @ ht could be setup.
878static bool
879setup_gnu_ht(Elf* elf_handle,
880 size_t ht_index,
881 size_t sym_tab_index,
882 gnu_ht& ht)
883{
884 ht.sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
885 ABG_ASSERT(ht.sym_tab_section);
886 ABG_ASSERT(gelf_getshdr(ht.sym_tab_section, &ht.sym_tab_section_header));
887 ht.sym_count =
888 ht.sym_tab_section_header.sh_size / ht.sym_tab_section_header.sh_entsize;
889 Elf_Scn* hash_section = elf_getscn(elf_handle, ht_index);
890 ABG_ASSERT(hash_section);
891
892 // Poke at the different parts of the hash table and get them ready
893 // to be used.
894 Elf_Data* ht_section_data = elf_getdata(hash_section, 0);
895 Elf32_Word* ht_data = reinterpret_cast<Elf32_Word*>(ht_section_data->d_buf);
896
897 ht.nb_buckets = ht_data[0];
898 if (ht.nb_buckets == 0)
899 // An empty hash table. Not sure if that is possible, but it
900 // would mean an empty table of exported symbols.
901 return false;
902 ht.first_sym_index = ht_data[1];
903 // The number of words used by the bloom filter. A size of a word
904 // is ELFCLASS.
905 ht.bf_nwords = ht_data[2];
906 // The shift used by the bloom filter code.
907 ht.shift = ht_data[3];
908 // The data of the bloom filter proper.
909 ht.bloom_filter = &ht_data[4];
910 // The size of the bloom filter in 4 bytes word. This is going to
911 // be used to index the 'bloom_filter' above, which is of type
912 // Elf32_Word*; thus we need that bf_size be expressed in 4 bytes
913 // words.
914 ht.bf_size = (get_elf_class_size_in_bytes(elf_handle) / 4) * ht.bf_nwords;
915 // The buckets of the hash table.
916 ht.buckets = ht.bloom_filter + ht.bf_size;
917 // The chain of the hash table.
918 ht.chain = ht.buckets + ht.nb_buckets;
919
920 return true;
921}
922
923/// Look into the symbol tables of the underlying elf file and find
924/// the symbol we are being asked.
925///
926/// This function uses the GNU hash table for the symbol lookup.
927///
928/// The reference of for the implementation of this function can be
929/// found at:
930/// - https://sourceware.org/ml/binutils/2006-10/msg00377.html
931/// - https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections.
932///
933/// @param elf_handle the elf handle to use.
934///
935/// @param sym_name the name of the symbol to look for.
936///
937/// @param ht_index the index of the hash table header to use.
938///
939/// @param sym_tab_index the index of the symbol table header to use
940/// with this hash table.
941///
942/// @param demangle if true, demangle @p sym_name.
943///
944/// @param syms_found the vector of symbols found with the name @p
945/// sym_name.
946///
947/// @return true if a symbol was actually found.
948static bool
949lookup_symbol_from_gnu_hash_tab(const environment& env,
950 Elf* elf_handle,
951 const string& sym_name,
952 size_t ht_index,
953 size_t sym_tab_index,
954 bool demangle,
955 vector<elf_symbol_sptr>& syms_found)
956{
957 gnu_ht ht;
958 if (!setup_gnu_ht(elf_handle, ht_index, sym_tab_index, ht))
959 return false;
960
961 // Now do the real work.
962
963 // Compute bloom hashes (GNU hash and second bloom specific hashes).
964 size_t h1 = elf_gnu_hash(sym_name.c_str());
965 size_t h2 = h1 >> ht.shift;
966 // The size of one of the words used in the bloom
967 // filter, in bits.
968 int c = get_elf_class_size_in_bytes(elf_handle) * 8;
969 int n = (h1 / c) % ht.bf_nwords;
970 // The bitmask of the bloom filter has a size of either 32-bits on
971 // ELFCLASS32 binaries or 64-bits on ELFCLASS64 binaries. So we
972 // need a 64-bits type to hold the bitmap, hence the Elf64_Xword
973 // type used here. When dealing with 32bits binaries, the upper
974 // bits of the bitmask will be zero anyway.
975 Elf64_Xword bitmask = (1ul << (h1 % c)) | (1ul << (h2 % c));
976
977 // Test if the symbol is *NOT* present in this ELF file.
978 if ((bloom_word_at(elf_handle, ht.bloom_filter, n) & bitmask) != bitmask)
979 return false;
980
981 size_t i = ht.buckets[h1 % ht.nb_buckets];
982 if (i == STN_UNDEF)
983 return false;
984
985 Elf32_Word stop_word, *stop_wordp;
986 elf_symbol::version ver;
987 GElf_Sym symbol;
988 const char* sym_name_str;
989 bool found = false;
990
991 elf_symbol::type sym_type;
992 elf_symbol::binding sym_binding;
993 elf_symbol::visibility sym_visibility;
994
995 // Let's walk the hash table and record the versions of all the
996 // symbols which name equal sym_name.
997 for (i = ht.buckets[h1 % ht.nb_buckets],
998 stop_wordp = &ht.chain[i - ht.first_sym_index];
999 i != STN_UNDEF
1000 && (stop_wordp
1001 < ht.chain + (ht.sym_count - ht.first_sym_index));
1002 ++i, ++stop_wordp)
1003 {
1004 stop_word = *stop_wordp;
1005 if ((stop_word & ~ 1)!= (h1 & ~1))
1006 // A given bucket can reference several hashes. Here we
1007 // stumbled across a hash value different from the one we are
1008 // looking for. Let's keep walking.
1009 continue;
1010
1011 ABG_ASSERT(gelf_getsym(elf_getdata(ht.sym_tab_section, 0),
1012 i, &symbol));
1013 sym_name_str = elf_strptr(elf_handle,
1014 ht.sym_tab_section_header.sh_link,
1015 symbol.st_name);
1016 if (sym_name_str
1017 && compare_symbol_name(sym_name_str, sym_name, demangle))
1018 {
1019 // So we found a symbol (in the symbol table) that equals
1020 // sym_name. Now lets try to get its version and record it.
1021 sym_type = stt_to_elf_symbol_type(GELF_ST_TYPE(symbol.st_info));
1022 sym_binding = stb_to_elf_symbol_binding(GELF_ST_BIND(symbol.st_info));
1023 sym_visibility =
1024 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(symbol.st_other));
1025
1026 if (get_version_for_symbol(elf_handle, i,
1027 /*get_def_version=*/true,
1028 ver))
1029 ABG_ASSERT(!ver.str().empty());
1030
1031 elf_symbol_sptr symbol_found =
1032 elf_symbol::create(env, i,
1033 symbol.st_size,
1034 sym_name_str,
1035 sym_type, sym_binding,
1036 symbol.st_shndx != SHN_UNDEF,
1037 symbol.st_shndx == SHN_COMMON,
1038 ver, sym_visibility);
1039 syms_found.push_back(symbol_found);
1040 found = true;
1041 }
1042
1043 if (stop_word & 1)
1044 // The last bit of the stop_word is 1. That means we need to
1045 // stop here. We reached the end of the chain of values
1046 // referenced by the hask bucket.
1047 break;
1048 }
1049 return found;
1050}
1051
1052/// Look into the symbol tables of the underlying elf file and find
1053/// the symbol we are being asked.
1054///
1055/// This function uses the elf hash table (be it the GNU hash table or
1056/// the sysv hash table) for the symbol lookup.
1057///
1058/// @param env the environment we are operating from.
1059///
1060/// @param elf_handle the elf handle to use.
1061///
1062/// @param ht_kind the kind of hash table to use. This is returned by
1063/// the function function find_hash_table_section_index.
1064///
1065/// @param ht_index the index (in the section headers table) of the
1066/// hash table section to use.
1067///
1068/// @param sym_tab_index the index (in section headers table) of the
1069/// symbol table index to use with this hash table.
1070///
1071/// @param symbol_name the name of the symbol to look for.
1072///
1073/// @param demangle if true, demangle @p sym_name.
1074///
1075/// @param syms_found the symbols that were actually found with the
1076/// name @p symbol_name.
1077///
1078/// @return true iff the function found the symbol from the elf hash
1079/// table.
1080static bool
1081lookup_symbol_from_elf_hash_tab(const environment& env,
1082 Elf* elf_handle,
1083 hash_table_kind ht_kind,
1084 size_t ht_index,
1085 size_t symtab_index,
1086 const string& symbol_name,
1087 bool demangle,
1088 vector<elf_symbol_sptr>& syms_found)
1089{
1090 if (elf_handle == 0 || symbol_name.empty())
1091 return false;
1092
1093 if (ht_kind == NO_HASH_TABLE_KIND)
1094 return false;
1095
1096 if (ht_kind == SYSV_HASH_TABLE_KIND)
1097 return lookup_symbol_from_sysv_hash_tab(env,
1098 elf_handle, symbol_name,
1099 ht_index,
1100 symtab_index,
1101 demangle,
1102 syms_found);
1103 else if (ht_kind == GNU_HASH_TABLE_KIND)
1104 return lookup_symbol_from_gnu_hash_tab(env,
1105 elf_handle, symbol_name,
1106 ht_index,
1107 symtab_index,
1108 demangle,
1109 syms_found);
1110 return false;
1111}
1112
1113/// Lookup a symbol from the symbol table directly.
1114///
1115///
1116/// @param env the environment we are operating from.
1117///
1118/// @param elf_handle the elf handle to use.
1119///
1120/// @param sym_name the name of the symbol to look up.
1121///
1122/// @param sym_tab_index the index (in the section headers table) of
1123/// the symbol table section.
1124///
1125/// @param demangle if true, demangle the names found in the symbol
1126/// table before comparing them with @p sym_name.
1127///
1128/// @param sym_name_found the actual name of the symbol found.
1129///
1130/// @param sym_type the type of the symbol found.
1131///
1132/// @param sym_binding the binding of the symbol found.
1133///
1134/// @param sym_versions the versions of the symbol found.
1135///
1136/// @return true iff the symbol was found.
1137static bool
1138lookup_symbol_from_symtab(const environment& env,
1139 Elf* elf_handle,
1140 const string& sym_name,
1141 size_t sym_tab_index,
1142 bool demangle,
1143 vector<elf_symbol_sptr>& syms_found)
1144{
1145 // TODO: read all of the symbol table, store it in memory in a data
1146 // structure that associates each symbol with its versions and in
1147 // which lookups of a given symbol is fast.
1148 Elf_Scn* sym_tab_section = elf_getscn(elf_handle, sym_tab_index);
1149 ABG_ASSERT(sym_tab_section);
1150
1151 GElf_Shdr header_mem;
1152 GElf_Shdr * sym_tab_header = gelf_getshdr(sym_tab_section,
1153 &header_mem);
1154
1155 size_t symcount = sym_tab_header->sh_size / sym_tab_header->sh_entsize;
1156 Elf_Data* symtab = elf_getdata(sym_tab_section, NULL);
1157 GElf_Sym* sym;
1158 char* name_str = 0;
1159 elf_symbol::version ver;
1160 bool found = false;
1161
1162 for (size_t i = 0; i < symcount; ++i)
1163 {
1164 GElf_Sym sym_mem;
1165 sym = gelf_getsym(symtab, i, &sym_mem);
1166 name_str = elf_strptr(elf_handle,
1167 sym_tab_header->sh_link,
1168 sym->st_name);
1169
1170 if (name_str && compare_symbol_name(name_str, sym_name, demangle))
1171 {
1172 elf_symbol::type sym_type =
1173 stt_to_elf_symbol_type(GELF_ST_TYPE(sym->st_info));
1174 elf_symbol::binding sym_binding =
1175 stb_to_elf_symbol_binding(GELF_ST_BIND(sym->st_info));
1176 elf_symbol::visibility sym_visibility =
1177 stv_to_elf_symbol_visibility(GELF_ST_VISIBILITY(sym->st_other));
1178 bool sym_is_defined = sym->st_shndx != SHN_UNDEF;
1179 bool sym_is_common = sym->st_shndx == SHN_COMMON;
1180
1181 if (get_version_for_symbol(elf_handle, i,
1182 /*get_def_version=*/sym_is_defined,
1183 ver))
1184 ABG_ASSERT(!ver.str().empty());
1185 elf_symbol_sptr symbol_found =
1186 elf_symbol::create(env, i, sym->st_size,
1187 name_str, sym_type,
1188 sym_binding, sym_is_defined,
1189 sym_is_common, ver, sym_visibility);
1190 syms_found.push_back(symbol_found);
1191 found = true;
1192 }
1193 }
1194
1195 if (found)
1196 return true;
1197
1198 return false;
1199}
1200
1201/// Look into the symbol tables of the underlying elf file and see
1202/// if we find a given symbol.
1203///
1204/// @param env the environment we are operating from.
1205///
1206/// @param symbol_name the name of the symbol to look for.
1207///
1208/// @param demangle if true, try to demangle the symbol name found in
1209/// the symbol table before comparing it to @p symbol_name.
1210///
1211/// @param syms_found the list of symbols found, with the name @p
1212/// symbol_name.
1213///
1214/// @param sym_type this is set to the type of the symbol found. This
1215/// shall b a standard elf.h value for symbol types, that is SHT_OBJECT,
1216/// STT_FUNC, STT_IFUNC, etc ...
1217///
1218/// Note that this parameter is set iff the function returns true.
1219///
1220/// @param sym_binding this is set to the binding of the symbol found.
1221/// This is a standard elf.h value of the symbol binding kind, that
1222/// is, STB_LOCAL, STB_GLOBAL, or STB_WEAK.
1223///
1224/// @param symbol_versions the versions of the symbol @p symbol_name,
1225/// if it was found.
1226///
1227/// @return true iff a symbol with the name @p symbol_name was found.
1228static bool
1229lookup_symbol_from_elf(const environment& env,
1230 Elf* elf_handle,
1231 const string& symbol_name,
1232 bool demangle,
1233 vector<elf_symbol_sptr>& syms_found)
1234{
1235 size_t hash_table_index = 0, symbol_table_index = 0;
1236 hash_table_kind ht_kind = NO_HASH_TABLE_KIND;
1237
1238 if (!demangle)
1239 ht_kind = find_hash_table_section_index(elf_handle,
1240 hash_table_index,
1241 symbol_table_index);
1242
1243 if (ht_kind == NO_HASH_TABLE_KIND)
1244 {
1245 if (!find_symbol_table_section_index(elf_handle, symbol_table_index))
1246 return false;
1247
1248 return lookup_symbol_from_symtab(env,
1249 elf_handle,
1250 symbol_name,
1251 symbol_table_index,
1252 demangle,
1253 syms_found);
1254 }
1255
1256 return lookup_symbol_from_elf_hash_tab(env,
1257 elf_handle,
1258 ht_kind,
1259 hash_table_index,
1260 symbol_table_index,
1261 symbol_name,
1262 demangle,
1263 syms_found);
1264}
1265
1266/// Look into the symbol tables of the underlying elf file and see if
1267/// we find a given public (global or weak) symbol of function type.
1268///
1269/// @param env the environment we are operating from.
1270///
1271/// @param elf_handle the elf handle to use for the query.
1272///
1273/// @param symbol_name the function symbol to look for.
1274///
1275/// @param func_syms the vector of public functions symbols found, if
1276/// any.
1277///
1278/// @return true iff the symbol was found.
1279static bool
1280lookup_public_function_symbol_from_elf(environment& env,
1281 Elf* elf_handle,
1282 const string& symbol_name,
1283 vector<elf_symbol_sptr>& func_syms)
1284{
1285 vector<elf_symbol_sptr> syms_found;
1286 bool found = false;
1287
1288 if (lookup_symbol_from_elf(env, elf_handle, symbol_name,
1289 /*demangle=*/false, syms_found))
1290 {
1291 for (vector<elf_symbol_sptr>::const_iterator i = syms_found.begin();
1292 i != syms_found.end();
1293 ++i)
1294 {
1295 elf_symbol::type type = (*i)->get_type();
1296 elf_symbol::binding binding = (*i)->get_binding();
1297
1298 if ((type == elf_symbol::FUNC_TYPE
1299 || type == elf_symbol::GNU_IFUNC_TYPE
1300 || type == elf_symbol::COMMON_TYPE)
1301 && (binding == elf_symbol::GLOBAL_BINDING
1302 || binding == elf_symbol::WEAK_BINDING))
1303 {
1304 func_syms.push_back(*i);
1305 found = true;
1306 }
1307 }
1308 }
1309
1310 return found;
1311}
1312
1313// ---------------------------------------
1314// <location expression evaluation types>
1315// ---------------------------------------
1316
1317/// An abstraction of a value representing the result of the
1318/// evaluation of a dwarf expression. This is abstraction represents
1319/// a partial view on the possible values because we are only
1320/// interested in extracting the latest and longuest constant
1321/// sub-expression of a given dwarf expression.
1322class expr_result
1323{
1324 bool is_const_;
1325 int64_t const_value_;
1326
1327public:
1328 expr_result()
1329 : is_const_(true),
1330 const_value_(0)
1331 {}
1332
1333 expr_result(bool is_const)
1334 : is_const_(is_const),
1335 const_value_(0)
1336 {}
1337
1338 explicit expr_result(int64_t v)
1339 :is_const_(true),
1340 const_value_(v)
1341 {}
1342
1343 /// @return true if the value is a constant. Otherwise, return
1344 /// false, meaning the value represents a quantity for which we need
1345 /// inferior (a running program) state to determine the value.
1346 bool
1347 is_const() const
1348 {return is_const_;}
1349
1350
1351 /// @param f a flag saying if the value is set to a constant or not.
1352 void
1353 is_const(bool f)
1354 {is_const_ = f;}
1355
1356 /// Get the current constant value iff this represents a
1357 /// constant.
1358 ///
1359 /// @param value the out parameter. Is set to the constant value of
1360 /// the @ref expr_result. This is set iff the function return true.
1361 ///
1362 ///@return true if this has a constant value, false otherwise.
1363 bool
1364 const_value(int64_t& value)
1365 {
1366 if (is_const())
1367 {
1368 value = const_value_;
1369 return true;
1370 }
1371 return false;
1372 }
1373
1374 /// Getter of the constant value of the current @ref expr_result.
1375 ///
1376 /// Note that the current @ref expr_result must be constant,
1377 /// otherwise the current process is aborted.
1378 ///
1379 /// @return the constant value of the current @ref expr_result.
1380 int64_t
1381 const_value() const
1382 {
1383 ABG_ASSERT(is_const());
1384 return const_value_;
1385 }
1386
1387 operator int64_t() const
1388 {return const_value();}
1389
1390 expr_result&
1391 operator=(const int64_t v)
1392 {
1393 const_value_ = v;
1394 return *this;
1395 }
1396
1397 bool
1398 operator==(const expr_result& o) const
1399 {return const_value_ == o.const_value_ && is_const_ == o.is_const_;}
1400
1401 bool
1402 operator>=(const expr_result& o) const
1403 {return const_value_ >= o.const_value_;}
1404
1405 bool
1406 operator<=(const expr_result& o) const
1407 {return const_value_ <= o.const_value_;}
1408
1409 bool
1410 operator>(const expr_result& o) const
1411 {return const_value_ > o.const_value_;}
1412
1413 bool
1414 operator<(const expr_result& o) const
1415 {return const_value_ < o.const_value_;}
1416
1417 expr_result
1418 operator+(const expr_result& v) const
1419 {
1420 expr_result r(*this);
1421 r.const_value_ += v.const_value_;
1422 r.is_const_ = r.is_const_ && v.is_const_;
1423 return r;
1424 }
1425
1426 expr_result&
1427 operator+=(int64_t v)
1428 {
1429 const_value_ += v;
1430 return *this;
1431 }
1432
1433 expr_result
1434 operator-(const expr_result& v) const
1435 {
1436 expr_result r(*this);
1437 r.const_value_ -= v.const_value_;
1438 r.is_const_ = r.is_const_ && v.is_const_;
1439 return r;
1440 }
1441
1442 expr_result
1443 operator%(const expr_result& v) const
1444 {
1445 expr_result r(*this);
1446 r.const_value_ %= v.const_value_;
1447 r.is_const_ = r.is_const_ && v.is_const();
1448 return r;
1449 }
1450
1451 expr_result
1452 operator*(const expr_result& v) const
1453 {
1454 expr_result r(*this);
1455 r.const_value_ *= v.const_value_;
1456 r.is_const_ = r.is_const_ && v.is_const();
1457 return r;
1458 }
1459
1460 expr_result
1461 operator|(const expr_result& v) const
1462 {
1463 expr_result r(*this);
1464 r.const_value_ |= v.const_value_;
1465 r.is_const_ = r.is_const_ && v.is_const_;
1466 return r;
1467 }
1468
1469 expr_result
1470 operator^(const expr_result& v) const
1471 {
1472 expr_result r(*this);
1473 r.const_value_ ^= v.const_value_;
1474 r.is_const_ = r.is_const_ && v.is_const_;
1475 return r;
1476 }
1477
1478 expr_result
1479 operator>>(const expr_result& v) const
1480 {
1481 expr_result r(*this);
1482 r.const_value_ = r.const_value_ >> v.const_value_;
1483 r.is_const_ = r.is_const_ && v.is_const_;
1484 return r;
1485 }
1486
1487 expr_result
1488 operator<<(const expr_result& v) const
1489 {
1490 expr_result r(*this);
1491 r.const_value_ = r.const_value_ << v.const_value_;
1492 r.is_const_ = r.is_const_ && v.is_const_;
1493 return r;
1494 }
1495
1496 expr_result
1497 operator~() const
1498 {
1499 expr_result r(*this);
1500 r.const_value_ = ~r.const_value_;
1501 return r;
1502 }
1503
1504 expr_result
1505 neg() const
1506 {
1507 expr_result r(*this);
1508 r.const_value_ = -r.const_value_;
1509 return r;
1510 }
1511
1512 expr_result
1513 abs() const
1514 {
1515 expr_result r = *this;
1516 r.const_value_ = std::abs(static_cast<long double>(r.const_value()));
1517 return r;
1518 }
1519
1520 expr_result
1521 operator&(const expr_result& o)
1522 {
1523 expr_result r(*this);
1524 r.const_value_ &= o.const_value_;
1525 r.is_const_ = r.is_const_ && o.is_const_;
1526 return r;
1527 }
1528
1529 expr_result
1530 operator/(const expr_result& o)
1531 {
1532 expr_result r(*this);
1533 r.is_const_ = r.is_const_ && o.is_const_;
1534 return r.const_value() / o.const_value();
1535 }
1536};// class end expr_result;
1537
1538/// A class that implements a stack of @ref expr_result, to be used in
1539/// the engine evaluating DWARF expressions.
1540class expr_result_stack_type
1541{
1542 vector<expr_result> elems_;
1543
1544public:
1545
1546 expr_result_stack_type()
1547 {elems_.reserve(4);}
1548
1549 expr_result&
1550 operator[](unsigned i)
1551 {
1552 unsigned s = elems_.size();
1553 ABG_ASSERT(s > i);
1554 return elems_[s - 1 -i];
1555 }
1556
1557 const expr_result&
1558 operator[](unsigned i) const
1559 {return const_cast<expr_result_stack_type*>(this)->operator[](i);}
1560
1561 unsigned
1562 size() const
1563 {return elems_.size();}
1564
1565 vector<expr_result>::reverse_iterator
1566 begin()
1567 {return elems_.rbegin();}
1568
1569 const vector<expr_result>::reverse_iterator
1570 begin() const
1571 {return const_cast<expr_result_stack_type*>(this)->begin();}
1572
1573 vector<expr_result>::reverse_iterator
1574 end()
1575 {return elems_.rend();}
1576
1577 const vector<expr_result>::reverse_iterator
1578 end() const
1579 {return const_cast<expr_result_stack_type*>(this)->end();}
1580
1581 expr_result&
1582 front()
1583 {return elems_.back();}
1584
1585 const expr_result&
1586 front() const
1587 {return const_cast<expr_result_stack_type*>(this)->front();}
1588
1589 void
1590 push_front(expr_result e)
1591 {elems_.push_back(e);}
1592
1593 expr_result
1594 pop_front()
1595 {
1596 expr_result r = front();
1597 elems_.pop_back();
1598 return r;
1599 }
1600
1601 void
1602 erase(vector<expr_result>::reverse_iterator i)
1603 {elems_.erase(--i.base());}
1604
1605 void
1606 clear()
1607 {elems_.clear();}
1608}; // end class expr_result_stack_type
1609
1610/// Abstraction of the evaluation context of a dwarf expression.
1611struct dwarf_expr_eval_context
1612{
1613 expr_result accum;
1614 expr_result_stack_type stack;
1615 // Is set to true if the result of the expression that got evaluated
1616 // is a TLS address.
1617 bool set_tls_addr;
1618
1619 dwarf_expr_eval_context()
1620 : accum(/*is_const=*/false),
1621 set_tls_addr(false)
1622 {
1623 stack.push_front(expr_result(true));
1624 }
1625
1626 void
1627 reset()
1628 {
1629 stack.clear();
1630 stack.push_front(expr_result(true));
1631 accum = expr_result(false);
1632 set_tls_addr = false;
1633 }
1634
1635 /// Set a flag to to tell that the result of the expression that got
1636 /// evaluated is a TLS address.
1637 ///
1638 /// @param f true iff the result of the expression that got
1639 /// evaluated is a TLS address, false otherwise.
1640 void
1641 set_tls_address(bool f)
1642 {set_tls_addr = f;}
1643
1644 /// Getter for the flag that tells if the result of the expression
1645 /// that got evaluated is a TLS address.
1646 ///
1647 /// @return true iff the result of the expression that got evaluated
1648 /// is a TLS address.
1649 bool
1650 set_tls_address() const
1651 {return set_tls_addr;}
1652
1653 expr_result
1654 pop()
1655 {
1656 expr_result r = stack.front();
1657 stack.pop_front();
1658 return r;
1659 }
1660
1661 void
1662 push(const expr_result& v)
1663 {stack.push_front(v);}
1664};//end class dwarf_expr_eval_context
1665
1666// ---------------------------------------
1667// </location expression evaluation types>
1668// ---------------------------------------
1669
1670class reader;
1671
1672typedef shared_ptr<reader> reader_sptr;
1673
1674struct die_parent_relations_builder_task : public task
1675{
1676 reader& rdr;
1677 Dwarf_Die tu_die;
1678 addr_addr_phmap_type parent_of;
1679 imported_unit_points_type& imported_units;
1680
1681 die_parent_relations_builder_task(reader& r,
1682 Dwarf_Die& tu,
1683 imported_unit_points_type& iprtd_units)
1684 : rdr(r),
1685 tu_die(tu),
1686 imported_units(iprtd_units)
1687 {}
1688
1689 virtual void perform();
1690
1691 void merge_die_parent_maps();
1692}; // end struct die_parent_relations_builder_task
1693
1694/// A convenience typedef for the a shared_ptr of
1695/// die_parent_relations_builder_task.
1696typedef shared_ptr<die_parent_relations_builder_task>
1698
1699/// The DWARF reader used to build the ABI corpus from debug info in
1700/// DWARF format.
1701///
1702/// This type is to be instanciated
1703/// abigail::dwarf::reader::create().
1704class reader : public elf_based_reader
1705{
1706public:
1707
1708 /// Statistics to help for debugging purposes.
1709 struct stats
1710 {
1711 unsigned number_of_suppressed_functions = 0;
1712 unsigned number_of_suppressed_variables = 0;
1713 unsigned number_of_allowed_functions = 0;
1714 unsigned number_of_allowed_variables = 0;
1715
1716 /// Clear the statistic data members.
1717 void
1718 clear()
1719 {
1720 number_of_suppressed_functions = 0;
1721 number_of_suppressed_variables = 0;
1722 number_of_allowed_functions = 0;
1723 number_of_allowed_variables = 0;
1724 }
1725 };
1726
1727 /// Context of the translation unit being constructed
1728 struct tu_context_type
1729 {
1730 Dwarf_Die tu_die_;
1732 scope_stack_type scope_stack_;
1733 list<var_decl_sptr> var_decls_to_add_;
1734 // A map of the DIEs of the classes being constructed, also known as
1735 // as work-in-progress classes, or WIP classes.
1736 mutable die_class_or_union_map_type die_wip_classes_map_;
1737
1738 // A map of the DIEs of the function types being constructed, also
1739 // known as as work-in-progress function types, or WIP function
1740 // types.
1741 mutable die_function_type_map_type die_wip_function_types_map_;
1742 type_sptr_set_type wip_function_types_;
1743 die_function_decl_map_type die_wip_function_decls_map_;
1744
1745 tu_context_type(Dwarf_Die tu_die, translation_unit* tu)
1746 : tu_die_(tu_die)
1747 {
1748 if (tu)
1749 tu_.reset(tu);
1750 }
1751
1752 tu_context_type(Dwarf_Die tu_die, translation_unit_sptr& tu)
1753 : tu_die_(tu_die), tu_(tu)
1754 {}
1755
1756 const Dwarf_Die*
1757 get_die() const
1758 {return &tu_die_;}
1759
1761 get_tu() const
1762 {return tu_;}
1763
1765 get_tu()
1766 {return tu_;}
1767
1768 const scope_stack_type&
1769 scope_stack() const
1770 {return scope_stack_;}
1771
1773 scope_stack()
1774 {return scope_stack_;}
1775
1777 current_scope()
1778 {
1779 if (scope_stack().empty())
1780 {
1781 if (get_tu())
1782 scope_stack().push(get_tu()->get_global_scope());
1783 }
1784 return scope_stack().top();
1785 }
1786
1787 list<var_decl_sptr>&
1788 var_decls_to_re_add_to_tree()
1789 {return var_decls_to_add_;}
1790
1791 const list<var_decl_sptr>&
1792 var_decls_to_re_add_to_tree() const
1793 {return var_decls_to_add_;}
1794
1795 /// Getter of a map that associates a die that represents a
1796 /// class/struct with the declaration of the class, while the class
1797 /// is being constructed.
1798 ///
1799 /// @param source where the DIE is from.
1800 ///
1801 /// @return the map that associates a DIE to the class that is being
1802 /// built.
1804 die_wip_classes_map() const
1805 {return die_wip_classes_map_;}
1806
1807 /// Getter of a map that associates a die that represents a
1808 /// class/struct with the declaration of the class, while the class
1809 /// is being constructed.
1810 ///
1811 /// @param source where the DIE comes from.
1812 ///
1813 /// @return the map that associates a DIE to the class that is being
1814 /// built.
1816 die_wip_classes_map()
1817 {return die_wip_classes_map_;}
1818
1819 /// Test if a DIE designated by its address is for a
1820 /// work-in-progress class_or_union_sptr IR node.
1821 ///
1822 /// @param die_addr the address of the DIE to consider.
1823 ///
1824 /// @return the @ref class_or_union_sptr IR node for @p die_addr
1825 /// if it's WIP ir node, nullptr otherwise.
1826 class_or_union_sptr
1827 die_addr_is_wip_class(const void* die_addr) const
1828 {
1829 ABG_ASSERT(die_addr);
1830
1831 const auto i = die_wip_classes_map().find(const_cast<void*>(die_addr));
1832 if (i != die_wip_classes_map().end())
1833 return i->second;
1834 return nullptr;
1835 }
1836
1837 /// Test if a DIE is for a work-in-progress class_or_union_sptr IR
1838 /// node.
1839 ///
1840 /// @param die the DIE to consider.
1841 ///
1842 /// @return the @ref class_or_union_sptr IR node for @p die if
1843 /// it's WIP ir node, nullptr otherwise.
1844 class_or_union_sptr
1845 die_is_wip_class(const Dwarf_Die* die) const
1846 {
1847 ABG_ASSERT(die);
1848 return die_addr_is_wip_class(die->addr);
1849 }
1850
1851 /// Mark a DIE designated by its address as being for a @ref
1852 /// class_or_union_sptr IR node that is being constructed (WIP ==
1853 /// work-in-progress).
1854 ///
1855 /// @param die_addr the address of the DIE to consider.
1856 ///
1857 /// @param wip_class_or_union the WIP @ref class_or_union_sptr IR
1858 /// node being constructed for @p die_addr.
1859 void
1860 mark_class_or_union_die_addr_as_wip(const void* die_addr,
1861 const class_or_union_sptr& wip_class_or_union)
1862 {
1863 ABG_ASSERT(die_addr && wip_class_or_union);
1864 die_wip_classes_map()[const_cast<void*>(die_addr)] =
1865 const_cast<class_or_union_sptr&>(wip_class_or_union);
1866 }
1867
1868 /// Mark a DIE designated by its address as being for a @ref
1869 /// class_or_union_sptr IR node that is being constructed (WIP ==
1870 /// work-in-progress).
1871 ///
1872 /// @param die_addr the address of the DIE to consider.
1873 ///
1874 /// @param wip_class_or_union the WIP @ref class_or_union_sptr IR
1875 /// node being constructed for @p die_addr.
1876 void
1877 mark_class_or_union_die_as_wip(const Dwarf_Die*die,
1878 const class_or_union_sptr& wip_cou)
1879 {
1880 ABG_ASSERT(die);
1881 mark_class_or_union_die_addr_as_wip(die->addr, wip_cou);
1882 }
1883
1884 /// Un-mark a DIE (designated by its address) that was previously
1885 /// marked as being for a WIP @ref class_or_union_sptr IR node.
1886 ///
1887 /// @param die_addr the address of the DIE to consider.
1888 void
1889 unmark_class_or_union_die_addr_as_wip(const void* die_addr)
1890 {
1891 ABG_ASSERT(die_addr);
1892 die_class_or_union_map_type::iterator i =
1893 die_wip_classes_map().find(const_cast<void*>(die_addr));
1894 if (i != die_wip_classes_map().end())
1895 die_wip_classes_map().erase(i);
1896 }
1897
1898 /// Un-mark a DIE that was previously marked as being for a WIP
1899 /// @ref class_or_union_sptr IR node.
1900 ///
1901 /// @param die the address of the DIE to consider.
1902 void
1903 unmark_class_or_union_die_as_wip(const Dwarf_Die* die)
1904 {
1905 ABG_ASSERT(die);
1906 return unmark_class_or_union_die_addr_as_wip(die->addr);
1907 }
1908
1909 /// Getter for a map that associates a die (that represents a
1910 /// function type) whith a function type, while the function type is
1911 /// being constructed (WIP == work in progress).
1912 ///
1913 /// @param source where the DIE comes from.n
1914 ///
1915 /// @return the map of wip function types.
1917 die_wip_function_types_map() const
1918 {return const_cast<tu_context_type*>(this)->die_wip_function_types_map();}
1919
1920 /// Getter for a map that associates a die (that represents a
1921 /// function type) whith a function type, while the function type is
1922 /// being constructed (WIP == work in progress).
1923 ///
1924 /// @param source where DIEs of the map come from.
1925 ///
1926 /// @return the map of wip function types.
1928 die_wip_function_types_map()
1929 {return die_wip_function_types_map_;}
1930
1931 /// Return true iff a given address is for the DIE of a function type
1932 /// that is being built at the moment, but is not fully built yet.
1933 /// WIP == work in progress.
1934 ///
1935 /// @param addr DIE address to consider.
1936 ///
1937 /// @return true iff @p addr is the address of the DIE of a
1938 /// function type that is being currently built.
1940 is_wip_function_type_die_address(const void* addr) const
1941 {
1942 die_function_type_map_type::const_iterator i =
1943 die_wip_function_types_map().find(const_cast<void*>(addr));
1944 if (i != die_wip_function_types_map().end())
1945 return i->second;
1946 return nullptr;
1947 }
1948
1949 /// Return true iff a given DIE represents a function type that is
1950 /// being built at the moment, but is not fully built yet. WIP ==
1951 /// work in progress.
1952 ///
1953 /// @param d DIE to consider.
1954 ///
1955 /// @return true iff @p d is the DIE of a function type that is
1956 /// being currently built.
1958 is_wip_function_type_die(const Dwarf_Die& d) const
1959 {return is_wip_function_type_die_address(d.addr);}
1960
1961 /// Return true iff a given DIE represents a function type that is
1962 /// being built at the moment, but is not fully built yet. WIP ==
1963 /// work in progress.
1964 ///
1965 /// @param d DIE to consider.
1966 ///
1967 /// @return true iff @p d is the DIE of a function type that is
1968 /// being currently built.
1970 is_wip_function_type_die(const Dwarf_Die* d) const
1971 {
1972 if (!d)
1973 return nullptr;
1974 return is_wip_function_type_die(*d);
1975 }
1976
1977 /// Test if a @ref function_type_sptr is work-in-progress IR node.
1978 ///
1979 /// A WIP IR node is an IR node being currently built for the
1980 /// current translation unit.
1981 bool
1982 is_wip_function_type(const function_type_sptr& f) const
1983 {
1984 auto i = wip_function_types_.find(f);
1985 if (i != wip_function_types_.end())
1986 return true;
1987 return false;
1988 }
1989
1990 /// Mark a function type, referred to by its DWARF DIE address, as a
1991 /// Work-In-Progress (WIP).
1992 ///
1993 /// @param die_addr the address of the DWARF DIE that refers to the
1994 /// function type to mark as WIP.
1995 ///
1996 /// @param t the function type to mark as WIP.
1997 void
1998 mark_function_type_die_addr_as_wip(const void* die_addr,
1999 const function_type_sptr& t)
2000 {
2001 ABG_ASSERT(die_addr && t);
2002 die_wip_function_types_map()[const_cast<void*>(die_addr)] =
2003 const_cast<function_type_sptr&>(t);
2004 wip_function_types_.insert(t);
2005 }
2006
2007 /// Mark the DIE of a function type as being a Work In Progress (WIP).
2008 ///
2009 /// @param die the DIE of the function type to mark as WIP.
2010 ///
2011 /// @param wip_fn_type the function type that is being built and that
2012 /// is not yet complete.
2013 void
2014 mark_function_type_die_as_wip(const Dwarf_Die*die,
2015 const function_type_sptr wip_fn_type)
2016 {
2017 ABG_ASSERT(die);
2018 mark_function_type_die_addr_as_wip(die->addr, wip_fn_type);
2019 }
2020
2021 /// Remove a given DIE address from the map of DIE addresses of
2022 /// function types that are being currently built (work-in-progress).
2023 ///
2024 /// @param die_addr the address of the DIE to unmark as
2025 /// work-in-progress.
2026 void
2027 unmark_function_type_die_addr_as_wip(const void* die_addr)
2028 {
2029 ABG_ASSERT(die_addr);
2030 //std::lock_guard<recursive_mutex> lock(die_wip_function_types_map_mutex_);
2031 die_function_type_map_type::iterator i =
2032 die_wip_function_types_map().find(const_cast<void*>(die_addr));
2033 if (i != die_wip_function_types_map().end())
2034 {
2035 wip_function_types_.erase(i->second);
2036 die_wip_function_types_map().erase(i);
2037 }
2038 }
2039
2040 /// Unmark a function type DIE address as being a Work In Progress (WIP).
2041 ///
2042 /// This removes the function type associated with the given DIE address
2043 /// from the WIP function types map and the WIP function types set.
2044 ///
2045 /// @param die_addr the address of the DIE to unmark as WIP. Must not
2046 /// be null.
2047 void
2048 unmark_function_type_die_as_wip(const Dwarf_Die* die)
2049 {
2050 ABG_ASSERT(die);
2051 return unmark_function_type_die_addr_as_wip(die->addr);
2052 }
2053
2054 /// Lookup the type associated to a DIE at a given address in the
2055 /// current translation unit context, in the current thread. The
2056 /// type must be in the process of being constructed, aka, WIP
2057 /// class, union, or function type.
2058 ///
2059 /// @param die_offset the offset of the DIE to consider.
2060 ///
2061 /// @param source the source of the DIE to consider.
2062 ///
2063 /// @return the type associated to the DIE or NULL if no type is
2064 /// associated to the DIE.
2065 type_base_sptr
2066 lookup_wip_type_from_die_addr(void* die_addr) const
2067 {
2068 type_base_sptr result;
2069
2070 // Maybe we are looking for a class type being constructed?
2071 if ((result = die_addr_is_wip_class(die_addr)))
2072 return result;
2073
2074 // Maybe we are looking for a function type being constructed?
2075 if ((result = is_wip_function_type_die_address(die_addr)))
2076 return result;
2077
2078 return result;
2079 }
2080
2081 /// Lookup the type associated to a DIE at a given address in the
2082 /// current translation unit context, in the current thread.
2083 ///
2084 /// @param die_offset the offset of the DIE to consider.
2085 ///
2086 /// @param source the source of the DIE to consider.
2087 ///
2088 /// @return the type associated to the DIE or NULL if no type is
2089 /// associated to the DIE.
2090 type_base_sptr
2091 lookup_wip_type_from_die(const Dwarf_Die* die) const
2092 {
2093 if (!die)
2094 return nullptr;
2095 return lookup_wip_type_from_die_addr(die->addr);
2096 }
2097 }; // end of tu_context_type
2098
2099 /// A convenience typedef for a shared_ptr of tu_context_type.
2100 typedef shared_ptr<tu_context_type> tu_context_type_sptr;
2101
2102 friend string
2103 die_qualified_name(const reader& rdr, const Dwarf_Die* die, void* where,
2104 reader::tu_context_type_sptr& tu_ctxt,
2105 unordered_set<void*>& guard);
2106
2107 friend string
2108 die_class_or_enum_flat_representation(const reader& rdr,
2109 const Dwarf_Die* die,
2110 const string& indent,
2111 bool one_line,
2112 bool qualified_names,
2113 void* where_addr,
2114 reader::tu_context_type_sptr& tu_ctxt,
2115 unordered_set<void*>& infinite_loop_guard);
2116
2117 friend string
2118 die_class_or_enum_flat_representation(const reader& rdr,
2119 const Dwarf_Die* die,
2120 const string& indent,
2121 bool one_line,
2122 bool qualified_names,
2123 void* where_addr,
2124 reader::tu_context_type_sptr& tu_ctxt);
2125
2126 friend string
2127 die_qualified_type_name(const reader& rdr,
2128 const Dwarf_Die* die,
2129 void* where,
2130 reader::tu_context_type_sptr& tu_ctxt,
2131 unordered_set<void*>& guard);
2132
2133 friend string
2134 die_pretty_print(reader& rdr,
2135 const Dwarf_Die* die,
2136 void* where_addr,
2137 reader::tu_context_type_sptr& tu_ctxt,
2138 unordered_set<void*>& infinite_loop_guard);
2139
2140 friend location
2141 die_location(const Dwarf_Die* die,
2142 reader::tu_context_type_sptr& tu_ctxt);
2143
2144 mutable recursive_mutex mutex_;
2145
2146 unsigned short dwarf_version_;
2147 scope_decl_sptr nil_scope_;
2148
2149 mutable die_istring_map_type die_qualified_name_maps_;
2150 mutable mutex die_qualified_name_maps_mutex_;
2151
2152 mutable die_istring_map_type die_pretty_repr_maps_;
2153 mutable recursive_mutex die_pretty_repr_maps_mutex_;
2154
2155 // A map that associates the address of a decl die to its
2156 // corresponding decl artifact, along with its mutex.
2157 mutable die_artefact_map_type decl_die_artefact_maps_;
2158
2159 // A map that associates the address of a type die to its
2160 // corresponding type artifact along with its mutex.
2161 mutable die_artefact_map_type type_die_artefact_maps_;
2162 mutable recursive_mutex die_artefact_maps_mutex_;
2163
2164 die_function_decl_map_type die_function_with_no_symbol_map_;
2165
2166 mutable type_wptr_set_type types_to_canonicalize_;
2167 mutable mutex types_to_canonicalize_mutex_;
2168
2169 mutable string_classes_or_unions_map decl_only_classes_map_;
2170 mutable mutex decl_only_classes_map_mutex_;
2171
2172 string_enums_map decl_only_enums_map_;
2173 mutable mutex decl_only_enums_map_mutex_;
2174
2175 mutable die_tu_map_type die_tu_map_;
2176 mutable mutex die_tu_map_mutex_;
2177
2178 mutable addr_addr_phmap_type die_parent_map_;
2179
2180 // A map that associates each tu die to a vector of unit import
2181 // points
2182 mutable tu_die_imported_unit_points_map_type tu_die_imported_unit_points_map_;
2183
2184 mutable die_function_decl_map_type methods_to_finish_reading_;
2185 mutable recursive_mutex methods_to_finish_reading_mutex_;
2186
2187 mutable stats stats_;
2188
2189protected:
2190
2191 reader() = delete;
2192
2193 /// Constructor of reader.
2194 ///
2195 /// @param elf_path the path to the elf file the context is to be
2196 /// used for.
2197 ///
2198 /// @param debug_info_root_paths a vector of pointers to the path to
2199 /// the root directory under which the debug info is to be found for
2200 /// @p elf_path. Leave this empty if the debug info is not in a
2201 /// split file.
2202 ///
2203 /// @param environment the environment used by the current context.
2204 /// This environment contains resources needed by the DWARF reader and by
2205 /// the types and declarations that are to be created later. Note
2206 /// that ABI artifacts that are to be compared all need to be
2207 /// created within the same environment.
2208 ///
2209 /// Please also note that the life time of this environment object
2210 /// must be greater than the life time of the resulting @ref
2211 /// reader the context uses resources that are allocated in
2212 /// the environment.
2213 ///
2214 /// @param options the options to set to this instance of @ref
2215 /// fe_iface. The options object needs to be created by the caller
2216 /// code.
2217 reader(const string& elf_path,
2218 const vector<string>& debug_info_root_paths,
2221 : elf_based_reader(elf_path,
2224 {
2225 reset(options);
2226 }
2227
2228 /// Clear the statistics for reading the current corpus.
2229 void
2230 clear_stats()
2231 {
2232 stats_.clear();
2233 }
2234
2235public:
2236
2237 /// Initializer of reader.
2238 ///
2239 /// Resets the reader so that it can be re-used to read another binary.
2240 ///
2241 /// @param opts the options to set to this instance of @ref
2242 /// fe_iface. The options object needs to be created by the caller
2243 /// code.
2244 void
2245 reset(const fe_iface::options_type& opts)
2246 {
2247 // Take the big mutex of the DWARF reader. This is the big hammer
2248 // option.
2249 std::lock_guard<recursive_mutex> lock(mutex_);
2250
2251 options() = opts;
2252
2253 dwarf_version_ = 0;
2254 die_qualified_name_maps().clear();
2255 die_pretty_repr_maps().clear();
2256 decl_die_artefact_maps().clear();
2257 type_die_artefact_maps().clear();
2258 die_function_decl_with_no_symbol_map().clear();
2259 types_to_canonicalize().clear();
2260 declaration_only_classes().clear();
2261 declaration_only_enums().clear();
2262 die_tu_map().clear();
2263 corpus().reset();
2264 corpus_group().reset();
2265 die_parent_map().clear();
2266 tu_die_imported_unit_points_map_.clear();
2267 clear_per_corpus_data();
2268 load_in_linux_kernel_mode(options().load_in_linux_kernel_mode);
2269 clear_stats();
2270 }
2271
2272 /// Initializer of reader.
2273 ///
2274 /// Resets the reader so that it can be re-used to read another binary.
2275 ///
2276 /// @param elf_path the path to the new ELF file.
2277 ///
2278 /// @param debug_info_root_paths the vector of debug-info path to
2279 /// look for split debug info.
2280 void
2281 initialize(const string& elf_path,
2282 const vector<string>& debug_info_root_paths)
2283 {
2285 reset(options());
2286 }
2287
2288 /// Create an instance of DWARF Reader.
2289 ///
2290 /// @param elf_path the path to the ELF file to read from.
2291 ///
2292 /// @param debug_info_root_paths a vector of paths where to look up
2293 /// split debug info files.
2294 ///
2295 /// @param environment the environment to be used by the reader.
2296 ///
2297 /// @param options the options to set to the newly created instance
2298 /// of @ref fe_iface. The options object needs to be created by the
2299 /// caller code.
2300 static dwarf::reader_sptr
2301 create(const std::string& elf_path,
2302 const vector<string>& debug_info_root_paths,
2305 {
2306 reader_sptr result(new reader(elf_path, debug_info_root_paths,
2308 return result;
2309 }
2310
2311 /// Destructor of the @ref reader type.
2312 ~reader()
2313 {
2314 }
2315
2316 /// Read and analyze the ELF and DWARF information associated with
2317 /// the underlying ELF file and build an ABI corpus out of it.
2318 ///
2319 /// @param status output parameter. This is set to the status of
2320 /// the analysis of the debug info.
2321 ///
2322 /// @return the resulting ABI corpus.
2323 corpus_sptr
2324 read_corpus(status& status)
2325 {
2327
2328 // Load the generic ELF parts of the corpus.
2330
2331 if (!(status & STATUS_OK))
2332 {
2333 // Something went badly wrong. There is nothing we can do
2334 // with this ELF file. Bail out.
2335 return corpus_sptr();
2336 }
2337
2338 // If we couldn't find debug info from the elf path, then say it.
2339 if (dwarf_debug_info() == nullptr)
2341
2342 {
2343 string alt_di_path;
2344 if (refers_to_alt_debug_info(alt_di_path)
2347 }
2348
2349 if (// If debug info was found but not the required alternate debug
2350 // info ...
2353 // ... then we cannot handle the binary.
2354 return corpus_sptr();
2355
2356 // Read the variable and function descriptions from the debug info
2357 // we have, through the dwfl handle.
2358 corpus_sptr corp = read_debug_info_into_corpus();
2359
2360 status |= STATUS_OK;
2361
2362 return corp;
2363 }
2364
2365 /// Create a translation unit type for a translation unit DIE.
2366 ///
2367 /// @param die the TU DIE to consider.
2368 ///
2369 /// @param address_size the address size used in @p die.
2370 ///
2371 /// @return the resulting translation unit object that is associated
2372 /// with @p die.
2374 create_translation_unit_to_be_populated(const Dwarf_Die& die,
2375 char address_size)
2376 {
2377 // Clear the part of the context that is dependent on the translation
2378 // unit we are reading.
2379
2380 string path = get_path_of_translation_unit_die(die);
2381
2383 // See if the same translation unit exits already in the current
2384 // corpus. Sometimes, the same translation unit can be present
2385 // several times in the same debug info. The content of the
2386 // different instances of the translation unit are different. So to
2387 // represent that, we are going to re-use the same translation
2388 // unit. That is, it's going to be the union of all the translation
2389 // units of the same path.
2390 {
2391 {
2392 lock_guard<recursive_mutex> lock(mutex_);
2393 tu = corpus()->find_translation_unit(path);
2394 if (!tu)
2395 {
2396 tu.reset(new translation_unit(env(), path,
2397 address_size));
2398 string compilation_dir =
2399 get_comp_dir_of_translation_unit_die(die);
2400 tu->set_compilation_dir_path(compilation_dir);
2401 corpus()->add(tu);
2402 uint64_t l = 0;
2403 die_unsigned_constant_attribute(&die, DW_AT_language, l);
2404 tu->set_language(dwarf_language_to_tu_language(l));
2405 }
2406 }
2407 }
2408
2409 ABG_ASSERT(tu);
2410
2411 associate_tu_die_with_tu(&die, tu);
2412
2413 return tu;
2414 }
2415
2416 // Create a task to build a translation_unit IR node from
2417 // a DW_TAG_compile_unit (translation unit) die.
2418 //
2419 // That task is to be executed in a separate thread and can be used
2420 // by the work queue machinery.
2421 //
2422 // @param tu_die the DW_TAG_compile_unit to use.
2423 //
2424 // @param address_size the address size used by @p tu_die.
2425 //
2426 // @return a task to which aims at constructing the IR node from @p
2427 // tu_die in a separate thread.
2429 create_tu_building_task(const Dwarf_Die& tu_die, char address_size)
2430 {
2431 // Create the translation unit to be populated and add it to the
2432 // current ABI corpus. It's important for the TU to be added to
2433 // the corpus *BEFORE* the task starts executing, otherwise, IR
2434 // nodes won't find a home.
2436 create_translation_unit_to_be_populated(tu_die, address_size);
2437 ABG_ASSERT(tu);
2438
2439 // Create the task now, using the function that knows how to build
2440 // the IR for the TU and the TU itself.
2441 tu_building_task_type_sptr build_tu_ir
2442 (new tu_building_task_type(build_translation_unit_and_add_to_ir,
2443 *this, tu_die, tu));
2444
2445 return build_tu_ir;
2446 }
2447
2448 /// Read an analyze the DWARF information.
2449 ///
2450 /// Construct an ABI corpus from it.
2451 ///
2452 /// This is a sub-routine of abigail::dwarf::reader::read_corpus().
2453 ///
2454 /// @return the resulting ABI corpus.
2455 corpus_sptr
2456 read_debug_info_into_corpus()
2457 {
2458 tools_utils::timer read_debug_info_into_corpus_timer;
2459 read_debug_info_into_corpus_timer.start();
2460
2461 // First set some mundane properties of the corpus gathered from
2462 // ELF.
2463 corpus::origin origin = corpus()->get_origin();
2464 origin |= corpus::DWARF_ORIGIN;
2465 corpus()->set_origin(origin);
2466 if (corpus_group())
2467 {
2468 origin |= corpus_group()->get_origin();
2469 corpus_group()->set_origin(origin);
2470 }
2471
2472 env().analyze_exported_interfaces_only(true);
2473
2474 if (load_all_types())
2475 env().load_all_types(true);
2476 else
2477 env().load_all_types(false);
2478
2479 if (load_undefined_interfaces())
2480 env().analyze_exported_interfaces_only(false);
2481 else
2482 env().analyze_exported_interfaces_only(true);
2483
2487 // Set symbols information to the corpus.
2488 corpus()->set_symtab(symtab());
2489
2490 // Get out now if no debug info is found or if the symbol table is
2491 // empty.
2492 if (!dwarf_debug_info()
2493 || !corpus()->get_symtab()
2494 || !corpus()->get_symtab()->has_symbols())
2495 return corpus();
2496
2497 uint8_t address_size = 0;
2498 size_t header_size = 0;
2499
2500#ifdef WITH_DEBUG_SELF_COMPARISON
2501 if (env().self_comparison_debug_is_on())
2502 {
2503 corpus_group_sptr g = corpus_group();
2504 if (g)
2505 env().set_self_comparison_debug_input(g);
2506 else
2507 env().set_self_comparison_debug_input(corpus());
2508 }
2509#endif
2510
2511 env().priv_->do_log(do_log());
2512
2513 // Walk all the DIEs of the debug info to build a DIE -> parent map
2514 // useful for get_die_parent() to work.
2515 {
2517 if (do_log())
2518 {
2519 cerr << "building die -> parent maps ...\n";
2520 t.start();
2521 }
2522
2523 build_die_parent_maps();
2524
2525 if (do_log())
2526 {
2527 t.stop();
2528 cerr << "built DIE -> parent maps for " << corpus()->get_path()
2529 << " in : "
2530 << t
2531 << "\n";
2532 }
2533 }
2534
2535 {
2537 if (do_log())
2538 {
2539 cerr << "DWARF Reader: building the "
2540 "libabigail internal representation ...\n";
2541 t.start();
2542 }
2543
2544 // And now, let's walk all the DW_TAG_compile_unit DIEs again to
2545 // build the libabigail IR.
2546 //
2547 // We want to stage one task per TU (DW_TAG_compile_unit DIE)
2548 // found in the binary. The purpose of each task is to
2549 // construct the IR for the children DIEs of the
2550 // DW_TAG_compile_unit DIE. The tasks are all staged in a
2551 // queue, and once the staging is done, the tasks are scheduled
2552 // for execution by worker threads. The number of worker
2553 // threads is determined by the the variable nb_workers below.
2555 workers::queue tu_ir_building_queue(nb_workers);
2556
2557 if (do_log())
2558 cerr << "DWARF Reader: Using "
2559 << nb_workers
2560 << " threads to construct the internal representations ...\n";
2561
2562 Dwarf_Half dwarf_vers = 0;
2563 unsigned nb_tus = 0;
2564 for (Dwarf_Off offset = 0, next_offset = 0;
2565 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
2566 offset, &next_offset, &header_size,
2567 &dwarf_vers, NULL, &address_size, NULL,
2568 NULL, NULL) == 0);
2569 offset = next_offset)
2570 {
2571 Dwarf_Off die_offset = offset + header_size;
2572 Dwarf_Die unit;
2573 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
2574 die_offset, &unit)
2575 || dwarf_tag(&unit) != DW_TAG_compile_unit)
2576 continue;
2577
2578 dwarf_version(dwarf_vers);
2579
2580 address_size *= 8;
2581
2582 nb_tus++;
2583
2585 create_tu_building_task(unit, address_size);
2586
2587 // Stage the task to the queue of tasks to be performed by
2588 // the workers that are waiting on the queue.
2589 ABG_ASSERT(tu_ir_building_queue.stage_task(task));
2590 }
2591
2592 // Now that all the tasks are staged, schedule them. That is,
2593 // let the workers execute the tasks!
2594 tu_ir_building_queue.schedule_staged_tasks();
2595
2596 // Wait for the workers to perform all the tasks that have been
2597 // scheduled in the queue.
2598 tu_ir_building_queue.wait_for_workers_to_complete();
2599
2600 if (do_log())
2601 {
2602 t.stop();
2603 cerr << "DWARF Reader: building "
2604 << "the libabigail internal representation for "
2605 << nb_tus << " translation units "
2606 << "DONE for corpus " << corpus()->get_path()
2607 << " which ended up with "
2608 << corpus()->get_translation_units().size()
2609 << " translation units in: "
2610 << t
2611 << "\n";
2612
2613 cerr << "DWARF Reader: Number of suppressed functions: "
2614 << stats_.number_of_suppressed_functions << "\n"
2615 << "Number of allowed functions: "
2616 << stats_.number_of_allowed_functions << "\n"
2617 << "Total number of fns in the corpus: "
2618 << corpus()->get_functions().size() << "\n"
2619 << "Total number of variables in the corpus: "
2620 << corpus()->get_variables().size() << "\n";
2621 }
2622 }
2623
2624 {
2626
2627 if (do_log())
2628 {
2629 cerr << "DWARF Reader: finish reading some methods sequentially ...";
2630 t.start();
2631 }
2632
2633 finish_reading_scheduled_methods();
2634
2635 if (do_log())
2636 {
2637 t.stop();
2638 cerr << " DONE@" << corpus()->get_path()
2639 << " in :"
2640 << t
2641 <<"\n";
2642 }
2643 }
2644
2645 {
2647 if (do_log())
2648 {
2649 cerr << "DWARF Reader: resolving declaration only classes ...";
2650 t.start();
2651 }
2652 resolve_declaration_only_classes();
2653 if (do_log())
2654 {
2655 t.stop();
2656 cerr << " DONE@" << corpus()->get_path()
2657 << " in :"
2658 << t
2659 <<"\n";
2660 }
2661 }
2662
2663 {
2665 if (do_log())
2666 {
2667 cerr << "resolving declaration only enums ...";
2668 t.start();
2669 }
2670 resolve_declaration_only_enums();
2671 if (do_log())
2672 {
2673 t.stop();
2674 cerr << " DONE@" << corpus()->get_path()
2675 << ":"
2676 << t
2677 <<"\n";
2678 }
2679 }
2680
2681 {
2683 if (do_log())
2684 {
2685 cerr << "DWARF Reader: fixing up functions with linkage name but "
2686 << "no advertised underlying symbols ....";
2687 t.start();
2688 }
2689 fixup_functions_with_no_symbols();
2690 if (do_log())
2691 {
2692 t.stop();
2693 cerr << " DONE@" << corpus()->get_path()
2694 <<" in :"
2695 << t
2696 <<"\n";
2697 }
2698 }
2699
2700 /// Now, look at the types that needs to be canonicalized after the
2701 /// translation has been constructed (which is just now) and
2702 /// canonicalize them.
2703 ///
2704 /// These types need to be constructed at the end of the translation
2705 /// unit reading phase because some types are modified by some DIEs
2706 /// even after the principal DIE describing the type has been read;
2707 /// this happens for clones of virtual destructors (for instance) or
2708 /// even for some static data members. We need to do that for types
2709 /// are in the alternate debug info section and for types that in
2710 /// the main debug info section.
2711 {
2713 if (do_log())
2714 {
2715 cerr << "DWARF Reader: perform late type canonicalizing ...\n";
2716 t.start();
2717 }
2718
2719 perform_late_type_canonicalizing();
2720
2721 if (do_log())
2722 {
2723 t.stop();
2724 cerr << "DWARF Reader: late type canonicalizing DONE for "
2725 << corpus()->get_path()
2726 << " in :"
2727 << t
2728 << "\n";
2729 }
2730 }
2731
2732 {
2734 if (do_log())
2735 {
2736 cerr << "DWARF Reader: sort functions and variables ...";
2737 t.start();
2738 }
2739
2742
2743 corpus()->mark_non_reachable_types();
2744
2745 if (do_log())
2746 {
2747 t.stop();
2748 read_debug_info_into_corpus_timer.stop();
2749 cerr << " DONE@" << corpus()->get_path()
2750 << ":"
2751 << t
2752 <<" \n";
2753
2754 cerr << "DWARF Reader: loaded debug info and built ABI corpus IR in: "
2755 << read_debug_info_into_corpus_timer <<" \n";
2756 }
2757 }
2758
2759 return corpus();
2760 }
2761
2762 /// Clear the data that is relevant for the current corpus being
2763 /// read.
2764 void
2765 clear_per_corpus_data()
2766 {
2767 die_qualified_name_maps().clear();
2768 die_pretty_repr_maps().clear();
2769 clear_types_to_canonicalize();
2770 }
2771
2773 die_qualified_name_maps() const
2774 {return die_qualified_name_maps_;}
2775
2777 die_qualified_name_maps()
2778 {return die_qualified_name_maps_;}
2779
2780 void
2781 set_die_qualified_name(const void* die_addr, interned_string istr) const
2782 {
2783 ABG_ASSERT(die_addr && !istr.empty());
2784 std::lock_guard<mutex> lock(die_qualified_name_maps_mutex_);
2785 die_qualified_name_maps_[const_cast<void*>(die_addr)] = istr;
2786 }
2787
2789 die_pretty_repr_maps() const
2790 {return die_pretty_repr_maps_;}
2791
2793 die_pretty_repr_maps()
2794 {return die_pretty_repr_maps_;}
2795
2796 void
2797 set_die_pretty_repr(const void* die_addr, interned_string istr) const
2798 {
2799 ABG_ASSERT(die_addr && !istr.empty());
2800 std::lock_guard<recursive_mutex> lock(die_pretty_repr_maps_mutex_);
2801 die_pretty_repr_maps_[const_cast<void*>(die_addr)] = istr;
2802 }
2803
2804 /// Getter for the current environment.
2805 ///
2806 /// @return the current environment.
2808 env()
2809 {return options().env;}
2810
2811 /// Getter for the current environment.
2812 ///
2813 /// @return the current environment.
2814 const environment&
2815 env() const
2816 {return const_cast<reader*>(this)->env();}
2817
2818 /// Getter for the flag that tells us if we are dropping functions
2819 /// and variables that have undefined symbols.
2820 ///
2821 /// @return true iff we are dropping functions and variables that have
2822 /// undefined symbols.
2823 bool
2824 drop_undefined_syms() const
2825 {return options().drop_undefined_syms;}
2826
2827 /// Setter for the flag that tells us if we are dropping functions
2828 /// and variables that have undefined symbols.
2829 ///
2830 /// @param f the new value of the flag.
2831 void
2832 drop_undefined_syms(bool f)
2833 {options().drop_undefined_syms = f;}
2834
2835 /// Getter of the DWARF version.
2836 unsigned short
2837 dwarf_version() const
2838 {
2839 std::lock_guard<recursive_mutex> lock(mutex_);
2840 return dwarf_version_;
2841 }
2842
2843 void
2844 dwarf_version(unsigned short v)
2845 {
2846 std::lock_guard<recursive_mutex> lock(mutex_);
2847 dwarf_version_ = v;
2848 }
2849
2850 /// Return the ELF descriptor used for DWARF access.
2851 ///
2852 /// This can be the same as reader::elf_handle() above, if the
2853 /// DWARF info is in the same ELF file as the one of the binary we
2854 /// are analizing. It is different if e.g, the debug info is split
2855 /// from the ELF file we are analizing.
2856 ///
2857 /// @return a pointer to the ELF descriptor used to access debug
2858 /// info.
2859 Elf*
2860 dwarf_elf_handle() const
2861 {return dwarf_getelf(const_cast<Dwarf*>(dwarf_debug_info()));}
2862
2863 /// Test if the debug information is in a separate ELF file wrt the
2864 /// main ELF file of the program (application or shared library) we
2865 /// are analizing.
2866 ///
2867 /// @return true if the debug information is in a separate ELF file
2868 /// compared to the main ELF file of the program (application or
2869 /// shared library) that we are looking at.
2870 bool
2871 dwarf_is_splitted() const
2872 {return dwarf_elf_handle() != elf_handle();}
2873
2874 /// Get the source of the DIE.
2875 ///
2876 /// The function returns an enumerator value saying if the DIE comes
2877 /// from the .debug_info section of the primary debug info file, the
2878 /// .debug_info section of the alternate debug info file, or the
2879 /// .debug_types section.
2880 ///
2881 /// @param die the DIE to get the source of.
2882 ///
2883 /// @return the source of the DIE if it could be determined,
2884 /// NO_DEBUG_INFO_DIE_SOURCE otherwise.
2886 get_die_source(const Dwarf_Die *die) const
2887 {
2888 die_source source = NO_DEBUG_INFO_DIE_SOURCE;
2889 ABG_ASSERT(die);
2890 ABG_ASSERT(get_die_source(*die, source));
2891 return source;
2892 }
2893
2894 /// Get the source of the DIE.
2895 ///
2896 /// The function returns an enumerator value saying if the DIE comes
2897 /// from the .debug_info section of the primary debug info file, the
2898 /// .debug_info section of the alternate debug info file, or the
2899 /// .debug_types section.
2900 ///
2901 /// @param die the DIE to get the source of.
2902 ///
2903 /// @param source out parameter. The function sets this parameter
2904 /// to the source of the DIE @p iff it returns true.
2905 ///
2906 /// @return true iff the source of the DIE could be determined and
2907 /// returned.
2908 bool
2909 get_die_source(const Dwarf_Die &die, die_source &source) const
2910 {
2911 Dwarf_Die cu_die;
2912 Dwarf_Die cu_kind;
2913 uint8_t address_size = 0, offset_size = 0;
2914 if (!dwarf_diecu(const_cast<Dwarf_Die*>(&die),
2915 &cu_die, &address_size,
2916 &offset_size))
2917 return false;
2918
2919 Dwarf_Half version = 0;
2920 Dwarf_Off abbrev_offset = 0;
2921 uint64_t type_signature = 0;
2922 Dwarf_Off type_offset = 0;
2923 if (!dwarf_cu_die(cu_die.cu, &cu_kind,
2924 &version, &abbrev_offset,
2925 &address_size, &offset_size,
2926 &type_signature, &type_offset))
2927 return false;
2928
2929 int tag = dwarf_tag(&cu_kind);
2930 ABG_ASSERT(tag);
2931
2932 if (tag == DW_TAG_compile_unit
2933 || tag == DW_TAG_partial_unit)
2934 {
2935 const Dwarf *die_dwarf = dwarf_cu_getdwarf(cu_die.cu);
2936 if (dwarf_debug_info() == die_dwarf)
2937 source = PRIMARY_DEBUG_INFO_DIE_SOURCE;
2938 else if (alternate_dwarf_debug_info() == die_dwarf)
2939 source = ALT_DEBUG_INFO_DIE_SOURCE;
2940 else
2942 }
2943 else if (tag == DW_TAG_type_unit)
2944 source = TYPE_UNIT_DIE_SOURCE;
2945 else
2946 return false;
2947
2948 return true;
2949 }
2950
2951 /// Return the correct debug info, depending on the DIE source we
2952 /// are looking at.
2953 ///
2954 /// @param source the DIE source to consider.
2955 ///
2956 /// @return the right debug info, depending on @p source.
2957 const Dwarf*
2958 dwarf_per_die_source(die_source source) const
2959 {
2960 const Dwarf *result = 0;
2961 switch(source)
2962 {
2963 case PRIMARY_DEBUG_INFO_DIE_SOURCE:
2964 case TYPE_UNIT_DIE_SOURCE:
2965 result = dwarf_debug_info();
2966 break;
2967 case ALT_DEBUG_INFO_DIE_SOURCE:
2968 result = alternate_dwarf_debug_info();
2969 break;
2970 case NO_DEBUG_INFO_DIE_SOURCE:
2971 case NUMBER_OF_DIE_SOURCES:
2973 }
2974 return result;
2975 }
2976
2977 /// Return the path to the ELF path we are reading.
2978 ///
2979 /// @return the elf path.
2980 const string&
2981 elf_path() const
2982 {return corpus_path();}
2983
2984 /// Retrieve the a DIE that corresponds to a DIE address.
2985 ///
2986 /// @param die_addr the address of the DIE to consider.
2987 ///
2988 /// @param die the output to set to the DIE that matches @p
2989 /// die_addr, iff the function returns true.
2990 ///
2991 /// @return true iff the function could retrieve the DIE which
2992 /// address is @p die_addr.
2993 bool
2994 get_die_from_addr(const void* die_addr, Dwarf_Die& die) const
2995 {
2996 Dwarf_Die *result = nullptr;
2997 void* addr = const_cast<void*>(die_addr);
2998 Dwarf* debug_info = const_cast<Dwarf*>(dwarf_debug_info());
2999 if (!debug_info || !addr)
3000 return false;
3001
3002 result = dwarf_die_addr_die(debug_info, addr, &die);
3003 if (!result)
3004 {
3005 debug_info = const_cast<Dwarf*>(alternate_dwarf_debug_info());
3006 if (!debug_info)
3007 return false;
3008
3009 result = dwarf_die_addr_die(debug_info, addr, &die);
3010 if (!result)
3011 return false;
3012 }
3013
3014 // As a sanity check, make sure we can get the tag of the
3015 // resulting DIE.
3016 int tag = dwarf_tag(&die);
3017 ABG_ASSERT(tag);
3018
3019 return result;
3020 }
3021
3022public:
3023
3024 /// Return the parent DIE for a given DIE.
3025 ///
3026 /// Note that the function build_die_parent_map() must have been
3027 /// called before this one can work. This function either succeeds or
3028 /// aborts the current process.
3029 ///
3030 /// @param rdr the DWARF reader to consider.
3031 ///
3032 /// @param die the DIE for which we want the parent.
3033 ///
3034 /// @param parent_die the output parameter set to the parent die of
3035 /// @p die. Its memory must be allocated and handled by the caller.
3036 ///
3037 /// @param where_offset the offset of the DIE where we are "logically"
3038 /// positionned at, in the DIE tree. This is useful when @p die is
3039 /// e.g, DW_TAG_partial_unit that can be included in several places in
3040 /// the DIE tree.
3041 ///
3042 /// @return true if the function could get a parent DIE, false
3043 /// otherwise.
3044 bool
3045 get_parent_die(const Dwarf_Die* die,
3046 Dwarf_Die& parent_die,
3047 void* where_addr,
3048 reader::tu_context_type_sptr& tu_ctxt) const
3049 {
3051
3052 const addr_addr_phmap_type& m = die_parent_map();
3053 addr_addr_phmap_type::const_iterator i = m.find(die->addr);
3054
3055 if (i == m.end())
3056 return false;
3057
3058 get_die_from_addr(i->second, parent_die);
3059
3060 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit)
3061 {
3062 if (where_addr == nullptr)
3063 {
3064 parent_die = *tu_ctxt->get_die();
3065 return true;
3066 }
3067 void* import_point_addr = nullptr;
3068 bool found = find_import_unit_point_before_die(parent_die.addr,
3069 where_addr, tu_ctxt,
3070 import_point_addr);
3071 if (!found)
3072 // It looks like parent_die (which comes from the alternate
3073 // debug info file) hasn't been imported into this TU. So,
3074 // Let's assume its logical parent is the DIE of the current
3075 // TU.
3076 parent_die = *tu_ctxt->get_die();
3077 else
3078 {
3079 ABG_ASSERT(import_point_addr);
3080 Dwarf_Die import_point_die;
3081 ABG_ASSERT(get_die_from_addr(import_point_addr,
3082 import_point_die));
3083 return get_parent_die(&import_point_die,
3084 parent_die, where_addr,
3085 tu_ctxt);
3086 }
3087 }
3088
3089 return true;
3090 }
3091
3092 /// Get the DIE representing the scope of a given DIE.
3093 ///
3094 /// Please note that when the DIE we are looking at has a
3095 /// DW_AT_specification or DW_AT_abstract_origin attribute, the scope
3096 /// DIE is the parent DIE of the DIE referred to by that attribute.
3097 /// In other words, this function returns the scope of the origin DIE
3098 /// of the current DIE.
3099 ///
3100 /// So, the scope DIE can be different from the parent DIE of a given
3101 /// DIE.
3102 ///
3103 /// Also note that if the current translation unit is from C, then
3104 /// this returns the global scope.
3105 ///
3106 /// @param rdr the DWARF reader to use.
3107 ///
3108 /// @param dye the DIE to consider.
3109 ///
3110 /// @param where_offset where we are logically at in the DIE stream.
3111 ///
3112 /// @param scope_die out parameter. This is set to the resulting
3113 /// scope DIE iff the function returns true.
3114 ///
3115 /// @return true iff the scope was found and returned in the @p
3116 /// scope_die parameter.
3117 bool
3118 get_scope_die(const Dwarf_Die* dye,
3119 void* where_addr,
3120 reader::tu_context_type_sptr& tu_ctxt,
3121 Dwarf_Die& scope_die) const
3122 {
3123 Dwarf_Die origin_die_mem;
3124 Dwarf_Die *die = &origin_die_mem;
3125 if (!die_origin_die(dye, origin_die_mem))
3126 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
3127
3128 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
3129 get_die_language(die, die_lang);
3130 if (is_c_language(die_lang) || die_parent_map().empty())
3131 {
3132 ABG_ASSERT(dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member);
3133 return get_translation_unit_die_for_die(die, scope_die);
3134 }
3135
3136 if (!get_parent_die(die, scope_die, where_addr, tu_ctxt))
3137 return false;
3138
3139 if (dwarf_tag(&scope_die) == DW_TAG_array_type)
3140 // The scope DIE is for an array type. Let's return the scope of
3141 // the array.
3142 return get_scope_die(&scope_die, where_addr, tu_ctxt, scope_die);
3143
3144 return true;
3145 }
3146
3147 /// Get the point where a DW_AT_import DIE is used to import a given
3148 /// (unit) DIE, between two DIEs.
3149 ///
3150 /// @param rdr the dwarf reader to consider.
3151 ///
3152 /// @param partial_unit_offset the imported unit for which we want to
3153 /// know the insertion point. This is usually a partial unit (with
3154 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
3155 /// so.
3156 ///
3157 /// @param first_die_offset the offset of the DIE from which this
3158 /// function starts looking for the import point of
3159 /// @partial_unit_offset. Note that this offset is excluded from the
3160 /// set of potential solutions.
3161 ///
3162 /// @param first_die_cu_offset the offset of the (compilation) unit
3163 /// that @p first_die_cu_offset belongs to.
3164 ///
3165 /// @param source where the DIE of first_die_cu_offset unit comes
3166 /// from.
3167 ///
3168 /// @param last_die_offset the offset of the last DIE of the up to
3169 /// which this function looks for the import point of @p
3170 /// partial_unit_offset. Note that this offset is excluded from the
3171 /// set of potential solutions.
3172 ///
3173 /// @param imported_point_offset. The resulting
3174 /// imported_point_offset. Note that if the imported DIE @p
3175 /// partial_unit_offset is not found between @p first_die_offset and
3176 /// @p last_die_offset, this parameter is left untouched by this
3177 /// function.
3178 ///
3179 /// @return true iff an imported unit is found between @p
3180 /// first_die_offset and @p last_die_offset.
3181 bool
3182 find_import_unit_point_between_dies(void* partial_unit_addr,
3183 void* first_die_addr,
3184 void* last_die_addr,
3185 void*& imported_point_addr) const
3186 {
3188 tu_die_imported_unit_points_map();
3189
3190 Dwarf_Die first_die, first_die_cu;
3191 ABG_ASSERT(get_die_from_addr(first_die_addr, first_die));
3192 ABG_ASSERT(dwarf_diecu(&first_die, &first_die_cu, 0, 0));
3193 void *first_die_cu_addr = first_die_cu.addr;
3194
3195 tu_die_imported_unit_points_map_type::const_iterator iter =
3196 m.find(first_die_cu_addr);
3197
3198 ABG_ASSERT(iter != m.end());
3199
3200 const imported_unit_points_type& imported_unit_points = iter->second;
3201 if (imported_unit_points.empty())
3202 return false;
3203
3204 imported_unit_points_type::const_iterator b = imported_unit_points.begin();
3205 imported_unit_points_type::const_iterator e = imported_unit_points.end();
3206
3207 find_lower_bound_in_imported_unit_points(imported_unit_points,
3208 first_die_addr,
3209 b);
3210
3211 if (last_die_addr != nullptr)
3212 find_lower_bound_in_imported_unit_points(imported_unit_points,
3213 last_die_addr,
3214 e);
3215
3216 if (e != imported_unit_points.end())
3217 {
3218 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
3219 if (i->imported_unit_die_addr == partial_unit_addr)
3220 {
3221 imported_point_addr = i->addr_of_import ;
3222 return true;
3223 }
3224
3225 for (imported_unit_points_type::const_iterator i = e; i >= b; --i)
3226 {
3227 if (find_import_unit_point_between_dies(partial_unit_addr,
3228 i->imported_unit_child_addr,
3229 /*last_die_addr*/nullptr,
3230 imported_point_addr))
3231 return true;
3232 }
3233 }
3234 else
3235 {
3236 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
3237 if (i->imported_unit_die_addr == partial_unit_addr)
3238 {
3239 imported_point_addr = i->addr_of_import ;
3240 return true;
3241 }
3242
3243 for (imported_unit_points_type::const_iterator i = b; i != e; ++i)
3244 {
3245 if (find_import_unit_point_between_dies(partial_unit_addr,
3246 i->imported_unit_child_addr,
3247 /*last_die_addr*/nullptr,
3248 imported_point_addr))
3249 return true;
3250 }
3251 }
3252
3253 return false;
3254 }
3255
3256 /// In the current translation unit, get the last point where a
3257 /// DW_AT_import DIE is used to import a given (unit) DIE, before a
3258 /// given DIE is found. That given DIE is called the limit DIE.
3259 ///
3260 /// Said otherwise, this function returns the last import point of a
3261 /// unit, before a limit.
3262 ///
3263 /// @param rdr the dwarf reader to consider.
3264 ///
3265 /// @param partial_unit_offset the imported unit for which we want to
3266 /// know the insertion point of. This is usually a partial unit (with
3267 /// tag DW_TAG_partial_unit) but it does not necessarily have to be
3268 /// so.
3269 ///
3270 /// @param where_offset the offset of the limit DIE.
3271 ///
3272 /// @param imported_point_offset. The resulting imported_point_offset.
3273 /// Note that if the imported DIE @p partial_unit_offset is not found
3274 /// before @p die_offset, this is set to the last @p
3275 /// partial_unit_offset found under @p parent_die.
3276 ///
3277 /// @return true iff an imported unit is found before @p die_offset.
3278 /// Note that if an imported unit is found after @p die_offset then @p
3279 /// imported_point_offset is set and the function return false.
3280 bool
3281 find_import_unit_point_before_die(void* partial_unit_addr,
3282 void* where_addr,
3283 reader::tu_context_type_sptr& tu_ctxt,
3284 void*& imported_point_addr) const
3285 {
3286 void* import_point_addr = nullptr;
3287 Dwarf_Die first_die_of_tu;
3288
3289 if (dwarf_child(const_cast<Dwarf_Die*>(tu_ctxt->get_die()),
3290 &first_die_of_tu) != 0)
3291 return false;
3292
3293 if (find_import_unit_point_between_dies(partial_unit_addr,
3294 first_die_of_tu.addr,
3295 where_addr, import_point_addr))
3296 {
3297 imported_point_addr = import_point_addr;
3298 return true;
3299 }
3300
3301 if (import_point_addr)
3302 {
3303 imported_point_addr = import_point_addr;
3304 return true;
3305 }
3306
3307 return false;
3308 }
3309
3310 /// Add an entry to the relevant die->decl map if and only if no
3311 /// die->decl already existed for the given die.
3312 ///
3313 /// @param die the DIE to add the the map.
3314 ///
3315 /// @param decl the decl to consider.
3316 ///
3317 /// @return the decl that is associated with @p die. If a
3318 /// pre-existing declaration was already associated with @p die,
3319 /// then return it, otherwise, return the new @p decl.
3320 decl_base_sptr
3321 maybe_associate_die_to_decl(Dwarf_Die* die, decl_base_sptr decl)
3322 {
3323 if (!die ||!decl)
3324 return nullptr;
3325
3326 std::lock_guard<recursive_mutex> lock(die_artefact_maps_mutex_);
3327 die_artefact_map_type& m = decl_die_artefact_maps();
3328 void* die_addr = die->addr;
3329 auto it = m.find(die_addr);
3330 if (it != m.end())
3331 return is_decl(it->second);
3332 m[die_addr] = decl;
3333 return decl;
3334 }
3335
3336 /// Lookup the decl for a given DIE.
3337 ///
3338 /// The returned decl which of the DIE that has the exact address @p
3339 /// die_addr.
3340 ///
3341 /// @param die_addr the address of the DIE to consider.
3342 ///
3343 ///
3344 /// @return the resulting decl, or null if no decl is associated to
3345 /// the DIE represented by @p die_addr.
3346 decl_base_sptr
3347 lookup_decl_from_die_addr(void* die_addr)
3348 {
3349 decl_base_sptr result =
3350 is_decl(lookup_artifact_from_die_addr(die_addr, /*die_as_type=*/false));
3351
3352 return result;
3353 }
3354
3355 /// Get the qualified name of a given DIE.
3356 ///
3357 /// If the name of the DIE was already computed before just return
3358 /// that name from a cache. Otherwise, build the name, cache it and
3359 /// return it.
3360 ///
3361 /// @param die the DIE to consider.
3362 ///
3363 /// @param where_addr where in the DIE stream we logically are.
3364 ///
3365 /// @param guard the set of DIE addresses of the stack of DIEs
3366 /// involved in the construction of the qualified name of the type.
3367 /// This set is used to detect (and avoid) cycles in the stack of
3368 /// DIEs that is going to be walked to compute the qualified type
3369 /// name.
3370 ///
3371 /// @return the interned string representing the qualified name of
3372 /// @p die.
3374 get_die_qualified_name(Dwarf_Die *die, void* where_addr,
3375 reader::tu_context_type_sptr& tu_ctxt,
3376 unordered_set<void*>& guard) const
3377 {
3378 ABG_ASSERT(die);
3379 const die_istring_map_type& map = die_qualified_name_maps();
3380
3381 void* die_addr = die->addr;
3382 die_istring_map_type::const_iterator i = map.find(die_addr);
3383
3384 if (i == map.end())
3385 {
3386 reader& rdr = *const_cast<reader*>(this);
3387 string qualified_name = die_qualified_name(rdr, die, where_addr,
3388 tu_ctxt, guard);
3389 interned_string istr = env().intern(qualified_name);
3390 set_die_qualified_name(die_addr, istr);
3391 return istr;
3392 }
3393
3394 return i->second;
3395 }
3396
3397 /// Get the qualified name of a given DIE which is considered to be
3398 /// the DIE for a type.
3399 ///
3400 /// For instance, for a DW_TAG_subprogram DIE, this function
3401 /// computes the name of the function *type* that corresponds to the
3402 /// function.
3403 ///
3404 /// If the name of the DIE was already computed before just return
3405 /// that name from a cache. Otherwise, build the name, cache it and
3406 /// return it.
3407 ///
3408 /// @param die the DIE to consider.
3409 ///
3410 /// @param where_offset where in the DIE stream we logically are.
3411 ///
3412 /// @param guard the set of DIE addresses of the stack of DIEs
3413 /// involved in the construction of the qualified name of the type.
3414 /// This set is used to detect (and avoid) cycles in the stack of
3415 /// DIEs that is going to be walked to compute the qualified type
3416 /// name.
3417 ///
3418 /// @return the interned string representing the qualified name of
3419 /// @p die.
3421 get_die_qualified_type_name(const Dwarf_Die *die, void* where_addr,
3422 reader::tu_context_type_sptr& tu_ctxt,
3423 unordered_set<void*>& guard) const
3424 {
3425 ABG_ASSERT(die);
3426
3427 // The name of the translation unit die is "".
3428 if (die == tu_ctxt->get_die())
3429 return env().intern("");
3430
3431 const die_istring_map_type& map = die_qualified_name_maps();
3432 die_istring_map_type::const_iterator i = map.find(die->addr);
3433
3434 if (i == map.end())
3435 {
3436 reader& rdr = *const_cast<reader*>(this);
3437 string qualified_name;
3438 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
3439 if ((tag == DW_TAG_structure_type
3440 || tag == DW_TAG_class_type
3441 || tag == DW_TAG_union_type)
3442 && die_is_anonymous(die))
3443 qualified_name =
3444 die_class_or_enum_flat_representation(*this, die, /*indent=*/"",
3445 /*one_line=*/true,
3446 /*qualified_name=*/false,
3447 where_addr, tu_ctxt, guard);
3448 else
3449 qualified_name = die_qualified_type_name(rdr, die, where_addr,
3450 tu_ctxt, guard);
3451
3452 interned_string istr = env().intern(qualified_name);
3453 set_die_qualified_name(die->addr, istr);
3454 return istr;
3455 }
3456
3457 return i->second;
3458 }
3459
3460 /// Get the pretty representation of a DIE.
3461 ///
3462 /// Once the pretty representation is computed, it's stored in a
3463 /// cache. Subsequent invocations of this function on the same DIE
3464 /// will yield the cached name.
3465 ///
3466 /// @param die the DIE to consider.
3467 ///
3468 /// @param where_offset where in the DIE stream we logically are.
3469 ///
3470 /// @param guard the set of DIE addresses of the stack of DIEs
3471 /// involved in the construction of the pretty representation of the
3472 /// type. This set is used to detect (and avoid) cycles in the
3473 /// stack of DIEs that is going to be walked to compute the
3474 /// pretty representation.
3475 ///
3476 /// @return the interned_string that represents the pretty
3477 /// representation.
3479 get_die_pretty_representation(const Dwarf_Die *die, void* where_addr,
3480 reader::tu_context_type_sptr& tu_ctxt,
3481 unordered_set<void*>& guard) const
3482 {
3483 ABG_ASSERT(die);
3484
3485 const die_istring_map_type& map = die_pretty_repr_maps();
3486 lock_guard<recursive_mutex> lock(die_pretty_repr_maps_mutex_);
3487 die_istring_map_type::const_iterator i = map.find(die->addr);
3488
3489 if (i == map.end())
3490 {
3491 reader& rdr = *const_cast<reader*>(this);
3492 string pretty_representation =
3493 die_pretty_print(rdr, die, where_addr, tu_ctxt, guard);
3494 interned_string istr = env().intern(pretty_representation);
3495 set_die_pretty_repr(die->addr, istr);
3496 return istr;
3497 }
3498
3499 return i->second;
3500 }
3501
3502 /// Get the pretty representation of a DIE.
3503 ///
3504 /// Once the pretty representation is computed, it's stored in a
3505 /// cache. Subsequent invocations of this function on the same DIE
3506 /// will yield the cached name.
3507 ///
3508 /// @param die the DIE to consider.
3509 ///
3510 /// @param where_offset where in the DIE stream we logically are.
3511 ///
3512 /// @return the interned_string that represents the pretty
3513 /// representation.
3515 get_die_pretty_representation(const Dwarf_Die *die, void* where_offset,
3516 reader::tu_context_type_sptr& tu_ctxt) const
3517 {
3518 unordered_set<void*> guard;
3519 return get_die_pretty_representation(die, where_offset, tu_ctxt, guard);
3520 }
3521
3522 /// Lookup the artifact that was built to represent a type that has
3523 /// the same pretty representation as the type denoted by a given
3524 /// DIE.
3525 ///
3526 /// Note that the DIE must have previously been associated with the
3527 /// artifact using the functions maybe_associate_die_to_decl or
3528 /// maybe_associate_die_to_type.
3529 ///
3530 /// Also, note that the scope of the lookup is the current ABI
3531 /// corpus.
3532 ///
3533 /// @param die the DIE to consider.
3534 ///
3535 /// @param where_offset where in the DIE stream we logically are.
3536 ///
3537 /// @return the type artifact found.
3539 lookup_type_artifact_from_die(Dwarf_Die *die) const
3540 {
3541 type_or_decl_base_sptr artifact =
3542 lookup_artifact_from_die(die, /*type_as_die=*/true);
3543 if (function_decl_sptr fn = is_function_decl(artifact))
3544 return fn->get_type();
3545 return artifact;
3546 }
3547
3548 /// Check if the scope of a given DIE has been associated with a
3549 /// type artifact.
3550 ///
3551 /// This function looks up the scope DIE of a given DIE and checks whether
3552 /// that scope DIE is a type that has already been associated with a type
3553 /// artifact.
3554 ///
3555 /// @param die the DIE whose scope we want to check.
3556 ///
3557 /// @param where_addr the address of the point in the binary where the
3558 /// lookup is being performed.
3559 ///
3560 /// @param tu_ctxt the translation unit context to use for the lookup.
3561 ///
3562 /// @return true if the scope of @p die is a type DIE that has
3563 /// already been associated with a type artifact, false otherwise.
3564 bool
3565 has_scope_of_die_been_associated(const Dwarf_Die* die,
3566 void* where_addr,
3567 reader::tu_context_type_sptr& tu_ctxt)
3568 {
3569 Dwarf_Die scope;
3570
3571 if (get_scope_die(die, where_addr, tu_ctxt, scope))
3572 {
3573 if (die_is_type(&scope))
3574 if (lookup_type_artifact_from_die(&scope))
3575 return true;
3576 }
3577 return false;
3578 }
3579
3580 /// Lookup the artifact that was built to represent a type or a
3581 /// declaration that has the same pretty representation as the type
3582 /// denoted by a given DIE.
3583 ///
3584 /// Note that the DIE must have previously been associated with the
3585 /// artifact using the functions maybe_associate_die_to_decl or
3586 /// maybe_associate_die_to_type.
3587 ///
3588 /// Also, note that the scope of the lookup is the current ABI
3589 /// corpus.
3590 ///
3591 /// @param die the DIE to consider.
3592 ///
3593 /// @param where_offset where in the DIE stream we logically are.
3594 ///
3595 /// @param die_as_type if true, it means the DIE is to be considered
3596 /// as a type.
3597 ///
3598 /// @return the artifact found.
3600 lookup_artifact_from_die(const Dwarf_Die *die, bool die_as_type = false) const
3601 {
3602 lock_guard<recursive_mutex> lock(die_artefact_maps_mutex_);
3603 const die_artefact_map_type& m =
3604 die_as_type ? type_die_artefact_maps() : decl_die_artefact_maps();
3605
3606 die_artefact_map_type::const_iterator i = m.find(die->addr);
3607
3608 if (i == m.end())
3609 return type_or_decl_base_sptr();
3610 return i->second;
3611 }
3612
3613 /// Lookup the artifact that was built to represent a type or a
3614 /// declaration that has the same pretty representation as the type
3615 /// denoted by the offset of a given DIE.
3616 ///
3617 /// Note that the DIE must have previously been associated with the
3618 /// artifact using either maybe_associate_die_to_decl or
3619 /// maybe_associate_die_to_type.
3620 ///
3621 /// Also, note that the scope of the lookup is the current ABI
3622 /// corpus.
3623 ///
3624 /// @param die the DIE to consider.
3625 ///
3626 /// @param where_offset where in the DIE stream we logically are.
3627 ///
3628 /// @param die_as_type if true, it means the DIE is to be considered
3629 /// as a type.
3630 ///
3631 /// @return the artifact found.
3633 lookup_artifact_from_die_addr(void* die_addr,
3634 bool die_as_type = false) const
3635 {
3636 lock_guard<recursive_mutex> lock(die_artefact_maps_mutex_);
3637 const die_artefact_map_type& m =
3638 die_as_type ? type_die_artefact_maps() : decl_die_artefact_maps();
3639
3640 die_artefact_map_type::const_iterator i = m.find(die_addr);
3641 if (i == m.end())
3642 return type_or_decl_base_sptr();
3643 return i->second;
3644 }
3645
3646 /// Check if we can assume the One Definition Rule[1] to be relevant
3647 /// for the current translation unit.
3648 ///
3649 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3650 ///
3651 /// At the moment this returns true if the current translation unit
3652 /// is in C++ language. In that case, it's relevant to assume that
3653 /// we use optimizations based on the ODR.
3654 bool
3655 odr_is_relevant(tu_context_type& ctxt) const
3656 {return odr_is_relevant(ctxt.get_tu()->get_language());}
3657
3658 bool
3659 odr_is_relevant(tu_context_type_sptr& ctxt) const
3660 {return odr_is_relevant(*ctxt);}
3661
3662 /// Check if we can assume the One Definition Rule[1] to be relevant
3663 /// for a given language.
3664 ///
3665 /// [1]: https://en.wikipedia.org/wiki/One_Definition_Rule
3666 ///
3667 /// At the moment this returns true if the language considered
3668 /// is C++, Java or Ada.
3669 bool
3671 {
3672 return (is_cplus_plus_language(l)
3673 || is_java_language(l)
3674 || is_ada_language(l));
3675 }
3676
3677 /// Check if we can assume the One Definition Rule to be relevant
3678 /// for a given DIE.
3679 ///
3680 /// @param die the DIE to consider.
3681 ///
3682 /// @return true if the ODR is relevant for @p die.
3683 bool
3684 odr_is_relevant(const Dwarf_Die *die, tu_context_type_sptr& tu_ctxt) const
3685 {
3687 if (!get_die_language(die, lang))
3688 return odr_is_relevant(tu_ctxt);
3689
3690 return odr_is_relevant(lang);
3691 }
3692
3693 /// Getter for the maps set that associates a decl DIE address to an
3694 /// artifact.
3695 ///
3696 /// @return the maps set that associates a decl DIE address to an
3697 /// artifact.
3699 decl_die_artefact_maps()
3700 {return decl_die_artefact_maps_;}
3701
3702 /// Getter for the maps set that associates a decl DIE address to an
3703 /// artifact.
3704 ///
3705 /// @return the maps set that associates a decl DIE address to an
3706 /// artifact.
3708 decl_die_artefact_maps() const
3709 {return decl_die_artefact_maps_;}
3710
3711 /// Getter for the maps set that associates a type DIE address to an
3712 /// artifact.
3713 ///
3714 /// @return the maps set that associates a type DIE address to an
3715 /// artifact.
3717 type_die_artefact_maps()
3718 {return type_die_artefact_maps_;}
3719
3720 /// Getter for the maps set that associates a type DIE address to an
3721 /// artifact.
3722 ///
3723 /// @return the maps set that associates a type DIE address to an
3724 /// artifact.
3726 type_die_artefact_maps() const
3727 {return type_die_artefact_maps_;}
3728
3729 /// Associate a DIE (representing a type) to the type that it
3730 /// represents, if and only if no IR type node was already
3731 /// associated the DIE.
3732 ///
3733 /// If another IR type node, was previously associated with this new
3734 /// DIE, then the NEW type node is NOT associated. The previous
3735 /// type node that was already associated is returned.
3736 ///
3737 /// @param die the DIE to consider.
3738 ///
3739 /// @param type the type to associate the DIE to.
3740 ///
3741 /// @param where_offset where in the DIE stream we logically are.
3742 ///
3743 /// @return the IR type node that is associated with @p die. If
3744 /// there was an IR type node that was already associated with @p
3745 /// die, then it's returned. Otherwise @p type is returned once
3746 /// it's associated.
3747 type_base_sptr
3748 maybe_associate_die_to_type(const Dwarf_Die* die, const type_base_sptr type)
3749 {
3750 if (!type || !die)
3751 return nullptr;
3752
3753 std::lock_guard<recursive_mutex> lock(die_artefact_maps_mutex_);
3754 die_artefact_map_type& m = type_die_artefact_maps();
3755 auto it = m.find(die->addr);
3756 if (it != m.end())
3757 return is_type(it->second);
3758 m[die->addr] = type;
3759 return type;
3760 }
3761
3762 /// Getter for a map that associates a die with a function decl
3763 /// which has a linkage name but no elf symbol yet.
3764 ///
3765 /// This is to fixup function decls with linkage names, but with no
3766 /// link to their underlying elf symbol. There are some DIEs like
3767 /// that in DWARF sometimes, especially when the compiler optimizes
3768 /// stuff aggressively.
3770 die_function_decl_with_no_symbol_map()
3771 {return die_function_with_no_symbol_map_;}
3772
3773 void
3774 record_a_fn_decl_with_no_symbol(const Dwarf_Die* die,
3775 const function_decl_sptr& fn_decl)
3776 {
3777 if (!die || !fn_decl)
3778 return;
3779
3780 std::lock_guard<recursive_mutex> lock(mutex_);
3781 auto i = die_function_decl_with_no_symbol_map().find(die->addr);
3782 if (i == die_function_decl_with_no_symbol_map().end())
3783 die_function_decl_with_no_symbol_map()[die->addr] = fn_decl;
3784 }
3785
3786 /// Sometimes, a data member die can erroneously have an empty name as
3787 /// a result of a bug of the DWARF emitter.
3788 ///
3789 /// This is what happens in
3790 /// https://sourceware.org/bugzilla/show_bug.cgi?id=29934.
3791 ///
3792 /// In that case, this function constructs an artificial name for that
3793 /// data member. The pattern of the name is as follows:
3794 ///
3795 /// "unnamed-@-<location>".
3796 ///
3797 ///location is either the value of the data member location of the
3798 ///data member if it has one or concatenation of its source location
3799 ///if it has none. If no location can be calculated then the function
3800 ///returns the empty string.
3801 string
3802 build_name_for_buggy_anonymous_data_member(Dwarf_Die *die,
3803 tu_context_type_sptr& tu_ctxt)
3804 {
3805 string result;
3806 // Let's make sure we are looking at a data member with an empty
3807 // name ...
3808 if (!die
3809 || dwarf_tag(die) != DW_TAG_member
3810 || !die_name(die).empty())
3811 return result;
3812
3813 // ... and yet, it's not an anonymous data member (aka unnamed
3814 // field) as described in
3815 // https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
3816 if (die_is_anonymous_data_member(die))
3817 return result;
3818
3819 // If we come this far, it means we are looking at a buggy data
3820 // member with no name. Let's build a name for it so that it can be
3821 // addressed.
3822 int64_t offset_in_bits = 0;
3823 bool has_offset = die_member_offset(*this, die, offset_in_bits);
3824 location loc;
3825 if (!has_offset)
3826 {
3827 loc = die_location(die, tu_ctxt);
3828 if (!loc)
3829 return result;
3830 }
3831
3832 std::ostringstream o;
3833 o << "unnamed-dm-@-";
3834 if (has_offset)
3835 o << "offset-" << offset_in_bits << "bits";
3836 else
3837 o << "loc-" << loc.expand();
3838
3839 return o.str();
3840 }
3841
3842 /// Getter for the map of declaration-only classes that are to be
3843 /// resolved to their definition classes by the end of the corpus
3844 /// loading.
3845 ///
3846 /// @return a map of string -> vector of classes where the key is
3847 /// the fully qualified name of the class and the value is the
3848 /// vector of declaration-only class.
3850 declaration_only_classes() const
3851 {return decl_only_classes_map_;}
3852
3853 /// Getter for the map of declaration-only classes that are to be
3854 /// resolved to their definition classes by the end of the corpus
3855 /// loading.
3856 ///
3857 /// @return a map of string -> vector of classes where the key is
3858 /// the fully qualified name of the class and the value is the
3859 /// vector of declaration-only class.
3861 declaration_only_classes()
3862 {return decl_only_classes_map_;}
3863
3864 class_or_union_sptr
3865 get_a_declaration_only_class(const string& qualified_name)
3866 {
3867 std::lock_guard<mutex> lock(decl_only_classes_map_mutex_);
3868
3869 auto i = declaration_only_classes().find(qualified_name);
3870 if (i != declaration_only_classes().end())
3871 return i->second.back();
3872
3873 return nullptr;
3874 }
3875
3876 /// If a given artifact is a class, union or enum that is
3877 /// declaration-only, then stash it on the side so that at the end
3878 /// of the construction of the IR for the ABI corpus, we can resolve
3879 /// that declaration to its definition.
3880 ///
3881 /// @parameter t the ABI artifact to consider.
3882 void
3883 maybe_schedule_decl_only_type_for_resolution(const type_or_decl_base_sptr& t)
3884 {
3885 if (class_or_union_sptr cou = is_class_or_union_type(t))
3886 maybe_schedule_declaration_only_class_for_resolution(cou);
3887 else if (enum_type_decl_sptr e = is_enum_type(t))
3888 maybe_schedule_declaration_only_enum_for_resolution(e);
3889 }
3890
3891 /// If a given class is a declaration-only class then stash it on
3892 /// the side so that at the end of the corpus reading we can resolve
3893 /// it to its definition.
3894 ///
3895 /// @param klass the class to consider.
3896 void
3897 maybe_schedule_declaration_only_class_for_resolution(const class_or_union_sptr& cou)
3898 {
3899 if (cou->get_is_declaration_only()
3900 && cou->get_definition_of_declaration() == 0
3901 // Make sure the class is not anonymous. Anonymous classes
3902 // are usually later named by a typedef. At that time, after
3903 // being named by a typedef, this method is going to be called
3904 // with the class being named by the typedef.
3905 && !cou->get_qualified_name().empty())
3906 {
3907 string qn = cou->get_qualified_name();
3908
3909 std::lock_guard<mutex> lock(decl_only_classes_map_mutex_);
3910 string_classes_or_unions_map::iterator record =
3911 declaration_only_classes().find(qn);
3912 if (record == declaration_only_classes().end())
3913 declaration_only_classes()[qn].push_back(cou);
3914 else
3915 record->second.push_back(cou);
3916 }
3917 }
3918
3919 /// Test if a given declaration-only class has been scheduled for
3920 /// resolution to a defined class.
3921 ///
3922 /// @param klass the class to consider for the test.
3923 ///
3924 /// @return true iff @p klass is a declaration-only class and if
3925 /// it's been scheduled for resolution to a defined class.
3926 bool
3927 is_decl_only_class_scheduled_for_resolution(const class_or_union_sptr& cou)
3928 {
3929 if (cou->get_is_declaration_only())
3930 {
3931 std::lock_guard<mutex> lock(decl_only_classes_map_mutex_);
3932 return ((declaration_only_classes().find(cou->get_qualified_name())
3933 != declaration_only_classes().end())
3934 || (declaration_only_classes().find(cou->get_name())
3935 != declaration_only_classes().end()));
3936 }
3937
3938 return false;
3939 }
3940
3941 /// Compare two ABI artifacts in a context which canonicalization
3942 /// has not be done yet.
3943 ///
3944 /// Please note that this should only be called on IR nodes that
3945 /// belong to the same binary.
3946 ///
3947 /// @param l the left-hand-side operand of the comparison
3948 ///
3949 /// @param r the right-hand-side operand of the comparison.
3950 ///
3951 /// @return true if @p l equals @p r.
3952 bool
3953 compare_before_canonicalisation(const type_or_decl_base_sptr &l,
3954 const type_or_decl_base_sptr &r)
3955 {
3956 if (!l || !r)
3957 return !!l == !!r;
3958
3959 const environment& e = l->get_environment();
3960 bool canonicalization_is_done = false;
3961
3962 auto l_abi = l->get_corpus();
3963 auto r_abi = r->get_corpus();
3964
3965 if ((l_abi && l_abi->priv_->types_are_canonicalized())
3966 || (r_abi && r_abi->priv_->types_are_canonicalized()))
3967 canonicalization_is_done = true;
3968
3969 ABG_ASSERT(!canonicalization_is_done);
3970
3971 if (is_decl(l) && is_decl(r)
3972 && l->kind() == r->kind()
3973 && ((l_abi && r_abi
3974 && (l_abi == r_abi))
3975 ||(l->get_translation_unit()
3976 && r->get_translation_unit()
3977 && l->get_translation_unit() == r->get_translation_unit())))
3978 {
3979 // Fast path optimization. If the two types are declared at
3980 // the same location (in the same binary) then it very likely
3981 // means the two types are equal.
3982 //
3983 // We really need every bit of optimization here because
3984 // otherwise, comparing types before canonicalization can take
3985 // forever.*
3986 decl_base *ld = is_decl(l.get());
3987 decl_base *rd = is_decl(r.get());
3988 ABG_ASSERT(ld && rd);
3989 if (ld->get_qualified_name() != rd->get_qualified_name())
3990 return false;
3991
3992 location ll = ld->get_location(), rl = rd->get_location();
3993 if (ll && rl)
3994 {
3995 string l1 = ll.expand();
3996 string l2 = rl.expand();
3997 if (l1 == l2)
3998 return true;
3999 }
4000 }
4001
4002 e.priv_->allow_type_comparison_results_caching(true);
4005 bool equal = l == r;
4007 e.priv_->clear_type_comparison_results_cache();
4008 e.priv_->allow_type_comparison_results_caching(false);
4009 return equal;
4010 }
4011
4012 /// Walk the declaration-only classes that have been found during
4013 /// the building of the corpus and resolve them to their definitions.
4014 void
4015 resolve_declaration_only_classes()
4016 {
4017 std::lock_guard<mutex> lock(decl_only_classes_map_mutex_);
4018 vector<string> resolved_classes;
4019 for (string_classes_or_unions_map::iterator i =
4020 declaration_only_classes().begin();
4021 i != declaration_only_classes().end();
4022 ++i)
4023 {
4024 bool to_resolve = false;
4025 for (classes_or_unions_type::iterator j = i->second.begin();
4026 j != i->second.end();
4027 ++j)
4028 if ((*j)->get_is_declaration_only()
4029 && ((*j)->get_definition_of_declaration() == 0))
4030 to_resolve = true;
4031
4032 if (!to_resolve)
4033 {
4034 resolved_classes.push_back(i->first);
4035 continue;
4036 }
4037
4038 // Now, for each decl-only class that have the current name
4039 // 'i->first', let's try to poke at the fully defined class
4040 // that is defined in the same translation unit as the
4041 // declaration.
4042 //
4043 // If we find one class (defined in the TU of the declaration)
4044 // that defines the declaration, then the declaration can be
4045 // resolved to that class.
4046 //
4047 // If no defining class is found in the TU of the declaration,
4048 // then there are possibly three cases to consider:
4049 //
4050 // 1/ There is exactly one class that defines the
4051 // declaration and that class is defined in another TU. In
4052 // this case, the declaration is resolved to that
4053 // definition.
4054 //
4055 // 2/ There are more than one class that define that
4056 // declaration and none of them is defined in the TU of the
4057 // declaration. If those classes are all different, then
4058 // the declaration is left unresolved.
4059 //
4060 // 3/ No class defines the declaration. In this case, the
4061 // declaration is left unresoved.
4062
4063 // So get the classes that might define the current
4064 // declarations which name is i->first.
4065 const type_base_wptrs_type *classes =
4066 lookup_class_types(i->first, *corpus());
4067 if (!classes)
4068 classes = lookup_union_types(i->first, *corpus());
4069
4070 if (!classes)
4071 continue;
4072
4073 // This is a map that associates the translation unit path to
4074 // the class (that potentially defines the declarations that
4075 // we consider) that are defined in that translation unit. It
4076 // should stay ordered by using the TU path as key to ensure
4077 // stability of the order of classe definitions in ABIXML
4078 // output.
4079 map<string, class_or_union_sptr> per_tu_class_map;
4080 for (type_base_wptrs_type::const_iterator c = classes->begin();
4081 c != classes->end();
4082 ++c)
4083 {
4084 class_or_union_sptr klass = is_class_or_union_type(type_base_sptr(*c));
4085 ABG_ASSERT(klass);
4086
4088 if (klass->get_is_declaration_only())
4089 continue;
4090
4091 string tu_path = klass->get_translation_unit()->get_absolute_path();
4092 if (tu_path.empty())
4093 continue;
4094
4095 // Build a map that associates the translation unit path
4096 // to the class (that potentially defines the declarations
4097 // that we consider) that are defined in that translation unit.
4098 per_tu_class_map[tu_path] = klass;
4099 }
4100
4101 if (!per_tu_class_map.empty())
4102 {
4103 // Walk the declarations to resolve and resolve them
4104 // either to the definitions that are in the same TU as
4105 // the declaration, or to the definition found elsewhere,
4106 // if there is only one such definition.
4107 for (classes_or_unions_type::iterator j = i->second.begin();
4108 j != i->second.end();
4109 ++j)
4110 {
4111 if ((*j)->get_is_declaration_only()
4112 && ((*j)->get_definition_of_declaration() == 0))
4113 {
4114 string tu_path =
4115 (*j)->get_translation_unit()->get_absolute_path();
4116 map<string, class_or_union_sptr>::const_iterator e =
4117 per_tu_class_map.find(tu_path);
4118 if (e != per_tu_class_map.end())
4119 (*j)->set_definition_of_declaration(e->second);
4120 else if (per_tu_class_map.size() == 1)
4121 (*j)->set_definition_of_declaration
4122 (per_tu_class_map.begin()->second);
4123 else
4124 {
4125 // We are in case where there are more than
4126 // one definition for the declaration. Let's
4127 // see if they are all equal. If they are,
4128 // then the declaration resolves to the
4129 // definition. Otherwise, we are in the case
4130 // 3/ described above.
4131 map<string,
4132 class_or_union_sptr>::const_iterator it;
4133 class_or_union_sptr first_class =
4134 per_tu_class_map.begin()->second;
4135 bool all_class_definitions_are_equal = true;
4136 for (it = per_tu_class_map.begin();
4137 it != per_tu_class_map.end();
4138 ++it)
4139 {
4140 if (it == per_tu_class_map.begin())
4141 continue;
4142 else
4143 {
4144 if (!compare_before_canonicalisation(it->second,
4145 first_class))
4146 {
4147 all_class_definitions_are_equal = false;
4148 break;
4149 }
4150 }
4151 }
4152 if (all_class_definitions_are_equal)
4153 (*j)->set_definition_of_declaration(first_class);
4154 }
4155 }
4156 }
4157 resolved_classes.push_back(i->first);
4158 }
4159 }
4160
4161 size_t num_decl_only_classes = declaration_only_classes().size(),
4162 num_resolved = resolved_classes.size();
4163 if (show_stats())
4164 cerr << "resolved " << num_resolved
4165 << " class declarations out of "
4166 << num_decl_only_classes
4167 << "\n";
4168
4169 for (vector<string>::const_iterator i = resolved_classes.begin();
4170 i != resolved_classes.end();
4171 ++i)
4172 declaration_only_classes().erase(*i);
4173
4174 if (show_stats() && !declaration_only_classes().empty())
4175 {
4176 cerr << "Here are the "
4177 << num_decl_only_classes - num_resolved
4178 << " unresolved class declarations:\n";
4179 for (string_classes_or_unions_map::iterator i =
4180 declaration_only_classes().begin();
4181 i != declaration_only_classes().end();
4182 ++i)
4183 cerr << " " << i->first << "\n";
4184 }
4185 }
4186
4187 /// Getter for the map of declaration-only enums that are to be
4188 /// resolved to their definition enums by the end of the corpus
4189 /// loading.
4190 ///
4191 /// @return a map of string -> vector of enums where the key is
4192 /// the fully qualified name of the enum and the value is the
4193 /// vector of declaration-only enum.
4194 const string_enums_map&
4195 declaration_only_enums() const
4196 {return decl_only_enums_map_;}
4197
4198 /// Getter for the map of declaration-only enums that are to be
4199 /// resolved to their definition enums by the end of the corpus
4200 /// loading.
4201 ///
4202 /// @return a map of string -> vector of enums where the key is
4203 /// the fully qualified name of the enum and the value is the
4204 /// vector of declaration-only enum.
4206 declaration_only_enums()
4207 {return decl_only_enums_map_;}
4208
4210 get_a_declaration_only_enum(const string& qualified_name)
4211 {
4212 auto i = declaration_only_enums().find(qualified_name);
4213 if (i != declaration_only_enums().end())
4214 return i->second.back();
4215
4216 return nullptr;
4217 }
4218
4219 /// If a given enum is a declaration-only enum then stash it on
4220 /// the side so that at the end of the corpus reading we can resolve
4221 /// it to its definition.
4222 ///
4223 /// @param enom the enum to consider.
4224 void
4225 maybe_schedule_declaration_only_enum_for_resolution(const enum_type_decl_sptr& enom)
4226 {
4227 if (enom->get_is_declaration_only()
4228 && enom->get_definition_of_declaration() == 0
4229 // Make sure the enum is not anonymous. Anonymous enums are
4230 // usually later named by a typedef. At that time, after
4231 // being named by a typedef, this method is going to be called
4232 // with the enum being named by the typedef.
4233 && !enom->get_qualified_name().empty())
4234 {
4235 string qn = enom->get_qualified_name();
4236 std::lock_guard<mutex> lock(decl_only_enums_map_mutex_);
4237 string_enums_map::iterator record =
4238 declaration_only_enums().find(qn);
4239 if (record == declaration_only_enums().end())
4240 declaration_only_enums()[qn].push_back(enom);
4241 else
4242 record->second.push_back(enom);
4243 }
4244 }
4245
4246 /// Test if a given declaration-only enum has been scheduled for
4247 /// resolution to a defined enum.
4248 ///
4249 /// @param enom the enum to consider for the test.
4250 ///
4251 /// @return true iff @p enom is a declaration-only enum and if
4252 /// it's been scheduled for resolution to a defined enum.
4253 bool
4254 is_decl_only_enum_scheduled_for_resolution(enum_type_decl_sptr& enom)
4255 {
4256 if (enom->get_is_declaration_only())
4257 return (declaration_only_enums().find(enom->get_qualified_name())
4258 != declaration_only_enums().end());
4259
4260 return false;
4261 }
4262
4263 /// Walk the declaration-only enums that have been found during
4264 /// the building of the corpus and resolve them to their definitions.
4265 ///
4266 /// TODO: Do away with this function by factorizing it with
4267 /// resolve_declaration_only_classes. All declaration-only decls
4268 /// could be handled the same way as declaration-only-ness is a
4269 /// property of abigail::ir::decl_base now.
4270 void
4271 resolve_declaration_only_enums()
4272 {
4273 vector<string> resolved_enums;
4274
4275 std::lock_guard<mutex> lock(decl_only_enums_map_mutex_);
4276
4277 for (string_enums_map::iterator i =
4278 declaration_only_enums().begin();
4279 i != declaration_only_enums().end();
4280 ++i)
4281 {
4282 bool to_resolve = false;
4283 for (enums_type::iterator j = i->second.begin();
4284 j != i->second.end();
4285 ++j)
4286 if ((*j)->get_is_declaration_only()
4287 && ((*j)->get_definition_of_declaration() == 0))
4288 to_resolve = true;
4289
4290 if (!to_resolve)
4291 {
4292 resolved_enums.push_back(i->first);
4293 continue;
4294 }
4295
4296 // Now, for each decl-only enum that have the current name
4297 // 'i->first', let's try to poke at the fully defined enum
4298 // that is defined in the same translation unit as the
4299 // declaration.
4300 //
4301 // If we find one enum (defined in the TU of the declaration)
4302 // that defines the declaration, then the declaration can be
4303 // resolved to that enum.
4304 //
4305 // If no defining enum is found in the TU of the declaration,
4306 // then there are possibly three cases to consider:
4307 //
4308 // 1/ There is exactly one enum that defines the
4309 // declaration and that enum is defined in another TU. In
4310 // this case, the declaration is resolved to that
4311 // definition.
4312 //
4313 // 2/ There are more than one (different) enum that define
4314 // that declaration and none of them is defined in the TU of
4315 // the declaration. In this case, the declaration is left
4316 // unresolved.
4317 //
4318 // 3/ No enum defines the declaration. In this case, the
4319 // declaration is left unresolved.
4320
4321 // So get the enums that might define the current
4322 // declarations which name is i->first.
4323 const type_base_wptrs_type *enums =
4324 lookup_enum_types(i->first, *corpus());
4325 if (!enums)
4326 continue;
4327
4328 // This is a map that associates the translation unit path to
4329 // the enum (that potentially defines the declarations that
4330 // we consider) that are defined in that translation unit. It
4331 // should stay ordered by using the TU path as key to ensure
4332 // stability of the order of enum definitions in ABIXML
4333 // output.
4334 map<string, enum_type_decl_sptr> per_tu_enum_map;
4335 for (type_base_wptrs_type::const_iterator c = enums->begin();
4336 c != enums->end();
4337 ++c)
4338 {
4339 enum_type_decl_sptr enom = is_enum_type(type_base_sptr(*c));
4340 ABG_ASSERT(enom);
4341
4343 if (enom->get_is_declaration_only())
4344 continue;
4345
4346 string tu_path = enom->get_translation_unit()->get_absolute_path();
4347 if (tu_path.empty())
4348 continue;
4349
4350 // Build a map that associates the translation unit path
4351 // to the enum (that potentially defines the declarations
4352 // that we consider) that are defined in that translation unit.
4353 per_tu_enum_map[tu_path] = enom;
4354 }
4355
4356 if (!per_tu_enum_map.empty())
4357 {
4358 // Walk the declarations to resolve and resolve them
4359 // either to the definitions that are in the same TU as
4360 // the declaration, or to the definition found elsewhere,
4361 // if there is only one such definition.
4362 for (enums_type::iterator j = i->second.begin();
4363 j != i->second.end();
4364 ++j)
4365 {
4366 if ((*j)->get_is_declaration_only()
4367 && ((*j)->get_definition_of_declaration() == 0))
4368 {
4369 string tu_path =
4370 (*j)->get_translation_unit()->get_absolute_path();
4371 map<string, enum_type_decl_sptr>::const_iterator e =
4372 per_tu_enum_map.find(tu_path);
4373 if (e != per_tu_enum_map.end())
4374 (*j)->set_definition_of_declaration(e->second);
4375 else if (per_tu_enum_map.size() == 1)
4376 (*j)->set_definition_of_declaration
4377 (per_tu_enum_map.begin()->second);
4378 else
4379 {
4380 // We are in case where there are more than
4381 // one definition for the declaration. Let's
4382 // see if they are all equal. If they are,
4383 // then the declaration resolves to the
4384 // definition. Otherwise, we are in the case
4385 // 3/ described above.
4386 map<string,
4387 enum_type_decl_sptr>::const_iterator it;
4388 enum_type_decl_sptr first_enum =
4389 per_tu_enum_map.begin()->second;
4390 bool all_enum_definitions_are_equal = true;
4391 for (it = per_tu_enum_map.begin();
4392 it != per_tu_enum_map.end();
4393 ++it)
4394 {
4395 if (it == per_tu_enum_map.begin())
4396 continue;
4397 else
4398 {
4399 if (!compare_before_canonicalisation(it->second,
4400 first_enum))
4401 {
4402 all_enum_definitions_are_equal = false;
4403 break;
4404 }
4405 }
4406 }
4407 if (all_enum_definitions_are_equal)
4408 (*j)->set_definition_of_declaration(first_enum);
4409 }
4410 }
4411 }
4412 resolved_enums.push_back(i->first);
4413 }
4414 }
4415
4416 size_t num_decl_only_enums = declaration_only_enums().size(),
4417 num_resolved = resolved_enums.size();
4418 if (show_stats())
4419 cerr << "resolved " << num_resolved
4420 << " enum declarations out of "
4421 << num_decl_only_enums
4422 << "\n";
4423
4424 for (vector<string>::const_iterator i = resolved_enums.begin();
4425 i != resolved_enums.end();
4426 ++i)
4427 declaration_only_enums().erase(*i);
4428
4429 if (show_stats() && !declaration_only_enums().empty())
4430 {
4431 cerr << "Here are the "
4432 << num_decl_only_enums - num_resolved
4433 << " unresolved enum declarations:\n";
4434 for (string_enums_map::iterator i = declaration_only_enums().begin();
4435 i != declaration_only_enums().end();
4436 ++i)
4437 cerr << " " << i->first << "\n";
4438 }
4439 }
4440
4441 /// Test if a symbol belongs to a function of the current ABI
4442 /// corpus.
4443 ///
4444 /// This is a sub-routine of fixup_functions_with_no_symbols.
4445 ///
4446 /// @param fn the function symbol to consider.
4447 ///
4448 /// @returnt true if @p fn belongs to a function of the current ABI
4449 /// corpus.
4450 bool
4451 symbol_already_belongs_to_a_function(elf_symbol_sptr& fn)
4452 {
4453 corpus_sptr corp = corpus();
4454 if (!corp)
4455 return false;
4456
4457 interned_string id = corp->get_environment().intern(fn->get_id_string());
4458
4459 const std::unordered_set<const function_decl*> *fns = corp->lookup_functions(id);
4460 if (!fns)
4461 return false;
4462
4463 for (auto f : *fns)
4464 if (f->get_symbol())
4465 return true;
4466
4467 return false;
4468 }
4469
4470 /// Some functions described by DWARF may have their linkage name
4471 /// set, but no link to their actual underlying elf symbol. When
4472 /// these are virtual member functions, comparing the enclosing type
4473 /// against another one which has its underlying symbol properly set
4474 /// might lead to spurious type changes.
4475 ///
4476 /// If the corpus contains a symbol with the same name as the
4477 /// linkage name of the function, then set up the link between the
4478 /// function and its underlying symbol.
4479 ///
4480 /// Note that for the moment, only virtual member functions are
4481 /// fixed up like this. This is because they really are the only
4482 /// fuctions of functions that can affect types (in spurious ways).
4483 ///
4484 /// Note that it doesn't make sense for this function to be invoked
4485 /// from two different threads but it's synchronized on the
4486 /// mutex_, just in case.
4487 void
4488 fixup_functions_with_no_symbols()
4489 {
4490 corpus_sptr corp = corpus();
4491 if (!corp)
4492 return;
4493
4494 die_function_decl_map_type &fns_with_no_symbol =
4495 die_function_decl_with_no_symbol_map();
4496
4497 if (fns_with_no_symbol.empty())
4498 return;
4499
4500 if (do_log())
4501 cerr << fns_with_no_symbol.size()
4502 << " functions to fixup, potentially\n";
4503
4504 std::lock_guard<recursive_mutex> lock(mutex_);
4505
4506 for (die_function_decl_map_type::iterator i = fns_with_no_symbol.begin();
4507 i != fns_with_no_symbol.end();
4508 ++i)
4509 if (elf_symbol_sptr sym =
4510 corp->lookup_function_symbol(i->second->get_linkage_name()))
4511 {
4512 // So i->second is a virtual member function that was
4513 // previously scheduled to be set a function symbol.
4514 //
4515 // But if it appears that it now has a symbol already set,
4516 // then do not set a symbol to it again.
4517 //
4518 // Or if it appears that another virtual member function
4519 // from the current ABI Corpus, with the same linkage
4520 // (mangled) name has already been set a symbol, then do not
4521 // set a symbol to this function either. Otherwise, there
4522 // will be two virtual member functions with the same symbol
4523 // in the class and that leads to spurious hard-to-debug
4524 // change reports later down the road.
4525
4526 ABG_ASSERT(is_member_function(i->second));
4528 i->second->set_symbol(sym);
4529
4530 if (do_log() && show_stats())
4531 cerr << "fixed up '"
4532 << i->second->get_pretty_representation()
4533 << "' with symbol '"
4534 << sym->get_id_string()
4535 << "'\n";
4536 }
4537
4538 fns_with_no_symbol.clear();
4539 }
4540
4541 /// @return vectors of types created during the analysis of the
4542 /// DWARF and in the need of being canonicalized.
4543 const type_wptr_set_type&
4544 types_to_canonicalize() const
4545 {return types_to_canonicalize_;}
4546
4547 /// @return vectors of types created during the analysis of the
4548 /// DWARF and in the need of being canonicalized.
4550 types_to_canonicalize()
4551 {return types_to_canonicalize_;}
4552
4553 /// Clear the containers holding types to canonicalize.
4554 void
4555 clear_types_to_canonicalize()
4556 {
4557 types_to_canonicalize().clear();
4558 }
4559
4560 /// Types that were created but not tied to a particular DIE, must
4561 /// be scheduled for late canonicalization using this method.
4562 ///
4563 /// @param t the type to schedule for late canonicalization.
4564 void
4565 schedule_type_for_late_canonicalization(const type_base_sptr &t)
4566 {
4567 std::lock_guard<mutex> lock(types_to_canonicalize_mutex_);
4568 types_to_canonicalize().insert(t);
4569 }
4570
4571 /// Remove a type from the set of types scheduled to be canonicalized.
4572 /// @param t the type to schedule for late canonicalization.
4573 void
4574 unschedule_type_from_late_canonicalization(const type_base_sptr &t)
4575 {
4576 std::lock_guard<mutex> lock(types_to_canonicalize_mutex_);
4577 types_to_canonicalize().erase(t);
4578 }
4579
4580 /// Canonicalize types which are stored in vectors on the side.
4581 ///
4582 /// This is a sub-routine of
4583 /// reader::perform_late_type_canonicalizing().
4584 ///
4585 void
4586 canonicalize_types_scheduled()
4587 {
4588 tools_utils::timer cn_timer;
4589 if (do_log())
4590 {
4591 cerr << "DWARF Reader canonicalizing "
4592 << std::dec
4593 << types_to_canonicalize().size()
4594 << " types";
4595 corpus_sptr c = corpus();
4596 if (c)
4597 cerr << " from corpus " << corpus()->get_path() << "\n";
4598 cn_timer.start();
4599 }
4600
4601 vector<type_base_sptr> types;
4602 types.reserve(types_to_canonicalize().size());
4603
4604 for (type_base_wptr wt : types_to_canonicalize())
4605 {
4606 type_base_sptr t = wt.lock();
4607 if (t)
4608 types.push_back(t);
4609 }
4610
4611 ir::perform_type_canonicalization(types, do_log(), show_stats());
4612
4613 corpus()->priv_->types_are_canonicalized(true);
4614
4615 if (do_log())
4616 {
4617 cn_timer.stop();
4618 const environment& env = types.front()->get_environment();
4619 cerr << "DWARF Reader finished types "
4620 << "sorting, hashing & canonicalizing in: "
4621 << cn_timer
4622 << ", for "
4623 << env.priv_->get_number_of_canonical_types()
4624 << " types\n";
4625 }
4626 }
4627
4628 /// Compute the number of canonicalized and missed types in the late
4629 /// canonicalization phase.
4630 ///
4631 /// @param source where the DIEs of the canonicalized types are
4632 /// from.
4633 ///
4634 /// @param canonicalized the number of types that got canonicalized
4635 /// is added to the value already present in this parameter.
4636 ///
4637 /// @param missed the number of types scheduled for late
4638 /// canonicalization and which couldn't be canonicalized (for a
4639 /// reason) is added to the value already present in this parameter.
4640 void
4641 add_late_canonicalized_types_stats(size_t& canonicalized,
4642 size_t& missed) const
4643 {
4644 std::lock_guard<mutex> lock(types_to_canonicalize_mutex_);
4645
4646 for (type_base_wptr wt : types_to_canonicalize())
4647 {
4648 type_base_sptr t = wt.lock();
4649 if (t && t->get_canonical_type())
4650 ++canonicalized;
4651 else if (t && !t->get_canonical_type())
4652 ++missed;
4653 }
4654 }
4655
4656 // Look at the types that need to be canonicalized after the
4657 // translation unit has been constructed and canonicalize them.
4658 void
4659 perform_late_type_canonicalizing()
4660 {
4661 canonicalize_types_scheduled();
4662
4663 if (show_stats())
4664 {
4665 size_t num_canonicalized = 0, num_missed = 0, total = 0;
4666 add_late_canonicalized_types_stats(num_canonicalized,
4667 num_missed);
4668 total = num_canonicalized + num_missed;
4669 cerr << "binary: "
4670 << elf_path()
4671 << "\n";
4672 cerr << " # late canonicalized types: "
4673 << num_canonicalized;
4674 if (total)
4675 cerr << " (" << num_canonicalized * 100 / total << "%)";
4676 cerr << "\n"
4677 << " # missed canonicalization opportunities: "
4678 << num_missed;
4679 if (total)
4680 cerr << " (" << num_missed * 100 / total << "%)";
4681 cerr << "\n";
4682 }
4683
4684 }
4685
4686 const die_tu_map_type&
4687 die_tu_map() const
4688 {
4689 return die_tu_map_;
4690 }
4691
4693 die_tu_map()
4694 {
4695 return die_tu_map_;
4696 }
4697
4699 get_translation_unit_for_die(const Dwarf_Die* die)
4700 {
4701 if (!die)
4702 return nullptr;
4703
4704 Dwarf_Die cu_die;
4705 ABG_ASSERT(dwarf_diecu(const_cast<Dwarf_Die*>(die), &cu_die, 0, 0));
4706
4707 translation_unit_sptr result;
4708 result = get_translation_unit_for_tu_die(&cu_die);
4709
4710 return result;
4711 }
4712
4714 get_translation_unit_for_tu_die(const Dwarf_Die* die)
4715 {
4716 if (!die)
4717 return nullptr;
4718
4719 std::lock_guard<mutex> lock(die_tu_map_mutex_);
4720 auto i = die_tu_map().find(die->addr);
4721 if (i == die_tu_map().end())
4722 return nullptr;
4723 return i->second;
4724 }
4725
4726 void
4727 associate_tu_die_with_tu(const Dwarf_Die* die, translation_unit_sptr tu)
4728 {
4729 if (!die || !tu)
4730 return;
4731
4732 if (!get_translation_unit_for_tu_die(die))
4733 {
4734 std::lock_guard<mutex> lock(die_tu_map_mutex_);
4735 die_tu_map()[die->addr] = tu;
4736 }
4737 }
4738
4739 /// Getter for the map that associates a translation unit DIE to the
4740 /// vector of imported unit points that it contains.
4741 ///
4742 /// @param source where the DIEs are from.
4743 ///
4744 /// @return the map.
4746 tu_die_imported_unit_points_map() const
4747 {return const_cast<reader*>(this)->tu_die_imported_unit_points_map();}
4748
4749 /// Getter for the map that associates a translation unit DIE to the
4750 /// vector of imported unit points that it contains.
4751 ///
4752 /// @param source where the DIEs are from.
4753 ///
4754 /// @return the map.
4756 tu_die_imported_unit_points_map()
4757 {return tu_die_imported_unit_points_map_;}
4758
4759 /// Reset the current corpus being constructed.
4760 ///
4761 /// This actually deletes the current corpus being constructed.
4762 void
4763 reset_corpus()
4764 {corpus().reset();}
4765
4766 /// Get the map that associates each DIE to its parent DIE. This is
4767 /// for DIEs coming from the main debug info sections.
4768 ///
4769 /// @param source where the DIEs in the map come from.
4770 ///
4771 /// @return the DIE -> parent map.
4773 die_parent_map()
4774 {return die_parent_map_;}
4775
4776 /// Get the map that associates each DIE to its parent DIE. This is
4777 /// for DIEs coming from the main debug info sections.
4778 ///
4779 /// @param source where the DIEs in the map come from.
4780 ///
4781 /// @return the DIE -> parent map.
4783 die_parent_map() const
4784 {return die_parent_map_;}
4785
4787 methods_to_finish_reading() const
4788 {return methods_to_finish_reading_;}
4789
4791 methods_to_finish_reading()
4792 {return methods_to_finish_reading_;}
4793
4794 void
4795 schedule_method_to_finish_reading(Dwarf_Die& method_die,
4796 function_decl_sptr method)
4797 {
4798 lock_guard<recursive_mutex> lock(methods_to_finish_reading_mutex_);
4799 methods_to_finish_reading()[method_die.addr] = method;
4800 }
4801
4802 void
4803 finish_reading_scheduled_methods()
4804 {
4805 lock_guard<recursive_mutex> lock(methods_to_finish_reading_mutex_);
4806 for (auto& entry : methods_to_finish_reading())
4807 {
4808 Dwarf_Die die;
4809 ABG_ASSERT(get_die_from_addr(entry.first, die));
4810 auto fn = entry.second;
4811 class_or_union_sptr scope = is_class_or_union_type(fn->get_scope());
4812 ABG_ASSERT(scope);
4813 finish_member_function_reading(&die, entry.second,
4814 scope, *this);
4815 }
4816 }
4817
4818 /// Return the global scope of the current translation unit.
4819 ///
4820 /// @return the global scope of the current translation unit.
4821 const scope_decl_sptr
4822 global_scope(const tu_context_type& tu_ctxt) const
4823 {return tu_ctxt.get_tu()->get_global_scope();}
4824
4825 const scope_decl_sptr
4826 global_scope(const tu_context_type_sptr& tu_ctxt) const
4827 {return global_scope(*tu_ctxt);}
4828
4829 /// Return a scope that is nil.
4830 ///
4831 /// @return a scope that is nil.
4832 const scope_decl_sptr&
4833 nil_scope() const
4834 {return nil_scope_;}
4835
4837 current_scope(tu_context_type& ctxt)
4838 {
4839 return ctxt.current_scope();
4840 }
4841
4843 current_scope(tu_context_type_sptr& ctxt)
4844 {return current_scope(*ctxt);}
4845
4846 /// Test if a DIE represents a decl (function or variable) that has
4847 /// a symbol that is exported, whatever that means. This is
4848 /// supposed to work for Linux Kernel binaries as well.
4849 ///
4850 /// This is useful to limit the amount of DIEs taken into account to
4851 /// the strict limit of what an ABI actually means. Limiting the
4852 /// volume of DIEs analyzed this way is an important optimization to
4853 /// keep big binaries "manageable" by libabigail.
4854 ///
4855 /// @param DIE the die to consider.
4856 bool
4857 is_decl_die_with_exported_symbol(const Dwarf_Die *die) const
4858 {
4859 if (!die || !die_is_decl(die))
4860 return false;
4861
4862 bool result = false, address_found = false, symbol_is_exported = false;;
4863 Dwarf_Addr decl_symbol_address = 0;
4864
4865 if (die_is_variable_decl(die))
4866 {
4867 if ((address_found = get_variable_address(die, decl_symbol_address)))
4868 symbol_is_exported =
4869 !!variable_symbol_is_exported(decl_symbol_address);
4870 }
4871 else if (die_is_function_decl(die))
4872 {
4873 if ((address_found = get_function_address(die, decl_symbol_address)))
4874 symbol_is_exported =
4875 !!function_symbol_is_exported(decl_symbol_address);
4876 }
4877
4878 if (address_found)
4879 result = symbol_is_exported;
4880
4881 return result;
4882 }
4883
4884 /// Test if a DIE is a variable or function DIE which name denotes
4885 /// an undefined ELF symbol.
4886 ///
4887 /// @return true iff @p die represents a function or variable that
4888 /// has an undefined symbol.
4889 bool
4890 is_decl_die_with_undefined_symbol(const Dwarf_Die *die) const
4891 {
4892 if (is_decl_die_with_exported_symbol(die))
4893 return false;
4894
4895 string name, linkage_name;
4896 die_name_and_linkage_name(die, name, linkage_name);
4897 if (linkage_name.empty())
4898 linkage_name = name;
4899
4900 bool result = false;
4901 if ((die_is_variable_decl(die)
4902 && symtab()->variable_symbol_is_undefined(linkage_name))
4903 ||
4904 (die_is_function_decl(die)
4905 && symtab()->function_symbol_is_undefined(linkage_name)))
4906 result = true;
4907
4908 return result;
4909 }
4910
4911 /// This is a sub-routine of maybe_adjust_fn_sym_address and
4912 /// maybe_adjust_var_sym_address.
4913 ///
4914 /// Given an address that we got by looking at some debug
4915 /// information (e.g, a symbol's address referred to by a DWARF
4916 /// TAG), If the ELF file we are interested in is a shared library
4917 /// or an executable, then adjust the address to be coherent with
4918 /// where the executable (or shared library) is loaded. That way,
4919 /// the address can be used to look for symbols in the executable or
4920 /// shared library.
4921 ///
4922 /// @return the adjusted address, or the same address as @p addr if
4923 /// it didn't need any adjustment.
4924 Dwarf_Addr
4925 maybe_adjust_address_for_exec_or_dyn(Dwarf_Addr addr) const
4926 {
4927 if (addr == 0)
4928 return addr;
4929
4930 GElf_Ehdr eh_mem;
4931 GElf_Ehdr *elf_header = gelf_getehdr(elf_handle(), &eh_mem);
4932
4933 if (elf_header->e_type == ET_DYN || elf_header->e_type == ET_EXEC)
4934 {
4935 Dwarf_Addr dwarf_elf_load_address = 0, elf_load_address = 0;
4936 if (get_binary_load_address(dwarf_elf_handle(),
4937 dwarf_elf_load_address)
4939 elf_load_address))
4940 if (dwarf_is_splitted()
4941 && (dwarf_elf_load_address != elf_load_address))
4942 // This means that in theory the DWARF and the executable are
4943 // not loaded at the same address. And addr is meaningful
4944 // only in the context of the DWARF.
4945 //
4946 // So let's transform addr into an offset relative to where
4947 // the DWARF is loaded, and let's add that relative offset
4948 // to the load address of the executable. That way, addr
4949 // becomes meaningful in the context of the executable and
4950 // can thus be used to compare against the address of
4951 // symbols of the executable, for instance.
4952 addr = addr - dwarf_elf_load_address + elf_load_address;
4953 }
4954
4955 return addr;
4956 }
4957
4958 /// For a relocatable (*.o) elf file, this function expects an
4959 /// absolute address, representing a function symbol. It then
4960 /// extracts the address of the .text section from the symbol
4961 /// absolute address to get the relative address of the function
4962 /// from the beginning of the .text section.
4963 ///
4964 /// For executable or shared library, this function expects an
4965 /// address of a function symbol that was retrieved by looking at a
4966 /// DWARF "file". The function thus adjusts the address to make it
4967 /// be meaningful in the context of the ELF file.
4968 ///
4969 /// In both cases, the address can then be compared against the
4970 /// st_value field of a function symbol from the ELF file.
4971 ///
4972 /// @param addr an adress for a function symbol that was retrieved
4973 /// from a DWARF file.
4974 ///
4975 /// @return the (possibly) adjusted address, or just @p addr if no
4976 /// adjustment took place.
4977 Dwarf_Addr
4978 maybe_adjust_fn_sym_address(Dwarf_Addr addr) const
4979 {
4980 if (addr == 0)
4981 return addr;
4982
4983 Elf* elf = elf_handle();
4984 GElf_Ehdr eh_mem;
4985 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
4986
4987 if (elf_header->e_type == ET_REL)
4988 // We are looking at a relocatable file. In this case, we don't
4989 // do anything because:
4990 //
4991 // 1/ the addresses from DWARF are absolute (relative to the
4992 // beginning of the relocatable file)
4993 //
4994 // 2/ The ELF symbol addresses that we store in our lookup
4995 // tables are translated from section-related to absolute as
4996 // well. So we don't have anything to do at this point for
4997 // ET_REL files.
4998 ;
4999 else
5000 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5001
5002 return addr;
5003 }
5004
5005 /// For a relocatable (*.o) elf file, this function expects an
5006 /// absolute address, representing a global variable symbol. It
5007 /// then extracts the address of the {.data,.data1,.rodata,.bss}
5008 /// section from the symbol absolute address to get the relative
5009 /// address of the variable from the beginning of the data section.
5010 ///
5011 /// For executable or shared library, this function expects an
5012 /// address of a variable symbol that was retrieved by looking at a
5013 /// DWARF "file". The function thus adjusts the address to make it
5014 /// be meaningful in the context of the ELF file.
5015 ///
5016 /// In both cases, the address can then be compared against the
5017 /// st_value field of a function symbol from the ELF file.
5018 ///
5019 /// @param addr an address for a global variable symbol that was
5020 /// retrieved from a DWARF file.
5021 ///
5022 /// @return the (possibly) adjusted address, or just @p addr if no
5023 /// adjustment took place.
5024 Dwarf_Addr
5025 maybe_adjust_var_sym_address(Dwarf_Addr addr) const
5026 {
5027 Elf* elf = elf_handle();
5028 GElf_Ehdr eh_mem;
5029 GElf_Ehdr* elf_header = gelf_getehdr(elf, &eh_mem);
5030
5031 if (elf_header->e_type == ET_REL)
5032 // We are looking at a relocatable file. In this case, we don't
5033 // do anything because:
5034 //
5035 // 1/ the addresses from DWARF are absolute (relative to the
5036 // beginning of the relocatable file)
5037 //
5038 // 2/ The ELF symbol addresses that we store in our lookup
5039 // tables are translated from section-related to absolute as
5040 // well. So we don't have anything to do at this point for
5041 // ET_REL files.
5042 ;
5043 else
5044 addr = maybe_adjust_address_for_exec_or_dyn(addr);
5045
5046 return addr;
5047 }
5048
5049 /// Get the first exported function address in the set of addresses
5050 /// referred to by the DW_AT_ranges attribute of a given DIE.
5051 ///
5052 /// @param die the DIE we are considering.
5053 ///
5054 /// @param address output parameter. This is set to the first
5055 /// address found in the sequence pointed to by the DW_AT_ranges
5056 /// attribute found on the DIE @p die, iff the function returns
5057 /// true. Otherwise, no value is set into this output parameter.
5058 ///
5059 /// @return true iff the DIE @p die does have a DW_AT_ranges
5060 /// attribute and an address of an exported function was found in
5061 /// its sequence value.
5062 bool
5063 get_first_exported_fn_address_from_DW_AT_ranges(Dwarf_Die* die,
5064 Dwarf_Addr& address) const
5065 {
5066 Dwarf_Addr base;
5067 Dwarf_Addr end_addr;
5068 ptrdiff_t offset = 0;
5069
5070 do
5071 {
5072 Dwarf_Addr addr = 0, fn_addr = 0;
5073 if ((offset = dwarf_ranges(die, offset, &base, &addr, &end_addr)) >= 0)
5074 {
5075 fn_addr = maybe_adjust_fn_sym_address(addr);
5076 if (function_symbol_is_exported(fn_addr))
5077 {
5078 address = fn_addr;
5079 return true;
5080 }
5081 }
5082 } while (offset > 0);
5083 return false;
5084 }
5085
5086 /// Get the address of the function.
5087 ///
5088 /// The address of the function is considered to be the value of the
5089 /// DW_AT_low_pc attribute, possibly adjusted (in relocatable files
5090 /// only) to not point to an absolute address anymore, but rather to
5091 /// the address of the function inside the .text segment.
5092 ///
5093 /// @param function_die the die of the function to consider.
5094 ///
5095 /// @param address the resulting address iff the function returns
5096 /// true.
5097 ///
5098 /// @return true if the function address was found.
5099 bool
5100 get_function_address(const Dwarf_Die* function_die, Dwarf_Addr& address) const
5101 {
5102 if (!die_address_attribute(const_cast<Dwarf_Die*>(function_die),
5103 DW_AT_low_pc, address))
5104 // So no DW_AT_low_pc was found. Let's see if the function DIE
5105 // has got a DW_AT_ranges attribute instead. If it does, the
5106 // first address of the set of addresses represented by the
5107 // value of that DW_AT_ranges represents the function (symbol)
5108 // address we are looking for.
5109 if (!get_first_exported_fn_address_from_DW_AT_ranges
5110 (const_cast<Dwarf_Die*>(function_die),
5111 address))
5112 return false;
5113
5114 address = maybe_adjust_fn_sym_address(address);
5115 return true;
5116 }
5117
5118 /// Test if a function DIE has an associated symbol address.
5119 ///
5120 /// @param function_die the function DIE to consider.
5121 ///
5122 /// @return true iff the function DIE @p function_die has an
5123 /// associated symbol address.
5124 bool
5125 function_has_address(const Dwarf_Die* function_die) const
5126 {
5127 if (!function_die || dwarf_tag(const_cast<Dwarf_Die*>(function_die)) != DW_TAG_subprogram)
5128 return false;
5129
5130 Dwarf_Addr address = 0;
5131 if (get_function_address(function_die, address))
5132 return true;
5133
5134 return false;
5135 }
5136
5137 /// Get the address of the global variable.
5138 ///
5139 /// The address of the global variable is considered to be the value
5140 /// of the DW_AT_location attribute, possibly adjusted (in
5141 /// relocatable files only) to not point to an absolute address
5142 /// anymore, but rather to the address of the global variable inside
5143 /// the data segment.
5144 ///
5145 /// @param variable_die the die of the function to consider.
5146 ///
5147 /// @param address the resulting address iff this function returns
5148 /// true.
5149 ///
5150 /// @return true if the variable address was found.
5151 bool
5152 get_variable_address(const Dwarf_Die* variable_die,
5153 Dwarf_Addr& address) const
5154 {
5155 bool is_tls_address = false;
5156 if (!die_location_address(const_cast<Dwarf_Die*>(variable_die),
5157 address, is_tls_address))
5158 return false;
5159 if (!is_tls_address)
5160 address = maybe_adjust_var_sym_address(address);
5161 return true;
5162 }
5163
5164 /// Getter of the exported decls builder object.
5165 ///
5166 /// @return the exported decls builder.
5168 exported_decls_builder()
5169 {return corpus()->get_exported_decls_builder().get();}
5170
5171 /// Getter of the "load_all_types" flag. This flag tells if all the
5172 /// types (including those not reachable by public declarations) are
5173 /// to be read and represented in the final ABI corpus.
5174 ///
5175 /// @return the load_all_types flag.
5176 bool
5177 load_all_types() const
5178 {return options().load_all_types;}
5179
5180 /// Setter of the "load_all_types" flag. This flag tells if all the
5181 /// types (including those not reachable by public declarations) are
5182 /// to be read and represented in the final ABI corpus.
5183 ///
5184 /// @param f the new load_all_types flag.
5185 void
5186 load_all_types(bool f)
5187 {options().load_all_types = f;}
5188
5189 bool
5190 load_in_linux_kernel_mode() const
5191 {return options().load_in_linux_kernel_mode;}
5192
5193 void
5194 load_in_linux_kernel_mode(bool f)
5195 {options().load_in_linux_kernel_mode = f;}
5196
5197 /// Getter of the 'load-undefined-interface' property.
5198 ///
5199 /// That property tells the reader if it should load the interfaces
5200 /// that are undefined in the binary. An undefined interface is a
5201 /// variable or function which has a symbol that is not defined in
5202 /// the binary.
5203 ///
5204 /// @return true iff the front-end has to load the undefined
5205 /// interfaces.
5206 bool
5207 load_undefined_interfaces() const
5209
5210 /// Getter of the "show_stats" flag.
5211 ///
5212 /// This flag tells if we should emit statistics about various
5213 /// internal stuff.
5214 ///
5215 /// @return the value of the flag.
5216 bool
5217 show_stats() const
5218 {return options().show_stats;}
5219
5220 /// Setter of the "show_stats" flag.
5221 ///
5222 /// This flag tells if we should emit statistics about various
5223 /// internal stuff.
5224 ///
5225 /// @param f the value of the flag.
5226 void
5227 show_stats(bool f)
5228 {options().show_stats = f;}
5229
5230 /// Getter of the "do_log" flag.
5231 ///
5232 /// This flag tells if we should log about various internal
5233 /// details.
5234 ///
5235 /// return the "do_log" flag.
5236 bool
5237 do_log() const
5238 {return options().do_log;}
5239
5240 /// Setter of the "do_log" flag.
5241 ///
5242 /// This flag tells if we should log about various internal details.
5243 ///
5244 /// @param f the new value of the flag.
5245 void
5246 do_log(bool f)
5247 {options().do_log = f;}
5248
5249 /// Walk the DIEs under a given die and for each child, populate the
5250 /// die -> parent map to record the child -> parent relationship
5251 /// that
5252 /// exists between the child and the given die.
5253 ///
5254 /// The function also builds the vector of places where units are
5255 /// imported.
5256 ///
5257 /// This is done recursively as for each child DIE, this function
5258 /// walks its children as well.
5259 ///
5260 /// @param die the DIE whose children to walk recursively.
5261 ///
5262 /// @param parent_of the hash map to populate.
5263 ///
5264 /// @param imported_units a vector containing all the addresses of the
5265 /// points where unit have been imported, under @p die.
5266 void
5267 build_die_parent_relations_under(Dwarf_Die* die,
5268 addr_addr_phmap_type& parent_of,
5269 imported_unit_points_type& imported_units)
5270 {
5271 if (!die)
5272 return;
5273
5274 Dwarf_Die child;
5275 if (dwarf_child(die, &child) != 0)
5276 return;
5277
5278 do
5279 {
5280 parent_of[child.addr] = die->addr;
5281 if (dwarf_tag(&child) == DW_TAG_imported_unit)
5282 {
5283 Dwarf_Die imported_unit;
5284 if (die_die_attribute(&child, DW_AT_import, imported_unit)
5285 // If the imported_unit has a sub-tree, let's record
5286 // this point at which the sub-tree is imported into
5287 // the current debug info.
5288 //
5289 // Otherwise, if the imported_unit has no sub-tree,
5290 // there is no point in recording where a non-existent
5291 // sub-tree is being imported.
5292 //
5293 // Note that the imported_unit_points_type type below
5294 // expects the imported_unit to have a sub-tree.
5295 && die_has_children(&imported_unit))
5296 {
5297 imported_units.push_back(imported_unit_point(child.addr,
5298 imported_unit));
5299 }
5300 }
5301 build_die_parent_relations_under(&child, parent_of,
5302 imported_units);
5303 }
5304 while (dwarf_siblingof(&child, &child) == 0);
5305
5306 }
5307
5308 /// Determine if we do have to build a DIE -> parent map, depending
5309 /// on a given language.
5310 ///
5311 /// Some languages like C++, Ada etc, do have the concept of
5312 /// namespace and yet, the DIE data structure doesn't provide us
5313 /// with a way to get the parent namespace of a given DIE. So for
5314 /// those languages, we need to build a DIE -> parent map so that we
5315 /// can get the namespace DIE (or more generally the scope DIE) of a given
5316 /// DIE as we need it.
5317 ///
5318 /// But then some more basic languages like C or assembly don't have
5319 /// that need.
5320 ///
5321 /// This function, depending on the language, tells us if we need to
5322 /// build the DIE -> parent map or not.
5323 ///
5324 /// @param lang the language to consider.
5325 ///
5326 /// @return true iff we need to build the DIE -> parent map for this
5327 /// language.
5328 bool
5329 do_we_build_die_parent_maps(translation_unit::language lang)
5330 {
5331 if (is_c_language(lang))
5332 return false;
5333
5334 switch (lang)
5335 {
5336 case translation_unit::LANG_UNKNOWN:
5337#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
5338 case translation_unit::LANG_Mips_Assembler:
5339#endif
5340 return false;
5341 default:
5342 break;
5343 }
5344 return true;
5345 }
5346
5347 /// Merge the DIE -> parent maps that are in a set of completed
5348 /// tasks info the DIE -> parent map that is in the current DWARF
5349 /// reader. The tasks were performed in // to build one DIE ->
5350 /// parent map per translation unit found in a given DWARF.
5351 ///
5352 /// @param completed_tasks the tasks completed by build_die_parent_map.
5353 ///
5354 /// This is a sub-routine of build_die_parent_map.
5355 void
5356 merge_die_parent_maps(workers::queue::tasks_type& completed_tasks)
5357 {
5359 if (do_log())
5360 {
5361 auto n = completed_tasks.size();
5362 cerr << "Merging the " << n << " DIE -> parent maps ...\n";
5363 t.start();
5364 }
5365
5366 unsigned size = 0;
5367 for (auto t : completed_tasks)
5368 {
5370 dynamic_pointer_cast<die_parent_relations_builder_task>(t);
5371 size += tsk->parent_of.size();
5372 }
5373 die_parent_map().reserve(size);
5374
5375 for (auto t : completed_tasks)
5376 {
5378 dynamic_pointer_cast<die_parent_relations_builder_task>(t);
5379 ABG_ASSERT(tsk);
5380 tsk->merge_die_parent_maps();
5381 }
5382
5383 if (do_log())
5384 {
5385 t.stop();
5386 cerr << "Merged all the DIE -> parent maps in " << t << "\n";
5387 }
5388 }
5389
5390 /// Walk all the DIEs accessible in the debug info (and in the
5391 /// alternate debug info as well) and build a map representing the
5392 /// relationship DIE -> parent. That is, make it so that we can get
5393 /// the parent for a given DIE.
5394 ///
5395 /// Note that the goal of this map is to be able to get the parent
5396 /// of a given DIE. This is to mainly to handle namespaces. For instance,
5397 /// when we get a DIE of a type, and we want to build an internal
5398 /// representation for it, we need to get its fully qualified name.
5399 /// For that, we need to know what is the parent DIE of that type
5400 /// DIE, so that we can know what the namespace of that type is.
5401 ///
5402 /// Note that as the C language doesn't have namespaces (all types
5403 /// are defined in the same global namespace), this function doesn't
5404 /// build the DIE -> parent map if the current translation unit
5405 /// comes from C. This saves time on big C ELF files with a lot of
5406 /// DIEs.
5407 void
5408 build_die_parent_maps()
5409 {
5410 bool we_do_have_to_build_die_parent_map = false;
5411 uint8_t address_size = 0;
5412 size_t header_size = 0;
5413
5415 if (do_log())
5416 {
5417 cerr << "Do we need to build the DIE -> parent map at all ... ? ";
5418 t.start();
5419 }
5420
5421 // Get the DIE of the current translation unit, look at it to get
5422 // its language. If that language is the C language, then all
5423 // types are in the global namespace so we don't need to build the
5424 // DIE -> parent map. So we dont build it in that case.
5425 for (Dwarf_Off offset = 0, next_offset = 0;
5426 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5427 offset, &next_offset, &header_size,
5428 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5429 offset = next_offset)
5430 {
5431 Dwarf_Off die_offset = offset + header_size;
5432 Dwarf_Die cu;
5433 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5434 die_offset, &cu))
5435 continue;
5436
5437 uint64_t l = 0;
5438 die_unsigned_constant_attribute(&cu, DW_AT_language, l);
5439 translation_unit::language lang = dwarf_language_to_tu_language(l);
5440 if (do_we_build_die_parent_maps(lang))
5441 we_do_have_to_build_die_parent_map = true;
5442 }
5443
5444 if (!we_do_have_to_build_die_parent_map)
5445 {
5446 if (do_log())
5447 {
5448 t.stop();
5449 cerr << " ... No we don't: (" << t << ")\n" ;
5450 }
5451 return;
5452 }
5453
5454 if (do_log())
5455 {
5456 t.stop();
5457 cerr << " ... yes we do: (" << t << ")\n" ;
5458 }
5459
5461 workers::queue die_parent_map_building_queue(nb_workers);
5462
5463 if (do_log())
5464 {
5465 cerr << "Building several DIE -> parent maps in // ...\n";
5466 t.start();
5467 }
5468
5469 // Build the DIE -> parent relation for DIEs coming from the
5470 // .debug_info section in the alternate debug info file.
5471 {
5472 for (Dwarf_Off offset = 0, next_offset = 0;
5473 (dwarf_next_unit(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5474 offset, &next_offset, &header_size,
5475 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5476 offset = next_offset)
5477 {
5478 Dwarf_Off die_offset = offset + header_size;
5479 Dwarf_Die cu;
5480 if (!dwarf_offdie(const_cast<Dwarf*>(alternate_dwarf_debug_info()),
5481 die_offset, &cu))
5482 continue;
5483
5484 imported_unit_points_type& imported_units =
5485 tu_die_imported_unit_points_map()[cu.addr] =
5487
5489 (new die_parent_relations_builder_task(*this, cu, imported_units));
5490
5491 ABG_ASSERT(die_parent_map_building_queue.schedule_task(task));
5492 }
5493 }
5494
5495 // Build the DIE -> parent relation for DIEs coming from the
5496 // .debug_info section of the main debug info file.
5497 {
5498 address_size = 0;
5499 header_size = 0;
5500 for (Dwarf_Off offset = 0, next_offset = 0;
5501 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5502 offset, &next_offset, &header_size,
5503 NULL, NULL, &address_size, NULL, NULL, NULL) == 0);
5504 offset = next_offset)
5505 {
5506 Dwarf_Off die_offset = offset + header_size;
5507 Dwarf_Die cu;
5508 if (!dwarf_offdie(const_cast<Dwarf*>(dwarf_debug_info()),
5509 die_offset, &cu))
5510 continue;
5511
5512 imported_unit_points_type& imported_units =
5513 tu_die_imported_unit_points_map()[cu.addr] =
5515
5517 (new die_parent_relations_builder_task(*this, cu, imported_units));
5518
5519 ABG_ASSERT(die_parent_map_building_queue.schedule_task(task));
5520 }
5521 }
5522
5523 // Build the DIE -> parent relation for DIEs coming from the
5524 // .debug_types section.
5525 {
5526 address_size = 0;
5527 header_size = 0;
5528 uint64_t type_signature = 0;
5529 Dwarf_Off type_offset;
5530 for (Dwarf_Off offset = 0, next_offset = 0;
5531 (dwarf_next_unit(const_cast<Dwarf*>(dwarf_debug_info()),
5532 offset, &next_offset, &header_size,
5533 NULL, NULL, &address_size, NULL,
5534 &type_signature, &type_offset) == 0);
5535 offset = next_offset)
5536 {
5537 Dwarf_Off die_offset = offset + header_size;
5538 Dwarf_Die cu;
5539
5540 if (!dwarf_offdie_types(const_cast<Dwarf*>(dwarf_debug_info()),
5541 die_offset, &cu))
5542 continue;
5543
5544 imported_unit_points_type& imported_units =
5545 tu_die_imported_unit_points_map()[cu.addr] =
5547
5549 (new die_parent_relations_builder_task(*this, cu, imported_units));
5550
5551 ABG_ASSERT(die_parent_map_building_queue.schedule_task(task));
5552 }
5553 }
5554
5555 die_parent_map_building_queue.wait_for_workers_to_complete();
5556
5557 if (do_log())
5558 {
5559 t.stop();
5560 auto n = die_parent_map_building_queue.get_completed_tasks().size();
5561 cerr << "Built " << n << " DIE -> parent maps in // in " << t << "\n";
5562 }
5563
5564 merge_die_parent_maps(die_parent_map_building_queue.get_completed_tasks());
5565 }
5566};// end class reader.
5567
5568static bool
5569do_handle_dwarf_die(reader& rdr, reader::tu_context_type_sptr tu_ctxt,
5570 Dwarf_Die& die, bool die_is_public);
5571
5572static bool
5573potential_member_fn_should_be_dropped(reader& rdr,
5574 reader::tu_context_type_sptr& tu_ctxt,
5575 const Dwarf_Die* fn_die);
5576
5577static string
5578die_qualified_decl_name(const reader& rdr,
5579 const Dwarf_Die* die,
5580 void* where,
5581 reader::tu_context_type_sptr& tu_ctxt,
5582 unordered_set<void*>& guard);
5583
5584static string
5585die_qualified_name(const reader& rdr, const Dwarf_Die* die, void* where,
5586 reader::tu_context_type_sptr& tu_ctxt);
5587
5588static string
5589die_type_name(const reader& rdr, const Dwarf_Die* die,
5590 bool qualified_name, void* where_addr,
5591 reader::tu_context_type_sptr& tu_ctxt,
5592 unordered_set<void*>& infinite_loop_guard);
5593
5594static string
5595die_type_name(const reader& rdr, const Dwarf_Die* die,
5596 bool qualified_name, void* where_addr,
5597 reader::tu_context_type_sptr& tu_ctxt);
5598
5599static bool
5600die_qualified_type_name_empty(const reader& rdr, const Dwarf_Die* die,
5601 void* where, string &qualified_name,
5602 reader::tu_context_type_sptr& tu_ctxt,
5603 unordered_set<void*>& infinite_loop_guard);
5604
5605static string
5606die_function_signature(const reader& rdr, const Dwarf_Die *die,
5607 bool qualified_name, void* where_addr,
5608 reader::tu_context_type_sptr& tu_ctxt,
5609 unordered_set<void*>& infinite_loop_guard);
5610
5611static string
5612die_enum_flat_representation(const reader& rdr,
5613 const Dwarf_Die* die,
5614 const string& indent,
5615 bool one_line,
5616 bool qualified_names,
5617 void* where,
5618 reader::tu_context_type_sptr& tu_ctxt);
5619
5620static string
5621die_class_flat_representation(const reader& rdr,
5622 const Dwarf_Die* die,
5623 const string& indent,
5624 bool one_line,
5625 bool qualified_names,
5626 void* where,
5627 reader::tu_context_type_sptr& tu_ctxt,
5628 unordered_set<void*>& infinite_loop_guard);
5629
5630static string
5631die_pretty_print_type(const reader& rdr, const Dwarf_Die* die,
5632 void* where_addr,
5633 reader::tu_context_type_sptr& tu_ctxt,
5634 unordered_set<void*>& guard);
5635
5636static string
5637die_pretty_print_decl(const reader& rdr, const Dwarf_Die* die,
5638 bool qualified_name, bool include_fns,
5639 void* where_addr,
5640 reader::tu_context_type_sptr& tu_ctxt,
5641 unordered_set<void*>& infinite_loop_guard);
5642
5644build_subrange_type(reader& rdr,
5645 const Dwarf_Die* die,
5646 void* where,
5647 reader::tu_context_type_sptr& tu_ctxt,
5648 bool associate_type_to_die = true);
5649
5650static void
5651build_subranges_from_array_type_die(const reader& rdr,
5652 const Dwarf_Die* die,
5654 void* where,
5655 reader::tu_context_type_sptr& tu_ctxt,
5656 bool associate_type_to_die = true);
5657static bool
5658subrange_die_indirect_bound_value(const Dwarf_Die *die,
5659 unsigned attr_name,
5661 bool& is_signed);
5662
5663static void
5664die_return_and_parm_names_from_fn_type_die(const reader& rdr,
5665 const Dwarf_Die* die,
5666 void* where,
5667 bool pretty_print,
5668 bool qualified_name,
5669 bool &is_method_type,
5670 string &return_type_name,
5671 string &class_name,
5672 vector<string>& parm_names,
5673 bool& is_const,
5674 bool& is_static,
5675 reader::tu_context_type_sptr& tu_ctxt,
5676 unordered_set<void*>& infinite_loop_guard);
5677
5678static bool
5679die_is_at_class_scope(const reader& rdr,
5680 const Dwarf_Die* die,
5681 void* where,
5682 reader::tu_context_type_sptr& tu_ctxt,
5683 Dwarf_Die& class_scope_die);
5684
5685static bool
5686die_is_member_function(const reader& rdr,
5687 const Dwarf_Die* die,
5688 void* where_addr,
5689 reader::tu_context_type_sptr& tu_ctxt,
5690 Dwarf_Die& class_die);
5691
5692static bool
5693member_fn_die_has_this_pointer(const reader& rdr,
5694 const Dwarf_Die* die,
5695 void* where,
5696 reader::tu_context_type_sptr& tu_ctxt,
5697 Dwarf_Die& class_die,
5698 Dwarf_Die& object_pointer_die);
5699
5700static bool
5701die_function_type_is_method_type(const reader& rdr,
5702 const Dwarf_Die *die,
5703 void* where,
5704 reader::tu_context_type_sptr& tu_ctxt,
5705 Dwarf_Die& object_pointer_die,
5706 Dwarf_Die& class_die,
5707 bool& is_static);
5708
5709static void
5710die_loc_and_name(Dwarf_Die* die,
5711 reader::tu_context_type_sptr& tu_ctxt,
5712 location& loc,
5713 string& name,
5714 string& linkage_name);
5715
5716static bool
5717build_ir_nodes_from_imported_unit(reader& rdr,
5718 Dwarf_Die* die,
5719 reader::tu_context_type_sptr tu_ctxt);
5720
5722build_ir_node_from_die(reader& rdr,
5723 Dwarf_Die* die,
5724 scope_decl_sptr scope,
5725 bool called_from_public_decl,
5726 void* where_addr,
5727 reader::tu_context_type_sptr& tu_ctxt,
5728 bool is_declaration_only = true,
5729 bool is_required_decl_spec = false);
5730
5732build_ir_node_from_die(reader& rdr,
5733 Dwarf_Die* die,
5734 bool called_from_public_decl,
5735 void* where_addr,
5736 reader::tu_context_type_sptr& tu_ctxt,
5737 bool is_required_decl_spec = false);
5738
5739static decl_base_sptr
5740build_ir_node_for_void_type(reader& rdr,
5741 reader::tu_context_type_sptr& tu_ctxt);
5742
5744build_ir_node_for_void_pointer_type(reader& rdr,
5745 reader::tu_context_type_sptr& tu_ctxt);
5746
5747static decl_base_sptr
5748build_ir_node_for_variadic_parameter_type(reader &rdr,
5749 reader::tu_context_type_sptr& tu_ctxt);
5750
5751static class_decl_sptr
5752add_or_update_class_type(reader& rdr,
5753 Dwarf_Die* die,
5754 bool is_struct,
5755 class_decl_sptr klass,
5756 bool called_from_public_decl,
5757 void* where,
5758 bool is_declaration_only,
5759 reader::tu_context_type_sptr& tu_ctxt);
5760
5761static union_decl_sptr
5762add_or_update_union_type(reader& rdr,
5763 Dwarf_Die* die,
5764 union_decl_sptr union_type,
5765 bool called_from_public_decl,
5766 void* where,
5767 bool is_declaration_only,
5768 reader::tu_context_type_sptr& tu_ctxt);
5769
5770static bool
5771maybe_get_origin_type(reader& rdr,
5772 const Dwarf_Die* die,
5773 Dwarf_Die& origin_die,
5774 type_base_sptr& origin_type);
5775
5776static function_type_sptr
5777build_function_type(reader& rdr,
5778 Dwarf_Die* die,
5779 class_or_union_sptr is_method,
5780 void* where_addr,
5781 vector<decl_base_sptr>& decls,
5782 reader::tu_context_type_sptr& tu_ctxt);
5783
5784static function_decl_sptr
5785build_function_decl(reader& rdr, Dwarf_Die* die, void* where,
5786 reader::tu_context_type_sptr& tu_ctxt,
5788
5789static bool
5790function_is_suppressed(const reader& rdr,
5791 const scope_decl_sptr scope,
5792 Dwarf_Die *function_die,
5793 bool is_declaration_only);
5794
5795static function_decl_sptr
5796build_or_get_fn_decl_if_not_suppressed(reader& rdr,
5797 scope_decl_sptr scope,
5798 Dwarf_Die *die,
5799 void* where,
5800 reader::tu_context_type_sptr& tu_ctxt,
5801 bool is_declaration_only,
5803
5804static var_decl_sptr
5805build_var_decl(reader& rdr,
5806 Dwarf_Die* die,
5807 void* where,
5808 reader::tu_context_type_sptr& tu_ctxt,
5809 var_decl_sptr result = var_decl_sptr());
5810
5811static var_decl_sptr
5812build_or_get_var_decl_if_not_suppressed(reader& rdr,
5813 scope_decl_sptr scope,
5814 Dwarf_Die *die,
5815 void* where,
5816 reader::tu_context_type_sptr& tu_ctxt,
5817 bool is_declaration_only,
5819 bool is_required_decl_spec = false);
5820static bool
5821variable_is_suppressed(const reader& rdr,
5822 const scope_decl_sptr scope,
5823 Dwarf_Die *variable_die,
5824 bool is_declaration_only,
5825 bool is_required_decl_spec = false);
5826
5827/// This is what the die_parent_relations_builder_task actually does
5828/// in its own thread.
5829void
5830die_parent_relations_builder_task::perform()
5831{
5832 rdr.build_die_parent_relations_under(&tu_die, parent_of, imported_units);
5833}
5834
5835/// This merges the map carried by a given
5836/// die_parent_relations_builder_task into the map
5837/// dwarf::reader::die_parent_map().
5838void
5839die_parent_relations_builder_task::merge_die_parent_maps()
5840{
5841 rdr.die_parent_map().merge(parent_of);
5842}
5843
5844/// Test if a given DIE is anonymous
5845///
5846/// @param die the DIE to consider.
5847///
5848/// @return true iff @p die is anonymous.
5849static bool
5850die_is_anonymous(const Dwarf_Die* die)
5851{
5852 Dwarf_Attribute attr;
5853 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_name, &attr))
5854 return true;
5855 return false;
5856}
5857
5858/// Test if a DIE is an anonymous data member, aka, "unnamed field".
5859///
5860/// Unnamed fields are specified at
5861/// https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html.
5862///
5863/// @param die the DIE to consider.
5864///
5865/// @return true iff @p die is an anonymous data member.
5866static bool
5867die_is_anonymous_data_member(const Dwarf_Die* die)
5868{
5869 if (!die
5870 || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_member
5871 || !die_name(die).empty())
5872 return false;
5873
5874 Dwarf_Die type_die;
5875 if (!die_die_attribute(die, DW_AT_type, type_die))
5876 return false;
5877
5878 if (dwarf_tag(&type_die) != DW_TAG_structure_type
5879 && dwarf_tag(&type_die) != DW_TAG_union_type)
5880 return false;
5881
5882 return true;
5883}
5884
5885/// Get the value of an attribute that is supposed to be a string, or
5886/// an empty string if the attribute could not be found.
5887///
5888/// @param die the DIE to get the attribute value from.
5889///
5890/// @param attr_name the attribute name. Must come from dwarf.h and
5891/// be an enumerator representing an attribute like, e.g, DW_AT_name.
5892///
5893/// @return the string representing the value of the attribute, or an
5894/// empty string if no string attribute could be found.
5895static string
5896die_string_attribute(const Dwarf_Die* die, unsigned attr_name)
5897{
5898 if (!die)
5899 return "";
5900
5901 Dwarf_Attribute attr;
5902 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
5903 return "";
5904
5905 const char* str = dwarf_formstring(&attr);
5906 return str ? str : "";
5907}
5908
5909/// Get the value of an attribute that is supposed to be an unsigned
5910/// constant.
5911///
5912/// @param die the DIE to read the information from.
5913///
5914/// @param attr_name the DW_AT_* name of the attribute. Must come
5915/// from dwarf.h and be an enumerator representing an attribute like,
5916/// e.g, DW_AT_decl_line.
5917///
5918///@param cst the output parameter that is set to the value of the
5919/// attribute @p attr_name. This parameter is set iff the function
5920/// return true.
5921///
5922/// @return true if there was an attribute of the name @p attr_name
5923/// and with a value that is a constant, false otherwise.
5924static bool
5925die_unsigned_constant_attribute(const Dwarf_Die* die,
5926 unsigned attr_name,
5927 uint64_t& cst)
5928{
5929 if (!die)
5930 return false;
5931
5932 Dwarf_Attribute attr;
5933 Dwarf_Word result = 0;
5934 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5935 || dwarf_formudata(&attr, &result))
5936 return false;
5937
5938 cst = result;
5939 return true;
5940}
5941
5942/// Read a signed constant value from a given attribute.
5943///
5944/// The signed constant expected must be of constant form.
5945///
5946/// @param die the DIE to get the attribute from.
5947///
5948/// @param attr_name the attribute name.
5949///
5950/// @param cst the resulting signed constant read.
5951///
5952/// @return true iff a signed constant attribute of the name @p
5953/// attr_name was found on the DIE @p die.
5954static bool
5955die_signed_constant_attribute(const Dwarf_Die *die,
5956 unsigned attr_name,
5957 int64_t& cst)
5958{
5959 if (!die)
5960 return false;
5961
5962 Dwarf_Attribute attr;
5963 Dwarf_Sword result = 0;
5964 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
5965 || dwarf_formsdata(&attr, &result))
5966 return false;
5967
5968 cst = result;
5969 return true;
5970}
5971
5972/// Read the value of a constant attribute that is either signed or
5973/// unsigned into a array_type_def::subrange_type::bound_value value.
5974///
5975/// The bound_value instance will capture the actual signedness of the
5976/// read attribute.
5977///
5978/// @param die the DIE from which to read the value of the attribute.
5979///
5980/// @param attr_name the attribute name to consider.
5981///
5982/// @param is_signed true if the attribute value has to read as
5983/// signed.
5984///
5985/// @param value the resulting value read from attribute @p attr_name
5986/// on DIE @p die.
5987///
5988/// @return true iff DIE @p die has an attribute named @p attr_name
5989/// with a constant value.
5990static bool
5991die_constant_attribute(const Dwarf_Die *die,
5992 unsigned attr_name,
5993 bool is_signed,
5994 array_type_def::subrange_type::bound_value &value)
5995{
5996 if (!is_signed)
5997 {
5998 uint64_t l = 0;
5999 if (!die_unsigned_constant_attribute(die, attr_name, l))
6000 return false;
6001 value.set_unsigned(l);
6002 }
6003 else
6004 {
6005 int64_t l = 0;
6006 if (!die_signed_constant_attribute(die, attr_name, l))
6007 return false;
6008 value.set_signed(l);
6009 }
6010 return true;
6011}
6012
6013/// Get the value of a DIE attribute; that value is meant to be a
6014/// flag.
6015///
6016/// @param die the DIE to get the attribute from.
6017///
6018/// @param attr_name the DW_AT_* name of the attribute. Must come
6019/// from dwarf.h and be an enumerator representing an attribute like,
6020/// e.g, DW_AT_external.
6021///
6022/// @param flag the output parameter to store the flag value into.
6023/// This is set iff the function returns true.
6024///
6025/// @param recursively if true, the function looks through the
6026/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6027/// all the way down to the initial DIE that is cloned and look on
6028/// that DIE to see if it has the @p attr_name attribute.
6029///
6030/// @return true if the DIE has a flag attribute named @p attr_name,
6031/// false otherwise.
6032static bool
6033die_flag_attribute(const Dwarf_Die* die,
6034 unsigned attr_name,
6035 bool& flag,
6036 bool recursively = true)
6037{
6038 Dwarf_Attribute attr;
6039 if (recursively
6040 ? !dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr)
6041 : !dwarf_attr(const_cast<Dwarf_Die*>(die), attr_name, &attr))
6042 return false;
6043
6044 bool f = false;
6045 if (dwarf_formflag(&attr, &f))
6046 return false;
6047
6048 flag = f;
6049 return true;
6050}
6051
6052/// Get the mangled name from a given DIE.
6053///
6054/// @param die the DIE to read the mangled name from.
6055///
6056/// @return the mangled name if it's present in the DIE, or just an
6057/// empty string if it's not.
6058static string
6059die_linkage_name(const Dwarf_Die* die)
6060{
6061 if (!die)
6062 return "";
6063
6064 string linkage_name = die_string_attribute(die, DW_AT_linkage_name);
6065 if (linkage_name.empty())
6066 linkage_name = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6067 return linkage_name;
6068}
6069
6070/// Get the file path that is the value of the DW_AT_decl_file
6071/// attribute on a given DIE, if the DIE is a decl DIE having that
6072/// attribute.
6073///
6074/// @param die the DIE to consider.
6075///
6076/// @return a string containing the file path that is the logical
6077/// value of the DW_AT_decl_file attribute. If the DIE @p die
6078/// doesn't have a DW_AT_decl_file attribute, then the return value is
6079/// just an empty string.
6080static string
6081die_decl_file_attribute(const Dwarf_Die* die)
6082{
6083 if (!die)
6084 return "";
6085
6086 const char* str = dwarf_decl_file(const_cast<Dwarf_Die*>(die));
6087
6088 return str ? str : "";
6089}
6090
6091/// Get the value of an attribute which value is supposed to be a
6092/// reference to a DIE.
6093///
6094/// @param the_die the DIE to read the value from.
6095///
6096/// @param attr_name the DW_AT_* attribute name to read.
6097///
6098/// @param result the DIE resulting from reading the attribute value.
6099/// This is set iff the function returns true.
6100///
6101/// @param recursively if true, the function looks through the
6102/// possible DW_AT_specification and DW_AT_abstract_origin attribute
6103/// all the way down to the initial DIE that is cloned and look on
6104/// that DIE to see if it has the @p attr_name attribute.
6105///
6106/// @return true if the DIE @p die contains an attribute named @p
6107/// attr_name that is a DIE reference, false otherwise.
6108static bool
6109die_die_attribute(const Dwarf_Die* the_die,
6110 unsigned attr_name,
6111 Dwarf_Die& result,
6112 bool recursively)
6113{
6114 Dwarf_Die *die = const_cast<Dwarf_Die*>(the_die);
6115 Dwarf_Attribute attr;
6116 if (recursively
6117 ? !dwarf_attr_integrate(die, attr_name, &attr)
6118 : !dwarf_attr(die, attr_name, &attr))
6119 return false;
6120
6121 bool res = dwarf_formref_die(&attr, &result);
6122
6123 if (res)
6124 {
6125 // As a sanity check, make sure we can get the tag of the
6126 // resulting DIE.
6127 int tag = dwarf_tag(&result);
6128 if (tag == DW_TAG_invalid)
6129 return false;
6130 }
6131
6132 return res;
6133}
6134
6135/// Get the DIE that is the "origin" of the current one.
6136///
6137/// Some DIEs have a DW_AT_abstract_origin or a DW_AT_specification
6138/// attribute. Those DIEs represent a concrete instance of an
6139/// abstract entity. The concrete instance can be a concrete instance
6140/// of an inline function, or the concrete implementation of an
6141/// abstract interface. On both cases, we call the abstract instance
6142/// from which the concrete instance derives the "origin".
6143///
6144/// This function returns the ultimate origin DIE of a given DIE by
6145/// following the chain of its DW_AT_abstract_origin and
6146/// DW_AT_specification attributes.
6147///
6148/// @param die the DIE to consider.
6149///
6150/// @param origin_die this is an output parameter that is set by this
6151/// function to the resulting origin DIE iff the function returns
6152/// true.
6153///
6154/// @return true iff the function actually found an origin DIE and
6155/// set it to the @p origin_die parameter.
6156static bool
6157die_origin_die(const Dwarf_Die* die, Dwarf_Die& origin_die)
6158{
6159 if (die_die_attribute(die, DW_AT_specification, origin_die, true)
6160 || die_die_attribute(die, DW_AT_abstract_origin, origin_die, true))
6161 {
6162 while (die_die_attribute(&origin_die,
6163 DW_AT_specification,
6164 origin_die, true)
6165 || die_die_attribute(&origin_die,
6166 DW_AT_abstract_origin,
6167 origin_die, true))
6168 {
6169 // Keep looking for the origin die ...
6170 ;
6171 }
6172 return true;
6173 }
6174 return false;
6175}
6176
6177/// Test if a subrange DIE indirectly references another subrange DIE
6178/// through a given attribute.
6179///
6180/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6181/// attribute be a reference to either a data member or a variable
6182/// which type is itself a DW_TAG_subrange_type. This latter subrange
6183/// DIE is said to be "indirectly referenced" by the former subrange
6184/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6185/// the value we want for the DW_AT_upper_bound of the former.
6186///
6187/// This function tests if the former subrange DIE does indirectly
6188/// reference another subrange DIE through a given attribute (not
6189/// necessarily DW_AT_upper_bound).
6190///
6191/// @param die the DIE to consider. Note that It must be a
6192/// DW_TAG_subrange_type.
6193///
6194/// @param attr_name the name of the attribute to look through for the
6195/// indirectly referenced subrange DIE.
6196///
6197/// @param referenced_subrange if the function returns true, then the
6198/// argument of this parameter is set to the indirectly referenced
6199/// DW_TAG_subrange_type DIE.
6200///
6201/// @return true iff @p DIE indirectly references a subrange DIE
6202/// through the attribute @p attr_name.
6203static bool
6204subrange_die_indirectly_references_subrange_die(const Dwarf_Die *die,
6205 unsigned attr_name,
6206 Dwarf_Die& referenced_subrange)
6207{
6208 bool result = false;
6209
6210 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6211 return result;
6212
6213 Dwarf_Die referenced_die;
6214 if (die_die_attribute(die, attr_name, referenced_die))
6215 {
6216 unsigned tag = dwarf_tag(&referenced_die);
6217 if ( tag == DW_TAG_member || tag == DW_TAG_variable)
6218 {
6219 Dwarf_Die type_die;
6220 if (die_die_attribute(&referenced_die, DW_AT_type, type_die))
6221 {
6222 tag = dwarf_tag(&type_die);
6223 if (tag == DW_TAG_subrange_type)
6224 {
6225 memcpy(&referenced_subrange, &type_die, sizeof(type_die));
6226 result = true;
6227 }
6228 }
6229 }
6230 }
6231 return result;
6232}
6233
6234/// Return the bound value of subrange die by looking at an indirectly
6235/// referenced subrange DIE.
6236///
6237/// A DW_TAG_subrange_type DIE can have its DW_AT_{lower,upper}_bound
6238/// attribute be a reference to either a data member or a variable
6239/// which type is itself a DW_TAG_subrange_type. This latter subrange
6240/// DIE is said to be "indirectly referenced" by the former subrange
6241/// DIE. In that case, the DW_AT_{lower,upper}_bound of the latter is
6242/// the value we want for the DW_AT_{lower,upper}_bound of the former.
6243///
6244/// This function gets the DW_AT_{lower,upper}_bound value of a
6245/// subrange type by looking at the DW_AT_{lower,upper}_bound value of
6246/// the indirectly referenced subrange type, if it exists.
6247///
6248/// @param die the subrange DIE to consider.
6249///
6250/// @param attr_name the name of the attribute to consider, typically,
6251/// DW_AT_{lower,upper}_bound.
6252///
6253/// @param v the found value, iff this function returned true.
6254///
6255/// @param is_signed, this is set to true if @p v is signed. This
6256/// parameter is set at all only if the function returns true.
6257///
6258/// @return true iff the DW_AT_{lower,upper}_bound was found on the
6259/// indirectly referenced subrange type.
6260static bool
6261subrange_die_indirect_bound_value(const Dwarf_Die *die,
6262 unsigned attr_name,
6263 array_type_def::subrange_type::bound_value& v,
6264 bool& is_signed)
6265{
6266 bool result = false;
6267
6268 if (dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subrange_type)
6269 return result;
6270
6271 Dwarf_Die subrange_die;
6272 if (subrange_die_indirectly_references_subrange_die(die, attr_name,
6273 subrange_die))
6274 {
6275 if (die_constant_attribute(&subrange_die, attr_name, is_signed, v))
6276 result = true;
6277 }
6278 return result;
6279}
6280
6281/// Read and return an addresss class attribute from a given DIE.
6282///
6283/// @param die the DIE to consider.
6284///
6285/// @param attr_name the name of the address class attribute to read
6286/// the value from.
6287///
6288/// @param the resulting address.
6289///
6290/// @return true iff the attribute could be read, was of the expected
6291/// address class and could thus be translated into the @p result.
6292static bool
6293die_address_attribute(Dwarf_Die* die, unsigned attr_name, Dwarf_Addr& result)
6294{
6295 Dwarf_Attribute attr;
6296 if (!dwarf_attr_integrate(die, attr_name, &attr))
6297 return false;
6298 return dwarf_formaddr(&attr, &result) == 0;
6299}
6300
6301/// Returns the source location associated with a decl DIE.
6302///
6303/// @param rdr the @ref reader to use.
6304///
6305/// @param die the DIE the read the source location from.
6306///
6307/// @return the location associated with @p die.
6308location
6309die_location(const Dwarf_Die* die,
6310 reader::tu_context_type_sptr& tu_ctxt)
6311{
6312 if (!die)
6313 return location();
6314
6315 string file = die_decl_file_attribute(die);
6316 uint64_t line = 0;
6317 die_unsigned_constant_attribute(die, DW_AT_decl_line, line);
6318
6319 if (!file.empty() && line != 0)
6320 {
6321 translation_unit_sptr tu = tu_ctxt->get_tu();
6322 location l = tu->get_loc_mgr().create_new_location(file, line, 1);
6323 return l;
6324 }
6325 return location();
6326}
6327
6328/// Return a copy of the name of a DIE.
6329///
6330/// @param die the DIE to consider.
6331///
6332/// @return a copy of the name of the DIE.
6333static string
6334die_name(const Dwarf_Die* die)
6335{
6336 string name = die_string_attribute(die, DW_AT_name);
6337 return name;
6338}
6339
6340/// Return the location, the name and the mangled name of a given DIE.
6341///
6342/// @param rdr the DWARF reader to use.
6343///
6344/// @param die the DIE to read location and names from.
6345///
6346/// @param loc the location output parameter to set.
6347///
6348/// @param name the name output parameter to set.
6349///
6350/// @param linkage_name the linkage_name output parameter to set.
6351static void
6352die_loc_and_name(Dwarf_Die* die,
6353 reader::tu_context_type_sptr& tu_ctxt,
6354 location& loc,
6355 string& name,
6356 string& linkage_name)
6357{
6358 loc = die_location(die, tu_ctxt);
6359 name = die_name(die);
6360 linkage_name = die_linkage_name(die);
6361}
6362
6363/// Return the name and the mangled name of a given DIE.
6364///
6365/// @param die the DIE to read location and names from.
6366///
6367/// @param name the name output parameter to set.
6368///
6369/// @param linkage_name the linkage_name output parameter to set.
6370static void
6371die_name_and_linkage_name(const Dwarf_Die* die,
6372 string& name,
6373 string& linkage_name)
6374{
6375 name = die_name(die);
6376 linkage_name = die_linkage_name(die);
6377}
6378
6379/// Get the size of a (type) DIE as the value for the parameter
6380/// DW_AT_byte_size or DW_AT_bit_size.
6381///
6382/// @param die the DIE to read the information from.
6383///
6384/// @param size the resulting size in bits. This is set iff the
6385/// function return true.
6386///
6387/// @return true if the size attribute was found.
6388static bool
6389die_size_in_bits(const Dwarf_Die* die, uint64_t& size)
6390{
6391 if (!die)
6392 return false;
6393
6394 uint64_t byte_size = 0, bit_size = 0;
6395
6396 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
6397 {
6398 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
6399 return false;
6400 }
6401 else
6402 bit_size = byte_size * 8;
6403
6404 size = bit_size;
6405
6406 return true;
6407}
6408
6409/// Get the access specifier (from the DW_AT_accessibility attribute
6410/// value) of a given DIE.
6411///
6412/// @param die the DIE to consider.
6413///
6414/// @param access the resulting access. This is set iff the function
6415/// returns true.
6416///
6417/// @return bool if the DIE contains the DW_AT_accessibility die.
6418static bool
6419die_access_specifier(Dwarf_Die * die, access_specifier& access)
6420{
6421 if (!die)
6422 return false;
6423
6424 uint64_t a = 0;
6425 if (!die_unsigned_constant_attribute(die, DW_AT_accessibility, a))
6426 return false;
6427
6428 access_specifier result = private_access;
6429
6430 switch (a)
6431 {
6432 case private_access:
6433 result = private_access;
6434 break;
6435
6436 case protected_access:
6437 result = protected_access;
6438 break;
6439
6440 case public_access:
6441 result = public_access;
6442 break;
6443
6444 default:
6445 break;
6446 }
6447
6448 access = result;
6449 return true;
6450}
6451
6452/// Test whether a given DIE represents a decl that is public. That
6453/// is, one with the DW_AT_external attribute set.
6454///
6455/// @param die the DIE to consider for testing.
6456///
6457/// @return true if a DW_AT_external attribute is present and its
6458/// value is set to the true; return false otherwise.
6459static bool
6460die_is_public_decl(const Dwarf_Die* die)
6461{
6462 if (!die)
6463 return false;
6464 bool is_public = false;
6465
6466 // If this is a DW_TAG_subprogram DIE, look for the
6467 // DW_AT_external attribute on it. Otherwise, if it's a non-anonymous namespace,
6468 // then it's public. In all other cases, this should return false.
6469
6470 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6471 if (tag == DW_TAG_subprogram || tag == DW_TAG_variable)
6472 {
6473 die_flag_attribute(die, DW_AT_external, is_public);
6474 if (!is_public)
6475 {
6476 // try linkage name
6477 string n = die_string_attribute(die, DW_AT_MIPS_linkage_name);
6478 if (n.empty())
6479 n = die_string_attribute(die, DW_AT_linkage_name);
6480 is_public = !n.empty();
6481 }
6482 }
6483 else if (tag == DW_TAG_namespace)
6484 {
6485 string name = die_name(die);
6486 is_public = !name.empty();
6487 }
6488
6489 return is_public;
6490}
6491
6492/// Test if a DIE is effectively public.
6493///
6494/// This is meant to return true when either the DIE is public or when
6495/// it's a variable DIE that is at (global) namespace level.
6496///
6497/// @return true iff either the DIE is public or is a variable DIE
6498/// that is at (global) namespace level.
6499static bool
6500die_is_effectively_public_decl(const reader& rdr, const Dwarf_Die* die,
6501 reader::tu_context_type_sptr& tu_ctxt)
6502{
6503 if (die_is_public_decl(die))
6504 return true;
6505
6506 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6507 if (tag == DW_TAG_variable || tag == DW_TAG_member)
6508 {
6509 // The DIE is a variable.
6510 Dwarf_Die parent_die;
6511 if (!rdr.get_parent_die(die, parent_die,
6512 /*where_addr=*/nullptr,
6513 tu_ctxt))
6514 return false;
6515
6516 tag = dwarf_tag(&parent_die);
6517 if (tag == DW_TAG_compile_unit
6518 || tag == DW_TAG_partial_unit
6519 || tag == DW_TAG_type_unit)
6520 // The DIE is at global scope.
6521 return true;
6522
6523 if (tag == DW_TAG_namespace)
6524 {
6525 string name = die_name(&parent_die);
6526 if (name.empty())
6527 // The DIE at unnamed namespace scope, so it's not public.
6528 return false;
6529 // The DIE is at namespace scope.
6530 return true;
6531 }
6532 }
6533 return false;
6534}
6535
6536/// Test whether a given DIE represents a declaration-only DIE.
6537///
6538/// That is, if the DIE has the DW_AT_declaration flag set.
6539///
6540/// @param die the DIE to consider.
6541//
6542/// @return true if a DW_AT_declaration is present, false otherwise.
6543static bool
6544die_is_declaration_only(Dwarf_Die* die)
6545{
6546 bool is_declaration = false;
6547 die_flag_attribute(die, DW_AT_declaration, is_declaration, false);
6548 if (is_declaration && (!die_has_size_attribute(die)
6549 || !die_has_children(die)))
6550 return true;
6551 return false;
6552}
6553
6554/// Test if a DIE is for a function decl.
6555///
6556/// @param die the DIE to consider.
6557///
6558/// @return true iff @p die represents a function decl.
6559static bool
6560die_is_function_decl(const Dwarf_Die *die)
6561{
6562 if (!die)
6563 return false;
6564
6565 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6566 ABG_ASSERT(tag);
6567 if (tag == DW_TAG_subprogram)
6568 return true;
6569 return false;
6570}
6571
6572/// Test if a DIE is for a member function.
6573///
6574/// @param rdr the DWARF reader to use to read the properties of the
6575/// DIE.
6576///
6577/// @param die the DIE to consider.
6578///
6579/// @param where_addr where we currently are in the DIE tree. This is
6580/// useful when dealing with DWARF compressed with the 'dwz' tool.
6581///
6582/// @param class_die output parameter that is set iff @p die is a
6583/// member function and thus, iff the function returns true. This is
6584/// set to the containing class DIE of the member function.
6585///
6586/// @return true iff @p die is for a member function.
6587static bool
6588die_is_member_function(const reader& rdr,
6589 const Dwarf_Die* die,
6590 void* where_addr,
6591 reader::tu_context_type_sptr& tu_ctxt,
6592 Dwarf_Die& class_die)
6593{
6594 if (!die_is_function_decl(die))
6595 return false;
6596
6597 if (die_is_at_class_scope(rdr, die, where_addr, tu_ctxt, class_die))
6598 return true;
6599
6600 return false;
6601}
6602
6603/// Test if a DIE is for a function decl representing a destructor.
6604///
6605/// @param die the DIE to consider.
6606///
6607/// @return true iff @p die represents a function decl representing a
6608/// destructor.
6609static bool
6610die_is_destructor(const Dwarf_Die *die)
6611{
6612 if (!die_is_function_decl(die))
6613 return false;
6614
6615 string name = die_name(die);
6616 if (!name.empty() && name[0] == '~')
6617 return true;
6618
6619 return false;
6620}
6621
6622/// Test if a DIE is for a variable decl.
6623///
6624/// @param die the DIE to consider.
6625///
6626/// @return true iff @p die represents a variable decl.
6627static bool
6628die_is_variable_decl(const Dwarf_Die *die)
6629{
6630 if (!die)
6631 return false;
6632
6633 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6634 ABG_ASSERT(tag);
6635
6636 if (tag == DW_TAG_variable)
6637 return true;
6638 return false;
6639}
6640
6641/// Test if a DIE has size attribute.
6642///
6643/// @param die the DIE to consider.
6644///
6645/// @return true if the DIE has a size attribute.
6646static bool
6647die_has_size_attribute(const Dwarf_Die *die)
6648{
6649 uint64_t s;
6650 if (die_size_in_bits(die, s))
6651 return true;
6652 return false;
6653}
6654
6655/// Tests whether a given DIE is artificial.
6656///
6657/// @param die the test to test for.
6658///
6659/// @return true if the DIE is artificial, false otherwise.
6660static bool
6661die_is_artificial(Dwarf_Die* die)
6662{
6663 bool is_artificial;
6664 return die_flag_attribute(die, DW_AT_artificial, is_artificial);
6665}
6666
6667///@return true if a tag represents a type, false otherwise.
6668///
6669///@param tag the tag to consider.
6670static bool
6671is_type_tag(unsigned tag)
6672{
6673 bool result = false;
6674
6675 switch (tag)
6676 {
6677 case DW_TAG_array_type:
6678 case DW_TAG_class_type:
6679 case DW_TAG_enumeration_type:
6680 case DW_TAG_pointer_type:
6681 case DW_TAG_reference_type:
6682 case DW_TAG_string_type:
6683 case DW_TAG_structure_type:
6684 case DW_TAG_subroutine_type:
6685 case DW_TAG_typedef:
6686 case DW_TAG_union_type:
6687 case DW_TAG_ptr_to_member_type:
6688 case DW_TAG_set_type:
6689 case DW_TAG_subrange_type:
6690 case DW_TAG_base_type:
6691 case DW_TAG_const_type:
6692 case DW_TAG_file_type:
6693 case DW_TAG_packed_type:
6694 case DW_TAG_thrown_type:
6695 case DW_TAG_volatile_type:
6696 case DW_TAG_restrict_type:
6697 case DW_TAG_interface_type:
6698 case DW_TAG_unspecified_type:
6699 case DW_TAG_shared_type:
6700 case DW_TAG_rvalue_reference_type:
6701 case DW_TAG_coarray_type:
6702 case DW_TAG_atomic_type:
6703 case DW_TAG_immutable_type:
6704 result = true;
6705 break;
6706
6707 default:
6708 result = false;
6709 break;
6710 }
6711
6712 return result;
6713}
6714
6715/// Test if a DIE tag represents a declaration.
6716///
6717/// @param tag the DWARF tag to consider.
6718///
6719/// @return true iff @p tag is for a declaration.
6720static bool
6721is_decl_tag(unsigned tag)
6722{
6723 switch (tag)
6724 {
6725 case DW_TAG_formal_parameter:
6726 case DW_TAG_imported_declaration:
6727 case DW_TAG_member:
6728 case DW_TAG_unspecified_parameters:
6729 case DW_TAG_subprogram:
6730 case DW_TAG_inlined_subroutine:
6731 case DW_TAG_variable:
6732 case DW_TAG_namespace:
6733 case DW_TAG_GNU_template_template_param:
6734 case DW_TAG_GNU_template_parameter_pack:
6735 case DW_TAG_GNU_formal_parameter_pack:
6736 return true;
6737 }
6738 return false;
6739}
6740
6741/// Test if a DIE represents a type DIE.
6742///
6743/// @param die the DIE to consider.
6744///
6745/// @return true if @p die represents a type, false otherwise.
6746static bool
6747die_is_type(const Dwarf_Die* die)
6748{
6749 if (!die)
6750 return false;
6751
6752 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6753 ABG_ASSERT(tag);
6754
6755 return is_type_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6756}
6757
6758/// Test if a DIE represents a declaration.
6759///
6760/// @param die the DIE to consider.
6761///
6762/// @return true if @p die represents a decl, false otherwise.
6763static bool
6764die_is_decl(const Dwarf_Die* die)
6765{
6766 if (!die)
6767 return false;
6768 return is_decl_tag(dwarf_tag(const_cast<Dwarf_Die*>(die)));
6769}
6770
6771/// Test if a DIE represents a namespace.
6772///
6773/// @param die the DIE to consider.
6774///
6775/// @return true if @p die represents a namespace, false otherwise.
6776static bool
6777die_is_namespace(const Dwarf_Die* die)
6778{
6779 if (!die)
6780 return false;
6781 return (dwarf_tag(const_cast<Dwarf_Die*>(die)) == DW_TAG_namespace);
6782}
6783
6784/// Test if a DIE has tag DW_TAG_unspecified_type.
6785///
6786/// @param die the DIE to consider.
6787///
6788/// @return true if @p die has tag DW_TAG_unspecified_type.
6789static bool
6790die_is_unspecified(Dwarf_Die* die)
6791{
6792 if (!die)
6793 return false;
6794 return (dwarf_tag(die) == DW_TAG_unspecified_type);
6795}
6796
6797/// Test if a DIE represents a void type.
6798///
6799/// @param die the DIE to consider.
6800///
6801/// @return true if @p die represents a void type, false otherwise.
6802static bool
6803die_is_void_type(Dwarf_Die* die)
6804{
6805 if (!die || dwarf_tag(die) != DW_TAG_base_type)
6806 return false;
6807
6808 string name = die_name(die);
6809 if (name == "void")
6810 return true;
6811
6812 return false;
6813}
6814
6815/// Test if a DIE represents a pointer type.
6816///
6817/// @param die the die to consider.
6818///
6819/// @return true iff @p die represents a pointer type.
6820static bool
6821die_is_pointer_type(const Dwarf_Die* die)
6822{
6823 if (!die)
6824 return false;
6825
6826 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6827 ABG_ASSERT(tag);
6828
6829 if (tag == DW_TAG_pointer_type)
6830 return true;
6831
6832 return false;
6833}
6834
6835/// Test if a DIE represents a reference type.
6836///
6837/// @param die the die to consider.
6838///
6839/// @return true iff @p die represents a reference type.
6840static bool
6841die_is_reference_type(const Dwarf_Die* die)
6842{
6843 if (!die)
6844 return false;
6845
6846 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6847 ABG_ASSERT(tag);
6848
6849 if (tag == DW_TAG_reference_type || tag == DW_TAG_rvalue_reference_type)
6850 return true;
6851
6852 return false;
6853}
6854
6855/// Test if a DIE represents a pointer or a reference type.
6856///
6857/// @param die the die to consider.
6858///
6859/// @return true iff @p die represents a pointer or reference type.
6860static bool
6861die_is_pointer_or_reference_type(const Dwarf_Die* die)
6862{return (die_is_pointer_type(die) || die_is_reference_type(die));}
6863
6864/// Test if a DIE represents a class type.
6865///
6866/// @param die the die to consider.
6867///
6868/// @return true iff @p die represents a class type.
6869static bool
6870die_is_class_type(const Dwarf_Die* die)
6871{
6872 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6873 ABG_ASSERT(tag);
6874
6875 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
6876 return true;
6877
6878 return false;
6879}
6880
6881/// Test if a DIE for a function pointer or member function has an
6882/// DW_AT_object_pointer attribute.
6883///
6884/// @param die the DIE to consider.
6885///
6886/// @param object_pointer out parameter. It's set to the DIE for the
6887/// object pointer iff the function returns true.
6888///
6889/// @return true iff the DIE @p die has an object pointer. In that
6890/// case, the parameter @p object_pointer is set to the DIE of that
6891/// object pointer.
6892static bool
6893die_has_object_pointer(const Dwarf_Die* die, Dwarf_Die& object_pointer)
6894{
6895 if (!die)
6896 return false;
6897
6898 if (die_die_attribute(die, DW_AT_object_pointer, object_pointer))
6899 return true;
6900
6901 return false;
6902}
6903
6904/// Test if a DIE has children DIEs.
6905///
6906/// @param die the DIE to consider.
6907///
6908/// @return true iff @p DIE has at least one child node.
6909static bool
6910die_has_children(const Dwarf_Die* die)
6911{
6912 if (!die)
6913 return false;
6914
6915 Dwarf_Die child;
6916 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6917 return true;
6918
6919 return false;
6920}
6921
6922/// Get the DIE representing the first parameter of the function
6923/// denoted by a given DIE.
6924///
6925/// @param die the function DIE to consider. Note that if this
6926/// parameter is neither a DW_TAG_subprogram nor a
6927/// DW_TAG_subroutine_type, then the current process is aborted.
6928///
6929/// @param first_parm_die output parameter. This is set to the DIE of
6930/// the first parameter of the function denoted by @p die. This
6931/// output parameter is set iff the function returns true.
6932///
6933/// @return true iff the first parameter of the function denoted by @p
6934/// die is returned in output parameter @p first_parm_die.
6935static bool
6936fn_die_first_parameter_die(const Dwarf_Die* die, Dwarf_Die& first_parm_die)
6937{
6938 if (!die)
6939 return false;
6940
6941 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6942 ABG_ASSERT(tag);
6943
6944 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
6945
6946 Dwarf_Die child;
6947 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
6948 {
6949 int child_tag = dwarf_tag(&child);
6950 if (child_tag == DW_TAG_formal_parameter)
6951 {
6952 memcpy(&first_parm_die, &child, sizeof(Dwarf_Die));
6953 return true;
6954 }
6955 }
6956 return false;
6957}
6958
6959/// Get the class DIE from a member function DIE by looking following
6960/// the DW_AT_object_pointer attribute.
6961///
6962/// @param die the DW_TAG_subprogram or DW_TAG_subroutine_type DIE
6963/// representing the member function (or member function type) to
6964/// consider.
6965///
6966/// @param class_die output parameter. This is set by the function to
6967/// the resulting class DIE found by following the value of the
6968/// DW_AT_object_pointer attribute of @p die. This is set iff the
6969/// function returns true.
6970///
6971/// @param object_ptr_die output parameter. This is set by the
6972/// function to the DIE for object pointer (this pointer), iff the
6973/// function returns true.
6974///
6975/// @return true if @p die has a DW_AT_object_pointer and a class_die
6976/// could be found from it, then this function returns true after
6977/// setting @p class_die and @p object_ptr_die accordingly.
6978static bool
6979get_member_fn_class_die_from_object_pointer(const Dwarf_Die* die,
6980 Dwarf_Die& class_die,
6981 Dwarf_Die& object_ptr_die)
6982{
6983 if (!die)
6984 return false;
6985
6986 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
6987 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
6988 return false;
6989
6990 Dwarf_Die first_parm_die;
6991 Dwarf_Die parm_type_die;
6992 if (die_has_object_pointer(die, object_ptr_die))
6993 {
6994 // This can be either a member function with a
6995 // DW_AT_object_pointer attribute or a DW_TAG_subroutine_type
6996 // with a DW_AT_object_pointer. In the later case, we are
6997 // looking at a member function type.
6998 memcpy(&first_parm_die, &object_ptr_die, sizeof(Dwarf_Die));
6999 if (!die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7000 return false;
7001 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7002 die_peel_typedef(&parm_type_die, parm_type_die);
7003 }
7004 else
7005 return false;
7006
7007 tag = dwarf_tag(&parm_type_die);
7008 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7009 {
7010 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7011 return true;
7012 }
7013
7014 return false;
7015}
7016
7017/// Test if a member function denoted by a given DIE has a parameter
7018/// which is a "this pointer".
7019///
7020/// Please note that if the member function denotes a static member
7021/// function or if the DIE does not denote a member function to begin
7022/// with, then the function will return false because no "this
7023/// pointer" will be found.
7024///
7025/// @param rdr the current DWARF reader in use.
7026///
7027/// @param die the DIE of the member function this function should
7028/// inspect.
7029///
7030/// @param where_offset where in the DIE stream we logically are.
7031///
7032/// @param class_die output parameter. This is set iff a "this
7033/// pointer" was found as the first parameters of the member function
7034/// denoted by @p die, and thus the function returns true If set, this
7035/// then points to the DIE of the class containing the member function
7036/// denoted by @p die.
7037///
7038/// @param object_pointer_die output parameter. This is set to the
7039/// DIE of the function parameter that carries the "this pointe".
7040/// This is set iff this function return true.
7041///
7042/// @return true iff the first parameter of the member function
7043/// denoted by @p die points to a "this pointer".
7044static bool
7045member_fn_die_has_this_pointer(const reader& rdr,
7046 const Dwarf_Die* die,
7047 void* where_addr,
7048 reader::tu_context_type_sptr& tu_ctxt,
7049 Dwarf_Die& class_die,
7050 Dwarf_Die& object_pointer_die)
7051{
7052 if (!die)
7053 return false;
7054
7055 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7056 if (tag != DW_TAG_subprogram && tag != DW_TAG_subroutine_type)
7057 return false;
7058
7059 if (tag == DW_TAG_subprogram
7060 && !die_is_at_class_scope(rdr, die, where_addr, tu_ctxt, class_die))
7061 return false;
7062
7063 if (get_member_fn_class_die_from_object_pointer(die, class_die,
7064 object_pointer_die))
7065 return true;
7066
7067 Dwarf_Die first_parm_die;
7068 Dwarf_Die parm_type_die;
7069 if (fn_die_first_parameter_die(die, first_parm_die))
7070 {
7071 memcpy(&object_pointer_die, &first_parm_die, sizeof(Dwarf_Die));
7072 bool is_artificial = false;
7073 if (die_flag_attribute(&first_parm_die, DW_AT_artificial, is_artificial))
7074 {
7075 if (die_die_attribute(&first_parm_die, DW_AT_type, parm_type_die))
7076 {
7077 tag = dwarf_tag(&parm_type_die);
7078 if (tag == DW_TAG_pointer_type)
7079 {
7080 die_peel_qual_ptr(&parm_type_die, parm_type_die);
7081 die_peel_typedef(&parm_type_die, parm_type_die);
7082 }
7083 else
7084 return false;
7085 }
7086 else
7087 return false;
7088 }
7089 else
7090 return false;
7091 }
7092 else
7093 return false;
7094
7095 tag = dwarf_tag(&parm_type_die);
7096 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
7097 {
7098 memcpy(&class_die, &parm_type_die, sizeof(Dwarf_Die));
7099 return true;
7100 }
7101 return false;
7102}
7103
7104/// When given the object pointer DIE of a function type or member
7105/// function DIE, this function returns the "this" pointer that points
7106/// to the associated class.
7107///
7108/// @param die the DIE of the object pointer of the function or member
7109/// function to consider.
7110///
7111/// @param this_pointer_die out parameter. This is set to the DIE of
7112/// the "this" pointer iff the function returns true.
7113///
7114/// @return true iff the function found the "this" pointer from the
7115/// object pointer DIE @p die. In that case, the parameter @p
7116/// this_pointer_die is set to the DIE of that "this" pointer.
7117static bool
7118die_this_pointer_from_object_pointer(Dwarf_Die* die,
7119 Dwarf_Die& this_pointer_die)
7120{
7121 ABG_ASSERT(die);
7122 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7123
7124 if (die_die_attribute(die, DW_AT_type, this_pointer_die))
7125 return true;
7126
7127 return false;
7128}
7129
7130/// Test if a given "this" pointer that points to a particular class
7131/// type is for a const class or not. If it's for a const class, then
7132/// it means the function type or the member function associated to
7133/// that "this" pointer is const.
7134///
7135/// @param dye the DIE of the "this" pointer to consider.
7136///
7137/// @return true iff @p die points to a const class type.
7138static bool
7139die_this_pointer_is_const(Dwarf_Die* dye)
7140{
7141 ABG_ASSERT(dye);
7142
7143 Dwarf_Die die;
7144 memcpy(&die, dye, sizeof(Dwarf_Die));
7145 if (dwarf_tag(&die) == DW_TAG_const_type)
7146 ABG_ASSERT(die_peel_qualified(&die, die));
7147
7148 if (dwarf_tag(&die) == DW_TAG_pointer_type)
7149 {
7150 Dwarf_Die pointed_to_type_die;
7151 if (die_die_attribute(&die, DW_AT_type, pointed_to_type_die))
7152 if (dwarf_tag(&pointed_to_type_die) == DW_TAG_const_type)
7153 return true;
7154 }
7155
7156 return false;
7157}
7158
7159/// Test if an object pointer (referred-to via a DW_AT_object_pointer
7160/// attribute) points to a const implicit class and so is for a const
7161/// method or or a const member function type.
7162///
7163/// @param die the DIE of the object pointer to consider.
7164///
7165/// @return true iff the object pointer represented by @p die is for a
7166/// a const method or const member function type.
7167static bool
7168die_object_pointer_is_for_const_method(Dwarf_Die* die)
7169{
7170 ABG_ASSERT(die);
7171 ABG_ASSERT(dwarf_tag(die) == DW_TAG_formal_parameter);
7172
7173 Dwarf_Die this_pointer_die;
7174 if (die_this_pointer_from_object_pointer(die, this_pointer_die))
7175 if (die_this_pointer_is_const(&this_pointer_die))
7176 return true;
7177
7178 return false;
7179}
7180
7181/// Test if a DIE represents an entity that is at class scope.
7182///
7183/// @param rdr the DWARF reader to use.
7184///
7185/// @param die the DIE to consider.
7186///
7187/// @param where_offset where we are logically at in the DIE stream.
7188///
7189/// @param class_scope_die out parameter. Set to the DIE of the
7190/// containing class iff @p die happens to be at class scope; that is,
7191/// iff the function returns true.
7192///
7193/// @return true iff @p die is at class scope. In that case, @p
7194/// class_scope_die is set to the DIE of the class that contains @p
7195/// die.
7196static bool
7197die_is_at_class_scope(const reader& rdr, const Dwarf_Die* die, void* where,
7198 reader::tu_context_type_sptr& tu_ctxt,
7199 Dwarf_Die& class_scope_die)
7200{
7201 if (!rdr.get_scope_die(die, where, tu_ctxt, class_scope_die))
7202 return false;
7203
7204 int tag = dwarf_tag(&class_scope_die);
7205
7206 return (tag == DW_TAG_structure_type
7207 || tag == DW_TAG_class_type
7208 || tag == DW_TAG_union_type);
7209}
7210
7211/// Return the leaf object under a pointer, reference or qualified
7212/// type DIE.
7213///
7214/// @param die the DIE of the type to consider.
7215///
7216/// @param peeled_die out parameter. Set to the DIE of the leaf
7217/// object iff the function actually peeled anything.
7218///
7219/// @return true upon successful completion.
7220static bool
7221die_peel_qual_ptr(Dwarf_Die *die, Dwarf_Die& peeled_die)
7222{
7223 if (!die)
7224 return false;
7225
7226 int tag = dwarf_tag(die);
7227
7228 if (tag == DW_TAG_const_type
7229 || tag == DW_TAG_volatile_type
7230 || tag == DW_TAG_restrict_type
7231 || tag == DW_TAG_pointer_type
7232 || tag == DW_TAG_reference_type
7233 || tag == DW_TAG_rvalue_reference_type)
7234 {
7235 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7236 return false;
7237 }
7238 else
7239 return false;
7240
7241 memcpy(&peeled_die, die, sizeof(peeled_die));
7242
7243 while (tag == DW_TAG_const_type
7244 || tag == DW_TAG_volatile_type
7245 || tag == DW_TAG_restrict_type
7246 || tag == DW_TAG_pointer_type
7247 || tag == DW_TAG_reference_type
7248 || tag == DW_TAG_rvalue_reference_type)
7249 {
7250 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7251 break;
7252 tag = dwarf_tag(&peeled_die);
7253 }
7254
7255 return true;
7256}
7257
7258/// Return the leaf object under a qualified type DIE.
7259///
7260/// @param die the DIE of the type to consider.
7261///
7262/// @param peeled_die out parameter. Set to the DIE of the leaf
7263/// object iff the function actually peeled anything.
7264///
7265/// @return true upon successful completion.
7266static bool
7267die_peel_qualified(Dwarf_Die *die, Dwarf_Die& peeled_die)
7268{
7269 if (!die)
7270 return false;
7271
7272 memcpy(&peeled_die, die, sizeof(peeled_die));
7273
7274 int tag = dwarf_tag(&peeled_die);
7275
7276 bool result = false;
7277 while (tag == DW_TAG_const_type
7278 || tag == DW_TAG_volatile_type
7279 || tag == DW_TAG_restrict_type)
7280 {
7281 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7282 break;
7283 tag = dwarf_tag(&peeled_die);
7284 result = true;
7285 }
7286
7287 return result;
7288}
7289
7290/// Return the leaf object under a typedef type DIE.
7291///
7292/// @param die the DIE of the type to consider.
7293///
7294/// @param peeled_die out parameter. Set to the DIE of the leaf
7295/// object iff the function actually peeled anything.
7296///
7297/// @return true upon successful completion.
7298static bool
7299die_peel_typedef(Dwarf_Die *die, Dwarf_Die& peeled_die)
7300{
7301 if (!die)
7302 return false;
7303
7304 int tag = dwarf_tag(die);
7305
7306 memcpy(&peeled_die, die, sizeof(peeled_die));
7307
7308 if (tag == DW_TAG_typedef)
7309 {
7310 if (!die_die_attribute(die, DW_AT_type, peeled_die))
7311 return false;
7312 }
7313 else
7314 return false;
7315
7316 while (tag == DW_TAG_typedef)
7317 {
7318 if (!die_die_attribute(&peeled_die, DW_AT_type, peeled_die))
7319 break;
7320 tag = dwarf_tag(&peeled_die);
7321 }
7322
7323 return true;
7324
7325}
7326
7327/// Test if a DIE for a function type represents a method type.
7328///
7329/// @param rdr the DWARF reader.
7330///
7331/// @param die the DIE to consider.
7332///
7333/// @param where_offset where we logically are in the stream of DIEs.
7334///
7335/// @param object_pointer_die out parameter. This is set by the
7336/// function to the DIE that refers to the formal function parameter
7337/// which holds the implicit "this" pointer of the method. That die
7338/// is called the object pointer DIE. This is set iff the member
7339/// function is a non-static member function and if the function
7340/// returns true. In other words, this is only set if the is_static
7341/// out parameter is set to false and the function returns true.
7342///
7343/// @param class_die out parameter. This is set by the function to
7344/// the DIE that represents the class of the method type. This is set
7345/// iff the function returns true.
7346///
7347/// @param is_static out parameter. This is set to true by the
7348/// function if @p die is a static method or a the type of a static
7349/// method. This is set iff the function returns true.
7350///
7351/// @return true iff @p die is a DIE for a method type.
7352static bool
7353die_function_type_is_method_type(const reader& rdr,
7354 const Dwarf_Die *die,
7355 void* where_addr,
7356 reader::tu_context_type_sptr& tu_ctxt,
7357 Dwarf_Die& object_pointer_die,
7358 Dwarf_Die& class_die,
7359 bool& is_static)
7360{
7361 if (!die)
7362 return false;
7363
7364 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
7365 ABG_ASSERT(tag == DW_TAG_subroutine_type || tag == DW_TAG_subprogram);
7366
7367 if (member_fn_die_has_this_pointer(rdr, die, where_addr, tu_ctxt,
7368 class_die, object_pointer_die))
7369 {
7370 is_static = false;
7371 return true;
7372 }
7373 else if (die_is_at_class_scope(rdr, die, where_addr, tu_ctxt, class_die))
7374 {
7375 is_static = true;
7376 return true;
7377 }
7378
7379 return false;
7380}
7381
7382enum virtuality
7383{
7384 VIRTUALITY_NOT_VIRTUAL,
7385 VIRTUALITY_VIRTUAL,
7386 VIRTUALITY_PURE_VIRTUAL
7387};
7388
7389/// Get the virtual-ness of a given DIE, that is, the value of the
7390/// DW_AT_virtuality attribute.
7391///
7392/// @param die the DIE to read from.
7393///
7394/// @param virt the resulting virtuality attribute. This is set iff
7395/// the function returns true.
7396///
7397/// @return true if the virtual-ness could be determined.
7398static bool
7399die_virtuality(const Dwarf_Die* die, virtuality& virt)
7400{
7401 if (!die)
7402 return false;
7403
7404 uint64_t v = 0;
7405 die_unsigned_constant_attribute(die, DW_AT_virtuality, v);
7406
7407 if (v == DW_VIRTUALITY_virtual)
7408 virt = VIRTUALITY_VIRTUAL;
7409 else if (v == DW_VIRTUALITY_pure_virtual)
7410 virt = VIRTUALITY_PURE_VIRTUAL;
7411 else
7412 virt = VIRTUALITY_NOT_VIRTUAL;
7413
7414 return true;
7415}
7416
7417/// Test whether the DIE represent either a virtual base or function.
7418///
7419/// @param die the DIE to consider.
7420///
7421/// @return bool if the DIE represents a virtual base or function,
7422/// false othersise.
7423static bool
7424die_is_virtual(const Dwarf_Die* die)
7425{
7426 virtuality v;
7427 if (!die_virtuality(die, v))
7428 return false;
7429
7430 return v == VIRTUALITY_PURE_VIRTUAL || v == VIRTUALITY_VIRTUAL;
7431}
7432
7433/// Test if the DIE represents an entity that was declared inlined.
7434///
7435/// @param die the DIE to test for.
7436///
7437/// @return true if the DIE represents an entity that was declared
7438/// inlined.
7439static bool
7440die_is_declared_inline(Dwarf_Die* die)
7441{
7442 uint64_t inline_value = 0;
7443 if (!die_unsigned_constant_attribute(die, DW_AT_inline, inline_value))
7444 return false;
7445 return (inline_value == DW_INL_declared_inlined
7446 || inline_value == DW_INL_declared_not_inlined);
7447}
7448
7449// -----------------------------------
7450// <location expression evaluation>
7451// -----------------------------------
7452
7453/// Get the value of a given DIE attribute, knowing that it must be a
7454/// location expression.
7455///
7456/// @param die the DIE to read the attribute from.
7457///
7458/// @param attr_name the name of the attribute to read the value for.
7459///
7460/// @param expr the pointer to allocate and fill with the resulting
7461/// array of operators + operands forming a dwarf expression. This is
7462/// set iff the function returns true.
7463///
7464/// @param expr_len the length of the resulting dwarf expression.
7465/// This is set iff the function returns true.
7466///
7467/// @return true if the attribute exists and has a non-empty dwarf expression
7468/// as value. In that case the expr and expr_len arguments are set to the
7469/// resulting dwarf expression.
7470static bool
7471die_location_expr(const Dwarf_Die* die,
7472 unsigned attr_name,
7473 Dwarf_Op** expr,
7474 size_t* expr_len)
7475{
7476 if (!die)
7477 return false;
7478
7479 Dwarf_Attribute attr;
7480 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), attr_name, &attr))
7481 return false;
7482
7483 size_t len = 0;
7484 bool result = (dwarf_getlocation(&attr, expr, &len) == 0);
7485
7486 // Ignore location expressions where reading them succeeded but
7487 // their length is 0.
7488 result &= len > 0;
7489
7490 if (result)
7491 *expr_len = len;
7492
7493 return result;
7494}
7495
7496/// If the current operation in the dwarf expression represents a push
7497/// of a constant value onto the dwarf expr virtual machine (aka
7498/// DEVM), perform the operation and update the DEVM.
7499///
7500/// If the result of the operation is a constant, update the DEVM
7501/// accumulator with its value. Otherwise, the DEVM accumulator is
7502/// left with its previous value.
7503///
7504/// @param ops the array of the dwarf expression operations to consider.
7505///
7506/// @param ops_len the lengths of @p ops array above.
7507///
7508/// @param index the index of the operation to interpret, in @p ops.
7509///
7510/// @param next_index the index of the operation to interpret at the
7511/// next step, after this function completed and returned. This is
7512/// set an output parameter that is set iff the function returns true.
7513///
7514/// @param ctxt the DEVM evaluation context.
7515///
7516/// @return true if the current operation actually pushes a constant
7517/// value onto the DEVM stack, false otherwise.
7518static bool
7519op_pushes_constant_value(Dwarf_Op* ops,
7520 size_t ops_len,
7521 size_t index,
7522 size_t& next_index,
7523 dwarf_expr_eval_context& ctxt)
7524{
7525 ABG_ASSERT(index < ops_len);
7526
7527 Dwarf_Op& op = ops[index];
7528 int64_t value = 0;
7529
7530 switch (op.atom)
7531 {
7532 case DW_OP_addr:
7533 value = ops[index].number;
7534 break;
7535
7536 case DW_OP_const1u:
7537 case DW_OP_const1s:
7538 case DW_OP_const2u:
7539 case DW_OP_const2s:
7540 case DW_OP_const4u:
7541 case DW_OP_const4s:
7542 case DW_OP_const8u:
7543 case DW_OP_const8s:
7544 case DW_OP_constu:
7545 case DW_OP_consts:
7546 value = ops[index].number;
7547 break;
7548
7549 case DW_OP_lit0:
7550 value = 0;
7551 break;
7552 case DW_OP_lit1:
7553 value = 1;
7554 break;
7555 case DW_OP_lit2:
7556 value = 2;
7557 break;
7558 case DW_OP_lit3:
7559 value = 3;
7560 break;
7561 case DW_OP_lit4:
7562 value = 4;
7563 break;
7564 case DW_OP_lit5:
7565 value = 5;
7566 break;
7567 case DW_OP_lit6:
7568 value = 6;
7569 break;
7570 case DW_OP_lit7:
7571 value = 7;
7572 break;
7573 case DW_OP_lit8:
7574 value = 8;
7575 break;
7576 case DW_OP_lit9:
7577 value = 9;
7578 break;
7579 case DW_OP_lit10:
7580 value = 10;
7581 break;
7582 case DW_OP_lit11:
7583 value = 11;
7584 break;
7585 case DW_OP_lit12:
7586 value = 12;
7587 break;
7588 case DW_OP_lit13:
7589 value = 13;
7590 break;
7591 case DW_OP_lit14:
7592 value = 14;
7593 break;
7594 case DW_OP_lit15:
7595 value = 15;
7596 break;
7597 case DW_OP_lit16:
7598 value = 16;
7599 break;
7600 case DW_OP_lit17:
7601 value = 17;
7602 break;
7603 case DW_OP_lit18:
7604 value = 18;
7605 break;
7606 case DW_OP_lit19:
7607 value = 19;
7608 break;
7609 case DW_OP_lit20:
7610 value = 20;
7611 break;
7612 case DW_OP_lit21:
7613 value = 21;
7614 break;
7615 case DW_OP_lit22:
7616 value = 22;
7617 break;
7618 case DW_OP_lit23:
7619 value = 23;
7620 break;
7621 case DW_OP_lit24:
7622 value = 24;
7623 break;
7624 case DW_OP_lit25:
7625 value = 25;
7626 break;
7627 case DW_OP_lit26:
7628 value = 26;
7629 break;
7630 case DW_OP_lit27:
7631 value = 27;
7632 break;
7633 case DW_OP_lit28:
7634 value = 28;
7635 break;
7636 case DW_OP_lit29:
7637 value = 29;
7638 break;
7639 case DW_OP_lit30:
7640 value = 30;
7641 break;
7642 case DW_OP_lit31:
7643 value = 31;
7644 break;
7645
7646 default:
7647 return false;
7648 }
7649
7650 expr_result r(value);
7651 ctxt.push(r);
7652 ctxt.accum = r;
7653 next_index = index + 1;
7654
7655 return true;
7656}
7657
7658/// If the current operation in the dwarf expression represents a push
7659/// of a non-constant value onto the dwarf expr virtual machine (aka
7660/// DEVM), perform the operation and update the DEVM. A non-constant
7661/// is namely a quantity for which we need inferior (a running program
7662/// image) state to know the exact value.
7663///
7664/// Upon successful completion, as the result of the operation is a
7665/// non-constant the DEVM accumulator value is left to its state as of
7666/// before the invocation of this function.
7667///
7668/// @param ops the array of the dwarf expression operations to consider.
7669///
7670/// @param ops_len the lengths of @p ops array above.
7671///
7672/// @param index the index of the operation to interpret, in @p ops.
7673///
7674/// @param next_index the index of the operation to interpret at the
7675/// next step, after this function completed and returned. This is
7676/// set an output parameter that is set iff the function returns true.
7677///
7678/// @param ctxt the DEVM evaluation context.
7679///
7680/// @return true if the current operation actually pushes a
7681/// non-constant value onto the DEVM stack, false otherwise.
7682static bool
7683op_pushes_non_constant_value(Dwarf_Op* ops,
7684 size_t ops_len,
7685 size_t index,
7686 size_t& next_index,
7687 dwarf_expr_eval_context& ctxt)
7688{
7689 ABG_ASSERT(index < ops_len);
7690 Dwarf_Op& op = ops[index];
7691
7692 switch (op.atom)
7693 {
7694 case DW_OP_reg0:
7695 case DW_OP_reg1:
7696 case DW_OP_reg2:
7697 case DW_OP_reg3:
7698 case DW_OP_reg4:
7699 case DW_OP_reg5:
7700 case DW_OP_reg6:
7701 case DW_OP_reg7:
7702 case DW_OP_reg8:
7703 case DW_OP_reg9:
7704 case DW_OP_reg10:
7705 case DW_OP_reg11:
7706 case DW_OP_reg12:
7707 case DW_OP_reg13:
7708 case DW_OP_reg14:
7709 case DW_OP_reg15:
7710 case DW_OP_reg16:
7711 case DW_OP_reg17:
7712 case DW_OP_reg18:
7713 case DW_OP_reg19:
7714 case DW_OP_reg20:
7715 case DW_OP_reg21:
7716 case DW_OP_reg22:
7717 case DW_OP_reg23:
7718 case DW_OP_reg24:
7719 case DW_OP_reg25:
7720 case DW_OP_reg26:
7721 case DW_OP_reg27:
7722 case DW_OP_reg28:
7723 case DW_OP_reg29:
7724 case DW_OP_reg30:
7725 case DW_OP_reg31:
7726 next_index = index + 1;
7727 break;
7728
7729 case DW_OP_breg0:
7730 case DW_OP_breg1:
7731 case DW_OP_breg2:
7732 case DW_OP_breg3:
7733 case DW_OP_breg4:
7734 case DW_OP_breg5:
7735 case DW_OP_breg6:
7736 case DW_OP_breg7:
7737 case DW_OP_breg8:
7738 case DW_OP_breg9:
7739 case DW_OP_breg10:
7740 case DW_OP_breg11:
7741 case DW_OP_breg12:
7742 case DW_OP_breg13:
7743 case DW_OP_breg14:
7744 case DW_OP_breg15:
7745 case DW_OP_breg16:
7746 case DW_OP_breg17:
7747 case DW_OP_breg18:
7748 case DW_OP_breg19:
7749 case DW_OP_breg20:
7750 case DW_OP_breg21:
7751 case DW_OP_breg22:
7752 case DW_OP_breg23:
7753 case DW_OP_breg24:
7754 case DW_OP_breg25:
7755 case DW_OP_breg26:
7756 case DW_OP_breg27:
7757 case DW_OP_breg28:
7758 case DW_OP_breg29:
7759 case DW_OP_breg30:
7760 case DW_OP_breg31:
7761 next_index = index + 1;
7762 break;
7763
7764 case DW_OP_regx:
7765 next_index = index + 2;
7766 break;
7767
7768 case DW_OP_fbreg:
7769 next_index = index + 1;
7770 break;
7771
7772 case DW_OP_bregx:
7773 next_index = index + 1;
7774 break;
7775
7776 case DW_OP_GNU_variable_value:
7777 next_index = index + 1;
7778 break;
7779
7780 default:
7781 return false;
7782 }
7783
7784 expr_result r(false);
7785 ctxt.push(r);
7786
7787 return true;
7788}
7789
7790/// If the current operation in the dwarf expression represents a
7791/// manipulation of the stack of the DWARF Expression Virtual Machine
7792/// (aka DEVM), this function performs the operation and updates the
7793/// state of the DEVM. If the result of the operation represents a
7794/// constant value, then the accumulator of the DEVM is set to that
7795/// result's value, Otherwise, the DEVM accumulator is left with its
7796/// previous value.
7797///
7798/// @param expr the array of the dwarf expression operations to consider.
7799///
7800/// @param expr_len the lengths of @p ops array above.
7801///
7802/// @param index the index of the operation to interpret, in @p ops.
7803///
7804/// @param next_index the index of the operation to interpret at the
7805/// next step, after this function completed and returned. This is
7806/// set an output parameter that is set iff the function returns true.
7807///
7808/// @param ctxt the DEVM evaluation context.
7809///
7810/// @return true if the current operation actually manipulates the
7811/// DEVM stack, false otherwise.
7812static bool
7813op_manipulates_stack(Dwarf_Op* expr,
7814 size_t expr_len,
7815 size_t index,
7816 size_t& next_index,
7817 dwarf_expr_eval_context& ctxt)
7818{
7819 Dwarf_Op& op = expr[index];
7820 expr_result v;
7821
7822 switch (op.atom)
7823 {
7824 case DW_OP_dup:
7825 v = ctxt.stack.front();
7826 ctxt.push(v);
7827 break;
7828
7829 case DW_OP_drop:
7830 v = ctxt.stack.front();
7831 ctxt.pop();
7832 break;
7833
7834 case DW_OP_over:
7835 ABG_ASSERT(ctxt.stack.size() > 1);
7836 v = ctxt.stack[1];
7837 ctxt.push(v);
7838 break;
7839
7840 case DW_OP_pick:
7841 ABG_ASSERT(index + 1 < expr_len);
7842 v = op.number;
7843 ctxt.push(v);
7844 break;
7845
7846 case DW_OP_swap:
7847 ABG_ASSERT(ctxt.stack.size() > 1);
7848 v = ctxt.stack[1];
7849 ctxt.stack.erase(ctxt.stack.begin() + 1);
7850 ctxt.push(v);
7851 break;
7852
7853 case DW_OP_rot:
7854 ABG_ASSERT(ctxt.stack.size() > 2);
7855 v = ctxt.stack[2];
7856 ctxt.stack.erase(ctxt.stack.begin() + 2);
7857 ctxt.push(v);
7858 break;
7859
7860 case DW_OP_deref:
7861 case DW_OP_deref_size:
7862 ABG_ASSERT(ctxt.stack.size() > 0);
7863 ctxt.pop();
7864 v.is_const(false);
7865 ctxt.push(v);
7866 break;
7867
7868 case DW_OP_xderef:
7869 case DW_OP_xderef_size:
7870 ABG_ASSERT(ctxt.stack.size() > 1);
7871 ctxt.pop();
7872 ctxt.pop();
7873 v.is_const(false);
7874 ctxt.push(v);
7875 break;
7876
7877 case DW_OP_push_object_address:
7878 v.is_const(false);
7879 ctxt.push(v);
7880 break;
7881
7882 case DW_OP_form_tls_address:
7883 case DW_OP_GNU_push_tls_address:
7884 ABG_ASSERT(ctxt.stack.size() > 0);
7885 v = ctxt.pop();
7886 if (op.atom == DW_OP_form_tls_address)
7887 v.is_const(false);
7888 ctxt.push(v);
7889 break;
7890
7891 case DW_OP_call_frame_cfa:
7892 v.is_const(false);
7893 ctxt.push(v);
7894 break;
7895
7896 default:
7897 return false;
7898 }
7899
7900 if (v.is_const())
7901 ctxt.accum = v;
7902
7903 if (op.atom == DW_OP_form_tls_address
7904 || op.atom == DW_OP_GNU_push_tls_address)
7905 ctxt.set_tls_address(true);
7906 else
7907 ctxt.set_tls_address(false);
7908
7909 next_index = index + 1;
7910
7911 return true;
7912}
7913
7914/// If the current operation in the dwarf expression represents a push
7915/// of an arithmetic or logic operation onto the dwarf expr virtual
7916/// machine (aka DEVM), perform the operation and update the DEVM.
7917///
7918/// If the result of the operation is a constant, update the DEVM
7919/// accumulator with its value. Otherwise, the DEVM accumulator is
7920/// left with its previous value.
7921///
7922/// @param expr the array of the dwarf expression operations to consider.
7923///
7924/// @param expr_len the lengths of @p expr array above.
7925///
7926/// @param index the index of the operation to interpret, in @p expr.
7927///
7928/// @param next_index the index of the operation to interpret at the
7929/// next step, after this function completed and returned. This is
7930/// set an output parameter that is set iff the function returns true.
7931///
7932/// @param ctxt the DEVM evaluation context.
7933///
7934/// @return true if the current operation actually represent an
7935/// arithmetic or logic operation.
7936static bool
7937op_is_arith_logic(Dwarf_Op* expr,
7938 size_t expr_len,
7939 size_t index,
7940 size_t& next_index,
7941 dwarf_expr_eval_context& ctxt)
7942{
7943 ABG_ASSERT(index < expr_len);
7944
7945 Dwarf_Op& op = expr[index];
7946 expr_result val1, val2;
7947 bool result = false;
7948
7949 switch (op.atom)
7950 {
7951 case DW_OP_abs:
7952 ABG_ASSERT(ctxt.stack.size() > 0);
7953 val1 = ctxt.pop();
7954 val1 = val1.abs();
7955 ctxt.push(val1);
7956 result = true;
7957 break;
7958
7959 case DW_OP_and:
7960 ABG_ASSERT(ctxt.stack.size() > 1);
7961 val1 = ctxt.pop();
7962 val2 = ctxt.pop();
7963 ctxt.push(val1 & val2);
7964 break;
7965
7966 case DW_OP_div:
7967 ABG_ASSERT(ctxt.stack.size() > 1);
7968 val1 = ctxt.pop();
7969 val2 = ctxt.pop();
7970 if (!val1.is_const())
7971 val1 = 1;
7972 ctxt.push(val2 / val1);
7973 result = true;
7974 break;
7975
7976 case DW_OP_minus:
7977 ABG_ASSERT(ctxt.stack.size() > 1);
7978 val1 = ctxt.pop();
7979 val2 = ctxt.pop();
7980 ctxt.push(val2 - val1);
7981 result = true;
7982 break;
7983
7984 case DW_OP_mod:
7985 ABG_ASSERT(ctxt.stack.size() > 1);
7986 val1 = ctxt.pop();
7987 val2 = ctxt.pop();
7988 ctxt.push(val2 % val1);
7989 result = true;
7990 break;
7991
7992 case DW_OP_mul:
7993 ABG_ASSERT(ctxt.stack.size() > 1);
7994 val1 = ctxt.pop();
7995 val2 = ctxt.pop();
7996 ctxt.push(val2 * val1);
7997 result = true;
7998 break;
7999
8000 case DW_OP_neg:
8001 ABG_ASSERT(ctxt.stack.size() > 0);
8002 val1 = ctxt.pop();
8003 ctxt.push(-val1);
8004 result = true;
8005 break;
8006
8007 case DW_OP_not:
8008 ABG_ASSERT(ctxt.stack.size() > 0);
8009 val1 = ctxt.pop();
8010 ctxt.push(~val1);
8011 result = true;
8012 break;
8013
8014 case DW_OP_or:
8015 ABG_ASSERT(ctxt.stack.size() > 1);
8016 val1 = ctxt.pop();
8017 val2 = ctxt.pop();
8018 ctxt.push(val1 | val2);
8019 result = true;
8020 break;
8021
8022 case DW_OP_plus:
8023 ABG_ASSERT(ctxt.stack.size() > 1);
8024 val1 = ctxt.pop();
8025 val2 = ctxt.pop();
8026 ctxt.push(val2 + val1);
8027 result = true;
8028 break;
8029
8030 case DW_OP_plus_uconst:
8031 ABG_ASSERT(ctxt.stack.size() > 0);
8032 val1 = ctxt.pop();
8033 val1 += op.number;
8034 ctxt.push(val1);
8035 result = true;
8036 break;
8037
8038 case DW_OP_shl:
8039 ABG_ASSERT(ctxt.stack.size() > 1);
8040 val1 = ctxt.pop();
8041 val2 = ctxt.pop();
8042 ctxt.push(val2 << val1);
8043 result = true;
8044 break;
8045
8046 case DW_OP_shr:
8047 case DW_OP_shra:
8048 ABG_ASSERT(ctxt.stack.size() > 1);
8049 val1 = ctxt.pop();
8050 val2 = ctxt.pop();
8051 ctxt.push(val2 >> val1);
8052 result = true;
8053 break;
8054
8055 case DW_OP_xor:
8056 ABG_ASSERT(ctxt.stack.size() > 1);
8057 val1 = ctxt.pop();
8058 val2 = ctxt.pop();
8059 ctxt.push(val2 ^ val1);
8060 result = true;
8061 break;
8062
8063 default:
8064 break;
8065 }
8066
8067 if (result == true)
8068 {
8069 if (ctxt.stack.front().is_const())
8070 ctxt.accum = ctxt.stack.front();
8071
8072 next_index = index + 1;
8073 }
8074 return result;;
8075}
8076
8077/// If the current operation in the dwarf expression represents a push
8078/// of a control flow operation onto the dwarf expr virtual machine
8079/// (aka DEVM), perform the operation and update the DEVM.
8080///
8081/// If the result of the operation is a constant, update the DEVM
8082/// accumulator with its value. Otherwise, the DEVM accumulator is
8083/// left with its previous value.
8084///
8085/// @param expr the array of the dwarf expression operations to consider.
8086///
8087/// @param expr_len the lengths of @p expr array above.
8088///
8089/// @param index the index of the operation to interpret, in @p expr.
8090///
8091/// @param next_index the index of the operation to interpret at the
8092/// next step, after this function completed and returned. This is
8093/// set an output parameter that is set iff the function returns true.
8094///
8095/// @param ctxt the DEVM evaluation context.
8096///
8097/// @return true if the current operation actually represents a
8098/// control flow operation, false otherwise.
8099static bool
8100op_is_control_flow(Dwarf_Op* expr,
8101 size_t expr_len,
8102 size_t index,
8103 size_t& next_index,
8104 dwarf_expr_eval_context& ctxt)
8105{
8106 ABG_ASSERT(index < expr_len);
8107
8108 Dwarf_Op& op = expr[index];
8109 expr_result val1, val2;
8110
8111 switch (op.atom)
8112 {
8113 case DW_OP_eq:
8114 case DW_OP_ge:
8115 case DW_OP_gt:
8116 case DW_OP_le:
8117 case DW_OP_lt:
8118 case DW_OP_ne:
8119 {
8120 bool value = true;
8121 val1 = ctxt.pop();
8122 val2 = ctxt.pop();
8123 if (op.atom == DW_OP_eq)
8124 value = val2 == val1;
8125 else if (op.atom == DW_OP_ge)
8126 value = val2 >= val1;
8127 else if (op.atom == DW_OP_gt)
8128 value = val2 > val1;
8129 else if (op.atom == DW_OP_le)
8130 value = val2 <= val1;
8131 else if (op.atom == DW_OP_lt)
8132 value = val2 < val1;
8133 else if (op.atom == DW_OP_ne)
8134 value = val2 != val1;
8135
8136 val1 = value ? 1 : 0;
8137 ctxt.push(val1);
8138 }
8139 break;
8140
8141 case DW_OP_skip:
8142 if (op.number > 0)
8143 index += op.number - 1;
8144 break;
8145
8146 case DW_OP_bra:
8147 val1 = ctxt.pop();
8148 if (val1.const_value() != 0)
8149 index += val1.const_value() - 1;
8150 break;
8151
8152 case DW_OP_call2:
8153 case DW_OP_call4:
8154 case DW_OP_call_ref:
8155 case DW_OP_nop:
8156 break;
8157
8158 default:
8159 return false;
8160 }
8161
8162 if (ctxt.stack.front().is_const())
8163 ctxt.accum = ctxt.stack.front();
8164
8165 next_index = index + 1;
8166 return true;
8167}
8168
8169/// This function quickly evaluates a DWARF expression that is a
8170/// constant.
8171///
8172/// This is a "fast path" function that quickly evaluates a DWARF
8173/// expression that is only made of a DW_OP_plus_uconst operator.
8174///
8175/// This is a sub-routine of die_member_offset.
8176///
8177/// @param expr the DWARF expression to evaluate.
8178///
8179/// @param expr_len the length of the expression @p expr.
8180///
8181/// @param value out parameter. This is set to the result of the
8182/// evaluation of @p expr, iff this function returns true.
8183///
8184/// @return true iff the evaluation of @p expr went OK.
8185static bool
8186eval_quickly(Dwarf_Op* expr,
8187 uint64_t expr_len,
8188 int64_t& value)
8189{
8190 if (expr_len == 1 && (expr[0].atom == DW_OP_plus_uconst))
8191 {
8192 value = expr[0].number;
8193 return true;
8194 }
8195 return false;
8196}
8197
8198/// Evaluate the value of the last sub-expression that is a constant,
8199/// inside a given DWARF expression.
8200///
8201/// @param expr the DWARF expression to consider.
8202///
8203/// @param expr_len the length of the expression to consider.
8204///
8205/// @param value the resulting value of the last constant
8206/// sub-expression of the DWARF expression. This is set iff the
8207/// function returns true.
8208///
8209/// @param is_tls_address out parameter. This is set to true iff
8210/// the resulting value of the evaluation is a TLS (thread local
8211/// storage) address.
8212///
8213/// @param eval_ctxt the evaluation context to (re)use. Note that
8214/// this function initializes this context before using it.
8215///
8216/// @return true if the function could find a constant sub-expression
8217/// to evaluate, false otherwise.
8218static bool
8219eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8220 size_t expr_len,
8221 int64_t& value,
8222 bool& is_tls_address,
8223 dwarf_expr_eval_context &eval_ctxt)
8224{
8225 // Reset the evaluation context before evaluating the constant sub
8226 // expression contained in the DWARF expression 'expr'.
8227 eval_ctxt.reset();
8228
8229 size_t index = 0, next_index = 0;
8230 do
8231 {
8232 if (op_is_arith_logic(expr, expr_len, index,
8233 next_index, eval_ctxt)
8234 || op_pushes_constant_value(expr, expr_len, index,
8235 next_index, eval_ctxt)
8236 || op_manipulates_stack(expr, expr_len, index,
8237 next_index, eval_ctxt)
8238 || op_pushes_non_constant_value(expr, expr_len, index,
8239 next_index, eval_ctxt)
8240 || op_is_control_flow(expr, expr_len, index,
8241 next_index, eval_ctxt))
8242 ;
8243 else
8244 next_index = index + 1;
8245
8246 ABG_ASSERT(next_index > index);
8247 index = next_index;
8248 } while (index < expr_len);
8249
8250 is_tls_address = eval_ctxt.set_tls_address();
8251 if (eval_ctxt.accum.is_const())
8252 {
8253 value = eval_ctxt.accum;
8254 return true;
8255 }
8256 return false;
8257}
8258
8259/// Evaluate the value of the last sub-expression that is a constant,
8260/// inside a given DWARF expression.
8261///
8262/// @param expr the DWARF expression to consider.
8263///
8264/// @param expr_len the length of the expression to consider.
8265///
8266/// @param value the resulting value of the last constant
8267/// sub-expression of the DWARF expression. This is set iff the
8268/// function returns true.
8269///
8270/// @return true if the function could find a constant sub-expression
8271/// to evaluate, false otherwise.
8272static bool
8273eval_last_constant_dwarf_sub_expr(Dwarf_Op* expr,
8274 size_t expr_len,
8275 int64_t& value,
8276 bool& is_tls_address)
8277{
8278 dwarf_expr_eval_context eval_ctxt;
8279 return eval_last_constant_dwarf_sub_expr(expr, expr_len, value,
8280 is_tls_address, eval_ctxt);
8281}
8282
8283// -----------------------------------
8284// </location expression evaluation>
8285// -----------------------------------
8286
8287/// Convert a DW_AT_bit_offset attribute value into the same value as
8288/// DW_AT_data_bit_offset - 8 * DW_AT_data_member_location.
8289///
8290/// On big endian machines, the value of the DW_AT_bit_offset
8291/// attribute + 8 * the value of the DW_AT_data_member_location
8292/// attribute is the same as the value of the DW_AT_data_bit_offset
8293/// attribute.
8294///
8295/// On little endian machines however, the situation is different.
8296/// The DW_AT_bit_offset value for a bit field is the number of bits
8297/// to the left of the most significant bit of the bit field, within
8298/// the integer value at DW_AT_data_member_location.
8299///
8300/// The DW_AT_data_bit_offset offset value is the number of bits to
8301/// the right of the least significant bit of the bit field, again
8302/// relative to the containing integer value.
8303///
8304/// In other words, DW_AT_data_bit_offset is what everybody would
8305/// instinctively think of as being the "offset of the bit field". 8 *
8306/// DW_AT_data_member_location + DW_AT_bit_offset however is very
8307/// counter-intuitive on little endian machines.
8308///
8309/// This function thus reads the value of a DW_AT_bit_offset property
8310/// of a DIE and converts it into what the DW_AT_data_bit_offset would
8311/// have been if it was present, ignoring the contribution of
8312/// DW_AT_data_member_location.
8313///
8314/// Note that DW_AT_bit_offset has been made obsolete starting from
8315/// DWARF5 (for GCC; Clang still emits it).
8316///
8317/// If you like coffee and it's not too late, now might be a good time
8318/// to have a coffee break. Otherwise if it's late at night, you
8319/// might want to consider an herbal tea break. Then come back to
8320/// read this.
8321///
8322///
8323/// In what follows, the bit fields are all contained within the first
8324/// whole int of the struct, so DW_AT_data_member_location is 0.
8325///
8326/// Okay, to have a better idea of what DW_AT_bit_offset and
8327/// DW_AT_data_bit_offset represent, let's consider a struct 'S' which
8328/// have bit fields data members defined as:
8329///
8330/// struct S
8331/// {
8332/// int j:5;
8333/// int k:6;
8334/// int m:5;
8335/// int n:8;
8336/// };
8337///
8338/// The below wonderful (at least!) ASCII art sketch describes the
8339/// layout of the bitfields of 'struct S' on a little endian machine.
8340/// You need to read the sketch from the bottom-up.
8341///
8342/// So please scroll down to its bottom. Note how the 32 bits integer
8343/// word containing the bit fields is laid out with its least
8344/// significant bit starting on the right hand side, at index 0.
8345///
8346/// Then slowly scroll up starting from there, and take the time to
8347/// read each line and see how the bit fields are laid out and what
8348/// DW_AT_bit_offset and DW_AT_data_bit_offset represent for each of
8349/// the bit fields.
8350///
8351/// DW_AT_bit_offset(n)
8352/// < - - - - - - >
8353/// | | n |
8354/// ^ ^< - - - - >^
8355/// DW_AT_data_bit_offset(n)
8356/// < - - - - - - - - - - - - - - - >
8357/// | |
8358/// ^ ^
8359/// DW_AT_bit_offset(m)
8360/// <--------------------------------->
8361/// | | m |
8362/// ^ ^< - >^
8363/// DW_AT_data_bit_offset(m)
8364/// < - - - - - - - - - - >
8365/// | |
8366/// ^ ^
8367/// DW_AT_bit_offset(k)
8368/// <-------------------------------------------->
8369/// | | k |
8370/// ^ ^< - - >^
8371/// DW_AT_data_bit_offset(k)
8372/// < - - - - >
8373/// | |
8374/// ^ ^
8375/// DW_AT_bit_offset(j)
8376/// <-------------------------------------------------------->
8377/// | |
8378/// ^ ^
8379/// n m k j
8380/// < - - - - - - > < - - - > < - - - - > < - - - >
8381///
8382/// | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
8383/// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
8384/// 31 27 23 16 15 11 10 6 5 4 0
8385///
8386/// So, the different bit fields all fit in one 32 bits word, assuming
8387/// the bit fields are tightly packed.
8388///
8389/// Let's look at what DW_AT_bit_offset of the 'j' bit field would be
8390/// on this little endian machine and let's see how it relates to
8391/// DW_AT_data_bit_offset of j.
8392///
8393/// DW_AT_bit_offset(j) would be equal to the number of bits from the
8394/// left of the 32 bits word (i.e from bit number 31) to the most
8395/// significant bit of the j bit field (i.e, bit number 4). Thus:
8396///
8397/// DW_AT_bit_offset(j) =
8398/// sizeof_in_bits(int) - size_in_bits_of(j) = 32 - 5 = 27.
8399///
8400/// DW_AT_data_bit_offset(j) is the number of bits from the right of the
8401/// 32 bits word (i.e, bit number 0) to the lest significant bit of
8402/// the 'j' bit field (ie, bit number 0). Thus:
8403///
8404/// DW_AT_data_bit_offset(j) = 0.
8405///
8406/// More generally, we can notice that:
8407///
8408/// sizeof_in_bits(int) =
8409/// DW_AT_bit_offset(j) + sizeof_in_bits(j) + DW_AT_data_bit_offset(j).
8410///
8411/// It follows that:
8412///
8413/// DW_AT_data_bit_offset(j) =
8414/// sizeof_in_bits(int) - sizeof_in_bits(j) - DW_AT_bit_offset(j);
8415///
8416/// Thus:
8417///
8418/// DW_AT_data_bit_offset(j) = 32 - 27 - 5 = 0;
8419///
8420/// Note that DW_AT_data_bit_offset(j) is the offset of 'j' starting
8421/// from the right hand side of the word. It is what we would
8422/// intuitively think it is. DW_AT_bit_offset however is super
8423/// counter-intuitive, pfff.
8424///
8425/// Anyway, this general equation holds true for all bit fields.
8426///
8427/// Similarly, it follows that:
8428///
8429/// DW_AT_bit_offset(k) =
8430/// sizeof_in_bits(int) - sizeof_in_bits(k) - DW_AT_data_bit_offset(k);
8431///
8432/// Thus:
8433/// DW_AT_bit_offset(k) = 32 - 6 - 5 = 21.
8434///
8435///
8436/// Likewise:
8437///
8438/// DW_AT_bit_offset(m) =
8439/// sizeof_in_bits(int) - sizeof_in_bits(m) - DW_AT_data_bit_offset(m);
8440///
8441///
8442/// Thus:
8443/// DW_AT_bit_offset(m) = 32 - 5 - (5 + 6) = 16.
8444///
8445/// And:
8446///
8447///
8448/// Lastly:
8449///
8450/// DW_AT_bit_offset(n) =
8451/// sizeof_in_bits(int) - sizeof_in_bits(n) - DW_AT_bit_offset(n);
8452///
8453/// Thus:
8454/// DW_AT_bit_offset(n) = 32 - 8 - (5 + 6 + 5) = 8.
8455///
8456/// Luckily, the body of the function is much smaller than this
8457/// comment. Enjoy!
8458///
8459/// @param die the DIE to consider.
8460///
8461/// @param is_big_endian this is true iff the machine we are looking at
8462/// is big endian.
8463///
8464/// @param offset this is the output parameter into which the value of
8465/// the DW_AT_bit_offset is put, converted as if it was the value of
8466/// the DW_AT_data_bit_offset parameter, less the contribution of
8467/// DW_AT_data_member_location. This parameter is set iff the
8468/// function returns true.
8469///
8470/// @return true if DW_AT_bit_offset was found on @p die.
8471static bool
8472read_and_convert_DW_at_bit_offset(const Dwarf_Die* die,
8473 bool is_big_endian,
8474 uint64_t &offset)
8475{
8476 uint64_t off = 0;
8477 if (!die_unsigned_constant_attribute(die, DW_AT_bit_offset, off))
8478 return false;
8479
8480 if (is_big_endian)
8481 {
8482 offset = off;
8483 return true;
8484 }
8485
8486 // Okay, we are looking at a little endian machine. We need to
8487 // convert DW_AT_bit_offset into what DW_AT_data_bit_offset would
8488 // have been. To understand this, you really need to read the
8489 // preliminary comment of this function.
8490 uint64_t containing_anonymous_object_size = 0;
8491 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_byte_size,
8492 containing_anonymous_object_size));
8493 containing_anonymous_object_size *= 8;
8494
8495 uint64_t bitfield_size = 0;
8496 ABG_ASSERT(die_unsigned_constant_attribute(die, DW_AT_bit_size,
8497 bitfield_size));
8498
8499 // As noted in the the preliminary comment of this function if we
8500 // want to get the DW_AT_data_bit_offset of a bit field 'k' from the
8501 // its DW_AT_bit_offset value, the equation is:
8502 //
8503 // DW_AT_data_bit_offset(k) =
8504 // sizeof_in_bits(containing_anonymous_object_size)
8505 // - DW_AT_data_bit_offset(k)
8506 // - sizeof_in_bits(k)
8507 offset = containing_anonymous_object_size - off - bitfield_size;
8508
8509 return true;
8510}
8511
8512/// Get the value of the DW_AT_data_member_location of the given DIE
8513/// attribute as an constant.
8514///
8515/// @param die the DIE to read the attribute from.
8516///
8517/// @param offset the attribute as a constant value. This is set iff
8518/// the function returns true.
8519///
8520/// @return true if the attribute exists and has a constant value. In
8521/// that case the offset is set to the value.
8522static bool
8523die_constant_data_member_location(const Dwarf_Die *die,
8524 int64_t& offset)
8525{
8526 if (!die)
8527 return false;
8528
8529 Dwarf_Attribute attr;
8530 if (!dwarf_attr(const_cast<Dwarf_Die*>(die),
8531 DW_AT_data_member_location,
8532 &attr))
8533 return false;
8534
8535 Dwarf_Word val;
8536 if (dwarf_formudata(&attr, &val) != 0)
8537 return false;
8538
8539 offset = val;
8540 return true;
8541}
8542
8543/// Get the offset of a struct/class member as represented by the
8544/// value of the DW_AT_data_member_location attribute.
8545///
8546/// There is a huge gotcha in here. The value of the
8547/// DW_AT_data_member_location is not necessarily a constant that one
8548/// would just read and be done with it. Rather, it can be a DWARF
8549/// expression that one has to interpret. In general, the offset can
8550/// be given by the DW_AT_data_bit_offset or by the
8551/// DW_AT_data_member_location attribute and optionally the
8552/// DW_AT_bit_offset attribute. The bit offset attributes are
8553/// always simple constants, but the DW_AT_data_member_location
8554/// attribute is a DWARF location expression.
8555///
8556/// When it's the DW_AT_data_member_location that is present,
8557/// there are three cases to possibly take into account:
8558///
8559/// 1/ The offset in the vtable where the offset of a virtual base
8560/// can be found, aka vptr offset. Given the address of a
8561/// given object O, the vptr offset for B is given by the
8562/// (DWARF) expression:
8563///
8564/// address(O) + *(*address(0) - VIRTUAL_OFFSET)
8565///
8566/// where VIRTUAL_OFFSET is a constant value; In this case,
8567/// this function returns the constant VIRTUAL_OFFSET, as this
8568/// is enough to detect changes in a given virtual base
8569/// relative to the other virtual bases.
8570///
8571/// 2/ The offset of a regular data member. Given the address of
8572/// a struct object named O, the memory location for a
8573/// particular data member is given by the (DWARF) expression:
8574///
8575/// address(O) + OFFSET
8576///
8577/// where OFFSET is a constant. In this case, this function
8578/// returns the OFFSET constant.
8579///
8580/// 3/ The offset of a virtual member function in the virtual
8581/// pointer. The DWARF expression is a constant that designates
8582/// the offset of the function in the vtable. In this case this
8583/// function returns that constant.
8584///
8585/// @param rdr the DWARF reader to consider.
8586///
8587/// @param die the DIE to read the information from.
8588///
8589/// @param offset the resulting constant offset, in bits. This
8590/// argument is set iff the function returns true.
8591static bool
8592die_member_offset(const reader& rdr,
8593 const Dwarf_Die* die,
8594 int64_t& offset)
8595{
8596 Dwarf_Op* expr = NULL;
8597 size_t expr_len = 0;
8598 uint64_t bit_offset = 0;
8599
8600 // First let's see if the DW_AT_data_bit_offset attribute is
8601 // present.
8602 if (die_unsigned_constant_attribute(die, DW_AT_data_bit_offset, bit_offset))
8603 {
8604 offset = bit_offset;
8605 return true;
8606 }
8607
8608 // First try to read DW_AT_data_member_location as a plain constant.
8609 // We do this because the generic method using die_location_expr
8610 // might hit a bug in elfutils libdw dwarf_location_expression only
8611 // fixed in elfutils 0.184+. The bug only triggers if the attribute
8612 // is expressed as a (DWARF 5) DW_FORM_implicit_constant. But we
8613 // handle all constants here because that is more consistent (and
8614 // slightly faster in the general case where the attribute isn't a
8615 // full DWARF expression).
8616 if (!die_constant_data_member_location(die, offset))
8617 {
8618 // Otherwise, let's see if the DW_AT_data_member_location
8619 // attribute and, optionally, the DW_AT_bit_offset attributes
8620 // are present.
8621 if (!die_location_expr(die, DW_AT_data_member_location,
8622 &expr, &expr_len))
8623 return false;
8624
8625 // The DW_AT_data_member_location attribute is present. Let's
8626 // evaluate it and get its constant sub-expression and return
8627 // that one.
8628 if (!eval_quickly(expr, expr_len, offset))
8629 {
8630 bool is_tls_address = false;
8631 dwarf_expr_eval_context eval_ctxt;
8632 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len,
8633 offset, is_tls_address,
8634 eval_ctxt))
8635 return false;
8636 }
8637 }
8638 offset *= 8;
8639
8640 // On little endian machines, we need to convert the
8641 // DW_AT_bit_offset attribute into a relative offset to 8 *
8642 // DW_AT_data_member_location equal to what DW_AT_data_bit_offset
8643 // would be if it were used instead.
8644 //
8645 // In other words, before adding it to 8 *
8646 // DW_AT_data_member_location, DW_AT_bit_offset needs to be
8647 // converted into a human-understandable form that represents the
8648 // offset of the bitfield data member it describes. For details
8649 // about the conversion, please read the extensive comments of
8650 // read_and_convert_DW_at_bit_offset.
8651 bool is_big_endian = architecture_is_big_endian(rdr.elf_handle());
8652 if (read_and_convert_DW_at_bit_offset(die, is_big_endian, bit_offset))
8653 offset += bit_offset;
8654
8655 return true;
8656}
8657
8658/// Read the value of the DW_AT_location attribute from a DIE,
8659/// evaluate the resulting DWARF expression and, if it's a constant
8660/// expression, return it.
8661///
8662/// @param die the DIE to consider.
8663///
8664/// @param address the resulting constant address. This is set iff
8665/// the function returns true.
8666///
8667/// @return true iff the whole sequence of action described above
8668/// could be completed normally.
8669static bool
8670die_location_address(Dwarf_Die* die,
8671 Dwarf_Addr& address,
8672 bool& is_tls_address)
8673{
8674 Dwarf_Op* expr = NULL;
8675 size_t expr_len = 0;
8676
8677 is_tls_address = false;
8678
8679 if (!die)
8680 return false;
8681
8682 Dwarf_Attribute attr;
8683 if (!dwarf_attr_integrate(const_cast<Dwarf_Die*>(die), DW_AT_location, &attr))
8684 return false;
8685
8686 if (dwarf_getlocation(&attr, &expr, &expr_len))
8687 return false;
8688 // Ignore location expressions where reading them succeeded but
8689 // their length is 0.
8690 if (expr_len == 0)
8691 return false;
8692
8693 Dwarf_Attribute result;
8694 if (!dwarf_getlocation_attr(&attr, expr, &result))
8695 // A location that has been interpreted as an address.
8696 return !dwarf_formaddr(&result, &address);
8697
8698 // Just get the address out of the number field.
8699 address = expr->number;
8700 return true;
8701}
8702
8703/// Return the index of a function in its virtual table. That is,
8704/// return the value of the DW_AT_vtable_elem_location attribute.
8705///
8706/// @param die the DIE of the function to consider.
8707///
8708/// @param vindex the resulting index. This is set iff the function
8709/// returns true.
8710///
8711/// @return true if the DIE has a DW_AT_vtable_elem_location
8712/// attribute.
8713static bool
8714die_virtual_function_index(Dwarf_Die* die,
8715 int64_t& vindex)
8716{
8717 if (!die)
8718 return false;
8719
8720 Dwarf_Op* expr = NULL;
8721 size_t expr_len = 0;
8722 if (die_is_virtual(die))
8723 vindex = 0;
8724 if (!die_location_expr(die, DW_AT_vtable_elem_location,
8725 &expr, &expr_len))
8726 return false;
8727
8728 int64_t i = 0;
8729 bool is_tls_addr = false;
8730 if (!eval_last_constant_dwarf_sub_expr(expr, expr_len, i, is_tls_addr))
8731 return false;
8732
8733 vindex = i;
8734 return true;
8735}
8736
8737/// Test if a given DIE represents an anonymous type.
8738///
8739/// Anonymous types we are interested in are classes, unions and
8740/// enumerations.
8741///
8742/// @param die the DIE to consider.
8743///
8744/// @return true iff @p die represents an anonymous type.
8745bool
8747{
8748 int tag = dwarf_tag(die);
8749
8750 if (tag == DW_TAG_class_type
8751 || tag == DW_TAG_structure_type
8752 || tag == DW_TAG_union_type
8753 || tag == DW_TAG_enumeration_type)
8754 return die_is_anonymous(die);
8755
8756 return false;
8757}
8758
8759/// Return the base of the internal name to represent an anonymous
8760/// type.
8761///
8762/// Typically, anonymous enums would be named
8763/// __anonymous_enum__<number>, anonymous struct or classes would be
8764/// named __anonymous_struct__<number> and anonymous unions would be
8765/// named __anonymous_union__<number>. The first part of these
8766/// anonymous names (i.e, __anonymous_{enum,struct,union}__ is called
8767/// the base name. This function returns that base name, depending on
8768/// the kind of type DIE we are looking at.
8769///
8770/// @param die the type DIE to look at. This function expects a type
8771/// DIE with an empty DW_AT_name property value (anonymous).
8772///
8773/// @return a string representing the base of the internal anonymous
8774/// name.
8775static string
8776get_internal_anonymous_die_prefix_name(const Dwarf_Die *die)
8777{
8778 ABG_ASSERT(die_is_type(die));
8779 ABG_ASSERT(die_string_attribute(die, DW_AT_name) == "");
8780
8781 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
8782 string type_name;
8783 if (tag == DW_TAG_class_type || tag == DW_TAG_structure_type)
8785 else if (tag == DW_TAG_union_type)
8787 else if (tag == DW_TAG_enumeration_type)
8789
8790 return type_name;
8791}
8792
8793// ------------------------------------
8794// <DIE pretty printer>
8795// ------------------------------------
8796
8797/// Compute the qualified name of a DIE that represents a type.
8798///
8799/// For instance, if the DIE tag is DW_TAG_subprogram then this
8800/// function computes the name of the function *type*.
8801///
8802/// @param rdr the DWARF reader.
8803///
8804/// @param die the DIE to consider.
8805///
8806/// @param where_offset where in the are logically are in the DIE
8807/// stream.
8808///
8809/// @param guard the set of DIE addresses of the stack of DIEs involved
8810/// in the construction of the qualified name of the type. This set
8811/// is used to detect (and avoid) cycles in the stack of DIEs that is
8812/// going to be walked to compute the qualified type name.
8813///
8814/// @return a copy of the qualified name of the type.
8815string
8816die_qualified_type_name(const reader& rdr,
8817 const Dwarf_Die* die,
8818 void* where,
8819 reader::tu_context_type_sptr& tu_ctxt,
8820 unordered_set<void*>& guard)
8821{
8822 if (!die)
8823 return "";
8824
8825 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
8826 if (tag == DW_TAG_compile_unit
8827 || tag == DW_TAG_partial_unit
8828 || tag == DW_TAG_type_unit)
8829 return "";
8830
8831 string name = die_name(die);
8832
8833 Dwarf_Die scope_die;
8834 if (!rdr.get_scope_die(die, where, tu_ctxt, scope_die))
8835 return "";
8836
8837 bool colon_colon = die_is_type(die) || die_is_namespace(die);
8838 string separator = colon_colon ? "::" : ".";
8839
8840 string repr;
8841
8842 switch (tag)
8843 {
8844 case DW_TAG_unspecified_type:
8845 break;
8846
8847 case DW_TAG_base_type:
8848 {
8850 if (parse_real_type(name, real_type))
8851 repr = real_type;
8852 else
8853 repr = name;
8854 }
8855 break;
8856
8857 case DW_TAG_typedef:
8858 ABG_ASSERT(!name.empty());
8859 // fall through
8860
8861 case DW_TAG_enumeration_type:
8862 case DW_TAG_structure_type:
8863 case DW_TAG_class_type:
8864 case DW_TAG_union_type:
8865 {
8866 if (die_is_anonymous(die))
8867 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
8868 /*one_line=*/true,
8869 /*qualed_name=*/false,
8870 where, tu_ctxt, guard);
8871 else
8872 {
8873 string parent_name = die_qualified_name(rdr, &scope_die,
8874 where, tu_ctxt, guard);
8875 repr = parent_name.empty() ? name : parent_name + separator + name;
8876 }
8877 }
8878 break;
8879
8880 case DW_TAG_const_type:
8881 case DW_TAG_volatile_type:
8882 case DW_TAG_restrict_type:
8883 {
8884 Dwarf_Die underlying_type_die;
8885 bool has_underlying_type_die =
8886 die_die_attribute(die, DW_AT_type, underlying_type_die);
8887
8888 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
8889 break;
8890
8891 if (tag == DW_TAG_const_type)
8892 {
8893 if (has_underlying_type_die
8894 && die_is_reference_type(&underlying_type_die))
8895 // A reference is always const. So, to lower false
8896 // positive reports in diff computations, we consider a
8897 // const reference just as a reference. But we need to
8898 // keep the qualified-ness of the type. So we introduce
8899 // a 'no-op' qualifier here. Please remember that this
8900 // has to be kept in sync with what is done in
8901 // get_name_of_qualified_type. So if you change this
8902 // here, you have to change that code there too.
8903 repr = "";
8904 else if (!has_underlying_type_die
8905 || die_is_void_type(&underlying_type_die))
8906 {
8907 repr = "void";
8908 break;
8909 }
8910 else
8911 repr = "const";
8912 }
8913 else if (tag == DW_TAG_volatile_type)
8914 repr = "volatile";
8915 else if (tag == DW_TAG_restrict_type)
8916 repr = "restrict";
8917 else
8919
8920 string underlying_type_repr;
8921 if (has_underlying_type_die)
8922 underlying_type_repr =
8923 die_qualified_type_name(rdr, &underlying_type_die,
8924 where, tu_ctxt, guard);
8925 else
8926 underlying_type_repr = "void";
8927
8928 if (underlying_type_repr.empty())
8929 repr.clear();
8930 else
8931 {
8932 if (has_underlying_type_die)
8933 {
8934 Dwarf_Die peeled;
8935 die_peel_qualified(&underlying_type_die, peeled);
8936 if (die_is_pointer_or_reference_type(&peeled))
8937 repr = underlying_type_repr + " " + repr;
8938 else
8939 repr += " " + underlying_type_repr;
8940 }
8941 else
8942 repr += " " + underlying_type_repr;
8943 }
8944 }
8945 break;
8946
8947 case DW_TAG_pointer_type:
8948 case DW_TAG_reference_type:
8949 case DW_TAG_rvalue_reference_type:
8950 {
8951 Dwarf_Die pointed_to_type_die;
8952 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
8953 {
8954 if (tag == DW_TAG_pointer_type)
8955 repr = "void*";
8956 break;
8957 }
8958
8959 if (die_is_unspecified(&pointed_to_type_die))
8960 break;
8961
8962 string pointed_type_repr =
8963 die_qualified_type_name(rdr, &pointed_to_type_die,
8964 where, tu_ctxt, guard);
8965
8966 repr = pointed_type_repr;
8967 if (repr.empty())
8968 break;
8969
8970 if (tag == DW_TAG_pointer_type)
8971 repr += "*";
8972 else if (tag == DW_TAG_reference_type)
8973 repr += "&";
8974 else if (tag == DW_TAG_rvalue_reference_type)
8975 repr += "&&";
8976 else
8978 }
8979 break;
8980
8981 case DW_TAG_subrange_type:
8982 {
8983 // In Ada, this one can be generated on its own, that is, not
8984 // as a sub-type of an array. So we need to support it on its
8985 // own. Note that when it's emitted as the sub-type of an
8986 // array like in C and C++, this is handled differently, for
8987 // now. But we try to make this usable by other languages
8988 // that are not Ada, even if we modelled it after Ada.
8989
8990 // So we build a subrange type for the sole purpose of using
8991 // the ::as_string() method of that type. So we don't add
8992 // that type to the current type tree being built.
8994 build_subrange_type(const_cast<reader&>(rdr),
8995 die, where, tu_ctxt,
8996 /*associate_die_to_type=*/false);
8997 repr += s->as_string();
8998 break;
8999 }
9000
9001 case DW_TAG_array_type:
9002 {
9003 Dwarf_Die element_type_die;
9004 if (!die_die_attribute(die, DW_AT_type, element_type_die))
9005 break;
9006 string element_type_name =
9007 die_qualified_type_name(rdr, &element_type_die,
9008 where, tu_ctxt, guard);
9009 if (element_type_name.empty())
9010 break;
9011
9013 build_subranges_from_array_type_die(const_cast<reader&>(rdr),
9014 die, subranges, where, tu_ctxt,
9015 /*associate_type_to_die=*/false);
9016
9017 repr = element_type_name;
9019 }
9020 break;
9021
9022 case DW_TAG_subroutine_type:
9023 case DW_TAG_subprogram:
9024 {
9025 string return_type_name;
9026 string class_name;
9027 vector<string> parm_names;
9028 bool is_const = false;
9029 bool is_static = false;
9030 bool is_method_type = false;
9031 die_return_and_parm_names_from_fn_type_die(rdr, die, where,
9032 /*pretty_print=*/true,
9033 /*qualified_name=*/true,
9035 return_type_name, class_name,
9036 parm_names, is_const,
9037 is_static, tu_ctxt, guard);
9038 if (return_type_name.empty())
9039 return_type_name = "void";
9040
9041 repr = return_type_name;
9042
9043 if (is_method_type)
9044 // This is a method, so print the class name.
9045 repr += " (" + class_name + "::*)";
9046
9047 // Now parameters.
9048 repr += " (";
9049 for (vector<string>::const_iterator i = parm_names.begin();
9050 i != parm_names.end();
9051 ++i)
9052 {
9053 if (i != parm_names.begin())
9054 repr += ", ";
9055 repr += *i;
9056 }
9057 repr += ")";
9058
9059 }
9060 break;
9061
9062 case DW_TAG_string_type:
9063 case DW_TAG_ptr_to_member_type:
9064 case DW_TAG_set_type:
9065 case DW_TAG_file_type:
9066 case DW_TAG_packed_type:
9067 case DW_TAG_thrown_type:
9068 case DW_TAG_interface_type:
9069 case DW_TAG_shared_type:
9070 break;
9071 }
9072
9073 return repr;
9074}
9075
9076/// Compute the qualified name of a decl represented by a given DIE.
9077///
9078/// For instance, for a DIE of tag DW_TAG_subprogram this function
9079/// computes the signature of the function *declaration*.
9080///
9081/// @param rdr the DWARF reader.
9082///
9083/// @param die the DIE to consider.
9084///
9085/// @param where_offset where we are logically at in the DIE stream.
9086///
9087/// @param guard the set of DIE addresses of the stack of DIEs involved
9088/// in the construction of the qualified name of the decl. This set
9089/// is used to detect (and avoid) cycles in the stack of DIEs that is
9090/// going to be walked to compute the qualified decl name.
9091///
9092/// @return a copy of the computed name.
9093static string
9094die_qualified_decl_name(const reader& rdr,
9095 const Dwarf_Die* die,
9096 void* where_addr,
9097 reader::tu_context_type_sptr& tu_ctxt,
9098 unordered_set<void*>& guard)
9099{
9100 if (!die || !die_is_decl(die))
9101 return "";
9102
9103 string name = die_name(die);
9104
9105 Dwarf_Die scope_die;
9106 if (!rdr.get_scope_die(die, where_addr, tu_ctxt, scope_die))
9107 return "";
9108
9109 string scope_name = die_qualified_name(rdr, &scope_die, where_addr,
9110 tu_ctxt, guard);
9111 string separator = "::";
9112
9113 string repr;
9114
9115 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9116 switch (tag)
9117 {
9118 case DW_TAG_namespace:
9119 case DW_TAG_member:
9120 case DW_TAG_variable:
9121 repr = scope_name.empty() ? name : scope_name + separator + name;
9122 break;
9123 case DW_TAG_subprogram:
9124 repr = die_function_signature(rdr, die,
9125 /*qualified_name=*/true,
9126 where_addr, tu_ctxt, guard);
9127 break;
9128
9129 case DW_TAG_unspecified_parameters:
9130 repr = "...";
9131 break;
9132
9133 case DW_TAG_formal_parameter:
9134 case DW_TAG_imported_declaration:
9135 case DW_TAG_GNU_template_template_param:
9136 case DW_TAG_GNU_template_parameter_pack:
9137 case DW_TAG_GNU_formal_parameter_pack:
9138 break;
9139 }
9140 return repr;
9141}
9142
9143/// Compute the qualified name of the artifact represented by a given
9144/// DIE.
9145///
9146/// If the DIE represents a type, then the function computes the name
9147/// of the type. Otherwise, if the DIE represents a decl then the
9148/// function computes the name of the decl. Note that a DIE of tag
9149/// DW_TAG_subprogram is going to be considered as a "type" -- just
9150/// like if it was a DW_TAG_subroutine_type.
9151///
9152/// @param rdr the DWARF reader.
9153///
9154/// @param die the DIE to consider.
9155///
9156/// @param where_offset where we are logically at in the DIE stream.
9157///
9158/// @param guard the set of DIE addresses of the stack of DIEs involved
9159/// in the construction of the qualified name of the DIE. This set is
9160/// used to detect (and avoid) cycles in the stack of DIEs that is
9161/// going to be walked to compute the qualified DIE name.
9162///
9163/// @return a copy of the computed name.
9164string
9165die_qualified_name(const reader& rdr, const Dwarf_Die* die, void* where,
9166 reader::tu_context_type_sptr& tu_ctxt,
9167 unordered_set<void*>& guard)
9168{
9169 if (die_is_type(die))
9170 return die_qualified_type_name(rdr, die, where, tu_ctxt, guard);
9171 else if (die_is_decl(die))
9172 return die_qualified_decl_name(rdr, die, where, tu_ctxt, guard);
9173 return "";
9174}
9175
9176/// Compute the qualified name of the artifact represented by a given
9177/// DIE.
9178///
9179/// If the DIE represents a type, then the function computes the name
9180/// of the type. Otherwise, if the DIE represents a decl then the
9181/// function computes the name of the decl. Note that a DIE of tag
9182/// DW_TAG_subprogram is going to be considered as a "type" -- just
9183/// like if it was a DW_TAG_subroutine_type.
9184///
9185/// @param rdr the DWARF reader.
9186///
9187/// @param die the DIE to consider.
9188///
9189/// @param where_offset where we are logically at in the DIE stream.
9190///
9191/// @return a copy of the computed name.
9192static string
9193die_qualified_name(const reader& rdr, const Dwarf_Die* die, void* where,
9194 reader::tu_context_type_sptr& tu_ctxt)
9195{
9196 unordered_set<void*> guard;
9197 return die_qualified_name(rdr, die, where, tu_ctxt, guard);
9198}
9199
9200/// Test if the qualified name of a given type should be empty.
9201///
9202/// The reason why the name of a DIE with a given tag would be empty
9203/// is that libabigail's internal representation doesn't yet support
9204/// that tag; or if the DIE's qualified name is built from names of
9205/// sub-types DIEs whose tags are not yet supported.
9206///
9207/// @param rdr the DWARF reader.
9208///
9209/// @param die the DIE to consider.
9210///
9211/// @param where where we are logically at, in the DIE stream.
9212///
9213/// @param qualified_name the qualified name of the DIE. This is set
9214/// only iff the function returns false.
9215///
9216/// @param guard the set of DIE addresses of the stack of DIEs involved
9217/// in the construction of the qualified name of the type. This set
9218/// is used to detect (and avoid) cycles in the stack of DIEs that is
9219/// going to be walked to compute the qualified type name.
9220///
9221/// @return true if the qualified name of the DIE is empty.
9222static bool
9223die_qualified_type_name_empty(const reader& rdr,
9224 const Dwarf_Die* die,
9225 void* where, string &qualified_name,
9226 reader::tu_context_type_sptr& tu_ctxt,
9227 unordered_set<void*>& guard)
9228{
9229 if (!die)
9230 return true;
9231
9232 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9233
9234 string qname;
9235 if (tag == DW_TAG_typedef
9236 || tag == DW_TAG_pointer_type
9237 || tag == DW_TAG_reference_type
9238 || tag == DW_TAG_rvalue_reference_type
9239 || tag == DW_TAG_array_type
9240 || tag == DW_TAG_const_type
9241 || tag == DW_TAG_volatile_type
9242 || tag == DW_TAG_restrict_type)
9243 {
9244 Dwarf_Die underlying_type_die;
9245 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
9246 {
9247 string name =
9248 die_qualified_type_name(rdr, &underlying_type_die,
9249 where, tu_ctxt, guard);
9250 if (name.empty())
9251 return true;
9252 }
9253 }
9254 else
9255 {
9256 string name = die_qualified_type_name(rdr, die, where,
9257 tu_ctxt, guard);
9258 if (name.empty())
9259 return true;
9260 }
9261
9262 qname = die_qualified_type_name(rdr, die, where, tu_ctxt, guard);
9263 if (qname.empty())
9264 return true;
9265
9266 qualified_name = qname;
9267 return false;
9268}
9269/// Given the DIE that represents a function type, compute the names
9270/// of the following properties the function's type:
9271///
9272/// - return type
9273/// - enclosing class (if the function is a member function)
9274/// - function parameter types
9275///
9276/// When the function we are looking at is a member function, it also
9277/// tells if it's const.
9278///
9279/// @param rdr the DWARF reader.
9280///
9281/// @param die the DIE of the function or function type we are looking
9282/// at.
9283///
9284/// @param where_offset where we are logically at in the DIE stream.
9285///
9286/// @param pretty_print if set to yes, the type names are going to be
9287/// pretty-printed names; otherwise, they are just qualified type
9288/// names.
9289///
9290/// @param qualified_name if true then the names returned are
9291/// qualified.
9292///
9293/// @param is_method_type output parameter. This is set by the
9294/// function to true iff the DIE @p die represents a method.
9295///
9296/// @param return_type_name out parameter. This contains the name of
9297/// the return type of the function.
9298///
9299/// @param class_name out parameter. If the function is a member
9300/// function, this contains the name of the enclosing class.
9301///
9302/// @param parm_names out parameter. This vector is set to the names
9303/// of the types of the parameters of the function.
9304///
9305/// @param is_const out parameter. If the function is a member
9306/// function, this is set to true iff the member function is const.
9307///
9308/// @param is_static out parameter. If the function is a static
9309/// member function, then this is set to true.
9310///
9311/// @param guard the set of DIE addresses of the stack of DIEs involved
9312/// in the construction of the qualified name of the function type.
9313/// This set is used to detect (and avoid) cycles in the stack of DIEs
9314/// that is going to be walked to compute the qualified function type
9315/// name.
9316static void
9317die_return_and_parm_names_from_fn_type_die(const reader& rdr,
9318 const Dwarf_Die* die,
9319 void* where_addr,
9320 bool pretty_print,
9321 bool qualified_name,
9322 bool &is_method_type,
9323 string &return_type_name,
9324 string &class_name,
9325 vector<string>& parm_names,
9326 bool& is_const,
9327 bool& is_static,
9328 reader::tu_context_type_sptr& tu_ctxt,
9329 unordered_set<void*>& guard)
9330{
9331 if (!die)
9332 return;
9333
9334 if (guard.find(die->addr) != guard.end())
9335 return;
9336 guard.insert(die->addr);
9337
9338 Dwarf_Die child;
9339 Dwarf_Die ret_type_die;
9340 if (!die_die_attribute(die, DW_AT_type, ret_type_die))
9341 return_type_name = "void";
9342 else
9343 {
9344 return_type_name =
9345 pretty_print
9346 ? rdr.get_die_pretty_representation(&ret_type_die, where_addr,
9347 tu_ctxt, guard)
9348 : die_type_name(rdr, &ret_type_die, qualified_name,
9349 where_addr, tu_ctxt, guard);
9350 }
9351
9352 if (return_type_name.empty())
9353 return_type_name = "void";
9354
9355 Dwarf_Die object_pointer_die, class_die;
9357 die_function_type_is_method_type(rdr, die, where_addr,
9358 tu_ctxt, object_pointer_die,
9359 class_die, is_static);
9360
9361 is_const = false;
9362 if (is_method_type)
9363 {
9364 if (!is_anonymous_type_die(&class_die))
9365 class_name = die_type_name(rdr, &class_die, qualified_name,
9366 where_addr, tu_ctxt, guard);
9367
9368 Dwarf_Die this_pointer_die;
9369 Dwarf_Die pointed_to_die;
9370 if (!is_static
9371 && die_die_attribute(&object_pointer_die, DW_AT_type,
9372 this_pointer_die))
9373 if (die_die_attribute(&this_pointer_die, DW_AT_type, pointed_to_die))
9374 if (dwarf_tag(&pointed_to_die) == DW_TAG_const_type)
9375 is_const = true;
9376
9377 string fn_name = die_name(die);
9378 string non_qualified_class_name = die_name(&class_die);
9379 bool is_ctor = fn_name == non_qualified_class_name;
9380 bool is_dtor = !fn_name.empty() && fn_name[0] == '~';
9381
9382 if (is_ctor || is_dtor)
9383 return_type_name.clear();
9384 }
9385
9386 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9387 do
9388 {
9389 int child_tag = dwarf_tag(&child);
9390 bool first_parm = true;
9391 if (child_tag == DW_TAG_formal_parameter)
9392 {
9393 // Skip the first parameter of a method.
9394 if (first_parm)
9395 {
9396 first_parm = false;
9397 if (is_method_type)
9398 continue;
9399 }
9400 Dwarf_Die parm_type_die;
9401 if (!die_die_attribute(&child, DW_AT_type, parm_type_die))
9402 continue;
9403 string qname =
9404 pretty_print
9405 ? rdr.get_die_pretty_representation(&parm_type_die, where_addr,
9406 tu_ctxt, guard)
9407 : die_type_name(rdr, &parm_type_die,
9408 qualified_name, where_addr, tu_ctxt, guard);
9409
9410 if (qname.empty())
9411 continue;
9412 parm_names.push_back(qname);
9413 }
9414 else if (child_tag == DW_TAG_unspecified_parameters)
9415 {
9416 // This is a variadic function parameter.
9417 parm_names.push_back(rdr.env().get_variadic_parameter_type_name());
9418 // After a DW_TAG_unspecified_parameters tag, we shouldn't
9419 // keep reading for parameters. The
9420 // unspecified_parameters TAG should be the last parameter
9421 // that we record. For instance, if there are multiple
9422 // DW_TAG_unspecified_parameters DIEs then we should care
9423 // only for the first one.
9424 break;
9425 }
9426 }
9427 while (dwarf_siblingof(&child, &child) == 0);
9428
9429 if (class_name.empty())
9430 {
9431 Dwarf_Die parent_die;
9432 if (rdr.get_parent_die(die, parent_die, where_addr, tu_ctxt))
9433 {
9434 if (die_is_class_type(&parent_die)
9435 && !is_anonymous_type_die(&parent_die))
9436 class_name = die_type_name(rdr, &parent_die, qualified_name,
9437 where_addr, tu_ctxt, guard);
9438 }
9439 }
9440
9441 guard.erase(die->addr);
9442}
9443
9444/// This computes the signature of the a function declaration
9445/// represented by a DIE.
9446///
9447/// @param rdr the DWARF reader.
9448///
9449/// @param fn_die the DIE of the function to consider.
9450///
9451/// @param qualified_name if set to true then a qualified name is
9452/// going to be computed.
9453///
9454/// @param where_offset where we are logically at in the stream of
9455/// DIEs.
9456///
9457/// @param guard the set of DIE addresses of the stack of DIEs involved
9458/// in the construction of the signature of the function type. This
9459/// set is used to detect (and avoid) cycles in the stack of DIEs that
9460/// is going to be walked to compute the signature.
9461///
9462/// @return a copy of the computed function signature string.
9463static string
9464die_function_signature(const reader& rdr, const Dwarf_Die *fn_die,
9465 bool qualified_name, void* where_addr,
9466 reader::tu_context_type_sptr& tu_ctxt,
9467 unordered_set<void*>& guard)
9468{
9469
9471 bool has_lang = false;
9472 if ((has_lang = get_die_language(fn_die, lang)))
9473 {
9474 // In a binary originating from the C language, it's OK to use
9475 // the linkage name of the function as a key for the map which
9476 // is meant to reduce the number of DIE comparisons involved
9477 // during DIE canonicalization computation.
9478 if (is_c_language(lang))
9479 {
9480 string fn_name = die_linkage_name(fn_die);
9481 if (fn_name.empty())
9482 fn_name = die_name(fn_die);
9483 return fn_name;
9484 }
9485 }
9486
9487 // TODO: When we can structurally compare DIEs originating from C++
9488 // as well, we can use the linkage name of functions in C++ too, to
9489 // reduce the number of comparisons involved during DIE
9490 // canonicalization.
9491
9492 string return_type_name;
9493 Dwarf_Die ret_type_die;
9494 if (die_die_attribute(fn_die, DW_AT_type, ret_type_die))
9495 return_type_name =
9496 rdr.get_die_qualified_type_name(&ret_type_die, where_addr,
9497 tu_ctxt, guard);
9498
9499 if (return_type_name.empty())
9500 return_type_name = "void";
9501
9502 Dwarf_Die scope_die;
9503 string scope_name;
9504 if (qualified_name && rdr.get_scope_die(fn_die, where_addr,
9505 tu_ctxt, scope_die))
9506 scope_name = rdr.get_die_qualified_name(&scope_die, where_addr,
9507 tu_ctxt, guard);
9508 string fn_name = die_name(fn_die);
9509 if (!scope_name.empty())
9510 fn_name = scope_name + "::" + fn_name;
9511
9512 string class_name;
9513 vector<string> parm_names;
9514 bool is_const = false;
9515 bool is_static = false;
9516 bool is_method_type = false;
9517
9518 die_return_and_parm_names_from_fn_type_die(rdr, fn_die, where_addr,
9519 /*pretty_print=*/false,
9520 qualified_name, is_method_type,
9521 return_type_name, class_name,
9522 parm_names, is_const, is_static,
9523 tu_ctxt, guard);
9524
9525 bool is_virtual = die_is_virtual(fn_die);
9526
9527 string repr = is_method_type? "method" : "function";
9528 if (is_virtual)
9529 repr += " virtual";
9530
9531 if (!return_type_name.empty())
9532 repr += " " + return_type_name;
9533
9534 repr += " " + fn_name;
9535
9536 // Now parameters.
9537 repr += "(";
9538 bool some_parm_emitted = false;
9539 for (vector<string>::const_iterator i = parm_names.begin();
9540 i != parm_names.end();
9541 ++i)
9542 {
9543 if (i != parm_names.begin())
9544 {
9545 if (some_parm_emitted)
9546 repr += ", ";
9547 }
9548 else
9549 if (!is_static && is_method_type)
9550 // We are printing a non-static method name, skip the implicit "this"
9551 // parameter type.
9552 continue;
9553 repr += *i;
9554 some_parm_emitted = true;
9555 }
9556 repr += ")";
9557
9558 if (is_const)
9559 {
9561 repr += " const";
9562 }
9563
9564 return repr;
9565}
9566
9567/// Compute the flat representation string of a struct, class or union
9568/// type represented by a DIE.
9569///
9570/// The flat representation string looks like:
9571/// "struct {int foo; char blah;}.
9572///
9573/// That is useful to designate a struct (class or union) that is
9574/// anonymous.
9575///
9576/// @param rdr the DWARF reader to consider.
9577///
9578/// @param die the DIE of the type to return the flat representation
9579/// for.
9580///
9581/// @param indent the indentation string to use for the
9582/// representation.
9583///
9584/// @param one_line if true then the flat representation is
9585/// constructed on one line. Otherwise, each data member is
9586/// represented on its own line.
9587///
9588/// @param qualified_names if true then the data member (and their
9589/// type) names using in the representation are qualified.
9590///
9591/// @param where_offset where in the are logically are in the DIE
9592/// stream.
9593///
9594/// @param guard the set of DIE addresses of the stack of DIEs involved
9595/// in the construction of the flat representation of the type. This
9596/// set is used to detect (and avoid) cycles in the stack of DIEs that
9597/// is going to be walked to compute the flat representation.
9598static string
9599die_class_flat_representation(const reader& rdr,
9600 const Dwarf_Die* die,
9601 const string& indent,
9602 bool one_line,
9603 bool qualified_names,
9604 void* where_addr,
9605 reader::tu_context_type_sptr& tu_ctxt,
9606 unordered_set<void*>& guard)
9607{
9608 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9609
9610 string repr = indent;
9611 string local_indent = " ";
9612 string real_indent;
9613
9614 if (tag == DW_TAG_union_type)
9615 repr += "union";
9616 else if (tag == DW_TAG_structure_type)
9617 repr += "struct";
9618 else if (tag == DW_TAG_class_type)
9619 repr += "class";
9620 else
9622
9623 repr += " ";
9624
9625 if (die_is_anonymous(die))
9626 {
9627 if (guard.find(die->addr) != guard.end())
9628 {
9629 repr += "{}";
9630 return repr;
9631 }
9632 guard.insert(die->addr);
9633 }
9634
9635 if (!die_is_anonymous(die))
9636 repr += die_qualified_name(rdr, die, where_addr, tu_ctxt, guard);
9637
9638 repr += "{";
9639
9640 if (!one_line)
9641 repr += "\n";
9642
9643 Dwarf_Die member_child_die;
9644 bool first_sibling = true;
9645 for (bool got_it = get_member_child_die(die, &member_child_die);
9646 got_it;
9647 got_it = get_next_member_sibling_die(&member_child_die,
9648 &member_child_die),
9649 first_sibling = false)
9650 {
9651 // A member of the class is either a declaration or an anonymous
9652 // type. Otherwise, let's skip it.
9653 if (!die_is_decl(&member_child_die)
9654 && !(die_is_type(&member_child_die)
9655 && die_is_anonymous(&member_child_die)))
9656 continue;
9657
9658 if (one_line)
9659 real_indent = first_sibling ? "" : " " ;
9660 else
9661 real_indent = (first_sibling ? "": "\n") + indent + local_indent;
9662
9663 repr += real_indent;
9664
9665 repr += die_pretty_print_decl(rdr, &member_child_die,
9666 qualified_names,
9667 /*include_fns=*/false,
9668 where_addr, tu_ctxt, guard);
9669 repr += ";";
9670 }
9671
9672 if (one_line)
9673 repr += "}";
9674 else
9675 repr += indent + "}";
9676
9677 if (die_is_anonymous(die))
9678 guard.erase(die->addr);
9679 return repr;
9680}
9681
9682/// Compute the flat representation string of a enum type represented
9683/// by a DIE.
9684///
9685/// The flat representation string looks like:
9686/// "enum {int foo; char blah;}.
9687///
9688/// That is useful to designate an enum that is anonymous.
9689///
9690/// @param rdr the DWARF reader to consider.
9691///
9692/// @param die the DIE of the type to return the flat representation
9693/// for.
9694///
9695/// @param indent the indentation string to use for the
9696/// representation.
9697///
9698/// @param one_line if true then the flat representation is
9699/// constructed on one line. Otherwise, each data member is
9700/// represented on its own line.
9701///
9702/// @param qualified_names if true then the data member (and their
9703/// type) names using in the representation are qualified.
9704///
9705/// @param where_offset where in the are logically are in the DIE
9706/// stream.
9707static string
9708die_enum_flat_representation(const reader& rdr,
9709 const Dwarf_Die* die,
9710 const string& indent,
9711 bool one_line,
9712 bool qualified_names,
9713 void* where_addr,
9714 reader::tu_context_type_sptr& tu_ctxt)
9715{
9716 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9717
9718 std::ostringstream o;
9719 string local_indent = " ";
9720 string real_indent;
9721
9722 if (tag == DW_TAG_enumeration_type)
9723 o << "enum";
9724 else
9726
9727 o << " ";
9728
9729 if (!die_is_anonymous(die))
9730 o << (qualified_names
9731 ? die_qualified_name(rdr, die, where_addr, tu_ctxt)
9732 : die_name(die));
9733
9734 o << "{";
9735
9736 if (!one_line)
9737 o << "\n";
9738
9740 Dwarf_Die child;
9741 bool first_enumerator= true;
9742 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
9743 {
9744 do
9745 {
9746 if (dwarf_tag(&child) != DW_TAG_enumerator)
9747 continue;
9748
9749 string name, m;
9750 location l;
9751 die_loc_and_name(&child, tu_ctxt, l, name, m);
9752 uint64_t val = 0;
9753 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
9754
9755 if (one_line)
9756 real_indent = first_enumerator ? "" : ", ";
9757 else
9758 real_indent = first_enumerator ? "" : ",\n" + indent + local_indent;
9759 o << name + " = " << val;
9760 first_enumerator = false;
9761 }
9762 while (dwarf_siblingof(&child, &child) == 0);
9763 }
9764
9765 o << (one_line
9766 ? string("}")
9767 : "\n" + indent);
9768
9769 o << "}";
9770
9771 return o.str();
9772}
9773
9774/// Compute the flat representation string of a class or enum type
9775/// represented by a DIE.
9776///
9777/// The flat representation string looks like:
9778/// "union {int foo; char blah;}.
9779///
9780/// That is useful to designate a class or enum type that is
9781/// anonymous.
9782///
9783/// @param rdr the DWARF reader to consider.
9784///
9785/// @param die the DIE of the type to return the flat representation
9786/// for.
9787///
9788/// @param indent the indentation string to use for the
9789/// representation.
9790///
9791/// @param one_line if true then the flat representation is
9792/// constructed on one line. Otherwise, each data member is
9793/// represented on its own line.
9794///
9795/// @param qualified_names if true then the data member (and their
9796/// type) names using in the representation are qualified.
9797///
9798/// @param where_offset where in the are logically are in the DIE
9799/// stream.
9800///
9801/// @param guard the set of DIE addresses of the stack of DIEs involved
9802/// in the construction of the flat representation of the type. This
9803/// set is used to detect (and avoid) cycles in the stack of DIEs that
9804/// is going to be walked to compute the flat representation.
9805string
9807 const Dwarf_Die* die,
9808 const string& indent,
9809 bool one_line,
9810 bool qualified_names,
9811 void* where_addr,
9812 reader::tu_context_type_sptr& tu_ctxt,
9813 unordered_set<void*>& guard)
9814{
9815 if (!die)
9816 return string();
9817
9818 string result;
9819 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
9820
9821 switch (tag)
9822 {
9823 case DW_TAG_class_type:
9824 case DW_TAG_structure_type:
9825 case DW_TAG_union_type:
9826 result = die_class_flat_representation(rdr, die, indent,
9827 one_line, qualified_names,
9828 where_addr, tu_ctxt, guard);
9829 break;
9830 case DW_TAG_enumeration_type:
9831 result = die_enum_flat_representation(rdr, die, indent,
9832 one_line, qualified_names,
9833 where_addr, tu_ctxt);
9834 break;
9835 default:
9837 }
9838
9839 return result;
9840}
9841
9842/// Compute the flat representation string of a class or enum type
9843/// represented by a DIE.
9844///
9845/// The flat representation string looks like:
9846/// "union {int foo; char blah;}.
9847///
9848/// That is useful to designate a class or enum type that is
9849/// anonymous.
9850///
9851/// @param rdr the DWARF reader to consider.
9852///
9853/// @param die the DIE of the type to return the flat representation
9854/// for.
9855///
9856/// @param indent the indentation string to use for the
9857/// representation.
9858///
9859/// @param one_line if true then the flat representation is
9860/// constructed on one line. Otherwise, each data member is
9861/// represented on its own line.
9862///
9863/// @param qualified_names if true then the data member (and their
9864/// type) names using in the representation are qualified.
9865///
9866/// @param where_offset where in the are logically are in the DIE
9867/// stream.
9868string
9870 const Dwarf_Die* die,
9871 const string& indent,
9872 bool one_line,
9873 bool qualified_names,
9874 void* where_addr,
9875 reader::tu_context_type_sptr& tu_ctxt)
9876{
9877 unordered_set<void*> guard;
9878 return die_class_or_enum_flat_representation(rdr, die, indent,
9879 one_line, qualified_names,
9880 where_addr, tu_ctxt, guard);
9881}
9882
9883/// Compute the name of a type represented by a DIE.
9884///
9885/// @param rdr the reader to use.
9886///
9887/// @param die the type DIE to consider.
9888///
9889/// @param qualified_name if true then compute a qualified name.
9890///
9891/// @param where_offset where in the are logically are in the DIE
9892/// stream.
9893///
9894/// @param guard the set of DIE addresses of the stack of DIEs involved
9895/// in the construction of the name of the type. This set is used to
9896/// detect (and avoid) cycles in the stack of DIEs that is going to be
9897/// walked to compute the type name.
9898///
9899/// @return a copy of the string representing the type represented by
9900/// @p die.
9901static string
9902die_type_name(const reader& rdr,
9903 const Dwarf_Die* die,
9904 bool qualified_name,
9905 void* where_addr,
9906 reader::tu_context_type_sptr& tu_ctxt,
9907 unordered_set<void*>& guard)
9908{
9909 if (!die)
9910 return "";
9911
9912 int tag = dwarf_tag (const_cast<Dwarf_Die*>(die));
9913 if (tag == DW_TAG_compile_unit
9914 || tag == DW_TAG_partial_unit
9915 || tag == DW_TAG_type_unit)
9916 return "";
9917
9918 string name = die_name(die);
9919
9920 Dwarf_Die scope_die;
9921 if (!rdr.get_scope_die(die, where_addr, tu_ctxt, scope_die))
9922 return "";
9923
9924 bool colon_colon = die_is_type(die) || die_is_namespace(die);
9925 string separator = colon_colon ? "::" : ".";
9926
9927 string repr;
9928
9929 switch (tag)
9930 {
9931 case DW_TAG_unspecified_type:
9932 break;
9933
9934 case DW_TAG_base_type:
9935 {
9936 abigail::ir::real_type int_type;
9937 if (parse_real_type(name, int_type))
9938 repr = int_type;
9939 else
9940 repr = name;
9941 }
9942 break;
9943
9944 case DW_TAG_typedef:
9945 ABG_ASSERT(!name.empty());
9946 // fall through
9947
9948 case DW_TAG_enumeration_type:
9949 case DW_TAG_structure_type:
9950 case DW_TAG_class_type:
9951 case DW_TAG_union_type:
9952 {
9953 if (die_is_anonymous(die))
9954 repr = die_class_or_enum_flat_representation(rdr, die, /*indent=*/"",
9955 /*one_line=*/true,
9956 /*qualed_name=*/false,
9957 where_addr, tu_ctxt,
9958 guard);
9959 else
9960 {
9961 string parent_name;
9962 if (qualified_name)
9963 {
9964 if (!is_anonymous_type_die(&scope_die))
9965 parent_name = die_qualified_name(rdr, &scope_die,
9966 where_addr, tu_ctxt, guard);
9967 }
9968 repr = parent_name.empty() ? name : parent_name + separator + name;
9969 }
9970 }
9971 break;
9972
9973 case DW_TAG_const_type:
9974 case DW_TAG_volatile_type:
9975 case DW_TAG_restrict_type:
9976 {
9977 Dwarf_Die underlying_type_die;
9978 bool has_underlying_type_die =
9979 die_die_attribute(die, DW_AT_type, underlying_type_die);
9980
9981 if (has_underlying_type_die && die_is_unspecified(&underlying_type_die))
9982 break;
9983
9984 if (tag == DW_TAG_const_type)
9985 {
9986 if (has_underlying_type_die
9987 && die_is_reference_type(&underlying_type_die))
9988 // A reference is always const. So, to lower false
9989 // positive reports in diff computations, we consider a
9990 // const reference just as a reference. But we need to
9991 // keep the qualified-ness of the type. So we introduce
9992 // a 'no-op' qualifier here. Please remember that this
9993 // has to be kept in sync with what is done in
9994 // get_name_of_qualified_type. So if you change this
9995 // here, you have to change that code there too.
9996 repr = "";
9997 else if (!has_underlying_type_die
9998 || die_is_void_type(&underlying_type_die))
9999 {
10000 repr = "void";
10001 break;
10002 }
10003 else
10004 repr = "const";
10005 }
10006 else if (tag == DW_TAG_volatile_type)
10007 repr = "volatile";
10008 else if (tag == DW_TAG_restrict_type)
10009 repr = "restrict";
10010 else
10012
10013 string underlying_type_repr;
10014 if (has_underlying_type_die)
10015 underlying_type_repr =
10016 die_type_name(rdr, &underlying_type_die,
10017 qualified_name, where_addr,
10018 tu_ctxt, guard);
10019 else
10020 underlying_type_repr = "void";
10021
10022 if (underlying_type_repr.empty())
10023 repr.clear();
10024 else
10025 {
10026 if (has_underlying_type_die)
10027 {
10028 Dwarf_Die peeled;
10029 die_peel_qualified(&underlying_type_die, peeled);
10030 if (die_is_pointer_or_reference_type(&peeled))
10031 repr = underlying_type_repr + " " + repr;
10032 else
10033 repr += " " + underlying_type_repr;
10034 }
10035 else
10036 repr += " " + underlying_type_repr;
10037 }
10038 }
10039 break;
10040
10041 case DW_TAG_pointer_type:
10042 case DW_TAG_reference_type:
10043 case DW_TAG_rvalue_reference_type:
10044 {
10045 Dwarf_Die pointed_to_type_die;
10046 if (!die_die_attribute(die, DW_AT_type, pointed_to_type_die))
10047 {
10048 if (tag == DW_TAG_pointer_type)
10049 repr = "void*";
10050 break;
10051 }
10052
10053 if (die_is_unspecified(&pointed_to_type_die))
10054 break;
10055
10056 string pointed_type_repr =
10057 die_type_name(rdr, &pointed_to_type_die,
10058 qualified_name, where_addr,
10059 tu_ctxt, guard);
10060
10061 repr = pointed_type_repr;
10062 if (repr.empty())
10063 break;
10064
10065 if (tag == DW_TAG_pointer_type)
10066 repr += "*";
10067 else if (tag == DW_TAG_reference_type)
10068 repr += "&";
10069 else if (tag == DW_TAG_rvalue_reference_type)
10070 repr += "&&";
10071 else
10073 }
10074 break;
10075
10076 case DW_TAG_subrange_type:
10077 {
10078 // In Ada, this one can be generated on its own, that is, not
10079 // as a sub-type of an array. So we need to support it on its
10080 // own. Note that when it's emitted as the sub-type of an
10081 // array like in C and C++, this is handled differently, for
10082 // now. But we try to make this usable by other languages
10083 // that are not Ada, even if we modelled it after Ada.
10084
10085 // So we build a subrange type for the sole purpose of using
10086 // the ::as_string() method of that type. So we don't add
10087 // that type to the current type tree being built.
10089 build_subrange_type(const_cast<reader&>(rdr),
10090 die, where_addr, tu_ctxt,
10091 /*associate_die_to_type=*/false);
10092 repr += s->as_string();
10093 break;
10094 }
10095
10096 case DW_TAG_array_type:
10097 {
10098 Dwarf_Die element_type_die;
10099 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10100 break;
10101 string element_type_name =
10102 die_type_name(rdr, &element_type_die,
10103 qualified_name, where_addr,
10104 tu_ctxt, guard);
10105 if (element_type_name.empty())
10106 break;
10107
10109 build_subranges_from_array_type_die(const_cast<reader&>(rdr), die,
10110 subranges, where_addr, tu_ctxt,
10111 /*associate_type_to_die=*/false);
10112
10113 repr = element_type_name;
10115 }
10116 break;
10117
10118 case DW_TAG_subroutine_type:
10119 case DW_TAG_subprogram:
10120 {
10121 string return_type_name;
10122 string class_name;
10123 vector<string> parm_names;
10124 bool is_const = false;
10125 bool is_static = false;
10126 bool is_method_type = false;
10127 die_return_and_parm_names_from_fn_type_die(rdr, die, where_addr,
10128 /*pretty_print=*/true,
10129 qualified_name,
10131 return_type_name,
10132 class_name,
10133 parm_names, is_const,
10134 is_static, tu_ctxt, guard);
10135 if (return_type_name.empty())
10136 return_type_name = "void";
10137
10138 repr = return_type_name;
10139
10140 if (is_method_type)
10141 {
10142 // This is a method, so print the class name.
10143 repr += " (" + class_name + "::*)";
10144 }
10145
10146 // Now parameters.
10147 repr += " (";
10148 for (vector<string>::const_iterator i = parm_names.begin();
10149 i != parm_names.end();
10150 ++i)
10151 {
10152 if (i != parm_names.begin())
10153 repr += ", ";
10154 repr += *i;
10155 }
10156 repr += ")";
10157
10158 }
10159 break;
10160
10161 case DW_TAG_string_type:
10162 case DW_TAG_ptr_to_member_type:
10163 case DW_TAG_set_type:
10164 case DW_TAG_file_type:
10165 case DW_TAG_packed_type:
10166 case DW_TAG_thrown_type:
10167 case DW_TAG_interface_type:
10168 case DW_TAG_shared_type:
10169 break;
10170 }
10171
10172 return repr;
10173}
10174
10175/// Compute the name of a type represented by a DIE.
10176///
10177/// @param rdr the reader to use.
10178///
10179/// @param die the type DIE to consider.
10180///
10181/// @param qualified_name if true then compute a qualified name.
10182///
10183/// @param where_offset where in the are logically are in the DIE
10184/// stream.
10185///
10186/// @return a copy of the string representing the type represented by
10187/// @p die.
10188static string
10189die_type_name(const reader& rdr,
10190 const Dwarf_Die* die,
10191 bool qualified_name,
10192 void* where_addr,
10193 reader::tu_context_type_sptr& tu_ctxt)
10194{
10195 unordered_set<void*> guard;
10196 return die_type_name(rdr, die, qualified_name,
10197 where_addr, tu_ctxt, guard);
10198}
10199
10200/// Return a pretty string representation of a type, for internal purposes.
10201///
10202/// By internal purpose, we mean things like key-ing types for lookup
10203/// purposes and so on.
10204///
10205/// Note that this function is also used to pretty print functions.
10206/// For functions, it prints the *type* of the function.
10207///
10208/// @param rdr the context to use.
10209///
10210/// @param the DIE of the type to pretty print.
10211///
10212/// @param where_offset where we logically are placed when calling
10213/// this. It's useful to handle inclusion of DW_TAG_compile_unit
10214/// entries.
10215///
10216/// @param guard the set of DIE addresses of the stack of DIEs involved
10217/// in the construction of the pretty representation of the type.
10218/// This set is used to detect (and avoid) cycles in the stack of DIEs
10219/// that is going to be walked to compute the pretty representation.
10220///
10221/// @return the resulting pretty representation.
10222static string
10223die_pretty_print_type(const reader& rdr, const Dwarf_Die* die, void* where_addr,
10224 reader::tu_context_type_sptr& tu_ctxt,
10225 unordered_set<void*>& guard)
10226{
10227 if (!die
10228 || (!die_is_type(die)
10229 && dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_subprogram))
10230 return "";
10231
10232 string repr;
10233
10234 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10235 switch (tag)
10236 {
10237 case DW_TAG_string_type:
10238 // For now, we won't try to go get the actual representation of
10239 // the string because this would make things more complicated;
10240 // for that we'd need to interpret some location expressions to
10241 // get the length of the string. And for dynamically allocated
10242 // strings, the result of the location expression evaluation
10243 // might not even be a constant. So at the moment I consider
10244 // this to be a lot of hassle for no great return. Until proven
10245 // otherwise, of course.
10246 repr = "string type";
10247
10248 case DW_TAG_unspecified_type:
10249 case DW_TAG_ptr_to_member_type:
10250 break;
10251
10252 case DW_TAG_namespace:
10253 repr = "namespace " + rdr.get_die_qualified_type_name(die, where_addr,
10254 tu_ctxt, guard);
10255 break;
10256
10257 case DW_TAG_base_type:
10258 repr = rdr.get_die_qualified_type_name(die, where_addr, tu_ctxt, guard);
10259 break;
10260
10261 case DW_TAG_typedef:
10262 {
10263 string qualified_name;
10264 if (!die_qualified_type_name_empty(rdr, die,
10265 where_addr,
10266 qualified_name,
10267 tu_ctxt, guard))
10268 repr = "typedef " + qualified_name;
10269 }
10270 break;
10271
10272 case DW_TAG_const_type:
10273 case DW_TAG_volatile_type:
10274 case DW_TAG_restrict_type:
10275 case DW_TAG_pointer_type:
10276 case DW_TAG_reference_type:
10277 case DW_TAG_rvalue_reference_type:
10278 repr = rdr.get_die_qualified_type_name(die, where_addr, tu_ctxt, guard);
10279 break;
10280
10281 case DW_TAG_enumeration_type:
10282 {
10283 string qualified_name =
10284 rdr.get_die_qualified_type_name(die, where_addr, tu_ctxt, guard);
10285 repr = "enum " + qualified_name;
10286 }
10287 break;
10288
10289 case DW_TAG_structure_type:
10290 case DW_TAG_class_type:
10291 {
10292 string qualified_name =
10293 rdr.get_die_qualified_type_name(die, where_addr, tu_ctxt, guard);
10294 repr = "class " + qualified_name;
10295 }
10296 break;
10297
10298 case DW_TAG_union_type:
10299 {
10300 string qualified_name =
10301 rdr.get_die_qualified_type_name(die, where_addr, tu_ctxt, guard);
10302 repr = "union " + qualified_name;
10303 }
10304 break;
10305
10306 case DW_TAG_array_type:
10307 {
10308 Dwarf_Die element_type_die;
10309 if (!die_die_attribute(die, DW_AT_type, element_type_die))
10310 break;
10311 string element_type_name =
10312 rdr.get_die_qualified_type_name(&element_type_die, where_addr,
10313 tu_ctxt, guard);
10314 if (element_type_name.empty())
10315 break;
10316
10318 build_subranges_from_array_type_die(rdr, die, subranges,
10319 where_addr, tu_ctxt,
10320 /*associate_type_to_die=*/false);
10321
10322 repr = element_type_name;
10324 }
10325 break;
10326
10327 case DW_TAG_subrange_type:
10328 {
10329 // So this can be generated by Ada, on its own; that is, not
10330 // as a subtype of an array. In that case we need to handle
10331 // it properly.
10332
10333 // For now, we consider that the pretty printed name of the
10334 // subrange type is its name. We might need something more
10335 // advance, should the needs of the users get more
10336 // complicated.
10337 repr += die_qualified_type_name(rdr, die, where_addr, tu_ctxt, guard);
10338 }
10339 break;
10340
10341 case DW_TAG_subroutine_type:
10342 case DW_TAG_subprogram:
10343 {
10344 string return_type_name;
10345 string class_name;
10346 vector<string> parm_names;
10347 bool is_const = false;
10348 bool is_static = false;
10349 bool is_method_type = false;
10350 die_return_and_parm_names_from_fn_type_die(rdr, die, where_addr,
10351 /*pretty_print=*/true,
10352 /*qualified_name=*/true,
10354 return_type_name, class_name,
10355 parm_names, is_const,
10356 is_static, tu_ctxt, guard);
10357 if (!is_method_type)
10358 repr = "function type";
10359 else
10360 repr = "method type";
10361 repr += " " + rdr.get_die_qualified_type_name(die, where_addr,
10362 tu_ctxt, guard);
10363 }
10364 break;
10365
10366 case DW_TAG_set_type:
10367 case DW_TAG_file_type:
10368 case DW_TAG_packed_type:
10369 case DW_TAG_thrown_type:
10370 case DW_TAG_interface_type:
10371 case DW_TAG_shared_type:
10373 }
10374
10375 return repr;
10376}
10377
10378/// Return a pretty string representation of a declaration, for
10379/// internal purposes.
10380///
10381/// By internal purpose, we mean things like key-ing declarations for
10382/// lookup purposes and so on.
10383///
10384/// Note that this function is also used to pretty print functions.
10385/// For functions, it prints the signature of the function.
10386///
10387/// @param rdr the context to use.
10388///
10389/// @param die the DIE of the declaration to pretty print.
10390///
10391/// @param qualified_name if true then use qualified names.
10392///
10393/// @param where_offset where we logically are placed when calling
10394/// this. It's useful to handle inclusion of DW_TAG_compile_unit
10395/// entries.
10396///
10397/// @param guard the set of DIE addresses of the stack of DIEs involved
10398/// in the construction of the pretty representation of the decl.
10399/// This set is used to detect (and avoid) cycles in the stack of DIEs
10400/// that is going to be walked to compute the pretty representation.
10401///
10402/// @return the resulting pretty representation.
10403static string
10404die_pretty_print_decl(const reader& rdr, const Dwarf_Die* die,
10405 bool qualified_name, bool include_fns,
10406 void* where_offset,
10407 reader::tu_context_type_sptr& tu_ctxt,
10408 unordered_set<void*>& guard)
10409{
10410 if (!die || !die_is_decl(die))
10411 return "";
10412
10413 string repr;
10414
10415 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10416 switch (tag)
10417 {
10418 case DW_TAG_namespace:
10419 repr = "namespace " + die_qualified_name(rdr, die, where_offset,
10420 tu_ctxt, guard);
10421 break;
10422
10423 case DW_TAG_member:
10424 case DW_TAG_variable:
10425 {
10426 string type_repr = "void";
10427 Dwarf_Die type_die;
10428 if (die_die_attribute(die, DW_AT_type, type_die))
10429 type_repr = die_type_name(rdr, &type_die, qualified_name,
10430 where_offset, tu_ctxt, guard);
10431 repr = (qualified_name
10432 ? die_qualified_name(rdr, die, where_offset, tu_ctxt, guard)
10433 : die_name(die));
10434
10435 if (repr.empty())
10436 repr = type_repr;
10437 else
10438 repr = type_repr + " " + repr;
10439 }
10440 break;
10441
10442 case DW_TAG_subprogram:
10443 if (include_fns)
10444 repr = die_function_signature(rdr, die, qualified_name,
10445 where_offset, tu_ctxt, guard);
10446 break;
10447
10448 default:
10449 break;
10450 }
10451 return repr;
10452}
10453
10454/// Compute the pretty printed representation of an artifact
10455/// represented by a DIE.
10456///
10457/// If the DIE is a type, compute the its pretty representation as a
10458/// type; otherwise, if it's a declaration, compute its pretty
10459/// representation as a declaration. Note for For instance, that a
10460/// DW_TAG_subprogram DIE is going to be represented as a function
10461/// *type*.
10462///
10463/// @param rdr the DWARF reader.
10464///
10465/// @param die the DIE to consider.
10466///
10467/// @param where_offset we in the DIE stream we are logically at.
10468///
10469/// @param guard the set of DIE addresses of the stack of DIEs involved
10470/// in the construction of the pretty representation of the DIe. This
10471/// set is used to detect (and avoid) cycles in the stack of DIEs that
10472/// is going to be walked to compute the pretty representation.
10473///
10474/// @return a copy of the pretty printed artifact.
10475string
10476die_pretty_print(reader& rdr, const Dwarf_Die* die, void* where_addr,
10477 reader::tu_context_type_sptr& tu_ctxt,
10478 unordered_set<void*>& guard)
10479{
10480 if (die_is_type(die))
10481 return die_pretty_print_type(rdr, die, where_addr, tu_ctxt, guard);
10482 else if (die_is_decl(die))
10483 return die_pretty_print_decl(rdr, die,
10484 /*qualified_names=*/true,
10485 /*include_fns=*/true,
10486 where_addr, tu_ctxt, guard);
10487 return "";
10488}
10489
10490// -----------------------------------
10491// </die pretty printer>
10492// -----------------------------------
10493
10494/// Get the next member sibling of a given class or union member DIE.
10495///
10496/// @param die the DIE to consider.
10497///
10498/// @param member out parameter. This is set to the next member
10499/// sibling, iff the function returns TRUE.
10500///
10501/// @return TRUE iff the function set @p member to the next member
10502/// sibling DIE.
10503static bool
10504get_next_member_sibling_die(const Dwarf_Die *die, Dwarf_Die *member)
10505{
10506 if (!die)
10507 return false;
10508
10509 bool found_member = false;
10510 for (found_member = (dwarf_siblingof(const_cast<Dwarf_Die*>(die),
10511 member) == 0);
10512 found_member;
10513 found_member = (dwarf_siblingof(member, member) == 0))
10514 {
10515 int tag = dwarf_tag(member);
10516 if (tag == DW_TAG_member || tag == DW_TAG_inheritance)
10517 break;
10518 }
10519
10520 return found_member;
10521}
10522
10523/// Get the first child DIE of a class/struct/union DIE that is a
10524/// member DIE.
10525///
10526/// Note that a member DIE is represented by a DWARF tag that is
10527/// either DW_TAG_member, DW_TAG_inheritance.
10528///
10529/// @param die the DIE to consider.
10530///
10531/// @param child out parameter. This is set to the first child DIE of
10532/// @p iff this function returns TRUE.
10533///
10534/// @return TRUE iff @p child is set to the first child DIE of @p die
10535/// that is a member DIE.
10536static bool
10537get_member_child_die(const Dwarf_Die *die, Dwarf_Die *child)
10538{
10539 if (!die)
10540 return false;
10541
10542 int tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
10543 ABG_ASSERT(tag == DW_TAG_structure_type
10544 || tag == DW_TAG_union_type
10545 || tag == DW_TAG_class_type);
10546
10547 bool found_child = (dwarf_child(const_cast<Dwarf_Die*>(die), child) == 0);
10548
10549 if (!found_child)
10550 return false;
10551
10552 tag = dwarf_tag(child);
10553
10554 if (!(tag == DW_TAG_member
10555 || tag == DW_TAG_inheritance
10556 || tag == DW_TAG_subprogram))
10557 found_child = get_next_member_sibling_die(child, child);
10558
10559 return found_child;
10560}
10561
10562/// Get the global scope associated to a given translation unit
10563/// context.
10564///
10565/// @param tu_ctxt the translation unit context to consider.
10566///
10567/// @return the global scope associated to @p tu_ctxt.
10568static scope_decl_sptr
10569get_global_scope(reader::tu_context_type_sptr& tu_ctxt)
10570{
10571 return tu_ctxt->get_tu()->get_global_scope();
10572}
10573
10574
10575/// Get the global scope associated to a given DIE.
10576///
10577/// If no global scope could be found for the DIE, the function
10578/// returns the global scope associated to a given translation unit,
10579/// as a fallback choice.
10580///
10581/// @param rdr the reader to consider.
10582///
10583/// @param tu_ctxt the context of the translation unit which global
10584/// scope is to be used a potential fallback, in case @p die doesn't
10585/// have an associated global scope.
10586///
10587/// @param die the DIE to consider.
10588///
10589/// @return the global scope.
10590static scope_decl_sptr
10591get_global_scope(reader& rdr,
10592 reader::tu_context_type_sptr& tu_ctxt,
10593 const Dwarf_Die* die)
10594{
10595 translation_unit_sptr tu = rdr.get_translation_unit_for_die(die);
10596
10597 if (!tu)
10598 // Some DIEs might not have an easily associated TU (i.e, the TU
10599 // they belong to). For instance, imported unit DIEs require some
10600 // computing to know which TU they actually belong to.
10601 //
10602 // However, all DW_TAG_compile_unit have an associated translation
10603 // unit object, created by reader::create_tu_building_task in
10604 // reader::read_debug_info_into_corpus.
10605 if (!die || dwarf_tag(const_cast<Dwarf_Die*>(die)) != DW_TAG_compile_unit)
10606 // For the other kinds of DIE, let's get the current TU that we
10607 // are building the IR for.
10608 tu = tu_ctxt->get_tu();
10609
10610 ABG_ASSERT(tu);
10611
10612 return tu->get_global_scope();;
10613}
10614
10615/// Return the abigail IR node representing the scope of a given DIE.
10616///
10617/// Note that it is the logical scope that is returned. That is, if
10618/// the DIE has a DW_AT_specification or DW_AT_abstract_origin
10619/// attribute, it's the scope of the referred-to DIE (via these
10620/// attributes) that is returned. In other words, its the scope of
10621/// the origin DIE that is returned.
10622///
10623/// Also note that if the current translation unit is from C, then
10624/// this returns the global scope.
10625///
10626/// @param rdr the dwarf reader to use.
10627///
10628/// @param dye the DIE to get the scope for.
10629///
10630/// @param called_from_public_decl is true if this function has been
10631/// initially called within the context of a public decl.
10632///
10633/// @param where_offset the offset of the DIE where we are "logically"
10634/// positionned at, in the DIE tree. This is useful when @p die is
10635/// e.g, DW_TAG_partial_unit that can be included in several places in
10636/// the DIE tree.
10637///
10638/// @return the resulting scope, or nil if could not be computed.
10639static scope_decl_sptr
10640get_scope_for_die(reader& rdr,
10641 Dwarf_Die* dye,
10642 bool called_for_public_decl,
10643 void* where_addr,
10644 reader::tu_context_type_sptr& tu_ctxt)
10645{
10646 Dwarf_Die origin_die_mem;
10647 Dwarf_Die *die = &origin_die_mem;
10648
10649 if (!die_origin_die(dye, origin_die_mem))
10650 // There was no origin DIE found, so let's make the "die" pointer
10651 // above point to the content of the input "dye".
10652 memcpy(&origin_die_mem, dye, sizeof(origin_die_mem));
10653
10654 translation_unit::language die_lang = translation_unit::LANG_UNKNOWN;
10655 get_die_language(die, die_lang);
10656 if (is_c_language(die_lang) || rdr.die_parent_map().empty())
10657 {
10658 // In compilation units originating from the C languages all
10659 // decls belong to the global namespace. This is generally the
10660 // case if Libabigail determined that no DIE -> parent map was
10661 // needed.
10662 ABG_ASSERT(dwarf_tag(die) != DW_TAG_member);
10663 return get_global_scope(rdr, tu_ctxt, die);
10664 }
10665
10666 int tag = dwarf_tag(die);
10667 Dwarf_Die parent_die;
10668 bool got_parent = false;
10669
10670 if (tag == DW_TAG_subprogram)
10671 {
10672 Dwarf_Die object_ptr_die;
10673 if (get_member_fn_class_die_from_object_pointer(die, parent_die,
10674 object_ptr_die))
10675 got_parent = true;
10676 }
10677
10678 if (!got_parent && !rdr.get_parent_die(die, parent_die,
10679 where_addr, tu_ctxt))
10680 return rdr.nil_scope();
10681
10682 if (dwarf_tag(&parent_die) == DW_TAG_compile_unit
10683 || dwarf_tag(&parent_die) == DW_TAG_partial_unit
10684 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
10685 {
10686 if (dwarf_tag(&parent_die) == DW_TAG_partial_unit
10687 || dwarf_tag(&parent_die) == DW_TAG_type_unit)
10688 return get_global_scope(tu_ctxt);
10689
10690 // For top level DIEs like DW_TAG_compile_unit, we just want to
10691 // return the global scope for the corresponding translation
10692 // unit. This must have been set by
10693 // build_translation_unit_and_add_to_ir if we already started to
10694 // build the translation unit of parent_die. Otherwise, just
10695 // return the global scope of the current translation unit.
10696
10697 return get_global_scope(rdr, tu_ctxt, &parent_die);
10698 }
10699
10702 if (dwarf_tag(&parent_die) == DW_TAG_array_type
10703 || dwarf_tag(&parent_die) == DW_TAG_lexical_block)
10704 // this is an entity defined in a scope that is either an array or
10705 // a lexical block inside a function. Normally, I would say that
10706 // this should be dropped. But I have seen cases where a typedef
10707 // DIE needed by a relevant ABI artifact is defined in an array
10708 // (if the ABI artifact is the array) or in the lexical block
10709 // where the ABI artifact is defined. Yeah, weird. So for those
10710 // cases, let's take/consider the scope of the array/lexical
10711 // block.
10712 {
10713 scope_decl_sptr s = get_scope_for_die(rdr, &parent_die,
10714 called_for_public_decl,
10715 where_addr, tu_ctxt);
10716 if (is_anonymous_type_die(die))
10717 // For an anonymous type that have nothing to do in a lexical
10718 // block or array type context, let's put it in the containing
10719 // namespace. That is, do not let it be in a containing class
10720 // or union where it has nothing to do.
10721 while (is_class_or_union_type(s))
10722 {
10723 if (!rdr.get_parent_die(&parent_die, parent_die,
10724 where_addr, tu_ctxt))
10725 return rdr.nil_scope();
10726 s = get_scope_for_die(rdr, &parent_die,
10727 called_for_public_decl,
10728 where_addr, tu_ctxt);
10729 }
10730 return s;
10731 }
10732 else
10733 d = build_ir_node_from_die(rdr, &parent_die,
10734 called_for_public_decl,
10735 where_addr, tu_ctxt,
10736 /*is_required_decl_spec=*/true);
10737 s = dynamic_pointer_cast<scope_decl>(d);
10738 if (!s)
10739 // this is an entity defined in someting that is not a scope.
10740 // Let's drop it.
10741 return rdr.nil_scope();
10742
10743 class_decl_sptr cl = dynamic_pointer_cast<class_decl>(d);
10744 if (cl && cl->get_is_declaration_only())
10745 {
10746 scope_decl_sptr scop =
10747 dynamic_pointer_cast<scope_decl>(cl->get_definition_of_declaration());
10748 if (scop)
10749 s = scop;
10750 else
10751 s = cl;
10752 }
10753 return s;
10754}
10755
10756/// Convert a DWARF constant representing the value of the
10757/// DW_AT_language property into the translation_unit::language
10758/// enumerator.
10759///
10760/// @param l the DWARF constant to convert.
10761///
10762/// @return the resulting translation_unit::language enumerator.
10764dwarf_language_to_tu_language(size_t l)
10765{
10766 switch (l)
10767 {
10768 case DW_LANG_C89:
10769 return translation_unit::LANG_C89;
10770 case DW_LANG_C99:
10771 return translation_unit::LANG_C99;
10772#ifdef HAVE_DW_LANG_C11_enumerator
10773 case DW_LANG_C11:
10774 return translation_unit::LANG_C11;
10775#endif
10776#ifdef HAVE_DW_LANG_C17
10777 case DW_LANG_C17:
10778 return translation_unit::LANG_C17;
10779#endif
10780#ifdef HAVE_DW_LANG_C23
10781 case DW_LANG_C23:
10782 return translation_unit::LANG_C23;
10783#endif
10784 case DW_LANG_C:
10785 return translation_unit::LANG_C;
10786#ifdef HAVE_DW_LANG_C_plus_plus_03_enumerator
10787 case DW_LANG_C_plus_plus_03:
10788 return translation_unit::LANG_C_plus_plus_03;
10789#endif
10790
10791#ifdef HAVE_DW_LANG_C_plus_plus_11_enumerator
10792 case DW_LANG_C_plus_plus_11:
10793 return translation_unit::LANG_C_plus_plus_11;
10794#endif
10795
10796#ifdef HAVE_DW_LANG_C_plus_plus_14_enumerator
10797 case DW_LANG_C_plus_plus_14:
10798 return translation_unit::LANG_C_plus_plus_14;
10799#endif
10800#ifdef HAVE_DW_LANG_C_plus_plus_17
10801 case DW_LANG_C_plus_plus_17:
10802 return translation_unit::LANG_C_plus_plus_17;
10803#endif
10804
10805#ifdef HAVE_DW_LANG_C_plus_plus_20
10806 case DW_LANG_C_plus_plus_20:
10807 return translation_unit::LANG_C_plus_plus_20;
10808#endif
10809#ifdef HAVE_DW_LANG_C_plus_plus_23
10810 case DW_LANG_C_plus_plus_23:
10811 return translation_unit::LANG_C_plus_plus_23;
10812#endif
10813 case DW_LANG_C_plus_plus:
10814 return translation_unit::LANG_C_plus_plus;
10815#ifdef HAVE_DW_LANG_D_enumerator
10816 case DW_LANG_D:
10817 return translation_unit::LANG_D;
10818#endif
10819#ifdef HAVE_DW_LANG_OCaml_enumerator
10820 case DW_LANG_OCaml:
10821 return translation_unit::LANG_OCaml;
10822#endif
10823#ifdef HAVE_DW_LANG_Go_enumerator
10824 case DW_LANG_Go:
10825 return translation_unit::LANG_Go;
10826#endif
10827#ifdef HAVE_DW_LANG_Rust_enumerator
10828 case DW_LANG_Rust:
10829 return translation_unit::LANG_Rust;
10830#endif
10831#ifdef HAVE_DW_LANG_Zig
10832 case DW_LANG_Zig:
10833 return translation_unit::LANG_Zig;
10834#endif
10835#ifdef HAVE_DW_LANG_Metal
10836 case DW_LANG_Metal:
10837 return translation_unit::LANG_Metal;
10838#endif
10839 case DW_LANG_Ada83:
10840 return translation_unit::LANG_Ada83;
10841 case DW_LANG_Ada95:
10842 return translation_unit::LANG_Ada95;
10843#ifdef HAVE_DW_LANG_Ada2005
10844 case DW_LANG_Ada2005:
10845 return translation_unit::LANG_Ada2005;
10846#endif
10847
10848#ifdef HAVE_DW_LANG_Ada2012
10849 case DW_LANG_Ada2012:
10850 return translation_unit::LANG_Ada2012;
10851#endif
10852 case DW_LANG_Cobol74:
10853 return translation_unit::LANG_Cobol74;
10854 case DW_LANG_Cobol85:
10855 return translation_unit::LANG_Cobol85;
10856 case DW_LANG_Fortran77:
10857 return translation_unit::LANG_Fortran77;
10858 case DW_LANG_Fortran90:
10859 return translation_unit::LANG_Fortran90;
10860 case DW_LANG_Fortran95:
10861 return translation_unit::LANG_Fortran95;
10862#ifdef HAVE_DW_LANG_Fortran18
10863 case DW_LANG_Fortran18:
10864 return translation_unit::LANG_Fortran18;
10865#endif
10866#ifdef HAVE_DW_LANG_Fortran23
10867 case DW_LANG_Fortran23:
10868 return translation_unit::LANG_Fortran23;
10869#endif
10870 case DW_LANG_Pascal83:
10871 return translation_unit::LANG_Pascal83;
10872 case DW_LANG_Modula2:
10873 return translation_unit::LANG_Modula2;
10874 case DW_LANG_Java:
10875 return translation_unit::LANG_Java;
10876#ifdef HAVE_DW_LANG_Kotlin
10877 case DW_LANG_Kotlin:
10878 return translation_unit::LANG_Kotlin;
10879#endif
10880 case DW_LANG_PLI:
10881 return translation_unit::LANG_PLI;
10882 case DW_LANG_ObjC:
10883 return translation_unit::LANG_ObjC;
10884 case DW_LANG_ObjC_plus_plus:
10885 return translation_unit::LANG_ObjC_plus_plus;
10886
10887#ifdef HAVE_DW_LANG_UPC_enumerator
10888 case DW_LANG_UPC:
10889 return translation_unit::LANG_UPC;
10890#endif
10891#ifdef HAVE_DW_LANG_Python_enumerator
10892 case DW_LANG_Python:
10893 return translation_unit::LANG_Python;
10894#endif
10895#ifdef HAVE_DW_LANG_Ruby
10896 case DW_LANG_Ruby:
10897 return translation_unit::LANG_Ruby;
10898#endif
10899#ifdef HAVE_DW_LANG_Mips_Assembler_enumerator
10900 case DW_LANG_Mips_Assembler:
10901 return translation_unit::LANG_Mips_Assembler;
10902#endif
10903#ifdef HAVE_DW_LANG_Assembly
10904 case DW_LANG_Assembly:
10905 return translation_unit::LANG_Assembly;
10906#endif
10907#ifdef HAVE_DW_LANG_Crystal
10908 case DW_LANG_Crystal:
10909 return translation_unit::LANG_Crystal;
10910#endif
10911#ifdef HAVE_DW_LANG_HIP
10912 case DW_LANG_HIP:
10913 return translation_unit::LANG_HIP;
10914#endif
10915#ifdef HAVE_DW_LANG_C_sharp
10916 case DW_LANG_C_sharp:
10917 return translation_unit::LANG_C_sharp;
10918#endif
10919#ifdef HAVE_DW_LANG_Mojo
10920 case DW_LANG_Mojo:
10921 return translation_unit::LANG_Mojo;
10922#endif
10923#ifdef HAVE_DW_LANG_GLSL
10924 case DW_LANG_GLSL:
10925 return translation_unit::LANG_GLSL;
10926#endif
10927#ifdef HAVE_DW_LANG_GLSL_ES
10928 case DW_LANG_GLSL_ES:
10929 return translation_unit::LANG_GLSL_ES;
10930#endif
10931#ifdef HAVE_DW_LANG_HLSL
10932 case DW_LANG_HLSL:
10933 return translation_unit::LANG_HLSL;
10934#endif
10935#ifdef HAVE_DW_LANG_OpenCL_CPP
10936 case DW_LANG_OpenCL_CPP:
10937 return translation_unit::LANG_OpenCL_CPP;
10938#endif
10939#ifdef HAVE_DW_LANG_CPP_for_OpenCL
10940 case DW_LANG_CPP_for_OpenCL:
10941 return translation_unit::LANG_CPP_for_OpenCL;
10942#endif
10943#ifdef HAVE_DW_LANG_SYCL
10944 case DW_LANG_SYCL:
10945 return translation_unit::LANG_SYCL;
10946#endif
10947#ifdef HAVE_DW_LANG_Odin
10948 case DW_LANG_Odin:
10949 return translation_unit::LANG_Odin;
10950#endif
10951#ifdef HAVE_DW_LANG_P4
10952 case DW_LANG_P4:
10953 return translation_unit::LANG_P4;
10954#endif
10955#ifdef HAVE_DW_LANG_Move
10956 case DW_LANG_Move:
10957 return translation_unit::LANG_Move;
10958#endif
10959#ifdef HAVE_DW_LANG_Hylo
10960 case DW_LANG_Hylo:
10961 return translation_unit::LANG_Hylo;
10962#endif
10963
10964 default:
10965 return translation_unit::LANG_UNKNOWN;
10966 }
10967}
10968
10969/// Get the default array lower bound value as defined by the DWARF
10970/// specification, version 4, depending on the language of the
10971/// translation unit.
10972///
10973/// @param l the language of the translation unit.
10974///
10975/// @return the default array lower bound value.
10976static uint64_t
10977get_default_array_lower_bound(translation_unit::language l)
10978{
10979 int value = 0;
10980 switch (l)
10981 {
10982 case translation_unit::LANG_UNKNOWN:
10983 case translation_unit::LANG_C89:
10984 case translation_unit::LANG_C99:
10985 case translation_unit::LANG_C11:
10986 case translation_unit::LANG_C17:
10987 case translation_unit::LANG_C23:
10988 case translation_unit::LANG_C:
10989 case translation_unit::LANG_C_plus_plus_03:
10990 case translation_unit::LANG_C_plus_plus_11:
10991 case translation_unit::LANG_C_plus_plus_14:
10992 case translation_unit::LANG_C_plus_plus_17:
10993 case translation_unit::LANG_C_plus_plus_20:
10994 case translation_unit::LANG_C_plus_plus_23:
10995 case translation_unit::LANG_C_plus_plus:
10996 case translation_unit::LANG_OCaml:
10997 case translation_unit::LANG_ObjC:
10998 case translation_unit::LANG_ObjC_plus_plus:
10999 case translation_unit::LANG_D:
11000 case translation_unit::LANG_Rust:
11001 case translation_unit::LANG_Go:
11002 case translation_unit::LANG_Zig:
11003 case translation_unit::LANG_Metal:
11004 case translation_unit::LANG_Java:
11005 case translation_unit::LANG_Kotlin:
11006 case translation_unit::LANG_Python:
11007 case translation_unit::LANG_Ruby:
11008 case translation_unit::LANG_UPC:
11009 case translation_unit::LANG_Mips_Assembler:
11010 case translation_unit::LANG_Assembly:
11011 case translation_unit::LANG_Crystal:
11012 case translation_unit::LANG_HIP:
11013 case translation_unit::LANG_C_sharp:
11014 case translation_unit::LANG_Mojo:
11015 case translation_unit::LANG_GLSL:
11016 case translation_unit::LANG_GLSL_ES:
11017 case translation_unit::LANG_HLSL:
11018 case translation_unit::LANG_Odin:
11019 case translation_unit::LANG_P4:
11020 case translation_unit::LANG_OpenCL_CPP:
11021 case translation_unit::LANG_CPP_for_OpenCL:
11022 case translation_unit::LANG_SYCL:
11023 case translation_unit::LANG_Move:
11024 case translation_unit::LANG_Hylo:
11025 value = 0;
11026 break;
11027 case translation_unit::LANG_Cobol74:
11028 case translation_unit::LANG_Cobol85:
11029 case translation_unit::LANG_Fortran77:
11030 case translation_unit::LANG_Fortran90:
11031 case translation_unit::LANG_Fortran95:
11032 case translation_unit::LANG_Fortran18:
11033 case translation_unit::LANG_Fortran23:
11034 case translation_unit::LANG_Ada83:
11035 case translation_unit::LANG_Ada95:
11036 case translation_unit::LANG_Ada2005:
11037 case translation_unit::LANG_Ada2012:
11038 case translation_unit::LANG_Pascal83:
11039 case translation_unit::LANG_Modula2:
11040 case translation_unit::LANG_PLI:
11041 value = 1;
11042 break;
11043 }
11044
11045 return value;
11046}
11047
11048/// For a given DIE address, find the lower bound of a sorted vector
11049/// of imported unit point addressed.
11050///
11051/// The lower bound is the smallest point (the point with the smallest
11052/// offset) which is the greater than a given offset.
11053///
11054/// @param imported_unit_points_type the sorted vector of imported
11055/// unit points.
11056///
11057/// @param val the offset to consider when looking for the lower
11058/// bound.
11059///
11060/// @param r an iterator to the lower bound found. This parameter is
11061/// set iff the function returns true.
11062///
11063/// @return true iff the lower bound has been found.
11064static bool
11065find_lower_bound_in_imported_unit_points(const imported_unit_points_type& p,
11066 const void* val,
11067 imported_unit_points_type::const_iterator& r)
11068{
11069 imported_unit_point v(val);
11070 imported_unit_points_type::const_iterator result =
11071 std::lower_bound(p.begin(), p.end(), v);
11072
11073 bool is_ok = result != p.end();
11074
11075 if (is_ok)
11076 r = result;
11077
11078 return is_ok;
11079}
11080
11081/// Build the IR nodes for a DW_TAG_imported_unit DIE.
11082///
11083/// This function inspects the DIE referenced by the DW_AT_import
11084/// attribute and recursively walks the tree of potential
11085/// DW_TAG_imported_unit from there. When the function encounters
11086/// DIEs for decls and types, it builds the IR nodes for them and add
11087/// them to the current translation unit context it's operating on.
11088///
11089/// @param rdr the DWARF reader to using.
11090///
11091/// @param die the DIE for the DW_TAG_imported_unit to consider.
11092///
11093/// @param tu_ctxt the current translation unit context we are
11094/// operating in.
11095///
11096/// @return true iff the function could find a proper
11097/// DW_TAG_imported_unit with a proper DW_AT_import attribute that has
11098/// children DIEs that could be processed.
11099static bool
11100build_ir_nodes_from_imported_unit(reader& rdr,
11101 Dwarf_Die* die,
11102 reader::tu_context_type_sptr tu_ctxt)
11103{
11104 Dwarf_Die unit_to_import;
11105 if (!die_die_attribute(die, DW_AT_import, unit_to_import))
11106 return false;
11107
11108 Dwarf_Die child;
11109 if (dwarf_child(&unit_to_import, &child) != 0)
11110 return false;
11111
11112 string s = die_string_attribute(&unit_to_import, DW_AT_comp_dir);
11113
11114 scope_decl_sptr scope = tu_ctxt->get_tu()->get_global_scope();
11115
11116 do
11117 do_handle_dwarf_die(rdr, tu_ctxt, child, die_is_public_decl(&child));
11118 while (dwarf_siblingof(&child, &child) == 0);
11119
11120 return true;
11121}
11122
11123static bool
11124do_handle_dwarf_die(reader& rdr, reader::tu_context_type_sptr tu_ctxt,
11125 Dwarf_Die& die, bool die_is_public)
11126{
11127 int tag = dwarf_tag(&die);
11128
11129 if ((rdr.load_undefined_interfaces()
11130 && (rdr.is_decl_die_with_undefined_symbol(&die)
11131 || rdr.is_decl_die_with_exported_symbol(&die)))
11132 || tag == DW_TAG_namespace)
11133 {
11134 // Analyze undefined functions & variables for the purpose of
11135 // analyzing compatibility matters.
11136 build_ir_node_from_die(rdr, &die,
11137 // Pretend the DIE is publicly defined
11138 // so that types that are reachable
11139 // from it get analyzed as well.
11140 /*die_is_public=*/true,
11141 die.addr, tu_ctxt);
11142 return true;
11143 }
11144 else if (rdr.is_decl_die_with_exported_symbol(&die)
11145 || (!rdr.env().analyze_exported_interfaces_only()
11146 && die_is_decl(&die))
11147 || (rdr.env().load_all_types() && die_is_type(&die)))
11148 {
11149 // Analyze all the DIEs we encounter unless we are asked to only
11150 // analyze exported interfaces and the types reachables from
11151 // them.
11152 build_ir_node_from_die(rdr, &die,
11153 die_is_public,
11154 die.addr, tu_ctxt);
11155 return true;
11156 }
11157 else if (tag == DW_TAG_imported_unit)
11158 {
11159 build_ir_nodes_from_imported_unit(rdr, &die, tu_ctxt);
11160 return true;
11161 }
11162 return false;
11163}
11164
11165/// Given a DW_TAG_compile_unit, build and return the corresponding
11166/// abigail::translation_unit ir node. Note that this function
11167/// recursively reads the children dies of the current DIE and
11168/// populates the resulting translation unit.
11169///
11170/// @param rdr the DWARF reader to use.
11171///
11172/// @param die the DW_TAG_compile_unit DIE to consider.
11173///
11174/// @param address_size the size of the addresses expressed in this
11175/// translation unit in general.
11176///
11177/// @return a pointer to the resulting translation_unit.
11178static void
11179build_translation_unit_and_add_to_ir(reader& rdr,
11180 Dwarf_Die die,
11182{
11183 reader::tu_context_type_sptr result;
11184
11185 ABG_ASSERT(tu);
11186 result.reset(new reader::tu_context_type(die, tu));
11187
11188 Dwarf_Die child;
11189 if (dwarf_child(&die, &child) != 0)
11190 // Empty translation unit, so nothing to be done.
11191 return;
11192
11193 result->get_tu()->set_is_constructed(false);
11194
11195 do
11196 do_handle_dwarf_die(rdr, result, child, die_is_public_decl(&child));
11197 while (dwarf_siblingof(&child, &child) == 0);
11198
11199 if (!result->var_decls_to_re_add_to_tree().empty())
11200 for (list<var_decl_sptr>::const_iterator v =
11201 result->var_decls_to_re_add_to_tree().begin();
11202 v != result->var_decls_to_re_add_to_tree().end();
11203 ++v)
11204 {
11205 if (is_member_decl(*v))
11206 continue;
11207
11208 ABG_ASSERT((*v)->get_scope());
11209 string demangled_name =
11210 demangle_cplus_mangled_name((*v)->get_linkage_name());
11211 if (!demangled_name.empty())
11212 {
11213 std::list<string> fqn_comps;
11214 fqn_to_components(demangled_name, fqn_comps);
11215 string mem_name = fqn_comps.back();
11216 fqn_comps.pop_back();
11217 class_decl_sptr class_type;
11218 string ty_name;
11219 if (!fqn_comps.empty())
11220 {
11221 ty_name = components_to_type_name(fqn_comps);
11222 class_type =
11223 lookup_type<class_decl>(ty_name, *result->get_tu());
11224 }
11225 if (class_type)
11226 {
11227 // So we are seeing a member variable for which there
11228 // is a global variable definition DIE not having a
11229 // reference attribute pointing back to the member
11230 // variable declaration DIE. Thus remove the global
11231 // variable definition from its current non-class
11232 // scope ...
11233 decl_base_sptr d;
11234 if ((d = lookup_var_decl_in_scope(mem_name, class_type)))
11235 // This is the data member with the same name in cl.
11236 // We just need to flag it as static.
11237 ;
11238 else
11239 {
11240 // In this case there is no data member with the
11241 // same name in cl already. Let's add it there then
11242 // ...
11244 d = add_decl_to_scope(*v, class_type);
11245 }
11246
11247 ABG_ASSERT(dynamic_pointer_cast<var_decl>(d));
11248 // Let's flag the data member as static.
11249 set_member_is_static(d, true);
11250 }
11251 }
11252 }
11253
11254 result->var_decls_to_re_add_to_tree().clear();
11255
11256 result->get_tu()->set_is_constructed(true);
11257}
11258
11259/// Build a abigail::namespace_decl out of a DW_TAG_namespace or
11260/// DW_TAG_module (for fortran) DIE.
11261///
11262/// Note that this function connects the DW_TAG_namespace to the IR
11263/// being currently created, reads the children of the DIE and
11264/// connects them to the IR as well.
11265///
11266/// @param rdr the DWARF reader to use.
11267///
11268/// @param die the DIE to read from. Must be either DW_TAG_namespace
11269/// or DW_TAG_module.
11270///
11271/// @param where_offset the offset of the DIE where we are "logically"
11272/// positionned at, in the DIE tree. This is useful when @p die is
11273/// e.g, DW_TAG_partial_unit that can be included in several places in
11274/// the DIE tree.
11275///
11276/// @return the resulting @ref abigail::namespace_decl or NULL if it
11277/// couldn't be created.
11279build_namespace_decl_and_add_to_ir(reader& rdr,
11280 Dwarf_Die* die,
11281 void* where_addr,
11282 reader::tu_context_type_sptr& tu_ctxt)
11283{
11284 namespace_decl_sptr result;
11285
11286 if (!die)
11287 return result;
11288
11289 unsigned tag = dwarf_tag(die);
11290 if (tag != DW_TAG_namespace && tag != DW_TAG_module)
11291 return result;
11292
11293 scope_decl_sptr scope = get_scope_for_die(rdr, die,
11294 /*called_for_public_decl=*/false,
11295 where_addr, tu_ctxt);
11296
11297 if ((result =
11298 is_namespace(rdr.lookup_artifact_from_die(die,/*is_type=*/false))))
11299 return result;
11300
11301 string name, linkage_name;
11302 location loc;
11303 die_loc_and_name(die, tu_ctxt, loc, name, linkage_name);
11304
11305 result.reset(new namespace_decl(rdr.env(), name, loc));
11306 result = is_namespace(rdr.maybe_associate_die_to_decl(die, result));
11307 add_decl_to_scope(result, scope);
11308
11309 Dwarf_Die child;
11310 if (dwarf_child(die, &child) != 0)
11311 return result;
11312
11313 tu_ctxt->scope_stack().push(result);
11314 do
11315 do_handle_dwarf_die(rdr, tu_ctxt, child,
11316 // If this namespace DIE is private
11317 // (anonymous) then all its content is
11318 // considered private. Otherwise, its
11319 // public decls are considered public.
11320 /*called_from_public_decl=*/
11321 die_is_public_decl(die)
11322 && die_is_public_decl(&child));
11323 while (dwarf_siblingof(&child, &child) == 0);
11324 tu_ctxt->scope_stack().pop();
11325
11326 return result;
11327}
11328
11329/// Build a @ref type_decl out of a DW_TAG_base_type DIE.
11330///
11331/// @param rdr the DWARF reader to use.
11332///
11333/// @param die the DW_TAG_base_type to consider.
11334///
11335/// @param where_offset where we are logically at in the DIE stream.
11336///
11337/// @return the resulting decl_base_sptr.
11338static type_decl_sptr
11339build_type_decl(reader& rdr, Dwarf_Die* die,
11340 reader::tu_context_type_sptr& tu_ctxt)
11341{
11342 type_decl_sptr result;
11343
11344 if (!die)
11345 return result;
11346 ABG_ASSERT(dwarf_tag(die) == DW_TAG_base_type);
11347
11348 if ((result = is_type_decl(rdr.lookup_type_artifact_from_die(die))))
11349 return result;
11350
11351 uint64_t byte_size = 0, bit_size = 0;
11352 if (!die_unsigned_constant_attribute(die, DW_AT_byte_size, byte_size))
11353 if (!die_unsigned_constant_attribute(die, DW_AT_bit_size, bit_size))
11354 return result;
11355
11356 if (bit_size == 0 && byte_size != 0)
11357 // Update the bit size.
11358 bit_size = byte_size * 8;
11359
11360 string type_name, linkage_name;
11361 location loc;
11362 die_loc_and_name(die, tu_ctxt, loc, type_name, linkage_name);
11363
11364 if (byte_size == 0)
11365 {
11366 // The size of the type is zero, that must mean that we are
11367 // looking at the definition of the void type.
11368 if (type_name == "void")
11369 result = is_type_decl(build_ir_node_for_void_type(rdr, tu_ctxt));
11370 else
11371 // A type of size zero that is not void? Hmmh, I am not sure
11372 // what that means. Return nil for now.
11373 return result;
11374 }
11375
11376 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
11377 {
11378 string normalized_type_name = type_name;
11379 real_type real_type;
11380 if (parse_real_type(type_name, real_type))
11381 normalized_type_name = real_type.to_string();
11382 result = lookup_type<type_decl>(normalized_type_name, *corp);
11383 }
11384
11385 if (!result)
11386 if (corpus_sptr corp = rdr.corpus())
11387 result = lookup_type<type_decl>(type_name, *corp);
11388 if (!result)
11389 result.reset(new type_decl(rdr.env(), type_name, bit_size,
11390 /*alignment=*/0, loc, linkage_name));
11391 return result;
11392}
11393
11394/// Construct the type that is to be used as the underlying type of an
11395/// enum.
11396///
11397/// @param rdr the DWARF reader to use.
11398///
11399/// @param enum_name the name of the enum that this type is going to
11400/// be the underlying type of.
11401///
11402/// @param enum_size the size of the enum.
11403///
11404/// @param is_anonymous whether the underlying type is anonymous or
11405/// not. By default, this should be set to true as before c++11 (and
11406/// in C), it's almost the case.
11407static type_decl_sptr
11408build_enum_underlying_type(reader& rdr,
11409 string enum_name,
11410 uint64_t enum_size,
11411 reader::tu_context_type_sptr& tu_ctxt,
11412 bool is_anonymous = true)
11413{
11414 string underlying_type_name =
11415 build_internal_underlying_enum_type_name(enum_name, is_anonymous,
11416 enum_size);
11417
11418 type_decl_sptr result(new type_decl(rdr.env(), underlying_type_name,
11419 enum_size, enum_size, location()));
11420 result->set_is_anonymous(is_anonymous);
11421 result->set_is_artificial(true);
11422 translation_unit_sptr tu = tu_ctxt->get_tu();
11423 decl_base_sptr d = add_decl_to_scope(result, tu->get_global_scope());
11424 result = dynamic_pointer_cast<type_decl>(d);
11425 ABG_ASSERT(result);
11426 maybe_canonicalize_type(result, rdr);
11427 return result;
11428}
11429
11430/// Build an enum_type_decl from a DW_TAG_enumeration_type DIE.
11431///
11432/// @param rdr the DWARF reader to use.
11433///
11434/// @param die the DIE to read from.
11435///
11436/// @param is_declaration_only is true if the DIE denoted by @p die is
11437/// a declaration-only DIE.
11438///
11439/// @return the built enum_type_decl or NULL if it could not be built.
11441build_enum_type(reader& rdr,
11442 Dwarf_Die* die,
11443 reader::tu_context_type_sptr& tu_ctxt,
11444 bool is_declaration_only)
11445{
11446 enum_type_decl_sptr result;
11447 if (!die)
11448 return result;
11449
11450 unsigned tag = dwarf_tag(die);
11451 if (tag != DW_TAG_enumeration_type)
11452 return result;
11453
11454 string name, linkage_name;
11455 location loc;
11456 die_loc_and_name(die, tu_ctxt, loc, name, linkage_name);
11457
11458 bool is_anonymous = false;
11459 // If the enum is anonymous, let's give it a name.
11460 if (name.empty())
11461 {
11462 name = get_internal_anonymous_die_prefix_name(die);
11463 ABG_ASSERT(!name.empty());
11464 // But we remember that the type is anonymous.
11465 is_anonymous = true;
11466 }
11467
11468 bool use_odr = rdr.odr_is_relevant(die, tu_ctxt);
11469 // If the type has location, then associate it to its
11470 // representation. This way, all occurences of types with the same
11471 // representation (name) and location can be later detected as being
11472 // for the same type.
11473
11474 if (!is_anonymous)
11475 {
11476 if (use_odr)
11477 {
11478 if (enum_type_decl_sptr pre_existing_enum =
11479 is_enum_type(rdr.lookup_artifact_from_die(die)))
11480 result = pre_existing_enum;
11481 }
11482 else if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
11483 {
11484 if (loc)
11485 result = lookup_enum_type_per_location(loc.expand(), *corp);
11486 }
11487 else if (loc)
11488 {
11489 if (enum_type_decl_sptr pre_existing_enum =
11490 is_enum_type(rdr.lookup_artifact_from_die(die)))
11491 if (pre_existing_enum->get_location() == loc)
11492 result = pre_existing_enum;
11493 }
11494
11495 if (result)
11496 return result;
11497 }
11498 // TODO: for anonymous enums, maybe have a map of loc -> enums so that
11499 // we can look them up?
11500
11501 uint64_t size = 0;
11502 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
11503 size *= 8;
11504 bool is_artificial = die_is_artificial(die);
11505
11506 // for now we consider that underlying types of enums are all anonymous
11507 bool enum_underlying_type_is_anonymous= true;
11508
11510 Dwarf_Die child;
11511 if (dwarf_child(die, &child) == 0)
11512 {
11513 do
11514 {
11515 if (dwarf_tag(&child) != DW_TAG_enumerator)
11516 continue;
11517
11518 string n, m;
11519 location l;
11520 die_loc_and_name(&child, tu_ctxt, l, n, m);
11521 uint64_t val = 0;
11522 die_unsigned_constant_attribute(&child, DW_AT_const_value, val);
11523 enms.push_back(enum_type_decl::enumerator(n, val));
11524 }
11525 while (dwarf_siblingof(&child, &child) == 0);
11526 }
11527
11528 // DWARF up to version 4 (at least) doesn't seem to carry the
11529 // underlying type, so let's create an artificial one here, which
11530 // sole purpose is to be passed to the constructor of the
11531 // enum_type_decl type.
11532 type_decl_sptr t =
11533 build_enum_underlying_type(rdr, name, size, tu_ctxt,
11534 enum_underlying_type_is_anonymous);
11535
11536 result.reset(new enum_type_decl(name, loc, t, enms, linkage_name));
11537 result->set_is_anonymous(is_anonymous);
11538 result->set_is_declaration_only(is_declaration_only);
11539 t->set_is_declaration_only(is_declaration_only);
11540 result->set_is_artificial(is_artificial);
11541
11542 return result;
11543}
11544
11545/// Once a function_decl has been built and added to a class as a
11546/// member function, this function updates the information of the
11547/// function_decl concerning the properties of its relationship with
11548/// the member class. That is, it updates properties like
11549/// virtualness, access, constness, cdtorness, etc ...
11550///
11551/// @param die the DIE of the function_decl that has been just built.
11552///
11553/// @param f the function_decl that has just been built from @p die.
11554///
11555/// @param klass the @ref class_or_union that @p f belongs to.
11556///
11557/// @param rdr the context used to read the ELF/DWARF information.
11558static void
11559finish_member_function_reading(Dwarf_Die* die,
11560 const function_decl_sptr& f,
11561 const class_or_union_sptr klass,
11562 reader& rdr)
11563{
11564 ABG_ASSERT(klass);
11565
11566 method_decl_sptr m = is_method_decl(f);
11567 ABG_ASSERT(m);
11568
11569 method_type_sptr method_t = is_method_type(m->get_type());
11570 ABG_ASSERT(method_t);
11571
11572 size_t is_inline = die_is_declared_inline(die);
11573 bool is_ctor = (f->get_name() == klass->get_name());
11574 bool is_dtor = (!f->get_name().empty()
11575 && static_cast<string>(f->get_name())[0] == '~');
11576 bool is_virtual = die_is_virtual(die);
11577 int64_t vindex = -1;
11578 if (is_virtual)
11579 die_virtual_function_index(die, vindex);
11580 access_specifier access = public_access;
11581 if (class_decl_sptr c = is_class_type(klass))
11582 if (!c->is_struct())
11583 access = private_access;
11584 die_access_specifier(die, access);
11585
11586 m->is_declared_inline(is_inline);
11587 set_member_access_specifier(m, access);
11588 if (is_virtual)
11589 set_member_function_virtuality(m, is_virtual, vindex);
11590 bool is_static = method_t->get_is_for_static_method();
11591 set_member_is_static(m, is_static);
11592 set_member_function_is_ctor(m, is_ctor);
11593 set_member_function_is_dtor(m, is_dtor);
11594 set_member_function_is_const(m, method_t->get_is_const());
11595
11597
11598 if (is_virtual && !f->get_linkage_name().empty() && !f->get_symbol()
11600 {
11601 // This is a virtual member function which has a linkage name
11602 // but has no underlying symbol set.
11603 //
11604 // The underlying elf symbol to set to this function can show up
11605 // later in the DWARF input or it can be that, because of some
11606 // compiler optimization, the relation between this function and
11607 // its underlying elf symbol is simply not emitted in the DWARF.
11608 //
11609 // Let's thus schedule this function for a later fixup pass
11610 // (performed by
11611 // reader::fixup_functions_with_no_symbols()) that will
11612 // set its underlying symbol.
11613 //
11614 // Note that if the underlying symbol is encountered later in
11615 // the DWARF input, then the part of build_function_decl() that
11616 // updates the function to set its underlying symbol will
11617 // de-schedule this function wrt fixup pass.
11618 rdr.record_a_fn_decl_with_no_symbol(die, f);
11619 }
11620}
11621
11622/// Test if a DIE represents a function that is a member of a given
11623/// class type.
11624///
11625/// @param rdr the DWARF reader.
11626///
11627/// @param function_die the DIE of the function to consider.
11628///
11629/// @param class_type the class type to consider.
11630///
11631/// @param where_offset where we are logically at in the DIE stream.
11632///
11633/// @return the method declaration corresponding to the member
11634/// function of @p class_type, iff @p function_die is for a member
11635/// function of @p class_type.
11636static method_decl_sptr
11637is_function_for_die_a_member_of_class(reader& rdr,
11638 Dwarf_Die* function_die,
11639 const class_or_union_sptr& class_type)
11640{
11641 type_or_decl_base_sptr artifact = rdr.lookup_artifact_from_die(function_die);
11642
11643 if (!artifact)
11644 return method_decl_sptr();
11645
11646 method_decl_sptr method = is_method_decl(artifact);
11647 method_type_sptr method_type;
11648 scope_decl_sptr method_scope = nullptr;
11649
11650 if (method)
11651 {
11652 method_type = method->get_type();
11653 method_scope = method->get_scope();
11654 }
11655 else
11656 method_type = is_method_type(artifact);
11657
11658 ABG_ASSERT(method_type);
11659
11660 class_or_union_sptr method_class = method_type->get_class_type();
11661 ABG_ASSERT(method_class);
11662
11663 if (method_class.get() == class_type.get())
11664 {
11665 if (!method_scope
11666 || (method_scope.get() == method_class.get()))
11667 return method;
11668 }
11669
11670 return nullptr;
11671}
11672
11673/// If a given function DIE represents an existing member function of
11674/// a given class, then update that member function with new
11675/// properties present in the DIE. Otherwise, if the DIE represents a
11676/// new member function that is not already present in the class then
11677/// add that new member function to the class.
11678///
11679/// @param rdr the DWARF reader.
11680///
11681/// @param function_die the DIE of the potential member function to
11682/// consider.
11683///
11684/// @param class_type the class type to consider.
11685///
11686/// @param called_from_public_decl is true iff this function was
11687/// called from a publicly defined and exported declaration.
11688///
11689/// @param where_offset where we are logically at in the DIE stream.
11690///
11691/// @return the method decl representing the member function.
11692static method_decl_sptr
11693add_or_update_member_function(reader& rdr,
11694 Dwarf_Die* function_die,
11695 const class_or_union_sptr class_type,
11696 bool called_from_public_decl,
11697 void* where,
11698 reader::tu_context_type_sptr& tu_ctxt)
11699{
11700 if (!function_die || dwarf_tag(function_die) != DW_TAG_subprogram)
11701 return nullptr;
11702
11703 method_decl_sptr method =
11704 is_function_for_die_a_member_of_class(rdr, function_die, class_type);
11705
11706 if (method)
11707 {
11708 class_or_union_sptr scope = is_class_or_union_type(method->get_scope());
11709 ABG_ASSERT(!scope || scope.get() == class_type.get());
11710 }
11711 else
11712 if (!potential_member_fn_should_be_dropped(rdr, tu_ctxt, function_die))
11713 {
11714 string linkage_name = die_linkage_name(function_die);
11715 if (!linkage_name.empty())
11716 method = class_type->find_member_function_sptr(linkage_name);
11717
11718 if (!method)
11719 method = is_method_decl(build_ir_node_from_die(rdr, function_die,
11720 class_type,
11721 called_from_public_decl,
11722 where, tu_ctxt));
11723 }
11724 if (!method)
11725 return method_decl_sptr();
11726
11727 if (method)
11728 {
11729 if (!tu_ctxt->is_wip_function_type_die(function_die))
11730 finish_member_function_reading(function_die,
11731 is_function_decl(method),
11732 class_type, rdr);
11733 else
11734 {
11735 ABG_ASSERT(method->get_scope());
11736 rdr.schedule_method_to_finish_reading(*function_die, method);
11737 }
11738 }
11739
11740 return method;
11741}
11742
11743/// Build a an IR node for class type from a DW_TAG_structure_type or
11744/// DW_TAG_class_type DIE and add that node to the ABI corpus being
11745/// currently built.
11746///
11747/// If the represents class type that already exists, then update the
11748/// existing class type with the new properties found in the DIE.
11749///
11750/// It meanst that this function can also update an existing
11751/// class_decl node with data members, member functions and other
11752/// properties coming from the DIE.
11753///
11754/// @param rdr the DWARF reader to consider.
11755///
11756/// @param die the DIE to read information from. Must be either a
11757/// DW_TAG_structure_type or a DW_TAG_class_type.
11758///
11759/// @param scope a pointer to the scope_decl under which this class
11760/// is to be added to.
11761///
11762/// @param is_struct whether the class was declared as a struct.
11763///
11764/// @param klass if non-null, this is a klass to append the members
11765/// to. Otherwise, this function just builds the class from scratch.
11766///
11767/// @param called_from_public_decl set to true if this class is being
11768/// called from a "Public declaration like vars or public symbols".
11769///
11770/// @param where_offset the offset of the DIE where we are "logically"
11771/// positionned at, in the DIE tree. This is useful when @p die is
11772/// e.g, DW_TAG_partial_unit that can be included in several places in
11773/// the DIE tree.
11774///
11775/// @param is_declaration_only is true if the DIE denoted by @p die is
11776/// a declaration-only DIE.
11777///
11778/// @return the resulting class_type.
11779static class_decl_sptr
11780add_or_update_class_type(reader& rdr,
11781 Dwarf_Die* die,
11782 bool is_struct,
11783 class_decl_sptr klass,
11784 bool called_from_public_decl,
11785 void* where,
11786 bool is_declaration_only,
11787 reader::tu_context_type_sptr& tu_ctxt)
11788{
11789 class_decl_sptr result;
11790 if (!die)
11791 return result;
11792
11793 unsigned tag = dwarf_tag(die);
11794
11795 if (tag != DW_TAG_class_type && tag != DW_TAG_structure_type)
11796 return result;
11797
11798 if ((result = is_class_type(tu_ctxt->lookup_wip_type_from_die(die))))
11799 return result;
11800
11801 string name, linkage_name;
11802 location loc;
11803 die_loc_and_name(die, tu_ctxt, loc, name, linkage_name);
11804 cleanup_decl_name(name);
11805
11806 bool is_anonymous = false;
11807 if (name.empty())
11808 {
11809 // So we are looking at an anonymous struct. Let's
11810 // give it a name.
11811 name = get_internal_anonymous_die_prefix_name(die);
11812 ABG_ASSERT(!name.empty());
11813 // But we remember that the type is anonymous.
11814 is_anonymous = true;
11815 }
11816
11817 if (!is_anonymous)
11818 {
11819 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
11820 {
11821 if (loc)
11822 // TODO: if there is only one class defined in the corpus
11823 // for this location, then re-use it. But if there are
11824 // more than one, then do not re-use it, for now.
11825 result = lookup_class_type_per_location(loc.expand(), *corp);
11826 else
11827 // TODO: if there is just one class for that name defined,
11828 // then re-use it. Otherwise, don't.
11829 result = lookup_type<class_decl>(name, *corp);
11830 if (result
11831 // If we are seeing a declaration of a definition we
11832 // already had, or if we are seing a type with the same
11833 // declaration-only-ness that we had before, then keep
11834 // the one we already had.
11835 && (result->get_is_declaration_only() == is_declaration_only
11836 || (!result->get_is_declaration_only()
11837 && is_declaration_only)))
11838 {
11839 result = is_class_type(rdr.maybe_associate_die_to_type(die, result));
11840 return result;
11841 }
11842 else
11843 // We might be seeing the definition of a declaration we
11844 // already had. In that case, keep the definition and
11845 // drop the declaration.
11846 result.reset();
11847 }
11848 }
11849
11850
11851 uint64_t size = 0;
11852 die_size_in_bits(die, size);
11853 bool is_artificial = die_is_artificial(die);
11854
11855 Dwarf_Die child;
11856 bool has_child = (dwarf_child(die, &child) == 0);
11857
11858 if (klass)
11859 {
11860 // We are amending a class that was built before.
11861 result = klass;
11862
11863 if (has_child && klass->get_is_declaration_only()
11864 && klass->get_definition_of_declaration())
11865 result = is_class_type(klass->get_definition_of_declaration());
11866
11867 if (loc)
11868 result->set_location(loc);
11869
11870 // Let's check if we need to amend its "declaration-only-ness"
11871 // status.
11872
11873 if (!!result->get_size_in_bits() == result->get_is_declaration_only())
11874 // The size of the class doesn't match its
11875 // 'declaration-only-ness". We might have a non-zero sized
11876 // class which is declaration-only, or a zero sized class that
11877 // is not declaration-only. Let's set the declaration-only-ness
11878 // according to what we are instructed to.
11879 //
11880 // Note however that there are binaries out there emitted by
11881 // compilers (Clang, in C++) emit declarations-only classes that
11882 // have non-zero size. So we must honor these too. That is why
11883 // we are not forcing the declaration-only-ness to false when a
11884 // class has non-zero size. An example of such binary is
11885 // tests/data/test-diff-filter/test41-PR21486-abg-writer.llvm.o.
11886 result->set_is_declaration_only(is_declaration_only);
11887 }
11888 else
11889 result.reset(new class_decl(rdr.env(), name, size,
11890 /*alignment=*/0, is_struct, loc,
11891 decl_base::VISIBILITY_DEFAULT,
11892 is_anonymous));
11893
11894 ABG_ASSERT(result);
11895
11896 result->set_is_declaration_only(is_declaration_only);
11897
11898 // If a new class being built (or a decl-only class that we are
11899 // amending) has a size that doesn't match what the current DIE is
11900 // adverstising, let's update the size of the class being build.
11901 if (!klass || klass->get_is_declaration_only())
11902 if (size != result->get_size_in_bits())
11903 result->set_size_in_bits(size);
11904
11905 // If a non-decl-only class has children node and is advertized as
11906 // having a non-zero size let's trust that.
11907 if (!result->get_is_declaration_only() && has_child)
11908 if (result->get_size_in_bits() == 0 && size != 0)
11909 result->set_size_in_bits(size);
11910
11911 result->set_is_artificial(is_artificial);
11912
11913 if (!has_child)
11914 // TODO: set the access specifier for the declaration-only class
11915 // here.
11916 return result;
11917
11918 tu_ctxt->mark_class_or_union_die_as_wip(die, result);
11919
11920 bool is_incomplete_type = false;
11921 if (is_declaration_only && size == 0 && has_child)
11922 // this is an incomplete DWARF type as defined by [5.7.1]
11923 //
11924 // An incomplete structure, union or class type is represented by
11925 // a structure, union or class entry that does not have a byte
11926 // size attribute and that has a DW_AT_declaration attribute.
11927 //
11928 // Let's consider that it's thus a decl-only class, likely
11929 // referred to by a pointer. If we later encounter a definition
11930 // for this decl-only class type, then this decl-only class will
11931 // be resolved to it by the code in
11932 // reader::resolve_declaration_only_classes.
11933 is_incomplete_type = true;
11934
11935 scope_decl_sptr scop =
11936 dynamic_pointer_cast<scope_decl>(result);
11937
11938 ABG_ASSERT(scop);
11939 tu_ctxt->scope_stack().push(scop);
11940
11941 if (has_child && !is_incomplete_type)
11942 {
11943 do
11944 {
11945 tag = dwarf_tag(&child);
11946
11947 // Handle base classes.
11948 if (tag == DW_TAG_inheritance)
11949 {
11950 result->set_is_declaration_only(false);
11951
11952 Dwarf_Die type_die;
11953 if (!die_die_attribute(&child, DW_AT_type, type_die))
11954 continue;
11955
11956 string type_name = die_type_name(rdr, &type_die,
11957 /*qualified_name=*/true,
11958 where, tu_ctxt);
11959 type_base_sptr base_type;
11960 if (!type_name.empty())
11961 {
11962 base_type = result->find_base_class(type_name);
11963 if (base_type)
11964 continue;
11965 }
11966
11967 base_type =
11968 is_type(build_ir_node_from_die(rdr, &type_die,
11969 called_from_public_decl,
11970 where, tu_ctxt));
11971
11972 // Sometimes base_type can be a typedef. Let's make
11973 // sure that typedef is compatible with a class type.
11975 if (!b)
11976 continue;
11977
11978 access_specifier access =
11979 is_struct
11980 ? public_access
11981 : private_access;
11982
11983 die_access_specifier(&child, access);
11984
11985 bool is_virt= die_is_virtual(&child);
11986 int64_t offset = 0;
11987 bool is_offset_present =
11988 die_member_offset(rdr, &child, offset);
11989
11990 class_decl::base_spec_sptr base(new class_decl::base_spec
11991 (b, access,
11992 is_offset_present ? offset : -1,
11993 is_virt));
11994
11995 if (result->find_base_class(b->get_qualified_name()))
11996 continue;
11997 result->add_base_specifier(base);
11998 }
11999 // Handle data members.
12000 else if (tag == DW_TAG_member
12001 || tag == DW_TAG_variable)
12002 {
12003 Dwarf_Die type_die;
12004 if (!die_die_attribute(&child, DW_AT_type, type_die))
12005 continue;
12006
12007 string n, m;
12008 location loc;
12009 die_loc_and_name(&child, tu_ctxt, loc, n, m);
12010 /// For now, we skip the hidden vtable pointer.
12011 /// Currently, we're looking for a member starting with
12012 /// "_vptr[^0-9a-zA-Z_]", which is what Clang and GCC
12013 /// use as a name for the hidden vtable pointer.
12014 if (n.substr(0, 5) == "_vptr"
12015 && n.size() > 5
12016 && !std::isalnum(n.at(5))
12017 && n.at(5) != '_')
12018 continue;
12019
12020 int64_t offset_in_bits = 0;
12021 bool is_laid_out = die_member_offset(rdr, &child,
12022 offset_in_bits);
12023 // For now, is_static == !is_laid_out. When we have
12024 // templates, we'll try to be more specific. For now,
12025 // this approximation should do OK.
12026 bool is_static = !is_laid_out;
12027
12028 if (is_static)
12029 // We are looking at the *declaration* of a static
12030 // data member. The definition comes later (or
12031 // somewhere else, rather)in the DWARF. It's the
12032 // definition that we are interested in because it has
12033 // attributes of the concrete representation of the
12034 // static data member like, the ELF symbol (storage
12035 // address) of the variable, etc. It's at that point
12036 // that the IR of the data member is going to be
12037 // created (by build_ir_node_from_die, in the
12038 // DW_TAG_variable case) and added to this class/struct
12039 // being created. So for now, just ignore it.
12040 continue;
12041
12042 decl_base_sptr ty =
12043 is_decl(build_ir_node_from_die(rdr, &type_die,
12044 called_from_public_decl,
12045 where, tu_ctxt));
12046 type_base_sptr t = is_type(ty);
12047 if (!t)
12048 continue;
12049
12050 if (n.empty() && !die_is_anonymous_data_member(&child))
12051 {
12052 // We must be in a case where the data member has an
12053 // empty name because the DWARF emitter has a bug.
12054 // Let's generate an artificial name for that data
12055 // member.
12056 n = rdr.build_name_for_buggy_anonymous_data_member(&child,
12057 tu_ctxt);
12058 ABG_ASSERT(!n.empty());
12059 }
12060
12061 if (!is_static)
12062 // We have a non-static data member. So this class
12063 // cannot be a declaration-only class anymore, even if
12064 // some DWARF emitters might consider it otherwise.
12065 result->set_is_declaration_only(false);
12066 access_specifier access =
12067 is_struct
12068 ? public_access
12069 : private_access;
12070
12071 die_access_specifier(&child, access);
12072
12073 var_decl_sptr dm(new var_decl(n, t, loc, m));
12074
12075 {
12076 lock_guard<recursive_mutex> lock(result->get_mutex());
12077 if (dm->get_name().empty()
12078 || !result->find_data_member(dm))
12079 {
12080 add_data_member(result, dm, access, is_laid_out,
12081 is_static, offset_in_bits);
12082 ABG_ASSERT(has_scope(dm));
12083 }
12084 }
12085 }
12086 // Handle member functions;
12087 else if (tag == DW_TAG_subprogram)
12088 decl_base_sptr r =
12089 add_or_update_member_function(rdr, &child, result,
12090 called_from_public_decl,
12091 where, tu_ctxt);
12092 // Handle member types;
12093 else if (die_is_type(&child))
12094 {
12095 // Do not add any member type, by default. Needed member
12096 // types will be added as part of building the required type
12097 // graph for an exported interface.
12098 ;
12099 }
12100 } while (dwarf_siblingof(&child, &child) == 0);
12101 }
12102
12103 tu_ctxt->scope_stack().pop();
12104
12105 tu_ctxt->unmark_class_or_union_die_as_wip(die);
12106
12107 return result;
12108}
12109
12110/// Build an @ref union_decl from a DW_TAG_union_type DIE.
12111///
12112/// @param rdr the DWARF reader to use.
12113///
12114/// @param die the DIE to read from.
12115///
12116/// @param union_type if this parameter is non-nil, then this function
12117/// updates the @ref union_decl that it points to, rather than
12118/// creating a new @ref union_decl.
12119///
12120/// @param called_from_public_decl is true if this function has been
12121/// initially called within the context of a public decl.
12122///
12123/// @param where_offset the offset of the DIE where we are "logically"
12124/// positionned at, in the DIE tree. This is useful when @p die is
12125/// e.g, DW_TAG_partial_unit that can be included in several places in
12126/// the DIE tree.
12127///
12128/// @param is_declaration_only is true if the DIE denoted by @p die is
12129/// a declaration-only DIE.
12130///
12131/// @return the resulting @ref union_decl type.
12132static union_decl_sptr
12133add_or_update_union_type(reader& rdr,
12134 Dwarf_Die* die,
12135 union_decl_sptr union_type,
12136 bool called_from_public_decl,
12137 void* where_addr,
12138 bool is_declaration_only,
12139 reader::tu_context_type_sptr& tu_ctxt)
12140{
12141 union_decl_sptr result;
12142 if (!die)
12143 return result;
12144
12145 unsigned tag = dwarf_tag(die);
12146
12147 if (tag != DW_TAG_union_type)
12148 return result;
12149
12150 if ((result = is_union_type(tu_ctxt->lookup_wip_type_from_die(die))))
12151 return union_type;
12152
12153 string name, linkage_name;
12154 location loc;
12155 die_loc_and_name(die, tu_ctxt, loc, name, linkage_name);
12156 cleanup_decl_name(name);
12157
12158 bool is_anonymous = false;
12159 if (name.empty())
12160 {
12161 // So we are looking at an anonymous union. Let's give it a
12162 // name.
12163 name = get_internal_anonymous_die_prefix_name(die);
12164 ABG_ASSERT(!name.empty());
12165 // But we remember that the type is anonymous.
12166 is_anonymous = true;
12167 }
12168
12169 // If the type has location, then associate it to its
12170 // representation. This way, all occurences of types with the same
12171 // representation (name) and location can be later detected as being
12172 // for the same type.
12173
12174 if (!is_anonymous)
12175 {
12176 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
12177 {
12178 if (loc)
12179 result = lookup_union_type_per_location(loc.expand(), *corp);
12180 else
12181 result = lookup_type<union_decl>(name, *corp);
12182
12183 if (result)
12184 {
12185 result = is_union_type(rdr.maybe_associate_die_to_type(die, result));
12186 return result;
12187 }
12188 }
12189 }
12190
12191 uint64_t size = 0;
12192 die_size_in_bits(die, size);
12193 bool is_artificial = die_is_artificial(die);
12194
12195 if (union_type)
12196 {
12197 result = union_type;
12198 result->set_location(loc);
12199 }
12200 else
12201 {
12202 result.reset(new union_decl(rdr.env(), name, size, loc,
12203 decl_base::VISIBILITY_DEFAULT,
12204 is_anonymous));
12205 if (is_declaration_only)
12206 result->set_is_declaration_only(true);
12207 }
12208
12209 ABG_ASSERT(result);
12210
12211 if (size)
12212 {
12213 result->set_size_in_bits(size);
12214 result->set_is_declaration_only(false);
12215 }
12216
12217 result->set_is_artificial(is_artificial);
12218
12219 Dwarf_Die child;
12220 bool has_child = (dwarf_child(die, &child) == 0);
12221 if (!has_child)
12222 return result;
12223
12224 tu_ctxt->mark_class_or_union_die_as_wip(die, result);
12225
12226 if (has_child)
12227 {
12228 do
12229 {
12230 tag = dwarf_tag(&child);
12231 // Handle data members.
12232 if (tag == DW_TAG_member || tag == DW_TAG_variable)
12233 {
12234 Dwarf_Die type_die;
12235 if (!die_die_attribute(&child, DW_AT_type, type_die))
12236 continue;
12237
12238 string n, m;
12239 location loc;
12240 die_loc_and_name(&child, tu_ctxt, loc, n, m);
12241
12242 ssize_t offset_in_bits = 0;
12243 decl_base_sptr ty =
12244 is_decl(build_ir_node_from_die(rdr, &type_die,
12245 called_from_public_decl,
12246 where_addr, tu_ctxt));
12247 type_base_sptr t = is_type(ty);
12248 if (!t)
12249 continue;
12250
12251 // We have a non-static data member. So this union
12252 // cannot be a declaration-only union anymore, even if
12253 // some DWARF emitters might consider it otherwise.
12254 result->set_is_declaration_only(false);
12255 access_specifier access = public_access;
12256
12257 die_access_specifier(&child, access);
12258
12259 var_decl_sptr dm(new var_decl(n, t, loc, m));
12260 {
12261 lock_guard<recursive_mutex> lock(result->get_mutex());
12262 if (dm->get_name().empty()
12263 || !result->find_data_member(dm))
12264 {
12265 add_data_member(result, dm, access, /*is_laid_out=*/true,
12266 /*is_static=*/false,
12267 offset_in_bits);
12268 ABG_ASSERT(has_scope(dm));
12269 }
12270 }
12271 }
12272 // Handle member functions;
12273 else if (tag == DW_TAG_subprogram)
12274 decl_base_sptr r =
12275 add_or_update_member_function(rdr, &child, result,
12276 called_from_public_decl,
12277 where_addr, tu_ctxt);
12278
12279 // Do not add member types here. That will be automagically
12280 // by build_ir_node_from_die when the member type is going
12281 // to be needed by a decl somehow.
12282 } while (dwarf_siblingof(&child, &child) == 0);
12283 }
12284
12285 tu_ctxt->unmark_class_or_union_die_as_wip(die);
12286
12287 return result;
12288}
12289
12290/// build a qualified type from a DW_TAG_const_type,
12291/// DW_TAG_volatile_type or DW_TAG_restrict_type DIE.
12292///
12293/// @param rdr the DWARF reader to consider.
12294///
12295/// @param die the input DIE to read from.
12296///
12297/// @param called_from_public_decl true if this function was called
12298/// from a context where either a public function or a public variable
12299/// is being built.
12300///
12301/// @param where_offset the offset of the DIE where we are "logically"
12302/// positionned at, in the DIE tree. This is useful when @p die is
12303/// e.g, DW_TAG_partial_unit that can be included in several places in
12304/// the DIE tree.
12305///
12306/// @return the resulting qualified_type_def.
12307static type_base_sptr
12308build_qualified_type(reader& rdr,
12309 Dwarf_Die* die,
12310 bool called_from_public_decl,
12311 void* where_addr,
12312 reader::tu_context_type_sptr& tu_ctxt)
12313{
12314 type_base_sptr result;
12315 if (!die)
12316 return result;
12317
12318 unsigned tag = dwarf_tag(die);
12319
12320 if (tag != DW_TAG_const_type
12321 && tag != DW_TAG_volatile_type
12322 && tag != DW_TAG_restrict_type)
12323 return result;
12324
12325 Dwarf_Die underlying_type_die;
12326 decl_base_sptr utype_decl;
12327 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
12328 // So, if no DW_AT_type is present, then this means (if we are
12329 // looking at a debug info emitted by GCC) that we are looking
12330 // at a qualified void type.
12331 utype_decl = build_ir_node_for_void_type(rdr, tu_ctxt);
12332
12333 if (!utype_decl)
12334 utype_decl = is_decl(build_ir_node_from_die(rdr, &underlying_type_die,
12335 called_from_public_decl,
12336 where_addr, tu_ctxt));
12337 if (!utype_decl)
12338 return result;
12339
12340 type_base_sptr utype = is_type(utype_decl);
12341 ABG_ASSERT(utype);
12342
12343 qualified_type_def::CV qual = qualified_type_def::CV_NONE;
12344 if (tag == DW_TAG_const_type)
12345 qual |= qualified_type_def::CV_CONST;
12346 else if (tag == DW_TAG_volatile_type)
12347 qual |= qualified_type_def::CV_VOLATILE;
12348 else if (tag == DW_TAG_restrict_type)
12349 qual |= qualified_type_def::CV_RESTRICT;
12350 else
12352
12353 if (!result)
12354 result.reset(new qualified_type_def(utype, qual, location()));
12355
12356 return result;
12357}
12358
12359/// Walk a tree of typedef of qualified arrays and schedule all type
12360/// nodes for canonicalization.
12361///
12362/// This is to be used after an array tree has been cloned. In that
12363/// case, the newly cloned type nodes have to be scheduled for
12364/// canonicalization.
12365///
12366/// This is a subroutine of maybe_strip_qualification.
12367///
12368/// @param t the type node to be scheduled for canonicalization.
12369///
12370/// @param rdr the DWARF reader to use.
12371static void
12372schedule_array_tree_for_late_canonicalization(const type_base_sptr& t,
12373 reader &rdr)
12374{
12375 if (typedef_decl_sptr type = is_typedef(t))
12376 {
12377 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
12378 rdr);
12379 rdr.schedule_type_for_late_canonicalization(t);
12380 }
12381 else if (qualified_type_def_sptr type = is_qualified_type(t))
12382 {
12383 schedule_array_tree_for_late_canonicalization(type->get_underlying_type(),
12384 rdr);
12385 rdr.schedule_type_for_late_canonicalization(t);
12386 }
12387 else if (array_type_def_sptr type = is_array_type(t))
12388 {
12389 for (vector<array_type_def::subrange_sptr>::const_iterator i =
12390 type->get_subranges().begin();
12391 i != type->get_subranges().end();
12392 ++i)
12393 {
12394 if (!(*i)->get_scope())
12396 type->get_translation_unit()->get_global_scope());
12397 rdr.schedule_type_for_late_canonicalization(*i);
12398
12399 }
12400
12401 schedule_array_tree_for_late_canonicalization(type->get_element_type(),
12402 rdr);
12403 rdr.schedule_type_for_late_canonicalization(type);
12404 }
12405}
12406
12407/// Strip qualification from a qualified type, when it makes sense.
12408///
12409/// DWARF constructs "const reference". This is redundant because a
12410/// reference is always const. The issue is these redundant types then
12411/// leak into the IR and make for bad diagnostics.
12412///
12413/// This function thus strips the const qualifier from the type in
12414/// that case. It might contain code to strip other cases like this
12415/// in the future.
12416///
12417/// @param t the type to strip const qualification from.
12418///
12419/// @param rdr the @ref reader to use.
12420///
12421/// @return the stripped type or just return @p t.
12422static decl_base_sptr
12423maybe_strip_qualification(const qualified_type_def_sptr t,
12424 reader &rdr)
12425{
12426 if (!t)
12427 return t;
12428
12429 decl_base_sptr result = t;
12430 type_base_sptr u = t->get_underlying_type();
12431
12434 if (result.get() != t.get())
12435 return result;
12436
12438 {
12439 array_type_def_sptr array;
12440 scope_decl_sptr scope = 0;
12441 if ((array = is_array_type(u)))
12442 {
12443 scope = array->get_scope();
12444 ABG_ASSERT(scope);
12445 array = is_array_type(clone_array_tree(array));
12446 schedule_array_tree_for_late_canonicalization(array, rdr);
12447 add_decl_to_scope(array, scope);
12448 t->set_underlying_type(array);
12449 u = t->get_underlying_type();
12450 }
12451 else if (is_typedef_of_array(u))
12452 {
12453 scope = is_decl(u)->get_scope();
12454 ABG_ASSERT(scope);
12455 typedef_decl_sptr typdef =
12457 schedule_array_tree_for_late_canonicalization(typdef, rdr);
12458 ABG_ASSERT(typdef);
12459 add_decl_to_scope(typdef, scope);
12460 t->set_underlying_type(typdef);
12461 u = t->get_underlying_type();
12462 array = is_typedef_of_array(u);
12463 }
12464 else
12466
12467 ABG_ASSERT(array);
12468 // We should not be editing types that are already canonicalized.
12469 ABG_ASSERT(!array->get_canonical_type());
12470 type_base_sptr element_type = array->get_element_type();
12471
12472 if (qualified_type_def_sptr qualified = is_qualified_type(element_type))
12473 {
12474 // We should not be editing types that are already canonicalized.
12475 ABG_ASSERT(!qualified->get_canonical_type());
12476 qualified_type_def::CV quals = qualified->get_cv_quals();
12477 quals |= t->get_cv_quals();
12478 qualified->set_cv_quals(quals);
12480 result = is_decl(u);
12481 }
12482 else
12483 {
12484 qualified_type_def_sptr qual_type
12485 (new qualified_type_def(element_type,
12486 t->get_cv_quals(),
12487 t->get_location()));
12489 add_decl_to_scope(qual_type, is_decl(element_type)->get_scope());
12490 array->set_element_type(qual_type);
12491 rdr.schedule_type_for_late_canonicalization(is_type(qual_type));
12492 result = is_decl(u);
12493 }
12494 }
12495
12496 return result;
12497}
12498
12499/// Build a pointer type from a DW_TAG_pointer_type DIE.
12500///
12501/// @param rdr the DWARF reader to consider.
12502///
12503/// @param die the DIE to read information from.
12504///
12505/// @param called_from_public_decl true if this function was called
12506/// from a context where either a public function or a public variable
12507/// is being built.
12508///
12509/// @param where_offset the offset of the DIE where we are "logically"
12510/// positionned at, in the DIE tree. This is useful when @p die is
12511/// e.g, DW_TAG_partial_unit that can be included in several places in
12512/// the DIE tree.
12513///
12514/// @return the resulting pointer to pointer_type_def.
12516build_pointer_type_def(reader& rdr,
12517 Dwarf_Die* die,
12518 bool called_from_public_decl,
12519 void* where_addr,
12520 reader::tu_context_type_sptr& tu_ctxt)
12521{
12522 pointer_type_def_sptr result;
12523
12524 if (!die)
12525 return result;
12526
12527 unsigned tag = dwarf_tag(die);
12528 if (tag != DW_TAG_pointer_type)
12529 return result;
12530
12531 type_or_decl_base_sptr utype_decl;
12532 Dwarf_Die underlying_type_die;
12533 bool has_underlying_type_die = false;
12534 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
12535 // If the DW_AT_type attribute is missing, that means we are
12536 // looking at a pointer to "void".
12537 utype_decl = build_ir_node_for_void_type(rdr, tu_ctxt);
12538 else
12539 has_underlying_type_die = true;
12540
12541 if (!utype_decl && has_underlying_type_die)
12542 utype_decl = build_ir_node_from_die(rdr, &underlying_type_die,
12543 called_from_public_decl,
12544 where_addr, tu_ctxt);
12545 if (!utype_decl)
12546 return result;
12547
12548 // The call to build_ir_node_from_die() could have triggered the
12549 // creation of the type for this DIE. In that case, just return it.
12550 if ((result = is_pointer_type(rdr.lookup_type_artifact_from_die(die))))
12551 return result;
12552
12553 type_base_sptr utype = is_type(utype_decl);
12554 ABG_ASSERT(utype);
12555
12556 // if the DIE for the pointer type doesn't have a byte_size
12557 // attribute then we assume the size of the pointer is the address
12558 // size of the current translation unit.
12559 uint64_t size = tu_ctxt->get_tu()->get_address_size();
12560 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12561 // The size as expressed by DW_AT_byte_size is in byte, so let's
12562 // convert it to bits.
12563 size *= 8;
12564
12565 // And the size of the pointer must be the same as the address size
12566 // of the current translation unit.
12567 ABG_ASSERT((size_t) tu_ctxt->get_tu()->get_address_size() == size);
12568
12569 result.reset(new pointer_type_def(utype, size, /*alignment=*/0, location()));
12570 ABG_ASSERT(result->get_pointed_to_type());
12571
12572 if (is_void_pointer_type(result))
12573 result = is_pointer_type(build_ir_node_for_void_pointer_type(rdr, tu_ctxt));
12574
12575 return result;
12576}
12577
12578/// Build a reference type from either a DW_TAG_reference_type or
12579/// DW_TAG_rvalue_reference_type DIE.
12580///
12581/// @param rdr the DWARF reader to consider.
12582///
12583/// @param die the DIE to read from.
12584///
12585/// @param called_from_public_decl true if this function was called
12586/// from a context where either a public function or a public variable
12587/// is being built.
12588///
12589/// @param where_offset the offset of the DIE where we are "logically"
12590/// positionned at, in the DIE tree. This is useful when @p die is
12591/// e.g, DW_TAG_partial_unit that can be included in several places in
12592/// the DIE tree.
12593///
12594/// @return a pointer to the resulting reference_type_def.
12596build_reference_type(reader& rdr,
12597 Dwarf_Die* die,
12598 bool called_from_public_decl,
12599 void* where_addr,
12600 reader::tu_context_type_sptr& tu_ctxt)
12601{
12603
12604 if (!die)
12605 return result;
12606
12607 unsigned tag = dwarf_tag(die);
12608 if (tag != DW_TAG_reference_type
12609 && tag != DW_TAG_rvalue_reference_type)
12610 return result;
12611
12612 if ((result = is_reference_type(rdr.lookup_type_artifact_from_die(die))))
12613 return result;
12614
12615 Dwarf_Die underlying_type_die;
12616 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
12617 return result;
12618
12619 type_or_decl_base_sptr utype_decl =
12620 build_ir_node_from_die(rdr, &underlying_type_die,
12621 called_from_public_decl,
12622 where_addr, tu_ctxt);
12623 if (!utype_decl)
12624 return result;
12625
12626 // The call to build_ir_node_from_die() could have triggered the
12627 // creation of the type for this DIE. In that case, just return it.
12628 if ((result = is_reference_type(rdr.lookup_type_artifact_from_die(die))))
12629 return result;
12630
12631 type_base_sptr utype = is_type(utype_decl);
12632 ABG_ASSERT(utype);
12633
12634 // if the DIE for the reference type doesn't have a byte_size
12635 // attribute then we assume the size of the reference is the address
12636 // size of the current translation unit.
12637 uint64_t size = tu_ctxt->get_tu()->get_address_size();
12638 if (die_unsigned_constant_attribute(die, DW_AT_byte_size, size))
12639 size *= 8;
12640
12641 // And the size of the pointer must be the same as the address size
12642 // of the current translation unit.
12643 ABG_ASSERT((size_t) tu_ctxt->get_tu()->get_address_size() == size);
12644
12645 bool is_lvalue = tag == DW_TAG_reference_type;
12646
12647 result.reset(new reference_type_def(utype, is_lvalue, size,
12648 /*alignment=*/0,
12649 location()));
12650 return result;
12651}
12652
12653/// Build an instance of @ref ptr_to_mbr_type from a DIE of tag
12654/// DW_TAG_ptr_to_member_type.
12655///
12656/// @param the DWARF reader touse.
12657///
12658/// @param the DIE to consider. It must carry the tag
12659/// DW_TAG_ptr_to_member_type.
12660///
12661/// @param called_from_public_decl true if this function was called
12662/// from a context where either a public function or a public variable
12663/// is being built.
12664///
12665/// @param where_offset the offset of the DIE where we are "logically"
12666/// positionned at, in the DIE tree. This is useful when @p die is
12667/// e.g, DW_TAG_partial_unit that can be included in several places in
12668/// the DIE tree.
12669///
12670/// @return a pointer to the resulting @ref ptr_to_mbr_type.
12672build_ptr_to_mbr_type(reader& rdr,
12673 Dwarf_Die* die,
12674 bool called_from_public_decl,
12675 void* where_addr,
12676 reader::tu_context_type_sptr& tu_ctxt)
12677{
12678 ptr_to_mbr_type_sptr result;
12679
12680 if (!die)
12681 return result;
12682
12683 unsigned tag = dwarf_tag(die);
12684 if (tag != DW_TAG_ptr_to_member_type)
12685 return result;
12686
12687 Dwarf_Die data_member_type_die, containing_type_die;
12688
12689 if (!die_die_attribute(die, DW_AT_type, data_member_type_die)
12690 || !die_die_attribute(die, DW_AT_containing_type, containing_type_die))
12691 return result;
12692
12693 type_or_decl_base_sptr data_member_type =
12694 build_ir_node_from_die(rdr, &data_member_type_die,
12695 called_from_public_decl,
12696 where_addr, tu_ctxt);
12697 if (!data_member_type)
12698 return result;
12699
12700 type_or_decl_base_sptr containing_type =
12701 build_ir_node_from_die(rdr, &containing_type_die,
12702 called_from_public_decl,
12703 where_addr, tu_ctxt);
12704 if (!containing_type)
12705 return result;
12706
12708 (is_type(containing_type)))
12709 return result;
12710
12711 if ((result = is_ptr_to_mbr_type(rdr.lookup_type_artifact_from_die(die))))
12712 return result;
12713
12714 uint64_t size_in_bits = tu_ctxt->get_tu()->get_address_size();
12715
12716 result.reset(new ptr_to_mbr_type(data_member_type->get_environment(),
12717 is_type(data_member_type),
12718 is_type(containing_type),
12719 size_in_bits,
12720 /*alignment=*/0,
12721 location()));
12722
12723 return result;
12724}
12725
12726/// Get the type (and DIE) referred to by a DW_AT_abstract_origin attribute.
12727///
12728/// @param rdr the DWARF reader to use.
12729///
12730/// @param die the DIE of the type to consider.
12731///
12732/// @param output parameter this DIE is set by the function if @p die
12733/// has a DW_AT_abstract_origin attribute.
12734///
12735/// @parm output parameter. This is set by the function if @p die has
12736/// a DW_AT_abstract_origin and if the referred-to type DIE already
12737/// has a type IR.
12738static bool
12739maybe_get_origin_type(reader& rdr,
12740 const Dwarf_Die* die,
12741 Dwarf_Die& origin_die,
12742 type_base_sptr& origin_type)
12743{
12744 bool found = false;
12745
12746 if (die_origin_die(die, origin_die))
12747 {
12748 found = true;
12749 origin_type = is_type(rdr.lookup_type_artifact_from_die(&origin_die));
12750 }
12751 return found;
12752}
12753
12754/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
12755///
12756/// @param rdr the DWARF reader to consider.
12757///
12758/// @param die the DIE to read from.
12759///
12760/// @param is_method points to a class or union declaration iff we're
12761/// building the type for a method. This is the enclosing class or
12762/// union of the method.
12763///
12764/// @param where_addr the address of the DIE where we are "logically"
12765/// positioned at, in the DIE tree. This is useful when @p die is
12766/// e.g, DW_TAG_partial_unit that can be included in several places in
12767/// the DIE tree.
12768///
12769/// @param decls the declarations of the types declared in the scope
12770/// of the function and used by the ABI of the function decl that uses
12771/// this function type as a type.
12772///
12773/// @return a pointer to the resulting function_type_sptr iff the
12774/// function could build it.
12775static function_type_sptr
12776build_function_type(reader& rdr,
12777 Dwarf_Die* die,
12778 class_or_union_sptr is_method,
12779 void* where_addr,
12780 vector<decl_base_sptr>& decls,
12781 reader::tu_context_type_sptr& tu_ctxt)
12782{
12783 function_type_sptr result;
12784
12785 if (!die)
12786 return result;
12787
12788 ABG_ASSERT(dwarf_tag(die) == DW_TAG_subroutine_type
12789 || dwarf_tag(die) == DW_TAG_subprogram);
12790
12791 if ((result = is_function_type(tu_ctxt->lookup_wip_type_from_die(die))))
12792 return result;
12793
12794 if ((result = is_function_type(rdr.lookup_type_artifact_from_die(die))))
12795 return result;
12796
12797 decl_base_sptr type_decl;
12798
12799 translation_unit_sptr tu = tu_ctxt->get_tu();
12800 ABG_ASSERT(tu);
12801
12802 bool odr_is_relevant = rdr.odr_is_relevant(die, tu_ctxt);
12803 if (odr_is_relevant)
12804 {
12805 // So we can rely on the One Definition Rule to say that if
12806 // several different function types have the same name (or
12807 // rather, representation) across the entire binary, then they
12808 // ought to designate the same function type. So let's ensure
12809 // that if we've already seen a function type with the same
12810 // representation as the function type 'die', then it's the same
12811 // type as the one denoted by 'die'.
12812 if (function_type_sptr fn_type =
12813 is_function_type(rdr.lookup_type_artifact_from_die(die)))
12814 {
12815 rdr.maybe_associate_die_to_type(die, fn_type);
12816 offset_t native_offset = dwarf_dieoffset(die);
12817 fn_type->set_native_offset(native_offset);
12818 return fn_type;
12819 }
12820 }
12821
12822 // Let's look at the DIE to detect if it's the DIE for a method
12823 // (type). If it is, we can deduce the name of its enclosing class
12824 // and if it's a static or const.
12825 bool is_const = false;
12826 bool is_static = false;
12827 Dwarf_Die object_pointer_die;
12828 Dwarf_Die class_type_die;
12829 bool has_this_parm_die =
12830 die_function_type_is_method_type(rdr, die, where_addr,
12831 tu_ctxt, object_pointer_die,
12832 class_type_die,
12833 is_static);
12834 if (has_this_parm_die)
12835 {
12836 // The function (type) has a "this" parameter DIE. It means it's
12837 // a member function DIE.
12838 if (!is_static)
12839 if (die_object_pointer_is_for_const_method(&object_pointer_die))
12840 is_const = true;
12841
12842 if (!is_method)
12843 {
12844 // We were initially called as if the function represented
12845 // by DIE was *NOT* a member function. But now we know it's
12846 // a member function. Let's take that into account.
12847 class_or_union_sptr klass_type =
12848 is_class_or_union_type(build_ir_node_from_die(rdr, &class_type_die,
12849 /*called_from_pub_decl=*/true,
12850 where_addr, tu_ctxt));
12851 if (!klass_type)
12852 {
12853 // We could not create the class type. For instance,
12854 // this can be due to the fact that the class is
12855 // suppressed. In those cases, we just bail out.
12856 return nullptr;
12857 }
12858 is_method = klass_type;
12859 }
12860 }
12861
12862 if ((result = is_function_type(rdr.lookup_type_artifact_from_die(die))))
12863 return result;
12864
12865 // Let's create the type early and record it as being for the DIE
12866 // 'die'. This way, when building the sub-type triggers the
12867 // creation of a type matching the same 'die', then we'll reuse this
12868 // one.
12869
12870 result.reset(is_method
12871 ? new method_type(is_method, is_const,
12872 tu->get_address_size(),
12873 /*alignment=*/0)
12874 : new function_type(rdr.env(), tu->get_address_size(),
12875 /*alignment=*/0));
12876 offset_t native_offset = dwarf_dieoffset(die);
12877 result->set_native_offset(native_offset);
12878 tu_ctxt->mark_function_type_die_as_wip(die, result);
12879
12880 bool is_destructor = false;
12881 if (is_method)
12882 is_destructor = die_is_destructor(die);
12883
12884 type_base_sptr return_type;
12885 Dwarf_Die ret_type_die;
12886 if (die_die_attribute(die, DW_AT_type, ret_type_die))
12887 return_type =
12888 is_type(build_ir_node_from_die(rdr, &ret_type_die,
12889 /*called_from_public_decl=*/true,
12890 where_addr, tu_ctxt));
12891
12892 if (!return_type)
12893 return_type = is_type(build_ir_node_for_void_type(rdr, tu_ctxt));
12894
12895 result->set_return_type(return_type);
12896
12897 Dwarf_Die child;
12898 function_decl::parameters function_parms;
12899 function_type_sptr orig_fn_type;
12900
12901 {
12902 // Concrete instance of function type vs abstract origin instance
12903 // ===============================================================
12904 //
12905 // This function type might be the concrete instance of an
12906 // abstract one, referred to via the DW_AT_abstract_origin
12907 // attribute. If that is the case, the function parameters that
12908 // are specified here are just the ones that have additional
12909 // attributes specific to the fact that this is a concrete
12910 // instance. The parameters that have no specific attributes can
12911 // be missing from this concrete instance of function type and
12912 // have to be retrieved from the abstract origin! So let's get
12913 // the origin abstract DIE of the function type, if it exists.
12914 Dwarf_Die original_die;
12915 type_base_sptr t;
12916 if (maybe_get_origin_type(rdr, die, original_die, t))
12917 orig_fn_type = is_function_type(t);
12918 }
12919
12920 if (dwarf_child(die, &child) == 0)
12921 do
12922 {
12923 int child_tag = dwarf_tag(&child);
12924 if (child_tag == DW_TAG_formal_parameter)
12925 {
12926 // This is a "normal" function parameter.
12927 string name, linkage_name;
12928 location loc;
12929 die_loc_and_name(&child, tu_ctxt, loc, name, linkage_name);
12931 // Sometimes, bogus compiler emit names that are
12932 // non-ascii garbage. Let's just ditch that for now.
12933 name.clear();
12934
12935 bool parm_is_artificial = die_is_artificial(&child);
12936 type_base_sptr parm_type;
12937 Dwarf_Die parm_type_die;
12938
12939 if (is_destructor
12940 && parm_is_artificial
12941 && function_parms.size() == 1)
12942 // we are looking at the type for a destructor and we
12943 // have already gotten the "this" pointer. We are
12944 // looking at a second parameter here. This is likely a
12945 // GCC-ism where we are getting at the "in-charge
12946 // parameter" of the internal implementation of a
12947 // destructor. Let's skip this in order to stay
12948 // compatible with Clang's and other compilers
12949 // implementation.
12950 continue;
12951
12952 if (die_die_attribute(&child, DW_AT_type, parm_type_die))
12953 parm_type =
12954 is_type(build_ir_node_from_die(rdr, &parm_type_die,
12955 /*called_from_public_decl=*/true,
12956 where_addr, tu_ctxt));
12957
12958 if (!parm_type)
12959 continue;
12960
12961 if (is_method
12962 && is_const_qualified_type(parm_type)
12963 && function_parms.empty())
12964 // We are looking at the first (implicit) parameter of a
12965 // method. This is basically the "this pointer". For
12966 // concrete instances of abstract methods, GCC sometimes
12967 // represents that pointer as a const pointer, whereas
12968 // in the abstract interface representing that method
12969 // the this-pointer is represented as a non-qualified
12970 // pointer. Let's trim the const qualifier away. That
12971 // will minize the chance to have spurious
12972 // const-qualifier changes on implicit parameters when
12973 // comparing methods that otherwise have no meaningful
12974 // ABI changes.
12975 parm_type =
12977
12979 (new function_decl::parameter(parm_type, name, loc,
12980 /*variadic_marker=*/false,
12981 parm_is_artificial));
12982 function_parms.push_back(p);
12983 }
12984 else if (child_tag == DW_TAG_unspecified_parameters)
12985 {
12986 // This is a variadic function parameter.
12987 bool is_artificial = die_is_artificial(&child);
12988
12989 type_base_sptr parm_type =
12990 is_type(build_ir_node_for_variadic_parameter_type(rdr, tu_ctxt));
12992 (new function_decl::parameter(parm_type,
12993 /*name=*/"",
12994 location(),
12995 /*variadic_marker=*/true,
12996 is_artificial));
12997 function_parms.push_back(p);
12998 // After a DW_TAG_unspecified_parameters tag, we shouldn't
12999 // keep reading for parameters. The
13000 // unspecified_parameters TAG should be the last parameter
13001 // that we record. For instance, if there are multiple
13002 // DW_TAG_unspecified_parameters DIEs then we should care
13003 // only for the first one.
13004 break;
13005 }
13006 else
13007 {
13008 // This might be the declaration of a type that is used by
13009 // one of the function parameters. This is rare, but it
13010 // happens.
13011 type_or_decl_base_sptr ir_node =
13012 build_ir_node_from_die(rdr, &child,
13013 /*called_from_public_decl=*/true,
13014 where_addr, tu_ctxt);
13015
13016 if (decl_base_sptr d = is_decl(ir_node))
13017 decls.push_back(d);
13018 }
13019 }
13020 while (dwarf_siblingof(&child, &child) == 0);
13021
13022 // Following up on the earlier comment named:
13023 //
13024 // Concrete instance of function type vs abstract origin instance
13025 // ===============================================================
13026 //
13027 // [Please make sure you've read that previous original comment
13028 // before reading this one].
13029 //
13030 // If there is an origin abstract function type, then that means we
13031 // are looking at a concrete instance of a function type that was
13032 // defined in a prior abstract origin function type. In that case,
13033 // we might be missing some function parameters that are only
13034 // defined in the origin abstract instance. Let's add these
13035 // parameters if that is the case.
13036 if (orig_fn_type)
13037 if (!orig_fn_type->has_empty_parameters())
13038 {
13039 unsigned o_nb_parms = orig_fn_type->get_nb_parameters();
13040 if (o_nb_parms > function_parms.size())
13041 // The abstract instance of function type has more
13042 // parameters than the concrete instance we are looking at.
13043 // That means we need to get the additionnal parameters from
13044 // the abstract instance. Let's just do that then.
13045 for (unsigned i = function_parms.size(); i < o_nb_parms; ++i)
13046 if (function_type::parameter_sptr p = orig_fn_type->get_parm_at(i))
13047 function_parms.push_back(p);
13048 }
13049
13050 result->set_parameters(function_parms);
13051
13052 result->set_is_artificial(true);
13053
13054 tu_ctxt->unmark_function_type_die_as_wip(die);
13055
13056 result = is_function_type(rdr.maybe_associate_die_to_type(die, result));
13057
13058 return result;
13059}
13060
13061/// Build a subroutine type from a DW_TAG_subroutine_type DIE.
13062///
13063/// @param rdr the DWARF reader to consider.
13064///
13065/// @param die the DIE to read from.
13066///
13067/// @param is_method points to a class or union declaration iff we're
13068/// building the type for a method. This is the enclosing class or
13069/// union of the method.
13070///
13071/// @param where_addr the address of the DIE where we are "logically"
13072/// positioned at, in the DIE tree. This is useful when @p die is
13073/// e.g, DW_TAG_partial_unit that can be included in several places in
13074/// the DIE tree.
13075///
13076/// @return a pointer to the resulting function_type_sptr iff the
13077/// function could build it.
13078static function_type_sptr
13079build_function_type(reader& rdr,
13080 Dwarf_Die* die,
13081 class_or_union_sptr is_method,
13082 void* where_addr,
13083 reader::tu_context_type_sptr& tu_ctxt)
13084{
13085 vector<decl_base_sptr> decls;
13086 return build_function_type(rdr, die, is_method, where_addr, decls, tu_ctxt);
13087}
13088
13089/// Build a subrange type from a DW_TAG_subrange_type.
13090///
13091/// @param rdr the DWARF reader to consider.
13092///
13093/// @param die the DIE to read from.
13094///
13095/// @param where_offset the offset of the DIE where we are "logically"
13096/// positionned at in the DIE tree. This is useful when @p die is
13097/// e,g, DW_TAG_partial_unit that can be included in several places in
13098/// the DIE tree.
13099///
13100/// @param associate_die_to_type if this is true then the resulting
13101/// type is associated to the @p die, so that next time when the
13102/// system looks up the type associated to it, the current resulting
13103/// type is returned. If false, then no association is done and the
13104/// resulting type can be destroyed right after. This can be useful
13105/// when the sole purpose of building the @ref
13106/// array_type_def::subrange_type is to use some of its method like,
13107/// e.g, its name pretty printing methods.
13108///
13109/// @return the newly built instance of @ref
13110/// array_type_def::subrange_type, or nil if no type could be built.
13112build_subrange_type(reader& rdr,
13113 const Dwarf_Die* die,
13114 void* where,
13115 reader::tu_context_type_sptr& tu_ctxt,
13116 bool associate_type_to_die)
13117{
13119
13120 if (!die)
13121 return result;
13122
13123 unsigned tag = dwarf_tag(const_cast<Dwarf_Die*>(die));
13124 if (tag != DW_TAG_subrange_type)
13125 return result;
13126
13127 string name = die_name(die);
13128
13129 // load the underlying type.
13130 Dwarf_Die underlying_type_die;
13131 type_base_sptr underlying_type;
13132 /* Unless there is an underlying type which says differently. */
13133 bool is_signed = false;
13134 if (die_die_attribute(die, DW_AT_type, underlying_type_die))
13135 underlying_type =
13136 is_type(build_ir_node_from_die(rdr,
13137 &underlying_type_die,
13138 /*called_from_public_decl=*/true,
13139 where, tu_ctxt));
13140
13141 if (underlying_type)
13142 {
13143 uint64_t ate;
13144 if (die_unsigned_constant_attribute (&underlying_type_die,
13145 DW_AT_encoding,
13146 ate))
13147 is_signed = (ate == DW_ATE_signed || ate == DW_ATE_signed_char);
13148 }
13149
13150 // The DW_TAG_subrange_type DIE may have some size related
13151 // attributes (DW_AT_byte_size or DW_AT_bit_size). If not, then the
13152 // size is deduced from the size of its underlying type.
13153 bool has_size_info = false;
13154 uint64_t size = 0;
13155 if ((has_size_info = die_unsigned_constant_attribute(die,
13156 DW_AT_byte_size, size)))
13157 size *= 8;
13158 else
13159 has_size_info = die_unsigned_constant_attribute(die,
13160 DW_AT_bit_size, size);
13161
13162 translation_unit::language language = tu_ctxt->get_tu()->get_language();
13163 array_type_def::subrange_type::bound_value lower_bound =
13164 get_default_array_lower_bound(language);
13165 array_type_def::subrange_type::bound_value upper_bound;
13166 uint64_t count = 0;
13167 bool is_non_finite = false;
13168 bool non_zero_count_present = false;
13169
13170 // The DWARF 4 specifications says, in [5.11 Subrange
13171 // Type Entries]:
13172 //
13173 // The subrange entry may have the attributes
13174 // DW_AT_lower_bound and DW_AT_upper_bound to
13175 // specify, respectively, the lower and upper bound
13176 // values of the subrange.
13177 //
13178 // So let's look for DW_AT_lower_bound first.
13179 die_constant_attribute(die, DW_AT_lower_bound, is_signed, lower_bound);
13180
13181 bool found_upper_bound = die_constant_attribute(die, DW_AT_upper_bound,
13182 is_signed, upper_bound);
13183 if (!found_upper_bound)
13184 found_upper_bound = subrange_die_indirect_bound_value(die,
13185 DW_AT_upper_bound,
13186 upper_bound,
13187 is_signed);
13188 // Then, DW_AT_upper_bound.
13189 if (!found_upper_bound)
13190 {
13191 // The DWARF 4 spec says, in [5.11 Subrange Type
13192 // Entries]:
13193 //
13194 // The DW_AT_upper_bound attribute may be replaced
13195 // by a DW_AT_count attribute, whose value
13196 // describes the number of elements in the
13197 // subrange rather than the value of the last
13198 // element."
13199 //
13200 // So, as DW_AT_upper_bound is not present in this
13201 // case, let's see if there is a DW_AT_count.
13202 if (die_unsigned_constant_attribute(die, DW_AT_count, count))
13203 {
13204 if (count)
13205 // DW_AT_count can be present and be set to zero. This is
13206 // for instance the case to model this gcc extension to
13207 // represent flexible arrays:
13208 // https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html.
13209 // For instance: int flex_array[0];
13210 non_zero_count_present = true;
13211
13212 // When the count is present and non-zero, we can deduce the
13213 // upper_bound from the lower_bound and the number of
13214 // elements of the array:
13215 int64_t u = lower_bound.get_signed_value() + count;
13216 if (u)
13217 upper_bound = u - 1;
13218 }
13219
13220 if (!non_zero_count_present)
13221 // No upper_bound nor count was present on the DIE, this means
13222 // the array is considered to have an infinite (or rather not
13223 // known) size.
13224 is_non_finite = true;
13225 }
13226
13227 if (UINT64_MAX == upper_bound.get_unsigned_value())
13228 // If the upper_bound size is the max of the integer value
13229 // then it most certainly means unknown size.
13230 is_non_finite = true;
13231
13232 result.reset
13233 (new array_type_def::subrange_type(rdr.env(),
13234 name,
13235 lower_bound,
13236 upper_bound,
13237 underlying_type,
13238 location()));
13239 result->is_non_finite(is_non_finite);
13240
13241 if (has_size_info)
13242 result->set_size_in_bits(size);
13243 else
13244 {
13245 // The DW_TAG_subrange_type doesn't appear to have any size
13246 // attribute. In that case, the size is deduced from the size
13247 // of the underlying type. If there is no underlying type
13248 // specified, then the size of the subrange type is the size
13249 if (!underlying_type)
13250 result->set_size_in_bits(tu_ctxt->get_tu()->get_address_size());
13251 }
13252
13253 // Let's ensure the resulting subrange looks metabolically healthy.
13254 ABG_ASSERT(result->is_non_finite()
13255 || (result->get_length() ==
13256 (uint64_t) (result->get_upper_bound()
13257 - result->get_lower_bound() + 1)));
13258
13259 if (associate_type_to_die)
13260 result = is_subrange_type(rdr.maybe_associate_die_to_type(die, result));
13261
13262 return result;
13263}
13264
13265/// Build the sub-ranges of an array type.
13266///
13267/// This is a sub-routine of build_array_type().
13268///
13269/// @param rdr the context to read from.
13270///
13271/// @param die the DIE of tag DW_TAG_array_type which contains
13272/// children DIEs that represent the sub-ranges.
13273///
13274/// @param subranges out parameter. This is set to the sub-ranges
13275/// that are built from @p die.
13276///
13277/// @param where_offset the offset of the DIE where we are "logically"
13278/// positioned at, in the DIE tree. This is useful when @p die is
13279/// e.g, DW_TAG_partial_unit that can be included in several places in
13280/// the DIE tree.
13281static void
13282build_subranges_from_array_type_die(const reader& rdr,
13283 const Dwarf_Die* die,
13285 void* where,
13286 reader::tu_context_type_sptr& tu_ctxt,
13287 bool associate_type_to_die)
13288{
13289 Dwarf_Die child;
13290
13291 if (dwarf_child(const_cast<Dwarf_Die*>(die), &child) == 0)
13292 {
13293 do
13294 {
13295 int child_tag = dwarf_tag(&child);
13296 if (child_tag == DW_TAG_subrange_type)
13297 {
13299 if (associate_type_to_die)
13300 {
13301 // We are being called to create the type, add it to
13302 // the current type graph and associate it to the
13303 // DIE it's been created from.
13305 build_ir_node_from_die(const_cast<reader&>(rdr), &child,
13306 /*called_from_public_decl=*/true,
13307 where, tu_ctxt);
13308 s = is_subrange_type(t);
13309 }
13310 else
13311 // We are being called to create the type but *NOT*
13312 // add it to the current tyupe tree, *NOR* associate
13313 // it to the DIE it's been created from.
13314 s = build_subrange_type(const_cast<reader&>(rdr),
13315 &child, where, tu_ctxt,
13316 /*associate_type_to_die=*/false);
13317 if (s)
13318 subranges.push_back(s);
13319 }
13320 }
13321 while (dwarf_siblingof(&child, &child) == 0);
13322 }
13323}
13324
13325/// Build an array type from a DW_TAG_array_type DIE.
13326///
13327/// @param rdr the DWARF reader to consider.
13328///
13329/// @param die the DIE to read from.
13330///
13331/// @param called_from_public_decl true if this function was called
13332/// from a context where either a public function or a public variable
13333/// is being built.
13334///
13335/// @param where_offset the offset of the DIE where we are "logically"
13336/// positioned at, in the DIE tree. This is useful when @p die is
13337/// e.g, DW_TAG_partial_unit that can be included in several places in
13338/// the DIE tree.
13339///
13340/// @return a pointer to the resulting array_type_def.
13342build_array_type(reader& rdr,
13343 Dwarf_Die* die,
13344 bool called_from_public_decl,
13345 void* where_addr,
13346 reader::tu_context_type_sptr& tu_ctxt)
13347{
13348 array_type_def_sptr result;
13349
13350 if (!die)
13351 return result;
13352
13353 unsigned tag = dwarf_tag(die);
13354 if (tag != DW_TAG_array_type)
13355 return result;
13356
13357 decl_base_sptr type_decl;
13358 Dwarf_Die type_die;
13359
13360 if (die_die_attribute(die, DW_AT_type, type_die))
13361 type_decl = is_decl(build_ir_node_from_die(rdr, &type_die,
13362 called_from_public_decl,
13363 where_addr, tu_ctxt));
13364 if (!type_decl)
13365 return result;
13366
13367 type_base_sptr type = is_type(type_decl);
13368 ABG_ASSERT(type);
13369
13371
13372 build_subranges_from_array_type_die(rdr, die, subranges, where_addr, tu_ctxt);
13373
13374 result.reset(new array_type_def(type, subranges, location()));
13375
13376 return result;
13377}
13378
13379/// Create a typedef_decl from a DW_TAG_typedef DIE.
13380///
13381/// @param rdr the DWARF reader to consider.
13382///
13383/// @param die the DIE to read from.
13384///
13385/// @param called_from_public_decl true if this function was called
13386/// from a context where either a public function or a public variable
13387/// is being built.
13388///
13389/// @param where_addr the address of the DIE where we are "logically"
13390/// positionned at, in the DIE tree. This is useful when @p die is in
13391/// a e.g, DW_TAG_partial_unit that can be included in several places
13392/// in the DIE tree.
13393///
13394/// @return the newly created typedef_decl.
13395static typedef_decl_sptr
13396build_typedef_type(reader& rdr,
13397 Dwarf_Die* die,
13398 bool called_from_public_decl,
13399 void* where_addr,
13400 reader::tu_context_type_sptr& tu_ctxt)
13401{
13402 typedef_decl_sptr result;
13403
13404 if (!die)
13405 return result;
13406
13407 unsigned tag = dwarf_tag(die);
13408 if (tag != DW_TAG_typedef)
13409 return result;
13410
13411 if ((result = is_typedef(rdr.lookup_type_artifact_from_die(die))))
13412 return result;
13413
13414 string name, linkage_name;
13415 location loc;
13416 die_loc_and_name(die, tu_ctxt, loc, name, linkage_name);
13417
13418 if (corpus_sptr corp = rdr.should_reuse_type_from_corpus_group())
13419 if (loc)
13420 result = lookup_typedef_type_per_location(loc.expand(), *corp);
13421
13422 if (!result)
13423 {
13424 type_base_sptr utype;
13425 Dwarf_Die underlying_type_die;
13426 if (!die_die_attribute(die, DW_AT_type, underlying_type_die))
13427 // A typedef DIE with no underlying type means a typedef to
13428 // void type.
13429 utype = rdr.env().get_void_type();
13430
13431 if (!utype)
13432 utype =
13433 is_type(build_ir_node_from_die(rdr,
13434 &underlying_type_die,
13435 called_from_public_decl,
13436 where_addr, tu_ctxt));
13437 if (!utype)
13438 return result;
13439
13440 if ((result = is_typedef(rdr.lookup_type_artifact_from_die(die))))
13441 return result;
13442
13443 ABG_ASSERT(utype);
13444 result.reset(new typedef_decl(name, utype, loc, linkage_name));
13445 }
13446
13447 return result;
13448}
13449
13450/// Build a @ref var_decl out of a DW_TAG_variable DIE if the variable
13451/// denoted by the DIE is not suppressed by a suppression
13452/// specification associated to the current DWARF reader.
13453///
13454/// Note that if a member variable declaration with the same name as
13455/// the name of the DIE we are looking at exists, this function returns
13456/// that existing variable declaration.
13457///
13458/// @param rdr the DWARF reader to use.
13459///
13460/// @param die the DIE representing the variable we are looking at.
13461///
13462/// @param where_offset the offset of the DIE where we are "logically"
13463/// positionned at, in the DIE tree. This is useful when @p die is
13464/// e.g, DW_TAG_partial_unit that can be included in several places in
13465/// the DIE tree.
13466///
13467/// @param is_declaration_only if true, it means the variable DIE has
13468/// the is_declaration_only only attribute.
13469///
13470/// @param result if this is set to an existing var_decl, this means
13471/// that the function will append the new properties it sees on @p die
13472/// to that exising var_decl. Otherwise, if this parameter is NULL, a
13473/// new var_decl is going to be allocated and returned.
13474///
13475/// @param is_required_decl_spec this is true iff the variable to
13476/// build is referred to as being the specification of another
13477/// variable. So it *has* to be emitted, i.e, it is not going to be
13478/// dropped on the floor.
13479///
13480/// @return a pointer to the newly created var_decl. If the var_decl
13481/// could not be built, this function returns NULL.
13482static var_decl_sptr
13483build_or_get_var_decl_if_not_suppressed(reader& rdr,
13484 scope_decl_sptr scope,
13485 Dwarf_Die* die,
13486 void* where_addr,
13487 reader::tu_context_type_sptr& tu_ctxt,
13488 bool is_declaration_only,
13489 var_decl_sptr result,
13490 bool is_required_decl_spec)
13491{
13492 var_decl_sptr var;
13493 if (variable_is_suppressed(rdr, scope, die,
13494 is_declaration_only,
13495 is_required_decl_spec))
13496 {
13497 lock_guard<recursive_mutex> lock(rdr.mutex_);
13498 ++rdr.stats_.number_of_suppressed_variables;
13499 return var;
13500 }
13501
13502 if (class_decl_sptr class_type = is_class_type(scope))
13503 {
13504 string var_name = die_name(die);
13505 if (!var_name.empty())
13506 if ((var = class_type->find_data_member(var_name)))
13507 return var;
13508 }
13509
13510 // The variable was not suppressed.
13511 {
13512 lock_guard<recursive_mutex> lock(rdr.mutex_);
13513 ++rdr.stats_.number_of_suppressed_variables;
13514 }
13515
13516 var = build_var_decl(rdr, die, where_addr, tu_ctxt, result);
13517 return var;
13518}
13519
13520/// Build a @ref var_decl out of a DW_TAG_variable DIE.
13521///
13522/// @param rdr the DWARF reader to use.
13523///
13524/// @param die the DIE representing the variable we are looking at.
13525///
13526/// @param where_offset the offset of the DIE where we are "logically"
13527/// positionned at, in the DIE tree. This is useful when @p die is
13528/// e.g, DW_TAG_partial_unit that can be included in several places in
13529/// the DIE tree.
13530///
13531/// @param result if this is set to an existing var_decl, this means
13532/// that the function will append the new properties it sees on @p die
13533/// to that exising var_decl. Otherwise, if this parameter is NULL, a
13534/// new var_decl is going to be allocated and returned.
13535///
13536/// @return a pointer to the newly created var_decl. If the var_decl
13537/// could not be built, this function returns NULL.
13538static var_decl_sptr
13539build_var_decl(reader& rdr,
13540 Dwarf_Die* die,
13541 void* where_addr,
13542 reader::tu_context_type_sptr& tu_ctxt,
13543 var_decl_sptr result)
13544{
13545 if (!die)
13546 return result;
13547
13548 int tag = dwarf_tag(die);
13549 ABG_ASSERT(tag == DW_TAG_variable || tag == DW_TAG_member);
13550
13551 if (!die_is_public_decl(die))
13552 return result;
13553
13554 type_base_sptr type;
13555 Dwarf_Die type_die;
13556 if (die_die_attribute(die, DW_AT_type, type_die))
13557 {
13558 type_or_decl_base_sptr artifact =
13559 build_ir_node_from_die(rdr, &type_die,
13560 /*called_from_public_decl=*/true,
13561 where_addr, tu_ctxt);
13562 if (artifact)
13563 {
13564 type = is_type(artifact);
13565 ABG_ASSERT(type);
13566 }
13567 }
13568
13569 if (!type && !result)
13570 return result;
13571
13572 string name, linkage_name;
13573 location loc;
13574 die_loc_and_name(die, tu_ctxt, loc, name, linkage_name);
13575
13576 if (!type && result && !result->get_type())
13577 type = rdr.env().get_void_type();
13578
13579 if (!result)
13580 result.reset(new var_decl(name, type, loc, linkage_name));
13581 else
13582 {
13583 // We were called to append properties that might have been
13584 // missing from the first version of the variable. And usually
13585 // that missing property is the mangled name or the type.
13586 if (!linkage_name.empty())
13587 result->set_linkage_name(linkage_name);
13588
13589 if (type)
13590 result->set_type(type);
13591 }
13592
13593 // Check if a variable symbol with this name is exported by the elf
13594 // binary. If it is, then set the symbol of the variable, if it's
13595 // not set already.
13596 if (!result->get_symbol())
13597 {
13598 elf_symbol_sptr var_sym;
13599 Dwarf_Addr var_addr;
13600
13601 if (rdr.get_variable_address(die, var_addr))
13602 {
13603 rdr.symtab()->
13604 update_main_symbol(var_addr,
13605 result->get_linkage_name().empty()
13606 ? result->get_name()
13607 : result->get_linkage_name());
13608 var_sym = rdr.variable_symbol_is_exported(var_addr);
13609 }
13610
13611 if (var_sym)
13612 {
13613 result->set_symbol(var_sym);
13614 // If the linkage name is not set or is wrong, set it to
13615 // the name of the underlying symbol.
13616 string linkage_name = result->get_linkage_name();
13617 if (linkage_name.empty()
13618 || !var_sym->get_alias_from_name(linkage_name))
13619 result->set_linkage_name(var_sym->get_name());
13620 result->set_is_in_public_symbol_table(true);
13621 }
13622
13623 if (!var_sym && rdr.is_decl_die_with_undefined_symbol(die))
13624 {
13625 // We are looking at a global variable which symbol is
13626 // undefined. Let's set its symbol.
13627 string n = result->get_linkage_name();
13628 if (n.empty())
13629 n = result->get_name();
13630 var_sym = rdr.symtab()->lookup_undefined_variable_symbol(n);
13631 if (var_sym)
13632 {
13633 result->set_symbol(var_sym);
13634 result->set_is_in_public_symbol_table(false);
13635 }
13636 }
13637 }
13638
13639 return result;
13640}
13641
13642/// Test if a given function denoted by its DIE and its scope is
13643/// suppressed by any of the suppression specifications associated to
13644/// a given context of ELF/DWARF reading.
13645///
13646/// Note that a non-member function which symbol is not exported is
13647/// also suppressed.
13648///
13649/// @param rdr the ELF/DWARF reading content of interest.
13650///
13651/// @param scope of the scope of the function.
13652///
13653/// @param function_die the DIE representing the function.
13654///
13655/// @param is_declaration_only is true if the DIE denoted by @p die is
13656/// a declaration-only DIE.
13657///
13658/// @return true iff @p function_die is suppressed by at least one
13659/// suppression specification attached to the @p rdr.
13660static bool
13661function_is_suppressed(const reader& rdr,
13662 const scope_decl_sptr scope,
13663 Dwarf_Die *function_die,
13664 bool is_declaration_only)
13665{
13666 if (function_die == 0
13667 || dwarf_tag(function_die) != DW_TAG_subprogram)
13668 return false;
13669
13670 string fname = die_string_attribute(function_die, DW_AT_name);
13671 string flinkage_name = die_linkage_name(function_die);
13672 if (flinkage_name.empty() && die_is_in_c(function_die))
13673 flinkage_name = fname;
13674 string qualified_name = build_qualified_name(scope, fname);
13675
13676 // A non-member non-static function which symbol is not exported is
13677 // suppressed.
13678 //
13679 // Note that if the non-member non-static function has an undefined
13680 // symbol, by default, it's not suppressed. Unless we are asked to
13681 // drop undefined symbols too.
13682 if (!is_class_type(scope)
13683 && (!is_declaration_only || rdr.drop_undefined_syms()))
13684 {
13685 Dwarf_Addr fn_addr;
13686 if (!rdr.get_function_address(function_die, fn_addr))
13687 return true;
13688
13689 elf_symbol_sptr symbol =
13690 rdr.function_symbol_is_exported(fn_addr);
13691 if (!symbol)
13692 return true;
13693 if (symbol->is_suppressed())
13694 return true;
13695
13696 // Since there is only one symbol in DWARF associated with an elf_symbol,
13697 // we can assume this is the main symbol then. Otherwise the main hinting
13698 // did not work as expected.
13699 ABG_ASSERT(symbol->is_main_symbol());
13700 if (symbol->has_aliases())
13701 for (elf_symbol_sptr a = symbol->get_next_alias();
13702 !a->is_main_symbol(); a = a->get_next_alias())
13703 if (a->is_suppressed())
13704 return true;
13705 }
13706
13707 return suppr::is_function_suppressed(rdr, qualified_name, flinkage_name,
13708 /*require_drop_property=*/true);
13709}
13710
13711/// Build a @ref function_decl out of a DW_TAG_subprogram DIE if the
13712/// function denoted by the DIE is not suppressed by a suppression
13713/// specification associated to the current DWARF reader.
13714///
13715/// Note that if a member function declaration with the same signature
13716/// (pretty representation) as one of the DIE we are looking at
13717/// exists, this function returns that existing function declaration.
13718/// Similarly, if there is already a constructed member function with
13719/// the same linkage name as the one on the DIE, this function returns
13720/// that member function.
13721///
13722/// Also note that the function_decl IR returned by this function must
13723/// be passed to finish_member_function_reading because several
13724/// properties from the DIE are actually read by that function, and
13725/// the corresponding properties on the function_decl IR are updated
13726/// accordingly. This is done to support "updating" a function_decl
13727/// IR with properties scathered across several DIEs.
13728///
13729/// @param rdr the DWARF reader to use.
13730///
13731/// @param scope the scope of the function we are looking at.
13732///
13733/// @param fn_die the DIE representing the function we are looking at.
13734///
13735/// @param where_offset the offset of the DIE where we are "logically"
13736/// positionned at, in the DIE tree. This is useful when @p die is
13737/// e.g, DW_TAG_partial_unit that can be included in several places in
13738/// the DIE tree.
13739///
13740/// @param is_declaration_only is true if the DIE denoted by @p fn_die
13741/// is a declaration-only DIE.
13742///
13743/// @param result if this is set to an existing function_decl, this
13744/// means that the function will append the new properties it sees on
13745/// @p fn_die to that exising function_decl. Otherwise, if this
13746/// parameter is NULL, a new function_decl is going to be allocated
13747/// and returned.
13748///
13749/// @return a pointer to the newly created var_decl. If the var_decl
13750/// could not be built, this function returns NULL.
13751static function_decl_sptr
13752build_or_get_fn_decl_if_not_suppressed(reader& rdr,
13753 scope_decl_sptr scope,
13754 Dwarf_Die *fn_die,
13755 void* where_addr,
13756 reader::tu_context_type_sptr& tu_ctxt,
13757 bool is_declaration_only,
13758 function_decl_sptr result)
13759{
13760 if (!die_is_function_decl(fn_die))
13761 return nullptr;
13762
13764 if (function_is_suppressed(rdr, scope, fn_die, is_declaration_only))
13765 {
13766 lock_guard<recursive_mutex> lock(rdr.mutex_);
13767 ++rdr.stats_.number_of_suppressed_functions;
13768 return nullptr;
13769 }
13770
13771 if (potential_member_fn_should_be_dropped(rdr, tu_ctxt, fn_die))
13772 return nullptr;
13773
13774 // The function was not suppressed.
13775 {
13776 lock_guard<recursive_mutex> lock(rdr.mutex_);
13777 ++rdr.stats_.number_of_allowed_functions;
13778 }
13779
13780 // If a member function with the same linkage name as the one
13781 // carried by the DIE already exists, then return it.
13782 class_decl_sptr klass = is_class_type(scope);
13783 if (klass)
13784 {
13785 string linkage_name = die_linkage_name(fn_die);
13786 fn = klass->find_member_function_sptr(linkage_name);
13787 if (fn)
13788 // We found a member function that has the same signature.
13789 // Let's mark it for update.
13790 result = fn;
13791 }
13792
13793 if (!fn || !fn->get_symbol())
13794 // We haven't yet been able to construct a function IR, or, we
13795 // have one 'partial' function IR that doesn't have any associated
13796 // symbol yet. Note that in the later case, a function IR without
13797 // any associated symbol will be dropped on the floor by
13798 // potential_member_fn_should_be_dropped. So let's build or a new
13799 // function IR or complete the existing partial IR.
13800 fn = build_function_decl(rdr, fn_die, where_addr, tu_ctxt, result);
13801
13802 ABG_ASSERT(!fn || !fn->get_scope() || fn->get_scope().get() == scope.get());
13803
13804 return fn;
13805}
13806
13807/// Test if a given variable denoted by its DIE and its scope is
13808/// suppressed by any of the suppression specifications associated to
13809/// a given context of ELF/DWARF reading.
13810///
13811/// @param rdr the ELF/DWARF reading content of interest.
13812///
13813/// @param scope of the scope of the variable.
13814///
13815/// @param variable_die the DIE representing the variable.
13816///
13817/// @param is_declaration_only true if the variable is supposed to be
13818/// decl-only.
13819///
13820/// @param is_required_decl_spec if true, means that the @p
13821/// variable_die being considered is for a variable decl that is a
13822/// specification for a concrete variable being built.
13823///
13824/// @return true iff @p variable_die is suppressed by at least one
13825/// suppression specification attached to the @p rdr.
13826static bool
13827variable_is_suppressed(const reader& rdr,
13828 const scope_decl_sptr scope,
13829 Dwarf_Die *variable_die,
13830 bool is_declaration_only,
13831 bool is_required_decl_spec)
13832{
13833 if (variable_die == 0
13834 || (dwarf_tag(variable_die) != DW_TAG_variable
13835 && dwarf_tag(variable_die) != DW_TAG_member))
13836 return false;
13837
13838 string name = die_string_attribute(variable_die, DW_AT_name);
13839 string linkage_name = die_linkage_name(variable_die);
13840 if (linkage_name.empty() && die_is_in_c(variable_die))
13841 linkage_name = name;
13842 string qualified_name = build_qualified_name(scope, name);
13843
13844 // If a non member variable that is a declaration (has no defined
13845 // and exported symbol) and is not the specification of another
13846 // concrete variable, then it's suppressed. This is a size
13847 // optimization; it removes useless declaration-only variables from
13848 // the IR.
13849 if (!is_class_type(scope)
13850 && !is_required_decl_spec
13851 // If we are asked to load undefined interfaces, then we don't
13852 // suppress declaration-only variables as they might have
13853 // undefined elf-symbols.
13854 && (!is_declaration_only || !rdr.load_undefined_interfaces()))
13855 {
13856 Dwarf_Addr var_addr = 0;
13857 if (!rdr.get_variable_address(variable_die, var_addr))
13858 return true;
13859
13860 elf_symbol_sptr symbol =
13861 rdr.variable_symbol_is_exported(var_addr);
13862 if (!symbol)
13863 return true;
13864 if (symbol->is_suppressed())
13865 return true;
13866
13867 // Since there is only one symbol in DWARF associated with an elf_symbol,
13868 // we can assume this is the main symbol then. Otherwise the main hinting
13869 // did not work as expected.
13870 ABG_ASSERT(symbol->is_main_symbol());
13871 if (symbol->has_aliases())
13872 for (elf_symbol_sptr a = symbol->get_next_alias();
13873 !a->is_main_symbol(); a = a->get_next_alias())
13874 if (a->is_suppressed())
13875 return true;
13876 }
13877
13879 qualified_name,
13880 linkage_name,
13881 /*require_drop_property=*/true);
13882}
13883
13884/// Test if a type (designated by a given DIE) in a given scope is
13885/// suppressed by the suppression specifications that are associated
13886/// to a given DWARF reader.
13887///
13888/// @param rdr the DWARF reader to consider.
13889///
13890/// @param scope of the scope of the type DIE to consider.
13891///
13892/// @param type_die the DIE that designates the type to consider.
13893///
13894/// @param type_is_opaque out parameter. If this function returns
13895/// true (the type @p type_die is suppressed) and if the type was
13896/// suppressed because it's opaque then this parameter is set to
13897/// true.
13898///
13899/// @return true iff the type designated by the DIE @p type_die, in
13900/// the scope @p scope is suppressed by at the suppression
13901/// specifications associated to the current DWARF reader.
13902static bool
13903type_is_suppressed(const reader& rdr,
13904 const scope_decl_sptr scope,
13905 Dwarf_Die* type_die,
13906 reader::tu_context_type_sptr& tu_ctxt,
13907 bool& type_is_opaque)
13908{
13909 if (type_die == 0
13910 || (dwarf_tag(type_die) != DW_TAG_enumeration_type
13911 && dwarf_tag(type_die) != DW_TAG_class_type
13912 && dwarf_tag(type_die) != DW_TAG_structure_type
13913 && dwarf_tag(type_die) != DW_TAG_union_type))
13914 return false;
13915
13916 string type_name, linkage_name;
13917 location type_location;
13918 die_loc_and_name(type_die, tu_ctxt, type_location,
13919 type_name, linkage_name);
13920 string qualified_name = build_qualified_name(scope, type_name);
13921
13922 return suppr::is_type_suppressed(rdr,
13923 qualified_name,
13924 type_location,
13925 type_is_opaque,
13926 /*require_drop_property=*/true);
13927}
13928
13929/// Test if a type (designated by a given DIE) in a given scope is
13930/// suppressed by the suppression specifications that are associated
13931/// to a given DWARF reader.
13932///
13933/// @param rdr the DWARF reader to consider.
13934///
13935/// @param scope of the scope of the type DIE to consider.
13936///
13937/// @param type_die the DIE that designates the type to consider.
13938///
13939/// @return true iff the type designated by the DIE @p type_die, in
13940/// the scope @p scope is suppressed by at the suppression
13941/// specifications associated to the current DWARF reader.
13942static bool
13943type_is_suppressed(const reader& rdr,
13944 const scope_decl_sptr scope,
13945 Dwarf_Die* type_die,
13946 reader::tu_context_type_sptr& tu_ctxt)
13947{
13948 bool type_is_opaque = false;
13949 return type_is_suppressed(rdr, scope, type_die, tu_ctxt, type_is_opaque);
13950}
13951
13952/// Get the opaque version of a type that was suppressed because it's
13953/// a private type.
13954///
13955/// The opaque version version of the type is just a declared-only
13956/// version of the type (class, union or enum type) denoted by @p
13957/// type_die.
13958///
13959/// @param rdr the DWARF reader in use.
13960///
13961/// @param scope the scope of the type die we are looking at.
13962///
13963/// @param type_die the type DIE we are looking at.
13964///
13965/// @param where_offset the offset of the DIE where we are "logically"
13966/// positionned at, in the DIE tree. This is useful when @p die is
13967/// e.g, DW_TAG_partial_unit that can be included in several places in
13968/// the DIE tree.
13969///
13970/// @return the opaque version of the type denoted by @p type_die or
13971/// nil if no opaque version was found.
13973get_opaque_version_of_type(reader& rdr,
13974 scope_decl_sptr scope,
13975 Dwarf_Die* type_die,
13976 reader::tu_context_type_sptr& tu_ctxt)
13977{
13979
13980 if (type_die == 0)
13981 return result;
13982
13983 unsigned tag = dwarf_tag(type_die);
13984 if (tag != DW_TAG_class_type
13985 && tag != DW_TAG_structure_type
13986 && tag != DW_TAG_union_type
13987 && tag != DW_TAG_enumeration_type)
13988 return result;
13989
13990 string type_name, linkage_name;
13991 location type_location;
13992 die_loc_and_name(type_die, tu_ctxt, type_location, type_name, linkage_name);
13993
13994 string qualified_name = build_qualified_name(scope, type_name);
13995
13996 //
13997 // TODO: also handle declaration-only unions. To do that, we mostly
13998 // need to adapt add_or_update_union_type to make it schedule
13999 // declaration-only unions for resolution too.
14000 //
14001 if (tag == DW_TAG_structure_type || tag == DW_TAG_class_type)
14002 {
14003 result = rdr.get_a_declaration_only_class(qualified_name);
14004
14005 if (!result)
14006 {
14007 // So we didn't find any pre-existing forward-declared-only
14008 // class for the class definition that we could return as an
14009 // opaque type. So let's build one.
14010 //
14011 // TODO: we need to be able to do this for unions too!
14012 class_decl_sptr klass(new class_decl(rdr.env(), type_name,
14013 /*alignment=*/0, /*size=*/0,
14014 tag == DW_TAG_structure_type,
14015 type_location,
14016 decl_base::VISIBILITY_DEFAULT));
14017 klass->set_is_declaration_only(true);
14018 klass->set_is_artificial(die_is_artificial(type_die));
14019 klass = is_class_type(rdr.maybe_associate_die_to_type(type_die, klass));
14020 rdr.maybe_schedule_declaration_only_class_for_resolution(klass);
14021 add_decl_to_scope(klass, scope);
14022 result = klass;
14023 }
14024 }
14025
14026 if (tag == DW_TAG_enumeration_type)
14027 {
14028 result = rdr.get_a_declaration_only_enum(qualified_name);
14029
14030 if (!result)
14031 {
14032 uint64_t size = 0;
14033 if (die_unsigned_constant_attribute(type_die, DW_AT_byte_size, size))
14034 size *= 8;
14035 type_decl_sptr underlying_type =
14036 build_enum_underlying_type(rdr, type_name, size,
14037 tu_ctxt, /*anonymous=*/true);
14038 enum_type_decl::enumerators enumeratorz;
14039 enum_type_decl_sptr enum_type (new enum_type_decl(type_name,
14040 type_location,
14041 underlying_type,
14042 enumeratorz,
14043 linkage_name));
14044 enum_type->set_is_artificial(die_is_artificial(type_die));
14045 enum_type = is_enum_type(rdr.maybe_associate_die_to_type(type_die,
14046 enum_type));
14047 add_decl_to_scope(enum_type, scope);
14048 result = enum_type;
14049 }
14050 }
14051
14052 return result;
14053}
14054
14055/// Create a function symbol with a given name.
14056///
14057/// @param sym_name the name of the symbol to create.
14058///
14059/// @param env the environment to create the symbol in.
14060///
14061/// @return the newly created symbol.
14063create_default_fn_sym(const string& sym_name, const environment& env)
14064{
14066 elf_symbol_sptr result =
14068 /*symbol index=*/ 0,
14069 /*symbol size=*/ 0,
14070 sym_name,
14071 /*symbol type=*/ elf_symbol::FUNC_TYPE,
14072 /*symbol binding=*/ elf_symbol::GLOBAL_BINDING,
14073 /*symbol is defined=*/ true,
14074 /*symbol is common=*/ false,
14075 /*symbol version=*/ ver,
14076 /*symbol visibility=*/elf_symbol::DEFAULT_VISIBILITY);
14077 return result;
14078}
14079
14080/// Build a @ref function_decl our of a DW_TAG_subprogram DIE.
14081///
14082/// @param rdr the DWARF reader to use
14083///
14084/// @param die the DW_TAG_subprogram DIE to read from.
14085///
14086/// @param where_offset the offset of the DIE where we are "logically"
14087/// positionned at, in the DIE tree. This is useful when @p die is
14088/// e.g, DW_TAG_partial_unit that can be included in several places in
14089/// the DIE tree.
14090///
14091/// @param called_for_public_decl this is set to true if the function
14092/// was called for a public (function) decl.
14093static function_decl_sptr
14094build_function_decl(reader& rdr,
14095 Dwarf_Die* die,
14096 void* where_addr,
14097 reader::tu_context_type_sptr& tu_ctxt,
14099{
14100 function_decl_sptr result = fn;
14101 if (!die)
14102 return result;
14103 int tag = dwarf_tag(die);
14104
14105 ABG_ASSERT(tag == DW_TAG_subprogram
14106 || tag == DW_TAG_inlined_subroutine
14107 || tag == DW_TAG_dwarf_procedure);
14108
14109 if (!die_is_public_decl(die))
14110 return result;
14111
14112 translation_unit_sptr tu = tu_ctxt->get_tu();
14113 ABG_ASSERT(tu);
14114
14115 string fname, flinkage_name;
14116 location floc;
14117 die_loc_and_name(die, tu_ctxt, floc, fname, flinkage_name);
14118 cleanup_decl_name(fname);
14119
14120 size_t is_inline = die_is_declared_inline(die);
14121 class_or_union_sptr is_method =
14122 is_class_or_union_type(get_scope_for_die(rdr, die, true,
14123 where_addr, tu_ctxt));
14124
14125 function_type_sptr fn_type;
14126 if (result)
14127 {
14128 // Add the properties that might have been missing from the
14129 // first declaration of the function. For now, it usually is
14130 // the mangled name that goes missing in the first declarations.
14131 //
14132 // Also note that if 'fn' has just been cloned, the current
14133 // linkage name (of the current DIE) might be different from the
14134 // linkage name of 'fn'. In that case, update the linkage name
14135 // of 'fn' too.
14136 if (!flinkage_name.empty()
14137 && result->get_linkage_name() != flinkage_name)
14138 result->set_linkage_name(flinkage_name);
14139 if (floc)
14140 if (!result->get_location())
14141 result->set_location(floc);
14142 result->is_declared_inline(is_inline);
14143 }
14144 else
14145 {
14146 // These are decls of types that might be used by the ABI of the
14147 // function.
14148 vector<decl_base_sptr> decls;
14149 fn_type = is_function_type(rdr.lookup_type_artifact_from_die(die));
14150 if (!fn_type)
14151 {
14152 fn_type = is_function_type(rdr.lookup_type_artifact_from_die(die));
14153 if (!fn_type)
14154 fn_type = build_function_type(rdr, die, is_method,
14155 where_addr, decls, tu_ctxt);
14156 }
14157
14158 if (!fn_type)
14159 return result;
14160
14161 result.reset(is_method
14162 ? new method_decl(fname, fn_type,
14163 is_inline, floc,
14164 flinkage_name)
14165 : new function_decl(fname, fn_type,
14166 is_inline, floc,
14167 flinkage_name));
14168
14169 // Add the types that might be used by the ABI (parameters or
14170 // return type) of the function and are declared right before
14171 // the function itself into the scope of the type.
14172 for (const auto& decl : decls)
14173 add_decl_to_scope(decl, result);
14174 }
14175
14176 // Set the symbol of the function. If the linkage name doesn't
14177 // match the name of the symbol, then update the symbol.
14178 if (!result->get_symbol()
14179 || result->get_symbol()->get_name() != result->get_linkage_name())
14180 {
14181 elf_symbol_sptr fn_sym;
14182 Dwarf_Addr fn_addr;
14183 if (rdr.get_function_address(die, fn_addr))
14184 {
14185 rdr.symtab()->
14186 update_main_symbol(fn_addr,
14187 result->get_linkage_name().empty()
14188 ? result->get_name()
14189 : result->get_linkage_name());
14190 fn_sym = rdr.function_symbol_is_exported(fn_addr);
14191 }
14192
14193 if (fn_sym)
14194 {
14195 result->set_symbol(fn_sym);
14196 result->set_is_in_public_symbol_table(true);
14197 if (result->get_linkage_name().empty())
14198 result->set_linkage_name(fn_sym->get_name());
14199 }
14200
14201 if (!fn_sym && rdr.is_decl_die_with_undefined_symbol(die))
14202 {
14203 // We are looking at a function which symbol is undefined.
14204 // let's set its symbol.
14205 string n = result->get_linkage_name();
14206 if (n.empty())
14207 n = result->get_name();
14208 fn_sym = rdr.symtab()->lookup_undefined_function_symbol(n);
14209 if (fn_sym)
14210 {
14211 result->set_symbol(fn_sym);
14212 result->set_is_in_public_symbol_table(false);
14213 }
14214 }
14215 }
14216
14217 return result;
14218}
14219
14220/// Canonicalize a type if it's suitable for early canonicalizing, or,
14221/// if it's not, schedule it for late canonicalization, after the
14222/// debug info of the current translation unit has been fully read.
14223///
14224/// A (composite) type is deemed suitable for early canonicalizing iff
14225/// all of its sub-types are canonicalized themselve. Non composite
14226/// types are always deemed suitable for early canonicalization.
14227///
14228/// Note that this function knows how to deal with anonymous classes,
14229/// structs and enums, unlike the overload below:
14230///
14231/// @param t the type DIE to consider for canonicalization.
14232///
14233/// @param rdr the @ref reader to use.
14234static void
14235maybe_canonicalize_type(const type_base_sptr& t,
14236 reader& rdr)
14237{
14238 if (!t)
14239 return;
14240
14241 rdr.schedule_type_for_late_canonicalization(t);
14242}
14243
14244/// If a given decl is a member type declaration, set its access
14245/// specifier from the DIE that represents it.
14246///
14247/// @param member_type_declaration the member type declaration to
14248/// consider.
14249static void
14250maybe_set_member_type_access_specifier(decl_base_sptr member_type_declaration,
14251 Dwarf_Die* die)
14252{
14253 if (is_type(member_type_declaration)
14254 && is_member_decl(member_type_declaration))
14255 {
14256 class_or_union_sptr scope =
14257 is_class_or_union_type(member_type_declaration->get_scope());
14258 ABG_ASSERT(scope);
14259
14260 access_specifier access = no_access;
14261 die_access_specifier(die, access);
14262 set_member_access_specifier(member_type_declaration, access);
14263 }
14264}
14265
14266/// Normalize a decl name so that it can be compared to other decl
14267/// names without risking to have spurious changes.
14268///
14269/// The function removes white spaces from the and normalizes
14270/// numerical litterals.
14271///
14272/// @param str in/out parameter. The string to normalize, in place.
14273static void
14274cleanup_decl_name(string& str)
14275{
14278}
14279
14280/// This function tests if a given function which might be intented to
14281/// be added to a class scope (to become a member function) should be
14282/// dropped on the floor instead and not be added to the class.
14283///
14284/// This is a subroutine of build_ir_node_from_die.
14285///
14286/// @param fn the function to consider.
14287///
14288/// @param fn_die the DWARF die of @p fn.
14289///
14290/// @return true iff @p fn should be dropped on the floor.
14291static bool
14292potential_member_fn_should_be_dropped(reader& rdr,
14293 reader::tu_context_type_sptr& tu_ctxt,
14294 const Dwarf_Die *fn_die)
14295{
14296 if (!fn_die)
14297 return true;
14298
14299 Dwarf_Die class_die;
14300 bool is_member_function = die_is_member_function(rdr, fn_die,
14301 /*where_addr=*/nullptr,
14302 tu_ctxt, class_die);
14303 if (// A member function ...
14305 // ... that is neither virtual ...
14306 && !die_is_virtual(fn_die)
14307 // ... with no exported ELF symbol
14308 &&!rdr.function_has_address(fn_die)
14309 // ... and yet we were instructed to NOT load undefined
14310 // interfaces.
14311 && !rdr.load_undefined_interfaces())
14312 // Should not be added to its class scope.
14313 //
14314 // Why would it? It's not part of the ABI anyway, as it doesn't
14315 // have any ELF symbol associated and is not a virtual member
14316 // function. It just constitutes bloat in the IR and might even
14317 // induce spurious change reports down the road.
14318 return true;
14319
14320 if (die_is_virtual(fn_die)
14321 && ((die_is_destructor(fn_die) && !rdr.function_has_address(fn_die))
14322 || die_linkage_name(fn_die).empty())
14323 // A virtual destructor with no ELF symbol is dropped on the
14324 // floor, as we only collect the concrete instances of the
14325 // virtual destructors thumb functions. Those always have ELF
14326 // symbol, if they are part of the ABI. Otherwise, they are
14327 // just abstract representation carried by the abstract instance
14328 // DIE of the containing class of the destructor.
14329 //
14330 // For virtual non-destructor member functions, we just require
14331 // that they have linkage names. This is important for pure virtual
14332 // member functions.
14333 //
14334 // Any other virtual function (with no linkage name or
14335 // destructor with no ELF symbol) is likely an abstract function
14336 // instance representation in DWARF. We'll later encounter its
14337 // concrete definition and that one will be added to its scope,
14338 // avoiding spurious duplication.
14339 )
14340 return true;
14341
14342 // Reject non-member functions not having a defined and exported
14343 // symbol, unless the user wants to load undefined interfaces.
14345 && (rdr.is_decl_die_with_undefined_symbol(fn_die)
14346 || !rdr.is_decl_die_with_exported_symbol(fn_die))
14347 && !rdr.load_undefined_interfaces())
14348 return true;
14349
14350 return false;
14351}
14352
14353/// Build an IR node from a given DIE and add the node to the current
14354/// IR being build and held in the DWARF reader. Doing that is called
14355/// "emitting an IR node for the DIE".
14356///
14357/// @param rdr the DWARF reader.
14358///
14359/// @param die the DIE to consider.
14360///
14361/// @param scope the scope under which the resulting IR node has to be
14362/// added.
14363///
14364/// @param called_from_public_decl set to yes if this function is
14365/// called from the functions used to build a public decl (functions
14366/// and variables). In that case, this function accepts building IR
14367/// nodes representing types. Otherwise, this function only creates
14368/// IR nodes representing public decls (functions and variables).
14369/// This is done to avoid emitting IR nodes for types that are not
14370/// referenced by public functions or variables.
14371///
14372/// @param where_addr the address of the DIE where we are "logically"
14373/// positionned at, in the DIE tree. This is useful when @p die is
14374/// e.g, DW_TAG_partial_unit that can be included in several places in
14375/// the DIE tree.
14376///
14377/// @param is_declaration_only is true if the DIE denoted by @p die is
14378/// a declaration-only DIE.
14379///
14380/// @param is_required_decl_spec this is set to true if the IR node of
14381/// the DIE is considered like if it is going to be added to the IR
14382/// being built. E.g, if the IR node is for a function_decl, and if
14383/// the function_decl has no asscociated ELF symbol,
14384/// build_ir_node_from_die is supposed to silently drop the resulting
14385/// IR node on the floor and return nullptr. But if this this
14386/// parameter is set to true, then the IR node for the function_decl
14387/// is NOT going to be dropped on the floor.
14388///
14389/// @return the resulting IR node.
14391build_ir_node_from_die(reader& rdr,
14392 Dwarf_Die* die,
14393 scope_decl_sptr scope,
14394 bool called_from_public_decl,
14395 void* where_addr,
14396 reader::tu_context_type_sptr& tu_ctxt,
14397 bool is_declaration_only,
14398 bool is_required_decl_spec)
14399{
14401
14402 if (!die || !scope)
14403 return result;
14404
14405 int tag = dwarf_tag(die);
14406 ABG_ASSERT(tag);
14407
14408 if (!called_from_public_decl)
14409 {
14410 if (rdr.load_all_types() && die_is_type(die))
14411 /* We were instructed to load debug info for all types,
14412 included those that are not reachable from a public
14413 declaration. So load the debug info for this type. */;
14414 else if (tag != DW_TAG_subprogram
14415 && tag != DW_TAG_variable
14416 && tag != DW_TAG_member
14417 && tag != DW_TAG_namespace)
14418 return result;
14419 }
14420
14421 if ((result = rdr.lookup_artifact_from_die(die, die_is_type(die))))
14422 return result;
14423
14424 // This is *the* bit of code that ensures we have the right notion
14425 // of "declared" at any point in a DIE chain formed from
14426 // DW_AT_abstract_origin and DW_AT_specification links. There should
14427 // be no other callers of die_is_declaration_only.
14428 is_declaration_only = is_declaration_only && die_is_declaration_only(die);
14429
14430 switch (tag)
14431 {
14432 // Type DIEs we support.
14433 case DW_TAG_base_type:
14434 if (type_decl_sptr t = build_type_decl(rdr, die, tu_ctxt))
14435 {
14436 t = is_type_decl(rdr.maybe_associate_die_to_type(die, t));
14437
14438 result =
14439 add_decl_to_scope(t, tu_ctxt->get_tu()->get_global_scope());
14440 maybe_canonicalize_type(t, rdr);
14441 }
14442 break;
14443
14444 case DW_TAG_typedef:
14445 {
14446 typedef_decl_sptr t = build_typedef_type(rdr, die,
14447 called_from_public_decl,
14448 where_addr, tu_ctxt);
14449
14450 t = is_typedef(rdr.maybe_associate_die_to_type(die, t));
14451 result = add_decl_to_scope(t, scope);
14452 if (result)
14453 {
14454 maybe_set_member_type_access_specifier(is_decl(result), die);
14455 maybe_canonicalize_type(t, rdr);
14456
14457 auto utype = t->get_underlying_type();
14458 if ((is_class_or_union_type(utype) || is_enum_type(utype))
14459 && is_anonymous_type(utype))
14460 {
14461 // This is a naming typedef for an enum or a class. Let's
14462 // mark the underlying decl as such.
14463 decl_base_sptr decl = is_decl(utype);
14464 ABG_ASSERT(decl);
14465 decl->add_naming_typedef(t);
14466 rdr.maybe_schedule_decl_only_type_for_resolution(utype);
14467 }
14468
14469 }
14470 }
14471 break;
14472
14473 case DW_TAG_pointer_type:
14474 {
14476 build_pointer_type_def(rdr, die,
14477 called_from_public_decl,
14478 where_addr, tu_ctxt);
14479 if (p)
14480 {
14481 p = is_pointer_type(rdr.maybe_associate_die_to_type(die, p));
14482 result =
14483 add_decl_to_scope(p, tu_ctxt->get_tu()->get_global_scope());
14484 maybe_canonicalize_type(p, rdr);
14485 }
14486 }
14487 break;
14488
14489 case DW_TAG_reference_type:
14490 case DW_TAG_rvalue_reference_type:
14491 {
14493 build_reference_type(rdr, die,
14494 called_from_public_decl,
14495 where_addr, tu_ctxt);
14496 if (r)
14497 {
14498 r = is_reference_type(rdr.maybe_associate_die_to_type(die, r));
14499 result =
14500 add_decl_to_scope(r, tu_ctxt->get_tu()->get_global_scope());
14501 maybe_canonicalize_type(r, rdr);
14502 }
14503 }
14504 break;
14505
14506 case DW_TAG_ptr_to_member_type:
14507 {
14509 build_ptr_to_mbr_type(rdr, die, called_from_public_decl,
14510 where_addr, tu_ctxt);
14511 if (p)
14512 {
14513 p = is_ptr_to_mbr_type(rdr.maybe_associate_die_to_type(die, p));
14514 result =
14515 add_decl_to_scope(p, tu_ctxt->get_tu()->get_global_scope());
14516 maybe_canonicalize_type(p, rdr);
14517 }
14518 }
14519 break;
14520
14521 case DW_TAG_const_type:
14522 case DW_TAG_volatile_type:
14523 case DW_TAG_restrict_type:
14524 {
14525 type_base_sptr q =
14526 build_qualified_type(rdr, die,
14527 called_from_public_decl,
14528 where_addr, tu_ctxt);
14529 if (q)
14530 {
14531 // Strip some potentially redundant type qualifiers from
14532 // the qualified type we just built.
14533 decl_base_sptr d = maybe_strip_qualification(is_qualified_type(q),
14534 rdr);
14535 if (!d)
14536 d = get_type_declaration(q);
14537 ABG_ASSERT(d);
14538 type_base_sptr ty = is_type(d);
14539 // Associate the die to type ty again because 'ty'might be
14540 // different from 'q', because 'ty' is 'q' possibly
14541 // stripped from some redundant type qualifier.
14542 result = rdr.maybe_associate_die_to_type(die, ty);
14543 d = is_decl(result);
14544 result =
14545 add_decl_to_scope(d, tu_ctxt->get_tu()->get_global_scope());
14546 ABG_ASSERT(result);
14547 maybe_canonicalize_type(is_type(result), rdr);
14548 }
14549 }
14550 break;
14551
14552 case DW_TAG_enumeration_type:
14553 {
14554 bool type_is_opaque = false;
14555 bool type_suppressed =
14556 type_is_suppressed(rdr, scope, die, tu_ctxt, type_is_opaque);
14557 if (type_suppressed && type_is_opaque)
14558 {
14559 // The type is suppressed because it's private. If other
14560 // non-suppressed and declaration-only instances of this
14561 // type exist in the current corpus, then it means those
14562 // non-suppressed instances are opaque versions of the
14563 // suppressed private type. Lets return one of these opaque
14564 // types then.
14565 result = get_opaque_version_of_type(rdr, scope, die, tu_ctxt);
14566 maybe_canonicalize_type(is_type(result), rdr);
14567 }
14568 else if (!type_suppressed)
14569 {
14570 enum_type_decl_sptr e = build_enum_type(rdr, die, tu_ctxt,
14571 is_declaration_only);
14572 e = is_enum_type(rdr.maybe_associate_die_to_type(die, e));
14573 result = add_decl_to_scope(e, scope);
14574 if (result)
14575 {
14576 maybe_set_member_type_access_specifier(is_decl(result), die);
14577 maybe_canonicalize_type(is_type(result), rdr);
14578 }
14579 }
14580 }
14581 break;
14582
14583 case DW_TAG_class_type:
14584 case DW_TAG_structure_type:
14585 {
14586 bool type_is_opaque = false;
14587 bool type_suppressed=
14588 type_is_suppressed(rdr, scope, die, tu_ctxt, type_is_opaque);
14589
14590 if (type_suppressed && type_is_opaque)
14591 {
14592 // The type is suppressed because it's private. If other
14593 // non-suppressed and declaration-only instances of this
14594 // type exist in the current corpus, then it means those
14595 // non-suppressed instances are opaque versions of the
14596 // suppressed private type. Lets return one of these opaque
14597 // types then.
14598 result = get_opaque_version_of_type(rdr, scope, die, tu_ctxt);
14599 maybe_canonicalize_type(is_type(result), rdr);
14600 }
14601 else if (!type_suppressed)
14602 {
14603 class_decl_sptr klass;
14604 Dwarf_Die spec_die;
14605 if (die_die_attribute(die, DW_AT_specification, spec_die))
14606 {
14607 scope_decl_sptr skope =
14608 get_scope_for_die(rdr, &spec_die,
14609 called_from_public_decl,
14610 where_addr, tu_ctxt);
14611 ABG_ASSERT(skope);
14612 decl_base_sptr cl =
14613 is_decl(build_ir_node_from_die(rdr, &spec_die,
14614 skope,
14615 called_from_public_decl,
14616 where_addr, tu_ctxt,
14617 is_declaration_only,
14618 /*is_required_decl_spec=*/false));
14619 ABG_ASSERT(cl);
14620 klass = dynamic_pointer_cast<class_decl>(cl);
14621 ABG_ASSERT(klass);
14622
14623 klass =
14624 add_or_update_class_type(rdr, die,
14625 tag == DW_TAG_structure_type,
14626 klass,
14627 called_from_public_decl,
14628 where_addr,
14629 is_declaration_only,
14630 tu_ctxt);
14631 }
14632 else
14633 {
14634 if (class_decl_sptr class_sc = is_class_type(scope))
14635 {
14636 string type_name = die_type_name(rdr, die,
14637 /*qualified_name=*/false,
14638 where_addr, tu_ctxt);
14639 if (class_decl_sptr c =
14640 is_class_type(class_sc->find_member_type(type_name)))
14641 klass = c;
14642 else
14643 klass =
14644 add_or_update_class_type(rdr, die,
14645 tag == DW_TAG_structure_type,
14647 called_from_public_decl,
14648 where_addr,
14649 is_declaration_only,
14650 tu_ctxt);
14651 }
14652 else
14653 klass =
14654 add_or_update_class_type(rdr, die,
14655 tag == DW_TAG_structure_type,
14657 called_from_public_decl,
14658 where_addr,
14659 is_declaration_only,
14660 tu_ctxt);
14661 }
14662 if (klass)
14663 {
14664 maybe_set_member_type_access_specifier(klass, die);
14665 klass = is_class_type(rdr.maybe_associate_die_to_type(die, klass));
14666 add_decl_to_scope(klass, scope);
14667 maybe_canonicalize_type(klass, rdr);
14668 }
14669 result = klass;
14670 }
14671 }
14672 break;
14673
14674 case DW_TAG_union_type:
14675 {
14676 if (!type_is_suppressed(rdr, scope, die, tu_ctxt))
14677 {
14678 union_decl_sptr union_type;
14679 if (class_decl_sptr class_sc = is_class_type(scope))
14680 {
14681 string type_name = die_type_name(rdr, die,
14682 /*qualified_name=*/false,
14683 where_addr, tu_ctxt);
14684 if (union_decl_sptr u =
14685 is_union_type(class_sc->find_member_type(type_name)))
14686 union_type = u;
14687 }
14688
14689 if (!union_type)
14690 union_type =
14691 add_or_update_union_type(rdr, die, union_decl_sptr(),
14692 called_from_public_decl,
14693 where_addr,
14694 is_declaration_only,
14695 tu_ctxt);
14696
14697 if (union_type)
14698 {
14699 union_type =
14700 is_union_type(rdr.maybe_associate_die_to_type(die, union_type));
14701 add_decl_to_scope(union_type, scope);
14702 maybe_set_member_type_access_specifier(union_type, die);
14703 maybe_canonicalize_type(union_type, rdr);
14704 result = union_type;
14705 }
14706 }
14707 }
14708 break;
14709 case DW_TAG_string_type:
14710 break;
14711 case DW_TAG_subroutine_type:
14712 {
14713 function_type_sptr f = build_function_type(rdr, die,
14715 where_addr, tu_ctxt);
14716 if (f)
14717 {
14718 result = f;
14719 result->set_is_artificial(false);
14720 bind_function_type_life_time(f, tu_ctxt->get_tu());
14721 maybe_canonicalize_type(f, rdr);
14722 }
14723 }
14724 break;
14725 case DW_TAG_array_type:
14726 {
14727 array_type_def_sptr a = build_array_type(rdr, die,
14728 called_from_public_decl,
14729 where_addr, tu_ctxt);
14730 if (a)
14731 {
14732 a = is_array_type(rdr.maybe_associate_die_to_type(die, a));
14733 result =
14734 add_decl_to_scope(a, tu_ctxt->get_tu()->get_global_scope());
14735 maybe_canonicalize_type(a, rdr);
14736 }
14737 break;
14738 }
14739 case DW_TAG_subrange_type:
14740 {
14741 // If we got here, this means the subrange type is a "free
14742 // form" defined in the global namespace of the current
14743 // translation unit, like what is found in Ada.
14745 build_subrange_type(rdr, die, where_addr, tu_ctxt,
14746 /*associate_type_to_die=*/false);
14747 if (s)
14748 {
14749 s = is_subrange_type(rdr.maybe_associate_die_to_type(die, s));
14750 result =
14751 add_decl_to_scope(s, tu_ctxt->get_tu()->get_global_scope());
14752 maybe_canonicalize_type(s, rdr);
14753 }
14754 }
14755 break;
14756 case DW_TAG_packed_type:
14757 break;
14758 case DW_TAG_set_type:
14759 break;
14760 case DW_TAG_file_type:
14761 break;
14762 case DW_TAG_thrown_type:
14763 break;
14764 case DW_TAG_interface_type:
14765 break;
14766 case DW_TAG_unspecified_type:
14767 break;
14768 case DW_TAG_shared_type:
14769 break;
14770
14771 case DW_TAG_compile_unit:
14772 // We shouldn't reach this point b/c this should be handled by
14773 // build_translation_unit.
14775
14776 case DW_TAG_namespace:
14777 case DW_TAG_module:
14778 result = build_namespace_decl_and_add_to_ir(rdr, die,
14779 where_addr,
14780 tu_ctxt);
14781 break;
14782
14783 case DW_TAG_variable:
14784 case DW_TAG_member:
14785 {
14786 if (tag == DW_TAG_member)
14787 ABG_ASSERT(!die_is_in_c(die));
14788
14789 scope_decl_sptr var_scope =
14790 get_scope_for_die(rdr, die,
14791 /*called_from_public_decl=*/
14792 die_is_effectively_public_decl(rdr, die, tu_ctxt),
14793 where_addr, tu_ctxt);
14794 var_decl_sptr v =
14795 build_or_get_var_decl_if_not_suppressed(rdr, var_scope, die,
14796 where_addr, tu_ctxt,
14797 is_declaration_only,
14798 /*result=*/var_decl_sptr(),
14799 is_required_decl_spec);
14800 if (v && is_data_member(v))
14801 // We might have gotten a pre-existing data member variable
14802 // that was already built. This means this DIE is a
14803 // concrete implementation of a previous specification.
14804 // Read the specific attributes of this concrete
14805 // implementation and add them to the existing IR node we
14806 // have.
14807 v = build_var_decl(rdr, die, where_addr, tu_ctxt, v);
14808
14809 Dwarf_Addr addr = 0;
14810 bool has_data_location = false;
14811 has_data_location = rdr.get_variable_address(die, addr);
14812
14813 if ((v && has_data_location && is_class_type(var_scope))
14814 // This is most likely for a static data member's variable
14815 // that has data location ...
14816 || (v && rdr.is_decl_die_with_undefined_symbol(die))
14817 || (v && rdr.is_decl_die_with_exported_symbol(die))
14818 // ... or this is for an undefined or defined & exported
14819 // global variable.
14820 )
14821 {
14822 v = is_var_decl(rdr.maybe_associate_die_to_decl(die, v));
14823 add_decl_to_scope(v, var_scope);
14824 if (is_data_member(v))
14825 // We are sure this is a static data member at this
14826 // point because a non-static data member would have
14827 // been encountered as a child of a class or union DIE
14828 // and thus handled by add_or_update_class_type or
14829 // add_or_update_union_type.
14830 set_member_is_static(v, true);
14831 else if (is_global_scope(var_scope))
14832 // Some old DWARF emitters wrongly emit global variables
14833 // with linkage names that actually make these global
14834 // variables be static data members. Let's thus stash
14835 // global variables for now, and when the TU is built, a
14836 // pass will look into them and put the one in the need
14837 // into their right scope.
14838 tu_ctxt->var_decls_to_re_add_to_tree().push_back(v);
14839
14840 // Add the var to exported interface *only* if the
14841 // containing class (if any) has been associated to a die.
14842 if (is_data_member(v))
14843 {
14845 if (rdr.has_scope_of_die_been_associated(die, where_addr,
14846 tu_ctxt))
14847 rdr.add_var_to_exported_or_undefined_decls(v);
14848 }
14849 else
14850 rdr.add_var_to_exported_or_undefined_decls(v);
14851 result = v;
14852 }
14853 }
14854 break;
14855
14856 case DW_TAG_subprogram:
14857 case DW_TAG_inlined_subroutine:
14858 {
14859 if (die_is_artificial(die))
14860 break;
14861
14862 Dwarf_Die abstract_origin_die;
14863 memset(&abstract_origin_die, 0, sizeof(abstract_origin_die));
14864
14865 // The abstract origin is the ultimate value of the
14866 // DW_AT_abstract_origin or DW_AT_specification attribute.
14867 bool has_abstract_origin = die_origin_die(die, abstract_origin_die);
14868
14869 scope_decl_sptr s = get_scope_for_die(rdr, die, called_from_public_decl,
14870 where_addr, tu_ctxt);
14871 scope_decl_sptr interface_scope = scope ? scope : s;
14872 class_or_union_sptr class_scope =
14873 is_class_or_union_type(interface_scope);
14874
14875 string linkage_name = die_linkage_name(die);
14876
14877 string spec_linkage_name;
14878 function_decl_sptr existing_fn;
14879
14880 if (class_scope)
14881 {
14882 // The scope of the function DIE we are looking at is a
14883 // class. So we are looking at a member function.
14884 if (has_abstract_origin)
14885 existing_fn = is_function_decl
14886 (rdr.lookup_decl_from_die_addr(abstract_origin_die.addr));
14887 if (!existing_fn && !linkage_name.empty())
14888 existing_fn =
14889 class_scope->find_member_function_sptr(linkage_name);
14890
14891 if (existing_fn
14892 && existing_fn->get_scope()
14893 && existing_fn->get_scope().get() != class_scope.get())
14894 existing_fn.reset();
14895 }
14896 else if (has_abstract_origin)
14897 // Let's see if this function is the implementation of an
14898 // existing interface. In that case, let's read the
14899 // specification of the origin interface ...
14900 existing_fn = build_function_decl(rdr, &abstract_origin_die,
14901 where_addr, tu_ctxt,
14902 /*existing_fn=*/nullptr);
14903
14904 tu_ctxt->scope_stack().push(interface_scope);
14905
14906 ABG_ASSERT(!class_scope
14907 || !existing_fn
14908 || !existing_fn->get_scope()
14909 || existing_fn->get_scope() == class_scope);
14910
14911 // Either we create a brand new IR for the current function
14912 // DIE we are looking at, or we complete an existing IR node
14913 // with the new completementary information carried by this
14914 // DIE for that IR node or we drop this DIE on the floor
14915 // because of some suppression rule.
14916 result =
14917 build_or_get_fn_decl_if_not_suppressed(rdr, interface_scope,
14918 die, where_addr, tu_ctxt,
14919 is_declaration_only,
14920 existing_fn);
14921
14922 // OK so we came to the conclusion that we need to keep
14923 // the function. So let's add it to its scope.
14924 if (result)
14925 {
14927 ABG_ASSERT(!class_scope
14928 || !fn->get_scope()
14929 || fn->get_scope() == class_scope);
14930
14931 if (class_scope)
14932 {
14933 // The function is a member function. Let's make sure
14934 // the parent class doesn't contain duplicate member
14935 // functions with the same linkage name as this one.
14936 string linkage_name = fn->get_linkage_name();
14937 fn = class_scope->find_member_function_sptr(linkage_name);
14938 if (!fn)
14939 {
14940 // Let's try harder by using the symbol name as
14941 // linkage name.
14942 fn = is_function_decl(result);
14943 if (fn->get_symbol())
14944 linkage_name = fn->get_symbol()->get_name();
14945 fn = class_scope->find_member_function_sptr(linkage_name);
14946 }
14947
14948 if (!fn)
14949 // We haven't found any duplicate. So let's use the
14950 // new function that we built so far.
14951 fn = is_function_decl(result);
14952 }
14953
14954 result = add_decl_to_scope(is_decl(fn), interface_scope);
14955 }
14956
14958
14959 if (fn)
14960 {
14961 // At this point, fn DOES have a scope which must be equal
14962 // to the scope expected/requested by build_ir_node_from_die.
14963 ABG_ASSERT(fn->get_scope());
14964 ABG_ASSERT(!scope || fn->get_scope() == scope);
14965
14966 if (fn
14967 && !tu_ctxt->is_wip_function_type(fn->get_type())
14968 &&
14969 (!is_member_function(fn)
14970 || (fn->get_symbol() && fn->get_symbol()->is_public())))
14971 // Among member functions, only those with public ELF
14972 // symbols are added to the set of functions exported by
14973 // the current ABI corpus.
14974 rdr.add_fn_to_exported_or_undefined_decls(fn.get(), /*update=*/true);
14975 if (fn)
14976 {
14977 maybe_canonicalize_type(fn->get_type(), rdr);
14978 bind_function_type_life_time(fn->get_type(),
14979 tu_ctxt->get_tu());
14980 }
14981 }
14982
14983 if (fn)
14984 {
14985 if (is_member_function(fn)
14986 && !tu_ctxt->is_wip_function_type_die(die))
14987 {
14988 class_or_union_sptr cou =
14989 is_class_or_union_type(interface_scope);
14990 ABG_ASSERT(cou);
14991 finish_member_function_reading(die, fn, cou, rdr);
14992 }
14993 else if (is_member_function(fn)
14994 && tu_ctxt->is_wip_function_type_die(die))
14995 {
14996 ABG_ASSERT(fn->get_scope());
14997 rdr.schedule_method_to_finish_reading(*die, fn);
14998 }
14999 }
15000
15001 tu_ctxt->scope_stack().pop();
15002 }
15003 break;
15004
15005 case DW_TAG_formal_parameter:
15006 // We should not read this case as it should have been dealt
15007 // with by build_function_decl above.
15009
15010 case DW_TAG_constant:
15011 break;
15012 case DW_TAG_enumerator:
15013 break;
15014
15015 case DW_TAG_partial_unit:
15016 // For now, the DIEs under these are read lazily when they are
15017 // referenced by a public decl DIE that is under a
15018 // DW_TAG_compile_unit, so we shouldn't get here.
15020
15021 case DW_TAG_imported_unit:
15022 break;
15023
15024 // Other declaration we don't really intend to support yet.
15025 case DW_TAG_dwarf_procedure:
15026 case DW_TAG_imported_declaration:
15027 case DW_TAG_entry_point:
15028 case DW_TAG_label:
15029 case DW_TAG_lexical_block:
15030 case DW_TAG_unspecified_parameters:
15031 case DW_TAG_variant:
15032 case DW_TAG_common_block:
15033 case DW_TAG_common_inclusion:
15034 case DW_TAG_inheritance:
15035 case DW_TAG_with_stmt:
15036 case DW_TAG_access_declaration:
15037 case DW_TAG_catch_block:
15038 case DW_TAG_friend:
15039 case DW_TAG_namelist:
15040 case DW_TAG_namelist_item:
15041 case DW_TAG_template_type_parameter:
15042 case DW_TAG_template_value_parameter:
15043 case DW_TAG_try_block:
15044 case DW_TAG_variant_part:
15045 case DW_TAG_imported_module:
15046 case DW_TAG_condition:
15047 case DW_TAG_type_unit:
15048 case DW_TAG_template_alias:
15049 case DW_TAG_lo_user:
15050 case DW_TAG_MIPS_loop:
15051 case DW_TAG_format_label:
15052 case DW_TAG_function_template:
15053 case DW_TAG_class_template:
15054 case DW_TAG_GNU_BINCL:
15055 case DW_TAG_GNU_EINCL:
15056 case DW_TAG_GNU_template_template_param:
15057 case DW_TAG_GNU_template_parameter_pack:
15058 case DW_TAG_GNU_formal_parameter_pack:
15059 case DW_TAG_GNU_call_site:
15060 case DW_TAG_GNU_call_site_parameter:
15061 case DW_TAG_hi_user:
15062 default:
15063 break;
15064 }
15065
15066 if (is_type(result))
15067 {
15068 offset_t native_offset = dwarf_dieoffset(die);
15069 result->set_native_offset(native_offset);
15070 }
15071
15072 if (result)
15073 result->set_corpus(rdr.corpus().get());
15074
15075 rdr.maybe_schedule_decl_only_type_for_resolution(result);
15076
15077 return result;
15078}
15079
15080/// Build the IR node for a void type.
15081///
15082/// @param rdr the DWARF reader to use.
15083///
15084/// @return the void type node.
15085static decl_base_sptr
15086build_ir_node_for_void_type(reader& rdr,
15087 reader::tu_context_type_sptr& tu_ctxt)
15088{
15089 const environment& env = rdr.env();
15090
15091 type_base_sptr t = env.get_void_type();
15092 decl_base_sptr type_declaration = get_type_declaration(t);
15093 {
15094 if (!has_scope(type_declaration))
15095 {
15096 add_decl_to_scope(is_decl(t), tu_ctxt->get_tu()->get_global_scope());
15097 rdr.schedule_type_for_late_canonicalization(t);
15098 }
15099 }
15100 return type_declaration;
15101}
15102
15103/// Build the IR node for a "pointer to void type".
15104///
15105/// That IR node is shared across the ABI corpus.
15106///
15107/// Note that this function just gets that IR node from the
15108/// environment and, if it's not added to any scope yet, adds it to
15109/// the global scope associated to the current translation unit.
15110///
15111/// @param rdr the DWARF reader to consider.
15112///
15113/// @return the IR node.
15115build_ir_node_for_void_pointer_type(reader& rdr,
15116 reader::tu_context_type_sptr& tu_ctxt)
15117{
15118 const environment& env = rdr.env();
15119 type_base_sptr t = env.get_void_pointer_type();
15120 decl_base_sptr type_declaration = get_type_declaration(t);
15121 {
15122 if (!has_scope(type_declaration))
15123 {
15124 add_decl_to_scope(is_decl(t), tu_ctxt->get_tu()->get_global_scope());
15125 rdr.schedule_type_for_late_canonicalization(t);
15126 }
15127 }
15128 return type_declaration;
15129}
15130
15131/// Build the IR node for a variadic parameter type.
15132///
15133/// @param rdr the DWARF reader to use.
15134///
15135/// @return the variadic parameter type.
15136static decl_base_sptr
15137build_ir_node_for_variadic_parameter_type(reader &rdr,
15138 reader::tu_context_type_sptr& tu_ctxt)
15139{
15140
15141 const environment& env = rdr.env();
15142 type_base_sptr t = env.get_variadic_parameter_type();
15143 decl_base_sptr type_declaration = get_type_declaration(t);
15144 {
15145 if (!has_scope(type_declaration))
15146 {
15147 add_decl_to_scope(is_decl(t), tu_ctxt->get_tu()->get_global_scope());
15148 rdr.schedule_type_for_late_canonicalization(t);
15149 }
15150 }
15151 return type_declaration;
15152}
15153
15154/// Build an IR node from a given DIE and add the node to the current
15155/// IR being build and held in the DWARF reader. Doing that is called
15156/// "emitting an IR node for the DIE".
15157///
15158/// @param rdr the DWARF reader.
15159///
15160/// @param die the DIE to consider.
15161///
15162/// @param called_from_public_decl set to yes if this function is
15163/// called from the functions used to build a public decl (functions
15164/// and variables). In that case, this function accepts building IR
15165/// nodes representing types. Otherwise, this function only creates
15166/// IR nodes representing public decls (functions and variables).
15167/// This is done to avoid emitting IR nodes for types that are not
15168/// referenced by public functions or variables.
15169///
15170/// @param where_addr the address of the DIE where we are "logically"
15171/// positionned at, in the DIE tree. This is useful when @p die is in
15172/// a e.g, DW_TAG_partial_unit that can be included in several places
15173/// in the DIE tree.
15174///
15175/// @param is_required_decl_spec this is set to true if the IR node of
15176/// the DIE is considered like if it is going to be added to the IR
15177/// being built. E.g, if the IR node is for a function_decl, and if
15178/// the function_decl has no asscociated ELF symbol,
15179/// build_ir_node_from_die is supposed to silently drop the resulting
15180/// IR node on the floor and return nullptr. But if this this
15181/// parameter is set to true, then the IR node for the function_decl
15182/// is NOT going to be dropped on the floor.
15183///
15184/// @return the resulting IR node.
15186build_ir_node_from_die(reader& rdr,
15187 Dwarf_Die* die,
15188 bool called_from_public_decl,
15189 void* where_addr,
15190 reader::tu_context_type_sptr& tu_ctxt,
15191 bool is_required_decl_spec)
15192{
15193 if (!die)
15194 return decl_base_sptr();
15195
15196 // Normaly, a decl that is meant to be external has a DW_AT_external
15197 // set. But then some compilers fail to always emit that flag. For
15198 // instance, for static data members, some compilers won't emit the
15199 // DW_AT_external. In that case, we assume that if the variable is
15200 // at global or named namespace scope, then we can assume it's
15201 // external. If the variable doesn't have any ELF symbol associated
15202 // to it, it'll be dropped on the floor anyway. Those variable
15203 // decls are considered as being "effectively public".
15204 bool consider_as_called_from_public_decl =
15205 called_from_public_decl || die_is_effectively_public_decl(rdr, die,
15206 tu_ctxt);
15207 scope_decl_sptr scope = get_scope_for_die(rdr, die,
15208 consider_as_called_from_public_decl,
15209 where_addr, tu_ctxt);
15210
15212
15213 if ((result = rdr.lookup_artifact_from_die(die, die_is_type(die))))
15214 {
15215 if (auto d = is_decl(result))
15216 {
15217 if (d->get_scope().get() == scope.get())
15218 return result;
15219 else
15220 result.reset();
15221 }
15222 else
15223 return result;
15224 }
15225
15226 if (!scope)
15227 scope = get_global_scope(rdr, tu_ctxt, die);
15228
15229 result = build_ir_node_from_die(rdr, die, scope,
15230 called_from_public_decl,
15231 where_addr, tu_ctxt, true,
15232 is_required_decl_spec);
15233 return result;
15234}
15235
15236/// Create a dwarf::reader.
15237///
15238/// @param elf_path the path to the elf file the reader is to be used
15239/// for.
15240///
15241/// @param debug_info_root_paths a vector to the paths to the
15242/// directories under which the debug info is to be found for @p
15243/// elf_path. Pass an empty vector if the debug info is not in a
15244/// split file.
15245///
15246/// @param environment the environment used by the current context.
15247/// This environment contains resources needed by the DWARF reader and by
15248/// the types and declarations that are to be created later. Note
15249/// that ABI artifacts that are to be compared all need to be created
15250/// within the same environment.
15251///
15252/// Please also note that the life time of this environment object
15253/// must be greater than the life time of the resulting @ref
15254/// reader the context uses resources that are allocated in the
15255/// environment.
15256///
15257/// @param options the options to set to the newly created instance of
15258/// @ref fe_iface. The options object needs to be created by the
15259/// caller code.
15260///
15261/// @return a smart pointer to the resulting dwarf::reader.
15262elf_based_reader_sptr
15263create_reader(const std::string& elf_path,
15264 const vector<string>& debug_info_root_paths,
15266 const fe_iface::options_type& options)
15267{
15268
15269 reader_sptr r = reader::create(elf_path,
15270 debug_info_root_paths,
15271 environment, options);
15272 return static_pointer_cast<elf_based_reader>(r);
15273}
15274
15275/// Create a dwarf::reader.
15276///
15277/// @param elf_path the path to the elf file the reader is to be used
15278/// for.
15279///
15280/// @param debug_info_root_paths a vector to the paths to the
15281/// directories under which the debug info is to be found for @p
15282/// elf_path. Pass an empty vector if the debug info is not in a
15283/// split file.
15284///
15285/// @param environment the environment used by the current context.
15286/// This environment contains resources needed by the DWARF reader and by
15287/// the types and declarations that are to be created later. Note
15288/// that ABI artifacts that are to be compared all need to be created
15289/// within the same environment.
15290///
15291/// Please also note that the life time of this environment object
15292/// must be greater than the life time of the resulting @ref
15293/// reader the context uses resources that are allocated in the
15294/// environment.
15295///
15296/// @return a smart pointer to the resulting dwarf::reader.
15297elf_based_reader_sptr
15298create_reader(const std::string& elf_path,
15299 const vector<string>& debug_info_root_paths,
15301{
15303 return create_reader(elf_path, debug_info_root_paths, environment, o);
15304}
15305
15306/// Re-initialize a reader so that it can re-used to read
15307/// another binary.
15308///
15309/// @param rdr the context to re-initialize.
15310///
15311/// @param elf_path the path to the elf file the context is to be used
15312/// for.
15313///
15314/// @param debug_info_root_path a pointer to the path to the root
15315/// directory under which the debug info is to be found for @p
15316/// elf_path. Leave this to NULL if the debug info is not in a split
15317/// file.
15318///
15319/// @param environment the environment used by the current context.
15320/// This environment contains resources needed by the DWARF reader and by
15321/// the types and declarations that are to be created later. Note
15322/// that ABI artifacts that are to be compared all need to be created
15323/// within the same environment.
15324///
15325/// Please also note that the life time of this environment object
15326/// must be greater than the life time of the resulting @ref
15327/// reader the context uses resources that are allocated in the
15328/// environment.
15329///
15330/// @param options the options to set to the newly created instance of
15331/// @ref fe_iface. The options object needs to be created by the
15332/// caller code.
15333///
15334/// @return a smart pointer to the resulting dwarf::reader.
15335void
15337 const std::string& elf_path,
15338 const vector<string>& debug_info_root_path,
15339 const fe_iface::options_type& options)
15340{
15341 reader& r = dynamic_cast<reader&>(rdr);
15342 r.options() = options;
15343 r.initialize(elf_path, debug_info_root_path);
15344}
15345
15346/// Read all @ref abigail::translation_unit possible from the debug info
15347/// accessible from an elf file, stuff them into a libabigail ABI
15348/// Corpus and return it.
15349///
15350/// @param elf_path the path to the elf file.
15351///
15352/// @param debug_info_root_paths a vector of pointers to root paths
15353/// under which to look for the debug info of the elf files that are
15354/// later handled by the Dwfl. This for cases where the debug info is
15355/// split into a different file from the binary we want to inspect.
15356/// On Red Hat compatible systems, this root path is usually
15357/// /usr/lib/debug by default. If this argument is set to NULL, then
15358/// "./debug" and /usr/lib/debug will be searched for sub-directories
15359/// containing the debug info file.
15360///
15361/// @param environment the environment used by the current context.
15362/// This environment contains resources needed by the DWARF reader and by
15363/// the types and declarations that are to be created later. Note
15364/// that ABI artifacts that are to be compared all need to be created
15365/// within the same environment. Also, the lifetime of the
15366/// environment must be greater than the lifetime of the resulting
15367/// corpus because the corpus uses resources that are allocated in the
15368/// environment.
15369///
15370/// @param options the options to set to the newly created instance of
15371/// @ref fe_iface. The options object needs to be created by the
15372/// caller code.
15373///
15374/// @param resulting_corp a pointer to the resulting abigail::corpus.
15375///
15376/// @return the resulting status.
15377corpus_sptr
15378read_corpus_from_elf(const std::string& elf_path,
15379 const vector<string>& debug_info_root_paths,
15381 const fe_iface::options_type& options,
15382 fe_iface::status& status)
15383{
15384 elf_based_reader_sptr rdr =
15385 dwarf::reader::create(elf_path, debug_info_root_paths,
15386 environment, options);
15387
15388 return rdr->read_corpus(status);
15389}
15390
15391/// Read all @ref abigail::translation_unit possible from the debug info
15392/// accessible from an elf file, stuff them into a libabigail ABI
15393/// Corpus and return it.
15394///
15395/// @param elf_path the path to the elf file.
15396///
15397/// @param debug_info_root_paths a vector of pointers to root paths
15398/// under which to look for the debug info of the elf files that are
15399/// later handled by the Dwfl. This for cases where the debug info is
15400/// split into a different file from the binary we want to inspect.
15401/// On Red Hat compatible systems, this root path is usually
15402/// /usr/lib/debug by default. If this argument is set to NULL, then
15403/// "./debug" and /usr/lib/debug will be searched for sub-directories
15404/// containing the debug info file.
15405///
15406/// @param environment the environment used by the current context.
15407/// This environment contains resources needed by the DWARF reader and by
15408/// the types and declarations that are to be created later. Note
15409/// that ABI artifacts that are to be compared all need to be created
15410/// within the same environment. Also, the lifetime of the
15411/// environment must be greater than the lifetime of the resulting
15412/// corpus because the corpus uses resources that are allocated in the
15413/// environment.
15414///
15415/// @param resulting_corp a pointer to the resulting abigail::corpus.
15416///
15417/// @return the resulting status.
15418corpus_sptr
15419read_corpus_from_elf(const std::string& elf_path,
15420 const vector<string>& debug_info_root_paths,
15422 fe_iface::status& status)
15423{
15425 return read_corpus_from_elf(elf_path, debug_info_root_paths,
15426 environment, options, status);
15427}
15428
15429/// Look into the symbol tables of a given elf file and see if we find
15430/// a given symbol.
15431///
15432/// @param env the environment we are operating from.
15433///
15434/// @param elf_path the path to the elf file to consider.
15435///
15436/// @param symbol_name the name of the symbol to look for.
15437///
15438/// @param demangle if true, try to demangle the symbol name found in
15439/// the symbol table.
15440///
15441/// @param syms the vector of symbols found with the name @p symbol_name.
15442///
15443/// @return true iff the symbol was found among the publicly exported
15444/// symbols of the ELF file.
15445bool
15446lookup_symbol_from_elf(const environment& env,
15447 const string& elf_path,
15448 const string& symbol_name,
15449 bool demangle,
15450 vector<elf_symbol_sptr>& syms)
15451
15452{
15453 if (elf_version(EV_CURRENT) == EV_NONE)
15454 return false;
15455
15456 int fd = open(elf_path.c_str(), O_RDONLY);
15457 if (fd < 0)
15458 return false;
15459
15460 struct stat s;
15461 if (fstat(fd, &s))
15462 return false;
15463
15464 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
15465 if (elf == 0)
15466 return false;
15467
15468 bool value = lookup_symbol_from_elf(env, elf, symbol_name,
15469 demangle, syms);
15470 elf_end(elf);
15471 close(fd);
15472
15473 return value;
15474}
15475
15476/// Look into the symbol tables of an elf file to see if a public
15477/// function of a given name is found.
15478///
15479/// @param env the environment we are operating from.
15480///
15481/// @param elf_path the path to the elf file to consider.
15482///
15483/// @param symbol_name the name of the function to look for.
15484///
15485/// @param syms the vector of public function symbols found with the
15486/// name @p symname.
15487///
15488/// @return true iff a function with symbol name @p symbol_name is
15489/// found.
15490bool
15491lookup_public_function_symbol_from_elf(environment& env,
15492 const string& path,
15493 const string& symname,
15494 vector<elf_symbol_sptr>& syms)
15495{
15496 if (elf_version(EV_CURRENT) == EV_NONE)
15497 return false;
15498
15499 int fd = open(path.c_str(), O_RDONLY);
15500 if (fd < 0)
15501 return false;
15502
15503 struct stat s;
15504 if (fstat(fd, &s))
15505 return false;
15506
15507 Elf* elf = elf_begin(fd, ELF_C_READ, 0);
15508 if (elf == 0)
15509 return false;
15510
15511 bool value = lookup_public_function_symbol_from_elf(env, elf, symname, syms);
15512 elf_end(elf);
15513 close(fd);
15514
15515 return value;
15516}
15517
15518}// end namespace dwarf
15519
15520}// end namespace abigail
The private data and functions of the abigail::ir::corpus type.
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:1790
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.
This file declares an interface for the worker threads (or thread pool) design pattern....
Simplified implementation of std::optional just enough to be used as a replacement for our purposes a...
elf_symbol_sptr function_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined function symbol.
const string & elf_architecture() const
Get the value of the 'ARCHITECTURE' property of the current ELF file.
const vector< string > & debug_info_root_paths() const
Getter of the vector of directory paths to look into for split debug information files.
const Dwarf * dwarf_debug_info() const
Getter of the handle used to access DWARF information from the current ELF file.
Elf * elf_handle() const
Getter of the handle used to access ELF information from the current ELF file.
virtual ir::corpus_sptr read_corpus(status &status)
Read the ELF information associated to the current ELF file and construct an ABI representation from ...
const Dwarf * alternate_dwarf_debug_info() const
Getter of the handle use to access DWARF information from the alternate split DWARF information.
bool refers_to_alt_debug_info(string &alt_di_path) const
Check if the underlying elf file refers to an alternate debug info file associated to it.
elf_symbol_sptr variable_symbol_is_undefined(const string &name) const
Test if a name is the name of an undefined variable symbol.
elf_symbol_sptr variable_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given variable symbol has been exported.
symtab_reader::symtab_sptr & symtab() const
Getter of an abstract representation of the symbol table of the underlying ELF file.
elf_symbol_sptr function_symbol_is_exported(GElf_Addr symbol_address) const
Test if a given function symbol has been exported.
const vector< string > & dt_needed() const
Get the value of the DT_NEEDED property of the current ELF file.
The common interface of readers based on ELF.
virtual void initialize(const std::string &elf_path, const vector< string > &debug_info_root_paths)
(re)Initialize) the resources used by the current reader.
status
The status of the fe_iface::read_corpus call.
@ STATUS_DEBUG_INFO_NOT_FOUND
This status is for when the debug info could not be read.
@ STATUS_ALT_DEBUG_INFO_NOT_FOUND
This status is for when the alternate debug info could not be found.
@ STATUS_OK
This status is for when the call went OK.
@ STATUS_UNKNOWN
The status is in an unknown state.
const options_type & options() const
Getter of the the options of the current Front End Interface.
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.
bool empty() const
Test if the current instance of interned_string is empty.
Definition abg-ir.cc:232
This class is to hold the value of the bound of a subrange. The value can be either signed or unsigne...
Definition abg-ir.h:2593
static string vector_as_string(const vector< subrange_sptr > &)
Return a string representation of a vector of subranges.
Definition abg-ir.cc:20273
shared_ptr< subrange_type > subrange_sptr
Convenience typedef for a shared pointer on a function_decl::subrange.
Definition abg-ir.h:2570
std::vector< subrange_sptr > subranges_type
Convenience typedef for a vector of subrange_sptr.
Definition abg-ir.h:2573
shared_ptr< base_spec > base_spec_sptr
Convenience typedef.
Definition abg-ir.h:4229
Abstracts the building of the set of exported variables and functions.
Definition abg-corpus.h:427
Abstraction of a group of corpora.
Definition abg-corpus.h:470
This is the abstraction of a set of translation units (themselves seen as bundles of unitary abi arte...
Definition abg-corpus.h:95
origin
This abstracts where the corpus comes from. That is, either it has been read from the native xml form...
Definition abg-corpus.h:121
void sort_functions()
Sort the set of functions exported by this corpus.
void set_soname(const string &)
Setter for the soname property of the corpus.
exported_decls_builder_sptr get_exported_decls_builder() const
Getter for the object that is responsible for determining what decls ought to be in the set of export...
void add(const translation_unit_sptr &)
Add a translation unit to the current ABI Corpus.
const translation_units & get_translation_units() const
Return the list of translation units of the current corpus.
origin get_origin() const
Getter for the origin of the corpus.
const translation_unit_sptr find_translation_unit(const string &path) const
Find the translation unit that has a given path.
string & get_path() const
Get the file path associated to the corpus file.
void set_origin(origin)
Setter for the origin of the corpus.
void sort_variables()
Sort the set of variables exported by this corpus.
virtual const variables & get_variables() const
Return the public decl table of the global variables of the current corpus.
void set_needed(const vector< string > &)
Setter of the needed property of the corpus.
void set_architecture_name(const string &)
Setter for the architecture name of the corpus.
void set_symtab(symtab_reader::symtab_sptr)
Setter for the symtab object.
virtual const functions & get_functions() const
Return the functions public decl table of the current corpus.
The base type of all declarations.
Definition abg-ir.h:1584
virtual void get_qualified_name(interned_string &qualified_name, bool internal=false) const
Compute the qualified name of the decl.
Definition abg-ir.cc:5961
const location & get_location() const
Get the location of a given declaration.
Definition abg-ir.cc:5704
scope_decl_sptr get_scope() const
Return the type containing the current decl, if any.
Definition abg-ir.cc:5923
The abstraction of the version of an ELF symbol.
Definition abg-ir.h:1230
binding
The binding of a symbol.
Definition abg-ir.h:976
type
The type of a symbol.
Definition abg-ir.h:963
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:2585
visibility
The visibility of the symbol.
Definition abg-ir.h:985
std::vector< enumerator > enumerators
Convenience typedef for a list of enumerator.
Definition abg-ir.h:2812
This is an abstraction of the set of resources necessary to manage several aspects of the internal re...
Definition abg-ir.h:216
bool decl_only_class_equals_definition() const
Getter of the "decl-only-class-equals-definition" flag.
Definition abg-ir.cc:4455
static size_t get_number_of_threads_to_use()
Getter of the number of threads to use, as set by the user.
Definition abg-ir.cc:4650
shared_ptr< parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition abg-ir.h:3190
std::vector< parameter_sptr > parameters
Convenience typedef for a vector of parameter_sptr.
Definition abg-ir.h:3193
shared_ptr< function_decl::parameter > parameter_sptr
Convenience typedef for a shared pointer on a function_decl::parameter.
Definition abg-ir.h:3439
This abstracts the global scope of a given translation unit.
Definition abg-ir.h:1991
The source location of a token.
Definition abg-ir.h:385
void expand(std::string &path, unsigned &line, unsigned &column) const
Expand the location into a tripplet path, line and column number.
Definition abg-ir.cc:738
CV
Bit field values representing the cv qualifiers of the underlying type.
Definition abg-ir.h:2259
The internal representation of an integral type.
Definition abg-ir-priv.h:69
This is the abstraction of the set of relevant artefacts (types, variable declarations,...
Definition abg-ir.h:692
language
The language of the translation unit.
Definition abg-ir.h:706
enum type_or_decl_kind kind() const
Getter for the "kind" property of type_or_decl_base type.
Definition abg-ir.cc:5056
A type used to time various part of the libabigail system.
bool stop()
Stop the timer.
bool start()
Start the timer.
This represents a queue of tasks to be performed.
tasks_type & get_completed_tasks() const
Getter of the vector of tasks that got performed.
void wait_for_workers_to_complete()
Suspends the current thread until all worker threads finish performing the tasks they are executing.
void schedule_staged_tasks()
Schedule the tasks that have been previously staged by stage_task().
bool stage_task(const task_sptr &)
Stages a task to be scheduled later.
std::vector< task_sptr > tasks_type
A convenience typedef for a vector of task_sptr.
bool schedule_task(const task_sptr &)
Submit a task to the queue of tasks to be performed.
The template of a task to be performed.
Definition abg-workers.h:70
This represents a task to be performed.
Definition abg-workers.h:48
visiting_kind operator~(visiting_kind l)
The overloaded 'bit inversion' operator for visiting_kind.
visiting_kind operator&(visiting_kind l, visiting_kind r)
The overloaded and operator for visiting_kind.
visiting_kind operator|(visiting_kind l, visiting_kind r)
The overloaded or operator for visiting_kind.
ostream & operator<<(ostream &o, diff_category c)
Serialize an instance of diff_category to an output stream.
unordered_map< dwarf_addr_pair_type, dwarf_addr_pairs_type, dwarf_addr_pair_hash > dwarf_addr_pairs_map_type
A convenience typedef for an unordered map that associates a pair of dwarf_addr_pair_type to a vector...
shared_ptr< die_parent_relations_builder_task > die_parent_relations_builder_task_sptr
A convenience typedef for the a shared_ptr of die_parent_relations_builder_task.
shared_ptr< tu_building_task_type > tu_building_task_type_sptr
A typedef of a shared pointer of tu_building_task_type.
string die_qualified_type_name(const reader &rdr, const Dwarf_Die *die, void *where, reader::tu_context_type_sptr &tu_ctxt, unordered_set< void * > &guard)
Compute the qualified name of a DIE that represents a type.
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...
string die_class_or_enum_flat_representation(const reader &rdr, const Dwarf_Die *die, const string &indent, bool one_line, bool qualified_names, void *where_addr, reader::tu_context_type_sptr &tu_ctxt, unordered_set< void * > &guard)
Compute the flat representation string of a class or enum type represented by a DIE.
unordered_map< void *, function_decl_sptr > die_function_decl_map_type
Convenience typedef for a map which key is the address of a dwarf die and which value is the correspo...
string die_pretty_print(reader &rdr, const Dwarf_Die *die, void *where_addr, reader::tu_context_type_sptr &tu_ctxt, unordered_set< void * > &guard)
Compute the pretty printed representation of an artifact represented by a DIE.
stack< scope_decl_sptr > scope_stack_type
Convenience typedef for a stack containing the scopes up to the current point in the abigail Internal...
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.
location die_location(const Dwarf_Die *die, reader::tu_context_type_sptr &tu_ctxt)
Returns the source location associated with a decl DIE.
bool is_anonymous_type_die(Dwarf_Die *die)
Test if a given DIE represents an anonymous type.
unordered_set< void * > addr_set_type
A convenience typedef for an unordered set of 'void*'.
corpus_sptr read_corpus_from_elf(const std::string &elf_path, const vector< string > &debug_info_root_paths, environment &environment, const fe_iface::options_type &options, fe_iface::status &status)
Read all abigail::translation_unit possible from the debug info accessible from an elf file,...
void(*)(reader &, Dwarf_Die, translation_unit_sptr) tu_building_fn_type
A type alias for a function type to build a translation unit.
unordered_map< dwarf_addr_pair_type, dwarf_addr_pair_set_type, dwarf_addr_pair_hash > dwarf_addr_pair_set_map_type
A convenience typedef for an unordered_map that associates a pair of dwarf_addr_type to a set of pair...
void reset_reader(elf_based_reader &rdr, const std::string &elf_path, const vector< string > &debug_info_root_path, const fe_iface::options_type &options)
Re-initialize a reader so that it can re-used to read another binary.
unordered_map< void *, imported_unit_points_type > tu_die_imported_unit_points_map_type
Convenience typedef for a vector of imported_unit_point.
unordered_map< void *, class_or_union_sptr > die_class_or_union_map_type
Convenience typedef for a map which key is the address of a dwarf die and which value is the correspo...
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< 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...
std::pair< void *, void * > dwarf_addr_pair_type
A convenience typedef for a std::pair of void*, , representing the address of a DIE.
elf_symbol_sptr create_default_fn_sym(const string &sym_name, const environment &env)
Create a function symbol with a given name.
unordered_map< void *, class_decl_sptr > die_class_map_type
Convenience typedef for a map which key is the address of a dwarf die and which value is the correspo...
phmap::flat_hash_map< void *, void * > addr_addr_phmap_type
Convenience typedef for a flat hash which key is a dwarf DIE address. The value is also a dwarf addre...
vector< void * > dwarf_addrs_type
A convenience typedef for a vector of void*, representing the address of a DIE.
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...
unordered_map< interned_string, dwarf_addrs_type, hash_interned_string > istring_dwarf_addrs_map_type
Convenience typedef for a map which is an interned_string and which value is a vector of DIE addresse...
unordered_map< void *, translation_unit_sptr > die_tu_map_type
Convenience typedef for a map which key is the address of a DW_TAG_compile_unit and the value is the ...
unordered_map< void *, type_or_decl_base_sptr > die_artefact_map_type
Convenience typedef for a map which key is the address of a dwarf die and which value is the correspo...
die_source
Where a DIE comes from. For instance, a DIE can come from the main debug info section,...
unordered_map< void *, function_type_sptr > die_function_type_map_type
Convenience typedef for a map which key is the address of a dwarf die and which value is the correspo...
vector< imported_unit_point > imported_unit_points_type
Convenience typedef for a vector of imported_unit_point.
elf_based_reader_sptr create_reader(const std::string &elf_path, const vector< string > &debug_info_root_paths, environment &environment, const fe_iface::options_type &options)
Create a dwarf::reader.
unordered_map< void *, interned_string > die_istring_map_type
Convenience typedef for a map which key is the address of a DIE and the value is the corresponding qu...
workers::simple_task< tu_building_fn_type, void, reader &, Dwarf_Die, translation_unit_sptr > tu_building_task_type
A type alias for a task to build a translation unit in its own separate thread. The task executes a f...
vector< dwarf_addr_pair_type > dwarf_addr_pairs_type
A convenience typedef for a vector of pairs of void*.
hash_t combine_hashes(hash_t val1, hash_t val2)
Combine two hash values to produce a third hash value.
Definition abg-hash.cc:172
hash_t hash(uint64_t v, uint64_t seed)
Hash an integer value and combine it with a hash previously computed.
Definition abg-hash.cc:196
shared_ptr< reference_type_def > reference_type_def_sptr
Convenience typedef for a shared pointer on a reference_type_def.
Definition abg-fwd.h:236
decl_base_sptr add_decl_to_scope(decl_base_sptr decl, scope_decl_sptr 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:9616
bool get_member_function_is_dtor(const function_decl &f)
Test whether a member function is a destructor.
Definition abg-ir.cc:7544
shared_ptr< method_type > method_type_sptr
Convenience typedef for shared pointer to method_type.
Definition abg-fwd.h:222
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:13540
void add_data_member(class_or_union_sptr cou, var_decl_sptr v, access_specifier access, bool is_laid_out, bool is_static, size_t offset_in_bits)
Add a data member to the current instance of class_or_union.
Definition abg-ir.cc:25165
shared_ptr< function_decl > function_decl_sptr
Convenience typedef for a shared pointer on a function_decl.
Definition abg-fwd.h:273
access_specifier
Access specifier for class members.
Definition abg-ir.h:915
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:15250
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:13017
bool is_type(const type_or_decl_base &tod)
Test whether a declaration is a type.
Definition abg-ir.cc:12031
weak_ptr< type_base > type_base_wptr
Convenience typedef for a weak pointer on a type_base.
Definition abg-fwd.h:129
bool has_scope(const decl_base &d)
Tests if a declaration has got a scope.
Definition abg-ir.cc:6496
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:13446
shared_ptr< elf_symbol > elf_symbol_sptr
A convenience typedef for a shared pointer to elf_symbol.
Definition abg-ir.h:924
namespace_decl_sptr is_namespace(const type_or_decl_base_sptr &d)
Tests if a declaration is a namespace declaration.
Definition abg-ir.cc:13308
bool parse_real_type(const string &type_name, real_type &type)
Parse a real type from a string.
Definition abg-ir.cc:17518
void remove_decl_from_scope(decl_base_sptr decl)
Remove a given decl from its scope.
Definition abg-ir.cc:9642
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:11392
abg_compat::optional< uint64_t > offset_t
The abstraction for a native offset.
Definition abg-ir.h:117
const ptr_to_mbr_type * is_ptr_to_mbr_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a ptr_to_mbr_type.
Definition abg-ir.cc:12941
decl_base_sptr lookup_var_decl_in_scope(const string &fqn, scope_decl_sptr skope)
Lookup a var_decl in a scope.
Definition abg-ir.cc:14166
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:13566
bool is_class_type(const type_or_decl_base &t)
Test whether a type is a class.
Definition abg-ir.cc:12395
shared_ptr< array_type_def > array_type_def_sptr
Convenience typedef for a shared pointer on a array_type_def.
Definition abg-fwd.h:245
bool is_anonymous_type(const type_base *t)
Test whether a declaration is a type.
Definition abg-ir.cc:12080
void bind_function_type_life_time(const function_type_sptr &fn_type, translation_unit_sptr tu)
Bind the life time of a function type to the file time of a given translation unit.
Definition abg-ir.cc:31838
type_base_sptr peel_const_qualified_type(const qualified_type_def_sptr &q)
If a qualified type is const, then return its underlying type.
Definition abg-ir.cc:8426
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:15113
void set_member_function_is_dtor(function_decl &f, bool d)
Set the destructor-ness property of a member function.
Definition abg-ir.cc:7574
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:12626
shared_ptr< class_decl > class_decl_sptr
Convenience typedef for a shared pointer on a class_decl.
Definition abg-fwd.h:194
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:7632
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:8007
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:12139
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:13088
void set_member_access_specifier(decl_base &d, access_specifier a)
Sets the access specifier for a class member.
Definition abg-ir.cc:6653
const class_decl * is_compatible_with_class_type(const type_base *t)
Test if a type is a class. This function looks through typedefs.
Definition abg-ir.cc:12348
typedef_decl_sptr is_typedef(const type_or_decl_base_sptr t)
Test whether a type is a typedef.
Definition abg-ir.cc:12241
abg_compat::optional< uint64_t > hash_t
The abstraction for an 8 bytes hash value.
Definition abg-ir.h:109
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:15298
shared_ptr< function_type > function_type_sptr
Convenience typedef for a shared pointer on a function_type.
Definition abg-fwd.h:211
shared_ptr< typedef_decl > typedef_decl_sptr
Convenience typedef for a shared pointer on a typedef_decl.
Definition abg-fwd.h:168
bool is_typedef_of_maybe_qualified_class_or_union_type(const type_base *t)
Test if a type is a typedef of a class or union type, or a typedef of a qualified class or union type...
Definition abg-ir.cc:12844
reference_type_def * is_reference_type(type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a reference_type_def.
Definition abg-ir.cc:12881
bool is_cplus_plus_language(translation_unit::language l)
Test if a language enumerator designates the C++ language.
Definition abg-ir.cc:2329
void perform_type_canonicalization(vector< type_base_sptr > &types, bool do_log, bool show_stats)
Hash and canonicalize a sequence of types.
Definition abg-ir.cc:31859
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:12330
shared_ptr< var_decl > var_decl_sptr
Convenience typedef for a shared pointer on a var_decl.
Definition abg-fwd.h:257
shared_ptr< ptr_to_mbr_type > ptr_to_mbr_type_sptr
Convenience typedef for a shared pointer to a ptr_to_mbr_type.
Definition abg-fwd.h:240
shared_ptr< scope_decl > scope_decl_sptr
Convenience typedef for a shared pointer on a scope_decl.
Definition abg-fwd.h:265
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:118
shared_ptr< translation_unit > translation_unit_sptr
Convenience typedef for a shared pointer on a translation_unit type.
Definition abg-fwd.h:137
string build_qualified_name(const scope_decl_sptr scope, const string &name)
Build and return a qualified name from a name and its scope.
Definition abg-ir.cc:9934
bool is_java_language(translation_unit::language l)
Test if a language enumerator designates the Java language.
Definition abg-ir.cc:2345
shared_ptr< pointer_type_def > pointer_type_def_sptr
Convenience typedef for a shared pointer on a pointer_type_def.
Definition abg-fwd.h:227
bool is_const_qualified_type(const qualified_type_def_sptr &t)
Test if a given qualified type is const.
Definition abg-ir.cc:8394
bool is_member_function(const function_decl &f)
Test whether a function_decl is a member function.
Definition abg-ir.cc:7454
var_decl * is_var_decl(const type_or_decl_base *tod)
Tests if a declaration is a variable declaration.
Definition abg-ir.cc:13289
bool is_c_language(translation_unit::language l)
Test if a language enumerator designates the C language.
Definition abg-ir.cc:2313
decl_base * is_decl(const type_or_decl_base *d)
Test if an ABI artifact is a declaration.
Definition abg-ir.cc:11971
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:26879
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:30769
shared_ptr< enum_type_decl > enum_type_decl_sptr
Convenience typedef for shared pointer to a enum_type_decl.
Definition abg-fwd.h:176
void set_member_function_virtuality(const function_decl_sptr &fn, bool is_virtual, ssize_t voffset)
Set the virtual-ness of a member fcuntion.
Definition abg-ir.cc:7820
bool get_member_function_is_virtual(const function_decl &f)
Test if a given member function is virtual.
Definition abg-ir.cc:7742
const pointer_type_def * is_pointer_type(const type_or_decl_base *t, bool look_through_qualifiers)
Test whether a type is a pointer_type_def.
Definition abg-ir.cc:12709
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:13148
bool is_union_type(const type_or_decl_base &t)
Test if a type is a union_decl.
Definition abg-ir.cc:12675
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:15068
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:13424
bool is_global_scope(const scope_decl &scope)
Tests whether if a given scope is the global scope.
Definition abg-ir.cc:11737
bool is_data_member(const var_decl &v)
Test if a var_decl is a data member.
Definition abg-ir.cc:6722
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:14999
const decl_base * get_type_declaration(const type_base *t)
Get the declaration for a given type.
Definition abg-ir.cc:11414
void set_member_is_static(decl_base &d, bool s)
Sets the static-ness property of a class member.
Definition abg-ir.cc:28108
array_type_def * is_array_type(const type_or_decl_base *type, bool look_through_qualifiers)
Test if a type is an array_type_def.
Definition abg-ir.cc:13353
unordered_set< type_base_sptr, shallow_type_hasher, shallow_type_eq > type_sptr_set_type
Convenience typedef for a set of type_base_sptr.
Definition abg-ir.h:194
shared_ptr< type_decl > type_decl_sptr
Convenience typedef for a shared pointer on a type_decl.
Definition abg-fwd.h:162
unordered_set< type_base_wptr, shallow_type_hasher, shallow_type_eq > type_wptr_set_type
Convenience typedef for a set of type_base_wptr.
Definition abg-ir.h:199
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:8130
shared_ptr< namespace_decl > namespace_decl_sptr
Convenience typedef for a shared pointer on namespace_decl.
Definition abg-fwd.h:288
bool is_ada_language(translation_unit::language l)
Test if a language enumerator designates the Ada language.
Definition abg-ir.cc:2354
string demangle_cplus_mangled_name(const string &mangled_name)
Demangle a C++ mangled name and return the resulting string.
Definition abg-ir.cc:16227
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:8764
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:15411
function_decl * is_function_decl(const type_or_decl_base *d)
Test whether a declaration is a function_decl.
Definition abg-ir.cc:11919
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:13118
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:13068
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:13746
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:13178
bool is_member_decl(const decl_base_sptr d)
Tests if a declaration is a class member.
Definition abg-ir.cc:6514
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:7513
bool is_function_suppressed(const fe_iface &fe, const string &fn_name, const string &fn_linkage_name, bool require_drop_property)
Test if a function is matched by at least one suppression specification associated with a given front...
bool is_type_suppressed(const fe_iface &fe, const string &type_name, const location &type_location, bool &type_is_opaque, bool require_drop_property)
Test if a type is matched by at least one suppression specification associated with a given front-end...
bool is_variable_suppressed(const fe_iface &fe, const string &var_name, const string &var_linkage_name, bool require_drop_property)
Test if a variable is matched by at least one suppression specification associated with a given front...
bool normalize_litterals(string &str)
Normalize the numerical litteral in a string.
bool remove_white_spaces(string &str)
Remove white spaces from a string.
const char * get_anonymous_enum_internal_name_prefix()
Getter of the prefix for the name of anonymous enums.
const char * get_anonymous_struct_internal_name_prefix()
Getter of the prefix for the name of anonymous structs.
const char * get_anonymous_union_internal_name_prefix()
Getter of the prefix for the name of anonymous unions.
bool string_is_ascii_identifier(const string &str)
Test if a string is made of ascii characters which are identifiers acceptable in C or C++ programs.
Toplevel namespace for libabigail.
bool operator==(const std::string &l, const interned_string &r)
Equality operator.
Definition abg-ir.cc:341
std::string operator+(const interned_string &s1, const std::string &s2)
Concatenation operator.
Definition abg-ir.cc:375
The generic options that control the behaviour of all Front-End interfaces.
bool load_undefined_interfaces
If this option is set to true, then the functions and variables that have an undefined symbol are goi...
A functor to hash instances of interned_string.